Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

companion for jsonrpsee v0.10 #11158

Merged
merged 6 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 43 additions & 140 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bin/node-template/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ sp-runtime = { version = "6.0.0", path = "../../../primitives/runtime" }
sp-timestamp = { version = "4.0.0-dev", path = "../../../primitives/timestamp" }

# These dependencies are used for the node template's RPCs
jsonrpsee = { version = "0.9", features = ["server"] }
jsonrpsee = { version = "0.10.1", features = ["server"] }
sc-rpc = { version = "4.0.0-dev", path = "../../../client/rpc" }
sp-api = { version = "4.0.0-dev", path = "../../../primitives/api" }
sc-rpc-api = { version = "0.10.0-dev", path = "../../../client/rpc-api" }
Expand Down
2 changes: 1 addition & 1 deletion bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ crate-type = ["cdylib", "rlib"]
clap = { version = "3.1.6", features = ["derive"], optional = true }
codec = { package = "parity-scale-codec", version = "3.0.0" }
serde = { version = "1.0.136", features = ["derive"] }
jsonrpsee = { version = "0.9", features = ["server"] }
jsonrpsee = { version = "0.10.1", features = ["server"] }
futures = "0.3.21"
hex-literal = "0.3.4"
log = "0.4.8"
Expand Down
2 changes: 2 additions & 0 deletions bin/node/cli/benches/block_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
rpc_cors: None,
rpc_methods: Default::default(),
rpc_max_payload: None,
rpc_max_request_size: None,
rpc_max_response_size: None,
rpc_id_provider: None,
ws_max_out_buffer_capacity: None,
prometheus_config: None,
Expand Down
2 changes: 2 additions & 0 deletions bin/node/cli/benches/transaction_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
rpc_cors: None,
rpc_methods: Default::default(),
rpc_max_payload: None,
rpc_max_request_size: None,
rpc_max_response_size: None,
rpc_id_provider: None,
ws_max_out_buffer_capacity: None,
prometheus_config: None,
Expand Down
2 changes: 1 addition & 1 deletion bin/node/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository = "https:/paritytech/substrate/"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
jsonrpsee = { version = "0.9", features = ["server"] }
jsonrpsee = { version = "0.10.1", features = ["server"] }
node-primitives = { version = "2.0.0", path = "../primitives" }
pallet-contracts-rpc = { version = "4.0.0-dev", path = "../../../frame/contracts/rpc/" }
pallet-mmr-rpc = { version = "3.0.0", path = "../../../frame/merkle-mountain-range/rpc/" }
Expand Down
8 changes: 3 additions & 5 deletions bin/node/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,9 @@ where
use pallet_contracts_rpc::{ContractsApiServer, ContractsRpc};
use pallet_mmr_rpc::{MmrApiServer, MmrRpc};
use pallet_transaction_payment_rpc::{TransactionPaymentApiServer, TransactionPaymentRpc};
// TODO: (dp) need porting
// use sc_rpc::dev::{Dev, DevApi};
use sc_consensus_babe_rpc::BabeApiServer;
use sc_finality_grandpa_rpc::GrandpaApiServer;
use sc_rpc::dev::{Dev, DevApiServer};
use sc_sync_state_rpc::{SyncStateRpc, SyncStateRpcApiServer};
use substrate_frame_rpc_system::{SystemApiServer, SystemRpc};
use substrate_state_trie_migration_rpc::StateMigrationApiServer;
Expand Down Expand Up @@ -178,11 +177,10 @@ where
)?;

io.merge(
substrate_state_trie_migration_rpc::MigrationRpc::new(client, backend, deny_unsafe)
substrate_state_trie_migration_rpc::MigrationRpc::new(client.clone(), backend, deny_unsafe)
.into_rpc(),
)?;
// TODO: (dp) Port to jsonrpsee
// io.extend_with(DevApi::to_delegate(Dev::new(client, deny_unsafe)));
io.merge(Dev::new(client, deny_unsafe).into_rpc())?;

Ok(io)
}
2 changes: 1 addition & 1 deletion client/beefy/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ parking_lot = "0.12.0"
thiserror = "1.0"
serde = { version = "1.0.136", features = ["derive"] }

jsonrpsee = { version = "0.9", features = ["server", "macros"] }
jsonrpsee = { version = "0.10.1", features = ["server", "macros"] }

codec = { version = "3.0.0", package = "parity-scale-codec", features = ["derive"] }

Expand Down
1 change: 1 addition & 0 deletions client/beefy/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub trait BeefyApi<Notification, Hash> {
/// Returns the block most recently finalized by BEEFY, alongside side its justification.
#[subscription(
name = "beefy_subscribeJustifications" => "beefy_justifications",
unsubscribe = "beefy_unsubscribeJustifications",
item = Notification,
)]
fn subscribe_justifications(&self) -> RpcResult<()>;
Expand Down
18 changes: 14 additions & 4 deletions client/cli/src/commands/run_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,28 @@ pub struct RunCmd {
#[clap(long)]
pub unsafe_ws_external: bool,

/// Set the the maximum RPC payload size for both requests and responses (both http and ws), in
/// megabytes. Default is 15MiB.
/// DEPRECATED, this has no affect anymore. Use `rpc_max_request_size` or
/// `rpc_max_response_size` instead.
#[clap(long)]
pub rpc_max_payload: Option<usize>,

/// Set the the maximum RPC request payload size for both HTTP and WS in megabytes.
/// Default is 15MiB.
#[clap(long)]
pub rpc_max_request_size: Option<usize>,

/// Set the the maximum RPC response payload size for both HTTP and WS in megabytes.
/// Default is 15MiB.
#[clap(long)]
pub rpc_max_response_size: Option<usize>,

/// Expose Prometheus exporter on all interfaces.
///
/// Default is local.
#[clap(long)]
pub prometheus_external: bool,

/// Specify IPC RPC server path
/// DEPRECATED, IPC support has been removed.
#[clap(long, value_name = "PATH")]
pub ipc_path: Option<String>,

Expand All @@ -127,7 +137,7 @@ pub struct RunCmd {
#[clap(long, value_name = "COUNT")]
pub ws_max_connections: Option<usize>,

/// Set the the maximum WebSocket output buffer size in MiB. Default is 16.
/// DEPRECATED, this has no affect anymore. Use `rpc_max_response_size` instead.
#[clap(long)]
pub ws_max_out_buffer_capacity: Option<usize>,

Expand Down
12 changes: 12 additions & 0 deletions client/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
Ok(None)
}

/// Get maximum RPC request payload size.
fn rpc_max_request_size(&self) -> Result<Option<usize>> {
Ok(None)
}

/// Get maximum RPC response payload size.
fn rpc_max_response_size(&self) -> Result<Option<usize>> {
Ok(None)
}

/// Get maximum WS output buffer capacity.
fn ws_max_out_buffer_capacity(&self) -> Result<Option<usize>> {
Ok(None)
Expand Down Expand Up @@ -528,6 +538,8 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
rpc_ws_max_connections: self.rpc_ws_max_connections()?,
rpc_cors: self.rpc_cors(is_dev)?,
rpc_max_payload: self.rpc_max_payload()?,
rpc_max_request_size: self.rpc_max_request_size()?,
rpc_max_response_size: self.rpc_max_response_size()?,
rpc_id_provider: None,
ws_max_out_buffer_capacity: self.ws_max_out_buffer_capacity()?,
prometheus_config: self
Expand Down
2 changes: 1 addition & 1 deletion client/consensus/babe/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
jsonrpsee = { version = "0.9", features = ["server", "macros"] }
jsonrpsee = { version = "0.10.1", features = ["server", "macros"] }
sc-consensus-babe = { version = "0.10.0-dev", path = "../" }
sc-rpc-api = { version = "0.10.0-dev", path = "../../../rpc-api" }
sp-consensus-babe = { version = "0.10.0-dev", path = "../../../../primitives/consensus/babe" }
Expand Down
2 changes: 1 addition & 1 deletion client/consensus/manual-seal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
thiserror = "1.0"
jsonrpsee = { version = "0.9", features = ["server", "macros"] }
jsonrpsee = { version = "0.10.1", features = ["server", "macros"] }
futures = "0.3.21"
log = "0.4.8"
codec = { package = "parity-scale-codec", version = "3.0.0" }
Expand Down
2 changes: 1 addition & 1 deletion client/finality-grandpa/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ sp-blockchain = { version = "4.0.0-dev", path = "../../../primitives/blockchain"
sp-core = { version = "6.0.0", path = "../../../primitives/core" }
sp-runtime = { version = "6.0.0", path = "../../../primitives/runtime" }
finality-grandpa = { version = "0.15.0", features = ["derive-codec"] }
jsonrpsee = { version = "0.9", features = ["server", "macros"] }
jsonrpsee = { version = "0.10.1", features = ["server", "macros"] }
futures = "0.3.16"
serde = { version = "1.0.105", features = ["derive"] }
serde_json = "1.0.50"
Expand Down
1 change: 1 addition & 0 deletions client/finality-grandpa/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub trait GrandpaApi<Notification, Hash, Number> {
/// side its justification.
#[subscription(
name = "grandpa_subscribeJustifications" => "grandpa_justifications",
unsubscribe = "grandpa_unsubscribeJustifications",
item = Notification
)]
fn subscribe_justifications(&self) -> RpcResult<()>;
Expand Down
2 changes: 1 addition & 1 deletion client/rpc-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ serde_json = "1.0.79"
sc-transaction-pool-api = { version = "4.0.0-dev", path = "../transaction-pool/api" }
sp-rpc = { version = "6.0.0", path = "../../primitives/rpc" }
sp-tracing = { version = "5.0.0", path = "../../primitives/tracing" }
jsonrpsee = { version = "0.9", features = ["server", "macros"] }
jsonrpsee = { version = "0.10.1", features = ["server", "macros"] }
2 changes: 1 addition & 1 deletion client/rpc-api/src/author/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub trait AuthorApi<Hash, BlockHash> {
/// transaction life cycle.
#[subscription(
name = "author_submitAndWatchExtrinsic" => "author_extrinsicUpdate",
unsubscribe_aliases = ["author_unwatchExtrinsic"],
unsubscribe = "author_unwatchExtrinsic",
item = TransactionStatus<Hash, BlockHash>,
)]
fn watch_extrinsic(&self, bytes: Bytes) -> RpcResult<()>;
Expand Down
5 changes: 3 additions & 2 deletions client/rpc-api/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub trait ChainApi<Number, Hash, Header, SignedBlock> {
/// All head subscription.
#[subscription(
name = "chain_subscribeAllHeads" => "chain_allHead",
unsubscribe = "chain_unsubscribeAllHeads",
item = Header
)]
fn subscribe_all_heads(&self) -> RpcResult<()>;
Expand All @@ -57,7 +58,7 @@ pub trait ChainApi<Number, Hash, Header, SignedBlock> {
#[subscription(
name = "chain_subscribeNewHeads" => "chain_newHead",
aliases = ["subscribe_newHead", "chain_subscribeNewHead"],
unsubscribe_aliases = ["chain_unsubscribeNewHead"],
unsubscribe = "chain_unsubscribeNewHead",
item = Header
)]
fn subscribe_new_heads(&self) -> RpcResult<()>;
Expand All @@ -66,7 +67,7 @@ pub trait ChainApi<Number, Hash, Header, SignedBlock> {
#[subscription(
name = "chain_subscribeFinalizedHeads" => "chain_finalizedHead",
aliases = ["chain_subscribeFinalisedHeads"],
unsubscribe_aliases = ["chain_unsubscribeFinalisedHeads"],
unsubscribe = "chain_unsubscribeFinalisedHeads",
item = Header
)]
fn subscribe_finalized_heads(&self) -> RpcResult<()>;
Expand Down
10 changes: 6 additions & 4 deletions client/rpc-api/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,19 @@ pub trait StateApi<Hash> {
/// New runtime version subscription
#[subscription(
name = "state_subscribeRuntimeVersion" => "state_runtimeVersion",
unsubscribe = "state_unsubscribeRuntimeVersion",
aliases = ["chain_subscribeRuntimeVersion"],
unsubscribe_aliases = ["chain_unsubscribeRuntimeVersion"],
item = RuntimeVersion,
item = RuntimeVersion,
)]
fn subscribe_runtime_version(&self) -> RpcResult<()>;

/// New storage subscription
#[subscription(
name = "state_subscribeStorage" => "state_storage",
item = StorageChangeSet<Hash>,
)]
name = "state_subscribeStorage" => "state_storage",
unsubscribe = "state_unsubscribeStorage",
item = StorageChangeSet<Hash>,
)]
fn subscribe_storage(&self, keys: Option<Vec<StorageKey>>) -> RpcResult<()>;

/// The `traceBlock` RPC provides a way to trace the re-execution of a single
Expand Down
2 changes: 1 addition & 1 deletion client/rpc-servers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
anyhow = "1"
jsonrpsee = { version = "0.9", features = ["server"] }
jsonrpsee = { version = "0.10.1", features = ["server"] }
log = "0.4.8"
prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.10.0-dev"}
serde_json = "1.0.79"
Expand Down
31 changes: 19 additions & 12 deletions client/rpc-servers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ pub type WsServer = WsServerHandle;
pub fn start_http<M: Send + Sync + 'static>(
addrs: &[SocketAddr],
cors: Option<&Vec<String>>,
max_payload_mb: Option<usize>,
max_payload_in_mb: Option<usize>,
max_payload_out_mb: Option<usize>,
metrics: Option<RpcMetrics>,
rpc_api: RpcModule<M>,
rt: tokio::runtime::Handle,
) -> Result<HttpServerHandle, anyhow::Error> {
let max_request_body_size = max_payload_mb
.map(|mb| mb.saturating_mul(MEGABYTE))
.unwrap_or(RPC_MAX_PAYLOAD_DEFAULT);
let max_payload_in = payload_size_or_default(max_payload_in_mb);
let max_payload_out = payload_size_or_default(max_payload_out_mb);

let mut acl = AccessControlBuilder::new();

Expand All @@ -71,18 +71,19 @@ pub fn start_http<M: Send + Sync + 'static>(
};

let builder = HttpServerBuilder::new()
.max_request_body_size(max_request_body_size as u32)
.max_request_body_size(max_payload_in as u32)
.max_response_body_size(max_payload_out as u32)
.set_access_control(acl.build())
.custom_tokio_runtime(rt.clone());

let rpc_api = build_rpc_api(rpc_api);
let handle = if let Some(metrics) = metrics {
let middleware = RpcMiddleware::new(metrics, "http".into());
let builder = builder.set_middleware(middleware);
let server = tokio::task::block_in_place(|| rt.block_on(async { builder.build(addrs) }))?;
let server = tokio::task::block_in_place(|| rt.block_on(builder.build(addrs)))?;
server.start(rpc_api)?
} else {
let server = tokio::task::block_in_place(|| rt.block_on(async { builder.build(addrs) }))?;
let server = tokio::task::block_in_place(|| rt.block_on(builder.build(addrs)))?;
server.start(rpc_api)?
};

Expand All @@ -95,19 +96,21 @@ pub fn start_ws<M: Send + Sync + 'static>(
addrs: &[SocketAddr],
max_connections: Option<usize>,
cors: Option<&Vec<String>>,
max_payload_mb: Option<usize>,
max_payload_in_mb: Option<usize>,
max_payload_out_mb: Option<usize>,
metrics: Option<RpcMetrics>,
rpc_api: RpcModule<M>,
rt: tokio::runtime::Handle,
id_provider: Option<Box<dyn IdProvider>>,
) -> Result<WsServerHandle, anyhow::Error> {
let max_request_body_size = max_payload_mb
.map(|mb| mb.saturating_mul(MEGABYTE))
.unwrap_or(RPC_MAX_PAYLOAD_DEFAULT);
let max_payload_in = payload_size_or_default(max_payload_in_mb);
let max_payload_out = payload_size_or_default(max_payload_out_mb);

let max_connections = max_connections.unwrap_or(WS_MAX_CONNECTIONS);

let mut builder = WsServerBuilder::new()
.max_request_body_size(max_request_body_size as u32)
.max_request_body_size(max_payload_in as u32)
.max_response_body_size(max_payload_out as u32)
.max_connections(max_connections as u64)
.custom_tokio_runtime(rt.clone());

Expand Down Expand Up @@ -163,3 +166,7 @@ fn build_rpc_api<M: Send + Sync + 'static>(mut rpc_api: RpcModule<M>) -> RpcModu

rpc_api
}

fn payload_size_or_default(size_mb: Option<usize>) -> usize {
size_mb.map_or(RPC_MAX_PAYLOAD_DEFAULT, |mb| mb.saturating_mul(MEGABYTE))
}
2 changes: 1 addition & 1 deletion client/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ sc-tracing = { version = "4.0.0-dev", path = "../tracing" }
hash-db = { version = "0.15.2", default-features = false }
parking_lot = "0.12.0"
lazy_static = { version = "1.4.0", optional = true }
jsonrpsee = { version = "0.9", features = ["server"] }
jsonrpsee = { version = "0.10.1", features = ["server"] }
sc-transaction-pool-api = { version = "4.0.0-dev", path = "../transaction-pool/api" }
tokio = { version = "1.15.0", optional = true }

Expand Down
6 changes: 3 additions & 3 deletions client/rpc/src/author/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use jsonrpsee::{
error::{SubscriptionClosed, SubscriptionClosedReason},
Error as RpcError,
},
types::EmptyParams,
types::{error::CallError, EmptyParams},
RpcModule,
};
use sc_transaction_pool::{BasicPool, FullChainApi};
Expand Down Expand Up @@ -107,7 +107,7 @@ async fn author_submit_transaction_should_not_cause_error() {

assert_matches!(
api.call::<_, H256>("author_submitExtrinsic", [xt]).await,
Err(RpcError::Request(e)) if e.contains("Already imported")
Err(RpcError::Call(CallError::Custom { message, ..})) if message.contains("Already imported")
);
}

Expand Down Expand Up @@ -287,7 +287,7 @@ async fn author_has_session_keys() {

assert_matches!(
api.call::<_, bool>("author_hasSessionKeys", vec![Bytes::from(vec![1, 2, 3])]).await,
Err(RpcError::Request(e)) if e.contains("Session keys are not encoded correctly")
Err(RpcError::Call(CallError::Custom { message, ..})) if message.as_str() == "Session keys are not encoded correctly"
);
}

Expand Down
4 changes: 2 additions & 2 deletions client/rpc/src/dev/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use super::*;
use assert_matches::assert_matches;
use jsonrpsee::core::Error as JsonRpseeError;
use jsonrpsee::{core::Error as JsonRpseeError, types::error::CallError};
use sc_block_builder::BlockBuilderProvider;
use sp_blockchain::HeaderBackend;
use sp_consensus::BlockOrigin;
Expand Down Expand Up @@ -64,6 +64,6 @@ async fn deny_unsafe_works() {
assert_matches!(
api.call::<_, Option<BlockStats>>("dev_getBlockStats", [client.info().best_hash])
.await,
Err(JsonRpseeError::Request(e)) if e.to_string().contains("RPC call is unsafe to be called externally")
Err(JsonRpseeError::Call(CallError::Custom { message, .. })) if message.as_str() == "RPC call is unsafe to be called externally"
);
}
Loading