Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify existing validators() rust api to take both epoch_id and block_id #10097

Merged
merged 5 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chain/jsonrpc-primitives/src/types/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 2 additions & 13 deletions chain/jsonrpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, EpochId, EpochReference, MaybeBlockId, 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,
Expand Down Expand Up @@ -189,7 +187,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: Option<EpochReference>) -> RpcRequest<EpochValidatorInfo>;
pub fn gas_price(&self, block_id: MaybeBlockId) -> RpcRequest<GasPriceView>;
});

Expand Down Expand Up @@ -223,15 +221,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
62 changes: 61 additions & 1 deletion chain/jsonrpc/src/api/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl RpcRequest for RpcValidatorRequest {
None => Ok(EpochReference::Latest),
})
.unwrap_or_parse()?;
Ok(Self { epoch_reference })
Ok(Self { epoch_reference: epoch_reference })
}
}

Expand Down Expand Up @@ -49,3 +49,63 @@ 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;
use near_primitives::types::{BlockId, EpochId, EpochReference};

#[test]
fn test_serialize_validators_params_as_vec() {
let block_hash = CryptoHash::new();
let params = serde_json::json!([block_hash.to_string()]);
let result = RpcValidatorRequest::parse(params);
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 params = serde_json::json!({"block_id": block_hash.to_string()});
let result = RpcValidatorRequest::parse(params);
assert_eq!(
result.unwrap(),
RpcValidatorRequest {
epoch_reference: EpochReference::BlockId(BlockId::Hash(block_hash)),
}
);
}

#[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});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, it works the same as we have in blocks API (the ability of parsing both height and hash under the same name)
https://docs.near.org/api/rpc/block-chunk#block-details

Could you please update docs repo as well? Mentioing that we have a new format now
Be careful and don't merge it until we release 1.37

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Olga, what is the docs repo you are referring to?
And when you say don't merge it until we release 1.37, do you mean the change to the docs repo for the api change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/near/docs

do you mean the change to the docs repo

yes ofk
It's because the changes in nearcore will be live only after the release of 1.37, while the docs will be updated right after the merge
My PR is also waiting near/docs#1564

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Will open a PR in docs tmr.

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();
let params = serde_json::json!({"epoch_id": epoch_id.to_string()});
let result = RpcValidatorRequest::parse(params);
assert_eq!(
result.unwrap(),
RpcValidatorRequest { epoch_reference: EpochReference::EpochId(EpochId(epoch_id)) }
);
}
}
2 changes: 1 addition & 1 deletion core/primitives/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 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,11 @@ 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
11 changes: 8 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, EpochReference, ShardId,
};
use near_primitives::views::{
AccessKeyView, AccountView, BlockView, CallResult, ChunkView, ContractCodeView,
Expand Down Expand Up @@ -56,8 +56,13 @@ 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: Option<EpochReference>,
) -> Result<EpochValidatorInfo, String> {
self.actix(move |client| {
client.validators(epoch_id_or_block_id).map_err(|err| err.to_string())
})
}
}

Expand Down
15 changes: 8 additions & 7 deletions tools/state-parts-dump-check/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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 @@ -637,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_by_epoch_id(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?;
Expand All @@ -664,7 +665,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
Loading