From d58f08ac353421a2e9492e78f8c1df78078defe1 Mon Sep 17 00:00:00 2001 From: Xiangyi Zheng Date: Thu, 2 Nov 2023 11:34:48 -0700 Subject: [PATCH 1/5] modify existing validators() rust api to take both epoch_id and block_id --- chain/jsonrpc/client/src/lib.rs | 13 +---- chain/jsonrpc/src/api/validator.rs | 48 +++++++++++++++++-- core/primitives/src/types.rs | 2 + .../src/tests/nearcore/rpc_nodes.rs | 3 +- integration-tests/src/user/rpc_user.rs | 6 +-- tools/state-parts-dump-check/src/cli.rs | 6 +-- 6 files changed, 55 insertions(+), 23 deletions(-) diff --git a/chain/jsonrpc/client/src/lib.rs b/chain/jsonrpc/client/src/lib.rs index 52b76635603..fa7b04f01b4 100644 --- a/chain/jsonrpc/client/src/lib.rs +++ b/chain/jsonrpc/client/src/lib.rs @@ -11,7 +11,7 @@ use near_jsonrpc_primitives::types::transactions::{ use near_jsonrpc_primitives::types::validator::RpcValidatorsOrderedRequest; use near_primitives::hash::CryptoHash; use near_primitives::types::{ - BlockId, BlockReference, EpochId, EpochReference, MaybeBlockId, ShardId, + BlockId, BlockReference, MaybeEpochReference, MaybeBlockId, ShardId, }; use near_primitives::views::validator_stake_view::ValidatorStakeView; use near_primitives::views::{ @@ -189,7 +189,7 @@ jsonrpc_client!(pub struct JsonRpcClient { pub fn EXPERIMENTAL_tx_status(&self, tx: String) -> RpcRequest; pub fn health(&self) -> RpcRequest<()>; pub fn chunk(&self, id: ChunkId) -> RpcRequest; - pub fn validators(&self, block_id: MaybeBlockId) -> RpcRequest; + pub fn validators(&self, epoch_id_or_block_id: MaybeEpochReference) -> RpcRequest; pub fn gas_price(&self, block_id: MaybeBlockId) -> RpcRequest; }); @@ -223,15 +223,6 @@ impl JsonRpcClient { call_method(&self.client, &self.server_addr, "tx", request) } - pub fn validators_by_epoch_id(&self, epoch_id: EpochId) -> RpcRequest { - call_method( - &self.client, - &self.server_addr, - "validators", - EpochReference::EpochId(epoch_id), - ) - } - #[allow(non_snake_case)] pub fn EXPERIMENTAL_changes( &self, diff --git a/chain/jsonrpc/src/api/validator.rs b/chain/jsonrpc/src/api/validator.rs index 15ebd8e6d24..b8424996eb6 100644 --- a/chain/jsonrpc/src/api/validator.rs +++ b/chain/jsonrpc/src/api/validator.rs @@ -11,13 +11,15 @@ use super::{Params, RpcFrom, RpcRequest}; impl RpcRequest for RpcValidatorRequest { fn parse(value: Value) -> Result { - let epoch_reference = Params::new(value) + if let Ok(epoch_reference) = Params::new(value.clone()) .try_singleton(|block_id| match block_id { Some(id) => Ok(EpochReference::BlockId(id)), None => Ok(EpochReference::Latest), - }) - .unwrap_or_parse()?; - Ok(Self { epoch_reference }) + }).unwrap_or_parse() { + Ok(Self {epoch_reference}) + } else { + Params::parse(value).map(|epoch_reference| Self { epoch_reference }) + } } } @@ -49,3 +51,41 @@ impl RpcFrom for RpcValidatorError { } } } + +#[cfg(test)] +mod tests { + use crate::api::RpcRequest; + use near_jsonrpc_primitives::types::validator::RpcValidatorRequest; + use near_primitives::hash::CryptoHash; + + #[test] + fn test_serialize_validators_params_as_vec() { + let block_hash = CryptoHash::new().to_string(); + let params = serde_json::json!([block_hash]); + assert!(RpcValidatorRequest::parse(params).is_ok()); + } + + #[test] + fn test_serialize_validators_params_as_object_input_block_hash() { + let block_hash = CryptoHash::new().to_string(); + let params = serde_json::json!({"block_id": block_hash}); + assert!(RpcValidatorRequest::parse(params).is_ok()); + } + + #[test] + fn test_serialize_validators_params_as_object_input_block_height() { + let block_height: u64 = 12345; + let params = serde_json::json!({"block_id": block_height}); + println!("result is: {:?}", RpcValidatorRequest::parse(params.clone())); + assert!(RpcValidatorRequest::parse(params).is_ok()); + } + + #[test] + fn test_serialize_validators_params_as_object_input_epoch_id() { + let epoch_id = CryptoHash::new().to_string(); + let params = serde_json::json!({"epoch_id": epoch_id}); + println!("result is: {:?}", RpcValidatorRequest::parse(params.clone())); + assert!(RpcValidatorRequest::parse(params).is_ok()); + } + +} diff --git a/core/primitives/src/types.rs b/core/primitives/src/types.rs index 06cb28d291c..e14dd5251f9 100644 --- a/core/primitives/src/types.rs +++ b/core/primitives/src/types.rs @@ -905,6 +905,8 @@ pub enum EpochReference { Latest, } +pub type MaybeEpochReference = Option; + impl serde::Serialize for EpochReference { fn serialize(&self, s: S) -> Result where diff --git a/integration-tests/src/tests/nearcore/rpc_nodes.rs b/integration-tests/src/tests/nearcore/rpc_nodes.rs index a18e0b6f954..d176e1fe0d2 100644 --- a/integration-tests/src/tests/nearcore/rpc_nodes.rs +++ b/integration-tests/src/tests/nearcore/rpc_nodes.rs @@ -56,10 +56,9 @@ fn test_get_validator_info_rpc() { if block_view.header.height > 1 { let client = new_client(&format!("http://{}", rpc_addrs_copy[0])); let block_hash = block_view.header.hash; - let invalid_res = client.validators(Some(BlockId::Hash(block_hash))).await; + let invalid_res = client.validators(Some(EpochReference::BlockId(BlockId::Hash(block_hash)))).await; assert!(invalid_res.is_err()); let res = client.validators(None).await.unwrap(); - assert_eq!(res.current_validators.len(), 1); assert!(res.current_validators.iter().any(|r| r.account_id == "near.0")); System::current().stop(); diff --git a/integration-tests/src/user/rpc_user.rs b/integration-tests/src/user/rpc_user.rs index 2d3b2d69415..65451d10e83 100644 --- a/integration-tests/src/user/rpc_user.rs +++ b/integration-tests/src/user/rpc_user.rs @@ -16,7 +16,7 @@ use near_primitives::receipt::Receipt; use near_primitives::serialize::to_base64; use near_primitives::transaction::SignedTransaction; use near_primitives::types::{ - AccountId, BlockHeight, BlockId, BlockReference, MaybeBlockId, ShardId, + AccountId, BlockHeight, BlockId, BlockReference, ShardId, MaybeEpochReference, }; use near_primitives::views::{ AccessKeyView, AccountView, BlockView, CallResult, ChunkView, ContractCodeView, @@ -56,8 +56,8 @@ impl RpcUser { self.actix(move |client| client.query(request).map_err(|err| err.to_string())) } - pub fn validators(&self, block_id: MaybeBlockId) -> Result { - self.actix(move |client| client.validators(block_id).map_err(|err| err.to_string())) + pub fn validators(&self, epoch_id_or_block_id: MaybeEpochReference) -> Result { + self.actix(move |client| client.validators(epoch_id_or_block_id).map_err(|err| err.to_string())) } } diff --git a/tools/state-parts-dump-check/src/cli.rs b/tools/state-parts-dump-check/src/cli.rs index 061a0faefd5..df013f52ab9 100644 --- a/tools/state-parts-dump-check/src/cli.rs +++ b/tools/state-parts-dump-check/src/cli.rs @@ -8,7 +8,7 @@ use near_client::sync::external::{ use near_jsonrpc::client::{new_client, JsonRpcClient}; use near_primitives::hash::CryptoHash; use near_primitives::state_part::PartId; -use near_primitives::types::{BlockId, BlockReference, EpochId, Finality, ShardId, StateRoot}; +use near_primitives::types::{BlockId, BlockReference, EpochId, EpochReference, Finality, ShardId, StateRoot}; use near_primitives::views::BlockView; use near_store::Trie; use nearcore::state_sync::extract_part_id_from_part_file_name; @@ -639,7 +639,7 @@ async fn get_processing_epoch_information( let latest_epoch_id = latest_block_response.header.epoch_id; let latest_epoch_response = rpc_client - .validators_by_epoch_id(EpochId(latest_epoch_id)) + .validators(Some(EpochReference::EpochId(EpochId(latest_epoch_id)))) .await .or_else(|_| Err(anyhow!("validators_by_epoch_id for latest_epoch_id failed")))?; let latest_epoch_height = latest_epoch_response.epoch_height; @@ -664,7 +664,7 @@ async fn get_previous_epoch_last_block_response( current_epoch_id: CryptoHash, ) -> anyhow::Result { let current_epoch_response = rpc_client - .validators_by_epoch_id(EpochId(current_epoch_id)) + .validators(Some(EpochReference::EpochId(EpochId(current_epoch_id)))) .await .or_else(|_| Err(anyhow!("validators_by_epoch_id for current_epoch_id failed")))?; let current_epoch_first_block_height = current_epoch_response.epoch_start_height; From 4538b217b0aa7d8419fd4a20ba3107938217d622 Mon Sep 17 00:00:00 2001 From: Xiangyi Zheng Date: Fri, 3 Nov 2023 15:32:44 -0700 Subject: [PATCH 2/5] formatting --- chain/jsonrpc/client/src/lib.rs | 4 +--- chain/jsonrpc/src/api/validator.rs | 11 ++++++----- integration-tests/src/tests/nearcore/rpc_nodes.rs | 4 +++- integration-tests/src/user/rpc_user.rs | 11 ++++++++--- tools/state-parts-dump-check/src/cli.rs | 4 +++- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/chain/jsonrpc/client/src/lib.rs b/chain/jsonrpc/client/src/lib.rs index fa7b04f01b4..9a0750c51dd 100644 --- a/chain/jsonrpc/client/src/lib.rs +++ b/chain/jsonrpc/client/src/lib.rs @@ -10,9 +10,7 @@ use near_jsonrpc_primitives::types::transactions::{ }; use near_jsonrpc_primitives::types::validator::RpcValidatorsOrderedRequest; use near_primitives::hash::CryptoHash; -use near_primitives::types::{ - BlockId, BlockReference, MaybeEpochReference, MaybeBlockId, ShardId, -}; +use near_primitives::types::{BlockId, BlockReference, MaybeBlockId, MaybeEpochReference, ShardId}; use near_primitives::views::validator_stake_view::ValidatorStakeView; use near_primitives::views::{ BlockView, ChunkView, EpochValidatorInfo, GasPriceView, StatusResponse, diff --git a/chain/jsonrpc/src/api/validator.rs b/chain/jsonrpc/src/api/validator.rs index b8424996eb6..d95bbc21403 100644 --- a/chain/jsonrpc/src/api/validator.rs +++ b/chain/jsonrpc/src/api/validator.rs @@ -11,13 +11,17 @@ use super::{Params, RpcFrom, RpcRequest}; impl RpcRequest for RpcValidatorRequest { fn parse(value: Value) -> Result { + // this takes care of the legacy input format [block_id] if let Ok(epoch_reference) = Params::new(value.clone()) .try_singleton(|block_id| match block_id { Some(id) => Ok(EpochReference::BlockId(id)), None => Ok(EpochReference::Latest), - }).unwrap_or_parse() { - Ok(Self {epoch_reference}) + }) + .unwrap_or_parse() + { + Ok(Self { epoch_reference }) } else { + // this takes care of the map format input, e.g. {"epoch_id": "5Yheiw"} or {"block_id": 12345} Params::parse(value).map(|epoch_reference| Self { epoch_reference }) } } @@ -76,7 +80,6 @@ mod tests { fn test_serialize_validators_params_as_object_input_block_height() { let block_height: u64 = 12345; let params = serde_json::json!({"block_id": block_height}); - println!("result is: {:?}", RpcValidatorRequest::parse(params.clone())); assert!(RpcValidatorRequest::parse(params).is_ok()); } @@ -84,8 +87,6 @@ mod tests { fn test_serialize_validators_params_as_object_input_epoch_id() { let epoch_id = CryptoHash::new().to_string(); let params = serde_json::json!({"epoch_id": epoch_id}); - println!("result is: {:?}", RpcValidatorRequest::parse(params.clone())); assert!(RpcValidatorRequest::parse(params).is_ok()); } - } diff --git a/integration-tests/src/tests/nearcore/rpc_nodes.rs b/integration-tests/src/tests/nearcore/rpc_nodes.rs index d176e1fe0d2..50686526936 100644 --- a/integration-tests/src/tests/nearcore/rpc_nodes.rs +++ b/integration-tests/src/tests/nearcore/rpc_nodes.rs @@ -56,7 +56,9 @@ fn test_get_validator_info_rpc() { if block_view.header.height > 1 { let client = new_client(&format!("http://{}", rpc_addrs_copy[0])); let block_hash = block_view.header.hash; - let invalid_res = client.validators(Some(EpochReference::BlockId(BlockId::Hash(block_hash)))).await; + let invalid_res = client + .validators(Some(EpochReference::BlockId(BlockId::Hash(block_hash)))) + .await; assert!(invalid_res.is_err()); let res = client.validators(None).await.unwrap(); assert_eq!(res.current_validators.len(), 1); diff --git a/integration-tests/src/user/rpc_user.rs b/integration-tests/src/user/rpc_user.rs index 65451d10e83..ff43570e70f 100644 --- a/integration-tests/src/user/rpc_user.rs +++ b/integration-tests/src/user/rpc_user.rs @@ -16,7 +16,7 @@ use near_primitives::receipt::Receipt; use near_primitives::serialize::to_base64; use near_primitives::transaction::SignedTransaction; use near_primitives::types::{ - AccountId, BlockHeight, BlockId, BlockReference, ShardId, MaybeEpochReference, + AccountId, BlockHeight, BlockId, BlockReference, MaybeEpochReference, ShardId, }; use near_primitives::views::{ AccessKeyView, AccountView, BlockView, CallResult, ChunkView, ContractCodeView, @@ -56,8 +56,13 @@ impl RpcUser { self.actix(move |client| client.query(request).map_err(|err| err.to_string())) } - pub fn validators(&self, epoch_id_or_block_id: MaybeEpochReference) -> Result { - self.actix(move |client| client.validators(epoch_id_or_block_id).map_err(|err| err.to_string())) + pub fn validators( + &self, + epoch_id_or_block_id: MaybeEpochReference, + ) -> Result { + self.actix(move |client| { + client.validators(epoch_id_or_block_id).map_err(|err| err.to_string()) + }) } } diff --git a/tools/state-parts-dump-check/src/cli.rs b/tools/state-parts-dump-check/src/cli.rs index df013f52ab9..faacb6aab93 100644 --- a/tools/state-parts-dump-check/src/cli.rs +++ b/tools/state-parts-dump-check/src/cli.rs @@ -8,7 +8,9 @@ use near_client::sync::external::{ use near_jsonrpc::client::{new_client, JsonRpcClient}; use near_primitives::hash::CryptoHash; use near_primitives::state_part::PartId; -use near_primitives::types::{BlockId, BlockReference, EpochId, EpochReference, Finality, ShardId, StateRoot}; +use near_primitives::types::{ + BlockId, BlockReference, EpochId, EpochReference, Finality, ShardId, StateRoot, +}; use near_primitives::views::BlockView; use near_store::Trie; use nearcore::state_sync::extract_part_id_from_part_file_name; From 99c27839a2a40909294944771039f68cc417a87b Mon Sep 17 00:00:00 2001 From: Xiangyi Zheng Date: Fri, 3 Nov 2023 15:49:31 -0700 Subject: [PATCH 3/5] formatting --- tools/state-parts-dump-check/src/cli.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tools/state-parts-dump-check/src/cli.rs b/tools/state-parts-dump-check/src/cli.rs index faacb6aab93..3d2eb80ca58 100644 --- a/tools/state-parts-dump-check/src/cli.rs +++ b/tools/state-parts-dump-check/src/cli.rs @@ -639,11 +639,10 @@ async fn get_processing_epoch_information( .await .or_else(|_| Err(anyhow!("get final block failed")))?; let latest_epoch_id = latest_block_response.header.epoch_id; - let latest_epoch_response = - rpc_client - .validators(Some(EpochReference::EpochId(EpochId(latest_epoch_id)))) - .await - .or_else(|_| Err(anyhow!("validators_by_epoch_id for latest_epoch_id failed")))?; + let latest_epoch_response = rpc_client + .validators(Some(EpochReference::EpochId(EpochId(latest_epoch_id)))) + .await + .or_else(|_| Err(anyhow!("validators_by_epoch_id for latest_epoch_id failed")))?; let latest_epoch_height = latest_epoch_response.epoch_height; let prev_epoch_last_block_response = get_previous_epoch_last_block_response(rpc_client, latest_epoch_id).await?; From 45f54f19d0b0fc2cbaa5dad6bb8702a32195e168 Mon Sep 17 00:00:00 2001 From: Xiangyi Zheng Date: Mon, 6 Nov 2023 15:16:41 -0800 Subject: [PATCH 4/5] use assert_eq to make test result more explicit, and remove MaybeEpochReference --- chain/jsonrpc/client/src/lib.rs | 4 +- chain/jsonrpc/src/api/validator.rs | 55 +++++++++++++++++++++----- core/primitives/src/types.rs | 2 - integration-tests/src/user/rpc_user.rs | 4 +- 4 files changed, 49 insertions(+), 16 deletions(-) diff --git a/chain/jsonrpc/client/src/lib.rs b/chain/jsonrpc/client/src/lib.rs index 9a0750c51dd..b3724c25dcd 100644 --- a/chain/jsonrpc/client/src/lib.rs +++ b/chain/jsonrpc/client/src/lib.rs @@ -10,7 +10,7 @@ use near_jsonrpc_primitives::types::transactions::{ }; use near_jsonrpc_primitives::types::validator::RpcValidatorsOrderedRequest; use near_primitives::hash::CryptoHash; -use near_primitives::types::{BlockId, BlockReference, MaybeBlockId, MaybeEpochReference, ShardId}; +use near_primitives::types::{BlockId, BlockReference, EpochReference, MaybeBlockId, ShardId}; use near_primitives::views::validator_stake_view::ValidatorStakeView; use near_primitives::views::{ BlockView, ChunkView, EpochValidatorInfo, GasPriceView, StatusResponse, @@ -187,7 +187,7 @@ jsonrpc_client!(pub struct JsonRpcClient { pub fn EXPERIMENTAL_tx_status(&self, tx: String) -> RpcRequest; pub fn health(&self) -> RpcRequest<()>; pub fn chunk(&self, id: ChunkId) -> RpcRequest; - pub fn validators(&self, epoch_id_or_block_id: MaybeEpochReference) -> RpcRequest; + pub fn validators(&self, epoch_id_or_block_id: Option) -> RpcRequest; pub fn gas_price(&self, block_id: MaybeBlockId) -> RpcRequest; }); diff --git a/chain/jsonrpc/src/api/validator.rs b/chain/jsonrpc/src/api/validator.rs index d95bbc21403..168b7d78b34 100644 --- a/chain/jsonrpc/src/api/validator.rs +++ b/chain/jsonrpc/src/api/validator.rs @@ -61,32 +61,67 @@ mod tests { use crate::api::RpcRequest; use near_jsonrpc_primitives::types::validator::RpcValidatorRequest; use near_primitives::hash::CryptoHash; + use near_primitives::types::{BlockId, EpochId, EpochReference}; #[test] fn test_serialize_validators_params_as_vec() { - let block_hash = CryptoHash::new().to_string(); - let params = serde_json::json!([block_hash]); - assert!(RpcValidatorRequest::parse(params).is_ok()); + let block_hash = CryptoHash::new(); + let block_hash_string = CryptoHash::new().to_string(); + let params = serde_json::json!([block_hash_string]); + let result = RpcValidatorRequest::parse(params); + assert!(result.is_ok()); + let result_unwrap = result.unwrap(); + let res_serialized = format!("{result_unwrap:?}"); + let expected = RpcValidatorRequest { + epoch_reference: EpochReference::BlockId(BlockId::Hash(block_hash)), + }; + let expected_serialized = format!("{expected:?}"); + assert_eq!(res_serialized, expected_serialized); } #[test] fn test_serialize_validators_params_as_object_input_block_hash() { - let block_hash = CryptoHash::new().to_string(); - let params = serde_json::json!({"block_id": block_hash}); - assert!(RpcValidatorRequest::parse(params).is_ok()); + let block_hash = CryptoHash::new(); + let block_hash_string = CryptoHash::new().to_string(); + let params = serde_json::json!({"block_id": block_hash_string}); + let result = RpcValidatorRequest::parse(params); + assert!(result.is_ok()); + let result_unwrap = result.unwrap(); + let res_serialized = format!("{result_unwrap:?}"); + let expected = RpcValidatorRequest { + epoch_reference: EpochReference::BlockId(BlockId::Hash(block_hash)), + }; + let expected_serialized = format!("{expected:?}"); + assert_eq!(res_serialized, expected_serialized); } #[test] fn test_serialize_validators_params_as_object_input_block_height() { let block_height: u64 = 12345; let params = serde_json::json!({"block_id": block_height}); - assert!(RpcValidatorRequest::parse(params).is_ok()); + let result = RpcValidatorRequest::parse(params); + assert!(result.is_ok()); + let result_unwrap = result.unwrap(); + let res_serialized = format!("{result_unwrap:?}"); + let expected = RpcValidatorRequest { + epoch_reference: EpochReference::BlockId(BlockId::Height(block_height)), + }; + let expected_serialized = format!("{expected:?}"); + assert_eq!(res_serialized, expected_serialized); } #[test] fn test_serialize_validators_params_as_object_input_epoch_id() { - let epoch_id = CryptoHash::new().to_string(); - let params = serde_json::json!({"epoch_id": epoch_id}); - assert!(RpcValidatorRequest::parse(params).is_ok()); + let epoch_id = CryptoHash::new(); + let epoch_id_string = epoch_id.to_string(); + let params = serde_json::json!({"epoch_id": epoch_id_string}); + let result = RpcValidatorRequest::parse(params); + assert!(result.is_ok()); + let result_unwrap = result.unwrap(); + let res_serialized = format!("{result_unwrap:?}"); + let expected = + RpcValidatorRequest { epoch_reference: EpochReference::EpochId(EpochId(epoch_id)) }; + let expected_serialized = format!("{expected:?}"); + assert_eq!(res_serialized, expected_serialized); } } diff --git a/core/primitives/src/types.rs b/core/primitives/src/types.rs index e14dd5251f9..06cb28d291c 100644 --- a/core/primitives/src/types.rs +++ b/core/primitives/src/types.rs @@ -905,8 +905,6 @@ pub enum EpochReference { Latest, } -pub type MaybeEpochReference = Option; - impl serde::Serialize for EpochReference { fn serialize(&self, s: S) -> Result where diff --git a/integration-tests/src/user/rpc_user.rs b/integration-tests/src/user/rpc_user.rs index ff43570e70f..0e5e53c6cd2 100644 --- a/integration-tests/src/user/rpc_user.rs +++ b/integration-tests/src/user/rpc_user.rs @@ -16,7 +16,7 @@ use near_primitives::receipt::Receipt; use near_primitives::serialize::to_base64; use near_primitives::transaction::SignedTransaction; use near_primitives::types::{ - AccountId, BlockHeight, BlockId, BlockReference, MaybeEpochReference, ShardId, + AccountId, BlockHeight, BlockId, BlockReference, EpochReference, ShardId, }; use near_primitives::views::{ AccessKeyView, AccountView, BlockView, CallResult, ChunkView, ContractCodeView, @@ -58,7 +58,7 @@ impl RpcUser { pub fn validators( &self, - epoch_id_or_block_id: MaybeEpochReference, + epoch_id_or_block_id: Option, ) -> Result { self.actix(move |client| { client.validators(epoch_id_or_block_id).map_err(|err| err.to_string()) From e326e5e934698007c1a9a07f1ece2c24712332a4 Mon Sep 17 00:00:00 2001 From: Xiangyi Zheng Date: Tue, 7 Nov 2023 09:23:13 -0800 Subject: [PATCH 5/5] address comments --- .../jsonrpc-primitives/src/types/validator.rs | 2 +- chain/jsonrpc/src/api/validator.rs | 60 +++++++------------ core/primitives/src/types.rs | 2 +- 3 files changed, 24 insertions(+), 40 deletions(-) diff --git a/chain/jsonrpc-primitives/src/types/validator.rs b/chain/jsonrpc-primitives/src/types/validator.rs index fd3694b1c36..d3b55c01be1 100644 --- a/chain/jsonrpc-primitives/src/types/validator.rs +++ b/chain/jsonrpc-primitives/src/types/validator.rs @@ -14,7 +14,7 @@ pub enum RpcValidatorError { InternalError { error_message: String }, } -#[derive(serde::Serialize, serde::Deserialize, Debug, arbitrary::Arbitrary)] +#[derive(serde::Serialize, serde::Deserialize, Debug, arbitrary::Arbitrary, PartialEq, Eq)] pub struct RpcValidatorRequest { #[serde(flatten)] pub epoch_reference: near_primitives::types::EpochReference, diff --git a/chain/jsonrpc/src/api/validator.rs b/chain/jsonrpc/src/api/validator.rs index 168b7d78b34..f23384de361 100644 --- a/chain/jsonrpc/src/api/validator.rs +++ b/chain/jsonrpc/src/api/validator.rs @@ -11,19 +11,13 @@ use super::{Params, RpcFrom, RpcRequest}; impl RpcRequest for RpcValidatorRequest { fn parse(value: Value) -> Result { - // this takes care of the legacy input format [block_id] - if let Ok(epoch_reference) = Params::new(value.clone()) + let epoch_reference = Params::new(value) .try_singleton(|block_id| match block_id { Some(id) => Ok(EpochReference::BlockId(id)), None => Ok(EpochReference::Latest), }) - .unwrap_or_parse() - { - Ok(Self { epoch_reference }) - } else { - // this takes care of the map format input, e.g. {"epoch_id": "5Yheiw"} or {"block_id": 12345} - Params::parse(value).map(|epoch_reference| Self { epoch_reference }) - } + .unwrap_or_parse()?; + Ok(Self { epoch_reference: epoch_reference }) } } @@ -66,33 +60,27 @@ mod tests { #[test] fn test_serialize_validators_params_as_vec() { let block_hash = CryptoHash::new(); - let block_hash_string = CryptoHash::new().to_string(); - let params = serde_json::json!([block_hash_string]); + let params = serde_json::json!([block_hash.to_string()]); let result = RpcValidatorRequest::parse(params); - assert!(result.is_ok()); - let result_unwrap = result.unwrap(); - let res_serialized = format!("{result_unwrap:?}"); - let expected = RpcValidatorRequest { - epoch_reference: EpochReference::BlockId(BlockId::Hash(block_hash)), - }; - let expected_serialized = format!("{expected:?}"); - assert_eq!(res_serialized, expected_serialized); + assert_eq!( + result.unwrap(), + RpcValidatorRequest { + epoch_reference: EpochReference::BlockId(BlockId::Hash(block_hash)), + } + ); } #[test] fn test_serialize_validators_params_as_object_input_block_hash() { let block_hash = CryptoHash::new(); - let block_hash_string = CryptoHash::new().to_string(); - let params = serde_json::json!({"block_id": block_hash_string}); + let params = serde_json::json!({"block_id": block_hash.to_string()}); let result = RpcValidatorRequest::parse(params); - assert!(result.is_ok()); - let result_unwrap = result.unwrap(); - let res_serialized = format!("{result_unwrap:?}"); - let expected = RpcValidatorRequest { - epoch_reference: EpochReference::BlockId(BlockId::Hash(block_hash)), - }; - let expected_serialized = format!("{expected:?}"); - assert_eq!(res_serialized, expected_serialized); + assert_eq!( + result.unwrap(), + RpcValidatorRequest { + epoch_reference: EpochReference::BlockId(BlockId::Hash(block_hash)), + } + ); } #[test] @@ -113,15 +101,11 @@ mod tests { #[test] fn test_serialize_validators_params_as_object_input_epoch_id() { let epoch_id = CryptoHash::new(); - let epoch_id_string = epoch_id.to_string(); - let params = serde_json::json!({"epoch_id": epoch_id_string}); + let params = serde_json::json!({"epoch_id": epoch_id.to_string()}); let result = RpcValidatorRequest::parse(params); - assert!(result.is_ok()); - let result_unwrap = result.unwrap(); - let res_serialized = format!("{result_unwrap:?}"); - let expected = - RpcValidatorRequest { epoch_reference: EpochReference::EpochId(EpochId(epoch_id)) }; - let expected_serialized = format!("{expected:?}"); - assert_eq!(res_serialized, expected_serialized); + assert_eq!( + result.unwrap(), + RpcValidatorRequest { epoch_reference: EpochReference::EpochId(EpochId(epoch_id)) } + ); } } diff --git a/core/primitives/src/types.rs b/core/primitives/src/types.rs index 06cb28d291c..5e68f97484f 100644 --- a/core/primitives/src/types.rs +++ b/core/primitives/src/types.rs @@ -897,7 +897,7 @@ pub struct BlockChunkValidatorStats { pub chunk_stats: ValidatorStats, } -#[derive(serde::Deserialize, Debug, arbitrary::Arbitrary)] +#[derive(serde::Deserialize, Debug, arbitrary::Arbitrary, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub enum EpochReference { EpochId(EpochId),