Skip to content
Snippets Groups Projects

Fix #232

Merged Benjamin Gallois requested to merge fix-232 into master
Compare and
2 files
+ 54
19
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 49
15
@@ -20,9 +20,10 @@
// For testing purposes, we adopt a human-predictable weight system that remains invariant to the chosen fees model for release.
// This involves setting a constant weight_to_fee equal to 1 and a constant length_to_fee set to 0, resulting in each extrinsic costing 2 (2cG).
use frame_support::pallet_prelude::DispatchClass;
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,
@@ -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,25 +69,44 @@ 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,
}]
}
}
}
Loading