diff --git a/chain/jsonrpc-adversarial-primitives/Cargo.toml b/chain/jsonrpc-adversarial-primitives/Cargo.toml index 2d6cf1e1b1b..d95f6d81c78 100644 --- a/chain/jsonrpc-adversarial-primitives/Cargo.toml +++ b/chain/jsonrpc-adversarial-primitives/Cargo.toml @@ -8,8 +8,8 @@ rust-version = "1.60.0" edition = "2021" [dependencies] -serde = { version = "1", features = ["derive"], optional = true } -serde_json = { version = "1", optional = true } +serde = { version = "1", features = ["derive"] } +serde_json = { version = "1" } near-primitives = { path = "../../core/primitives" } near-jsonrpc-primitives = { path = "../jsonrpc-primitives", optional = true } @@ -17,10 +17,6 @@ near-network-primitives = { path = "../network-primitives" } deepsize = { version = "0.2.0", optional = true } [features] -ser_de = [ - "serde_json", - "serde", - "near-jsonrpc-primitives", - "near-network-primitives/test_features" -] -deepsize_feature = ["deepsize", "near-primitives/deepsize_feature"] \ No newline at end of file +server = ["near-jsonrpc-primitives/server"] +test_features = ["near-network-primitives/test_features"] +deepsize_feature = ["deepsize", "near-primitives/deepsize_feature"] diff --git a/chain/jsonrpc-adversarial-primitives/src/lib.rs b/chain/jsonrpc-adversarial-primitives/src/lib.rs index 8c582daeb65..8995fb4b670 100644 --- a/chain/jsonrpc-adversarial-primitives/src/lib.rs +++ b/chain/jsonrpc-adversarial-primitives/src/lib.rs @@ -1,13 +1,8 @@ -#[cfg(feature = "ser_de")] -use near_jsonrpc_primitives::errors::RpcError; use near_network_primitives::types::{Edge, SimpleEdge}; use near_primitives::network::PeerId; -#[cfg(feature = "ser_de")] use serde::Deserialize; -#[cfg(feature = "ser_de")] -use serde_json::Value; -#[cfg_attr(feature = "ser_de", derive(Deserialize))] +#[cfg_attr(feature = "test_features", derive(Deserialize))] #[cfg_attr(feature = "deepsize_feature", derive(deepsize::DeepSizeOf))] pub struct SetRoutingTableRequest { pub add_edges: Option>, @@ -15,26 +10,23 @@ pub struct SetRoutingTableRequest { pub prune_edges: Option, } -#[cfg(feature = "ser_de")] -impl SetRoutingTableRequest { - pub fn parse(value: Option) -> Result { - if let Some(value) = value { - serde_json::from_value(value) - .map_err(|err| RpcError::parse_error(format!("Error {:?}", err))) - } else { - Err(RpcError::parse_error("Require at least one parameter".to_owned())) - } +#[cfg(feature = "server")] +impl near_jsonrpc_primitives::RpcRequest for SetRoutingTableRequest { + fn parse( + value: Option, + ) -> Result { + near_jsonrpc_primitives::utils::parse_params::(value) } } -#[cfg_attr(feature = "ser_de", derive(Deserialize))] +#[derive(Deserialize)] pub struct SetAdvOptionsRequest { pub disable_edge_signature_verification: Option, pub disable_edge_propagation: Option, pub disable_edge_pruning: Option, } -#[cfg_attr(feature = "ser_de", derive(Deserialize))] +#[derive(Deserialize)] pub struct StartRoutingTableSyncRequest { pub peer_id: PeerId, } diff --git a/chain/jsonrpc-primitives/Cargo.toml b/chain/jsonrpc-primitives/Cargo.toml index c570565d1cf..b8db5e3a764 100644 --- a/chain/jsonrpc-primitives/Cargo.toml +++ b/chain/jsonrpc-primitives/Cargo.toml @@ -31,4 +31,5 @@ near-primitives-core = { path = "../../core/primitives-core" } near-rpc-error-macro = { path = "../../tools/rpctypegen/macro" } [features] +server = [] test_features = [] diff --git a/chain/jsonrpc-primitives/src/lib.rs b/chain/jsonrpc-primitives/src/lib.rs index 3cfd6d57947..c97fd3b6cf6 100644 --- a/chain/jsonrpc-primitives/src/lib.rs +++ b/chain/jsonrpc-primitives/src/lib.rs @@ -2,4 +2,10 @@ pub mod errors; pub mod message; pub(crate) mod metrics; pub mod types; -pub(crate) mod utils; +#[cfg(feature = "server")] +pub mod utils; + +#[cfg(feature = "server")] +pub trait RpcRequest: Sized { + fn parse(value: Option) -> Result; +} diff --git a/chain/jsonrpc-primitives/src/types/blocks.rs b/chain/jsonrpc-primitives/src/types/blocks.rs index 06b92bbacb2..67a7b14226d 100644 --- a/chain/jsonrpc-primitives/src/types/blocks.rs +++ b/chain/jsonrpc-primitives/src/types/blocks.rs @@ -83,8 +83,9 @@ impl From for crate::errors::RpcError { } } -impl RpcBlockRequest { - pub fn parse(value: Option) -> Result { +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcBlockRequest { + fn parse(value: Option) -> Result { let block_reference = if let Ok((block_id,)) = crate::utils::parse_params::<(near_primitives::types::BlockId,)>(value.clone()) { diff --git a/chain/jsonrpc-primitives/src/types/changes.rs b/chain/jsonrpc-primitives/src/types/changes.rs index d215d17c17f..a48715ae17c 100644 --- a/chain/jsonrpc-primitives/src/types/changes.rs +++ b/chain/jsonrpc-primitives/src/types/changes.rs @@ -41,15 +41,17 @@ pub enum RpcStateChangesError { InternalError { error_message: String }, } -impl RpcStateChangesInBlockRequest { - pub fn parse(value: Option) -> Result { - Ok(crate::utils::parse_params::(value)?) +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcStateChangesInBlockRequest { + fn parse(value: Option) -> Result { + crate::utils::parse_params::(value) } } -impl RpcStateChangesInBlockByTypeRequest { - pub fn parse(value: Option) -> Result { - Ok(crate::utils::parse_params::(value)?) +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcStateChangesInBlockByTypeRequest { + fn parse(value: Option) -> Result { + crate::utils::parse_params::(value) } } diff --git a/chain/jsonrpc-primitives/src/types/chunks.rs b/chain/jsonrpc-primitives/src/types/chunks.rs index eda4827af9d..373b2faf47e 100644 --- a/chain/jsonrpc-primitives/src/types/chunks.rs +++ b/chain/jsonrpc-primitives/src/types/chunks.rs @@ -55,8 +55,9 @@ impl From for near_client_primitives::types::GetChunk { } } -impl RpcChunkRequest { - pub fn parse(value: Option) -> Result { +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcChunkRequest { + fn parse(value: Option) -> Result { // Try to parse legacy positioned args and if it fails parse newer named args let chunk_reference = if let Ok((chunk_id,)) = crate::utils::parse_params::<(near_primitives::hash::CryptoHash,)>(value.clone()) diff --git a/chain/jsonrpc-primitives/src/types/config.rs b/chain/jsonrpc-primitives/src/types/config.rs index 73f06e9fb5f..8408613bb20 100644 --- a/chain/jsonrpc-primitives/src/types/config.rs +++ b/chain/jsonrpc-primitives/src/types/config.rs @@ -7,8 +7,9 @@ pub struct RpcProtocolConfigRequest { pub block_reference: near_primitives::types::BlockReference, } -impl RpcProtocolConfigRequest { - pub fn parse( +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcProtocolConfigRequest { + fn parse( value: Option, ) -> Result { crate::utils::parse_params::(value) diff --git a/chain/jsonrpc-primitives/src/types/gas_price.rs b/chain/jsonrpc-primitives/src/types/gas_price.rs index 5afc5e47fdd..a3d5b067fff 100644 --- a/chain/jsonrpc-primitives/src/types/gas_price.rs +++ b/chain/jsonrpc-primitives/src/types/gas_price.rs @@ -76,8 +76,9 @@ impl From for crate::errors::RpcError { } } -impl RpcGasPriceRequest { - pub fn parse(value: Option) -> Result { +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcGasPriceRequest { + fn parse(value: Option) -> Result { crate::utils::parse_params::<(MaybeBlockId,)>(value) .map(|(block_id,)| RpcGasPriceRequest { block_id }) } diff --git a/chain/jsonrpc-primitives/src/types/light_client.rs b/chain/jsonrpc-primitives/src/types/light_client.rs index 47403e0c3f7..b2122372824 100644 --- a/chain/jsonrpc-primitives/src/types/light_client.rs +++ b/chain/jsonrpc-primitives/src/types/light_client.rs @@ -67,20 +67,22 @@ pub enum RpcLightClientNextBlockError { EpochOutOfBounds { epoch_id: near_primitives::types::EpochId }, } -impl RpcLightClientExecutionProofRequest { - pub fn parse(value: Option) -> Result { - Ok(crate::utils::parse_params::(value)?) +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcLightClientExecutionProofRequest { + fn parse(value: Option) -> Result { + crate::utils::parse_params::(value) } } -impl RpcLightClientNextBlockRequest { - pub fn parse(value: Option) -> Result { +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcLightClientNextBlockRequest { + fn parse(value: Option) -> Result { if let Ok((last_block_hash,)) = crate::utils::parse_params::<(near_primitives::hash::CryptoHash,)>(value.clone()) { Ok(Self { last_block_hash }) } else { - Ok(crate::utils::parse_params::(value)?) + crate::utils::parse_params::(value) } } } diff --git a/chain/jsonrpc-primitives/src/types/query.rs b/chain/jsonrpc-primitives/src/types/query.rs index 46a545fb443..00ecf1b39e7 100644 --- a/chain/jsonrpc-primitives/src/types/query.rs +++ b/chain/jsonrpc-primitives/src/types/query.rs @@ -89,11 +89,10 @@ pub enum QueryResponseKind { AccessKeyList(near_primitives::views::AccessKeyList), } -impl RpcQueryRequest { - pub fn parse(value: Option) -> Result { - let query_request = if let Ok((path, data)) = - crate::utils::parse_params::<(String, String)>(value.clone()) - { +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcQueryRequest { + fn parse(value: Option) -> Result { + if let Ok((path, data)) = crate::utils::parse_params::<(String, String)>(value.clone()) { // Handle a soft-deprecated version of the query API, which is based on // positional arguments with a "path"-style first argument. // @@ -155,14 +154,12 @@ impl RpcQueryRequest { } }; // Use Finality::None here to make backward compatibility tests work - RpcQueryRequest { + return Ok(RpcQueryRequest { request, block_reference: near_primitives::types::BlockReference::latest(), - } - } else { - crate::utils::parse_params::(value)? - }; - Ok(query_request) + }); + } + crate::utils::parse_params::(value) } } diff --git a/chain/jsonrpc-primitives/src/types/receipts.rs b/chain/jsonrpc-primitives/src/types/receipts.rs index 1065674c1d1..558dfa31bbc 100644 --- a/chain/jsonrpc-primitives/src/types/receipts.rs +++ b/chain/jsonrpc-primitives/src/types/receipts.rs @@ -33,8 +33,9 @@ impl From for near_client_primitives::types::GetReceipt { } } -impl RpcReceiptRequest { - pub fn parse(value: Option) -> Result { +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcReceiptRequest { + fn parse(value: Option) -> Result { let receipt_reference = crate::utils::parse_params::(value)?; Ok(Self { receipt_reference }) } diff --git a/chain/jsonrpc-primitives/src/types/sandbox.rs b/chain/jsonrpc-primitives/src/types/sandbox.rs index d5d149e5cbd..9e297ff4f17 100644 --- a/chain/jsonrpc-primitives/src/types/sandbox.rs +++ b/chain/jsonrpc-primitives/src/types/sandbox.rs @@ -8,9 +8,10 @@ pub struct RpcSandboxPatchStateRequest { pub records: Vec, } -impl RpcSandboxPatchStateRequest { - pub fn parse(value: Option) -> Result { - Ok(crate::utils::parse_params::(value)?) +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcSandboxPatchStateRequest { + fn parse(value: Option) -> Result { + crate::utils::parse_params::(value) } } @@ -50,9 +51,10 @@ pub struct RpcSandboxFastForwardRequest { pub delta_height: BlockHeightDelta, } -impl RpcSandboxFastForwardRequest { - pub fn parse(value: Option) -> Result { - Ok(crate::utils::parse_params::(value)?) +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcSandboxFastForwardRequest { + fn parse(value: Option) -> Result { + crate::utils::parse_params::(value) } } diff --git a/chain/jsonrpc-primitives/src/types/transactions.rs b/chain/jsonrpc-primitives/src/types/transactions.rs index f5f948e6f64..6f1e8233d4c 100644 --- a/chain/jsonrpc-primitives/src/types/transactions.rs +++ b/chain/jsonrpc-primitives/src/types/transactions.rs @@ -53,15 +53,17 @@ pub struct RpcBroadcastTxSyncResponse { pub transaction_hash: near_primitives::hash::CryptoHash, } -impl RpcBroadcastTransactionRequest { - pub fn parse(value: Option) -> Result { +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcBroadcastTransactionRequest { + fn parse(value: Option) -> Result { let signed_transaction = crate::utils::parse_signed_transaction(value)?; Ok(Self { signed_transaction }) } } -impl RpcTransactionStatusCommonRequest { - pub fn parse(value: Option) -> Result { +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcTransactionStatusCommonRequest { + fn parse(value: Option) -> Result { if let Ok((hash, account_id)) = crate::utils::parse_params::<( near_primitives::hash::CryptoHash, AccountId, diff --git a/chain/jsonrpc-primitives/src/types/validator.rs b/chain/jsonrpc-primitives/src/types/validator.rs index 14b64fcf050..79ad6df0cae 100644 --- a/chain/jsonrpc-primitives/src/types/validator.rs +++ b/chain/jsonrpc-primitives/src/types/validator.rs @@ -63,8 +63,9 @@ impl From for RpcValidatorError { } } -impl RpcValidatorRequest { - pub fn parse(value: Option) -> Result { +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcValidatorRequest { + fn parse(value: Option) -> Result { let epoch_reference = if let Ok((block_id,)) = crate::utils::parse_params::<(near_primitives::types::MaybeBlockId,)>(value.clone()) { @@ -79,9 +80,10 @@ impl RpcValidatorRequest { } } -impl RpcValidatorsOrderedRequest { - pub fn parse(value: Option) -> Result { - Ok(crate::utils::parse_params::(value)?) +#[cfg(feature = "server")] +impl crate::RpcRequest for RpcValidatorsOrderedRequest { + fn parse(value: Option) -> Result { + crate::utils::parse_params::(value) } } diff --git a/chain/jsonrpc-primitives/src/utils.rs b/chain/jsonrpc-primitives/src/utils.rs index f19b03e4715..07017d54f48 100644 --- a/chain/jsonrpc-primitives/src/utils.rs +++ b/chain/jsonrpc-primitives/src/utils.rs @@ -3,7 +3,7 @@ use serde_json::Value; use near_primitives::borsh::BorshDeserialize; -pub(crate) fn parse_params( +pub fn parse_params( value: Option, ) -> Result { if let Some(value) = value { @@ -14,7 +14,7 @@ pub(crate) fn parse_params( } } -pub(crate) fn parse_signed_transaction( +pub fn parse_signed_transaction( value: Option, ) -> Result { let (encoded,) = crate::utils::parse_params::<(String,)>(value.clone())?; diff --git a/chain/jsonrpc/Cargo.toml b/chain/jsonrpc/Cargo.toml index 45b09a0fad3..f4a6a62c98a 100644 --- a/chain/jsonrpc/Cargo.toml +++ b/chain/jsonrpc/Cargo.toml @@ -29,8 +29,8 @@ near-client = { path = "../client" } near-network = { path = "../network" } near-o11y = { path = "../../core/o11y" } near-jsonrpc-client = { path = "client" } -near-jsonrpc-primitives = { path = "../jsonrpc-primitives" } -near-jsonrpc-adversarial-primitives = { path = "../jsonrpc-adversarial-primitives", optional = true } +near-jsonrpc-primitives = { path = "../jsonrpc-primitives", features = ["server"] } +near-jsonrpc-adversarial-primitives = { path = "../jsonrpc-adversarial-primitives", features = ["server"], optional = true } near-rpc-error-macro = { path = "../../tools/rpctypegen/macro" } near-network-primitives = { path = "../network-primitives" } @@ -40,7 +40,7 @@ test_features = [ "near-client/test_features", "near-network/test_features", "near-jsonrpc-primitives/test_features", - "near-jsonrpc-adversarial-primitives/ser_de", + "near-jsonrpc-adversarial-primitives/test_features", ] nightly = ["nightly_protocol"] nightly_protocol = ["near-primitives/nightly_protocol"] diff --git a/chain/jsonrpc/src/lib.rs b/chain/jsonrpc/src/lib.rs index 9167ab40cf8..23411bc26c7 100644 --- a/chain/jsonrpc/src/lib.rs +++ b/chain/jsonrpc/src/lib.rs @@ -23,7 +23,7 @@ use near_client::{ pub use near_jsonrpc_client as client; use near_jsonrpc_primitives::errors::RpcError; use near_jsonrpc_primitives::message::{Message, Request}; -use near_jsonrpc_primitives::types::config::RpcProtocolConfigResponse; +use near_jsonrpc_primitives::RpcRequest; use near_metrics::{Encoder, TextEncoder}; use near_network::types::{NetworkClientMessages, NetworkClientResponses}; use near_primitives::hash::CryptoHash; @@ -923,7 +923,7 @@ impl JsonRpcHandler { .view_client_addr .send(GetProtocolConfig(request_data.block_reference.into())) .await??; - Ok(RpcProtocolConfigResponse { config_view }) + Ok(near_jsonrpc_primitives::types::config::RpcProtocolConfigResponse { config_view }) } async fn query(