From f845877ee6a0e94b657dd97887415553ea3e4c8f Mon Sep 17 00:00:00 2001 From: Jacek Malec <145967538+jacek-casper@users.noreply.github.com> Date: Fri, 26 Jul 2024 11:36:31 +0100 Subject: [PATCH] Handle purse not found error --- Cargo.lock | 4 +-- Cargo.toml | 4 +-- rpc_sidecar/src/node_client.rs | 3 ++ rpc_sidecar/src/rpcs/error.rs | 3 +- rpc_sidecar/src/rpcs/error_code.rs | 7 +++-- rpc_sidecar/src/rpcs/state.rs | 47 ++++++++++++++++++++++++++++-- 6 files changed, 59 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3cebb932..d1c2bd41 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -471,7 +471,7 @@ dependencies = [ [[package]] name = "casper-binary-port" version = "1.0.0" -source = "git+https://github.com/casper-network/casper-node.git?branch=feat-2.0#2d6c295ae88efdfef10a1297d8845bdfe19fd774" +source = "git+https://github.com/jacek-casper/casper-node.git?branch=purse-not-found-error-code#711657e444bb0fe963ac4ce5cf517d14cc286706" dependencies = [ "bincode", "bytes", @@ -673,7 +673,7 @@ dependencies = [ [[package]] name = "casper-types" version = "5.0.0" -source = "git+https://github.com/casper-network/casper-node.git?branch=feat-2.0#2d6c295ae88efdfef10a1297d8845bdfe19fd774" +source = "git+https://github.com/jacek-casper/casper-node.git?branch=purse-not-found-error-code#711657e444bb0fe963ac4ce5cf517d14cc286706" dependencies = [ "base16", "base64 0.13.1", diff --git a/Cargo.toml b/Cargo.toml index 0c64330a..1c565139 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,8 @@ members = [ anyhow = "1" async-stream = "0.3.4" async-trait = "0.1.77" -casper-types = { git = "https://github.com/casper-network/casper-node.git", branch = "feat-2.0" } -casper-binary-port = { git = "https://github.com/casper-network/casper-node.git", branch = "feat-2.0" } +casper-types = { git = "https://github.com/jacek-casper/casper-node.git", branch = "purse-not-found-error-code" } +casper-binary-port = { git = "https://github.com/jacek-casper/casper-node.git", branch = "purse-not-found-error-code" } casper-event-sidecar = { path = "./event_sidecar", version = "1.0.0" } casper-event-types = { path = "./types", version = "1.0.0" } casper-rpc-sidecar = { path = "./rpc_sidecar", version = "1.0.0" } diff --git a/rpc_sidecar/src/node_client.rs b/rpc_sidecar/src/node_client.rs index 722fad8c..79817520 100644 --- a/rpc_sidecar/src/node_client.rs +++ b/rpc_sidecar/src/node_client.rs @@ -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})")] @@ -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 diff --git a/rpc_sidecar/src/rpcs/error.rs b/rpc_sidecar/src/rpcs/error.rs index 9444bf57..c8fbfbb6 100644 --- a/rpc_sidecar/src/rpcs/error.rs +++ b/rpc_sidecar/src/rpcs/error.rs @@ -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), diff --git a/rpc_sidecar/src/rpcs/error_code.rs b/rpc_sidecar/src/rpcs/error_code.rs index 085c08d5..d086f14e 100644 --- a/rpc_sidecar/src/rpcs/error_code.rs +++ b/rpc_sidecar/src/rpcs/error_code.rs @@ -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. @@ -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 for (i64, &'static str) { @@ -85,7 +87,7 @@ impl From 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"), @@ -109,6 +111,7 @@ impl From for (i64, &'static str) { error_code as i64, "Cannot serve rewards stored in V1 format", ), + ErrorCode::PurseNotFound => (error_code as i64, "Purse not found"), } } } diff --git a/rpc_sidecar/src/rpcs/state.rs b/rpc_sidecar/src/rpcs/state.rs index 94f7ee23..aeee966e 100644 --- a/rpc_sidecar/src/rpcs/state.rs +++ b/rpc_sidecar/src/rpcs/state.rs @@ -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}, @@ -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 { @@ -2387,4 +2404,30 @@ mod tests { } } } + + struct BalancePurseNotFoundMock; + + #[async_trait] + impl NodeClient for BalancePurseNotFoundMock { + async fn send_request( + &self, + req: BinaryRequest, + ) -> Result { + 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), + } + } + } }