Skip to content

Commit

Permalink
rpc: backpressured RPC server (bump jsonrpsee 0.20) (paritytech#1313)
Browse files Browse the repository at this point in the history
This is a rather big change in jsonrpsee, the major things in this bump
are:
- Server backpressure (the subscription impls are modified to deal with
that)
- Allow custom error types / return types (remove jsonrpsee::core::Error
and jsonrpee::core::CallError)
- Bug fixes (graceful shutdown in particular not used by substrate
anyway)
   - Less dependencies for the clients in particular
   - Return type requires Clone in method call responses
   - Moved to tokio channels
   - Async subscription API (not used in this PR)

Major changes in this PR:
- The subscriptions are now bounded and if subscription can't keep up
with the server it is dropped
- CLI: add parameter to configure the jsonrpc server bounded message
buffer (default is 64)
- Add our own subscription helper to deal with the unbounded streams in
substrate

The most important things in this PR to review is the added helpers
functions in `substrate/client/rpc/src/utils.rs` and the rest is pretty
much chore.

Regarding the "bounded buffer limit" it may cause the server to handle
the JSON-RPC calls
slower than before.

The message size limit is bounded by "--rpc-response-size" thus "by
default 10MB * 64 = 640MB"
but the subscription message size is not covered by this limit and could
be capped as well.

Hopefully the last release prior to 1.0, sorry in advance for a big PR

Previous attempt: paritytech/substrate#13992

Resolves paritytech#748, resolves
paritytech#627
  • Loading branch information
niklasad1 authored Jan 23, 2024
1 parent 5576986 commit e2b6bd5
Show file tree
Hide file tree
Showing 102 changed files with 1,107 additions and 986 deletions.
2 changes: 1 addition & 1 deletion substrate/bin/minimal/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ name = "minimal-node"
clap = { version = "4.4.18", features = ["derive"] }
futures = { version = "0.3.21", features = ["thread-pool"] }
futures-timer = "3.0.1"
jsonrpsee = { version = "0.16.2", features = ["server"] }
jsonrpsee = { version = "0.20.3", features = ["server"] }
serde_json = "1.0.111"

sc-cli = { path = "../../../client/cli" }
Expand Down
2 changes: 1 addition & 1 deletion substrate/bin/node-template/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ frame-system = { path = "../../../frame/system" }
pallet-transaction-payment = { path = "../../../frame/transaction-payment", default-features = false }

# These dependencies are used for the node template's RPCs
jsonrpsee = { version = "0.16.2", features = ["server"] }
jsonrpsee = { version = "0.20.3", features = ["server"] }
sp-api = { path = "../../../primitives/api" }
sc-rpc-api = { path = "../../../client/rpc-api" }
sp-blockchain = { path = "../../../primitives/blockchain" }
Expand Down
2 changes: 1 addition & 1 deletion substrate/bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ array-bytes = "6.1"
clap = { version = "4.4.18", features = ["derive"], optional = true }
codec = { package = "parity-scale-codec", version = "3.6.1" }
serde = { version = "1.0.195", features = ["derive"] }
jsonrpsee = { version = "0.16.2", features = ["server"] }
jsonrpsee = { version = "0.20.3", features = ["server"] }
futures = "0.3.21"
log = "0.4.17"
rand = "0.8"
Expand Down
1 change: 1 addition & 0 deletions substrate/bin/node/cli/benches/block_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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 substrate/bin/node/cli/benches/transaction_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,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 substrate/bin/node/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ workspace = true
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
jsonrpsee = { version = "0.16.2", features = ["server"] }
jsonrpsee = { version = "0.20.3", features = ["server"] }
node-primitives = { path = "../primitives" }
pallet-transaction-payment-rpc = { path = "../../../frame/transaction-payment/rpc" }
mmr-rpc = { path = "../../../client/merkle-mountain-range/rpc" }
Expand Down
17 changes: 14 additions & 3 deletions substrate/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 @@ -102,9 +102,20 @@ pub struct RunCmd {
#[arg(long, value_name = "COUNT", default_value_t = RPC_DEFAULT_MAX_CONNECTIONS)]
pub rpc_max_connections: u32,

/// Specify browser *origins* allowed to access the HTTP and WS RPC servers.
/// The number of messages the RPC server is allowed to keep in memory.
///
/// A comma-separated list of origins (`protocol://domain` or special `null`
/// 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,

/// Specify browser *origins* allowed to access the HTTP & WS RPC servers.
///
/// A comma-separated list of origins (protocol://domain or special `null`
/// value). Value of `all` will disable origin validation. Default is to
/// allow localhost and <https://polkadot.js.org> origins. When running in
/// `--dev` mode the default is to allow all origins.
Expand Down
11 changes: 10 additions & 1 deletion substrate/client/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,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 @@ -330,6 +333,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 @@ -501,6 +509,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 substrate/client/cli/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ mod tests {
rpc_max_response_size: Default::default(),
rpc_id_provider: Default::default(),
rpc_max_subs_per_conn: Default::default(),
rpc_message_buffer_capacity: Default::default(),
rpc_port: 9944,
prometheus_config: None,
telemetry_endpoints: None,
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/consensus/babe/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ workspace = true
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
jsonrpsee = { version = "0.16.2", features = ["client-core", "macros", "server"] }
jsonrpsee = { version = "0.20.3", features = ["client-core", "macros", "server"] }
futures = "0.3.21"
serde = { version = "1.0.195", features = ["derive"] }
thiserror = "1.0"
Expand Down
44 changes: 25 additions & 19 deletions substrate/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 @@ -251,7 +257,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 @@ -263,7 +269,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
2 changes: 1 addition & 1 deletion substrate/client/consensus/beefy/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ workspace = true
[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", features = ["derive"] }
futures = "0.3.21"
jsonrpsee = { version = "0.16.2", features = ["client-core", "macros", "server"] }
jsonrpsee = { version = "0.20.3", features = ["client-core", "macros", "server"] }
log = "0.4"
parking_lot = "0.12.1"
serde = { version = "1.0.195", features = ["derive"] }
Expand Down
49 changes: 18 additions & 31 deletions substrate/client/consensus/beefy/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
use parking_lot::RwLock;
use std::sync::Arc;

use sc_rpc::SubscriptionTaskExecutor;
use sc_rpc::{utils::pipe_from_stream, SubscriptionTaskExecutor};
use sp_runtime::traits::Block as BlockT;

use futures::{task::SpawnError, FutureExt, StreamExt};
use jsonrpsee::{
core::{async_trait, Error as JsonRpseeError, RpcResult},
core::async_trait,
proc_macros::rpc,
types::{error::CallError, ErrorObject, SubscriptionResult},
SubscriptionSink,
types::{ErrorObject, ErrorObjectOwned},
PendingSubscriptionSink,
};
use log::warn;

Expand Down Expand Up @@ -69,15 +69,11 @@ impl From<Error> for ErrorCode {
}
}

impl From<Error> for JsonRpseeError {
impl From<Error> for ErrorObjectOwned {
fn from(error: Error) -> Self {
let message = error.to_string();
let code = ErrorCode::from(error);
JsonRpseeError::Call(CallError::Custom(ErrorObject::owned(
code as i32,
message,
None::<()>,
)))
ErrorObject::owned(code as i32, message, None::<()>)
}
}

Expand All @@ -98,7 +94,7 @@ pub trait BeefyApi<Notification, Hash> {
/// in the network or if the client is still initializing or syncing with the network.
/// In such case an error would be returned.
#[method(name = "beefy_getFinalizedHead")]
async fn latest_finalized(&self) -> RpcResult<Hash>;
async fn latest_finalized(&self) -> Result<Hash, Error>;
}

/// Implements the BeefyApi RPC trait for interacting with BEEFY.
Expand Down Expand Up @@ -138,27 +134,17 @@ impl<Block> BeefyApiServer<notification::EncodedVersionedFinalityProof, Block::H
where
Block: BlockT,
{
fn subscribe_justifications(&self, mut sink: SubscriptionSink) -> SubscriptionResult {
fn subscribe_justifications(&self, pending: PendingSubscriptionSink) {
let stream = self
.finality_proof_stream
.subscribe(100_000)
.map(|vfp| notification::EncodedVersionedFinalityProof::new::<Block>(vfp));

let fut = async move {
sink.pipe_from_stream(stream).await;
};

self.executor.spawn("substrate-rpc-subscription", Some("rpc"), fut.boxed());
Ok(())
sc_rpc::utils::spawn_subscription_task(&self.executor, pipe_from_stream(pending, stream));
}

async fn latest_finalized(&self) -> RpcResult<Block::Hash> {
self.beefy_best_block
.read()
.as_ref()
.cloned()
.ok_or(Error::EndpointNotReady)
.map_err(Into::into)
async fn latest_finalized(&self) -> Result<Block::Hash, Error> {
self.beefy_best_block.read().as_ref().cloned().ok_or(Error::EndpointNotReady)
}
}

Expand All @@ -167,7 +153,7 @@ mod tests {
use super::*;

use codec::{Decode, Encode};
use jsonrpsee::{types::EmptyServerParams as EmptyParams, RpcModule};
use jsonrpsee::{core::EmptyServerParams as EmptyParams, RpcModule};
use sc_consensus_beefy::{
communication::notification::BeefyVersionedFinalityProofSender,
justification::BeefyVersionedFinalityProof,
Expand Down Expand Up @@ -199,7 +185,7 @@ mod tests {
let (rpc, _) = setup_io_handler();
let request = r#"{"jsonrpc":"2.0","method":"beefy_getFinalizedHead","params":[],"id":1}"#;
let expected_response = r#"{"jsonrpc":"2.0","error":{"code":1,"message":"BEEFY RPC endpoint not ready"},"id":1}"#.to_string();
let (response, _) = rpc.raw_json_request(&request).await.unwrap();
let (response, _) = rpc.raw_json_request(&request, 1).await.unwrap();

assert_eq!(expected_response, response.result);
}
Expand Down Expand Up @@ -230,13 +216,13 @@ mod tests {

let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
while std::time::Instant::now() < deadline {
let (response, _) = io.raw_json_request(request).await.expect("RPC requests work");
let (response, _) = io.raw_json_request(request, 1).await.expect("RPC requests work");
if response.result != not_ready {
assert_eq!(response.result, expected);
// Success
return
}
std::thread::sleep(std::time::Duration::from_millis(50))
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}

panic!(
Expand All @@ -249,14 +235,15 @@ mod tests {
let (rpc, _) = setup_io_handler();
// Subscribe call.
let _sub = rpc
.subscribe("beefy_subscribeJustifications", EmptyParams::new())
.subscribe_unbounded("beefy_subscribeJustifications", EmptyParams::new())
.await
.unwrap();

// Unsubscribe with wrong ID
let (response, _) = rpc
.raw_json_request(
r#"{"jsonrpc":"2.0","method":"beefy_unsubscribeJustifications","params":["FOO"],"id":1}"#,
1,
)
.await
.unwrap();
Expand Down Expand Up @@ -284,7 +271,7 @@ mod tests {

// Subscribe
let mut sub = rpc
.subscribe("beefy_subscribeJustifications", EmptyParams::new())
.subscribe_unbounded("beefy_subscribeJustifications", EmptyParams::new())
.await
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion substrate/client/consensus/common/src/block_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub enum ImportResult {
}

/// Auxiliary data associated with an imported block result.
#[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImportedAux {
/// Only the header has been imported. Block body verification was skipped.
pub header_only: bool,
Expand Down
Loading

0 comments on commit e2b6bd5

Please sign in to comment.