Skip to content

Commit

Permalink
Add DefaultFeePerGas associated type to pallet-base-fee (parityte…
Browse files Browse the repository at this point in the history
  • Loading branch information
tgmichel authored Feb 2, 2022
1 parent e5b2fa8 commit 3652409
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
56 changes: 39 additions & 17 deletions frame/base-fee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub mod pallet {
/// Lower and upper bounds for increasing / decreasing `BaseFeePerGas`.
type Threshold: BaseFeeThreshold;
type IsActive: Get<bool>;
type DefaultBaseFeePerGas: Get<U256>;
}

#[pallet::pallet]
Expand Down Expand Up @@ -67,8 +68,7 @@ pub mod pallet {
impl<T: Config> Default for GenesisConfig<T> {
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,
Expand All @@ -85,13 +85,13 @@ pub mod pallet {
}

#[pallet::type_value]
pub fn DefaultBaseFeePerGas() -> U256 {
U256::from(1_000_000_000)
pub fn DefaultBaseFeePerGas<T: Config>() -> U256 {
T::DefaultBaseFeePerGas::get()
}

#[pallet::storage]
#[pallet::getter(fn base_fee_per_gas)]
pub type BaseFeePerGas<T> = StorageValue<_, U256, ValueQuery, DefaultBaseFeePerGas>;
pub type BaseFeePerGas<T> = StorageValue<_, U256, ValueQuery, DefaultBaseFeePerGas<T>>;

#[pallet::type_value]
pub fn DefaultIsActive<T: Config>() -> bool {
Expand Down Expand Up @@ -256,14 +256,24 @@ mod tests {
Permill,
};

pub fn new_test_ext(base_fee: U256) -> TestExternalities {
pub fn new_test_ext(base_fee: Option<U256>) -> TestExternalities {
let mut t = frame_system::GenesisConfig::default()
.build_storage::<Test>()
.unwrap();

pallet_base_fee::GenesisConfig::<Test>::new(base_fee, true, Permill::from_parts(125_000))
if let Some(base_fee) = base_fee {
pallet_base_fee::GenesisConfig::<Test>::new(
base_fee,
true,
Permill::from_parts(125_000),
)
.assimilate_storage(&mut t)
.unwrap();
} else {
pallet_base_fee::GenesisConfig::<Test>::default()
.assimilate_storage(&mut t)
.unwrap();
}
TestExternalities::new(t)
}

Expand Down Expand Up @@ -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;
Expand All @@ -322,6 +333,7 @@ mod tests {
type Event = Event;
type Threshold = BaseFeeThreshold;
type IsActive = IsActive;
type DefaultBaseFeePerGas = DefaultBaseFeePerGas;
}

frame_support::construct_runtime!(
Expand All @@ -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());
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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);
Expand All @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {}
Expand Down

0 comments on commit 3652409

Please sign in to comment.