diff --git a/polkadot/Cargo.lock b/polkadot/Cargo.lock index 600d7977e2..58f9561276 100644 --- a/polkadot/Cargo.lock +++ b/polkadot/Cargo.lock @@ -5724,6 +5724,7 @@ dependencies = [ "polkadot-primitives", "polkadot-runtime-common", "rustc-hex", + "separator", "serde", "serde_derive", "serde_json", diff --git a/polkadot/runtime/common/src/lib.rs b/polkadot/runtime/common/src/lib.rs index 2f62eb9566..4ab1fa8fd2 100644 --- a/polkadot/runtime/common/src/lib.rs +++ b/polkadot/runtime/common/src/lib.rs @@ -54,9 +54,9 @@ pub type NegativeImbalance = as Currency<(w: Weight, assertions: F) where F: Fn() -> () { + fn run_with_system_weight(w: Weight, mut assertions: F) where F: FnMut() -> () { let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::default().build_storage::().unwrap().into(); t.execute_with(|| { @@ -258,10 +258,32 @@ mod multiplier_tests { let target = TargetBlockFullness::get() * BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap(); // if the min is too small, then this will not change, and we are doomed forever. - // the weight is 1/10th bigger than target. + // the weight is 1/100th bigger than target. run_with_system_weight(target * 101 / 100, || { let next = SlowAdjustingFeeUpdate::::convert(minimum_multiplier); assert!(next > minimum_multiplier, "{:?} !>= {:?}", next, minimum_multiplier); }) } + + #[test] + #[ignore] + fn multiplier_growth_simulator() { + // assume the multiplier is initially set to its minimum. We update it with values twice the + //target (target is 25%, thus 50%) and we see at which point it reaches 1. + let mut multiplier = MinimumMultiplier::get(); + let block_weight = TargetBlockFullness::get() + * BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap() + * 2; + let mut blocks = 0; + while multiplier <= Multiplier::one() { + run_with_system_weight(block_weight, || { + let next = SlowAdjustingFeeUpdate::::convert(multiplier); + // ensure that it is growing as well. + assert!(next > multiplier, "{:?} !>= {:?}", next, multiplier); + multiplier = next; + }); + blocks += 1; + println!("block = {} multiplier {:?}", blocks, multiplier); + } + } } diff --git a/polkadot/runtime/kusama/src/lib.rs b/polkadot/runtime/kusama/src/lib.rs index 69b90cbaa6..10c4152c40 100644 --- a/polkadot/runtime/kusama/src/lib.rs +++ b/polkadot/runtime/kusama/src/lib.rs @@ -1377,39 +1377,23 @@ mod test_fees { let len = call.using_encoded(|e| e.len()) as u32; let mut ext = sp_io::TestExternalities::new_empty(); - ext.execute_with(|| { - pallet_transaction_payment::NextFeeMultiplier::put(min_multiplier); - let fee = TransactionPayment::compute_fee(len, &info, 0); - println!( - "weight = {:?} // multiplier = {:?} // full transfer fee = {:?}", - info.weight.separated_string(), - pallet_transaction_payment::NextFeeMultiplier::get(), - fee.separated_string(), - ); - }); + let mut test_with_multiplier = |m| { + ext.execute_with(|| { + pallet_transaction_payment::NextFeeMultiplier::put(m); + let fee = TransactionPayment::compute_fee(len, &info, 0); + println!( + "weight = {:?} // multiplier = {:?} // full transfer fee = {:?}", + info.weight.separated_string(), + pallet_transaction_payment::NextFeeMultiplier::get(), + fee.separated_string(), + ); + }); + }; - ext.execute_with(|| { - let mul = Multiplier::saturating_from_rational(1, 1000_000_000u128); - pallet_transaction_payment::NextFeeMultiplier::put(mul); - let fee = TransactionPayment::compute_fee(len, &info, 0); - println!( - "weight = {:?} // multiplier = {:?} // full transfer fee = {:?}", - info.weight.separated_string(), - pallet_transaction_payment::NextFeeMultiplier::get(), - fee.separated_string(), - ); - }); - - ext.execute_with(|| { - let mul = Multiplier::saturating_from_rational(1, 1u128); - pallet_transaction_payment::NextFeeMultiplier::put(mul); - let fee = TransactionPayment::compute_fee(len, &info, 0); - println!( - "weight = {:?} // multiplier = {:?} // full transfer fee = {:?}", - info.weight.separated_string(), - pallet_transaction_payment::NextFeeMultiplier::get(), - fee.separated_string(), - ); - }); + test_with_multiplier(min_multiplier); + test_with_multiplier(Multiplier::saturating_from_rational(1, 1u128)); + test_with_multiplier(Multiplier::saturating_from_rational(1, 1_000u128)); + test_with_multiplier(Multiplier::saturating_from_rational(1, 1_000_000u128)); + test_with_multiplier(Multiplier::saturating_from_rational(1, 1_000_000_000u128)); } } diff --git a/polkadot/runtime/polkadot/Cargo.toml b/polkadot/runtime/polkadot/Cargo.toml index bc45f60347..02840176e7 100644 --- a/polkadot/runtime/polkadot/Cargo.toml +++ b/polkadot/runtime/polkadot/Cargo.toml @@ -81,6 +81,7 @@ keyring = { package = "sp-keyring", git = "https://github.com/paritytech/substra sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } trie-db = "0.22.3" serde_json = "1.0.61" +separator = "0.4.1" [build-dependencies] substrate-wasm-builder = "3.0.0" diff --git a/polkadot/runtime/polkadot/src/lib.rs b/polkadot/runtime/polkadot/src/lib.rs index dbbf15e4f5..072dcd43e3 100644 --- a/polkadot/runtime/polkadot/src/lib.rs +++ b/polkadot/runtime/polkadot/src/lib.rs @@ -1335,3 +1335,60 @@ sp_api::impl_runtime_apis! { } } } + +#[cfg(test)] +mod test_fees { + use super::*; + use frame_support::weights::WeightToFeePolynomial; + use frame_support::storage::StorageValue; + use sp_runtime::FixedPointNumber; + use frame_support::weights::GetDispatchInfo; + use parity_scale_codec::Encode; + use pallet_transaction_payment::Multiplier; + use separator::Separatable; + + + #[test] + #[ignore] + fn block_cost() { + let max_block_weight = BlockWeights::get().max_block; + let raw_fee = WeightToFee::calc(&max_block_weight); + + println!( + "Full Block weight == {} // WeightToFee(full_block) == {} plank", + max_block_weight, + raw_fee.separated_string(), + ); + } + + #[test] + #[ignore] + fn transfer_cost_min_multiplier() { + let min_multiplier = runtime_common::MinimumMultiplier::get(); + let call = >::transfer_keep_alive(Default::default(), Default::default()); + let info = call.get_dispatch_info(); + // convert to outer call. + let call = Call::Balances(call); + let len = call.using_encoded(|e| e.len()) as u32; + + let mut ext = sp_io::TestExternalities::new_empty(); + let mut test_with_multiplier = |m| { + ext.execute_with(|| { + pallet_transaction_payment::NextFeeMultiplier::put(m); + let fee = TransactionPayment::compute_fee(len, &info, 0); + println!( + "weight = {:?} // multiplier = {:?} // full transfer fee = {:?}", + info.weight.separated_string(), + pallet_transaction_payment::NextFeeMultiplier::get(), + fee.separated_string(), + ); + }); + }; + + test_with_multiplier(min_multiplier); + test_with_multiplier(Multiplier::saturating_from_rational(1, 1u128)); + test_with_multiplier(Multiplier::saturating_from_rational(1, 1_000u128)); + test_with_multiplier(Multiplier::saturating_from_rational(1, 1_000_000u128)); + test_with_multiplier(Multiplier::saturating_from_rational(1, 1_000_000_000u128)); + } +}