mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 03:31:10 +00:00
Companion PR for substrate#5446 (Update Weights to u64) (#994)
This commit is contained in:
@@ -16,8 +16,9 @@
|
||||
|
||||
//! Auxillary struct/enums for polkadot runtime.
|
||||
|
||||
use core::num::NonZeroI128;
|
||||
use sp_runtime::traits::{Convert, Saturating};
|
||||
use sp_runtime::{Fixed64, Perbill};
|
||||
use sp_runtime::{Fixed128, Perquintill};
|
||||
use frame_support::traits::{OnUnbalanced, Imbalance, Currency, Get};
|
||||
use crate::{MaximumBlockWeight, NegativeImbalance};
|
||||
|
||||
@@ -75,16 +76,16 @@ where
|
||||
|
||||
/// Update the given multiplier based on the following formula
|
||||
///
|
||||
/// diff = (target_weight - previous_block_weight)
|
||||
/// diff = (previous_block_weight - target_weight)/max_weight
|
||||
/// v = 0.00004
|
||||
/// next_weight = weight * (1 + (v . diff) + (v . diff)^2 / 2)
|
||||
/// 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<Perbill>, R: system::Trait> Convert<Fixed64, Fixed64> for TargetedFeeAdjustment<T, R> {
|
||||
fn convert(multiplier: Fixed64) -> Fixed64 {
|
||||
impl<T: Get<Perquintill>, R: system::Trait> Convert<Fixed128, Fixed128> for TargetedFeeAdjustment<T, R> {
|
||||
fn convert(multiplier: Fixed128) -> Fixed128 {
|
||||
let block_weight = <system::Module<R>>::all_extrinsics_weight();
|
||||
let max_weight = MaximumBlockWeight::get();
|
||||
let target_weight = (T::get() * max_weight) as u128;
|
||||
@@ -93,19 +94,20 @@ impl<T: Get<Perbill>, R: system::Trait> Convert<Fixed64, Fixed64> for TargetedFe
|
||||
// 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);
|
||||
// diff is within u32, safe.
|
||||
let diff = Fixed64::from_rational(diff_abs as i64, max_weight as u64);
|
||||
// safe, diff_abs cannot exceed u64 and it can always be computed safely even with the lossy
|
||||
// `Fixed128::from_rational`.
|
||||
let diff = Fixed128::from_rational(
|
||||
diff_abs as i128,
|
||||
NonZeroI128::new(max_weight.max(1) as i128).unwrap(),
|
||||
);
|
||||
let diff_squared = diff.saturating_mul(diff);
|
||||
|
||||
// 0.00004 = 4/100_000 = 40_000/10^9
|
||||
let v = Fixed64::from_rational(4, 100_000);
|
||||
// 0.00004^2 = 16/10^10 ~= 2/10^9. Taking the future /2 into account, then it is just 1
|
||||
// parts from a billionth.
|
||||
let v_squared_2 = Fixed64::from_rational(1, 1_000_000_000);
|
||||
let v = Fixed128::from_rational(4, NonZeroI128::new(100_000).unwrap());
|
||||
// 0.00004^2 = 16/10^10 Taking the future /2 into account... 8/10^10
|
||||
let v_squared_2 = Fixed128::from_rational(8, NonZeroI128::new(10_000_000_000).unwrap());
|
||||
|
||||
let first_term = v.saturating_mul(diff);
|
||||
// It is very unlikely that this will exist (in our poor perbill estimate) but we are giving
|
||||
// it a shot.
|
||||
let second_term = v_squared_2.saturating_mul(diff_squared);
|
||||
|
||||
if positive {
|
||||
@@ -114,15 +116,15 @@ impl<T: Get<Perbill>, R: system::Trait> Convert<Fixed64, Fixed64> for TargetedFe
|
||||
let excess = first_term.saturating_add(second_term);
|
||||
multiplier.saturating_add(excess)
|
||||
} else {
|
||||
// Proof: first_term > second_term. Safe subtraction.
|
||||
let negative = first_term - second_term;
|
||||
// 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(Fixed64::from_rational(-1, 1))
|
||||
.max(Fixed128::from_natural(-1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user