Skip to content

Commit

Permalink
modules/core: add incoming messages gas limit parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-wh committed Mar 30, 2022
1 parent 879f012 commit c3a024e
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions runtime-sdk/src/modules/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub struct GasCosts {
#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
pub struct Parameters {
pub max_batch_gas: u64,
pub max_in_msgs_gas: u64,
pub max_tx_signers: u32,
pub max_multisig_signers: u32,
pub gas_costs: GasCosts,
Expand All @@ -169,6 +170,9 @@ pub trait API {
/// Returns the remaining batch-wide gas.
fn remaining_batch_gas<C: Context>(ctx: &mut C) -> u64;

/// Returns the remaining batch-wide gas that can be used for roothash incoming messages.
fn remaining_in_msgs_gas<C: Context>(ctx: &mut C) -> u64;

/// Return the remaining tx-wide gas.
fn remaining_tx_gas<C: TxContext>(ctx: &mut C) -> u64;

Expand Down Expand Up @@ -300,6 +304,12 @@ impl<Cfg: Config> API for Module<Cfg> {
batch_gas_limit.saturating_sub(*batch_gas_used)
}

fn remaining_in_msgs_gas<C: Context>(ctx: &mut C) -> u64 {
let in_msgs_gas_limit = Self::params(ctx.runtime_state()).max_in_msgs_gas;
let batch_gas_used = ctx.value::<u64>(CONTEXT_KEY_GAS_USED).or_default();
in_msgs_gas_limit.saturating_sub(*batch_gas_used)
}

fn remaining_tx_gas<C: TxContext>(ctx: &mut C) -> u64 {
let gas_limit = ctx.tx_auth_info().fee.gas;
let gas_used = ctx.tx_value::<u64>(CONTEXT_KEY_GAS_USED).or_default();
Expand Down
7 changes: 7 additions & 0 deletions runtime-sdk/src/modules/core/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn test_use_gas() {
ctx.runtime_state(),
Parameters {
max_batch_gas: BLOCK_MAX_GAS,
max_in_msgs_gas: BLOCK_MAX_GAS / 4,
max_tx_signers: 8,
max_multisig_signers: 8,
gas_costs: Default::default(),
Expand Down Expand Up @@ -126,6 +127,7 @@ fn test_query_min_gas_price() {
ctx.runtime_state(),
Parameters {
max_batch_gas: 10000,
max_in_msgs_gas: 2500,
max_tx_signers: 8,
max_multisig_signers: 8,
gas_costs: Default::default(),
Expand Down Expand Up @@ -243,6 +245,7 @@ impl Runtime for GasWasterRuntime {
super::Genesis {
parameters: Parameters {
max_batch_gas: u64::MAX,
max_in_msgs_gas: u64::MAX,
max_tx_signers: 8,
max_multisig_signers: 8,
gas_costs: super::GasCosts {
Expand Down Expand Up @@ -373,6 +376,7 @@ fn test_approve_unverified_tx() {
ctx.runtime_state(),
Parameters {
max_batch_gas: u64::MAX,
max_in_msgs_gas: u64::MAX,
max_tx_signers: 2,
max_multisig_signers: 2,
gas_costs: Default::default(),
Expand Down Expand Up @@ -587,6 +591,7 @@ fn test_check_weights() {
ctx.runtime_state(),
Parameters {
max_batch_gas: u64::MAX,
max_in_msgs_gas: u64::MAX,
max_tx_signers: 8,
max_multisig_signers: 8,
gas_costs: super::GasCosts {
Expand Down Expand Up @@ -650,6 +655,7 @@ fn test_min_gas_price() {
ctx.runtime_state(),
Parameters {
max_batch_gas: u64::MAX,
max_in_msgs_gas: u64::MAX,
max_tx_signers: 8,
max_multisig_signers: 8,
gas_costs: super::GasCosts {
Expand Down Expand Up @@ -830,6 +836,7 @@ fn test_gas_used_events() {
ctx.runtime_state(),
Parameters {
max_batch_gas: 1_000_000,
max_in_msgs_gas: 250_000,
max_tx_signers: 8,
max_multisig_signers: 8,
gas_costs: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions tests/runtimes/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl sdk::Runtime for Runtime {
modules::core::Genesis {
parameters: modules::core::Parameters {
max_batch_gas: 10_000_000,
max_in_msgs_gas: 2_500_000,
max_tx_signers: 8,
max_multisig_signers: 8,
// These are free, in order to simplify benchmarking.
Expand Down
1 change: 1 addition & 0 deletions tests/runtimes/simple-consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl sdk::Runtime for Runtime {
modules::core::Genesis {
parameters: modules::core::Parameters {
max_batch_gas: 10_000,
max_in_msgs_gas: 2_500,
max_tx_signers: 8,
max_multisig_signers: 8,
// These are free, in order to simplify testing.
Expand Down
1 change: 1 addition & 0 deletions tests/runtimes/simple-contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl sdk::Runtime for Runtime {
modules::core::Genesis {
parameters: modules::core::Parameters {
max_batch_gas: 10_000_000,
max_in_msgs_gas: 2_500_000,
max_tx_signers: 8,
max_multisig_signers: 8,
gas_costs: modules::core::GasCosts {
Expand Down
1 change: 1 addition & 0 deletions tests/runtimes/simple-evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl sdk::Runtime for Runtime {
modules::core::Genesis {
parameters: modules::core::Parameters {
max_batch_gas: 1_000_000,
max_in_msgs_gas: 250_000,
max_tx_signers: 8,
max_multisig_signers: 8,
gas_costs: modules::core::GasCosts {
Expand Down
1 change: 1 addition & 0 deletions tests/runtimes/simple-keyvalue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl sdk::Runtime for Runtime {
modules::core::Genesis {
parameters: modules::core::Parameters {
max_batch_gas: 2_000,
max_in_msgs_gas: 500,
max_tx_signers: 8,
max_multisig_signers: 8,
gas_costs: modules::core::GasCosts {
Expand Down
1 change: 1 addition & 0 deletions tests/runtimes/simple-keyvalue/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fn test_impl_for_tuple() {
ctx.runtime_state(),
core::Parameters {
max_batch_gas: u64::MAX,
max_in_msgs_gas: u64::MAX,
max_tx_signers: 1,
max_multisig_signers: 1,
gas_costs: Default::default(),
Expand Down

0 comments on commit c3a024e

Please sign in to comment.