Skip to content

Commit

Permalink
Address review suggestion, create dummy for testing rpc query method
Browse files Browse the repository at this point in the history
  • Loading branch information
khorolets committed Feb 17, 2021
1 parent da77d8b commit 1cb0e04
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 21 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions chain/chain-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ log = "0.4"
thiserror = "1.0"

near-primitives = { path = "../../core/primitives" }
near-crypto = { path = "../../core/crypto" }
4 changes: 2 additions & 2 deletions chain/chain-primitives/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use near_primitives::types::{BlockHeight, ShardId};

#[derive(thiserror::Error, Debug)]
pub enum QueryError {
#[error("Invalid account ID #{requested_account_id}")]
#[error("Account ID #{requested_account_id} is invalid")]
InvalidAccount { requested_account_id: near_primitives::types::AccountId },
#[error("Account ID #{requested_account_id} does not exist while viewing")]
AccountDoesNotExist { requested_account_id: near_primitives::types::AccountId },
#[error("Contract ID #{contract_account_id} code does not exist while viewing")]
ContractCodeDoesNotExist { contract_account_id: near_primitives::types::AccountId },
#[error("Access key for public key #{public_key} does not exist while viewing")]
AccessKeyDoesNotExist { public_key: String },
AccessKeyDoesNotExist { public_key: near_crypto::PublicKey },
#[error("Storage error occurred: #{storage_error:?}")]
StorageError {
#[from]
Expand Down
1 change: 1 addition & 0 deletions chain/client-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ near-chain-primitives = { path = "../chain-primitives" }
near-chain-configs = { path = "../../core/chain-configs" }

near-chunks = { path = "../chunks" }
near-crypto = { path = "../../core/crypto" }
near-network = { path = "../network" }
near-primitives = { path = "../../core/primitives" }

Expand Down
2 changes: 1 addition & 1 deletion chain/client-primitives/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ pub enum QueryError {
)]
ContractCodeDoesNotExist { contract_account_id: near_primitives::types::AccountId },
#[error("Access key for public key #{public_key} has never been observed on the node")]
AccessKeyDoesNotExist { public_key: String },
AccessKeyDoesNotExist { public_key: near_crypto::PublicKey },
#[error("VM error occurred: #{error_message}")]
VMError { error_message: String },
// NOTE: Currently, the underlying errors are too broad, and while we tried to handle
Expand Down
6 changes: 4 additions & 2 deletions chain/jsonrpc-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ lazy_static = "1.4"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
thiserror = "1.0"
tracing = "0.1.13"
uuid = { version = "~0.8", features = ["v4"] }

near-chain-configs = { path = "../../core/chain-configs" }
near-client-primitives = { path = "../client-primitives" }
near-crypto = { path = "../../core/crypto" }
near-metrics = { path = "../../core/metrics" }
near-primitives = { path = "../../core/primitives" }
near-primitives-core = { path = "../../core/primitives-core" }
near-metrics = { path = "../../core/metrics" }
near-chain-configs = { path = "../../core/chain-configs" }
8 changes: 5 additions & 3 deletions chain/jsonrpc-primitives/src/types/blocks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::error;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
Expand Down Expand Up @@ -64,12 +65,13 @@ impl From<near_client_primitives::types::GetBlockError> for RpcBlockError {
}
near_client_primitives::types::GetBlockError::NotSyncedYet => Self::NotSyncedYet,
near_client_primitives::types::GetBlockError::IOError(s) => Self::InternalError(s),
near_client_primitives::types::GetBlockError::Unreachable(s) => {
near_client_primitives::types::GetBlockError::Unreachable(error_message) => {
error!(target: "jsonrpc", "Unreachable error occurred: {}", &error_message);
near_metrics::inc_counter_vec(
&crate::metrics::RPC_UNREACHABLE_ERROR_COUNT,
&["RpcBlockError", &s],
&["RpcBlockError", &error_message],
);
Self::Unreachable(s)
Self::Unreachable(error_message)
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions chain/jsonrpc-primitives/src/types/chunks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::error;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
Expand Down Expand Up @@ -93,12 +94,13 @@ impl From<near_client_primitives::types::GetChunkError> for RpcChunkError {
near_client_primitives::types::GetChunkError::UnknownChunk(hash) => {
Self::UnknownChunk(hash)
}
near_client_primitives::types::GetChunkError::Unreachable(s) => {
near_client_primitives::types::GetChunkError::Unreachable(error_message) => {
error!(target: "jsonrpc", "Unreachable error occurred: {}", &error_message);
near_metrics::inc_counter_vec(
&crate::metrics::RPC_UNREACHABLE_ERROR_COUNT,
&["RpcChunkError", &s],
&["RpcChunkError", &error_message],
);
Self::Unreachable(s)
Self::Unreachable(error_message)
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions chain/jsonrpc-primitives/src/types/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::types::blocks::BlockReference;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::error;

#[derive(Serialize, Deserialize)]
pub struct RpcProtocolConfigRequest {
Expand Down Expand Up @@ -46,12 +47,13 @@ impl From<near_client_primitives::types::GetProtocolConfigError> for RpcProtocol
near_client_primitives::types::GetProtocolConfigError::IOError(s) => {
Self::InternalError(s)
}
near_client_primitives::types::GetProtocolConfigError::Unreachable(s) => {
near_client_primitives::types::GetProtocolConfigError::Unreachable(error_message) => {
error!(target: "jsonrpc", "Unreachable error occurred: {}", &error_message);
near_metrics::inc_counter_vec(
&crate::metrics::RPC_UNREACHABLE_ERROR_COUNT,
&["RpcProtocolConfigError", &s],
&["RpcProtocolConfigError", &error_message],
);
Self::Unreachable(s)
Self::Unreachable(error_message)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion chain/jsonrpc-primitives/src/types/query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::error;

/// Max size of the query path (soft-deprecated)
const QUERY_DATA_MAX_SIZE: usize = 10 * 1024;
Expand Down Expand Up @@ -29,7 +30,7 @@ pub enum RpcQueryError {
)]
NoContractCode { contract_account_id: near_primitives::types::AccountId },
#[error("Access key for public key #{public_key} has never been observed on the node")]
UnknownAccessKey { public_key: String },
UnknownAccessKey { public_key: near_crypto::PublicKey },
#[error("Function call returned an error: #{vm_error}")]
FunctionCall { vm_error: String },
#[error("The node reached its limits. Try again later. More details: #{error_message}")]
Expand Down Expand Up @@ -148,6 +149,7 @@ impl From<near_client_primitives::types::QueryError> for RpcQueryError {
Self::FunctionCall { vm_error: error_message }
}
near_client_primitives::types::QueryError::Unreachable { error_message } => {
error!(target: "jsonrpc", "Unreachable error occurred: {}", &error_message);
near_metrics::inc_counter_vec(
&crate::metrics::RPC_UNREACHABLE_ERROR_COUNT,
&["RpcQueryError", &error_message],
Expand Down
8 changes: 5 additions & 3 deletions chain/jsonrpc-primitives/src/types/receipts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::error;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReceiptReference {
Expand Down Expand Up @@ -52,12 +53,13 @@ impl From<near_client_primitives::types::GetReceiptError> for RpcReceiptError {
near_client_primitives::types::GetReceiptError::UnknownReceipt(hash) => {
Self::UnknownReceipt(hash)
}
near_client_primitives::types::GetReceiptError::Unreachable(s) => {
near_client_primitives::types::GetReceiptError::Unreachable(error_message) => {
error!(target: "jsonrpc", "Unreachable error occurred: {}", &error_message);
near_metrics::inc_counter_vec(
&crate::metrics::RPC_UNREACHABLE_ERROR_COUNT,
&["RpcReceiptError", &s],
&["RpcReceiptError", &error_message],
);
Self::Unreachable(s)
Self::Unreachable(error_message)
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions chain/jsonrpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use serde::Serialize;
use near_jsonrpc_primitives::errors::RpcError;
use near_jsonrpc_primitives::message::{from_slice, Message};
use near_jsonrpc_primitives::rpc::{
RpcQueryRequest, RpcStateChangesRequest, RpcStateChangesResponse, RpcValidatorsOrderedRequest,
RpcStateChangesRequest, RpcStateChangesResponse, RpcValidatorsOrderedRequest,
};
use near_jsonrpc_primitives::types::query::RpcQueryRequest;
use near_primitives::hash::CryptoHash;
use near_primitives::types::{BlockId, BlockReference, MaybeBlockId, ShardId};
use near_primitives::views::{
Expand Down Expand Up @@ -198,7 +199,10 @@ impl JsonRpcClient {
call_method(&self.client, &self.server_addr, "query", [path, data])
}

pub fn query(&self, request: RpcQueryRequest) -> RpcRequest<QueryResponse> {
pub fn query(
&self,
request: near_jsonrpc_primitives::types::query::RpcQueryRequest,
) -> RpcRequest<near_jsonrpc_primitives::types::query::RpcQueryResponse> {
call_method(&self.client, &self.server_addr, "query", request)
}

Expand Down
2 changes: 1 addition & 1 deletion neard/src/runtime/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl From<node_runtime::state_viewer::errors::ViewAccessKeyError> for WrappedQue
} => Self(QueryError::StorageError { storage_error }),
node_runtime::state_viewer::errors::ViewAccessKeyError::AccessKeyDoesNotExist {
public_key,
} => Self(QueryError::AccessKeyDoesNotExist { public_key: public_key.to_string() }),
} => Self(QueryError::AccessKeyDoesNotExist { public_key }),
node_runtime::state_viewer::errors::ViewAccessKeyError::InternalError {
error_message,
} => Self(QueryError::InternalError { error_message }),
Expand Down
39 changes: 39 additions & 0 deletions neard/tests/rpc_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,3 +728,42 @@ fn test_protocol_config_rpc() {
.unwrap();
});
}

#[test]
fn test_query_rpc() {
init_integration_logger();
heavy_test(|| {
System::builder()
.stop_on_panic(true)
.run(move || {
let num_nodes = 1;
let dirs = (0..num_nodes)
.map(|i| {
tempfile::Builder::new()
.prefix(&format!("protocol_config{}", i))
.tempdir()
.unwrap()
})
.collect::<Vec<_>>();
let (_genesis, rpc_addrs, _) = start_nodes(1, &dirs, 1, 0, 10, 0);

actix::spawn(async move {
let client = new_client(&format!("http://{}", rpc_addrs[0]));
let query_response = client
.query(near_jsonrpc_primitives::types::query::RpcQueryRequest {
block_reference: near_primitives::types::BlockReference::Finality(
Finality::Final,
),
request: near_primitives::views::QueryRequest::ViewAccount {
account_id: "test.near".to_string(),
},
})
.await
.unwrap();
assert_eq!(false, true);
System::current().stop();
});
})
.unwrap();
});
}

0 comments on commit 1cb0e04

Please sign in to comment.