near_api/common/query/
validator_rpc.rs

1use near_api_types::EpochReference;
2use near_openapi_client::types::{
3    BlockId, EpochId, ErrorWrapperForRpcValidatorError, JsonRpcRequestForValidators,
4    JsonRpcRequestForValidatorsMethod, JsonRpcResponseForRpcValidatorResponseAndRpcValidatorError,
5    RpcValidatorError, RpcValidatorRequest, RpcValidatorResponse,
6};
7use near_openapi_client::Client;
8
9use crate::common::utils::to_retry_error;
10use crate::errors::SendRequestError;
11use crate::{
12    advanced::RpcType, common::utils::is_critical_validator_error, config::RetryResponse,
13    NetworkConfig,
14};
15
16#[derive(Clone, Debug)]
17pub struct SimpleValidatorRpc;
18
19#[async_trait::async_trait]
20impl RpcType for SimpleValidatorRpc {
21    type RpcReference = EpochReference;
22    type Response = RpcValidatorResponse;
23    type Error = RpcValidatorError;
24    async fn send_query(
25        &self,
26        client: &Client,
27        _network: &NetworkConfig,
28        reference: &EpochReference,
29    ) -> RetryResponse<RpcValidatorResponse, SendRequestError<RpcValidatorError>> {
30        let request = match reference {
31            EpochReference::Latest => RpcValidatorRequest::Latest,
32            EpochReference::AtEpoch(epoch) => {
33                RpcValidatorRequest::EpochId(EpochId((*epoch).into()))
34            }
35            EpochReference::AtBlock(block) => {
36                RpcValidatorRequest::BlockId(BlockId::BlockHeight(*block))
37            }
38            EpochReference::AtBlockHash(block_hash) => {
39                RpcValidatorRequest::BlockId(BlockId::CryptoHash((*block_hash).into()))
40            }
41        };
42        let response = client
43            .validators(&JsonRpcRequestForValidators {
44                id: "0".to_string(),
45                jsonrpc: "2.0".to_string(),
46                method: JsonRpcRequestForValidatorsMethod::Validators,
47                params: request,
48            })
49            .await
50            .map(|r| r.into_inner())
51            .map_err(SendRequestError::from);
52
53        match response {
54            Ok(JsonRpcResponseForRpcValidatorResponseAndRpcValidatorError::Variant0 {
55                result,
56                ..
57            }) => RetryResponse::Ok(result),
58            Ok(JsonRpcResponseForRpcValidatorResponseAndRpcValidatorError::Variant1 {
59                error,
60                ..
61            }) => {
62                let error: SendRequestError<RpcValidatorError> = SendRequestError::from(error);
63                to_retry_error(error, is_critical_validator_error)
64            }
65            Err(err) => to_retry_error(err, is_critical_validator_error),
66        }
67    }
68}
69
70impl From<ErrorWrapperForRpcValidatorError> for SendRequestError<RpcValidatorError> {
71    fn from(err: ErrorWrapperForRpcValidatorError) -> Self {
72        match err {
73            ErrorWrapperForRpcValidatorError::InternalError(internal_error) => {
74                Self::InternalError(internal_error)
75            }
76            ErrorWrapperForRpcValidatorError::RequestValidationError(
77                rpc_request_validation_error_kind,
78            ) => Self::RequestValidationError(rpc_request_validation_error_kind),
79            ErrorWrapperForRpcValidatorError::HandlerError(server_error) => {
80                Self::ServerError(server_error)
81            }
82        }
83    }
84}