near_jsonrpc_primitives/types/
chunks.rs1use near_primitives::types::ShardId;
2use serde_json::Value;
3
4#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, arbitrary::Arbitrary)]
5#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
6#[serde(untagged)]
7pub enum ChunkReference {
8 BlockShardId {
9 block_id: near_primitives::types::BlockId,
10 shard_id: near_primitives::types::ShardId,
11 },
12 ChunkHash {
13 chunk_id: near_primitives::hash::CryptoHash,
14 },
15}
16
17#[derive(serde::Serialize, serde::Deserialize, Debug, arbitrary::Arbitrary)]
18#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
19pub struct RpcChunkRequest {
20 #[serde(flatten)]
21 pub chunk_reference: ChunkReference,
22}
23
24#[derive(serde::Serialize, serde::Deserialize, Debug)]
25#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
26pub struct RpcChunkResponse {
27 #[serde(flatten)]
28 pub chunk_view: near_primitives::views::ChunkView,
29}
30
31#[derive(thiserror::Error, Debug, Clone, serde::Serialize, serde::Deserialize)]
32#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
33#[serde(tag = "name", content = "info", rename_all = "SCREAMING_SNAKE_CASE")]
34pub enum RpcChunkError {
35 #[error("The node reached its limits. Try again later. More details: {error_message}")]
36 InternalError { error_message: String },
37 #[error(
38 "Block either has never been observed on the node or has been garbage collected: {error_message}"
39 )]
40 UnknownBlock {
41 #[serde(skip_serializing)]
42 error_message: String,
43 },
44 #[error("Shard id {shard_id} does not exist")]
45 InvalidShardId { shard_id: ShardId },
46 #[error("Chunk with hash {chunk_hash:?} has never been observed on this node")]
47 UnknownChunk { chunk_hash: near_primitives::sharding::ChunkHash },
48}
49
50impl From<RpcChunkError> for crate::errors::RpcError {
51 fn from(error: RpcChunkError) -> Self {
52 let error_data = match &error {
53 RpcChunkError::InternalError { .. } => Some(Value::String(error.to_string())),
54 RpcChunkError::UnknownBlock { error_message } => Some(Value::String(format!(
55 "DB Not Found Error: {} \n Cause: Unknown",
56 error_message
57 ))),
58 RpcChunkError::InvalidShardId { .. } => Some(Value::String(error.to_string())),
59 RpcChunkError::UnknownChunk { chunk_hash } => Some(Value::String(format!(
60 "Chunk Missing (unavailable on the node): ChunkHash(`{}`) \n Cause: Unknown",
61 chunk_hash.0
62 ))),
63 };
64
65 let error_data_value = match serde_json::to_value(error) {
66 Ok(value) => value,
67 Err(err) => {
68 return Self::new_internal_error(
69 None,
70 format!("Failed to serialize RpcStateChangesError: {:?}", err),
71 );
72 }
73 };
74
75 Self::new_internal_or_handler_error(error_data, error_data_value)
76 }
77}