From 365240941d596902d5696893e1122be47550b9ef Mon Sep 17 00:00:00 2001 From: tgmichel Date: Wed, 2 Feb 2022 15:12:07 +0100 Subject: [PATCH] Add `DefaultFeePerGas` associated type to `pallet-base-fee` (#562) --- frame/base-fee/src/lib.rs | 56 ++++++++++++++++++++++++++----------- template/runtime/src/lib.rs | 2 ++ 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/frame/base-fee/src/lib.rs b/frame/base-fee/src/lib.rs index 74416d2df5b73..58c438162f7f4 100644 --- a/frame/base-fee/src/lib.rs +++ b/frame/base-fee/src/lib.rs @@ -37,6 +37,7 @@ pub mod pallet { /// Lower and upper bounds for increasing / decreasing `BaseFeePerGas`. type Threshold: BaseFeeThreshold; type IsActive: Get; + type DefaultBaseFeePerGas: Get; } #[pallet::pallet] @@ -67,8 +68,7 @@ pub mod pallet { impl Default for GenesisConfig { fn default() -> Self { Self { - // 1 GWEI - base_fee_per_gas: U256::from(1_000_000_000), + base_fee_per_gas: T::DefaultBaseFeePerGas::get(), is_active: true, elasticity: Permill::from_parts(125_000), _marker: PhantomData, @@ -85,13 +85,13 @@ pub mod pallet { } #[pallet::type_value] - pub fn DefaultBaseFeePerGas() -> U256 { - U256::from(1_000_000_000) + pub fn DefaultBaseFeePerGas() -> U256 { + T::DefaultBaseFeePerGas::get() } #[pallet::storage] #[pallet::getter(fn base_fee_per_gas)] - pub type BaseFeePerGas = StorageValue<_, U256, ValueQuery, DefaultBaseFeePerGas>; + pub type BaseFeePerGas = StorageValue<_, U256, ValueQuery, DefaultBaseFeePerGas>; #[pallet::type_value] pub fn DefaultIsActive() -> bool { @@ -256,14 +256,24 @@ mod tests { Permill, }; - pub fn new_test_ext(base_fee: U256) -> TestExternalities { + pub fn new_test_ext(base_fee: Option) -> TestExternalities { let mut t = frame_system::GenesisConfig::default() .build_storage::() .unwrap(); - pallet_base_fee::GenesisConfig::::new(base_fee, true, Permill::from_parts(125_000)) + if let Some(base_fee) = base_fee { + pallet_base_fee::GenesisConfig::::new( + base_fee, + true, + Permill::from_parts(125_000), + ) .assimilate_storage(&mut t) .unwrap(); + } else { + pallet_base_fee::GenesisConfig::::default() + .assimilate_storage(&mut t) + .unwrap(); + } TestExternalities::new(t) } @@ -303,6 +313,7 @@ mod tests { frame_support::parameter_types! { pub IsActive: bool = true; + pub DefaultBaseFeePerGas: U256 = U256::from(100_000_000_000 as u128); } pub struct BaseFeeThreshold; @@ -322,6 +333,7 @@ mod tests { type Event = Event; type Threshold = BaseFeeThreshold; type IsActive = IsActive; + type DefaultBaseFeePerGas = DefaultBaseFeePerGas; } frame_support::construct_runtime!( @@ -335,10 +347,20 @@ mod tests { } ); + #[test] + fn should_default() { + new_test_ext(None).execute_with(|| { + assert_eq!( + BaseFee::base_fee_per_gas(), + U256::from(100_000_000_000 as u128) + ); + }); + } + #[test] fn should_not_overflow_u256() { let base_fee = U256::max_value(); - new_test_ext(base_fee).execute_with(|| { + new_test_ext(Some(base_fee)).execute_with(|| { let init = BaseFee::base_fee_per_gas(); System::register_extra_weight_unchecked(1000000000000, DispatchClass::Normal); BaseFee::on_finalize(System::block_number()); @@ -349,7 +371,7 @@ mod tests { #[test] fn should_handle_zero() { let base_fee = U256::zero(); - new_test_ext(base_fee).execute_with(|| { + new_test_ext(Some(base_fee)).execute_with(|| { let init = BaseFee::base_fee_per_gas(); BaseFee::on_finalize(System::block_number()); assert_eq!(BaseFee::base_fee_per_gas(), init); @@ -359,7 +381,7 @@ mod tests { #[test] fn should_handle_consecutive_empty_blocks() { let base_fee = U256::from(1_000_000_000); - new_test_ext(base_fee).execute_with(|| { + new_test_ext(Some(base_fee)).execute_with(|| { for _ in 0..10000 { BaseFee::on_finalize(System::block_number()); System::set_block_number(System::block_number() + 1); @@ -375,7 +397,7 @@ mod tests { #[test] fn should_handle_consecutive_full_blocks() { let base_fee = U256::from(1_000_000_000); - new_test_ext(base_fee).execute_with(|| { + new_test_ext(Some(base_fee)).execute_with(|| { for _ in 0..10000 { // Register max weight in block. System::register_extra_weight_unchecked(1000000000000, DispatchClass::Normal); @@ -396,7 +418,7 @@ mod tests { #[test] fn should_increase_total_base_fee() { let base_fee = U256::from(1_000_000_000); - new_test_ext(base_fee).execute_with(|| { + new_test_ext(Some(base_fee)).execute_with(|| { assert_eq!(BaseFee::base_fee_per_gas(), U256::from(1000000000)); // Register max weight in block. System::register_extra_weight_unchecked(1000000000000, DispatchClass::Normal); @@ -409,7 +431,7 @@ mod tests { #[test] fn should_increase_delta_of_base_fee() { let base_fee = U256::from(1_000_000_000); - new_test_ext(base_fee).execute_with(|| { + new_test_ext(Some(base_fee)).execute_with(|| { assert_eq!(BaseFee::base_fee_per_gas(), U256::from(1000000000)); // Register 75% capacity in block weight. System::register_extra_weight_unchecked(750000000000, DispatchClass::Normal); @@ -422,7 +444,7 @@ mod tests { #[test] fn should_idle_base_fee() { let base_fee = U256::from(1_000_000_000); - new_test_ext(base_fee).execute_with(|| { + new_test_ext(Some(base_fee)).execute_with(|| { assert_eq!(BaseFee::base_fee_per_gas(), U256::from(1000000000)); // Register half capacity in block weight. System::register_extra_weight_unchecked(500000000000, DispatchClass::Normal); @@ -435,7 +457,7 @@ mod tests { #[test] fn set_base_fee_per_gas_dispatchable() { let base_fee = U256::from(1_000_000_000); - new_test_ext(base_fee).execute_with(|| { + new_test_ext(Some(base_fee)).execute_with(|| { assert_eq!(BaseFee::base_fee_per_gas(), U256::from(1000000000)); assert_ok!(BaseFee::set_base_fee_per_gas(Origin::root(), U256::from(1))); assert_eq!(BaseFee::base_fee_per_gas(), U256::from(1)); @@ -445,7 +467,7 @@ mod tests { #[test] fn set_is_active_dispatchable() { let base_fee = U256::from(1_000_000_000); - new_test_ext(base_fee).execute_with(|| { + new_test_ext(Some(base_fee)).execute_with(|| { assert_eq!(BaseFee::is_active(), true); assert_ok!(BaseFee::set_is_active(Origin::root(), false)); assert_eq!(BaseFee::is_active(), false); @@ -455,7 +477,7 @@ mod tests { #[test] fn set_elasticity_dispatchable() { let base_fee = U256::from(1_000_000_000); - new_test_ext(base_fee).execute_with(|| { + new_test_ext(Some(base_fee)).execute_with(|| { assert_eq!(BaseFee::elasticity(), Permill::from_parts(125_000)); assert_ok!(BaseFee::set_elasticity( Origin::root(), diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index 96eb8af0708a7..d8929e2087c83 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -334,6 +334,7 @@ impl pallet_dynamic_fee::Config for Runtime { frame_support::parameter_types! { pub IsActive: bool = true; + pub DefaultBaseFeePerGas: U256 = U256::from(1_000_000_000); } pub struct BaseFeeThreshold; @@ -353,6 +354,7 @@ impl pallet_base_fee::Config for Runtime { type Event = Event; type Threshold = BaseFeeThreshold; type IsActive = IsActive; + type DefaultBaseFeePerGas = DefaultBaseFeePerGas; } impl pallet_randomness_collective_flip::Config for Runtime {}