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

Apply WeightToFeePolynomials to pallet_transaction_payment's length fee #10785

Merged
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
22 changes: 17 additions & 5 deletions frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ pub mod pallet {
/// Convert a weight value into a deductible fee based on the currency type.
type WeightToFee: WeightToFeePolynomial<Balance = BalanceOf<Self>>;

/// Convert a length value into a deductible fee based on the currency type.
type LengthToFee: WeightToFeePolynomial<Balance = BalanceOf<Self>>;

/// Update the multiplier of the next block, based on the previous block's weight.
type FeeMultiplierUpdate: MultiplierUpdate;
}
Expand All @@ -304,6 +307,12 @@ pub mod pallet {
fn WeightToFee() -> Vec<WeightToFeeCoefficient<BalanceOf<T>>> {
T::WeightToFee::polynomial().to_vec()
}

#[allow(non_snake_case)]
notlesh marked this conversation as resolved.
Show resolved Hide resolved
/// The polynomial that is a pplied in order to derive fee from length.
fn LengthToFee() -> Vec<WeightToFeeCoefficient<BalanceOf<T>>> {
T::LengthToFee::polynomial().to_vec()
}
}

#[pallet::type_value]
Expand Down Expand Up @@ -512,23 +521,22 @@ where
class: DispatchClass,
) -> FeeDetails<BalanceOf<T>> {
if pays_fee == Pays::Yes {
let len = <BalanceOf<T>>::from(len);
let per_byte = T::TransactionByteFee::get();

// length fee. this is not adjusted.
let fixed_len_fee = per_byte.saturating_mul(len);

// the adjustable part of the fee.
let unadjusted_weight_fee = Self::weight_to_fee(weight);
let multiplier = Self::next_fee_multiplier();
// final adjusted weight fee.
let adjusted_weight_fee = multiplier.saturating_mul_int(unadjusted_weight_fee);

// length fee. this is adjusted via LengthToFee
notlesh marked this conversation as resolved.
Show resolved Hide resolved
let len_fee = Self::length_to_fee(len).saturating_mul(per_byte);

let base_fee = Self::weight_to_fee(T::BlockWeights::get().get(class).base_extrinsic);
FeeDetails {
inclusion_fee: Some(InclusionFee {
base_fee,
len_fee: fixed_len_fee,
len_fee,
adjusted_weight_fee,
}),
tip,
Expand All @@ -538,6 +546,10 @@ where
}
}

fn length_to_fee(length: u32) -> BalanceOf<T> {
T::LengthToFee::calc(&(length as u64)) // TODO: saturating / upper bound
Copy link
Contributor Author

@notlesh notlesh Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had left this TODO here because fn weight_to_fee below provides an upper bound.

While I'm not quite sure why it's needed in weight_to_fee, it's guaranteed to be no larger than u32::MAX in the case of length_to_fee, which is likely much smaller than max_block.

Another pair of eyes would be great though.

}

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.
Expand Down