Skip to content

Commit

Permalink
introduce --rpc_max_gas_allowance arg
Browse files Browse the repository at this point in the history
  • Loading branch information
ukint-vs committed Sep 8, 2023
1 parent 74f3752 commit 528cdff
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 27 deletions.
4 changes: 4 additions & 0 deletions node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ 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 = 600_000_000)]
pub rpc_max_gas_allowance: u64,
}

#[derive(Debug, Parser)]
Expand Down
24 changes: 17 additions & 7 deletions node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,28 +154,32 @@ 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_max_gas_allowance)?;
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_max_gas_allowance)?;
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_max_gas_allowance)?;
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_max_gas_allowance)?;
Ok((cmd.run(client, import_queue), task_manager))
})
}
Expand All @@ -186,7 +190,8 @@ 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_max_gas_allowance)?;
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 @@ -362,8 +367,13 @@ 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_max_gas_allowance,
)
.map_err(sc_cli::Error::Service)
})
}
}
Expand Down
31 changes: 26 additions & 5 deletions node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ 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_max_gas_allowance: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_max_gas_allowance)?;

Ok((
Arc::new(Client::$variant(client)),
Expand All @@ -112,6 +112,7 @@ macro_rules! chain_ops {
#[allow(clippy::type_complexity)]
pub fn new_chain_ops(
config: &Configuration,
rpc_max_gas_allowance: u64,
) -> Result<
(
Arc<Client>,
Expand All @@ -124,11 +125,23 @@ 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_max_gas_allowance,
gear_runtime,
GearExecutorDispatch,
Gear
)
}
#[cfg(feature = "vara-native")]
spec if spec.is_vara() => {
chain_ops!(config, vara_runtime, VaraExecutorDispatch, Vara)
chain_ops!(
config,
rpc_max_gas_allowance,
vara_runtime,
VaraExecutorDispatch,
Vara
)
}
_ => Err("invalid chain spec".into()),
}
Expand All @@ -139,6 +152,7 @@ pub fn new_chain_ops(
#[allow(clippy::type_complexity)]
pub fn new_partial<RuntimeApi, ExecutorDispatch>(
config: &Configuration,
rpc_max_gas_allowance: u64,
) -> Result<
PartialComponents<
FullClient<RuntimeApi, ExecutorDispatch>,
Expand Down Expand Up @@ -309,6 +323,9 @@ where
subscription_executor,
finality_provider: finality_proof_provider.clone(),
},
gear: crate::rpc::GearDeps {
gas_allowance: rpc_max_gas_allowance.clone(),
},
};

crate::rpc::create_full(deps, rpc_backend.clone()).map_err(Into::into)
Expand Down Expand Up @@ -369,6 +386,7 @@ pub fn new_full_base<RuntimeApi, ExecutorDispatch>(
&sc_consensus_babe::BabeLink<Block>,
),
max_gas: Option<u64>,
rpc_max_gas_allowance: u64,
) -> Result<NewFullBase<RuntimeApi, ExecutorDispatch>, ServiceError>
where
RuntimeApi: ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>
Expand All @@ -395,7 +413,7 @@ where
select_chain,
transaction_pool,
other: (rpc_builder, import_setup, rpc_setup, mut telemetry),
} = new_partial(&config)?;
} = new_partial(&config, rpc_max_gas_allowance)?;

let shared_voter_state = rpc_setup;
let grandpa_protocol_name = sc_consensus_grandpa::protocol_standard_name(
Expand Down Expand Up @@ -643,6 +661,7 @@ pub fn new_full(
config: Configuration,
disable_hardware_benchmarks: bool,
max_gas: Option<u64>,
rpc_max_gas_allowance: u64,
) -> Result<TaskManager, ServiceError> {
match &config.chain_spec {
#[cfg(feature = "gear-native")]
Expand All @@ -651,6 +670,7 @@ pub fn new_full(
disable_hardware_benchmarks,
|_, _| (),
max_gas,
rpc_max_gas_allowance,
)
.map(|NewFullBase { task_manager, .. }| task_manager),
#[cfg(feature = "vara-native")]
Expand All @@ -659,6 +679,7 @@ pub fn new_full(
disable_hardware_benchmarks,
|_, _| (),
max_gas,
rpc_max_gas_allowance,
)
.map(|NewFullBase { task_manager, .. }| task_manager),
_ => Err(ServiceError::Other("Invalid chain spec".into())),
Expand Down
13 changes: 12 additions & 1 deletion node/service/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ pub struct GrandpaDeps<B> {
pub finality_provider: Arc<FinalityProofProvider<B, Block>>,
}

/// Extra dependencies for GEAR.
pub struct GearDeps {
/// gas allowance limit.
pub gas_allowance: u64,
}

/// Full client dependencies.
pub struct FullDeps<C, P, SC, B> {
/// The client instance to use.
Expand All @@ -82,6 +88,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 @@ -129,6 +137,7 @@ where
deny_unsafe,
babe,
grandpa,
gear,
} = deps;

let BabeDeps {
Expand All @@ -144,6 +153,8 @@ where
finality_provider,
} = grandpa;

let GearDeps { gas_allowance } = gear;

let chain_name = chain_spec.name().to_string();
let genesis_hash = client
.block_hash(0)
Expand Down Expand Up @@ -190,7 +201,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(), gas_allowance).into_rpc())?;

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

Expand Down
35 changes: 21 additions & 14 deletions pallets/gear/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,16 @@ pub struct Gear<C, P> {
// If you have more generics, no need to Gear<C, M, N, P, ...>
// just use a tuple like Gear<C, (M, N, P, ...)>
client: Arc<C>,
gas_allowance: 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>) -> Self {
pub fn new(client: Arc<C>, gas_allowance: u64) -> Self {
Self {
client,
gas_allowance,
_marker: Default::default(),
}
}
Expand Down Expand Up @@ -261,7 +263,7 @@ where
value,
allow_other_panics,
None,
Some(GAS_ALLOWANCE),
Some(self.gas_allowance),
)
})?;
self.run_with_api_copy(|api| {
Expand All @@ -273,7 +275,7 @@ where
value,
allow_other_panics,
Some(min_limit),
Some(GAS_ALLOWANCE),
Some(self.gas_allowance),
)
})
}
Expand Down Expand Up @@ -338,7 +340,7 @@ where
value,
allow_other_panics,
None,
Some(GAS_ALLOWANCE),
Some(self.gas_allowance),
)
})?;
self.run_with_api_copy(|api| {
Expand All @@ -350,7 +352,7 @@ where
value,
allow_other_panics,
Some(min_limit),
Some(GAS_ALLOWANCE),
Some(self.gas_allowance),
)
})
}
Expand Down Expand Up @@ -415,7 +417,7 @@ where
value,
allow_other_panics,
None,
Some(GAS_ALLOWANCE),
Some(self.gas_allowance),
)
})?;
self.run_with_api_copy(|api| {
Expand All @@ -427,7 +429,7 @@ where
value,
allow_other_panics,
Some(min_limit),
Some(GAS_ALLOWANCE),
Some(self.gas_allowance),
)
})
}
Expand Down Expand Up @@ -501,7 +503,7 @@ where
value,
allow_other_panics,
None,
Some(GAS_ALLOWANCE),
Some(self.gas_allowance),
)
})?;
self.run_with_api_copy(|api| {
Expand All @@ -516,7 +518,7 @@ where
value,
allow_other_panics,
Some(min_limit),
Some(GAS_ALLOWANCE),
Some(self.gas_allowance),
)
})
}
Expand Down Expand Up @@ -551,7 +553,12 @@ where
.map(Bytes)
} else {
self.run_with_api_copy(|api| {
api.read_state(at_hash, program_id, payload.to_vec(), Some(GAS_ALLOWANCE))
api.read_state(
at_hash,
program_id,
payload.to_vec(),
Some(self.gas_allowance),
)
})
.map(Bytes)
}
Expand Down Expand Up @@ -602,7 +609,7 @@ where
.into_iter()
.map(|(program_id, payload)| {
self.run_with_api_copy(|api| {
api.read_state(at_hash, program_id, payload.0, Some(GAS_ALLOWANCE))
api.read_state(at_hash, program_id, payload.0, Some(self.gas_allowance))
})
.map(Bytes)
})
Expand Down Expand Up @@ -656,7 +663,7 @@ where
fn_name.to_vec(),
wasm.to_vec(),
argument.map(|v| v.to_vec()),
Some(GAS_ALLOWANCE),
Some(self.gas_allowance),
)
.map(|r| r.map(Bytes))
})
Expand Down Expand Up @@ -725,7 +732,7 @@ where
fn_name.clone().to_vec(),
wasm.clone().to_vec(),
argument.clone().map(|v| v.to_vec()),
Some(GAS_ALLOWANCE),
Some(self.gas_allowance),
)
.map(|r| r.map(Bytes))
})
Expand Down Expand Up @@ -759,7 +766,7 @@ where
self.run_with_api_copy(|api| api.read_metahash_before_version_2(at_hash, program_id))
} else {
self.run_with_api_copy(|api| {
api.read_metahash(at_hash, program_id, Some(GAS_ALLOWANCE))
api.read_metahash(at_hash, program_id, Some(self.gas_allowance))
})
}
}
Expand Down
1 change: 1 addition & 0 deletions pallets/gear/src/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ where
QueueProcessingOf::<T>::allow();
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn calculate_gas_info_impl(
source: H256,
kind: HandleKind,
Expand Down

0 comments on commit 528cdff

Please sign in to comment.