-
Notifications
You must be signed in to change notification settings - Fork 382
Improve readability in weight per gas calculation #2075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
1a70cf3
8b1ce84
cfe0e98
89e2303
d5f8809
4abb285
0070060
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing this constant means we have to use the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added the |
||
|
|
||
| pub struct RuntimeBlockWeights; | ||
| impl Get<frame_system::limits::BlockWeights> 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; | ||
|
Comment on lines
-373
to
-381
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, I think these constants are valuable for readability.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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<Runtime> = 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(), | ||
| <Runtime as frame_system::Config>::DbWeight::get().reads(1), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.