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