Skip to content

Commit

Permalink
fix: make validators() rust api compatible with RPC servers (near#10145)
Browse files Browse the repository at this point in the history
Problem:
I have a piece of code in my monitoring tool:
```
let latest_epoch_response = rpc_client
            .validators(Some(EpochReference::EpochId(EpochId(latest_epoch_id))))
            .await.unwrap()
```
And on latest master, this is failing with
```
RpcError { error_struct: Some(RequestValidationError(ParseError { error_message: "Failed parsing args: data did not match any variant of untagged enum BlockId" })), code: -32700, message: "Parse error", data: Some(String("Failed parsing args: data did not match any variant of untagged enum BlockId")) }
```

Solution: 
change validators() rust api by letting it directly call the
"validators" method.
  • Loading branch information
ppca authored Nov 9, 2023
1 parent 2f04b7f commit cc77683
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
12 changes: 11 additions & 1 deletion chain/jsonrpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ 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, epoch_id_or_block_id: Option<EpochReference>) -> RpcRequest<EpochValidatorInfo>;
pub fn gas_price(&self, block_id: MaybeBlockId) -> RpcRequest<GasPriceView>;
});

Expand Down Expand Up @@ -261,6 +260,17 @@ impl JsonRpcClient {
{
call_method(&self.client, &self.server_addr, "EXPERIMENTAL_split_storage_info", request)
}

pub fn validators(
&self,
epoch_id_or_block_id: Option<EpochReference>,
) -> RpcRequest<EpochValidatorInfo> {
let epoch_reference = match epoch_id_or_block_id {
Some(epoch_reference) => epoch_reference,
_ => EpochReference::Latest,
};
call_method(&self.client, &self.server_addr, "validators", epoch_reference)
}
}

fn create_client() -> Client {
Expand Down
14 changes: 6 additions & 8 deletions chain/jsonrpc/src/api/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,12 @@ mod tests {
let block_height: u64 = 12345;
let params = serde_json::json!({"block_id": block_height});
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);
assert_eq!(
result.unwrap(),
RpcValidatorRequest {
epoch_reference: EpochReference::BlockId(BlockId::Height(block_height)),
}
);
}

#[test]
Expand Down

0 comments on commit cc77683

Please sign in to comment.