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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(thiserror::Error, Debug, Serialize, Deserialize)]
#[serde(tag = "name", content = "info", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RpcBlockError {
#[error("Block not found: {error_message}")]
UnknownBlock {
#[serde(skip_serializing)]
error_message: String,
},
#[error("There are no fully synchronized blocks yet")]
NotSyncedYet,
#[error("The node reached its limits. Try again later. More details: {error_message}")]
InternalError { error_message: String },
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RpcBlockRequest {
#[serde(flatten)]
pub block_reference: near_primitives_v01::types::BlockReference,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RpcBlockResponse {
#[serde(flatten)]
pub block_view: near_primitives_v01::views::BlockView,
}
impl From<near_client_primitives::types::GetBlockError> for RpcBlockError {
fn from(error: near_client_primitives::types::GetBlockError) -> Self {
match error {
near_client_primitives::types::GetBlockError::UnknownBlock { error_message } => {
Self::UnknownBlock { error_message }
}
near_client_primitives::types::GetBlockError::NotSyncedYet => Self::NotSyncedYet,
near_client_primitives::types::GetBlockError::IOError { error_message } => {
Self::InternalError { error_message }
}
near_client_primitives::types::GetBlockError::Unreachable { ref error_message } => {
tracing::warn!(target: "jsonrpc", "Unreachable error occurred: {}", &error_message);
near_metrics::inc_counter_vec(
&crate::metrics::RPC_UNREACHABLE_ERROR_COUNT,
&["RpcBlockError"],
);
Self::InternalError { error_message: error.to_string() }
}
}
}
}
impl From<actix::MailboxError> for RpcBlockError {
fn from(error: actix::MailboxError) -> Self {
Self::InternalError { error_message: error.to_string() }
}
}
impl From<RpcBlockError> for crate::errors::RpcError {
fn from(error: RpcBlockError) -> Self {
let error_data = match &error {
RpcBlockError::UnknownBlock { error_message } => Some(Value::String(format!(
"DB Not Found Error: {} \n Cause: Unknown",
error_message
))),
RpcBlockError::NotSyncedYet | RpcBlockError::InternalError { .. } => {
Some(Value::String(error.to_string()))
}
};
let error_data_value = match serde_json::to_value(error) {
Ok(value) => value,
Err(err) => {
return Self::new_internal_error(
None,
format!("Failed to serialize RpcBlockError: {:?}", err),
)
}
};
Self::new_internal_or_handler_error(error_data, error_data_value)
}
}
impl RpcBlockRequest {
pub fn parse(value: Option<Value>) -> Result<RpcBlockRequest, crate::errors::RpcParseError> {
let block_reference = if let Ok((block_id,)) =
crate::utils::parse_params::<(near_primitives_v01::types::BlockId,)>(value.clone())
{
near_primitives_v01::types::BlockReference::BlockId(block_id)
} else {
crate::utils::parse_params::<near_primitives_v01::types::BlockReference>(value)?
};
Ok(RpcBlockRequest { block_reference })
}
}