Skip to content

Commit

Permalink
Handle purse not found error (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacek-casper authored Jul 26, 2024
1 parent 055e6d0 commit 476600b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rpc_sidecar/src/node_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,8 @@ pub enum Error {
SwitchBlockParentNotFound,
#[error("cannot serve rewards stored in V1 format")]
UnsupportedRewardsV1Request,
#[error("purse was not found for given identifier")]
PurseNotFound,
#[error("received a response with an unsupported protocol version: {0}")]
UnsupportedProtocolVersion(ProtocolVersion),
#[error("received an unexpected node error: {message} ({code})")]
Expand All @@ -645,6 +647,7 @@ impl Error {
Ok(ErrorCode::SwitchBlockParentNotFound) => Self::SwitchBlockParentNotFound,
Ok(ErrorCode::UnsupportedRewardsV1Request) => Self::UnsupportedRewardsV1Request,
Ok(ErrorCode::BinaryProtocolVersionMismatch) => Self::BinaryProtocolVersionMismatch,
Ok(ErrorCode::PurseNotFound) => Self::PurseNotFound,
Ok(
err @ (ErrorCode::InvalidDeployChainName
| ErrorCode::InvalidDeployDependenciesNoLongerSupported
Expand Down
3 changes: 2 additions & 1 deletion rpc_sidecar/src/rpcs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ impl Error {
Error::NodeRequest(_, NodeClientError::UnsupportedRewardsV1Request) => {
Some(ErrorCode::UnsupportedRewardsV1Request)
}
Error::NodeRequest(_, NodeClientError::PurseNotFound) => Some(ErrorCode::PurseNotFound),
Error::InvalidPurseURef(_) => Some(ErrorCode::FailedToParseGetBalanceURef),
Error::InvalidDictionaryKey(_) => Some(ErrorCode::FailedToParseQueryKey),
Error::MainPurseNotFound => Some(ErrorCode::NoSuchMainPurse),
Error::MainPurseNotFound => Some(ErrorCode::NoMainPurse),
Error::AccountNotFound => Some(ErrorCode::NoSuchAccount),
Error::AddressableEntityNotFound => Some(ErrorCode::NoSuchAddressableEntity),
Error::RewardNotFound => Some(ErrorCode::NoRewardsFound),
Expand Down
7 changes: 5 additions & 2 deletions rpc_sidecar/src/rpcs/error_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub enum ErrorCode {
/// The requested state root hash was not found.
NoSuchStateRoot = -32012,
/// The main purse for a given account hash does not exist.
NoSuchMainPurse = -32013,
NoMainPurse = -32013,
/// The requested Transaction was not found.
NoSuchTransaction = -32014,
/// Variant mismatch.
Expand All @@ -61,6 +61,8 @@ pub enum ErrorCode {
SwitchBlockParentNotFound = -32024,
/// Cannot serve rewards stored in V1 format
UnsupportedRewardsV1Request = -32025,
/// Purse was not found for given identifier.
PurseNotFound = -32026,
}

impl From<ErrorCode> for (i64, &'static str) {
Expand All @@ -85,7 +87,7 @@ impl From<ErrorCode> for (i64, &'static str) {
}
ErrorCode::FailedToGetTrie => (error_code as i64, "Failed to get trie"),
ErrorCode::NoSuchStateRoot => (error_code as i64, "No such state root"),
ErrorCode::NoSuchMainPurse => (error_code as i64, "Failed to get main purse"),
ErrorCode::NoMainPurse => (error_code as i64, "Failed to get main purse"),
ErrorCode::NoSuchTransaction => (error_code as i64, "No such transaction"),
ErrorCode::VariantMismatch => (error_code as i64, "Variant mismatch internal error"),
ErrorCode::InvalidTransaction => (error_code as i64, "Invalid transaction"),
Expand All @@ -109,6 +111,7 @@ impl From<ErrorCode> for (i64, &'static str) {
error_code as i64,
"Cannot serve rewards stored in V1 format",
),
ErrorCode::PurseNotFound => (error_code as i64, "Purse not found"),
}
}
}
Expand Down
47 changes: 45 additions & 2 deletions rpc_sidecar/src/rpcs/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,8 +1147,8 @@ mod tests {
use crate::{rpcs::ErrorCode, ClientError, SUPPORTED_PROTOCOL_VERSION};
use casper_binary_port::{
BalanceResponse, BinaryRequest, BinaryResponse, BinaryResponseAndRequest,
DictionaryQueryResult, GetRequest, GlobalStateQueryResult, GlobalStateRequest,
InformationRequestTag, KeyPrefix,
DictionaryQueryResult, ErrorCode as BinaryErrorCode, GetRequest, GlobalStateQueryResult,
GlobalStateRequest, InformationRequestTag, KeyPrefix,
};
use casper_types::{
addressable_entity::{MessageTopics, NamedKeyValue, NamedKeys},
Expand Down Expand Up @@ -1233,6 +1233,23 @@ mod tests {
);
}

#[tokio::test]
async fn should_handle_balance_not_found() {
let rng = &mut TestRng::new();

let err = GetBalance::do_handle_request(
Arc::new(BalancePurseNotFoundMock),
GetBalanceParams {
state_root_hash: rng.gen(),
purse_uref: URef::new(rng.gen(), AccessRights::empty()).to_formatted_string(),
},
)
.await
.expect_err("should fail request");

assert_eq!(err.code(), ErrorCode::PurseNotFound as i64);
}

#[tokio::test]
async fn should_read_auction_info() {
struct ClientMock {
Expand Down Expand Up @@ -2387,4 +2404,30 @@ mod tests {
}
}
}

struct BalancePurseNotFoundMock;

#[async_trait]
impl NodeClient for BalancePurseNotFoundMock {
async fn send_request(
&self,
req: BinaryRequest,
) -> Result<BinaryResponseAndRequest, ClientError> {
match req {
BinaryRequest::Get(GetRequest::State(req))
if matches!(&*req, GlobalStateRequest::Balance { .. }) =>
{
Ok(BinaryResponseAndRequest::new(
BinaryResponse::new_error(
BinaryErrorCode::PurseNotFound,
SUPPORTED_PROTOCOL_VERSION,
),
&[],
0,
))
}
req => unimplemented!("unexpected request: {:?}", req),
}
}
}
}

0 comments on commit 476600b

Please sign in to comment.