Skip to content

Commit

Permalink
set the target block fullness to 35% (#2816)
Browse files Browse the repository at this point in the history
* set the target block fullness to 35%
* update tests constants for target block fullness
* add fee integration tests for moonbeam and moonriver runtimes

---------

Co-authored-by: Rodrigo Quelhas <[email protected]>
  • Loading branch information
noandrea and RomarQ authored May 26, 2024
1 parent f63af52 commit cada1a7
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 37 deletions.
2 changes: 1 addition & 1 deletion runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ parameter_types! {
= U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS);
/// 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(50);
pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(35);
/// The adjustment variable of the runtime. Higher values will cause `TargetBlockFullness` to
/// change the fees more rapidly. This fast multiplier responds by doubling/halving in
/// approximately one hour at extreme block congestion levels.
Expand Down
38 changes: 19 additions & 19 deletions runtime/moonbase/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2962,54 +2962,54 @@ mod fee_tests {

assert_eq!(
sim(1_000_000_000, Perbill::from_percent(0), 1),
U256::from(998_002_000),
U256::from(998_600_980),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(25), 1),
U256::from(999_000_500),
U256::from(999_600_080),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(50), 1),
U256::from(1_000_000_000),
U256::from(1_000_600_180),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(100), 1),
U256::from(1_002_002_000),
U256::from(1_002_603_380),
);

// 1 "real" hour (at 12-second blocks)
// 1 "real" hour (at 6-second blocks)
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(0), 300),
U256::from(548_811_855),
sim(1_000_000_000, Perbill::from_percent(0), 600),
U256::from(431_710_642),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(25), 300),
U256::from(740_818_257),
sim(1_000_000_000, Perbill::from_percent(25), 600),
U256::from(786_627_866),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(50), 300),
U256::from(1_000_000_000),
sim(1_000_000_000, Perbill::from_percent(50), 600),
U256::from(1_433_329_383u128),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(100), 300),
U256::from(1_822_118_072u128),
sim(1_000_000_000, Perbill::from_percent(100), 600),
U256::from(4_758_812_897u128),
);

// 1 "real" day (at 12-second blocks)
// 1 "real" day (at 6-second blocks)
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(0), 7200),
sim(1_000_000_000, Perbill::from_percent(0), 14400),
U256::from(125_000_000), // lower bound enforced
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(25), 7200),
sim(1_000_000_000, Perbill::from_percent(25), 14400),
U256::from(125_000_000),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(50), 7200),
U256::from(1_000_000_000u128),
sim(1_000_000_000, Perbill::from_percent(50), 14400),
U256::from(5_653_326_895_069u128),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(100), 7200),
sim(1_000_000_000, Perbill::from_percent(100), 14400),
U256::from(125_000_000_000_000u128), // upper bound enforced
);
});
Expand Down
4 changes: 2 additions & 2 deletions runtime/moonbeam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ 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);
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.
Expand Down Expand Up @@ -394,7 +394,7 @@ parameter_types! {
= U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS);
/// 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(50);
pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(35);
/// The adjustment variable of the runtime. Higher values will cause `TargetBlockFullness` to
/// change the fees more rapidly. This low value causes changes to occur slowly over time.
pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(4, 1_000);
Expand Down
133 changes: 130 additions & 3 deletions runtime/moonbeam/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2667,16 +2667,17 @@ fn evm_success_keeps_substrate_events() {
#[cfg(test)]
mod fee_tests {
use super::*;
use fp_evm::FeeCalculator;
use frame_support::{
traits::ConstU128,
traits::{ConstU128, OnFinalize},
weights::{ConstantMultiplier, WeightToFee},
};
use moonbeam_runtime::{
currency, LengthToFee, MinimumMultiplier, RuntimeBlockWeights, SlowAdjustingFeeUpdate,
TargetBlockFullness, TransactionPayment,
TargetBlockFullness, NORMAL_WEIGHT, WEIGHT_PER_GAS,
};
use sp_core::Get;
use sp_runtime::FixedPointNumber;
use sp_runtime::{BuildStorage, FixedPointNumber, Perbill};

fn run_with_system_weight<F>(w: Weight, mut assertions: F)
where
Expand Down Expand Up @@ -2757,4 +2758,130 @@ mod fee_tests {
);
});
}

#[test]
fn test_min_gas_price_is_deterministic() {
let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap()
.into();
t.execute_with(|| {
let multiplier = sp_runtime::FixedU128::from_u32(1);
pallet_transaction_payment::NextFeeMultiplier::<Runtime>::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))
.into();

assert_eq!(expected, actual);
});
}

#[test]
fn test_min_gas_price_has_no_precision_loss_from_saturating_mul_int() {
let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap()
.into();
t.execute_with(|| {
let multiplier_1 = sp_runtime::FixedU128::from_float(0.999593900000000000);
let multiplier_2 = sp_runtime::FixedU128::from_float(0.999593200000000000);

pallet_transaction_payment::NextFeeMultiplier::<Runtime>::set(multiplier_1);
let a = TransactionPaymentAsGasPrice::min_gas_price();
pallet_transaction_payment::NextFeeMultiplier::<Runtime>::set(multiplier_2);
let b = TransactionPaymentAsGasPrice::min_gas_price();

assert_ne!(
a, b,
"both gas prices were equal, unexpected precision loss incurred"
);
});
}

#[test]
fn test_fee_scenarios() {
use sp_runtime::FixedU128;
let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap()
.into();
t.execute_with(|| {
let weight_fee_per_gas = currency::WEIGHT_FEE.saturating_mul(WEIGHT_PER_GAS 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);
pallet_transaction_payment::NextFeeMultiplier::<Runtime>::set(start_multiplier);

let block_weight = NORMAL_WEIGHT * fullness;

for i in 0..num_blocks {
System::set_block_number(i as u32);
System::set_block_consumed_resources(block_weight, 0);
TransactionPayment::on_finalize(i as u32);
}

TransactionPaymentAsGasPrice::min_gas_price().0
};

// The expected values are the ones observed during test execution,
// they are expected to change when parameters that influence
// the fee calculation are changed, and should be updated accordingly.
// If a test fails when nothing specific to fees has changed,
// it may indicate an unexpected collateral effect and should be investigated

assert_eq!(
sim(1_000_000_000, Perbill::from_percent(0), 1),
U256::from(125_000_000_000u128),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(25), 1),
U256::from(125_000_000_000u128),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(50), 1),
U256::from(125_075_022_500u128),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(100), 1),
U256::from(125_325_422_500u128),
);

// 1 "real" hour (at 12-second blocks)
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(0), 600),
U256::from(125_000_000_000u128),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(25), 600),
U256::from(125_000_000_000u128),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(50), 600),
U256::from(179_166_172_951u128),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(100), 600),
U256::from(594_851_612_166u128),
);

// 1 "real" day (at 12-second blocks)
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(0), 14400),
U256::from(125_000_000_000u128), // lower bound enforced
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(25), 14400),
U256::from(125_000_000_000u128),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(50), 14400),
U256::from(706_665_861_883_635u128),
);
assert_eq!(
sim(1_000_000_000, Perbill::from_percent(100), 14400),
U256::from(12_500_000_000_000_000u128), // upper bound enforced
);
});
}
}
2 changes: 1 addition & 1 deletion runtime/moonriver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ parameter_types! {
= U256::from(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS);
/// 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(50);
pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(35);
/// The adjustment variable of the runtime. Higher values will cause `TargetBlockFullness` to
/// change the fees more rapidly. This low value causes changes to occur slowly over time.
pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(4, 1_000);
Expand Down
Loading

0 comments on commit cada1a7

Please sign in to comment.