From 1a70cf3670a182f286f1c9a9050808f42431db52 Mon Sep 17 00:00:00 2001 From: koushiro Date: Tue, 7 Feb 2023 10:58:15 +0800 Subject: [PATCH 1/6] moonbase: improve weight per gas calculation --- runtime/common/src/lib.rs | 28 ++++++++++++- runtime/moonbase/src/governance/origins.rs | 2 - runtime/moonbase/src/lib.rs | 45 ++++++++++----------- runtime/moonriver/src/governance/origins.rs | 1 - 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 02cfe44cbb..1566e1dc2a 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -16,7 +16,9 @@ #![cfg_attr(not(feature = "std"), no_std)] +use frame_support::weights::constants::WEIGHT_REF_TIME_PER_MILLIS; use sp_core::H160; +use sp_runtime::Perbill; mod apis; mod impl_moonbeam_xcm_call; @@ -25,9 +27,33 @@ mod impl_on_charge_evm_transaction; mod impl_self_contained_call; pub mod migrations; +/// `WeightPerGas` is an approximate ratio of the amount of Weight per Gas. +/// u64 works for approximations because Weight is a very small unit compared to gas. +/// +/// `GAS_PER_MILLIS * WEIGHT_MILLIS_PER_BLOCK * TXN_RATIO ~= BLOCK_GAS_LIMIT` +/// `WEIGHT_PER_GAS = WEIGHT_REF_TIME_PER_MILLIS / GAS_PER_MILLIS +/// = WEIGHT_REF_TIME_PER_MILLIS / (BLOCK_GAS_LIMIT / TXN_RATIO / WEIGHT_MILLIS_PER_BLOCK) +/// = TXN_RATIO * (WEIGHT_REF_TIME_PER_MILLIS * WEIGHT_MILLIS_PER_BLOCK) / BLOCK_GAS_LIMIT` +/// +/// For example, given the 500ms Weight, from which 75% only are used for transactions, +/// the total EVM execution gas limit is `GAS_PER_MILLIS * 500 * 75% = BLOCK_GAS_LIMIT`. +pub fn weight_per_gas( + block_gas_limit: u64, + txn_ratio: Perbill, + weight_millis_per_block: u64, +) -> u64 { + let weight_per_block = WEIGHT_REF_TIME_PER_MILLIS.saturating_mul(weight_millis_per_block); + let weight_per_gas = (txn_ratio * weight_per_block).saturating_div(block_gas_limit); + assert!( + weight_per_gas >= 1, + "WeightPerGas must greater than or equal with 1" + ); + weight_per_gas +} + //TODO maybe this should be upstreamed into Frontier. -/// And ipmlementation of Frontier's AddressMapping trait for Moonbeam Accounts. +/// And implementation of Frontier's AddressMapping trait for Moonbeam Accounts. /// This is basically identical to Frontier's own IdentityAddressMapping, but it works for any type /// that is Into like AccountId20 for example. pub struct IntoAddressMapping; diff --git a/runtime/moonbase/src/governance/origins.rs b/runtime/moonbase/src/governance/origins.rs index 94aca8c27c..6901814aa9 100644 --- a/runtime/moonbase/src/governance/origins.rs +++ b/runtime/moonbase/src/governance/origins.rs @@ -11,8 +11,6 @@ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. -#![cfg_attr(not(feature = "std"), no_std)] - //! Custom origins for governance interventions. pub use custom_origins::*; diff --git a/runtime/moonbase/src/lib.rs b/runtime/moonbase/src/lib.rs index b94a7be284..b8e3e86714 100644 --- a/runtime/moonbase/src/lib.rs +++ b/runtime/moonbase/src/lib.rs @@ -49,7 +49,7 @@ use frame_support::{ OnUnbalanced, }, weights::{ - constants::{RocksDbWeight, WEIGHT_REF_TIME_PER_SECOND}, + constants::{RocksDbWeight, WEIGHT_REF_TIME_PER_MILLIS}, ConstantMultiplier, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial, }, @@ -143,9 +143,11 @@ pub mod currency { } /// Maximum weight per block -pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_ref_time(WEIGHT_REF_TIME_PER_SECOND) - .saturating_div(2) - .set_proof_size(cumulus_primitives_core::relay_chain::v2::MAX_POV_SIZE as u64); +pub const WEIGHT_MILLISECS_PER_BLOCK: u64 = 500; +pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts( + WEIGHT_MILLISECS_PER_BLOCK * WEIGHT_REF_TIME_PER_MILLIS, + cumulus_primitives_core::relay_chain::v2::MAX_POV_SIZE as u64, +); pub const MILLISECS_PER_BLOCK: u64 = 12000; pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber); @@ -197,18 +199,17 @@ pub fn native_version() -> NativeVersion { const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); pub const NORMAL_WEIGHT: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_mul(3).saturating_div(4); -// Here we assume Ethereum's base fee of 21000 gas and convert to weight, but we -// subtract roughly the cost of a balance transfer from it (about 1/3 the cost) -// and some cost to account for per-byte-fee. -// TODO: we should use benchmarking's overhead feature to measure this -pub const EXTRINSIC_BASE_WEIGHT: Weight = Weight::from_ref_time(10000 * WEIGHT_PER_GAS); pub struct RuntimeBlockWeights; impl Get for RuntimeBlockWeights { fn get() -> frame_system::limits::BlockWeights { frame_system::limits::BlockWeights::builder() .for_class(DispatchClass::Normal, |weights| { - weights.base_extrinsic = EXTRINSIC_BASE_WEIGHT; + // Here we assume Ethereum's base fee of 21000 gas and convert to weight, but we + // subtract roughly the cost of a balance transfer from it (about 1/3 the cost) + // and some cost to account for per-byte-fee. + // TODO: we should use benchmarking's overhead feature to measure this + weights.base_extrinsic = WeightPerGas::get() * 10000; weights.max_total = NORMAL_WEIGHT.into(); }) .for_class(DispatchClass::Operational, |weights| { @@ -370,19 +371,10 @@ impl pallet_ethereum_chain_id::Config for Runtime {} impl pallet_randomness_collective_flip::Config for Runtime {} -/// Current approximation of the gas/s consumption considering -/// EVM execution over compiled WASM (on 4.4Ghz CPU). -/// Given the 500ms Weight, from which 75% only are used for transactions, -/// the total EVM execution gas limit is: GAS_PER_SECOND * 0.500 * 0.75 ~= 15_000_000. -pub const GAS_PER_SECOND: u64 = 40_000_000; - -/// Approximate ratio of the amount of Weight per Gas. -/// u64 works for approximations because Weight is a very small unit compared to gas. -pub const WEIGHT_PER_GAS: u64 = WEIGHT_REF_TIME_PER_SECOND / GAS_PER_SECOND; +pub const BLOCK_GAS_LIMIT: u64 = 15_000_000; parameter_types! { - pub BlockGasLimit: U256 - = U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS); + pub BlockGasLimit: U256 = U256::from(BLOCK_GAS_LIMIT); /// The portion of the `NORMAL_DISPATCH_RATIO` that we adjust the fees with. Blocks filled less /// than this will decrease the weight and more will increase. pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25); @@ -398,7 +390,11 @@ parameter_types! { /// as a safety net. pub MaximumMultiplier: Multiplier = Multiplier::from(100_000u128); pub PrecompilesValue: MoonbasePrecompiles = MoonbasePrecompiles::<_>::new(); - pub WeightPerGas: Weight = Weight::from_ref_time(WEIGHT_PER_GAS); + pub WeightPerGas: Weight = Weight::from_ref_time( + moonbeam_runtime_common::weight_per_gas( + BLOCK_GAS_LIMIT, NORMAL_DISPATCH_RATIO, WEIGHT_MILLISECS_PER_BLOCK + ) + ); } pub struct TransactionPaymentAsGasPrice; @@ -417,8 +413,9 @@ impl FeeCalculator for TransactionPaymentAsGasPrice { // This can lead to min_gas_price being same across blocks even if the multiplier changes. // There's still some precision loss when the final `gas_price` (used_gas * min_gas_price) // is computed in frontier, but that's currently unavoidable. - let min_gas_price = TransactionPayment::next_fee_multiplier() - .saturating_mul_int(currency::WEIGHT_FEE.saturating_mul(WEIGHT_PER_GAS as u128)); + let min_gas_price = TransactionPayment::next_fee_multiplier().saturating_mul_int( + currency::WEIGHT_FEE.saturating_mul(WeightPerGas::get().ref_time() as u128), + ); ( min_gas_price.into(), ::DbWeight::get().reads(1), diff --git a/runtime/moonriver/src/governance/origins.rs b/runtime/moonriver/src/governance/origins.rs index 6f131bbd30..b68ba56f2a 100644 --- a/runtime/moonriver/src/governance/origins.rs +++ b/runtime/moonriver/src/governance/origins.rs @@ -12,7 +12,6 @@ // GNU General Public License for more details. //! Custom origins for governance interventions. -#![cfg_attr(not(feature = "std"), no_std)] pub use custom_origins::*; From 8b1ce84ee0745332e9681a2be3f86a6629a1e316 Mon Sep 17 00:00:00 2001 From: koushiro Date: Tue, 14 Feb 2023 11:15:58 +0800 Subject: [PATCH 2/6] moonriver: improve weight per gas calculation --- runtime/moonriver/src/lib.rs | 39 ++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/runtime/moonriver/src/lib.rs b/runtime/moonriver/src/lib.rs index 05b6c47d94..3039f7ba01 100644 --- a/runtime/moonriver/src/lib.rs +++ b/runtime/moonriver/src/lib.rs @@ -48,7 +48,7 @@ use frame_support::{ OffchainWorker, OnFinalize, OnIdle, OnInitialize, OnRuntimeUpgrade, OnUnbalanced, }, weights::{ - constants::{RocksDbWeight, WEIGHT_REF_TIME_PER_SECOND}, + constants::{RocksDbWeight, WEIGHT_REF_TIME_PER_MILLIS}, ConstantMultiplier, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial, }, @@ -138,9 +138,12 @@ pub mod currency { } /// Maximum weight per block -pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_ref_time(WEIGHT_REF_TIME_PER_SECOND) - .saturating_div(2) - .set_proof_size(cumulus_primitives_core::relay_chain::v2::MAX_POV_SIZE as u64); +/// Maximum weight per block +pub const WEIGHT_MILLISECS_PER_BLOCK: u64 = 500; +pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts( + WEIGHT_MILLISECS_PER_BLOCK * WEIGHT_REF_TIME_PER_MILLIS, + cumulus_primitives_core::relay_chain::v2::MAX_POV_SIZE as u64, +); pub const MILLISECS_PER_BLOCK: u64 = 12000; pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber); @@ -192,18 +195,17 @@ pub fn native_version() -> NativeVersion { const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); const NORMAL_WEIGHT: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_mul(3).saturating_div(4); -// Here we assume Ethereum's base fee of 21000 gas and convert to weight, but we -// subtract roughly the cost of a balance transfer from it (about 1/3 the cost) -// and some cost to account for per-byte-fee. -// TODO: we should use benchmarking's overhead feature to measure this -pub const EXTRINSIC_BASE_WEIGHT: Weight = Weight::from_ref_time(10000 * WEIGHT_PER_GAS); pub struct RuntimeBlockWeights; impl Get for RuntimeBlockWeights { fn get() -> frame_system::limits::BlockWeights { frame_system::limits::BlockWeights::builder() .for_class(DispatchClass::Normal, |weights| { - weights.base_extrinsic = EXTRINSIC_BASE_WEIGHT; + // Here we assume Ethereum's base fee of 21000 gas and convert to weight, but we + // subtract roughly the cost of a balance transfer from it (about 1/3 the cost) + // and some cost to account for per-byte-fee. + // TODO: we should use benchmarking's overhead feature to measure this + weights.base_extrinsic = WeightPerGas::get() * 10000; weights.max_total = NORMAL_WEIGHT.into(); }) .for_class(DispatchClass::Operational, |weights| { @@ -356,19 +358,10 @@ impl pallet_ethereum_chain_id::Config for Runtime {} impl pallet_randomness_collective_flip::Config for Runtime {} -/// Current approximation of the gas/s consumption considering -/// EVM execution over compiled WASM (on 4.4Ghz CPU). -/// Given the 500ms Weight, from which 75% only are used for transactions, -/// the total EVM execution gas limit is: GAS_PER_SECOND * 0.500 * 0.75 ~= 15_000_000. -pub const GAS_PER_SECOND: u64 = 40_000_000; - -/// Approximate ratio of the amount of Weight per Gas. -/// u64 works for approximations because Weight is a very small unit compared to gas. -pub const WEIGHT_PER_GAS: u64 = WEIGHT_REF_TIME_PER_SECOND / GAS_PER_SECOND; +pub const BLOCK_GAS_LIMIT: u64 = 15_000_000; parameter_types! { - pub BlockGasLimit: U256 - = U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS); + pub BlockGasLimit: U256 = U256::from(BLOCK_GAS_LIMIT); /// The portion of the `NORMAL_DISPATCH_RATIO` that we adjust the fees with. Blocks filled less /// than this will decrease the weight and more will increase. pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25); @@ -385,7 +378,9 @@ parameter_types! { /// as a safety net. pub MaximumMultiplier: Multiplier = Multiplier::from(100_000u128); pub PrecompilesValue: MoonriverPrecompiles = MoonriverPrecompiles::<_>::new(); - pub WeightPerGas: Weight = Weight::from_ref_time(WEIGHT_PER_GAS); + pub WeightPerGas: Weight = Weight::from_ref_time( + moonbeam_runtime_common::weight_per_gas(BLOCK_GAS_LIMIT, NORMAL_DISPATCH_RATIO, WEIGHT_MILLISECS_PER_BLOCK) + ); } pub struct FixedGasPrice; From cfe0e98740b595a7f8885d89e5492ae228d6abf6 Mon Sep 17 00:00:00 2001 From: koushiro Date: Tue, 14 Feb 2023 11:22:53 +0800 Subject: [PATCH 3/6] moonbeam: improve weight per gas calculation --- runtime/moonbeam/src/lib.rs | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/runtime/moonbeam/src/lib.rs b/runtime/moonbeam/src/lib.rs index 31353125c7..f537667c57 100644 --- a/runtime/moonbeam/src/lib.rs +++ b/runtime/moonbeam/src/lib.rs @@ -49,7 +49,7 @@ use frame_support::{ OffchainWorker, OnFinalize, OnIdle, OnInitialize, OnRuntimeUpgrade, OnUnbalanced, }, weights::{ - constants::{RocksDbWeight, WEIGHT_REF_TIME_PER_SECOND}, + constants::{RocksDbWeight, WEIGHT_REF_TIME_PER_MILLIS}, ConstantMultiplier, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial, }, @@ -134,9 +134,11 @@ pub mod currency { } /// Maximum weight per block -pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_ref_time(WEIGHT_REF_TIME_PER_SECOND) - .saturating_div(2) - .set_proof_size(cumulus_primitives_core::relay_chain::v2::MAX_POV_SIZE as u64); +pub const WEIGHT_MILLISECS_PER_BLOCK: u64 = 500; +pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts( + WEIGHT_MILLISECS_PER_BLOCK * WEIGHT_REF_TIME_PER_MILLIS, + cumulus_primitives_core::relay_chain::v2::MAX_POV_SIZE as u64, +); pub const MILLISECS_PER_BLOCK: u64 = 12000; pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber); @@ -189,18 +191,17 @@ pub fn native_version() -> NativeVersion { const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); const NORMAL_WEIGHT: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_mul(3).saturating_div(4); -// Here we assume Ethereum's base fee of 21000 gas and convert to weight, but we -// subtract roughly the cost of a balance transfer from it (about 1/3 the cost) -// and some cost to account for per-byte-fee. -// TODO: we should use benchmarking's overhead feature to measure this -pub const EXTRINSIC_BASE_WEIGHT: Weight = Weight::from_ref_time(10000 * WEIGHT_PER_GAS); pub struct RuntimeBlockWeights; impl Get for RuntimeBlockWeights { fn get() -> frame_system::limits::BlockWeights { frame_system::limits::BlockWeights::builder() .for_class(DispatchClass::Normal, |weights| { - weights.base_extrinsic = EXTRINSIC_BASE_WEIGHT; + // Here we assume Ethereum's base fee of 21000 gas and convert to weight, but we + // subtract roughly the cost of a balance transfer from it (about 1/3 the cost) + // and some cost to account for per-byte-fee. + // TODO: we should use benchmarking's overhead feature to measure this + weights.base_extrinsic = WeightPerGas::get() * 10000; weights.max_total = NORMAL_WEIGHT.into(); }) .for_class(DispatchClass::Operational, |weights| { @@ -353,19 +354,11 @@ impl pallet_ethereum_chain_id::Config for Runtime {} impl pallet_randomness_collective_flip::Config for Runtime {} -/// Current approximation of the gas/s consumption considering -/// EVM execution over compiled WASM (on 4.4Ghz CPU). -/// Given the 500ms Weight, from which 75% only are used for transactions, -/// the total EVM execution gas limit is: GAS_PER_SECOND * 0.500 * 0.75 ~= 15_000_000. -pub const GAS_PER_SECOND: u64 = 40_000_000; - -/// Approximate ratio of the amount of Weight per Gas. -/// u64 works for approximations because Weight is a very small unit compared to gas. -pub const WEIGHT_PER_GAS: u64 = WEIGHT_REF_TIME_PER_SECOND / GAS_PER_SECOND; +pub const BLOCK_GAS_LIMIT: u64 = 15_000_000; parameter_types! { pub BlockGasLimit: U256 - = U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS); + = U256::from(BLOCK_GAS_LIMIT); /// The portion of the `NORMAL_DISPATCH_RATIO` that we adjust the fees with. Blocks filled less /// than this will decrease the weight and more will increase. pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25); @@ -382,7 +375,9 @@ parameter_types! { /// as a safety net. pub MaximumMultiplier: Multiplier = Multiplier::from(100_000u128); pub PrecompilesValue: MoonbeamPrecompiles = MoonbeamPrecompiles::<_>::new(); - pub WeightPerGas: Weight = Weight::from_ref_time(WEIGHT_PER_GAS); + pub WeightPerGas: Weight = Weight::from_ref_time( + moonbeam_runtime_common::weight_per_gas(BLOCK_GAS_LIMIT, NORMAL_DISPATCH_RATIO, WEIGHT_MILLISECS_PER_BLOCK) + ); } pub struct FixedGasPrice; From 89e2303bc43f5146d343be179b6021214da09f15 Mon Sep 17 00:00:00 2001 From: koushiro Date: Tue, 14 Feb 2023 16:40:38 +0800 Subject: [PATCH 4/6] fix moonbase integration test --- runtime/moonbase/tests/integration_test.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/runtime/moonbase/tests/integration_test.rs b/runtime/moonbase/tests/integration_test.rs index e191cb2c0b..a1c85d2601 100644 --- a/runtime/moonbase/tests/integration_test.rs +++ b/runtime/moonbase/tests/integration_test.rs @@ -3008,7 +3008,7 @@ fn precompile_existence() { #[test] fn substrate_based_fees_zero_txn_costs_only_base_extrinsic() { use frame_support::dispatch::{DispatchInfo, Pays}; - use moonbase_runtime::{currency, EXTRINSIC_BASE_WEIGHT}; + use moonbase_runtime::{currency, WeightPerGas}; ExtBuilder::default().build().execute_with(|| { let size_bytes = 0; @@ -3021,7 +3021,7 @@ fn substrate_based_fees_zero_txn_costs_only_base_extrinsic() { assert_eq!( TransactionPayment::compute_fee(size_bytes, &dispatch_info, tip), - EXTRINSIC_BASE_WEIGHT.ref_time() as u128 * currency::WEIGHT_FEE, + (WeightPerGas::get().ref_time() * 10000) as u128 * currency::WEIGHT_FEE, ); }); } @@ -3146,7 +3146,7 @@ mod fee_tests { }; use moonbase_runtime::{ currency, BlockWeights, FastAdjustingFeeUpdate, LengthToFee, MinimumMultiplier, - TargetBlockFullness, NORMAL_WEIGHT, WEIGHT_PER_GAS, + TargetBlockFullness, WeightPerGas, NORMAL_WEIGHT, }; use sp_runtime::{FixedPointNumber, Perbill}; @@ -3242,7 +3242,9 @@ mod fee_tests { pallet_transaction_payment::NextFeeMultiplier::::set(multiplier); let actual = TransactionPaymentAsGasPrice::min_gas_price().0; let expected: U256 = multiplier - .saturating_mul_int(currency::WEIGHT_FEE.saturating_mul(WEIGHT_PER_GAS as u128)) + .saturating_mul_int( + currency::WEIGHT_FEE.saturating_mul(WeightPerGas::get().ref_time() as u128), + ) .into(); assert_eq!(expected, actual); @@ -3279,7 +3281,8 @@ mod fee_tests { .unwrap() .into(); t.execute_with(|| { - let weight_fee_per_gas = currency::WEIGHT_FEE.saturating_mul(WEIGHT_PER_GAS as u128); + let weight_fee_per_gas = + currency::WEIGHT_FEE.saturating_mul(WeightPerGas::get().ref_time() as u128); let sim = |start_gas_price: u128, fullness: Perbill, num_blocks: u64| -> U256 { let start_multiplier = FixedU128::from_rational(start_gas_price, weight_fee_per_gas); From d5f8809b4d87ed7ce026ca6671138bb3dcd56b38 Mon Sep 17 00:00:00 2001 From: koushiro Date: Wed, 15 Feb 2023 10:19:26 +0800 Subject: [PATCH 5/6] fix editorconfig checking --- runtime/moonbeam/src/lib.rs | 4 +++- runtime/moonriver/src/lib.rs | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/runtime/moonbeam/src/lib.rs b/runtime/moonbeam/src/lib.rs index f537667c57..9d0db98c26 100644 --- a/runtime/moonbeam/src/lib.rs +++ b/runtime/moonbeam/src/lib.rs @@ -376,7 +376,9 @@ parameter_types! { pub MaximumMultiplier: Multiplier = Multiplier::from(100_000u128); pub PrecompilesValue: MoonbeamPrecompiles = MoonbeamPrecompiles::<_>::new(); pub WeightPerGas: Weight = Weight::from_ref_time( - moonbeam_runtime_common::weight_per_gas(BLOCK_GAS_LIMIT, NORMAL_DISPATCH_RATIO, WEIGHT_MILLISECS_PER_BLOCK) + moonbeam_runtime_common::weight_per_gas( + BLOCK_GAS_LIMIT, NORMAL_DISPATCH_RATIO, WEIGHT_MILLISECS_PER_BLOCK + ) ); } diff --git a/runtime/moonriver/src/lib.rs b/runtime/moonriver/src/lib.rs index 3039f7ba01..52fff7510d 100644 --- a/runtime/moonriver/src/lib.rs +++ b/runtime/moonriver/src/lib.rs @@ -379,7 +379,9 @@ parameter_types! { pub MaximumMultiplier: Multiplier = Multiplier::from(100_000u128); pub PrecompilesValue: MoonriverPrecompiles = MoonriverPrecompiles::<_>::new(); pub WeightPerGas: Weight = Weight::from_ref_time( - moonbeam_runtime_common::weight_per_gas(BLOCK_GAS_LIMIT, NORMAL_DISPATCH_RATIO, WEIGHT_MILLISECS_PER_BLOCK) + moonbeam_runtime_common::weight_per_gas( + BLOCK_GAS_LIMIT, NORMAL_DISPATCH_RATIO, WEIGHT_MILLISECS_PER_BLOCK + ) ); } From 4abb28591be359b22f8b6eaca40014a503377a21 Mon Sep 17 00:00:00 2001 From: koushiro Date: Fri, 17 Feb 2023 00:00:04 +0800 Subject: [PATCH 6/6] add extrinsic_base_weight function --- runtime/common/src/lib.rs | 10 +++++++++- runtime/moonbase/src/lib.rs | 7 ++----- runtime/moonbase/tests/integration_test.rs | 3 ++- runtime/moonbeam/src/lib.rs | 7 ++----- runtime/moonriver/src/lib.rs | 7 ++----- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 1566e1dc2a..2e05cb220b 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -16,7 +16,7 @@ #![cfg_attr(not(feature = "std"), no_std)] -use frame_support::weights::constants::WEIGHT_REF_TIME_PER_MILLIS; +use frame_support::weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, Weight}; use sp_core::H160; use sp_runtime::Perbill; @@ -27,6 +27,14 @@ mod impl_on_charge_evm_transaction; mod impl_self_contained_call; pub mod migrations; +// Here we assume Ethereum's base fee of 21000 gas and convert to weight, but we +// subtract roughly the cost of a balance transfer from it (about 1/3 the cost) +// and some cost to account for per-byte-fee. +// TODO: we should use benchmarking's overhead feature to measure this +pub fn extrinsic_base_weight(weight_per_gas: Weight) -> Weight { + weight_per_gas * 10000 +} + /// `WeightPerGas` is an approximate ratio of the amount of Weight per Gas. /// u64 works for approximations because Weight is a very small unit compared to gas. /// diff --git a/runtime/moonbase/src/lib.rs b/runtime/moonbase/src/lib.rs index b8e3e86714..ac84213a41 100644 --- a/runtime/moonbase/src/lib.rs +++ b/runtime/moonbase/src/lib.rs @@ -205,11 +205,8 @@ impl Get for RuntimeBlockWeights { fn get() -> frame_system::limits::BlockWeights { frame_system::limits::BlockWeights::builder() .for_class(DispatchClass::Normal, |weights| { - // Here we assume Ethereum's base fee of 21000 gas and convert to weight, but we - // subtract roughly the cost of a balance transfer from it (about 1/3 the cost) - // and some cost to account for per-byte-fee. - // TODO: we should use benchmarking's overhead feature to measure this - weights.base_extrinsic = WeightPerGas::get() * 10000; + weights.base_extrinsic = + moonbeam_runtime_common::extrinsic_base_weight(WeightPerGas::get()); weights.max_total = NORMAL_WEIGHT.into(); }) .for_class(DispatchClass::Operational, |weights| { diff --git a/runtime/moonbase/tests/integration_test.rs b/runtime/moonbase/tests/integration_test.rs index a1c85d2601..aa91957104 100644 --- a/runtime/moonbase/tests/integration_test.rs +++ b/runtime/moonbase/tests/integration_test.rs @@ -3021,7 +3021,8 @@ fn substrate_based_fees_zero_txn_costs_only_base_extrinsic() { assert_eq!( TransactionPayment::compute_fee(size_bytes, &dispatch_info, tip), - (WeightPerGas::get().ref_time() * 10000) as u128 * currency::WEIGHT_FEE, + moonbeam_runtime_common::extrinsic_base_weight(WeightPerGas::get()).ref_time() as u128 + * currency::WEIGHT_FEE, ); }); } diff --git a/runtime/moonbeam/src/lib.rs b/runtime/moonbeam/src/lib.rs index 9d0db98c26..03c2687335 100644 --- a/runtime/moonbeam/src/lib.rs +++ b/runtime/moonbeam/src/lib.rs @@ -197,11 +197,8 @@ impl Get for RuntimeBlockWeights { fn get() -> frame_system::limits::BlockWeights { frame_system::limits::BlockWeights::builder() .for_class(DispatchClass::Normal, |weights| { - // Here we assume Ethereum's base fee of 21000 gas and convert to weight, but we - // subtract roughly the cost of a balance transfer from it (about 1/3 the cost) - // and some cost to account for per-byte-fee. - // TODO: we should use benchmarking's overhead feature to measure this - weights.base_extrinsic = WeightPerGas::get() * 10000; + weights.base_extrinsic = + moonbeam_runtime_common::extrinsic_base_weight(WeightPerGas::get()); weights.max_total = NORMAL_WEIGHT.into(); }) .for_class(DispatchClass::Operational, |weights| { diff --git a/runtime/moonriver/src/lib.rs b/runtime/moonriver/src/lib.rs index 52fff7510d..81fd1629eb 100644 --- a/runtime/moonriver/src/lib.rs +++ b/runtime/moonriver/src/lib.rs @@ -201,11 +201,8 @@ impl Get for RuntimeBlockWeights { fn get() -> frame_system::limits::BlockWeights { frame_system::limits::BlockWeights::builder() .for_class(DispatchClass::Normal, |weights| { - // Here we assume Ethereum's base fee of 21000 gas and convert to weight, but we - // subtract roughly the cost of a balance transfer from it (about 1/3 the cost) - // and some cost to account for per-byte-fee. - // TODO: we should use benchmarking's overhead feature to measure this - weights.base_extrinsic = WeightPerGas::get() * 10000; + weights.base_extrinsic = + moonbeam_runtime_common::extrinsic_base_weight(WeightPerGas::get()); weights.max_total = NORMAL_WEIGHT.into(); }) .for_class(DispatchClass::Operational, |weights| {