Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
893 changes: 450 additions & 443 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ system = { package = "frame-system", git = "https://github.com/paritytech/substr
timestamp = { package = "pallet-timestamp", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
vesting = { package = "pallet-vesting", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
offences = { package = "pallet-offences", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
transaction-payment = { package = "pallet-transaction-payment", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }

primitives = { package = "polkadot-primitives", path = "../../primitives", default-features = false }
Expand Down Expand Up @@ -76,6 +77,7 @@ std = [
"system/std",
"timestamp/std",
"vesting/std",
"transaction-payment/std",
]
runtime-benchmarks = [
"libsecp256k1/hmac",
Expand Down
59 changes: 3 additions & 56 deletions runtime/common/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

//! Auxillary struct/enums for polkadot runtime.

use sp_runtime::traits::{Convert, Saturating};
use sp_runtime::{FixedPointNumber, FixedI128, Perquintill};
use frame_support::traits::{OnUnbalanced, Imbalance, Currency, Get};
use crate::{MaximumBlockWeight, NegativeImbalance};
use sp_runtime::traits::Convert;
use frame_support::traits::{OnUnbalanced, Imbalance, Currency};
use crate::NegativeImbalance;

/// Logic for the author to get a portion of fees.
pub struct ToAuthor<R>(sp_std::marker::PhantomData<R>);
Expand Down Expand Up @@ -72,55 +71,3 @@ where
{
fn convert(x: u128) -> u128 { x * Self::factor() }
}

/// Update the given multiplier based on the following formula
///
/// diff = (previous_block_weight - target_weight)/max_weight
/// v = 0.00004
/// next_weight = weight * (1 + (v * diff) + (v * diff)^2 / 2)
///
/// Where `target_weight` must be given as the `Get` implementation of the `T` generic type.
/// https://research.web3.foundation/en/latest/polkadot/Token%20Economics/#relay-chain-transaction-fees
pub struct TargetedFeeAdjustment<T, R>(sp_std::marker::PhantomData<(T, R)>);

impl<T: Get<Perquintill>, R: system::Trait> Convert<FixedI128, FixedI128> for TargetedFeeAdjustment<T, R> {
fn convert(multiplier: FixedI128) -> FixedI128 {
let max_weight = MaximumBlockWeight::get();
let block_weight = <system::Module<R>>::block_weight().total().min(max_weight);
let target_weight = (T::get() * max_weight) as u128;
let block_weight = block_weight as u128;

// determines if the first_term is positive
let positive = block_weight >= target_weight;
let diff_abs = block_weight.max(target_weight) - block_weight.min(target_weight);
// safe, diff_abs cannot exceed u64 and it can always be computed safely even with the lossy
// `FixedI128::saturating_from_rational`.
let diff = FixedI128::saturating_from_rational(diff_abs, max_weight.max(1));
let diff_squared = diff.saturating_mul(diff);

// 0.00004 = 4/100_000 = 40_000/10^9
let v = FixedI128::saturating_from_rational(4, 100_000);
// 0.00004^2 = 16/10^10 Taking the future /2 into account... 8/10^10
let v_squared_2 = FixedI128::saturating_from_rational(8, 10_000_000_000u64);

let first_term = v.saturating_mul(diff);
let second_term = v_squared_2.saturating_mul(diff_squared);

if positive {
// Note: this is merely bounded by how big the multiplier and the inner value can go,
// not by any economical reasoning.
let excess = first_term.saturating_add(second_term);
multiplier.saturating_add(excess)
} else {
// Defensive-only: first_term > second_term. Safe subtraction.
let negative = first_term.saturating_sub(second_term);
multiplier.saturating_sub(negative)
// despite the fact that apply_to saturates weight (final fee cannot go below 0)
// it is crucially important to stop here and don't further reduce the weight fee
// multiplier. While at -1, it means that the network is so un-congested that all
// transactions have no weight fee. We stop here and only increase if the network
// became more busy.
.max(FixedI128::saturating_from_integer(-1))
}
}
}
109 changes: 107 additions & 2 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ pub mod crowdfund;
pub mod impls;

use primitives::BlockNumber;
use sp_runtime::{Perbill, traits::Saturating};
use sp_runtime::{Perquintill, Perbill, FixedPointNumber, traits::Saturating};
use frame_support::{
parameter_types, traits::{Currency},
weights::{Weight, constants::WEIGHT_PER_SECOND},
};
use transaction_payment::{TargetedFeeAdjustment, Multiplier};
use static_assertions::const_assert;
pub use frame_support::weights::constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};

Expand All @@ -46,18 +47,122 @@ pub use attestations::{Call as AttestationsCall, MORE_ATTESTATIONS_IDENTIFIER};
pub use parachains::Call as ParachainsCall;

/// Implementations of some helper traits passed into runtime modules as associated types.
pub use impls::{CurrencyToVoteHandler, TargetedFeeAdjustment, ToAuthor};
pub use impls::{CurrencyToVoteHandler, ToAuthor};

pub type NegativeImbalance<T> = <balances::Module<T> as Currency<<T as system::Trait>::AccountId>>::NegativeImbalance;

/// We assume that an on-initialize consumes 10% of the weight on average, hence a single extrinsic
/// will not be allowed to consume more than `AvailableBlockRatio - 10%`.
const AVERAGE_ON_INITIALIZE_WEIGHT: Perbill = Perbill::from_percent(10);

// Common constants used in all runtimes.
parameter_types! {
pub const BlockHashCount: BlockNumber = 2400;
/// Block time that can be used by weights.
pub const MaximumBlockWeight: Weight = 2 * WEIGHT_PER_SECOND;
/// Portion of the block available to normal class of dispatches.
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
/// Maximum weight that a _single_ extrinsic can take.
pub MaximumExtrinsicWeight: Weight = AvailableBlockRatio::get()
.saturating_sub(AVERAGE_ON_INITIALIZE_WEIGHT) * MaximumBlockWeight::get();
/// Maximum length of block. 5MB.
pub const MaximumBlockLength: u32 = 5 * 1024 * 1024;
/// The portion of the `AvailableBlockRatio` that we adjust the fees with. Blocks filled less
/// than this will decrease the weight and more will increase.
pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
/// The adjustment variable of the runtime. Higher values will cause `TargetBlockFullness` to
/// change the fees more rapidly.
pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(3, 100_000);
/// Minimum amount of the multiplier. This value cannot be too low. A test case should ensure
/// that combined with `AdjustmentVariable`, we can recover from the minimum.
/// See `multiplier_can_grow_from_zero`.
pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000_000u128);
}

const_assert!(AvailableBlockRatio::get().deconstruct() >= AVERAGE_ON_INITIALIZE_WEIGHT.deconstruct());

/// Parameterized slow adjusting fee updated based on
/// https://w3f-research.readthedocs.io/en/latest/polkadot/Token%20Economics.html#-2.-slow-adjusting-mechanism
pub type SlowAdjustingFeeUpdate<R> = TargetedFeeAdjustment<
R,
TargetBlockFullness,
AdjustmentVariable,
MinimumMultiplier
>;

#[cfg(test)]
mod multiplier_tests {
use super::*;
use frame_support::{impl_outer_origin, parameter_types, weights::Weight};
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup, Convert},
Perbill,
};

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Runtime;

impl_outer_origin!{
pub enum Origin for Runtime {}
}

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const ExtrinsicBaseWeight: u64 = 100;
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
}

impl system::Trait for Runtime {
type BaseCallFilter = ();
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Call = ();
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = ();
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = ();
type BlockExecutionWeight = ();
type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
type MaximumExtrinsicWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
}

type System = system::Module<Runtime>;

fn run_with_system_weight<F>(w: Weight, assertions: F) where F: Fn() -> () {
let mut t: sp_io::TestExternalities =
system::GenesisConfig::default().build_storage::<Runtime>().unwrap().into();
t.execute_with(|| {
System::set_block_limits(w, 0);
assertions()
});
}

#[test]
fn multiplier_can_grow_from_zero() {
let minimum_multiplier = MinimumMultiplier::get();
let target = TargetBlockFullness::get() * (AvailableBlockRatio::get() * MaximumBlockWeight::get());
// if the min is too small, then this will not change, and we are doomed forever.
// the weight is 1/10th bigger than target.
run_with_system_weight(target * 101 / 100, || {
let next = SlowAdjustingFeeUpdate::<Runtime>::convert(minimum_multiplier);
assert!(next > minimum_multiplier, "{:?} !>= {:?}", next, minimum_multiplier);
})
}
}
19 changes: 6 additions & 13 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ use primitives::{
AccountId, AccountIndex, Balance, BlockNumber, Hash, Nonce, Signature, Moment,
parachain::{self, ActiveParas, AbridgedCandidateReceipt, SigningContext},
};
use runtime_common::{attestations, claims, parachains, registrar, slots,
impls::{CurrencyToVoteHandler, TargetedFeeAdjustment, ToAuthor},
use runtime_common::{
attestations, claims, parachains, registrar, slots, SlowAdjustingFeeUpdate,
impls::{CurrencyToVoteHandler, ToAuthor},
NegativeImbalance, BlockHashCount, MaximumBlockWeight, AvailableBlockRatio,
MaximumBlockLength, BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight,
MaximumExtrinsicWeight,
};
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys, ModuleId,
ApplyExtrinsicResult, KeyTypeId, Percent, Permill, Perbill, Perquintill, PerThing,
ApplyExtrinsicResult, KeyTypeId, Percent, Permill, Perbill,
transaction_validity::{TransactionValidity, TransactionSource, TransactionPriority},
curve::PiecewiseLinear,
traits::{
Expand Down Expand Up @@ -85,7 +86,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("kusama"),
impl_name: create_runtime_str!("parity-kusama"),
authoring_version: 2,
spec_version: 2010,
spec_version: 2011,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -198,22 +199,14 @@ impl balances::Trait for Runtime {

parameter_types! {
pub const TransactionByteFee: Balance = 10 * MILLICENTS;
pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
}

// for a sane configuration, this should always be less than `AvailableBlockRatio`.
const_assert!(
TargetBlockFullness::get().deconstruct() <
(AvailableBlockRatio::get().deconstruct() as <Perquintill as PerThing>::Inner)
* (<Perquintill as PerThing>::ACCURACY / <Perbill as PerThing>::ACCURACY as <Perquintill as PerThing>::Inner)
);

impl transaction_payment::Trait for Runtime {
type Currency = Balances;
type OnTransactionPayment = DealWithFees;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = WeightToFee;
type FeeMultiplierUpdate = TargetedFeeAdjustment<TargetBlockFullness, Self>;
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Self>;
}

parameter_types! {
Expand Down
20 changes: 7 additions & 13 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
#![recursion_limit="256"]

use runtime_common::{attestations, claims, parachains, registrar, slots,
impls::{CurrencyToVoteHandler, TargetedFeeAdjustment, ToAuthor},
use runtime_common::{
attestations, claims, parachains, registrar, slots, SlowAdjustingFeeUpdate,
impls::{CurrencyToVoteHandler, ToAuthor},
NegativeImbalance, BlockHashCount, MaximumBlockWeight, AvailableBlockRatio,
MaximumBlockLength, BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight,
MaximumExtrinsicWeight,
Expand All @@ -36,8 +37,8 @@ use primitives::{
};
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys, ModuleId,
ApplyExtrinsicResult, KeyTypeId, Percent, Permill, Perbill, Perquintill, PerThing,
transaction_validity::{
ApplyExtrinsicResult, KeyTypeId, Percent, Permill, Perbill,
transaction_validity::{
TransactionValidity, TransactionSource, TransactionPriority,
},
curve::PiecewiseLinear,
Expand Down Expand Up @@ -90,7 +91,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("polkadot"),
impl_name: create_runtime_str!("parity-polkadot"),
authoring_version: 0,
spec_version: 10,
spec_version: 11,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 0,
Expand Down Expand Up @@ -224,22 +225,15 @@ impl balances::Trait for Runtime {

parameter_types! {
pub const TransactionByteFee: Balance = 10 * MILLICENTS;
pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
}

// for a sane configuration, this should always be less than `AvailableBlockRatio`.
const_assert!(
TargetBlockFullness::get().deconstruct() <
(AvailableBlockRatio::get().deconstruct() as <Perquintill as PerThing>::Inner)
* (<Perquintill as PerThing>::ACCURACY / <Perbill as PerThing>::ACCURACY as <Perquintill as PerThing>::Inner)
);

impl transaction_payment::Trait for Runtime {
type Currency = Balances;
type OnTransactionPayment = DealWithFees;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = WeightToFee;
type FeeMultiplierUpdate = TargetedFeeAdjustment<TargetBlockFullness, Self>;
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Self>;
}

parameter_types! {
Expand Down
12 changes: 6 additions & 6 deletions runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ use primitives::{
AccountId, AccountIndex, Balance, BlockNumber, Hash as HashT, Nonce, Signature, Moment,
parachain::{self, ActiveParas, AbridgedCandidateReceipt, SigningContext}, ValidityError,
};
use runtime_common::{attestations, claims, parachains, registrar, slots,
impls::{CurrencyToVoteHandler, TargetedFeeAdjustment},
use runtime_common::{
attestations, claims, parachains, registrar, slots, SlowAdjustingFeeUpdate,
impls::CurrencyToVoteHandler,
BlockHashCount, MaximumBlockWeight, AvailableBlockRatio,
MaximumBlockLength, BlockExecutionWeight, ExtrinsicBaseWeight,
};
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
ApplyExtrinsicResult, Perbill, Perquintill, RuntimeDebug, KeyTypeId,
ApplyExtrinsicResult, Perbill, RuntimeDebug, KeyTypeId,
transaction_validity::{
TransactionValidity, InvalidTransaction, TransactionValidityError, TransactionSource, TransactionPriority,
},
Expand Down Expand Up @@ -81,7 +82,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("polkadot-test-runtime"),
impl_name: create_runtime_str!("parity-polkadot-test-runtime"),
authoring_version: 2,
spec_version: 1052,
spec_version: 1053,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -201,15 +202,14 @@ impl balances::Trait for Runtime {

parameter_types! {
pub const TransactionByteFee: Balance = 10 * MILLICENTS;
pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
}

impl transaction_payment::Trait for Runtime {
type Currency = Balances;
type OnTransactionPayment = ();
type TransactionByteFee = TransactionByteFee;
type WeightToFee = WeightToFee;
type FeeMultiplierUpdate = TargetedFeeAdjustment<TargetBlockFullness, Self>;
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Self>;
}

parameter_types! {
Expand Down
Loading