Skip to content

Commit

Permalink
feat(pallet-gear-rpc): Pass gas_allowance multiplier from client (#3226)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitry Novikov <[email protected]>
  • Loading branch information
ukint-vs and breathx authored Sep 25, 2023
1 parent 2389c24 commit dacddf4
Show file tree
Hide file tree
Showing 11 changed files with 523 additions and 184 deletions.
8 changes: 8 additions & 0 deletions node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ pub struct RunCmd {
/// The upper limit for the amount of gas a validator can burn in one block.
#[arg(long)]
pub max_gas: Option<u64>,

/// 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: 53 additions & 11 deletions node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,28 +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)?;
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)?;
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)?;
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)?;
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 @@ -201,7 +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)?;
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 @@ -246,7 +266,11 @@ pub fn run() -> sc_cli::Result<()> {
}
}
BenchmarkCmd::Block(cmd) => {
let (client, _, _, _) = service::new_chain_ops(&config)?;
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 @@ -257,7 +281,11 @@ pub fn run() -> sc_cli::Result<()> {
),
#[cfg(feature = "runtime-benchmarks")]
BenchmarkCmd::Storage(cmd) => {
let (client, backend, _, _) = service::new_chain_ops(&config)?;
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 @@ -268,7 +296,11 @@ pub fn run() -> sc_cli::Result<()> {
sc_cli::Error::from(format!("generating inherent data: {e:?}"))
})?;

let (client, _, _, _) = service::new_chain_ops(&config)?;
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 @@ -286,7 +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)?;
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 @@ -377,8 +413,14 @@ pub fn run() -> sc_cli::Result<()> {
};

runner.run_node_until_exit(|config| async move {
service::new_full(config, cli.no_hardware_benchmarks, cli.run.max_gas)
.map_err(sc_cli::Error::Service)
service::new_full(
config,
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
44 changes: 39 additions & 5 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, $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)?;
} = new_partial::<$scope::RuntimeApi, $executor>(
$config,
$rpc_calculations_multiplier,
$rpc_max_batch_size,
)?;

Ok((
Arc::new(Client::$variant(client)),
Expand All @@ -112,6 +116,8 @@ macro_rules! chain_ops {
#[allow(clippy::type_complexity)]
pub fn new_chain_ops(
config: &Configuration,
rpc_calculations_multiplier: u64,
rpc_max_batch_size: u64,
) -> Result<
(
Arc<Client>,
Expand All @@ -124,11 +130,25 @@ pub fn new_chain_ops(
match &config.chain_spec {
#[cfg(feature = "gear-native")]
spec if spec.is_gear() => {
chain_ops!(config, gear_runtime, GearExecutorDispatch, Gear)
chain_ops!(
config,
rpc_calculations_multiplier,
rpc_max_batch_size,
gear_runtime,
GearExecutorDispatch,
Gear
)
}
#[cfg(feature = "vara-native")]
spec if spec.is_vara() => {
chain_ops!(config, vara_runtime, VaraExecutorDispatch, Vara)
chain_ops!(
config,
rpc_calculations_multiplier,
rpc_max_batch_size,
vara_runtime,
VaraExecutorDispatch,
Vara
)
}
_ => Err("invalid chain spec".into()),
}
Expand All @@ -139,6 +159,8 @@ pub fn new_chain_ops(
#[allow(clippy::type_complexity)]
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 @@ -309,6 +331,10 @@ where
subscription_executor,
finality_provider: finality_proof_provider.clone(),
},
gear: crate::rpc::GearDeps {
allowance_multiplier: rpc_calculations_multiplier,
max_batch_size: rpc_max_batch_size,
},
};

crate::rpc::create_full(deps, rpc_backend.clone()).map_err(Into::into)
Expand Down Expand Up @@ -369,6 +395,8 @@ pub fn new_full_base<RuntimeApi, ExecutorDispatch>(
&sc_consensus_babe::BabeLink<Block>,
),
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 @@ -395,7 +423,7 @@ where
select_chain,
transaction_pool,
other: (rpc_builder, import_setup, rpc_setup, mut telemetry),
} = new_partial(&config)?;
} = 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 @@ -643,6 +671,8 @@ pub fn new_full(
config: Configuration,
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 @@ -651,6 +681,8 @@ pub fn new_full(
disable_hardware_benchmarks,
|_, _| (),
max_gas,
rpc_calculations_multiplier,
rpc_max_batch_size,
)
.map(|NewFullBase { task_manager, .. }| task_manager),
#[cfg(feature = "vara-native")]
Expand All @@ -659,6 +691,8 @@ pub fn new_full(
disable_hardware_benchmarks,
|_, _| (),
max_gas,
rpc_calculations_multiplier,
rpc_max_batch_size,
)
.map(|NewFullBase { task_manager, .. }| task_manager),
_ => Err(ServiceError::Other("Invalid chain spec".into())),
Expand Down
18 changes: 17 additions & 1 deletion node/service/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ pub struct GrandpaDeps<B> {
pub finality_provider: Arc<FinalityProofProvider<B, Block>>,
}

/// Extra dependencies for GEAR.
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.
pub struct FullDeps<C, P, SC, B> {
/// The client instance to use.
Expand All @@ -82,6 +90,8 @@ pub struct FullDeps<C, P, SC, B> {
pub babe: BabeDeps,
/// GRANDPA specific dependencies.
pub grandpa: GrandpaDeps<B>,
/// GEAR specific dependencies.
pub gear: GearDeps,
}

/// Instantiate all Full RPC extensions.
Expand Down Expand Up @@ -131,6 +141,7 @@ where
deny_unsafe,
babe,
grandpa,
gear,
} = deps;

let BabeDeps {
Expand All @@ -146,6 +157,11 @@ where
finality_provider,
} = grandpa;

let GearDeps {
allowance_multiplier,
max_batch_size,
} = gear;

let chain_name = chain_spec.name().to_string();
let genesis_hash = client
.block_hash(0)
Expand Down Expand Up @@ -192,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()).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
19 changes: 18 additions & 1 deletion pallets/gear/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,35 @@ use sp_runtime::traits::Block as BlockT;
use sp_std::vec::Vec;

sp_api::decl_runtime_apis! {
#[api_version(2)]
pub trait GearApi {
#[allow(clippy::too_many_arguments)]
fn calculate_gas_info(source: H256, kind: HandleKind, payload: Vec<u8>, value: u128, allow_other_panics: bool, initial_gas: Option<u64>,) -> Result<GasInfo, Vec<u8>>;
fn calculate_gas_info(source: H256, kind: HandleKind, payload: Vec<u8>, value: u128, allow_other_panics: bool, initial_gas: Option<u64>, allowance_multiplier: Option<u64>) -> Result<GasInfo, Vec<u8>>;

/// Generate inherent-like extrinsic that runs message queue processing.
fn gear_run_extrinsic(max_gas: Option<u64>) -> <Block as BlockT>::Extrinsic;

fn read_state(program_id: H256, payload: Vec<u8>, allowance_multiplier: Option<u64>) -> Result<Vec<u8>, Vec<u8>>;

#[allow(clippy::too_many_arguments)]
fn read_state_using_wasm(program_id: H256, payload: Vec<u8>, fn_name: Vec<u8>, wasm: Vec<u8>, argument: Option<Vec<u8>>, allowance_multiplier: Option<u64>) -> Result<Vec<u8>, Vec<u8>>;

fn read_metahash(program_id: H256, allowance_multiplier: Option<u64>) -> Result<H256, Vec<u8>>;

// DEPRECATED APIS

#[allow(clippy::too_many_arguments)]
#[changed_in(2)]
fn calculate_gas_info(source: H256, kind: HandleKind, payload: Vec<u8>, value: u128, allow_other_panics: bool, initial_gas: Option<u64>) -> Result<GasInfo, Vec<u8>>;

#[changed_in(2)]
fn read_state(program_id: H256, payload: Vec<u8>) -> Result<Vec<u8>, Vec<u8>>;

#[allow(clippy::too_many_arguments)]
#[changed_in(2)]
fn read_state_using_wasm(program_id: H256, payload: Vec<u8>, fn_name: Vec<u8>, wasm: Vec<u8>, argument: Option<Vec<u8>>) -> Result<Vec<u8>, Vec<u8>>;

#[changed_in(2)]
fn read_metahash(program_id: H256) -> Result<H256, Vec<u8>>;
}
}
Loading

0 comments on commit dacddf4

Please sign in to comment.