Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 28 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fb263ce
jsonrpsee v0.20.3
niklasad1 Aug 30, 2023
989ad97
RpcHandlers: fix panic in tokio::mpsc
niklasad1 Oct 30, 2023
310227c
fix host filtering
niklasad1 Oct 30, 2023
635e6f9
Update substrate/client/rpc-spec-v2/src/chain_head/chain_head.rs
niklasad1 Oct 31, 2023
97335eb
fix nits
niklasad1 Oct 31, 2023
49dcc5f
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.20
niklasad1 Oct 31, 2023
875319c
Update substrate/client/rpc-spec-v2/src/chain_head/chain_head.rs
niklasad1 Nov 1, 2023
98da3fc
cargo fmt
niklasad1 Nov 1, 2023
4be7c05
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.20
niklasad1 Nov 1, 2023
b91076e
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.20
niklasad1 Nov 9, 2023
022745d
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.20
niklasad1 Nov 14, 2023
b0b2576
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.20
niklasad1 Jan 4, 2024
48e1e17
cargo fmt
niklasad1 Jan 4, 2024
eac69bc
fix toml fmt
niklasad1 Jan 4, 2024
b70d07c
fix merge nits
niklasad1 Jan 4, 2024
b0434f1
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.20
niklasad1 Jan 4, 2024
e3a62ec
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.20
niklasad1 Jan 19, 2024
34f8f94
address grumbles
niklasad1 Jan 19, 2024
231761e
commit missing file
niklasad1 Jan 19, 2024
ffb52b9
remove needless bounds
niklasad1 Jan 19, 2024
6bb81d4
add missing license header
niklasad1 Jan 22, 2024
8ed4bb7
Merge branch 'master' into na-jsonrpsee-v0.20
niklasad1 Jan 22, 2024
746075f
add prdoc
niklasad1 Jan 22, 2024
71f2bcb
Merge remote-tracking branch 'origin/na-jsonrpsee-v0.20' into na-json…
niklasad1 Jan 22, 2024
72f01e9
Merge branch 'master' into na-jsonrpsee-v0.20
niklasad1 Jan 22, 2024
2d9ee10
fix faulty comment + remove one test
niklasad1 Jan 23, 2024
91a735c
Merge remote-tracking branch 'origin/master' into na-jsonrpsee-v0.20
niklasad1 Jan 23, 2024
b67e888
grumbles: add comment for tokio::MAX_PERMITS
niklasad1 Jan 23, 2024
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
163 changes: 98 additions & 65 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cumulus/client/relay-chain-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ sc-client-api = { path = "../../../substrate/client/api" }
futures = "0.3.28"
async-trait = "0.1.74"
thiserror = "1.0.48"
jsonrpsee-core = "0.16.2"
jsonrpsee-core = "0.20.3"
parity-scale-codec = "3.6.4"
2 changes: 1 addition & 1 deletion cumulus/client/relay-chain-rpc-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tokio-util = { version = "0.7.8", features = ["compat"] }
futures = "0.3.28"
futures-timer = "3.0.2"
parity-scale-codec = "3.6.4"
jsonrpsee = { version = "0.16.2", features = ["ws-client"] }
jsonrpsee = { version = "0.20.3", features = ["ws-client"] }
tracing = "0.1.37"
async-trait = "0.1.74"
url = "2.4.0"
Expand Down
2 changes: 1 addition & 1 deletion cumulus/parachain-template/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ clap = { version = "4.4.18", features = ["derive"] }
log = "0.4.20"
codec = { package = "parity-scale-codec", version = "3.0.0" }
serde = { version = "1.0.195", features = ["derive"] }
jsonrpsee = { version = "0.16.2", features = ["server"] }
jsonrpsee = { version = "0.20.3", features = ["server"] }
futures = "0.3.28"
serde_json = "1.0.111"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,20 +236,14 @@ impl MatchesLocalAndForeignAssetsLocation<xcm::v3::Location>
{
fn is_local(location: &xcm::v3::Location) -> bool {
use assets_common::fungible_conversion::MatchesLocation;
let latest_location: Location = if let Ok(location) = (*location).try_into() {
location
} else {
return false;
};
let latest_location: Location =
if let Ok(location) = (*location).try_into() { location } else { return false };
TrustBackedAssetsConvertedConcreteId::contains(&latest_location)
}
fn is_foreign(location: &xcm::v3::Location) -> bool {
use assets_common::fungible_conversion::MatchesLocation;
let latest_location: Location = if let Ok(location) = (*location).try_into() {
location
} else {
return false;
};
let latest_location: Location =
if let Ok(location) = (*location).try_into() { location } else { return false };
ForeignAssetsConvertedConcreteId::contains(&latest_location)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,15 @@ impl MatchesLocalAndForeignAssetsLocation<xcm::v3::Location>
{
fn is_local(location: &xcm::v3::Location) -> bool {
use assets_common::fungible_conversion::MatchesLocation;
let latest_location: Location = if let Ok(location) = (*location).try_into() {
location
} else {
return false;
};
let latest_location: Location =
if let Ok(location) = (*location).try_into() { location } else { return false };
TrustBackedAssetsConvertedConcreteId::contains(&latest_location)
}

fn is_foreign(location: &xcm::v3::Location) -> bool {
use assets_common::fungible_conversion::MatchesLocation;
let latest_location: Location = if let Ok(location) = (*location).try_into() {
location
} else {
return false;
};
let latest_location: Location =
if let Ok(location) = (*location).try_into() { location } else { return false };
ForeignAssetsConvertedConcreteId::contains(&latest_location)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cumulus/polkadot-parachain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ coretime-rococo-runtime = { path = "../parachains/runtimes/coretime/coretime-roc
coretime-westend-runtime = { path = "../parachains/runtimes/coretime/coretime-westend" }
bridge-hub-westend-runtime = { path = "../parachains/runtimes/bridge-hubs/bridge-hub-westend" }
penpal-runtime = { path = "../parachains/runtimes/testing/penpal" }
jsonrpsee = { version = "0.20.3", features = ["server"] }
people-rococo-runtime = { path = "../parachains/runtimes/people/people-rococo" }
people-westend-runtime = { path = "../parachains/runtimes/people/people-westend" }
jsonrpsee = { version = "0.16.2", features = ["server"] }
parachains-common = { path = "../parachains/common" }

# Substrate
Expand Down
2 changes: 1 addition & 1 deletion cumulus/test/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async-trait = "0.1.74"
clap = { version = "4.4.18", features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.0.0" }
criterion = { version = "0.5.1", features = ["async_tokio"] }
jsonrpsee = { version = "0.16.2", features = ["server"] }
jsonrpsee = { version = "0.20.3", features = ["server"] }
rand = "0.8.5"
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.111"
Expand Down
1 change: 1 addition & 0 deletions cumulus/test/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ pub fn node_config(
rpc_id_provider: None,
rpc_max_subs_per_conn: Default::default(),
rpc_port: 9945,
rpc_message_buffer_capacity: Default::default(),
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: None,
Expand Down
2 changes: 1 addition & 1 deletion docs/sdk/src/guides/your_first_pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ pub mod pallet {
// ensure sender has enough balance, and if so, calculate what is left after `amount`.
let sender_balance = Balances::<T>::get(&sender).ok_or("NonExistentAccount")?;
if sender_balance < amount {
return Err("InsufficientBalance".into());
return Err("InsufficientBalance".into())
}
let reminder = sender_balance - amount;

Expand Down
1 change: 1 addition & 0 deletions polkadot/node/test/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ pub fn node_config(
rpc_id_provider: None,
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 polkadot/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ description = "Polkadot specific RPC functionality."
workspace = true

[dependencies]
jsonrpsee = { version = "0.16.2", features = ["server"] }
jsonrpsee = { version = "0.20.3", features = ["server"] }
polkadot-primitives = { path = "../primitives" }
sc-client-api = { path = "../../substrate/client/api" }
sp-blockchain = { path = "../../substrate/primitives/blockchain" }
Expand Down
2 changes: 1 addition & 1 deletion polkadot/xcm/procedural/src/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pub mod junctions {

pub fn generate_conversion_functions(input: proc_macro::TokenStream) -> Result<TokenStream> {
if !input.is_empty() {
return Err(syn::Error::new(Span::call_site(), "No arguments expected"));
return Err(syn::Error::new(Span::call_site(), "No arguments expected"))
}

let from_slice_syntax = generate_conversion_from_slice_syntax();
Expand Down
13 changes: 3 additions & 10 deletions polkadot/xcm/xcm-builder/src/matches_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,9 @@ use xcm::latest::{InteriorLocation, Location, NetworkId};
pub struct StartsWith<T, L = Location>(sp_std::marker::PhantomData<(T, L)>);
impl<T: Get<L>, L: TryInto<Location> + Clone> Contains<L> for StartsWith<T, L> {
fn contains(location: &L) -> bool {
let latest_location: Location = if let Ok(location) = (*location).clone().try_into() {
location
} else {
return false;
};
let latest_t = if let Ok(location) = T::get().try_into() {
location
} else {
return false;
};
let latest_location: Location =
if let Ok(location) = (*location).clone().try_into() { location } else { return false };
let latest_t = if let Ok(location) = T::get().try_into() { location } else { return false };
latest_location.starts_with(&latest_t)
}
}
Expand Down
18 changes: 18 additions & 0 deletions prdoc/pr_1313.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: backpressured JSON-RPC server (upgrade jsonrpsee)

doc:
- audience: Node Operator
description: |
Modifies the jsonrpc server to be "backpressured" and it's possible to configure
how many messages can be "buffered" via the CLI `--rpc_message_buffer_capacity`.

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

crates:
- name: sc-rpc-server
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
Loading