Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
4817: Add an error code for purse not found for balance requests r=jacek-casper a=jacek-casper

We currently return a generic query error (we did the same in 1.5), but bitvavo have asked for a dedicated error for this

Co-authored-by: Jacek Malec <[email protected]>
  • Loading branch information
casperlabs-bors-ng[bot] and jacek-casper authored Jul 26, 2024
2 parents 488a5f9 + 341f70c commit c58bccf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions binary_port/src/error_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ pub enum ErrorCode {
/// Received V1 Transaction for spec exec.
#[error("received v1 transaction for speculative execution")]
ReceivedV1Transaction = 86,
/// Purse was not found for given identifier.
#[error("purse was not found for given identifier")]
PurseNotFound = 87,
}

impl TryFrom<u16> for ErrorCode {
Expand Down Expand Up @@ -371,6 +374,7 @@ impl TryFrom<u16> for ErrorCode {
84 => Ok(ErrorCode::InvalidTransactionInvalidTransactionKind),
85 => Ok(ErrorCode::GasPriceToleranceTooLow),
86 => Ok(ErrorCode::ReceivedV1Transaction),
87 => Ok(ErrorCode::PurseNotFound),
_ => Err(UnknownErrorCode),
}
}
Expand Down
7 changes: 6 additions & 1 deletion node/src/components/binary_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use casper_storage::{
},
global_state::trie::TrieRaw,
system::auction,
tracking_copy::TrackingCopyError,
KeyPrefix as StorageKeyPrefix,
};
use casper_types::{
Expand Down Expand Up @@ -671,7 +672,11 @@ where
};
BinaryResponse::from_value(response, protocol_version)
}
BalanceResult::Failure(_) => {
BalanceResult::Failure(TrackingCopyError::KeyNotFound(_)) => {
BinaryResponse::new_error(ErrorCode::PurseNotFound, protocol_version)
}
BalanceResult::Failure(error) => {
debug!(%error, "failed when querying for a balance");
BinaryResponse::new_error(ErrorCode::FailedQuery, protocol_version)
}
}
Expand Down
24 changes: 24 additions & 0 deletions node/src/reactor/main_reactor/tests/binary_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ async fn binary_port_component_handles_all_requests() {
try_accept_transaction_invalid(&mut rng),
try_accept_transaction(&secret_signing_key),
get_balance(state_root_hash, test_account_hash),
get_balance_account_not_found(state_root_hash),
get_balance_purse_uref_not_found(state_root_hash),
get_named_keys_by_prefix(state_root_hash, test_entity_addr),
get_reward(
Some(EraIdentifier::Era(ERA_ONE)),
Expand Down Expand Up @@ -898,6 +900,28 @@ fn get_balance(state_root_hash: Digest, account_hash: AccountHash) -> TestCase {
}
}

fn get_balance_account_not_found(state_root_hash: Digest) -> TestCase {
TestCase {
name: "get_balance_account_not_found",
request: BinaryRequest::Get(GetRequest::State(Box::new(GlobalStateRequest::Balance {
state_identifier: Some(GlobalStateIdentifier::StateRootHash(state_root_hash)),
purse_identifier: PurseIdentifier::Account(AccountHash([9; 32])),
}))),
asserter: Box::new(|response| response.error_code() == ErrorCode::PurseNotFound as u16),
}
}

fn get_balance_purse_uref_not_found(state_root_hash: Digest) -> TestCase {
TestCase {
name: "get_balance_purse_uref_not_found",
request: BinaryRequest::Get(GetRequest::State(Box::new(GlobalStateRequest::Balance {
state_identifier: Some(GlobalStateIdentifier::StateRootHash(state_root_hash)),
purse_identifier: PurseIdentifier::Purse(URef::new([9; 32], Default::default())),
}))),
asserter: Box::new(|response| response.error_code() == ErrorCode::PurseNotFound as u16),
}
}

fn get_named_keys_by_prefix(state_root_hash: Digest, entity_addr: EntityAddr) -> TestCase {
TestCase {
name: "get_named_keys_by_prefix",
Expand Down

0 comments on commit c58bccf

Please sign in to comment.