Skip to content

Commit

Permalink
introduce --rpc_max_batch_size param
Browse files Browse the repository at this point in the history
  • Loading branch information
ukint-vs committed Sep 22, 2023
1 parent 14e1c5b commit 5294a70
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 29 deletions.
4 changes: 4 additions & 0 deletions node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ pub struct RunCmd {
/// The upper limit for the amount of gas a runtime api can burn in one call.
#[arg(long, default_value_t = 64)]
pub rpc_calculations_multiplier: u64,

/// The upper limit for the amount of calls in rpc batch.
#[arg(long, default_value_t = 256)]
pub rpc_max_batch_size: u64,
}

#[derive(Debug, Parser)]
Expand Down
64 changes: 46 additions & 18 deletions node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,32 +169,44 @@ pub fn run() -> sc_cli::Result<()> {
Some(Subcommand::CheckBlock(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let (client, _, import_queue, task_manager) =
service::new_chain_ops(&config, cli.run.rpc_calculations_multiplier)?;
let (client, _, import_queue, task_manager) = service::new_chain_ops(
&config,
cli.run.rpc_calculations_multiplier,
cli.run.rpc_max_batch_size,
)?;
Ok((cmd.run(client, import_queue), task_manager))
})
}
Some(Subcommand::ExportBlocks(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let (client, _, _, task_manager) =
service::new_chain_ops(&config, cli.run.rpc_calculations_multiplier)?;
let (client, _, _, task_manager) = service::new_chain_ops(
&config,
cli.run.rpc_calculations_multiplier,
cli.run.rpc_max_batch_size,
)?;
Ok((cmd.run(client, config.database), task_manager))
})
}
Some(Subcommand::ExportState(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let (client, _, _, task_manager) =
service::new_chain_ops(&config, cli.run.rpc_calculations_multiplier)?;
let (client, _, _, task_manager) = service::new_chain_ops(
&config,
cli.run.rpc_calculations_multiplier,
cli.run.rpc_max_batch_size,
)?;
Ok((cmd.run(client, config.chain_spec), task_manager))
})
}
Some(Subcommand::ImportBlocks(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let (client, _, import_queue, task_manager) =
service::new_chain_ops(&config, cli.run.rpc_calculations_multiplier)?;
let (client, _, import_queue, task_manager) = service::new_chain_ops(
&config,
cli.run.rpc_calculations_multiplier,
cli.run.rpc_max_batch_size,
)?;
Ok((cmd.run(client, import_queue), task_manager))
})
}
Expand All @@ -205,8 +217,11 @@ pub fn run() -> sc_cli::Result<()> {
Some(Subcommand::Revert(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|config| {
let (client, backend, _, task_manager) =
service::new_chain_ops(&config, cli.run.rpc_calculations_multiplier)?;
let (client, backend, _, task_manager) = service::new_chain_ops(
&config,
cli.run.rpc_calculations_multiplier,
cli.run.rpc_max_batch_size,
)?;
let aux_revert = Box::new(|client, backend, blocks| {
service::revert_backend(client, backend, blocks, config)
.map_err(|err| sc_cli::Error::Application(err.into()))
Expand Down Expand Up @@ -251,8 +266,11 @@ pub fn run() -> sc_cli::Result<()> {
}
}
BenchmarkCmd::Block(cmd) => {
let (client, _, _, _) =
service::new_chain_ops(&config, cli.run.rpc_calculations_multiplier)?;
let (client, _, _, _) = service::new_chain_ops(
&config,
cli.run.rpc_calculations_multiplier,
cli.run.rpc_max_batch_size,
)?;

unwrap_client!(client, cmd.run(client.clone()))
}
Expand All @@ -263,8 +281,11 @@ pub fn run() -> sc_cli::Result<()> {
),
#[cfg(feature = "runtime-benchmarks")]
BenchmarkCmd::Storage(cmd) => {
let (client, backend, _, _) =
service::new_chain_ops(&config, cli.run.rpc_calculations_multiplier)?;
let (client, backend, _, _) = service::new_chain_ops(
&config,
cli.run.rpc_calculations_multiplier,
cli.run.rpc_max_batch_size,
)?;
let db = backend.expose_db();
let storage = backend.expose_storage();

Expand All @@ -275,8 +296,11 @@ pub fn run() -> sc_cli::Result<()> {
sc_cli::Error::from(format!("generating inherent data: {e:?}"))
})?;

let (client, _, _, _) =
service::new_chain_ops(&config, cli.run.rpc_calculations_multiplier)?;
let (client, _, _, _) = service::new_chain_ops(
&config,
cli.run.rpc_calculations_multiplier,
cli.run.rpc_max_batch_size,
)?;
let ext_builder = RemarkBuilder::new(client.clone());

unwrap_client!(
Expand All @@ -294,8 +318,11 @@ pub fn run() -> sc_cli::Result<()> {
let inherent_data = inherent_benchmark_data().map_err(|e| {
sc_cli::Error::from(format!("generating inherent data: {e:?}"))
})?;
let (client, _, _, _) =
service::new_chain_ops(&config, cli.run.rpc_calculations_multiplier)?;
let (client, _, _, _) = service::new_chain_ops(
&config,
cli.run.rpc_calculations_multiplier,
cli.run.rpc_max_batch_size,
)?;
// Register the *Remark* and *TKA* builders.
let ext_factory = ExtrinsicFactory(vec![
Box::new(RemarkBuilder::new(client.clone())),
Expand Down Expand Up @@ -391,6 +418,7 @@ pub fn run() -> sc_cli::Result<()> {
cli.no_hardware_benchmarks,
cli.run.max_gas,
cli.run.rpc_calculations_multiplier,
cli.run.rpc_max_batch_size,
)
.map_err(sc_cli::Error::Service)
})
Expand Down
19 changes: 16 additions & 3 deletions node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,18 @@ type TransactionPool<RuntimeApi, ExecutorDispatch> =
sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>;

macro_rules! chain_ops {
($config:expr, $rpc_calculations_multiplier:expr, $scope:ident, $executor:ident, $variant:ident) => {{
($config:expr, $rpc_calculations_multiplier:expr, $rpc_max_batch_size:expr, $scope:ident, $executor:ident, $variant:ident) => {{
let PartialComponents {
client,
backend,
import_queue,
task_manager,
..
} = new_partial::<$scope::RuntimeApi, $executor>($config, $rpc_calculations_multiplier)?;
} = new_partial::<$scope::RuntimeApi, $executor>(
$config,
$rpc_calculations_multiplier,
$rpc_max_batch_size,
)?;

Ok((
Arc::new(Client::$variant(client)),
Expand All @@ -113,6 +117,7 @@ macro_rules! chain_ops {
pub fn new_chain_ops(
config: &Configuration,
rpc_calculations_multiplier: u64,
rpc_max_batch_size: u64,
) -> Result<
(
Arc<Client>,
Expand All @@ -128,6 +133,7 @@ pub fn new_chain_ops(
chain_ops!(
config,
rpc_calculations_multiplier,
rpc_max_batch_size,
gear_runtime,
GearExecutorDispatch,
Gear
Expand All @@ -138,6 +144,7 @@ pub fn new_chain_ops(
chain_ops!(
config,
rpc_calculations_multiplier,
rpc_max_batch_size,
vara_runtime,
VaraExecutorDispatch,
Vara
Expand All @@ -153,6 +160,7 @@ pub fn new_chain_ops(
pub fn new_partial<RuntimeApi, ExecutorDispatch>(
config: &Configuration,
rpc_calculations_multiplier: u64,
rpc_max_batch_size: u64,
) -> Result<
PartialComponents<
FullClient<RuntimeApi, ExecutorDispatch>,
Expand Down Expand Up @@ -325,6 +333,7 @@ where
},
gear: crate::rpc::GearDeps {
allowance_multiplier: rpc_calculations_multiplier,
max_batch_size: rpc_max_batch_size,
},
};

Expand Down Expand Up @@ -387,6 +396,7 @@ pub fn new_full_base<RuntimeApi, ExecutorDispatch>(
),
max_gas: Option<u64>,
rpc_calculations_multiplier: u64,
rpc_max_batch_size: u64,
) -> Result<NewFullBase<RuntimeApi, ExecutorDispatch>, ServiceError>
where
RuntimeApi: ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>
Expand All @@ -413,7 +423,7 @@ where
select_chain,
transaction_pool,
other: (rpc_builder, import_setup, rpc_setup, mut telemetry),
} = new_partial(&config, rpc_calculations_multiplier)?;
} = new_partial(&config, rpc_calculations_multiplier, rpc_max_batch_size)?;

let shared_voter_state = rpc_setup;
let grandpa_protocol_name = sc_consensus_grandpa::protocol_standard_name(
Expand Down Expand Up @@ -662,6 +672,7 @@ pub fn new_full(
disable_hardware_benchmarks: bool,
max_gas: Option<u64>,
rpc_calculations_multiplier: u64,
rpc_max_batch_size: u64,
) -> Result<TaskManager, ServiceError> {
match &config.chain_spec {
#[cfg(feature = "gear-native")]
Expand All @@ -671,6 +682,7 @@ pub fn new_full(
|_, _| (),
max_gas,
rpc_calculations_multiplier,
rpc_max_batch_size,
)
.map(|NewFullBase { task_manager, .. }| task_manager),
#[cfg(feature = "vara-native")]
Expand All @@ -680,6 +692,7 @@ pub fn new_full(
|_, _| (),
max_gas,
rpc_calculations_multiplier,
rpc_max_batch_size,
)
.map(|NewFullBase { task_manager, .. }| task_manager),
_ => Err(ServiceError::Other("Invalid chain spec".into())),
Expand Down
5 changes: 4 additions & 1 deletion node/service/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub struct GrandpaDeps<B> {
pub struct GearDeps {
/// gas allowance block limit multiplier.
pub allowance_multiplier: u64,
/// maximum batch size for rpc call.
pub max_batch_size: u64,
}

/// Full client dependencies.
Expand Down Expand Up @@ -157,6 +159,7 @@ where

let GearDeps {
allowance_multiplier,
max_batch_size,
} = gear;

let chain_name = chain_spec.name().to_string();
Expand Down Expand Up @@ -205,7 +208,7 @@ where
io.merge(StateMigration::new(client.clone(), backend, deny_unsafe).into_rpc())?;
io.merge(Dev::new(client.clone(), deny_unsafe).into_rpc())?;

io.merge(Gear::new(client.clone(), allowance_multiplier).into_rpc())?;
io.merge(Gear::new(client.clone(), allowance_multiplier, max_batch_size).into_rpc())?;

io.merge(RuntimeInfoApi::<C, Block, B>::new(client.clone()).into_rpc())?;

Expand Down
20 changes: 13 additions & 7 deletions pallets/gear/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ use sp_core::{Bytes, H256};
use sp_runtime::traits::Block as BlockT;
use std::sync::Arc;

const MAX_BATCH_SIZE: usize = 256;

/// Converts a runtime trap into a [`CallError`].
fn runtime_error_into_rpc_error(err: impl std::fmt::Debug) -> JsonRpseeError {
CallError::Custom(ErrorObject::owned(
Expand Down Expand Up @@ -141,15 +139,17 @@ pub struct Gear<C, P> {
// just use a tuple like Gear<C, (M, N, P, ...)>
client: Arc<C>,
allowance_multiplier: u64,
max_batch_size: u64,
_marker: std::marker::PhantomData<P>,
}

impl<C, P> Gear<C, P> {
/// Creates a new instance of the Gear Rpc helper.
pub fn new(client: Arc<C>, allowance_multiplier: u64) -> Self {
pub fn new(client: Arc<C>, allowance_multiplier: u64, max_batch_size: u64) -> Self {
Self {
client,
allowance_multiplier,
max_batch_size,
_marker: Default::default(),
}
}
Expand Down Expand Up @@ -424,11 +424,14 @@ where
batch_id_payload: Vec<(H256, Bytes)>,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<Bytes>> {
if batch_id_payload.len() > MAX_BATCH_SIZE {
if batch_id_payload.len() > self.max_batch_size as usize {
return Err(CallError::Custom(ErrorObject::owned(
8000,
"Runtime error",
Some(format!("Batch size must be lower than {MAX_BATCH_SIZE:?}")),
Some(format!(
"Batch size must be lower than {:?}",
self.max_batch_size
)),
))
.into());
}
Expand Down Expand Up @@ -516,11 +519,14 @@ where
argument: Option<Bytes>,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<Bytes>> {
if batch_id_payload.len() > MAX_BATCH_SIZE {
if batch_id_payload.len() > self.max_batch_size as usize {
return Err(CallError::Custom(ErrorObject::owned(
8000,
"Runtime error",
Some(format!("Batch size must be lower than {MAX_BATCH_SIZE:?}")),
Some(format!(
"Batch size must be lower than {:?}",
self.max_batch_size
)),
))
.into());
}
Expand Down

0 comments on commit 5294a70

Please sign in to comment.