unc_jsonrpc_primitives/types/
query.rs

1#[derive(serde::Serialize, serde::Deserialize, Debug)]
2pub struct RpcQueryRequest {
3    #[serde(flatten)]
4    pub block_reference: unc_primitives::types::BlockReference,
5    #[serde(flatten)]
6    pub request: unc_primitives::views::QueryRequest,
7}
8
9#[derive(thiserror::Error, Debug, serde::Serialize, serde::Deserialize)]
10#[serde(tag = "name", content = "info", rename_all = "SCREAMING_SNAKE_CASE")]
11pub enum RpcQueryError {
12    #[error("There are no fully synchronized blocks on the node yet")]
13    NoSyncedBlocks,
14    #[error("The node does not track the shard ID {requested_shard_id}")]
15    UnavailableShard { requested_shard_id: unc_primitives::types::ShardId },
16    #[error(
17        "The data for block #{block_height} is garbage collected on this node, use an archival node to fetch historical data"
18    )]
19    GarbageCollectedBlock {
20        block_height: unc_primitives::types::BlockHeight,
21        block_hash: unc_primitives::hash::CryptoHash,
22    },
23    #[error("Block either has never been observed on the node or has been garbage collected: {block_reference:?}")]
24    UnknownBlock { block_reference: unc_primitives::types::BlockReference },
25    #[error("Account ID {requested_account_id} is invalid")]
26    InvalidAccount {
27        requested_account_id: unc_primitives::types::AccountId,
28        block_height: unc_primitives::types::BlockHeight,
29        block_hash: unc_primitives::hash::CryptoHash,
30    },
31    #[error("account {requested_account_id} does not exist while viewing")]
32    UnknownAccount {
33        requested_account_id: unc_primitives::types::AccountId,
34        block_height: unc_primitives::types::BlockHeight,
35        block_hash: unc_primitives::hash::CryptoHash,
36    },
37    #[error(
38        "Contract code for contract ID #{contract_account_id} has never been observed on the node"
39    )]
40    NoContractCode {
41        contract_account_id: unc_primitives::types::AccountId,
42        block_height: unc_primitives::types::BlockHeight,
43        block_hash: unc_primitives::hash::CryptoHash,
44    },
45    #[error("State of contract {contract_account_id} is too large to be viewed")]
46    TooLargeContractState {
47        contract_account_id: unc_primitives::types::AccountId,
48        block_height: unc_primitives::types::BlockHeight,
49        block_hash: unc_primitives::hash::CryptoHash,
50    },
51    #[error("Access key for public key {public_key} has never been observed on the node")]
52    UnknownAccessKey {
53        public_key: unc_crypto::PublicKey,
54        block_height: unc_primitives::types::BlockHeight,
55        block_hash: unc_primitives::hash::CryptoHash,
56    },
57    #[error("Chip for public key {public_key} has never been observed on the node")]
58    UnknownChip {
59        public_key: unc_crypto::PublicKey,
60        block_height: unc_primitives::types::BlockHeight,
61        block_hash: unc_primitives::hash::CryptoHash,
62    },
63    #[error("Function call returned an error: {vm_error}")]
64    ContractExecutionError {
65        vm_error: String,
66        block_height: unc_primitives::types::BlockHeight,
67        block_hash: unc_primitives::hash::CryptoHash,
68    },
69    #[error("The node reached its limits. Try again later. More details: {error_message}")]
70    InternalError { error_message: String },
71}
72
73#[derive(serde::Serialize, serde::Deserialize, Debug)]
74pub struct RpcQueryResponse {
75    #[serde(flatten)]
76    pub kind: QueryResponseKind,
77    pub block_height: unc_primitives::types::BlockHeight,
78    pub block_hash: unc_primitives::hash::CryptoHash,
79}
80
81#[derive(serde::Serialize, serde::Deserialize, Debug)]
82#[serde(untagged)]
83pub enum QueryResponseKind {
84    ViewAccount(unc_primitives::views::AccountView),
85    ViewCode(unc_primitives::views::ContractCodeView),
86    ViewState(unc_primitives::views::ViewStateResult),
87    CallResult(unc_primitives::views::CallResult),
88    AccessKey(unc_primitives::views::AccessKeyView),
89    AccessKeyList(unc_primitives::views::AccessKeyList),
90    ChipList(unc_primitives::views::ChipsList),
91}
92
93impl From<RpcQueryError> for crate::errors::RpcError {
94    fn from(error: RpcQueryError) -> Self {
95        let error_data = Some(serde_json::Value::String(error.to_string()));
96        let error_data_value = match serde_json::to_value(error) {
97            Ok(value) => value,
98            Err(err) => {
99                return Self::new_internal_error(
100                    None,
101                    format!("Failed to serialize RpcQueryError: {:?}", err),
102                )
103            }
104        };
105        Self::new_internal_or_handler_error(error_data, error_data_value)
106    }
107}