Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ impl Default for Releases {
}
}

/// Default value for NextFeeMultiplier. This is used in genesis and is also used in
/// NextFeeMultiplierOnEmpty() to provide a value when none exists in storage.
const MULTIPLIER_DEFAULT_VALUE: Multiplier = Multiplier::from_u32(1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review, esp. that from_u32(1) is equivalent to saturating_from_integer(1). The latter wasn't const and couldn't be used here.

My tests should verify this, but I'd appreciate another look at it.


#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down Expand Up @@ -300,7 +304,7 @@ pub mod pallet {

#[pallet::type_value]
pub fn NextFeeMultiplierOnEmpty() -> Multiplier {
Multiplier::saturating_from_integer(1)
MULTIPLIER_DEFAULT_VALUE
}

#[pallet::storage]
Expand All @@ -312,19 +316,22 @@ pub mod pallet {
pub(super) type StorageVersion<T: Config> = StorageValue<_, Releases, ValueQuery>;

#[pallet::genesis_config]
pub struct GenesisConfig;
pub struct GenesisConfig {
pub multiplier: Multiplier,
}

#[cfg(feature = "std")]
impl Default for GenesisConfig {
fn default() -> Self {
Self
Self { multiplier: MULTIPLIER_DEFAULT_VALUE }
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
StorageVersion::<T>::put(Releases::V2);
NextFeeMultiplier::<T>::put(self.multiplier);
}
}

Expand Down Expand Up @@ -816,7 +823,7 @@ mod tests {

use frame_support::{
assert_noop, assert_ok, parameter_types,
traits::{ConstU32, ConstU64, Currency, Imbalance, OnUnbalanced},
traits::{ConstU32, ConstU64, Currency, GenesisBuild, Imbalance, OnUnbalanced},
weights::{
DispatchClass, DispatchInfo, GetDispatchInfo, PostDispatchInfo, Weight,
WeightToFee as WeightToFeeT,
Expand Down Expand Up @@ -958,11 +965,18 @@ mod tests {
base_weight: Weight,
byte_fee: u64,
weight_to_fee: u64,
initial_multiplier: Option<Multiplier>,
}

impl Default for ExtBuilder {
fn default() -> Self {
Self { balance_factor: 1, base_weight: Weight::zero(), byte_fee: 1, weight_to_fee: 1 }
Self {
balance_factor: 1,
base_weight: Weight::zero(),
byte_fee: 1,
weight_to_fee: 1,
initial_multiplier: None,
}
}
}

Expand All @@ -983,6 +997,10 @@ mod tests {
self.balance_factor = factor;
self
}
pub fn with_initial_multiplier(mut self, multiplier: Multiplier) -> Self {
self.initial_multiplier = Some(multiplier);
self
}
fn set_constants(&self) {
EXTRINSIC_BASE_WEIGHT.with(|v| *v.borrow_mut() = self.base_weight);
TRANSACTION_BYTE_FEE.with(|v| *v.borrow_mut() = self.byte_fee);
Expand All @@ -1007,6 +1025,12 @@ mod tests {
}
.assimilate_storage(&mut t)
.unwrap();

if let Some(multiplier) = self.initial_multiplier {
let genesis = pallet::GenesisConfig { multiplier };
GenesisBuild::<Runtime>::assimilate_storage(&genesis, &mut t).unwrap();
}

t.into()
}
}
Expand Down Expand Up @@ -1720,4 +1744,24 @@ mod tests {
assert_eq!(refund_based_fee, actual_fee);
});
}

#[test]
fn genesis_config_works() {
ExtBuilder::default()
.with_initial_multiplier(Multiplier::from_u32(100))
.build()
.execute_with(|| {
assert_eq!(
<NextFeeMultiplier<Runtime>>::get(),
Multiplier::saturating_from_integer(100)
);
});
}

#[test]
fn genesis_default_works() {
ExtBuilder::default().build().execute_with(|| {
assert_eq!(<NextFeeMultiplier<Runtime>>::get(), Multiplier::saturating_from_integer(1));
});
}
}