Skip to content
Snippets Groups Projects

Fix #232

Merged Benjamin Gallois requested to merge fix-232 into master
2 files
+ 55
20
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 50
16
@@ -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,
{
Loading