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 server: make possible to disable/enable batch requests #3364

Merged
merged 13 commits into from
Feb 20, 2024
10 changes: 9 additions & 1 deletion cumulus/client/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use codec::Encode;
use sc_chain_spec::ChainSpec;
use sc_client_api::HeaderBackend;
use sc_service::{
config::{PrometheusConfig, TelemetryEndpoints},
config::{PrometheusConfig, RpcBatchRequestConfig, TelemetryEndpoints},
BasePath, TransactionPoolOptions,
};
use sp_core::hexdisplay::HexDisplay;
Expand Down Expand Up @@ -443,6 +443,14 @@ impl sc_cli::CliConfiguration for NormalizedRunCmd {
Ok(self.base.rpc_max_subscriptions_per_connection)
}

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

fn rpc_batch_config(&self) -> sc_cli::Result<RpcBatchRequestConfig> {
self.base.rpc_batch_config()
}

fn transaction_pool(&self, is_dev: bool) -> sc_cli::Result<TransactionPoolOptions> {
self.base.transaction_pool(is_dev)
}
Expand Down
3 changes: 2 additions & 1 deletion cumulus/test/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use sc_network::{
use sc_service::{
config::{
BlocksPruning, DatabaseSource, KeystoreConfig, MultiaddrWithPeerId, NetworkConfiguration,
OffchainWorkerConfig, PruningMode, WasmExecutionMethod,
OffchainWorkerConfig, PruningMode, RpcBatchRequestConfig, WasmExecutionMethod,
},
BasePath, ChainSpec as ChainSpecService, Configuration, Error as ServiceError,
PartialComponents, Role, RpcHandlers, TFullBackend, TFullClient, TaskManager,
Expand Down Expand Up @@ -801,6 +801,7 @@ pub fn node_config(
rpc_max_subs_per_conn: Default::default(),
rpc_port: 9945,
rpc_message_buffer_capacity: Default::default(),
rpc_batch_config: RpcBatchRequestConfig::Unlimited,
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: None,
Expand Down
5 changes: 3 additions & 2 deletions polkadot/node/test/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ use sc_network::{
};
use sc_service::{
config::{
DatabaseSource, KeystoreConfig, MultiaddrWithPeerId, WasmExecutionMethod,
WasmtimeInstantiationStrategy,
DatabaseSource, KeystoreConfig, MultiaddrWithPeerId, RpcBatchRequestConfig,
WasmExecutionMethod, WasmtimeInstantiationStrategy,
},
BasePath, BlocksPruning, Configuration, Role, RpcHandlers, TaskManager,
};
Expand Down Expand Up @@ -186,6 +186,7 @@ pub fn node_config(
rpc_max_subs_per_conn: Default::default(),
rpc_port: 9944,
rpc_message_buffer_capacity: Default::default(),
rpc_batch_config: RpcBatchRequestConfig::Unlimited,
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: None,
Expand Down
12 changes: 12 additions & 0 deletions prdoc/pr_3364.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 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: rpc server expose batch request configuration

doc:
- audience: Node Operator
description: |
Add functionality to limit RPC batch requests by two new CLI options:
--rpc-disable-batch-request - disable batch requests on the server
--rpc_max_batch_request_len - limit batches to LEN on the server
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
crates: [ ]
3 changes: 2 additions & 1 deletion substrate/bin/node/cli/benches/block_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use sc_consensus::{
use sc_service::{
config::{
BlocksPruning, DatabaseSource, KeystoreConfig, NetworkConfiguration, OffchainWorkerConfig,
PruningMode, WasmExecutionMethod, WasmtimeInstantiationStrategy,
PruningMode, RpcBatchRequestConfig, WasmExecutionMethod, WasmtimeInstantiationStrategy,
},
BasePath, Configuration, Role,
};
Expand Down Expand Up @@ -84,6 +84,7 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
rpc_max_subs_per_conn: Default::default(),
rpc_port: 9944,
rpc_message_buffer_capacity: Default::default(),
rpc_batch_config: RpcBatchRequestConfig::Unlimited,
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: None,
Expand Down
3 changes: 2 additions & 1 deletion substrate/bin/node/cli/benches/transaction_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use node_primitives::AccountId;
use sc_service::{
config::{
BlocksPruning, DatabaseSource, KeystoreConfig, NetworkConfiguration, OffchainWorkerConfig,
PruningMode, TransactionPoolOptions,
PruningMode, RpcBatchRequestConfig, TransactionPoolOptions,
},
BasePath, Configuration, Role,
};
Expand Down Expand Up @@ -80,6 +80,7 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
rpc_max_subs_per_conn: Default::default(),
rpc_port: 9944,
rpc_message_buffer_capacity: Default::default(),
rpc_batch_config: RpcBatchRequestConfig::Unlimited,
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: None,
Expand Down
26 changes: 25 additions & 1 deletion substrate/client/cli/src/commands/run_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
use clap::Parser;
use regex::Regex;
use sc_service::{
config::{BasePath, PrometheusConfig, TransactionPoolOptions},
config::{BasePath, PrometheusConfig, RpcBatchRequestConfig, TransactionPoolOptions},
ChainSpec, Role,
};
use sc_telemetry::TelemetryEndpoints;
Expand Down Expand Up @@ -113,6 +113,14 @@ pub struct RunCmd {
#[arg(long, default_value_t = RPC_DEFAULT_MESSAGE_CAPACITY_PER_CONN)]
pub rpc_message_buffer_capacity_per_connection: u32,

/// Disable RPC batch requests
#[arg(long, alias = "rpc_no_batch_requests", conflicts_with_all = &["rpc_max_batch_request_len"])]
pub rpc_disable_batch_requests: bool,

/// Limit the max length per RPC batch request
#[arg(long, conflicts_with_all = &["rpc_disable_batch_requests"], value_name = "LEN")]
pub rpc_max_batch_request_len: Option<u32>,

/// Specify browser *origins* allowed to access the HTTP & WS RPC servers.
///
/// A comma-separated list of origins (protocol://domain or special `null`
Expand Down Expand Up @@ -399,6 +407,22 @@ impl CliConfiguration for RunCmd {
Ok(self.rpc_max_subscriptions_per_connection)
}

fn rpc_buffer_capacity_per_connection(&self) -> Result<u32> {
Copy link
Member Author

@niklasad1 niklasad1 Feb 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR but I noticed that this was missing and only the default value will be used regardless whether someone is using --rpc-buffer-capacity-per-connection X

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh this is the line I missed when I was looking at the PR and only noticed the batch config bits :)

Ok(self.rpc_message_buffer_capacity_per_connection)
}

fn rpc_batch_config(&self) -> Result<RpcBatchRequestConfig> {
let cfg = if self.rpc_disable_batch_requests {
RpcBatchRequestConfig::Disabled
} else if let Some(l) = self.rpc_max_batch_request_len {
RpcBatchRequestConfig::Limit(l)
} else {
RpcBatchRequestConfig::Unlimited
};

Ok(cfg)
}

fn transaction_pool(&self, is_dev: bool) -> Result<TransactionPoolOptions> {
Ok(self.pool_config.transaction_pool(is_dev))
}
Expand Down
9 changes: 8 additions & 1 deletion substrate/client/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use sc_service::{
config::{
BasePath, Configuration, DatabaseSource, KeystoreConfig, NetworkConfiguration,
NodeKeyConfig, OffchainWorkerConfig, OutputFormat, PrometheusConfig, PruningMode, Role,
RpcMethods, TelemetryEndpoints, TransactionPoolOptions, WasmExecutionMethod,
RpcBatchRequestConfig, RpcMethods, TelemetryEndpoints, TransactionPoolOptions,
WasmExecutionMethod,
},
BlocksPruning, ChainSpec, TracingReceiver,
};
Expand Down Expand Up @@ -338,6 +339,11 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
Ok(RPC_DEFAULT_MESSAGE_CAPACITY_PER_CONN)
}

/// RPC server batch request configuration.
fn rpc_batch_config(&self) -> Result<RpcBatchRequestConfig> {
Ok(RpcBatchRequestConfig::Unlimited)
}

/// Get the prometheus configuration (`None` if disabled)
///
/// By default this is `None`.
Expand Down Expand Up @@ -510,6 +516,7 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
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()?,
rpc_batch_config: self.rpc_batch_config()?,
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 @@ -271,6 +271,7 @@ mod tests {
rpc_max_subs_per_conn: Default::default(),
rpc_message_buffer_capacity: Default::default(),
rpc_port: 9944,
rpc_batch_config: sc_service::config::RpcBatchRequestConfig::Unlimited,
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: None,
Expand Down
13 changes: 10 additions & 3 deletions substrate/client/rpc-servers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ use tokio::net::TcpListener;
use tower::Service;
use tower_http::cors::{AllowOrigin, CorsLayer};

pub use jsonrpsee::core::{
id_providers::{RandomIntegerIdProvider, RandomStringIdProvider},
traits::IdProvider,
pub use jsonrpsee::{
core::{
id_providers::{RandomIntegerIdProvider, RandomStringIdProvider},
traits::IdProvider,
},
server::BatchRequestConfig,
};
pub use middleware::{MetricsLayer, RpcMetrics};

Expand Down Expand Up @@ -79,6 +82,8 @@ pub struct Config<'a, M: Send + Sync + 'static> {
pub id_provider: Option<Box<dyn IdProvider>>,
/// Tokio runtime handle.
pub tokio_handle: tokio::runtime::Handle,
/// Batch request config.
pub batch_config: BatchRequestConfig,
}

/// Start RPC server listening on given address.
Expand All @@ -87,6 +92,7 @@ pub async fn start_server<M: Send + Sync + 'static>(
) -> Result<Server, Box<dyn StdError + Send + Sync>> {
let Config {
addrs,
batch_config,
cors,
max_payload_in_mb,
max_payload_out_mb,
Expand Down Expand Up @@ -122,6 +128,7 @@ pub async fn start_server<M: Send + Sync + 'static>(
)
.set_http_middleware(http_middleware)
.set_message_buffer_capacity(message_buffer_capacity)
.set_batch_request_config(batch_config)
.custom_tokio_runtime(tokio_handle.clone());

if let Some(provider) = id_provider {
Expand Down
3 changes: 3 additions & 0 deletions substrate/client/service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

//! Service configuration.

pub use jsonrpsee::server::BatchRequestConfig as RpcBatchRequestConfig;
use prometheus_endpoint::Registry;
use sc_chain_spec::ChainSpec;
pub use sc_client_db::{BlocksPruning, Database, DatabaseSource, PruningMode};
Expand Down Expand Up @@ -102,6 +103,8 @@ pub struct Configuration {
pub rpc_port: u16,
/// The number of messages the JSON-RPC server is allowed to keep in memory.
pub rpc_message_buffer_capacity: u32,
/// JSON-RPC server batch config.
pub rpc_batch_config: RpcBatchRequestConfig,
/// Prometheus endpoint configuration. `None` if disabled.
pub prometheus_config: Option<PrometheusConfig>,
/// Telemetry service URL. `None` if disabled.
Expand Down
1 change: 1 addition & 0 deletions substrate/client/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ where

let server_config = sc_rpc_server::Config {
addrs: [addr, backup_addr],
batch_config: config.rpc_batch_config,
max_connections: config.rpc_max_connections,
max_payload_in_mb: config.rpc_max_request_size,
max_payload_out_mb: config.rpc_max_response_size,
Expand Down
3 changes: 2 additions & 1 deletion substrate/client/service/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use sc_network::{
use sc_network_sync::SyncingService;
use sc_service::{
client::Client,
config::{BasePath, DatabaseSource, KeystoreConfig},
config::{BasePath, DatabaseSource, KeystoreConfig, RpcBatchRequestConfig},
BlocksPruning, ChainSpecExtension, Configuration, Error, GenericChainSpec, Role,
RuntimeGenesis, SpawnTaskHandle, TaskManager,
};
Expand Down Expand Up @@ -254,6 +254,7 @@ fn node_config<
rpc_max_subs_per_conn: Default::default(),
rpc_port: 9944,
rpc_message_buffer_capacity: Default::default(),
rpc_batch_config: RpcBatchRequestConfig::Unlimited,
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: None,
Expand Down
Loading