Skip to content

Commit

Permalink
modify existing validators() rust api to take both epoch_id and block_id
Browse files Browse the repository at this point in the history
  • Loading branch information
ppca committed Nov 3, 2023
1 parent 4c3c373 commit 72f29e2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 23 deletions.
13 changes: 2 additions & 11 deletions chain/jsonrpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -189,7 +189,7 @@ jsonrpc_client!(pub struct JsonRpcClient {
pub fn EXPERIMENTAL_tx_status(&self, tx: String) -> RpcRequest<RpcTransactionResponse>;
pub fn health(&self) -> RpcRequest<()>;
pub fn chunk(&self, id: ChunkId) -> RpcRequest<ChunkView>;
pub fn validators(&self, block_id: MaybeBlockId) -> RpcRequest<EpochValidatorInfo>;
pub fn validators(&self, epoch_id_or_block_id: MaybeEpochReference) -> RpcRequest<EpochValidatorInfo>;
pub fn gas_price(&self, block_id: MaybeBlockId) -> RpcRequest<GasPriceView>;
});

Expand Down Expand Up @@ -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<EpochValidatorInfo> {
call_method(
&self.client,
&self.server_addr,
"validators",
EpochReference::EpochId(epoch_id),
)
}

#[allow(non_snake_case)]
pub fn EXPERIMENTAL_changes(
&self,
Expand Down
48 changes: 44 additions & 4 deletions chain/jsonrpc/src/api/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ use super::{Params, RpcFrom, RpcRequest};

impl RpcRequest for RpcValidatorRequest {
fn parse(value: Value) -> Result<Self, RpcParseError> {
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 })
}
}
}

Expand Down Expand Up @@ -49,3 +51,41 @@ impl RpcFrom<GetValidatorInfoError> 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());
}

}
2 changes: 2 additions & 0 deletions core/primitives/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,8 @@ pub enum EpochReference {
Latest,
}

pub type MaybeEpochReference = Option<EpochReference>;

impl serde::Serialize for EpochReference {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
Expand Down
3 changes: 1 addition & 2 deletions integration-tests/src/tests/nearcore/rpc_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/src/user/rpc_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<EpochValidatorInfo, String> {
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<EpochValidatorInfo, String> {
self.actix(move |client| client.validators(epoch_id_or_block_id).map_err(|err| err.to_string()))
}
}

Expand Down
6 changes: 3 additions & 3 deletions tools/state-parts-dump-check/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -642,7 +642,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 prev_epoch_id failed")))?;
let latest_epoch_height = latest_epoch_response.epoch_height;
Expand All @@ -667,7 +667,7 @@ async fn get_previous_epoch_last_block_response(
current_epoch_id: CryptoHash,
) -> anyhow::Result<BlockView> {
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;
Expand Down

0 comments on commit 72f29e2

Please sign in to comment.