Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

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

Merged
merged 4 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions bin/node/executor/tests/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
use codec::{Encode, Joiner};
use frame_support::{
traits::Currency,
weights::{
constants::ExtrinsicBaseWeight, GetDispatchInfo, IdentityFee, WeightToFeePolynomial,
},
weights::{constants::ExtrinsicBaseWeight, GetDispatchInfo, IdentityFee, WeightToFee},
};
use node_primitives::Balance;
use node_runtime::{
Expand Down
2 changes: 1 addition & 1 deletion bin/node/runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mod multiplier_tests {
AdjustmentVariable, MinimumMultiplier, Runtime, RuntimeBlockWeights as BlockWeights,
System, TargetBlockFullness, TransactionPayment,
};
use frame_support::weights::{DispatchClass, Weight, WeightToFeePolynomial};
use frame_support::weights::{DispatchClass, Weight, WeightToFee};

fn max_normal() -> Weight {
BlockWeights::get()
Expand Down
4 changes: 1 addition & 3 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,7 @@ mod tests {
ConstU32, ConstU64, ConstU8, Currency, LockIdentifier, LockableCurrency,
WithdrawReasons,
},
weights::{
ConstantMultiplier, IdentityFee, RuntimeDbWeight, Weight, WeightToFeePolynomial,
},
weights::{ConstantMultiplier, IdentityFee, RuntimeDbWeight, Weight, WeightToFee},
};
use frame_system::{Call as SystemCall, ChainContext, LastRuntimeUpgradeInfo};
use pallet_balances::Call as BalancesCall;
Expand Down
47 changes: 23 additions & 24 deletions frame/support/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ use codec::{Decode, Encode};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use smallvec::{smallvec, SmallVec};
use smallvec::SmallVec;
use sp_arithmetic::{
traits::{BaseArithmetic, Saturating, Unsigned},
Perbill,
Expand Down Expand Up @@ -625,7 +625,7 @@ impl RuntimeDbWeight {
}
}

/// One coefficient and its position in the `WeightToFeePolynomial`.
/// One coefficient and its position in the `WeightToFee`.
///
/// One term of polynomial is calculated as:
///
Expand All @@ -650,6 +650,15 @@ pub struct WeightToFeeCoefficient<Balance> {
/// A list of coefficients that represent one polynomial.
pub type WeightToFeeCoefficients<T> = SmallVec<[WeightToFeeCoefficient<T>; 4]>;

/// A trait that describes the weight to fee calculation.
pub trait WeightToFee {
/// The type that is returned as result from calculation.
type Balance: BaseArithmetic + From<u32> + Copy + Unsigned;

/// Calculates the fee from the passed `weight`.
fn calc(weight: &Weight) -> Self::Balance;
nazar-pc marked this conversation as resolved.
Show resolved Hide resolved
}

/// A trait that describes the weight to fee calculation as polynomial.
///
/// An implementor should only implement the `polynomial` function.
Expand All @@ -664,6 +673,13 @@ pub trait WeightToFeePolynomial {
/// that the order of coefficients is important as putting the negative coefficients
/// first will most likely saturate the result to zero mid evaluation.
fn polynomial() -> WeightToFeeCoefficients<Self::Balance>;
}

impl<T> WeightToFee for T
where
T: WeightToFeePolynomial,
{
type Balance = <Self as WeightToFeePolynomial>::Balance;

/// Calculates the fee from the passed `weight` according to the `polynomial`.
///
Expand Down Expand Up @@ -693,30 +709,21 @@ pub trait WeightToFeePolynomial {
}
}

/// Implementor of `WeightToFeePolynomial` that maps one unit of weight to one unit of fee.
/// Implementor of `WeightToFee` that maps one unit of weight to one unit of fee.
pub struct IdentityFee<T>(sp_std::marker::PhantomData<T>);

impl<T> WeightToFeePolynomial for IdentityFee<T>
impl<T> WeightToFee for IdentityFee<T>
where
T: BaseArithmetic + From<u32> + Copy + Unsigned,
{
type Balance = T;

fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
smallvec!(WeightToFeeCoefficient {
coeff_integer: 1u32.into(),
coeff_frac: Perbill::zero(),
negative: false,
degree: 1,
})
}

fn calc(weight: &Weight) -> Self::Balance {
Self::Balance::saturated_from(*weight)
}
}

/// Implementor of [`WeightToFeePolynomial`] that uses a constant multiplier.
/// Implementor of [`WeightToFee`] that uses a constant multiplier.
/// # Example
///
/// ```
Expand All @@ -727,22 +734,13 @@ where
/// ```
pub struct ConstantMultiplier<T, M>(sp_std::marker::PhantomData<(T, M)>);

impl<T, M> WeightToFeePolynomial for ConstantMultiplier<T, M>
impl<T, M> WeightToFee for ConstantMultiplier<T, M>
where
T: BaseArithmetic + From<u32> + Copy + Unsigned,
M: Get<T>,
{
type Balance = T;

fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
smallvec!(WeightToFeeCoefficient {
coeff_integer: M::get(),
coeff_frac: Perbill::zero(),
negative: false,
degree: 1,
})
}

fn calc(weight: &Weight) -> Self::Balance {
Self::Balance::saturated_from(*weight).saturating_mul(M::get())
}
Expand Down Expand Up @@ -834,6 +832,7 @@ impl PerDispatchClass<Weight> {
mod tests {
use super::*;
use crate::{decl_module, parameter_types, traits::Get};
use smallvec::smallvec;

pub trait Config: 'static {
type Origin;
Expand Down
1 change: 0 additions & 1 deletion frame/transaction-payment/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
] }
scale-info = { version = "2.0.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" }
Expand Down
1 change: 0 additions & 1 deletion frame/transaction-payment/asset-tx-payment/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ scale-info = { version = "2.0.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" }
Expand Down
33 changes: 16 additions & 17 deletions frame/transaction-payment/asset-tx-payment/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 calc(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 polynomial() -> WeightToFeeCoefficients<Self::Balance> {
smallvec![WeightToFeeCoefficient {
degree: 1,
coeff_frac: Perbill::zero(),
coeff_integer: WEIGHT_TO_FEE.with(|v| *v.borrow()),
negative: false,
}]
fn calc(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>;
}
Expand Down
50 changes: 12 additions & 38 deletions frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -776,22 +760,20 @@ 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::{
assert_noop, assert_ok, parameter_types,
traits::{ConstU32, ConstU64, Currency, Imbalance, OnUnbalanced},
weights::{
DispatchClass, DispatchInfo, GetDispatchInfo, PostDispatchInfo, Weight,
WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
WeightToFee as WeightToFeeT,
},
};
use frame_system as system;
Expand Down Expand Up @@ -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 calc(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 calc(weight: &Weight) -> Self::Balance {
Self::Balance::saturated_from(*weight)
.saturating_mul(TRANSACTION_BYTE_FEE.with(|v| *v.borrow()))
}
}

Expand Down