Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
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
Prev Previous commit
Next Next commit
Fix tests.
  • Loading branch information
kianenigma committed Oct 22, 2019
commit d1d57d10db5290b296859e6aa744588ad37532d5
4 changes: 1 addition & 3 deletions node/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ mod tests {
use super::Executor;
use {balances, contracts, indices, system, timestamp};
use codec::{Encode, Decode, Joiner};
use runtime_support::{
Hashable, StorageValue, StorageMap, traits::Currency,
};
use runtime_support::{Hashable, StorageValue, StorageMap, traits::Currency};
use state_machine::TestExternalities as CoreTestExternalities;
use primitives::{
Blake2Hasher, NeverNativeValue, NativeOrEncoded, map,
Expand Down
62 changes: 30 additions & 32 deletions node/runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ use sr_primitives::weights::Weight;
use sr_primitives::traits::{Convert, Saturating};
use sr_primitives::{Fixed64, Perbill};
use support::traits::{OnUnbalanced, Currency, Get};
use crate::{
Balances, System, Authorship, MaximumBlockWeight, NegativeImbalance,
TargetedFeeAdjustment,
};
use crate::{Balances, System, Authorship, MaximumBlockWeight, NegativeImbalance};

pub struct Author;
impl OnUnbalanced<NegativeImbalance> for Author {
Expand Down Expand Up @@ -53,9 +50,7 @@ impl Convert<u128, Balance> for CurrencyToVoteHandler {
/// The associated type C encapsulates a constant in units of balance per weight
pub struct LinearWeightToFee<C>(rstd::marker::PhantomData<C>);

impl<C> Convert<Weight, Balance> for LinearWeightToFee<C>
where C: Get<Balance> {

impl<C: Get<Balance>> Convert<Weight, Balance> for LinearWeightToFee<C> {
fn convert(w: Weight) -> Balance {
// substrate-node a weight of 10_000 (smallest non-zero weight) to be mapped to 10^7 units of
// fees, hence:
Expand All @@ -70,13 +65,15 @@ impl<C> Convert<Weight, Balance> for LinearWeightToFee<C>
/// v = 0.00004
/// next_weight = weight * (1 + (v . diff) + (v . diff)^2 / 2)
///
/// Where `target_weight` must be implemented as the `Get` trait.
/// 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
impl Convert<Fixed64, Fixed64> for TargetedFeeAdjustment {
pub struct TargetedFeeAdjustment<T>(rstd::marker::PhantomData<T>);

impl<T: Get<Perbill>> Convert<Fixed64, Fixed64> for TargetedFeeAdjustment<T> {
fn convert(multiplier: Fixed64) -> Fixed64 {
let block_weight = System::all_extrinsics_weight();
let max_weight = MaximumBlockWeight::get();
let target_weight = (<Self as Get<Perbill>>::get() * max_weight) as u128;
let target_weight = (T::get() * max_weight) as u128;
let block_weight = block_weight as u128;

// determines if the first_term is positive
Expand Down Expand Up @@ -122,14 +119,14 @@ mod tests {
use sr_primitives::weights::Weight;
use sr_primitives::assert_eq_error_rate;
use crate::{MaximumBlockWeight, AvailableBlockRatio, Runtime};
use crate::{constants::currency::*, TransactionPayment};
use crate::{constants::currency::*, TransactionPayment, TargetBlockFullness};

fn max() -> Weight {
MaximumBlockWeight::get()
}

fn target() -> Weight {
<Runtime as transaction_payment::Trait>::FeeMultiplierUpdate::get() * max()
TargetBlockFullness::get() * max()
}

// poc reference implementation.
Expand Down Expand Up @@ -176,7 +173,7 @@ mod tests {
run_with_system_weight(w, || {
assert_eq_error_rate!(
fee_multiplier_update(w, fm).into_inner(),
TargetedFeeAdjustment::convert(fm).into_inner(),
TargetedFeeAdjustment::<TargetBlockFullness>::convert(fm).into_inner(),
5,
);
})
Expand All @@ -191,17 +188,18 @@ mod tests {
let mut fm = Fixed64::default();
let mut iterations: u64 = 0;
loop {
let next = TargetedFeeAdjustment::convert(fm);
let next = TargetedFeeAdjustment::<TargetBlockFullness>::convert(fm);
fm = next;
if fm == Fixed64::from_rational(-1, 1) { break; }
iterations += 1;
}
println!("iteration {}, new fm = {:?}. Weight fee is now zero", iterations, fm);
assert!(iterations > 50_000, "This assertion is just a warning; Don't panic. \
Current substrate/polkadot node are configured with a _slow adjusting fee_ \
mechanism. Hence, it is really unlikely that fees collapse to zero even on an empty chain \
in less than at least of couple of thousands of empty blocks. But this simulation
indicates that fees collapsed to zero after {} almost-empty blocks. Check it",
mechanism. Hence, it is really unlikely that fees collapse to zero even on an \
empty chain in less than at least of couple of thousands of empty blocks. But this \
simulation indicates that fees collapsed to zero after {} almost-empty blocks. \
Check it",
iterations,
);
})
Expand All @@ -225,7 +223,7 @@ mod tests {

let mut iterations: u64 = 0;
loop {
let next = TargetedFeeAdjustment::convert(fm);
let next = TargetedFeeAdjustment::<TargetBlockFullness>::convert(fm);
// if no change, panic. This should never happen in this case.
if fm == next { panic!("The fee should ever increase"); }
fm = next;
Expand All @@ -251,29 +249,29 @@ mod tests {
run_with_system_weight(target() / 4, || {
// Light block. Fee is reduced a little.
assert_eq!(
TargetedFeeAdjustment::convert(Fixed64::default()),
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed64::default()),
feemul(-7500),
);
});
run_with_system_weight(target() / 2, || {
// a bit more. Fee is decreased less, meaning that the fee increases as the block grows.
assert_eq!(
TargetedFeeAdjustment::convert(Fixed64::default()),
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed64::default()),
feemul(-5000),
);

});
run_with_system_weight(target(), || {
// ideal. Original fee. No changes.
assert_eq!(
TargetedFeeAdjustment::convert(Fixed64::default()),
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed64::default()),
feemul(0),
);
});
run_with_system_weight(target() * 2, || {
// // More than ideal. Fee is increased.
assert_eq!(
TargetedFeeAdjustment::convert(Fixed64::default()),
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed64::default()),
feemul(10000),
);
});
Expand All @@ -283,20 +281,20 @@ mod tests {
fn stateful_weight_mul_grow_to_infinity() {
run_with_system_weight(target() * 2, || {
assert_eq!(
TargetedFeeAdjustment::convert(Fixed64::default()),
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed64::default()),
feemul(10000)
);
assert_eq!(
TargetedFeeAdjustment::convert(feemul(10000)),
TargetedFeeAdjustment::<TargetBlockFullness>::convert(feemul(10000)),
feemul(20000)
);
assert_eq!(
TargetedFeeAdjustment::convert(feemul(20000)),
TargetedFeeAdjustment::<TargetBlockFullness>::convert(feemul(20000)),
feemul(30000)
);
// ...
assert_eq!(
TargetedFeeAdjustment::convert(feemul(1_000_000_000)),
TargetedFeeAdjustment::<TargetBlockFullness>::convert(feemul(1_000_000_000)),
feemul(1_000_000_000 + 10000)
);
});
Expand All @@ -306,20 +304,20 @@ mod tests {
fn stateful_weight_mil_collapse_to_minus_one() {
run_with_system_weight(0, || {
assert_eq!(
TargetedFeeAdjustment::convert(Fixed64::default()),
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed64::default()),
feemul(-10000)
);
assert_eq!(
TargetedFeeAdjustment::convert(feemul(-10000)),
TargetedFeeAdjustment::<TargetBlockFullness>::convert(feemul(-10000)),
feemul(-20000)
);
assert_eq!(
TargetedFeeAdjustment::convert(feemul(-20000)),
TargetedFeeAdjustment::<TargetBlockFullness>::convert(feemul(-20000)),
feemul(-30000)
);
// ...
assert_eq!(
TargetedFeeAdjustment::convert(feemul(1_000_000_000 * -1)),
TargetedFeeAdjustment::<TargetBlockFullness>::convert(feemul(1_000_000_000 * -1)),
feemul(-1_000_000_000)
);
})
Expand All @@ -346,7 +344,7 @@ mod tests {
Weight::max_value()
].into_iter().for_each(|i| {
run_with_system_weight(i, || {
let next = TargetedFeeAdjustment::convert(Fixed64::default());
let next = TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed64::default());
let truth = fee_multiplier_update(i, Fixed64::default());
assert_eq_error_rate!(truth.into_inner(), next.into_inner(), 5);
});
Expand All @@ -358,7 +356,7 @@ mod tests {
.into_iter()
.for_each(|i| {
run_with_system_weight(i, || {
let fm = TargetedFeeAdjustment::convert(max_fm);
let fm = TargetedFeeAdjustment::<TargetBlockFullness>::convert(max_fm);
// won't grow. The convert saturates everything.
assert_eq!(fm, max_fm);
})
Expand Down
6 changes: 3 additions & 3 deletions node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub use staking::StakerStatus;

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

/// Constant values used within the runtime.
pub mod constants;
Expand Down Expand Up @@ -180,7 +180,7 @@ parameter_types! {
// setting this to zero will disable the weight fee.
pub const WeightFeeCoefficient: Balance = 1_000;
// for a sane configuration, this should always be less than `AvailableBlockRatio`.
pub const TargetedFeeAdjustment: Perbill = Perbill::from_percent(25);
pub const TargetBlockFullness: Perbill = Perbill::from_percent(25);
}

impl transaction_payment::Trait for Runtime {
Expand All @@ -189,7 +189,7 @@ impl transaction_payment::Trait for Runtime {
type TransactionBaseFee = TransactionBaseFee;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = LinearWeightToFee<WeightFeeCoefficient>;
type FeeMultiplierUpdate = TargetedFeeAdjustment;
type FeeMultiplierUpdate = TargetedFeeAdjustment<TargetBlockFullness>;
}

parameter_types! {
Expand Down