Introduce WeightToFee trait instead of WeightToFeePolynomial and make WeightToFeePolynomial implement it instead (#11415)

* Introduce `WeightToFee` trait instead of `WeightToFeePolynomial` and make `WeightToFeePolynomial` implement it instead

* Rename `WeightToFee::calc()` to `WeightToFee::wight_to_fee()`

* Fix typo
This commit is contained in:
Nazar Mokrynskyi
2022-05-25 08:32:11 +03:00
committed by GitHub
parent 29474f9893
commit 0c25a2ee40
9 changed files with 86 additions and 112 deletions
@@ -18,7 +18,6 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
] }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
serde = { version = "1.0.136", optional = true }
smallvec = "1.8.0"
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
sp-core = { version = "6.0.0", default-features = false, path = "../../primitives/core" }
@@ -29,7 +29,6 @@ scale-info = { version = "2.1.1", default-features = false, features = ["derive"
serde = { version = "1.0.136", optional = true }
[dev-dependencies]
smallvec = "1.8.0"
serde_json = "1.0.79"
sp-storage = { version = "6.0.0", default-features = false, path = "../../../primitives/storage" }
@@ -21,22 +21,17 @@ use frame_support::{
pallet_prelude::*,
parameter_types,
traits::{fungibles::Mutate, ConstU32, ConstU64, ConstU8, FindAuthor},
weights::{
DispatchClass, DispatchInfo, PostDispatchInfo, Weight, WeightToFeeCoefficient,
WeightToFeeCoefficients, WeightToFeePolynomial,
},
weights::{DispatchClass, DispatchInfo, PostDispatchInfo, Weight, WeightToFee as WeightToFeeT},
ConsensusEngineId,
};
use frame_system as system;
use frame_system::EnsureRoot;
use pallet_balances::Call as BalancesCall;
use pallet_transaction_payment::CurrencyAdapter;
use smallvec::smallvec;
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, ConvertInto, IdentityLookup, StaticLookup},
Perbill,
traits::{BlakeTwo256, ConvertInto, IdentityLookup, SaturatedConversion, StaticLookup},
};
use std::cell::RefCell;
@@ -83,8 +78,8 @@ impl Get<frame_system::limits::BlockWeights> for BlockWeights {
}
parameter_types! {
pub static TransactionByteFee: u64 = 1;
pub static WeightToFee: u64 = 1;
pub static TransactionByteFee: u64 = 1;
}
impl frame_system::Config for Runtime {
@@ -130,23 +125,27 @@ impl pallet_balances::Config for Runtime {
type ReserveIdentifier = [u8; 8];
}
impl WeightToFeePolynomial for WeightToFee {
impl WeightToFeeT for WeightToFee {
type Balance = u64;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
smallvec![WeightToFeeCoefficient {
degree: 1,
coeff_frac: Perbill::zero(),
coeff_integer: WEIGHT_TO_FEE.with(|v| *v.borrow()),
negative: false,
}]
fn weight_to_fee(weight: &Weight) -> Self::Balance {
Self::Balance::saturated_from(*weight).saturating_mul(WEIGHT_TO_FEE.with(|v| *v.borrow()))
}
}
impl WeightToFeeT for TransactionByteFee {
type Balance = u64;
fn weight_to_fee(weight: &Weight) -> Self::Balance {
Self::Balance::saturated_from(*weight)
.saturating_mul(TRANSACTION_BYTE_FEE.with(|v| *v.borrow()))
}
}
impl pallet_transaction_payment::Config for Runtime {
type OnChargeTransaction = CurrencyAdapter<Balances, ()>;
type WeightToFee = WeightToFee;
type LengthToFee = WeightToFee;
type LengthToFee = TransactionByteFee;
type FeeMultiplierUpdate = ();
type OperationalFeeMultiplier = ConstU8<5>;
}
+14 -40
View File
@@ -66,8 +66,7 @@ use frame_support::{
dispatch::DispatchResult,
traits::{EstimateCallFee, Get},
weights::{
DispatchClass, DispatchInfo, GetDispatchInfo, Pays, PostDispatchInfo, Weight,
WeightToFeeCoefficient, WeightToFeePolynomial,
DispatchClass, DispatchInfo, GetDispatchInfo, Pays, PostDispatchInfo, Weight, WeightToFee,
},
};
@@ -283,30 +282,15 @@ pub mod pallet {
type OperationalFeeMultiplier: Get<u8>;
/// Convert a weight value into a deductible fee based on the currency type.
type WeightToFee: WeightToFeePolynomial<Balance = BalanceOf<Self>>;
type WeightToFee: WeightToFee<Balance = BalanceOf<Self>>;
/// Convert a length value into a deductible fee based on the currency type.
type LengthToFee: WeightToFeePolynomial<Balance = BalanceOf<Self>>;
type LengthToFee: WeightToFee<Balance = BalanceOf<Self>>;
/// Update the multiplier of the next block, based on the previous block's weight.
type FeeMultiplierUpdate: MultiplierUpdate;
}
#[pallet::extra_constants]
impl<T: Config> Pallet<T> {
#[pallet::constant_name(WeightToFee)]
/// The polynomial that is applied in order to derive fee from weight.
fn weight_to_fee_polynomial() -> Vec<WeightToFeeCoefficient<BalanceOf<T>>> {
T::WeightToFee::polynomial().to_vec()
}
/// The polynomial that is applied in order to derive fee from length.
#[pallet::constant_name(LengthToFee)]
fn length_to_fee_polynomial() -> Vec<WeightToFeeCoefficient<BalanceOf<T>>> {
T::LengthToFee::polynomial().to_vec()
}
}
#[pallet::type_value]
pub fn NextFeeMultiplierOnEmpty() -> Multiplier {
Multiplier::saturating_from_integer(1)
@@ -533,14 +517,14 @@ where
}
fn length_to_fee(length: u32) -> BalanceOf<T> {
T::LengthToFee::calc(&(length as Weight))
T::LengthToFee::weight_to_fee(&(length as Weight))
}
fn weight_to_fee(weight: Weight) -> BalanceOf<T> {
// cap the weight to the maximum defined in runtime, otherwise it will be the
// `Bounded` maximum of its data type, which is not desired.
let capped_weight = weight.min(T::BlockWeights::get().max_block);
T::WeightToFee::calc(&capped_weight)
T::WeightToFee::weight_to_fee(&capped_weight)
}
}
@@ -776,14 +760,12 @@ mod tests {
use std::cell::RefCell;
use codec::Encode;
use smallvec::smallvec;
use sp_core::H256;
use sp_runtime::{
testing::{Header, TestXt},
traits::{BlakeTwo256, IdentityLookup, One},
transaction_validity::InvalidTransaction,
Perbill,
};
use frame_support::{
@@ -791,7 +773,7 @@ mod tests {
traits::{ConstU32, ConstU64, Currency, Imbalance, OnUnbalanced},
weights::{
DispatchClass, DispatchInfo, GetDispatchInfo, PostDispatchInfo, Weight,
WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
WeightToFee as WeightToFeeT,
},
};
use frame_system as system;
@@ -879,29 +861,21 @@ mod tests {
type WeightInfo = ();
}
impl WeightToFeePolynomial for WeightToFee {
impl WeightToFeeT for WeightToFee {
type Balance = u64;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
smallvec![WeightToFeeCoefficient {
degree: 1,
coeff_frac: Perbill::zero(),
coeff_integer: WEIGHT_TO_FEE.with(|v| *v.borrow()),
negative: false,
}]
fn weight_to_fee(weight: &Weight) -> Self::Balance {
Self::Balance::saturated_from(*weight)
.saturating_mul(WEIGHT_TO_FEE.with(|v| *v.borrow()))
}
}
impl WeightToFeePolynomial for TransactionByteFee {
impl WeightToFeeT for TransactionByteFee {
type Balance = u64;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
smallvec![WeightToFeeCoefficient {
degree: 1,
coeff_frac: Perbill::zero(),
coeff_integer: TRANSACTION_BYTE_FEE.with(|v| *v.borrow()),
negative: false,
}]
fn weight_to_fee(weight: &Weight) -> Self::Balance {
Self::Balance::saturated_from(*weight)
.saturating_mul(TRANSACTION_BYTE_FEE.with(|v| *v.borrow()))
}
}