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

Commit ef66142

Browse files
authored
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
1 parent f2d5ece commit ef66142

File tree

9 files changed

+86
-112
lines changed

9 files changed

+86
-112
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node/executor/tests/fees.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
use codec::{Encode, Joiner};
1919
use frame_support::{
2020
traits::Currency,
21-
weights::{
22-
constants::ExtrinsicBaseWeight, GetDispatchInfo, IdentityFee, WeightToFeePolynomial,
23-
},
21+
weights::{constants::ExtrinsicBaseWeight, GetDispatchInfo, IdentityFee, WeightToFee},
2422
};
2523
use node_primitives::Balance;
2624
use node_runtime::{
@@ -197,13 +195,13 @@ fn transaction_fee_is_correct() {
197195
let mut balance_alice = (100 - 69) * DOLLARS;
198196

199197
let base_weight = ExtrinsicBaseWeight::get();
200-
let base_fee = IdentityFee::<Balance>::calc(&base_weight);
198+
let base_fee = IdentityFee::<Balance>::weight_to_fee(&base_weight);
201199

202200
let length_fee = TransactionByteFee::get() * (xt.clone().encode().len() as Balance);
203201
balance_alice -= length_fee;
204202

205203
let weight = default_transfer_call().get_dispatch_info().weight;
206-
let weight_fee = IdentityFee::<Balance>::calc(&weight);
204+
let weight_fee = IdentityFee::<Balance>::weight_to_fee(&weight);
207205

208206
// we know that weight to fee multiplier is effect-less in block 1.
209207
// current weight of transfer = 200_000_000

bin/node/runtime/src/impls.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ mod multiplier_tests {
5959
AdjustmentVariable, MinimumMultiplier, Runtime, RuntimeBlockWeights as BlockWeights,
6060
System, TargetBlockFullness, TransactionPayment,
6161
};
62-
use frame_support::weights::{DispatchClass, Weight, WeightToFeePolynomial};
62+
use frame_support::weights::{DispatchClass, Weight, WeightToFee};
6363

6464
fn max_normal() -> Weight {
6565
BlockWeights::get()
@@ -234,7 +234,9 @@ mod multiplier_tests {
234234
fm = next;
235235
iterations += 1;
236236
let fee =
237-
<Runtime as pallet_transaction_payment::Config>::WeightToFee::calc(&tx_weight);
237+
<Runtime as pallet_transaction_payment::Config>::WeightToFee::weight_to_fee(
238+
&tx_weight,
239+
);
238240
let adjusted_fee = fm.saturating_mul_acc_int(fee);
239241
println!(
240242
"iteration {}, new fm = {:?}. Fee at this point is: {} units / {} millicents, \

frame/executive/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,7 @@ mod tests {
576576
ConstU32, ConstU64, ConstU8, Currency, LockIdentifier, LockableCurrency,
577577
WithdrawReasons,
578578
},
579-
weights::{
580-
ConstantMultiplier, IdentityFee, RuntimeDbWeight, Weight, WeightToFeePolynomial,
581-
},
579+
weights::{ConstantMultiplier, IdentityFee, RuntimeDbWeight, Weight, WeightToFee},
582580
};
583581
use frame_system::{Call as SystemCall, ChainContext, LastRuntimeUpgradeInfo};
584582
use pallet_balances::Call as BalancesCall;
@@ -866,7 +864,7 @@ mod tests {
866864
.get(DispatchClass::Normal)
867865
.base_extrinsic;
868866
let fee: Balance =
869-
<Runtime as pallet_transaction_payment::Config>::WeightToFee::calc(&weight);
867+
<Runtime as pallet_transaction_payment::Config>::WeightToFee::weight_to_fee(&weight);
870868
let mut t = sp_io::TestExternalities::new(t);
871869
t.execute_with(|| {
872870
Executive::initialize_block(&Header::new(
@@ -1153,7 +1151,9 @@ mod tests {
11531151
.get(DispatchClass::Normal)
11541152
.base_extrinsic;
11551153
let fee: Balance =
1156-
<Runtime as pallet_transaction_payment::Config>::WeightToFee::calc(&weight);
1154+
<Runtime as pallet_transaction_payment::Config>::WeightToFee::weight_to_fee(
1155+
&weight,
1156+
);
11571157
Executive::initialize_block(&Header::new(
11581158
1,
11591159
H256::default(),

frame/support/src/weights.rs

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ use codec::{Decode, Encode};
140140
use scale_info::TypeInfo;
141141
#[cfg(feature = "std")]
142142
use serde::{Deserialize, Serialize};
143-
use smallvec::{smallvec, SmallVec};
143+
use smallvec::SmallVec;
144144
use sp_arithmetic::{
145145
traits::{BaseArithmetic, Saturating, Unsigned},
146146
Perbill,
@@ -622,7 +622,7 @@ impl RuntimeDbWeight {
622622
}
623623
}
624624

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

650+
/// A trait that describes the weight to fee calculation.
651+
pub trait WeightToFee {
652+
/// The type that is returned as result from calculation.
653+
type Balance: BaseArithmetic + From<u32> + Copy + Unsigned;
654+
655+
/// Calculates the fee from the passed `weight`.
656+
fn weight_to_fee(weight: &Weight) -> Self::Balance;
657+
}
658+
650659
/// A trait that describes the weight to fee calculation as polynomial.
651660
///
652661
/// An implementor should only implement the `polynomial` function.
@@ -661,12 +670,19 @@ pub trait WeightToFeePolynomial {
661670
/// that the order of coefficients is important as putting the negative coefficients
662671
/// first will most likely saturate the result to zero mid evaluation.
663672
fn polynomial() -> WeightToFeeCoefficients<Self::Balance>;
673+
}
674+
675+
impl<T> WeightToFee for T
676+
where
677+
T: WeightToFeePolynomial,
678+
{
679+
type Balance = <Self as WeightToFeePolynomial>::Balance;
664680

665681
/// Calculates the fee from the passed `weight` according to the `polynomial`.
666682
///
667683
/// This should not be overriden in most circumstances. Calculation is done in the
668684
/// `Balance` type and never overflows. All evaluation is saturating.
669-
fn calc(weight: &Weight) -> Self::Balance {
685+
fn weight_to_fee(weight: &Weight) -> Self::Balance {
670686
Self::polynomial()
671687
.iter()
672688
.fold(Self::Balance::saturated_from(0u32), |mut acc, args| {
@@ -690,30 +706,21 @@ pub trait WeightToFeePolynomial {
690706
}
691707
}
692708

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

696-
impl<T> WeightToFeePolynomial for IdentityFee<T>
712+
impl<T> WeightToFee for IdentityFee<T>
697713
where
698714
T: BaseArithmetic + From<u32> + Copy + Unsigned,
699715
{
700716
type Balance = T;
701717

702-
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
703-
smallvec!(WeightToFeeCoefficient {
704-
coeff_integer: 1u32.into(),
705-
coeff_frac: Perbill::zero(),
706-
negative: false,
707-
degree: 1,
708-
})
709-
}
710-
711-
fn calc(weight: &Weight) -> Self::Balance {
718+
fn weight_to_fee(weight: &Weight) -> Self::Balance {
712719
Self::Balance::saturated_from(*weight)
713720
}
714721
}
715722

716-
/// Implementor of [`WeightToFeePolynomial`] that uses a constant multiplier.
723+
/// Implementor of [`WeightToFee`] that uses a constant multiplier.
717724
/// # Example
718725
///
719726
/// ```
@@ -724,23 +731,14 @@ where
724731
/// ```
725732
pub struct ConstantMultiplier<T, M>(sp_std::marker::PhantomData<(T, M)>);
726733

727-
impl<T, M> WeightToFeePolynomial for ConstantMultiplier<T, M>
734+
impl<T, M> WeightToFee for ConstantMultiplier<T, M>
728735
where
729736
T: BaseArithmetic + From<u32> + Copy + Unsigned,
730737
M: Get<T>,
731738
{
732739
type Balance = T;
733740

734-
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
735-
smallvec!(WeightToFeeCoefficient {
736-
coeff_integer: M::get(),
737-
coeff_frac: Perbill::zero(),
738-
negative: false,
739-
degree: 1,
740-
})
741-
}
742-
743-
fn calc(weight: &Weight) -> Self::Balance {
741+
fn weight_to_fee(weight: &Weight) -> Self::Balance {
744742
Self::Balance::saturated_from(*weight).saturating_mul(M::get())
745743
}
746744
}
@@ -831,6 +829,7 @@ impl PerDispatchClass<Weight> {
831829
mod tests {
832830
use super::*;
833831
use crate::{decl_module, parameter_types, traits::Get};
832+
use smallvec::smallvec;
834833

835834
pub trait Config: 'static {
836835
type Origin;
@@ -997,35 +996,41 @@ mod tests {
997996
#[test]
998997
fn polynomial_works() {
999998
// 100^3/2=500000 100^2*(2+1/3)=23333 700 -10000
1000-
assert_eq!(Poly::calc(&100), 514033);
999+
assert_eq!(Poly::weight_to_fee(&100), 514033);
10011000
// 10123^3/2=518677865433 10123^2*(2+1/3)=239108634 70861 -10000
1002-
assert_eq!(Poly::calc(&10_123), 518917034928);
1001+
assert_eq!(Poly::weight_to_fee(&10_123), 518917034928);
10031002
}
10041003

10051004
#[test]
10061005
fn polynomial_does_not_underflow() {
1007-
assert_eq!(Poly::calc(&0), 0);
1008-
assert_eq!(Poly::calc(&10), 0);
1006+
assert_eq!(Poly::weight_to_fee(&0), 0);
1007+
assert_eq!(Poly::weight_to_fee(&10), 0);
10091008
}
10101009

10111010
#[test]
10121011
fn polynomial_does_not_overflow() {
1013-
assert_eq!(Poly::calc(&Weight::max_value()), Balance::max_value() - 10_000);
1012+
assert_eq!(Poly::weight_to_fee(&Weight::max_value()), Balance::max_value() - 10_000);
10141013
}
10151014

10161015
#[test]
10171016
fn identity_fee_works() {
1018-
assert_eq!(IdentityFee::<Balance>::calc(&0), 0);
1019-
assert_eq!(IdentityFee::<Balance>::calc(&50), 50);
1020-
assert_eq!(IdentityFee::<Balance>::calc(&Weight::max_value()), Balance::max_value());
1017+
assert_eq!(IdentityFee::<Balance>::weight_to_fee(&0), 0);
1018+
assert_eq!(IdentityFee::<Balance>::weight_to_fee(&50), 50);
1019+
assert_eq!(
1020+
IdentityFee::<Balance>::weight_to_fee(&Weight::max_value()),
1021+
Balance::max_value()
1022+
);
10211023
}
10221024

10231025
#[test]
10241026
fn constant_fee_works() {
10251027
use crate::traits::ConstU128;
1026-
assert_eq!(ConstantMultiplier::<u128, ConstU128<100u128>>::calc(&0), 0);
1027-
assert_eq!(ConstantMultiplier::<u128, ConstU128<10u128>>::calc(&50), 500);
1028-
assert_eq!(ConstantMultiplier::<u128, ConstU128<1024u128>>::calc(&16), 16384);
1029-
assert_eq!(ConstantMultiplier::<u128, ConstU128<{ u128::MAX }>>::calc(&2), u128::MAX);
1028+
assert_eq!(ConstantMultiplier::<u128, ConstU128<100u128>>::weight_to_fee(&0), 0);
1029+
assert_eq!(ConstantMultiplier::<u128, ConstU128<10u128>>::weight_to_fee(&50), 500);
1030+
assert_eq!(ConstantMultiplier::<u128, ConstU128<1024u128>>::weight_to_fee(&16), 16384);
1031+
assert_eq!(
1032+
ConstantMultiplier::<u128, ConstU128<{ u128::MAX }>>::weight_to_fee(&2),
1033+
u128::MAX
1034+
);
10301035
}
10311036
}

frame/transaction-payment/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
1818
] }
1919
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
2020
serde = { version = "1.0.136", optional = true }
21-
smallvec = "1.8.0"
2221
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
2322
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
2423
sp-core = { version = "6.0.0", default-features = false, path = "../../primitives/core" }

frame/transaction-payment/asset-tx-payment/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ scale-info = { version = "2.1.1", default-features = false, features = ["derive"
2929
serde = { version = "1.0.136", optional = true }
3030

3131
[dev-dependencies]
32-
smallvec = "1.8.0"
3332
serde_json = "1.0.79"
3433

3534
sp-storage = { version = "6.0.0", default-features = false, path = "../../../primitives/storage" }

frame/transaction-payment/asset-tx-payment/src/tests.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,17 @@ use frame_support::{
2121
pallet_prelude::*,
2222
parameter_types,
2323
traits::{fungibles::Mutate, ConstU32, ConstU64, ConstU8, FindAuthor},
24-
weights::{
25-
DispatchClass, DispatchInfo, PostDispatchInfo, Weight, WeightToFeeCoefficient,
26-
WeightToFeeCoefficients, WeightToFeePolynomial,
27-
},
24+
weights::{DispatchClass, DispatchInfo, PostDispatchInfo, Weight, WeightToFee as WeightToFeeT},
2825
ConsensusEngineId,
2926
};
3027
use frame_system as system;
3128
use frame_system::EnsureRoot;
3229
use pallet_balances::Call as BalancesCall;
3330
use pallet_transaction_payment::CurrencyAdapter;
34-
use smallvec::smallvec;
3531
use sp_core::H256;
3632
use sp_runtime::{
3733
testing::Header,
38-
traits::{BlakeTwo256, ConvertInto, IdentityLookup, StaticLookup},
39-
Perbill,
34+
traits::{BlakeTwo256, ConvertInto, IdentityLookup, SaturatedConversion, StaticLookup},
4035
};
4136
use std::cell::RefCell;
4237

@@ -83,8 +78,8 @@ impl Get<frame_system::limits::BlockWeights> for BlockWeights {
8378
}
8479

8580
parameter_types! {
86-
pub static TransactionByteFee: u64 = 1;
8781
pub static WeightToFee: u64 = 1;
82+
pub static TransactionByteFee: u64 = 1;
8883
}
8984

9085
impl frame_system::Config for Runtime {
@@ -130,23 +125,27 @@ impl pallet_balances::Config for Runtime {
130125
type ReserveIdentifier = [u8; 8];
131126
}
132127

133-
impl WeightToFeePolynomial for WeightToFee {
128+
impl WeightToFeeT for WeightToFee {
129+
type Balance = u64;
130+
131+
fn weight_to_fee(weight: &Weight) -> Self::Balance {
132+
Self::Balance::saturated_from(*weight).saturating_mul(WEIGHT_TO_FEE.with(|v| *v.borrow()))
133+
}
134+
}
135+
136+
impl WeightToFeeT for TransactionByteFee {
134137
type Balance = u64;
135138

136-
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
137-
smallvec![WeightToFeeCoefficient {
138-
degree: 1,
139-
coeff_frac: Perbill::zero(),
140-
coeff_integer: WEIGHT_TO_FEE.with(|v| *v.borrow()),
141-
negative: false,
142-
}]
139+
fn weight_to_fee(weight: &Weight) -> Self::Balance {
140+
Self::Balance::saturated_from(*weight)
141+
.saturating_mul(TRANSACTION_BYTE_FEE.with(|v| *v.borrow()))
143142
}
144143
}
145144

146145
impl pallet_transaction_payment::Config for Runtime {
147146
type OnChargeTransaction = CurrencyAdapter<Balances, ()>;
148147
type WeightToFee = WeightToFee;
149-
type LengthToFee = WeightToFee;
148+
type LengthToFee = TransactionByteFee;
150149
type FeeMultiplierUpdate = ();
151150
type OperationalFeeMultiplier = ConstU8<5>;
152151
}

0 commit comments

Comments
 (0)