From f4f488144ef1c3d1246d1dba63a81147f1379a0d Mon Sep 17 00:00:00 2001 From: kianenigma Date: Mon, 27 Jun 2022 22:22:40 +0200 Subject: [PATCH 1/7] propose fix fees --- parachain/test-parachains/Cargo.toml | 2 +- runtime/common/src/lib.rs | 119 +-------------------------- runtime/kusama/Cargo.toml | 2 +- runtime/polkadot/Cargo.toml | 2 +- runtime/polkadot/src/lib.rs | 90 +++++++++++++++++++- runtime/test-runtime/Cargo.toml | 2 +- runtime/westend/Cargo.toml | 2 +- 7 files changed, 97 insertions(+), 122 deletions(-) diff --git a/parachain/test-parachains/Cargo.toml b/parachain/test-parachains/Cargo.toml index 6fe51fc0d3dc..63d3de210be5 100644 --- a/parachain/test-parachains/Cargo.toml +++ b/parachain/test-parachains/Cargo.toml @@ -6,7 +6,7 @@ description = "Integration tests using the test-parachains" edition = "2021" [dependencies] -tiny-keccak = "2.0.2" +tiny-keccak = { version = "2.0.2", features = ["keccak"] } parity-scale-codec = { version = "3.1.5", default-features = false, features = ["derive"] } adder = { package = "test-parachain-adder", path = "adder" } diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 0a6ca7b6e9c8..87b842ec1784 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -81,11 +81,13 @@ parameter_types! { pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25); /// The adjustment variable of the runtime. Higher values will cause `TargetBlockFullness` to /// change the fees more rapidly. - pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(3, 100_000); + // pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(3, 100_000); + pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(3, 1000); /// Minimum amount of the multiplier. This value cannot be too low. A test case should ensure /// that combined with `AdjustmentVariable`, we can recover from the minimum. /// See `multiplier_can_grow_from_zero`. - pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000u128); + // pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000u128); + pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000u128); /// Maximum length of block. Up to 5MB. pub BlockLength: limits::BlockLength = limits::BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO); @@ -244,116 +246,3 @@ macro_rules! prod_or_fast { } }; } - -#[cfg(test)] -mod multiplier_tests { - use super::*; - use frame_support::{ - parameter_types, - weights::{DispatchClass, Weight}, - }; - use sp_core::H256; - use sp_runtime::{ - testing::Header, - traits::{BlakeTwo256, Convert, IdentityLookup, One}, - Perbill, - }; - - type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; - type Block = frame_system::mocking::MockBlock; - - frame_support::construct_runtime!( - pub enum Runtime where - Block = Block, - NodeBlock = Block, - UncheckedExtrinsic = UncheckedExtrinsic, - { - System: frame_system::{Pallet, Call, Config, Storage, Event} - } - ); - - parameter_types! { - pub const BlockHashCount: u64 = 250; - pub const AvailableBlockRatio: Perbill = Perbill::one(); - pub BlockLength: frame_system::limits::BlockLength = - frame_system::limits::BlockLength::max(2 * 1024); - pub BlockWeights: frame_system::limits::BlockWeights = - frame_system::limits::BlockWeights::simple_max(1024); - } - - impl frame_system::Config for Runtime { - type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = BlockWeights; - type BlockLength = (); - type DbWeight = (); - type Origin = Origin; - type Index = u64; - type BlockNumber = u64; - type Call = Call; - type Hash = H256; - type Hashing = BlakeTwo256; - type AccountId = u64; - type Lookup = IdentityLookup; - type Header = Header; - type Event = Event; - type BlockHashCount = BlockHashCount; - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = (); - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; - } - - 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(|| { - System::set_block_consumed_resources(w, 0); - assertions() - }); - } - - #[test] - fn multiplier_can_grow_from_zero() { - let minimum_multiplier = MinimumMultiplier::get(); - 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/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/runtime/kusama/Cargo.toml b/runtime/kusama/Cargo.toml index 5c936764d7fb..17692ffbd05f 100644 --- a/runtime/kusama/Cargo.toml +++ b/runtime/kusama/Cargo.toml @@ -98,7 +98,7 @@ xcm-builder = { package = "xcm-builder", path = "../../xcm/xcm-builder", default [dev-dependencies] hex-literal = "0.3.4" -tiny-keccak = "2.0.2" +tiny-keccak = { version = "2.0.2", features = ["keccak"] } keyring = { package = "sp-keyring", git = "https://github.com/paritytech/substrate", branch = "master" } sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } separator = "0.4.1" diff --git a/runtime/polkadot/Cargo.toml b/runtime/polkadot/Cargo.toml index 4763d7f04283..ef88115655e3 100644 --- a/runtime/polkadot/Cargo.toml +++ b/runtime/polkadot/Cargo.toml @@ -91,7 +91,7 @@ xcm-builder = { package = "xcm-builder", path = "../../xcm/xcm-builder", default [dev-dependencies] hex-literal = "0.3.4" -tiny-keccak = "2.0.2" +tiny-keccak = { version = "2.0.2", features = ["keccak"] } keyring = { package = "sp-keyring", git = "https://github.com/paritytech/substrate", branch = "master" } sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } trie-db = "0.23.1" diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index 5cb94013c2fc..1d75ee63dc3a 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -2023,7 +2023,6 @@ mod test_fees { } #[test] - #[ignore] fn block_cost() { let max_block_weight = BlockWeights::get().max_block; let raw_fee = WeightToFee::weight_to_fee(&max_block_weight); @@ -2036,7 +2035,6 @@ mod test_fees { } #[test] - #[ignore] fn transfer_cost_min_multiplier() { let min_multiplier = MinimumMultiplier::get(); let call = pallet_balances::Call::::transfer_keep_alive { @@ -2151,3 +2149,91 @@ mod test { ); } } + +#[cfg(test)] +mod multiplier_tests { + use super::*; + use frame_support::{dispatch::GetDispatchInfo, traits::OnFinalize}; + use runtime_common::{MinimumMultiplier, TargetBlockFullness}; + use sp_runtime::traits::{Convert, One}; + + 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(|| { + System::set_block_consumed_resources(w, 0); + assertions() + }); + } + + #[test] + fn multiplier_can_grow_from_zero() { + let minimum_multiplier = MinimumMultiplier::get(); + 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/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] + 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 = BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap(); + let mut blocks = 0; + let mut fees_paid = 0; + + let call = frame_system::Call::::fill_block { + ratio: Perbill::from_rational( + block_weight, + BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap(), + ), + }; + println!("calling {:?}", call); + let info = call.get_dispatch_info(); + // convert to outer call. + let call = Call::System(call); + let len = call.using_encoded(|e| e.len()) as u32; + + let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::default() + .build_storage::() + .unwrap() + .into(); + // set the minimum + t.execute_with(|| { + pallet_transaction_payment::NextFeeMultiplier::::set(MinimumMultiplier::get()); + }); + + while multiplier <= Multiplier::one() { + t.execute_with(|| { + // imagine this tx was called. + let fee = TransactionPayment::compute_fee(len, &info, 0); + fees_paid += fee; + + // this will update the multiplier. + System::set_block_consumed_resources(block_weight, 0); + TransactionPayment::on_finalize(1); + let next = TransactionPayment::next_fee_multiplier(); + + assert!(next > multiplier, "{:?} !>= {:?}", next, multiplier); + multiplier = next; + + println!( + "block = {} / multiplier {:?} / fee = {:?} / fess so far {:?}", + blocks, multiplier, fee, fees_paid + ); + }); + blocks += 1; + } + } +} diff --git a/runtime/test-runtime/Cargo.toml b/runtime/test-runtime/Cargo.toml index b7b2c6ec2b10..fa3a69df7f4d 100644 --- a/runtime/test-runtime/Cargo.toml +++ b/runtime/test-runtime/Cargo.toml @@ -65,7 +65,7 @@ xcm = { path = "../../xcm", default-features = false } [dev-dependencies] hex-literal = "0.3.4" -tiny-keccak = "2.0.2" +tiny-keccak = { version = "2.0.2", features = ["keccak"] } keyring = { package = "sp-keyring", git = "https://github.com/paritytech/substrate", branch = "master" } sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } serde_json = "1.0.81" diff --git a/runtime/westend/Cargo.toml b/runtime/westend/Cargo.toml index 778483353ee3..3fbabea56a7c 100644 --- a/runtime/westend/Cargo.toml +++ b/runtime/westend/Cargo.toml @@ -94,7 +94,7 @@ xcm-builder = { package = "xcm-builder", path = "../../xcm/xcm-builder", default [dev-dependencies] hex-literal = "0.3.4" -tiny-keccak = "2.0.2" +tiny-keccak = { version = "2.0.2", features = ["keccak"] } keyring = { package = "sp-keyring", git = "https://github.com/paritytech/substrate", branch = "master" } sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } serde_json = "1.0.81" From 8eb79956d69e5eddbf1506166781299ce4233a3c Mon Sep 17 00:00:00 2001 From: kianenigma Date: Mon, 27 Jun 2022 23:22:12 +0200 Subject: [PATCH 2/7] add tests to kusama runtime as well --- runtime/common/src/lib.rs | 4 +- runtime/kusama/src/lib.rs | 124 ++++++++++++++++++++++++++++++++++++ runtime/polkadot/src/lib.rs | 42 +++++++++++- 3 files changed, 165 insertions(+), 5 deletions(-) diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 87b842ec1784..205334dacd27 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -81,13 +81,11 @@ parameter_types! { pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25); /// The adjustment variable of the runtime. Higher values will cause `TargetBlockFullness` to /// change the fees more rapidly. - // pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(3, 100_000); pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(3, 1000); /// Minimum amount of the multiplier. This value cannot be too low. A test case should ensure /// that combined with `AdjustmentVariable`, we can recover from the minimum. /// See `multiplier_can_grow_from_zero`. - // pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000u128); - pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000u128); + pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 10u128); /// Maximum length of block. Up to 5MB. pub BlockLength: limits::BlockLength = limits::BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO); diff --git a/runtime/kusama/src/lib.rs b/runtime/kusama/src/lib.rs index 93a6e6f836f0..fe557ae58fde 100644 --- a/runtime/kusama/src/lib.rs +++ b/runtime/kusama/src/lib.rs @@ -2171,3 +2171,127 @@ mod tests_fess { assert_eq_error_rate!(deposit, UNITS * 16 / 100, UNITS / 100); } } + +#[cfg(test)] +mod multiplier_tests { + use super::*; + use frame_support::{dispatch::GetDispatchInfo, traits::OnFinalize}; + use runtime_common::{MinimumMultiplier, TargetBlockFullness}; + use sp_runtime::traits::{Convert, One}; + use separator::Separatable; + + 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(|| { + System::set_block_consumed_resources(w, 0); + assertions() + }); + } + + #[test] + fn multiplier_can_grow_from_zero() { + let minimum_multiplier = MinimumMultiplier::get(); + 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/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] + 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 = BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap(); + let mut blocks = 0; + let mut fees_paid = 0; + + let call = frame_system::Call::::fill_block { + ratio: Perbill::from_rational( + block_weight, + BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap(), + ), + }; + println!("calling {:?}", call); + let info = call.get_dispatch_info(); + // convert to outer call. + let call = Call::System(call); + let len = call.using_encoded(|e| e.len()) as u32; + + let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::default() + .build_storage::() + .unwrap() + .into(); + // set the minimum + t.execute_with(|| { + pallet_transaction_payment::NextFeeMultiplier::::set(MinimumMultiplier::get()); + }); + + while multiplier <= Multiplier::from_u32(2) { + t.execute_with(|| { + // imagine this tx was called. + let fee = TransactionPayment::compute_fee(len, &info, 0); + fees_paid += fee; + + // this will update the multiplier. + System::set_block_consumed_resources(block_weight, 0); + TransactionPayment::on_finalize(1); + let next = TransactionPayment::next_fee_multiplier(); + + assert!(next > multiplier, "{:?} !>= {:?}", next, multiplier); + multiplier = next; + + println!( + "block = {} / multiplier {:?} / fee = {:?} / fess so far {:?}", + blocks, multiplier, fee.separated_string(), fees_paid.separated_string() + ); + }); + blocks += 1; + } + } + + #[test] + fn multiplier_cool_down_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 = Multiplier::from_u32(2); + let block_weight = 0; + let mut blocks = 0; + + let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::default() + .build_storage::() + .unwrap() + .into(); + // set the minimum + t.execute_with(|| { + pallet_transaction_payment::NextFeeMultiplier::::set(multiplier); + }); + + while multiplier > Multiplier::from_u32(0) { + t.execute_with(|| { + // this will update the multiplier. + TransactionPayment::on_finalize(1); + let next = TransactionPayment::next_fee_multiplier(); + + assert!(next < multiplier, "{:?} !>= {:?}", next, multiplier); + multiplier = next; + + println!( + "block = {} / multiplier {:?}", + blocks, multiplier + ); + }); + blocks += 1; + } + } +} diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index 1d75ee63dc3a..7866b1936f64 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -2062,6 +2062,8 @@ mod test_fees { test_with_multiplier(min_multiplier); test_with_multiplier(Multiplier::saturating_from_rational(1, 1u128)); + test_with_multiplier(Multiplier::saturating_from_rational(1, 1_0u128)); + test_with_multiplier(Multiplier::saturating_from_rational(1, 1_00u128)); 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)); @@ -2156,6 +2158,7 @@ mod multiplier_tests { use frame_support::{dispatch::GetDispatchInfo, traits::OnFinalize}; use runtime_common::{MinimumMultiplier, TargetBlockFullness}; use sp_runtime::traits::{Convert, One}; + use separator::Separatable; fn run_with_system_weight(w: Weight, mut assertions: F) where @@ -2214,7 +2217,7 @@ mod multiplier_tests { pallet_transaction_payment::NextFeeMultiplier::::set(MinimumMultiplier::get()); }); - while multiplier <= Multiplier::one() { + while multiplier <= Multiplier::from_u32(2) { t.execute_with(|| { // imagine this tx was called. let fee = TransactionPayment::compute_fee(len, &info, 0); @@ -2230,7 +2233,42 @@ mod multiplier_tests { println!( "block = {} / multiplier {:?} / fee = {:?} / fess so far {:?}", - blocks, multiplier, fee, fees_paid + blocks, multiplier, fee.separated_string(), fees_paid.separated_string() + ); + }); + blocks += 1; + } + } + + #[test] + fn multiplier_cool_down_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 = Multiplier::from_u32(2); + let block_weight = 0; + let mut blocks = 0; + + let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::default() + .build_storage::() + .unwrap() + .into(); + // set the minimum + t.execute_with(|| { + pallet_transaction_payment::NextFeeMultiplier::::set(multiplier); + }); + + while multiplier > Multiplier::from_u32(0) { + t.execute_with(|| { + // this will update the multiplier. + TransactionPayment::on_finalize(1); + let next = TransactionPayment::next_fee_multiplier(); + + assert!(next < multiplier, "{:?} !>= {:?}", next, multiplier); + multiplier = next; + + println!( + "block = {} / multiplier {:?}", + blocks, multiplier ); }); blocks += 1; From c0c5bbce8954f516acecda3a9eb959b8dbeabd89 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Tue, 28 Jun 2022 12:24:09 +0200 Subject: [PATCH 3/7] better tests --- runtime/common/src/lib.rs | 2 +- runtime/polkadot/src/lib.rs | 80 ++++++++++++++++++++++++------------- 2 files changed, 54 insertions(+), 28 deletions(-) diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 205334dacd27..41277f230598 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -81,7 +81,7 @@ parameter_types! { pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25); /// The adjustment variable of the runtime. Higher values will cause `TargetBlockFullness` to /// change the fees more rapidly. - pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(3, 1000); + pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(3, 10_000); /// Minimum amount of the multiplier. This value cannot be too low. A test case should ensure /// that combined with `AdjustmentVariable`, we can recover from the minimum. /// See `multiplier_can_grow_from_zero`. diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index 7866b1936f64..9a4973861ac9 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -1998,11 +1998,11 @@ sp_api::impl_runtime_apis! { mod test_fees { use super::*; use frame_support::weights::{GetDispatchInfo, WeightToFee as WeightToFeeT}; - use keyring::Sr25519Keyring::Charlie; + use keyring::Sr25519Keyring::{Alice, Charlie}; use pallet_transaction_payment::Multiplier; use runtime_common::MinimumMultiplier; use separator::Separatable; - use sp_runtime::{assert_eq_error_rate, FixedPointNumber}; + use sp_runtime::{assert_eq_error_rate, FixedPointNumber, MultiAddress, MultiSignature}; #[test] fn payout_weight_portion() { @@ -2027,11 +2027,18 @@ mod test_fees { let max_block_weight = BlockWeights::get().max_block; let raw_fee = WeightToFee::weight_to_fee(&max_block_weight); - println!( - "Full Block weight == {} // WeightToFee(full_block) == {} plank", - max_block_weight, - raw_fee.separated_string(), - ); + let fee_with_multiplier = |m: Multiplier| { + println!( + "Full Block weight == {} // multiplier: {:?} // WeightToFee(full_block) == {} plank", + max_block_weight, + m, + m.saturating_mul_int(raw_fee).separated_string(), + ); + }; + fee_with_multiplier(MinimumMultiplier::get()); + fee_with_multiplier(Multiplier::from_rational(1, 2)); + fee_with_multiplier(Multiplier::from_u32(1)); + fee_with_multiplier(Multiplier::from_u32(2)); } #[test] @@ -2042,31 +2049,51 @@ mod test_fees { value: Default::default(), }; let info = call.get_dispatch_info(); + println!("call = {:?} / info = {:?}", call, info); // convert to outer call. let call = Call::Balances(call); - let len = call.using_encoded(|e| e.len()) as u32; + let extra: SignedExtra = ( + frame_system::CheckNonZeroSender::::new(), + frame_system::CheckSpecVersion::::new(), + frame_system::CheckTxVersion::::new(), + frame_system::CheckGenesis::::new(), + frame_system::CheckMortality::::from(generic::Era::immortal()), + frame_system::CheckNonce::::from(1), + frame_system::CheckWeight::::new(), + pallet_transaction_payment::ChargeTransactionPayment::::from(0), + claims::PrevalidateAttests::::new(), + ); + let uxt = UncheckedExtrinsic { + function: call, + signature: Some(( + MultiAddress::Id(Alice.to_account_id()), + MultiSignature::Sr25519(Alice.sign(b"foo")), + extra, + )), + }; + let len = uxt.encoded_size(); let mut ext = sp_io::TestExternalities::new_empty(); - let mut test_with_multiplier = |m| { + let mut test_with_multiplier = |m: Multiplier| { ext.execute_with(|| { pallet_transaction_payment::NextFeeMultiplier::::put(m); - let fee = TransactionPayment::compute_fee(len, &info, 0); + let fee = TransactionPayment::query_fee_details(uxt.clone(), len as u32); println!( - "weight = {:?} // multiplier = {:?} // full transfer fee = {:?}", - info.weight.separated_string(), + "multiplier = {:?} // fee details = {:?} // final fee = {:?}", pallet_transaction_payment::NextFeeMultiplier::::get(), - fee.separated_string(), + fee, + fee.final_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_0u128)); - test_with_multiplier(Multiplier::saturating_from_rational(1, 1_00u128)); - 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)); + test_with_multiplier(Multiplier::saturating_from_rational(1u128, 1u128)); + test_with_multiplier(Multiplier::saturating_from_rational(1u128, 1_0u128)); + test_with_multiplier(Multiplier::saturating_from_rational(1u128, 1_00u128)); + test_with_multiplier(Multiplier::saturating_from_rational(1u128, 1_000u128)); + test_with_multiplier(Multiplier::saturating_from_rational(1u128, 1_000_000u128)); + test_with_multiplier(Multiplier::saturating_from_rational(1u128, 1_000_000_000u128)); } #[test] @@ -2157,8 +2184,8 @@ mod multiplier_tests { use super::*; use frame_support::{dispatch::GetDispatchInfo, traits::OnFinalize}; use runtime_common::{MinimumMultiplier, TargetBlockFullness}; - use sp_runtime::traits::{Convert, One}; use separator::Separatable; + use sp_runtime::traits::Convert; fn run_with_system_weight(w: Weight, mut assertions: F) where @@ -2217,7 +2244,7 @@ mod multiplier_tests { pallet_transaction_payment::NextFeeMultiplier::::set(MinimumMultiplier::get()); }); - while multiplier <= Multiplier::from_u32(2) { + while multiplier <= Multiplier::from_u32(1) { t.execute_with(|| { // imagine this tx was called. let fee = TransactionPayment::compute_fee(len, &info, 0); @@ -2233,7 +2260,10 @@ mod multiplier_tests { println!( "block = {} / multiplier {:?} / fee = {:?} / fess so far {:?}", - blocks, multiplier, fee.separated_string(), fees_paid.separated_string() + blocks, + multiplier, + fee.separated_string(), + fees_paid.separated_string() ); }); blocks += 1; @@ -2245,7 +2275,6 @@ mod multiplier_tests { // 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 = Multiplier::from_u32(2); - let block_weight = 0; let mut blocks = 0; let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::default() @@ -2266,10 +2295,7 @@ mod multiplier_tests { assert!(next < multiplier, "{:?} !>= {:?}", next, multiplier); multiplier = next; - println!( - "block = {} / multiplier {:?}", - blocks, multiplier - ); + println!("block = {} / multiplier {:?}", blocks, multiplier); }); blocks += 1; } From 3984eb160b4192d0a5425e5a3554648734c1a89e Mon Sep 17 00:00:00 2001 From: kianenigma Date: Tue, 28 Jun 2022 17:56:32 +0200 Subject: [PATCH 4/7] last change --- runtime/kusama/src/lib.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/runtime/kusama/src/lib.rs b/runtime/kusama/src/lib.rs index fe557ae58fde..9c3abc10c4d1 100644 --- a/runtime/kusama/src/lib.rs +++ b/runtime/kusama/src/lib.rs @@ -2177,8 +2177,8 @@ mod multiplier_tests { use super::*; use frame_support::{dispatch::GetDispatchInfo, traits::OnFinalize}; use runtime_common::{MinimumMultiplier, TargetBlockFullness}; - use sp_runtime::traits::{Convert, One}; use separator::Separatable; + use sp_runtime::traits::Convert; fn run_with_system_weight(w: Weight, mut assertions: F) where @@ -2237,7 +2237,7 @@ mod multiplier_tests { pallet_transaction_payment::NextFeeMultiplier::::set(MinimumMultiplier::get()); }); - while multiplier <= Multiplier::from_u32(2) { + while multiplier <= Multiplier::from_u32(1) { t.execute_with(|| { // imagine this tx was called. let fee = TransactionPayment::compute_fee(len, &info, 0); @@ -2253,7 +2253,10 @@ mod multiplier_tests { println!( "block = {} / multiplier {:?} / fee = {:?} / fess so far {:?}", - blocks, multiplier, fee.separated_string(), fees_paid.separated_string() + blocks, + multiplier, + fee.separated_string(), + fees_paid.separated_string() ); }); blocks += 1; @@ -2265,7 +2268,6 @@ mod multiplier_tests { // 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 = Multiplier::from_u32(2); - let block_weight = 0; let mut blocks = 0; let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::default() @@ -2286,10 +2288,7 @@ mod multiplier_tests { assert!(next < multiplier, "{:?} !>= {:?}", next, multiplier); multiplier = next; - println!( - "block = {} / multiplier {:?}", - blocks, multiplier - ); + println!("block = {} / multiplier {:?}", blocks, multiplier); }); blocks += 1; } From c1f67ee9a019c417b7d0a5c826635d1c8e052f20 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Tue, 28 Jun 2022 21:59:09 +0200 Subject: [PATCH 5/7] last update --- runtime/common/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 41277f230598..73c3ae0c5f39 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -81,7 +81,7 @@ parameter_types! { pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25); /// The adjustment variable of the runtime. Higher values will cause `TargetBlockFullness` to /// change the fees more rapidly. - pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(3, 10_000); + pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(75, 1000_000); /// Minimum amount of the multiplier. This value cannot be too low. A test case should ensure /// that combined with `AdjustmentVariable`, we can recover from the minimum. /// See `multiplier_can_grow_from_zero`. From 929fec6e61904e97d919b83a1cca534f22c7eed5 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Wed, 29 Jun 2022 16:33:32 +0200 Subject: [PATCH 6/7] Fix test --- runtime/kusama/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/runtime/kusama/src/lib.rs b/runtime/kusama/src/lib.rs index 9c3abc10c4d1..98b217352429 100644 --- a/runtime/kusama/src/lib.rs +++ b/runtime/kusama/src/lib.rs @@ -2208,6 +2208,7 @@ mod multiplier_tests { } #[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. @@ -2264,6 +2265,7 @@ mod multiplier_tests { } #[test] + #[ignore] fn multiplier_cool_down_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. From 7d3789662a891ab5c56ba8385d024af56be8c334 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Wed, 29 Jun 2022 17:07:43 +0200 Subject: [PATCH 7/7] ignore tests again --- runtime/polkadot/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index 9a4973861ac9..37c0778e680e 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -2215,6 +2215,7 @@ mod multiplier_tests { } #[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. @@ -2271,6 +2272,7 @@ mod multiplier_tests { } #[test] + #[ignore] fn multiplier_cool_down_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.