Skip to content

Commit

Permalink
Maximum value for MultiplierUpdate (paritytech#12282)
Browse files Browse the repository at this point in the history
* Maximum value for MultiplierUpdate

* Update frame/transaction-payment/src/lib.rs

Co-authored-by: Stephen Shelton <steve@brewcraft.org>

* Update lib.rs

* return constant

* fix in runtime

* Update frame/transaction-payment/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/transaction-payment/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* fixes

* remove unused import

* Update lib.rs

* more readable

* fix

* fix nits

Co-authored-by: Stephen Shelton <steve@brewcraft.org>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
  • Loading branch information
3 people committed Oct 6, 2022
1 parent a18cb81 commit 047da51
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
5 changes: 3 additions & 2 deletions bin/node/runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ mod multiplier_tests {

use crate::{
constants::{currency::*, time::*},
AdjustmentVariable, MinimumMultiplier, Runtime, RuntimeBlockWeights as BlockWeights,
System, TargetBlockFullness, TransactionPayment,
AdjustmentVariable, MaximumMultiplier, MinimumMultiplier, Runtime,
RuntimeBlockWeights as BlockWeights, System, TargetBlockFullness, TransactionPayment,
};
use frame_support::weights::{DispatchClass, Weight, WeightToFee};

Expand All @@ -152,6 +152,7 @@ mod multiplier_tests {
TargetBlockFullness,
AdjustmentVariable,
MinimumMultiplier,
MaximumMultiplier,
>::convert(fm)
}

Expand Down
12 changes: 9 additions & 3 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use sp_runtime::{
curve::PiecewiseLinear,
generic, impl_opaque_keys,
traits::{
self, BlakeTwo256, Block as BlockT, ConvertInto, NumberFor, OpaqueKeys,
self, BlakeTwo256, Block as BlockT, Bounded, ConvertInto, NumberFor, OpaqueKeys,
SaturatedConversion, StaticLookup,
},
transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity},
Expand Down Expand Up @@ -443,6 +443,7 @@ parameter_types! {
pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(1, 100_000);
pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000_000u128);
pub MaximumMultiplier: Multiplier = Bounded::max_value();
}

impl pallet_transaction_payment::Config for Runtime {
Expand All @@ -451,8 +452,13 @@ impl pallet_transaction_payment::Config for Runtime {
type OperationalFeeMultiplier = OperationalFeeMultiplier;
type WeightToFee = IdentityFee<Balance>;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
type FeeMultiplierUpdate =
TargetedFeeAdjustment<Self, TargetBlockFullness, AdjustmentVariable, MinimumMultiplier>;
type FeeMultiplierUpdate = TargetedFeeAdjustment<
Self,
TargetBlockFullness,
AdjustmentVariable,
MinimumMultiplier,
MaximumMultiplier,
>;
}

impl pallet_asset_tx_payment::Config for Runtime {
Expand Down
50 changes: 44 additions & 6 deletions frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,14 @@ type BalanceOf<T> = <<T as Config>::OnChargeTransaction as OnChargeTransaction<T
///
/// More info can be found at:
/// <https://research.web3.foundation/en/latest/polkadot/overview/2-token-economics.html>
pub struct TargetedFeeAdjustment<T, S, V, M>(sp_std::marker::PhantomData<(T, S, V, M)>);
pub struct TargetedFeeAdjustment<T, S, V, M, X>(sp_std::marker::PhantomData<(T, S, V, M, X)>);

/// Something that can convert the current multiplier to the next one.
pub trait MultiplierUpdate: Convert<Multiplier, Multiplier> {
/// Minimum multiplier
/// Minimum multiplier. Any outcome of the `convert` function should be at least this.
fn min() -> Multiplier;
/// Maximum multiplier. Any outcome of the `convert` function should be less or equal this.
fn max() -> Multiplier;
/// Target block saturation level
fn target() -> Perquintill;
/// Variability factor
Expand All @@ -143,6 +145,9 @@ impl MultiplierUpdate for () {
fn min() -> Multiplier {
Default::default()
}
fn max() -> Multiplier {
<Multiplier as sp_runtime::traits::Bounded>::max_value()
}
fn target() -> Perquintill {
Default::default()
}
Expand All @@ -151,16 +156,20 @@ impl MultiplierUpdate for () {
}
}

impl<T, S, V, M> MultiplierUpdate for TargetedFeeAdjustment<T, S, V, M>
impl<T, S, V, M, X> MultiplierUpdate for TargetedFeeAdjustment<T, S, V, M, X>
where
T: frame_system::Config,
S: Get<Perquintill>,
V: Get<Multiplier>,
M: Get<Multiplier>,
X: Get<Multiplier>,
{
fn min() -> Multiplier {
M::get()
}
fn max() -> Multiplier {
X::get()
}
fn target() -> Perquintill {
S::get()
}
Expand All @@ -169,18 +178,20 @@ where
}
}

impl<T, S, V, M> Convert<Multiplier, Multiplier> for TargetedFeeAdjustment<T, S, V, M>
impl<T, S, V, M, X> Convert<Multiplier, Multiplier> for TargetedFeeAdjustment<T, S, V, M, X>
where
T: frame_system::Config,
S: Get<Perquintill>,
V: Get<Multiplier>,
M: Get<Multiplier>,
X: Get<Multiplier>,
{
fn convert(previous: Multiplier) -> Multiplier {
// Defensive only. The multiplier in storage should always be at most positive. Nonetheless
// we recover here in case of errors, because any value below this would be stale and can
// never change.
let min_multiplier = M::get();
let max_multiplier = X::get();
let previous = previous.max(min_multiplier);

let weights = T::BlockWeights::get();
Expand Down Expand Up @@ -217,15 +228,42 @@ where

if positive {
let excess = first_term.saturating_add(second_term).saturating_mul(previous);
previous.saturating_add(excess).max(min_multiplier)
previous.saturating_add(excess).max(min_multiplier).min(max_multiplier)
} else {
// Defensive-only: first_term > second_term. Safe subtraction.
let negative = first_term.saturating_sub(second_term).saturating_mul(previous);
previous.saturating_sub(negative).max(min_multiplier)
previous.saturating_sub(negative).max(min_multiplier).min(max_multiplier)
}
}
}

/// A struct to make the fee multiplier a constant
pub struct ConstFeeMultiplier<M: Get<Multiplier>>(sp_std::marker::PhantomData<M>);

impl<M: Get<Multiplier>> MultiplierUpdate for ConstFeeMultiplier<M> {
fn min() -> Multiplier {
M::get()
}
fn max() -> Multiplier {
M::get()
}
fn target() -> Perquintill {
Default::default()
}
fn variability() -> Multiplier {
Default::default()
}
}

impl<M> Convert<Multiplier, Multiplier> for ConstFeeMultiplier<M>
where
M: Get<Multiplier>,
{
fn convert(_previous: Multiplier) -> Multiplier {
Self::min()
}
}

/// Storage releases of the pallet.
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
enum Releases {
Expand Down

0 comments on commit 047da51

Please sign in to comment.