mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 18:07:58 +00:00
Implement FixedPoint trait. (#5877)
* Implement Fixed trait. * Fix tests * Fix tests * Fix tests 2 * Address review comment regarding from_i129. * Remove precision by using log10() as suggested in review. * Add small comments. * Use checked versions + panic for ops::*. * Remove repeated test. * Uncomment test. * Remove casts. * Add more comments. * Add tests. * Panic on saturating_div_int * More tests. * More docs. * Saturating renames. * Fix to_bound doc. * Move some impl to trait. * Add range * Add macro pre. * More round() tests. * Delete confusion. * More impl to trait * Add doc for fixedpoint op. * Remove trailing spaces. * Suggested docs changes. * More tests and comments for roundings. * Some quickcheck tests. * Add missing panic, more test/comments. * Nits. * Rename. * Remove primitives-types import. * Apply review suggestions * Fix long lines and add some fuzz. * fix long line * Update fuzzer * Bump impl * fix warnings Co-authored-by: Gavin Wood <gavin@parity.io> Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
@@ -17,13 +17,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::{Fixed128, Perquintill};
|
||||
use frame_support::{
|
||||
traits::{OnUnbalanced, Currency, Get},
|
||||
};
|
||||
use sp_runtime::{FixedPointNumber, Fixed128, Perquintill};
|
||||
use frame_support::traits::{OnUnbalanced, Currency, Get};
|
||||
use crate::{Balances, System, Authorship, MaximumBlockWeight, NegativeImbalance};
|
||||
|
||||
pub struct Author;
|
||||
@@ -69,18 +66,14 @@ impl<T: Get<Perquintill>> Convert<Fixed128, Fixed128> for TargetedFeeAdjustment<
|
||||
// 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
|
||||
// `Fixed128::from_rational`.
|
||||
let diff = Fixed128::from_rational(
|
||||
diff_abs as i128,
|
||||
NonZeroI128::new(max_weight.max(1) as i128).unwrap(),
|
||||
);
|
||||
// safe, diff_abs cannot exceed u64.
|
||||
let diff = Fixed128::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::from_rational(4, NonZeroI128::new(100_000).unwrap());
|
||||
let v = Fixed128::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::from_rational(8, NonZeroI128::new(10_000_000_000).unwrap());
|
||||
let v_squared_2 = Fixed128::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);
|
||||
@@ -99,7 +92,7 @@ impl<T: Get<Perquintill>> Convert<Fixed128, Fixed128> for TargetedFeeAdjustment<
|
||||
// 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::from_natural(-1))
|
||||
.max(Fixed128::saturating_from_integer(-1))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -111,7 +104,6 @@ mod tests {
|
||||
use crate::{MaximumBlockWeight, AvailableBlockRatio, Runtime};
|
||||
use crate::{constants::currency::*, TransactionPayment, TargetBlockFullness};
|
||||
use frame_support::weights::{Weight, WeightToFeePolynomial};
|
||||
use core::num::NonZeroI128;
|
||||
|
||||
fn max() -> Weight {
|
||||
MaximumBlockWeight::get()
|
||||
@@ -135,7 +127,7 @@ mod tests {
|
||||
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 = Fixed128::from_parts((fm * Fixed128::accuracy() as f64).round() as i128);
|
||||
let addition_fm = Fixed128::from_inner((fm * Fixed128::accuracy() as f64).round() as i128);
|
||||
previous.saturating_add(addition_fm)
|
||||
}
|
||||
|
||||
@@ -150,7 +142,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn fee_multiplier_update_poc_works() {
|
||||
let fm = Fixed128::from_rational(0, NonZeroI128::new(1).unwrap());
|
||||
let fm = Fixed128::saturating_from_rational(0, 1);
|
||||
let test_set = vec![
|
||||
(0, fm.clone()),
|
||||
(100, fm.clone()),
|
||||
@@ -164,7 +156,7 @@ mod tests {
|
||||
fee_multiplier_update(w, fm),
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(fm),
|
||||
// Error is only 1 in 10^18
|
||||
Fixed128::from_parts(1),
|
||||
Fixed128::from_inner(1),
|
||||
);
|
||||
})
|
||||
})
|
||||
@@ -180,7 +172,7 @@ mod tests {
|
||||
loop {
|
||||
let next = TargetedFeeAdjustment::<TargetBlockFullness>::convert(fm);
|
||||
fm = next;
|
||||
if fm == Fixed128::from_natural(-1) { break; }
|
||||
if fm == Fixed128::saturating_from_integer(-1) { break; }
|
||||
iterations += 1;
|
||||
}
|
||||
println!("iteration {}, new fm = {:?}. Weight fee is now zero", iterations, fm);
|
||||
@@ -220,7 +212,7 @@ mod tests {
|
||||
iterations += 1;
|
||||
let fee =
|
||||
<Runtime as pallet_transaction_payment::Trait>::WeightToFee::calc(&tx_weight);
|
||||
let adjusted_fee = fm.saturated_multiply_accumulate(fee);
|
||||
let adjusted_fee = fm.saturating_mul_acc_int(fee);
|
||||
println!(
|
||||
"iteration {}, new fm = {:?}. Fee at this point is: {} units / {} millicents, \
|
||||
{} cents, {} dollars",
|
||||
@@ -323,8 +315,8 @@ mod tests {
|
||||
|
||||
// ... stops going down at -1
|
||||
assert_eq!(
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed128::from_natural(-1)),
|
||||
Fixed128::from_natural(-1)
|
||||
TargetedFeeAdjustment::<TargetBlockFullness>::convert(Fixed128::saturating_from_integer(-1)),
|
||||
Fixed128::saturating_from_integer(-1)
|
||||
);
|
||||
})
|
||||
}
|
||||
@@ -333,7 +325,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 = Fixed128::from_natural(i128::max_value());
|
||||
let max_fm = Fixed128::saturating_from_integer(i128::max_value());
|
||||
|
||||
// check that for all values it can compute, correctly.
|
||||
vec![
|
||||
@@ -356,7 +348,7 @@ mod tests {
|
||||
run_with_system_weight(i, || {
|
||||
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));
|
||||
assert_eq_error_rate!(truth, next, Fixed128::from_inner(50_000_000));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -94,7 +94,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
|
||||
// implementation changes and behavior does not, then leave spec_version as
|
||||
// is and increment impl_version.
|
||||
spec_version: 250,
|
||||
impl_version: 0,
|
||||
impl_version: 1,
|
||||
apis: RUNTIME_API_VERSIONS,
|
||||
transaction_version: 1,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user