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

rpc: backpressured RPC server (bump jsonrpsee 0.20) #13992

Open
wants to merge 64 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
d9e1bce
companion for jsonrpsee v0.17
niklasad1 Feb 9, 2023
0a29b07
dummy debug
niklasad1 Feb 24, 2023
e22f92a
rewrite me
niklasad1 Feb 24, 2023
7963722
adjust to custom subscription close type
niklasad1 Mar 20, 2023
5c2f8e6
Merge remote-tracking branch 'origin/na-jsonrpsee-v0.17-dev' into na-…
niklasad1 Mar 20, 2023
1e74b39
hack hack
niklasad1 Mar 20, 2023
8e9ad00
update jsonrpsee
niklasad1 Apr 18, 2023
d3ff524
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.17-dev
niklasad1 Apr 18, 2023
637ad0a
jsonrpsee v0.18
niklasad1 Apr 18, 2023
186cf03
Update client/rpc-servers/src/lib.rs
niklasad1 Apr 24, 2023
4944827
Update client/rpc-servers/src/lib.rs
niklasad1 Apr 24, 2023
da26f68
cleanup
niklasad1 Apr 25, 2023
63f6324
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.18
niklasad1 Apr 25, 2023
26aee9a
Update client/rpc/src/chain/chain_full.rs
niklasad1 Apr 25, 2023
7e01e10
fix rpcv2 tests
niklasad1 Apr 25, 2023
829bfad
Merge remote-tracking branch 'origin/na-jsonrpsee-v0.18' into na-json…
niklasad1 Apr 25, 2023
467493e
fix compile warns
niklasad1 Apr 25, 2023
2473f9e
update jsonrpsee branch
niklasad1 Apr 26, 2023
a67132c
update jsonrpsee v0.18.1
niklasad1 Apr 27, 2023
67cd9a6
remove ugly todo's
niklasad1 Apr 27, 2023
7283a0b
use subscription task executor
niklasad1 Apr 28, 2023
2cdd93f
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.18
niklasad1 Apr 28, 2023
1254b6b
update remote externalities
niklasad1 Apr 28, 2023
1dc07a5
fix nits
niklasad1 Apr 28, 2023
f6e4d86
Update client/rpc/src/lib.rs
niklasad1 Apr 28, 2023
c0f48c0
fix tests
niklasad1 Apr 28, 2023
de38a42
Merge remote-tracking branch 'origin/na-jsonrpsee-v0.18' into na-json…
niklasad1 Apr 28, 2023
cc06028
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.18
niklasad1 May 9, 2023
959ebf0
fix tests
niklasad1 May 9, 2023
da0d083
make it possible to configure buf capacity
niklasad1 May 9, 2023
6ca0bb2
Update client/cli/src/config.rs
niklasad1 May 10, 2023
4e0eff7
Update client/rpc/src/lib.rs
niklasad1 May 10, 2023
c77a429
Update client/cli/src/config.rs
niklasad1 May 10, 2023
cb2697f
Update client/cli/src/config.rs
niklasad1 May 10, 2023
b74462e
address grumbles
niklasad1 May 10, 2023
9e31b66
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.18
niklasad1 May 10, 2023
b5f6d8c
jsonrpsee v0.18.2
niklasad1 May 11, 2023
32c267b
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.18
May 11, 2023
b2d5b9b
Remove the `Copy` bound on `CollectionId` in the uniques pallet (#14111)
koute May 10, 2023
c360e4b
Timeout only if the referendum is not queued (#14106)
gavofyork May 10, 2023
c4bacfc
Include `node-template-release` in workspace (#14103)
ggwpez May 10, 2023
b8ed4bf
Bump `kvdb-rocksdb` to 0.19.0 (#14113)
tdimitrov May 10, 2023
008cef2
Remove `#[pallet::generate_storage_info]` from docs (#14116)
ggwpez May 10, 2023
e9057bc
Bump `Cargo.lock` (#14121)
rcny May 10, 2023
104d269
Prepare `sc-network` for `ProtocolController`/`NotificationService` (…
altonen May 11, 2023
a994ea6
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.18
May 12, 2023
1e4a58f
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.18
niklasad1 May 17, 2023
84c2d99
pin jsonrpsee to commit
niklasad1 May 17, 2023
978b2dd
decrease message buffer to 16
niklasad1 May 17, 2023
4c5a1c1
Merge remote-tracking branch 'origin/na-jsonrpsee-v0.18' into na-json…
niklasad1 May 17, 2023
68ddbc3
rpc helper: unwrap -> unwrap_or
niklasad1 May 18, 2023
d9fa3f7
update jsonrpsee
niklasad1 May 30, 2023
a419864
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.18
niklasad1 May 31, 2023
c3a2bb9
rpc: subscribe_storage use spawn_blocking
niklasad1 Jun 17, 2023
b431bd4
change default rpc message buffer
niklasad1 Jun 17, 2023
5f9e023
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.18
niklasad1 Jun 17, 2023
29b7589
fix nit
niklasad1 Jun 17, 2023
8e16ee0
add some debug logs
niklasad1 Jun 24, 2023
8af945e
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.18
niklasad1 Jun 24, 2023
c7e3e78
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.18
niklasad1 Jun 24, 2023
520aa85
update jsonrpsee
niklasad1 Jun 24, 2023
86f2938
revert me
niklasad1 Jun 27, 2023
0d1544e
update jsonrpsee
niklasad1 Jun 30, 2023
176500e
jsonrpsee v0.19
niklasad1 Jul 31, 2023
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
558 changes: 270 additions & 288 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 @@ -46,7 +46,7 @@ frame-system = { version = "4.0.0-dev", path = "../../../frame/system" }
pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, path = "../../../frame/transaction-payment" }

# These dependencies are used for the node template's RPCs
jsonrpsee = { version = "0.16.2", features = ["server"] }
jsonrpsee = { version = "0.19.0", 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 @@ -39,7 +39,7 @@ array-bytes = "4.1"
clap = { version = "4.2.5", features = ["derive"], optional = true }
codec = { package = "parity-scale-codec", version = "3.6.1" }
serde = { version = "1.0.163", features = ["derive"] }
jsonrpsee = { version = "0.16.2", features = ["server"] }
jsonrpsee = { version = "0.19.0", features = ["server"] }
futures = "0.3.21"
log = "0.4.17"
rand = "0.8"
Expand Down
1 change: 1 addition & 0 deletions bin/node/cli/benches/block_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
rpc_id_provider: Default::default(),
rpc_max_subs_per_conn: Default::default(),
rpc_port: 9944,
rpc_message_buffer_capacity: Default::default(),
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: None,
Expand Down
1 change: 1 addition & 0 deletions bin/node/cli/benches/transaction_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
rpc_id_provider: Default::default(),
rpc_max_subs_per_conn: Default::default(),
rpc_port: 9944,
rpc_message_buffer_capacity: Default::default(),
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: 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 @@ -13,7 +13,7 @@ publish = false
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
jsonrpsee = { version = "0.16.2", features = ["server"] }
jsonrpsee = { version = "0.19.0", features = ["server"] }
node-primitives = { version = "2.0.0", path = "../primitives" }
pallet-transaction-payment-rpc = { version = "4.0.0-dev", path = "../../../frame/transaction-payment/rpc/" }
mmr-rpc = { version = "4.0.0-dev", path = "../../../client/merkle-mountain-range/rpc/" }
Expand Down
19 changes: 17 additions & 2 deletions client/cli/src/commands/run_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
},
CliConfiguration, PrometheusParams, RuntimeParams, TelemetryParams,
RPC_DEFAULT_MAX_CONNECTIONS, RPC_DEFAULT_MAX_REQUEST_SIZE_MB, RPC_DEFAULT_MAX_RESPONSE_SIZE_MB,
RPC_DEFAULT_MAX_SUBS_PER_CONN,
RPC_DEFAULT_MAX_SUBS_PER_CONN, RPC_DEFAULT_MESSAGE_CAPACITY_PER_CONN,
};
use clap::Parser;
use regex::Regex;
Expand Down Expand Up @@ -87,7 +87,7 @@ pub struct RunCmd {
#[arg(long, default_value_t = RPC_DEFAULT_MAX_RESPONSE_SIZE_MB)]
pub rpc_max_response_size: u32,

/// Set the the maximum concurrent subscriptions per connection.
/// Set the maximum concurrent subscriptions per connection.
#[arg(long, default_value_t = RPC_DEFAULT_MAX_SUBS_PER_CONN)]
pub rpc_max_subscriptions_per_connection: u32,

Expand All @@ -107,6 +107,17 @@ pub struct RunCmd {
#[arg(long, value_name = "ORIGINS", value_parser = parse_cors)]
pub rpc_cors: Option<Cors>,

/// The number of messages the RPC server is allowed to keep in memory.
///
/// If the buffer becomes full then the server will not process
/// new messages until the connected client start reading the
/// underlying messages.
///
/// This applies per connection which includes both
/// JSON-RPC methods calls and subscriptions.
#[arg(long, default_value_t = RPC_DEFAULT_MESSAGE_CAPACITY_PER_CONN)]
pub rpc_message_buffer_capacity_per_connection: u32,

/// The human-readable name for this node.
/// It's used as network node name.
#[arg(long, value_name = "NAME")]
Expand Down Expand Up @@ -365,6 +376,10 @@ impl CliConfiguration for RunCmd {
Ok(self.rpc_max_subscriptions_per_connection)
}

fn rpc_buffer_capacity_per_connection(&self) -> Result<u32> {
Ok(self.rpc_message_buffer_capacity_per_connection)
}

fn transaction_pool(&self, is_dev: bool) -> Result<TransactionPoolOptions> {
Ok(self.pool_config.transaction_pool(is_dev))
}
Expand Down
11 changes: 10 additions & 1 deletion client/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ pub const RPC_DEFAULT_MAX_SUBS_PER_CONN: u32 = 1024;
pub const RPC_DEFAULT_MAX_REQUEST_SIZE_MB: u32 = 15;
/// The default max response size in MB.
pub const RPC_DEFAULT_MAX_RESPONSE_SIZE_MB: u32 = 15;
/// The default number of connection..
/// The default concurrent connection limit.
pub const RPC_DEFAULT_MAX_CONNECTIONS: u32 = 100;
/// The default number of messages the RPC server
/// is allowed to keep in memory per connection.
pub const RPC_DEFAULT_MESSAGE_CAPACITY_PER_CONN: u32 = 64;

/// Default configuration values used by Substrate
///
Expand Down Expand Up @@ -346,6 +349,11 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
Ok(RPC_DEFAULT_MAX_SUBS_PER_CONN)
}

/// The number of messages the RPC server is allowed to keep in memory per connection.
fn rpc_buffer_capacity_per_connection(&self) -> Result<u32> {
Ok(RPC_DEFAULT_MESSAGE_CAPACITY_PER_CONN)
}

/// Get the prometheus configuration (`None` if disabled)
///
/// By default this is `None`.
Expand Down Expand Up @@ -518,6 +526,7 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
rpc_id_provider: None,
rpc_max_subs_per_conn: self.rpc_max_subscriptions_per_connection()?,
rpc_port: DCV::rpc_listen_port(),
rpc_message_buffer_capacity: self.rpc_buffer_capacity_per_connection()?,
prometheus_config: self
.prometheus_config(DCV::prometheus_listen_port(), &chain_spec)?,
telemetry_endpoints,
Expand Down
1 change: 1 addition & 0 deletions client/cli/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ mod tests {
rpc_id_provider: Default::default(),
rpc_max_subs_per_conn: Default::default(),
rpc_port: 9944,
rpc_message_buffer_capacity: Default::default(),
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: None,
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.16.2", features = ["client-core", "server", "macros"] }
jsonrpsee = { version = "0.19.0", features = ["client-core", "server", "macros"] }
futures = "0.3.21"
serde = { version = "1.0.163", features = ["derive"] }
thiserror = "1.0"
Expand Down
44 changes: 25 additions & 19 deletions client/consensus/babe/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ use std::{collections::HashMap, sync::Arc};

use futures::TryFutureExt;
use jsonrpsee::{
core::{async_trait, Error as JsonRpseeError, RpcResult},
core::async_trait,
proc_macros::rpc,
types::{error::CallError, ErrorObject},
types::{ErrorObject, ErrorObjectOwned},
};
use serde::{Deserialize, Serialize};

use sc_consensus_babe::{authorship, BabeWorkerHandle};
use sc_consensus_epochs::Epoch as EpochT;
use sc_rpc_api::DenyUnsafe;
use sc_rpc_api::{DenyUnsafe, UnsafeRpcError};
use sp_api::ProvideRuntimeApi;
use sp_application_crypto::AppCrypto;
use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
Expand All @@ -48,7 +48,7 @@ pub trait BabeApi {
/// Returns data about which slots (primary or secondary) can be claimed in the current epoch
/// with the keys in the keystore.
#[method(name = "babe_epochAuthorship")]
async fn epoch_authorship(&self) -> RpcResult<HashMap<AuthorityId, EpochAuthorship>>;
async fn epoch_authorship(&self) -> Result<HashMap<AuthorityId, EpochAuthorship>, Error>;
}

/// Provides RPC methods for interacting with Babe.
Expand Down Expand Up @@ -89,7 +89,7 @@ where
C::Api: BabeRuntimeApi<B>,
SC: SelectChain<B> + Clone + 'static,
{
async fn epoch_authorship(&self) -> RpcResult<HashMap<AuthorityId, EpochAuthorship>> {
async fn epoch_authorship(&self) -> Result<HashMap<AuthorityId, EpochAuthorship>, Error> {
self.deny_unsafe.check_if_safe()?;

let best_header = self.select_chain.best_chain().map_err(Error::SelectChain).await?;
Expand Down Expand Up @@ -147,7 +147,7 @@ where
}

/// Holds information about the `slot`'s that can be claimed by a given key.
#[derive(Default, Debug, Deserialize, Serialize)]
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
pub struct EpochAuthorship {
/// the array of primary slots that can be claimed
primary: Vec<u64>,
Expand All @@ -166,20 +166,26 @@ pub enum Error {
/// Failed to fetch epoch data.
#[error("Failed to fetch epoch data")]
FetchEpoch,
/// Consensus error
#[error(transparent)]
Consensus(#[from] ConsensusError),
/// Errors that can be formatted as a String
#[error("{0}")]
StringError(String),
/// Call to an unsafe RPC was denied.
#[error(transparent)]
UnsafeRpcCalled(#[from] UnsafeRpcError),
}

impl From<Error> for JsonRpseeError {
impl From<Error> for ErrorObjectOwned {
fn from(error: Error) -> Self {
let error_code = match error {
Error::SelectChain(_) => 1,
Error::FetchEpoch => 2,
};

JsonRpseeError::Call(CallError::Custom(ErrorObject::owned(
BABE_ERROR + error_code,
error.to_string(),
Some(format!("{:?}", error)),
)))
match error {
Error::SelectChain(e) => ErrorObject::owned(BABE_ERROR + 1, e.to_string(), None::<()>),
Error::FetchEpoch => ErrorObject::owned(BABE_ERROR + 2, error.to_string(), None::<()>),
Error::Consensus(e) => ErrorObject::owned(BABE_ERROR + 3, e.to_string(), None::<()>),
Error::StringError(e) => ErrorObject::owned(BABE_ERROR + 4, e, None::<()>),
Error::UnsafeRpcCalled(e) => e.into(),
}
}
}

Expand Down Expand Up @@ -246,7 +252,7 @@ mod tests {
let api = babe_rpc.into_rpc();

let request = r#"{"jsonrpc":"2.0","method":"babe_epochAuthorship","params": [],"id":1}"#;
let (response, _) = api.raw_json_request(request).await.unwrap();
let (response, _) = api.raw_json_request(request, 1).await.unwrap();
let expected = r#"{"jsonrpc":"2.0","result":{"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY":{"primary":[0],"secondary":[1,2,4],"secondary_vrf":[]}},"id":1}"#;

assert_eq!(&response.result, expected);
Expand All @@ -258,7 +264,7 @@ mod tests {
let api = babe_rpc.into_rpc();

let request = r#"{"jsonrpc":"2.0","method":"babe_epochAuthorship","params":[],"id":1}"#;
let (response, _) = api.raw_json_request(request).await.unwrap();
let (response, _) = api.raw_json_request(request, 1).await.unwrap();
let expected = r#"{"jsonrpc":"2.0","error":{"code":-32601,"message":"RPC call is unsafe to be called externally"},"id":1}"#;

assert_eq!(&response.result, expected);
Expand Down
8 changes: 4 additions & 4 deletions client/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,10 +1152,10 @@ where
// Verification for imported blocks is skipped in two cases:
// 1. When importing blocks below the last finalized block during network initial
// synchronization.
// 2. When importing whole state we don't calculate epoch descriptor, but rather
// read it from the state after import. We also skip all verifications
// because there's no parent state and we trust the sync module to verify
// that the state is correct and finalized.
// 2. When importing whole state we don't calculate epoch descriptor, but rather read it
// from the state after import. We also skip all verifications because there's no
// parent state and we trust the sync module to verify that the state is correct and
// finalized.
return Ok(block)
}

Expand Down
4 changes: 2 additions & 2 deletions client/consensus/beefy/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ homepage = "https://substrate.io"
[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", features = ["derive"] }
futures = "0.3.21"
jsonrpsee = { version = "0.16.2", features = ["client-core", "server", "macros"] }
jsonrpsee = { version = "0.19.0", features = ["client-core", "server", "macros"] }
log = "0.4"
parking_lot = "0.12.1"
serde = { version = "1.0.163", features = ["derive"] }
Expand All @@ -26,4 +26,4 @@ sp-runtime = { version = "24.0.0", path = "../../../../primitives/runtime" }
serde_json = "1.0.85"
sc-rpc = { version = "4.0.0-dev", features = ["test-helpers"], path = "../../../rpc" }
substrate-test-runtime-client = { version = "2.0.0", path = "../../../../test-utils/runtime/client" }
tokio = { version = "1.22.0", features = ["macros"] }
tokio = { version = "1.22.0", features = ["macros", "time"] }
Loading