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

Conversation

ppca
Copy link
Contributor

@ppca ppca commented Nov 3, 2023

  1. removed the previously added validator_by_epoch_id() api
  2. modified the pre-existing validators(maybe_block_id) api to be validators(maybe_epoch_reference). Basically now within rust, we can input None, or Some(EpochReference::EpochId(epoch_id)) or Some(EpochReference::BlockId(block_id)).
  3. added some unit tests to test all currently supported json inputs to the validators RPC endpoint are working. When users send requests to the near rpc validators endpoint, they could use as input the legacy format [block_id] or the map format {"epoch_id": epoch_hash} or {"block_id": block hash/height}

@ppca ppca requested a review from a team as a code owner November 3, 2023 22:31
@ppca ppca requested review from Longarithm, telezhnaya and nikurt and removed request for Longarithm November 3, 2023 22:31
@ppca ppca force-pushed the xiangyi/rust_api_validators_fix branch from 1f0d9bd to 6b1d56e Compare November 3, 2023 22:35
chain/jsonrpc/src/api/validator.rs Outdated Show resolved Hide resolved
chain/jsonrpc/src/api/validator.rs Outdated Show resolved Hide resolved
@@ -905,6 +905,8 @@ pub enum EpochReference {
Latest,
}

pub type MaybeEpochReference = Option<EpochReference>;
Copy link
Contributor

@nikurt nikurt Nov 6, 2023

Choose a reason for hiding this comment

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

I'm not in favor of defining types for Options. The resulting code is almost the same length, but introduces an additional entity.

@ppca ppca force-pushed the xiangyi/rust_api_validators_fix branch from a2e2c66 to 5243988 Compare November 6, 2023 23:16
@ppca ppca requested a review from nikurt November 6, 2023 23:17
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());
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need this assert, if you do an unwrap() later.

epoch_reference: EpochReference::BlockId(BlockId::Hash(block_hash)),
};
let expected_serialized = format!("{expected:?}");
assert_eq!(res_serialized, expected_serialized);
Copy link
Contributor

Choose a reason for hiding this comment

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

This works, but the code is much simpler if you define Eq and PartialEq for EpochReference. Then you can simply do assert_eq!(result.unwrap(), RpcValidatorRequest { epoch_reference: EpochReference::BlockId(BlockId::Hash(block_hash)) };

I think it's acceptable because all network messages define Eq and PartialEq. RpcValidatorRequest is almost a network message. :)

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]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove block_hash_string and do this instead

Suggested change
let params = serde_json::json!([block_hash_string]);
let params = serde_json::json!([block_hash.to_string()]);

.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()
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be possible to get rid of if-else and have only

Ok(Params::new(value)
            .try_singleton(|block_id| match block_id {
                Some(id) => Ok(EpochReference::BlockId(id)),
                None => Ok(EpochReference::Latest),
            })
            .unwrap_or_parse())

See this as the reference

Ok(Params::new(value)

#[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.

@ppca ppca requested review from nikurt and telezhnaya November 7, 2023 17:23
@ppca ppca force-pushed the xiangyi/rust_api_validators_fix branch from ce19ca7 to e326e5e Compare November 7, 2023 17:33
Copy link
Contributor

@nikurt nikurt left a comment

Choose a reason for hiding this comment

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

Looks good.
And 👍 for the concise tests.

@ppca ppca added this pull request to the merge queue Nov 8, 2023
Merged via the queue into master with commit 76a8e59 Nov 8, 2023
12 of 13 checks passed
@ppca ppca deleted the xiangyi/rust_api_validators_fix branch November 8, 2023 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants