diff --git a/runtime/common/src/fees.rs b/runtime/common/src/fees.rs index 1bea9d5759d1a77d1c1aabf231c082a4bb72922d..326c3b4fdef4488c03c85cb6215a9399a1c8f057 100644 --- a/runtime/common/src/fees.rs +++ b/runtime/common/src/fees.rs @@ -22,10 +22,11 @@ pub use frame_support::weights::{Weight, WeightToFee}; use sp_arithmetic::traits::{BaseArithmetic, Unsigned}; - +use sp_core::Get; #[cfg(not(feature = "constant-fees"))] use { crate::weights::extrinsic_weights::ExtrinsicBaseWeight, + frame_support::pallet_prelude::DispatchClass, frame_support::weights::{ WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial, }, @@ -35,17 +36,31 @@ use { sp_runtime::SaturatedConversion, }; -pub struct LengthToFeeImpl<T>(sp_std::marker::PhantomData<T>); +pub struct LengthToFeeImpl<T, U, X>( + sp_std::marker::PhantomData<T>, + sp_std::marker::PhantomData<U>, + sp_std::marker::PhantomData<X>, +); -impl<T> WeightToFee for LengthToFeeImpl<T> +impl<T, U, X> WeightToFee for LengthToFeeImpl<T, U, X> where T: BaseArithmetic + From<u32> + Copy + Unsigned, + U: frame_system::Config, + X: Get<Weight>, { type Balance = T; #[cfg(not(feature = "constant-fees"))] fn weight_to_fee(length_in_bytes: &Weight) -> Self::Balance { - Self::Balance::saturated_from(length_in_bytes.ref_time() / 100u64) + let current_block_weight = <frame_system::Pallet<U>>::block_weight(); + if current_block_weight + .get(DispatchClass::Normal) + .any_lt(X::get()) + { + 0u32.into() + } else { + Self::Balance::saturated_from(length_in_bytes.ref_time() / 100u64) + } } #[cfg(feature = "constant-fees")] @@ -54,30 +69,49 @@ where } } -pub struct WeightToFeeImpl<T>(sp_std::marker::PhantomData<T>); +pub struct WeightToFeeImpl<T, U, X>( + sp_std::marker::PhantomData<T>, + sp_std::marker::PhantomData<U>, + sp_std::marker::PhantomData<X>, +); #[cfg(not(feature = "constant-fees"))] -impl<T> WeightToFeePolynomial for WeightToFeeImpl<T> +impl<T, U, X> WeightToFeePolynomial for WeightToFeeImpl<T, U, X> where T: BaseArithmetic + From<u64> + Copy + Unsigned + From<u32> + MultiplyRational, + U: frame_system::Config, + X: Get<Weight>, { type Balance = T; fn polynomial() -> WeightToFeeCoefficients<Self::Balance> { - // The extrinsic base weight (smallest non-zero weight) is mapped to 5 cent - let p: Self::Balance = 5u64.into(); - let q: Self::Balance = Self::Balance::from(ExtrinsicBaseWeight::get().ref_time()); - smallvec![WeightToFeeCoefficient { - degree: 1, - negative: false, - coeff_frac: Perbill::from_rational(p % q, q), - coeff_integer: p / q, - }] + let current_block_weight = <frame_system::Pallet<U>>::block_weight(); + if current_block_weight + .get(DispatchClass::Normal) + .any_lt(X::get()) + { + smallvec![WeightToFeeCoefficient { + degree: 1, + negative: false, + coeff_frac: Perbill::zero(), + coeff_integer: Self::Balance::zero(), + }] + } else { + // The extrinsic base weight (smallest non-zero weight) is mapped to 5 cent + let p: Self::Balance = 5u64.into(); + let q: Self::Balance = Self::Balance::from(ExtrinsicBaseWeight::get().ref_time()); + smallvec![WeightToFeeCoefficient { + degree: 1, + negative: false, + coeff_frac: Perbill::from_rational(p % q, q), + coeff_integer: p / q, + }] + } } } #[cfg(feature = "constant-fees")] -impl<T> WeightToFee for WeightToFeeImpl<T> +impl<T, U, X> WeightToFee for WeightToFeeImpl<T, U, X> where T: BaseArithmetic + From<u32> + Copy + Unsigned, { diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs index 694fdec255b008068a2b530b193d8d56050e0ca8..f0807d28905a491203ab85971298c241b5df0f22 100644 --- a/runtime/common/src/pallets_config.rs +++ b/runtime/common/src/pallets_config.rs @@ -190,17 +190,18 @@ macro_rules! pallets_config { pub struct OnChargeTransaction; parameter_types! { - pub FeeMultiplier: Multiplier = Multiplier::one(); - } + pub FeeMultiplier: Multiplier = Multiplier::one(); + pub TargetWeight: Weight = Perbill::from_percent(10) * BlockWeights::get().max_block; + } impl pallet_transaction_payment::Config for Runtime { type FeeMultiplierUpdate = pallet_transaction_payment::ConstFeeMultiplier<FeeMultiplier>; - type LengthToFee = common_runtime::fees::LengthToFeeImpl<Balance>; + type LengthToFee = common_runtime::fees::LengthToFeeImpl<Balance, Self, TargetWeight>; // does a filter on the call type OnChargeTransaction = OneshotAccount; type OperationalFeeMultiplier = frame_support::traits::ConstU8<5>; type RuntimeEvent = RuntimeEvent; - type WeightToFee = common_runtime::fees::WeightToFeeImpl<Balance>; + type WeightToFee = common_runtime::fees::WeightToFeeImpl<Balance, Self, TargetWeight>; } impl pallet_oneshot_account::Config for Runtime { type Currency = Balances;