From e5a525be359531ca638b11a34a01f7ba686bd827 Mon Sep 17 00:00:00 2001 From: Karan Dhareshwar Date: Tue, 2 May 2023 08:54:41 -0500 Subject: [PATCH 1/3] Update era response validation logic --- Cargo.toml | 8 ++++---- lib/deploy.rs | 6 +++--- lib/error.rs | 5 +++-- lib/keygen.rs | 3 +-- lib/lib.rs | 6 ++++-- lib/parsing.rs | 7 ++----- lib/rpc.rs | 5 ++--- lib/validation.rs | 8 ++++++-- src/common.rs | 1 - src/get_dictionary_item.rs | 1 - src/query_global_state.rs | 1 - tests/integration_test.rs | 39 +++++++++++++++++++++++--------------- 12 files changed, 49 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2d9c9298..cb8152d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,10 +24,10 @@ doc = false async-trait = "0.1.51" base16 = "0.2.1" base64 = "0.13.0" -casper-execution-engine = "=3.0.0" -casper-node = "=1.4.13" -casper-hashing = "=1.4.3" -casper-types = "=1.5.0" +casper-execution-engine = "3.1.0" +casper-node = "1.4.15" +casper-hashing = "1.4.3" +casper-types = {version = "1.6.0", features = ["std"]} clap = "2" humantime = "2" jsonrpc-lite = "0.5.0" diff --git a/lib/deploy.rs b/lib/deploy.rs index cc8e8bd8..5022903c 100644 --- a/lib/deploy.rs +++ b/lib/deploy.rs @@ -9,11 +9,11 @@ use std::{ use casper_execution_engine::core::engine_state::ExecutableDeployItem; use casper_node::{ rpcs::{account::PutDeploy, chain::GetBlockResult, info::GetDeploy, RpcWithParams}, - types::{Deploy, DeployHash, TimeDiff, Timestamp}, + types::{Deploy, DeployHash}, }; use casper_types::{ account::AccountHash, AsymmetricType, ProtocolVersion, PublicKey, RuntimeArgs, SecretKey, - UIntParseError, URef, U512, + TimeDiff, Timestamp, UIntParseError, URef, U512, }; use crate::{ @@ -339,7 +339,7 @@ impl DeployExt for Deploy { mod tests { use std::convert::TryInto; - use casper_node::{crypto::AsymmetricKeyExt, types::ExcessiveSizeDeployError}; + use casper_node::types::ExcessiveSizeDeployError; use super::*; use crate::{DeployStrParams, PaymentStrParams, SessionStrParams}; diff --git a/lib/error.rs b/lib/error.rs index daec5ece..8718f347 100644 --- a/lib/error.rs +++ b/lib/error.rs @@ -4,9 +4,10 @@ use humantime::{DurationError, TimestampError}; use jsonrpc_lite::JsonRpc; use thiserror::Error; -use casper_node::{crypto::Error as CryptoError, types::ExcessiveSizeDeployError}; +use casper_node::types::ExcessiveSizeDeployError; use casper_types::{ - bytesrepr::Error as ToBytesError, CLValueError, UIntParseError, URefFromStrError, + bytesrepr::Error as ToBytesError, crypto::ErrorExt as CryptoError, CLValueError, + UIntParseError, URefFromStrError, }; use crate::validation::ValidateResponseError; diff --git a/lib/keygen.rs b/lib/keygen.rs index 34d57718..4584df0b 100644 --- a/lib/keygen.rs +++ b/lib/keygen.rs @@ -2,8 +2,7 @@ use std::{fs, path::Path}; -use casper_node::crypto::AsymmetricKeyExt; -use casper_types::{AsymmetricType, PublicKey, SecretKey}; +use casper_types::{crypto::SecretKey, AsymmetricType, PublicKey}; use crate::error::{Error, Result}; diff --git a/lib/lib.rs b/lib/lib.rs index 8f15c562..17af2998 100644 --- a/lib/lib.rs +++ b/lib/lib.rs @@ -1551,7 +1551,7 @@ mod param_tests { #[test] fn should_fail_to_convert_with_bad_dependencies() { - use casper_node::crypto::Error as CryptoError; + use casper_types::crypto::ErrorExt as CryptoError; let mut params = test_value(); params.dependencies = vec!["invalid dep"]; let result: StdResult = params.try_into(); @@ -1559,7 +1559,9 @@ mod param_tests { result, Err(Error::CryptoError { context: "dependencies", - error: CryptoError::FromHex(base16::DecodeError::InvalidLength { length: 11 }) + error: CryptoError::CryptoError(casper_types::crypto::Error::FromHex( + base16::DecodeError::InvalidLength { length: 11 } + )) }) )); } diff --git a/lib/parsing.rs b/lib/parsing.rs index 7b3fdee1..adbf5d43 100644 --- a/lib/parsing.rs +++ b/lib/parsing.rs @@ -7,13 +7,10 @@ use serde::{self, Deserialize}; use casper_execution_engine::core::engine_state::executable_deploy_item::ExecutableDeployItem; use casper_hashing::Digest; -use casper_node::{ - crypto::AsymmetricKeyExt, - types::{DeployHash, TimeDiff, Timestamp}, -}; +use casper_node::types::DeployHash; use casper_types::{ bytesrepr, AsymmetricType, CLType, CLValue, HashAddr, Key, NamedArg, PublicKey, RuntimeArgs, - SecretKey, UIntParseError, U512, + SecretKey, TimeDiff, Timestamp, UIntParseError, U512, }; use crate::{ diff --git a/lib/rpc.rs b/lib/rpc.rs index d14e49d6..21621b26 100644 --- a/lib/rpc.rs +++ b/lib/rpc.rs @@ -10,7 +10,6 @@ use serde_json::{json, Map, Value}; use casper_execution_engine::core::engine_state::ExecutableDeployItem; use casper_hashing::Digest; use casper_node::{ - crypto, rpcs::{ account::{PutDeploy, PutDeployParams}, chain::{ @@ -28,7 +27,7 @@ use casper_node::{ }, types::{BlockHash, Deploy, DeployHash}, }; -use casper_types::{AsymmetricType, Key, PublicKey, URef}; +use casper_types::{crypto, AsymmetricType, Key, PublicKey, URef}; use crate::{ deploy::{DeployExt, DeployParams, SendDeploy, Transfer}, @@ -414,7 +413,7 @@ pub fn map_hashing_error(hashing_error: casper_hashing::Error) -> impl Fn(&'stat move |context: &'static str| match &hashing_error { casper_hashing::Error::Base16DecodeError(decode_error) => Error::CryptoError { context, - error: crypto::Error::FromHex(decode_error.clone()), + error: crypto::Error::FromHex(decode_error.clone()).into(), }, casper_hashing::Error::IncorrectDigestLength(length) => Error::InvalidArgument { context, diff --git a/lib/validation.rs b/lib/validation.rs index 8917b3b0..cbae8ca1 100644 --- a/lib/validation.rs +++ b/lib/validation.rs @@ -16,7 +16,7 @@ use casper_node::{ json_compatibility, Block, BlockHeader, BlockValidationError, JsonBlock, JsonBlockHeader, }, }; -use casper_types::{bytesrepr, Key, StoredValue, U512}; +use casper_types::{bytesrepr, Key, ProtocolVersion, StoredValue, U512}; const GET_ITEM_RESULT_BALANCE_VALUE: &str = "balance_value"; const GET_ITEM_RESULT_STORED_VALUE: &str = "stored_value"; @@ -100,7 +100,11 @@ pub(crate) fn validate_get_era_info_response( .map_err(|_| ValidateResponseError::ValidateResponseFailedToParse)?; let proofs: Vec> = bytesrepr::deserialize(proof_bytes)?; - let key = Key::EraInfo(era_id); + let key = if result.api_version < ProtocolVersion::from_parts(1u32, 4u32, 14u32) { + Key::EraSummary + } else { + Key::EraInfo(era_id) + }; let path = &[]; let proof_value = match stored_value { diff --git a/src/common.rs b/src/common.rs index 473e2c55..1cf948e4 100644 --- a/src/common.rs +++ b/src/common.rs @@ -198,7 +198,6 @@ pub mod block_identifier { /// Internal module to handle providing the arg for and retrieval of the public key or session /// account. mod sealed_public_key { - use casper_node::crypto::AsymmetricKeyExt; use casper_types::AsymmetricType; use super::*; diff --git a/src/get_dictionary_item.rs b/src/get_dictionary_item.rs index fcf4ebb5..dc6edaed 100644 --- a/src/get_dictionary_item.rs +++ b/src/get_dictionary_item.rs @@ -25,7 +25,6 @@ enum DisplayOrder { /// Handles providing the arg for and retrieval of the key. mod key { - use casper_node::crypto::AsymmetricKeyExt; use casper_types::AsymmetricType; use super::*; diff --git a/src/query_global_state.rs b/src/query_global_state.rs index 480be2db..4104d201 100644 --- a/src/query_global_state.rs +++ b/src/query_global_state.rs @@ -69,7 +69,6 @@ mod block_hash { /// Handles providing the arg for and retrieval of the key. mod key { - use casper_node::crypto::AsymmetricKeyExt; use casper_types::{AsymmetricType, PublicKey}; use super::*; diff --git a/tests/integration_test.rs b/tests/integration_test.rs index d42af4c7..362015bb 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -13,16 +13,14 @@ use casper_client::{ DeployStrParams, DictionaryItemStrParams, Error, GlobalStateStrParams, PaymentStrParams, SessionStrParams, }; -use casper_node::{ - crypto::Error as CryptoError, - rpcs::{ - account::{PutDeploy, PutDeployParams}, - chain::{GetStateRootHash, GetStateRootHashParams}, - info::{GetDeploy, GetDeployParams}, - state::{GetBalance, GetBalanceParams, GetDictionaryItem, GetDictionaryItemParams}, - RpcWithOptionalParams, RpcWithParams, - }, +use casper_node::rpcs::{ + account::{PutDeploy, PutDeployParams}, + chain::{GetStateRootHash, GetStateRootHashParams}, + info::{GetDeploy, GetDeployParams}, + state::{GetBalance, GetBalanceParams, GetDictionaryItem, GetDictionaryItemParams}, + RpcWithOptionalParams, RpcWithParams, }; +use casper_types::crypto::ErrorExt as CryptoError; const VALID_PURSE_UREF: &str = "uref-0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20-007"; @@ -593,7 +591,9 @@ mod get_item { .await, Err(Error::CryptoError { context: "state_root_hash", - error: CryptoError::FromHex(base16::DecodeError::InvalidLength { length: 25 }) + error: CryptoError::CryptoError(casper_types::crypto::Error::FromHex( + base16::DecodeError::InvalidLength { length: 25 } + )) }) )); } @@ -618,13 +618,16 @@ mod get_item { .await, Err(Error::CryptoError { context: "state_root_hash", - error: CryptoError::FromHex(base16::DecodeError::InvalidLength { length: 25 }) + error: CryptoError::CryptoError(casper_types::crypto::Error::FromHex( + base16::DecodeError::InvalidLength { length: 25 } + )) }) )); } } mod get_dictionary_item { + use super::*; #[tokio::test(flavor = "multi_thread")] @@ -720,7 +723,9 @@ mod get_dictionary_item { .await, Err(Error::CryptoError { context: "state_root_hash", - error: CryptoError::FromHex(base16::DecodeError::InvalidLength { length: _ }) + error: CryptoError::CryptoError(casper_types::crypto::Error::FromHex( + base16::DecodeError::InvalidLength { length: _ } + )) }) )); } @@ -768,7 +773,9 @@ mod query_global_state { .await, Err(Error::CryptoError { context: "global_state_identifier", - error: CryptoError::FromHex(base16::DecodeError::InvalidLength { length: _ }) + error: CryptoError::CryptoError(casper_types::crypto::Error::FromHex( + base16::DecodeError::InvalidLength { length: _ } + )) }) )); } @@ -803,7 +810,9 @@ mod query_global_state { .await, Err(Error::CryptoError { context: "global_state_identifier", - error: CryptoError::FromHex(base16::DecodeError::InvalidLength { length: _ }), + error: CryptoError::CryptoError(casper_types::crypto::Error::FromHex( + base16::DecodeError::InvalidLength { length: _ } + )) }) )); } @@ -1371,7 +1380,7 @@ mod put_deploy { mod rate_limit { use super::*; - use casper_node::types::Timestamp; + use casper_types::Timestamp; #[tokio::test(flavor = "multi_thread")] async fn client_should_should_be_rate_limited_to_approx_1_qps() { From ae9c468ed98ca1759ca9a737a9c6e507205f6382 Mon Sep 17 00:00:00 2001 From: Karan Dhareshwar Date: Mon, 8 May 2023 08:34:45 -0500 Subject: [PATCH 2/3] Address PR feedback --- lib/validation.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/validation.rs b/lib/validation.rs index cbae8ca1..6cc13fe5 100644 --- a/lib/validation.rs +++ b/lib/validation.rs @@ -16,7 +16,7 @@ use casper_node::{ json_compatibility, Block, BlockHeader, BlockValidationError, JsonBlock, JsonBlockHeader, }, }; -use casper_types::{bytesrepr, Key, ProtocolVersion, StoredValue, U512}; +use casper_types::{bytesrepr, Key, StoredValue, U512}; const GET_ITEM_RESULT_BALANCE_VALUE: &str = "balance_value"; const GET_ITEM_RESULT_STORED_VALUE: &str = "stored_value"; @@ -100,11 +100,6 @@ pub(crate) fn validate_get_era_info_response( .map_err(|_| ValidateResponseError::ValidateResponseFailedToParse)?; let proofs: Vec> = bytesrepr::deserialize(proof_bytes)?; - let key = if result.api_version < ProtocolVersion::from_parts(1u32, 4u32, 14u32) { - Key::EraSummary - } else { - Key::EraInfo(era_id) - }; let path = &[]; let proof_value = match stored_value { @@ -114,8 +109,15 @@ pub(crate) fn validate_get_era_info_response( _ => return Err(ValidateResponseError::ValidateResponseFailedToParse), }; - core::validate_query_proof(&state_root_hash, &proofs, &key, path, &proof_value) - .map_err(Into::into) + match core::validate_query_proof(&state_root_hash, &proofs, &Key::EraSummary, path, &proof_value) { + Ok(_) => {Ok(())} + Err(_) => { + let key = Key::EraInfo(era_id); + core::validate_query_proof(&state_root_hash, &proofs, &key, path, &proof_value) + .map_err(Into::into) + } + } + } None => Ok(()), } From c9cee943c2cb49197e694faf30ae38aab39783d9 Mon Sep 17 00:00:00 2001 From: Karan Dhareshwar Date: Mon, 8 May 2023 08:36:38 -0500 Subject: [PATCH 3/3] Run cargo fmt --- lib/validation.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/validation.rs b/lib/validation.rs index 6cc13fe5..5ea2058a 100644 --- a/lib/validation.rs +++ b/lib/validation.rs @@ -109,15 +109,20 @@ pub(crate) fn validate_get_era_info_response( _ => return Err(ValidateResponseError::ValidateResponseFailedToParse), }; - match core::validate_query_proof(&state_root_hash, &proofs, &Key::EraSummary, path, &proof_value) { - Ok(_) => {Ok(())} + match core::validate_query_proof( + &state_root_hash, + &proofs, + &Key::EraSummary, + path, + &proof_value, + ) { + Ok(_) => Ok(()), Err(_) => { let key = Key::EraInfo(era_id); core::validate_query_proof(&state_root_hash, &proofs, &key, path, &proof_value) .map_err(Into::into) } } - } None => Ok(()), }