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

Companion PR for #6214 #1204

Merged
merged 3 commits into from
Jun 6, 2020
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
16 changes: 8 additions & 8 deletions runtime/common/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! Auxillary struct/enums for polkadot runtime.

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

Expand Down Expand Up @@ -83,8 +83,8 @@ where
/// 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<Fixed128, Fixed128> for TargetedFeeAdjustment<T, R> {
fn convert(multiplier: Fixed128) -> Fixed128 {
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;
Expand All @@ -94,14 +94,14 @@ impl<T: Get<Perquintill>, R: system::Trait> Convert<Fixed128, Fixed128> for Targ
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
// `Fixed128::saturating_from_rational`.
let diff = Fixed128::saturating_from_rational(diff_abs, max_weight.max(1));
// `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 = Fixed128::saturating_from_rational(4, 100_000);
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 = Fixed128::saturating_from_rational(8, 10_000_000_000u64);
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);
Expand All @@ -120,7 +120,7 @@ impl<T: Get<Perquintill>, R: system::Trait> Convert<Fixed128, Fixed128> for Targ
// 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(Fixed128::saturating_from_integer(-1))
.max(FixedI128::saturating_from_integer(-1))
}
}
}