mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 15:47:58 +00:00
Weights to u64 + Balances Weights (#5446)
Co-Authored-By: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
@@ -16,9 +16,10 @@
|
||||
|
||||
//! Some configurable implementations as associated type for the substrate runtime.
|
||||
|
||||
use core::num::NonZeroI128;
|
||||
use node_primitives::Balance;
|
||||
use sp_runtime::traits::{Convert, Saturating};
|
||||
use sp_runtime::{Fixed64, Perbill};
|
||||
use sp_runtime::{Fixed128, Perquintill};
|
||||
use frame_support::{traits::{OnUnbalanced, Currency, Get}, weights::Weight};
|
||||
use crate::{Balances, System, Authorship, MaximumBlockWeight, NegativeImbalance};
|
||||
|
||||
@@ -51,8 +52,7 @@ pub struct LinearWeightToFee<C>(sp_std::marker::PhantomData<C>);
|
||||
|
||||
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:
|
||||
// setting this to zero will disable the weight fee.
|
||||
let coefficient = C::get();
|
||||
Balance::from(w).saturating_mul(coefficient)
|
||||
}
|
||||
@@ -60,16 +60,16 @@ impl<C: Get<Balance>> Convert<Weight, Balance> for LinearWeightToFee<C> {
|
||||
|
||||
/// Update the given multiplier based on the following formula
|
||||
///
|
||||
/// diff = (previous_block_weight - target_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>(sp_std::marker::PhantomData<T>);
|
||||
|
||||
impl<T: Get<Perbill>> Convert<Fixed64, Fixed64> for TargetedFeeAdjustment<T> {
|
||||
fn convert(multiplier: Fixed64) -> Fixed64 {
|
||||
impl<T: Get<Perquintill>> Convert<Fixed128, Fixed128> for TargetedFeeAdjustment<T> {
|
||||
fn convert(multiplier: Fixed128) -> Fixed128 {
|
||||
let block_weight = System::all_extrinsics_weight();
|
||||
let max_weight = MaximumBlockWeight::get();
|
||||
let target_weight = (T::get() * max_weight) as u128;
|
||||
@@ -78,19 +78,20 @@ impl<T: Get<Perbill>> Convert<Fixed64, Fixed64> for TargetedFeeAdjustment<T> {
|
||||
// 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 {
|
||||
@@ -99,15 +100,15 @@ impl<T: Get<Perbill>> Convert<Fixed64, Fixed64> for TargetedFeeAdjustment<T> {
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,6 +120,7 @@ mod tests {
|
||||
use crate::{MaximumBlockWeight, AvailableBlockRatio, Runtime};
|
||||
use crate::{constants::currency::*, TransactionPayment, TargetBlockFullness};
|
||||
use frame_support::weights::Weight;
|
||||
use core::num::NonZeroI128;
|
||||
|
||||
fn max() -> Weight {
|
||||
MaximumBlockWeight::get()
|
||||
@@ -129,26 +131,22 @@ mod tests {
|
||||
}
|
||||
|
||||
// poc reference implementation.
|
||||
fn fee_multiplier_update(block_weight: Weight, previous: Fixed64) -> Fixed64 {
|
||||
let block_weight = block_weight as f32;
|
||||
let v: f32 = 0.00004;
|
||||
fn fee_multiplier_update(block_weight: Weight, previous: Fixed128) -> Fixed128 {
|
||||
let block_weight = block_weight as f64;
|
||||
let v: f64 = 0.00004;
|
||||
|
||||
// maximum tx weight
|
||||
let m = max() as f32;
|
||||
let m = max() as f64;
|
||||
// Ideal saturation in terms of weight
|
||||
let ss = target() as f32;
|
||||
let ss = target() as f64;
|
||||
// Current saturation in terms of weight
|
||||
let s = block_weight;
|
||||
|
||||
let fm = v * (s/m - ss/m) + v.powi(2) * (s/m - ss/m).powi(2) / 2.0;
|
||||
let addition_fm = Fixed64::from_parts((fm * 1_000_000_000_f32).round() as i64);
|
||||
let addition_fm = Fixed128::from_parts((fm * Fixed128::accuracy() as f64).round() as i128);
|
||||
previous.saturating_add(addition_fm)
|
||||
}
|
||||
|
||||
fn feemul(parts: i64) -> Fixed64 {
|
||||
Fixed64::from_parts(parts)
|
||||
}
|
||||
|
||||
fn run_with_system_weight<F>(w: Weight, assertions: F) where F: Fn() -> () {
|
||||
let mut t: sp_io::TestExternalities =
|
||||
frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap().into();
|
||||
@@ -160,7 +158,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn fee_multiplier_update_poc_works() {
|
||||
let fm = Fixed64::from_rational(0, 1);
|
||||
let fm = Fixed128::from_rational(0, NonZeroI128::new(1).unwrap());
|
||||
let test_set = vec![
|
||||
(0, fm.clone()),
|
||||
(100, fm.clone()),
|
||||
@@ -171,9 +169,10 @@ mod tests {
|
||||
test_set.into_iter().for_each(|(w, fm)| {
|
||||
run_with_system_weight(w, || {
|
||||
assert_eq_error_rate!(
|
||||
fee_multiplier_update(w, fm).into_inner(),
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(fm).into_inner(),
|
||||
5,
|
||||
fee_multiplier_update(w, fm),
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(fm),
|
||||
// Error is only 1 in 10^18
|
||||
Fixed128::from_parts(1),
|
||||
);
|
||||
})
|
||||
})
|
||||
@@ -184,12 +183,12 @@ mod tests {
|
||||
// just a few txs per_block.
|
||||
let block_weight = 0;
|
||||
run_with_system_weight(block_weight, || {
|
||||
let mut fm = Fixed64::default();
|
||||
let mut fm = Fixed128::default();
|
||||
let mut iterations: u64 = 0;
|
||||
loop {
|
||||
let next = TargetedFeeAdjustment::<TargetBlockFullness>::convert(fm);
|
||||
fm = next;
|
||||
if fm == Fixed64::from_rational(-1, 1) { break; }
|
||||
if fm == Fixed128::from_natural(-1) { break; }
|
||||
iterations += 1;
|
||||
}
|
||||
println!("iteration {}, new fm = {:?}. Weight fee is now zero", iterations, fm);
|
||||
@@ -217,7 +216,7 @@ mod tests {
|
||||
|
||||
run_with_system_weight(block_weight, || {
|
||||
// initial value configured on module
|
||||
let mut fm = Fixed64::default();
|
||||
let mut fm = Fixed128::default();
|
||||
assert_eq!(fm, TransactionPayment::next_fee_multiplier());
|
||||
|
||||
let mut iterations: u64 = 0;
|
||||
@@ -245,79 +244,94 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn stateless_weight_mul() {
|
||||
// This test will show that heavy blocks have a weight multiplier greater than 0
|
||||
// and light blocks will have a weight multiplier less than 0.
|
||||
run_with_system_weight(target() / 4, || {
|
||||
// Light block. Fee is reduced a little.
|
||||
// `fee_multiplier_update` is enough as it is the absolute truth value.
|
||||
let next = TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed128::default());
|
||||
assert_eq!(
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed64::default()),
|
||||
feemul(-7500),
|
||||
next,
|
||||
fee_multiplier_update(target() / 4 ,Fixed128::default())
|
||||
);
|
||||
|
||||
// Light block. Fee is reduced a little.
|
||||
assert!(next < Fixed128::zero())
|
||||
});
|
||||
run_with_system_weight(target() / 2, || {
|
||||
// a bit more. Fee is decreased less, meaning that the fee increases as the block grows.
|
||||
let next = TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed128::default());
|
||||
assert_eq!(
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed64::default()),
|
||||
feemul(-5000),
|
||||
next,
|
||||
fee_multiplier_update(target() / 2 ,Fixed128::default())
|
||||
);
|
||||
|
||||
// Light block. Fee is reduced a little.
|
||||
assert!(next < Fixed128::zero())
|
||||
|
||||
});
|
||||
run_with_system_weight(target(), || {
|
||||
// ideal. Original fee. No changes.
|
||||
assert_eq!(
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed64::default()),
|
||||
feemul(0),
|
||||
);
|
||||
let next = TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed128::default());
|
||||
assert_eq!(next, Fixed128::zero())
|
||||
});
|
||||
run_with_system_weight(target() * 2, || {
|
||||
// // More than ideal. Fee is increased.
|
||||
// More than ideal. Fee is increased.
|
||||
let next = TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed128::default());
|
||||
assert_eq!(
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed64::default()),
|
||||
feemul(10000),
|
||||
next,
|
||||
fee_multiplier_update(target() * 2 ,Fixed128::default())
|
||||
);
|
||||
|
||||
// Heavy block. Fee is increased a little.
|
||||
assert!(next > Fixed128::zero())
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stateful_weight_mul_grow_to_infinity() {
|
||||
run_with_system_weight(target() * 2, || {
|
||||
assert_eq!(
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed64::default()),
|
||||
feemul(10000)
|
||||
);
|
||||
assert_eq!(
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(feemul(10000)),
|
||||
feemul(20000)
|
||||
);
|
||||
assert_eq!(
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(feemul(20000)),
|
||||
feemul(30000)
|
||||
);
|
||||
// ...
|
||||
assert_eq!(
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(feemul(1_000_000_000)),
|
||||
feemul(1_000_000_000 + 10000)
|
||||
);
|
||||
let mut original = Fixed128::default();
|
||||
let mut next = Fixed128::default();
|
||||
|
||||
(0..1_000).for_each(|_| {
|
||||
next = TargetedFeeAdjustment::<TargetBlockFullness>::convert(original);
|
||||
assert_eq!(
|
||||
next,
|
||||
fee_multiplier_update(target() * 2, original),
|
||||
);
|
||||
// must always increase
|
||||
assert!(next > original);
|
||||
original = next;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stateful_weight_mil_collapse_to_minus_one() {
|
||||
run_with_system_weight(0, || {
|
||||
let mut original = Fixed128::default(); // 0
|
||||
let mut next;
|
||||
|
||||
// decreases
|
||||
next = TargetedFeeAdjustment::<TargetBlockFullness>::convert(original);
|
||||
assert_eq!(
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed64::default()),
|
||||
feemul(-10000)
|
||||
next,
|
||||
fee_multiplier_update(0, original),
|
||||
);
|
||||
assert!(next < original);
|
||||
original = next;
|
||||
|
||||
// keeps decreasing
|
||||
next = TargetedFeeAdjustment::<TargetBlockFullness>::convert(original);
|
||||
assert_eq!(
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(feemul(-10000)),
|
||||
feemul(-20000)
|
||||
next,
|
||||
fee_multiplier_update(0, original),
|
||||
);
|
||||
assert!(next < original);
|
||||
|
||||
// ... stops going down at -1
|
||||
assert_eq!(
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(feemul(-20000)),
|
||||
feemul(-30000)
|
||||
);
|
||||
// ...
|
||||
assert_eq!(
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(feemul(1_000_000_000 * -1)),
|
||||
feemul(-1_000_000_000)
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed128::from_natural(-1)),
|
||||
Fixed128::from_natural(-1)
|
||||
);
|
||||
})
|
||||
}
|
||||
@@ -326,7 +340,7 @@ mod tests {
|
||||
fn weight_to_fee_should_not_overflow_on_large_weights() {
|
||||
let kb = 1024 as Weight;
|
||||
let mb = kb * kb;
|
||||
let max_fm = Fixed64::from_natural(i64::max_value());
|
||||
let max_fm = Fixed128::from_natural(i128::max_value());
|
||||
|
||||
// check that for all values it can compute, correctly.
|
||||
vec![
|
||||
@@ -339,13 +353,17 @@ mod tests {
|
||||
100 * kb,
|
||||
mb,
|
||||
10 * mb,
|
||||
2147483647,
|
||||
4294967295,
|
||||
MaximumBlockWeight::get() / 2,
|
||||
MaximumBlockWeight::get(),
|
||||
Weight::max_value() / 2,
|
||||
Weight::max_value(),
|
||||
].into_iter().for_each(|i| {
|
||||
run_with_system_weight(i, || {
|
||||
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);
|
||||
let next = TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed128::default());
|
||||
let truth = fee_multiplier_update(i, Fixed128::default());
|
||||
assert_eq_error_rate!(truth, next, Fixed128::from_parts(50_000_000));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user