Change a few fee/weight parameters (#2481)

* Change a few fee parameters

* Update runtime/common/src/lib.rs
This commit is contained in:
Kian Paimani
2021-02-23 13:34:29 +00:00
committed by GitHub
parent ece23588d2
commit 571651e326
5 changed files with 104 additions and 39 deletions
+1
View File
@@ -5724,6 +5724,7 @@ dependencies = [
"polkadot-primitives", "polkadot-primitives",
"polkadot-runtime-common", "polkadot-runtime-common",
"rustc-hex", "rustc-hex",
"separator",
"serde", "serde",
"serde_derive", "serde_derive",
"serde_json", "serde_json",
+28 -6
View File
@@ -54,9 +54,9 @@ pub type NegativeImbalance<T> = <pallet_balances::Module<T> as Currency<<T as fr
/// valid wasm module. /// valid wasm module.
pub const WASM_MAGIC: &[u8] = &[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]; pub const WASM_MAGIC: &[u8] = &[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00];
/// We assume that an on-initialize consumes 2.5% of the weight on average, hence a single extrinsic /// We assume that an on-initialize consumes 1% of the weight on average, hence a single extrinsic
/// will not be allowed to consume more than `AvailableBlockRatio - 2.5%`. /// will not be allowed to consume more than `AvailableBlockRatio - 1%`.
pub const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_perthousand(25); pub const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(1);
/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used /// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used
/// by Operational extrinsics. /// by Operational extrinsics.
const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
@@ -77,7 +77,7 @@ parameter_types! {
/// Minimum amount of the multiplier. This value cannot be too low. A test case should ensure /// 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. /// that combined with `AdjustmentVariable`, we can recover from the minimum.
/// See `multiplier_can_grow_from_zero`. /// See `multiplier_can_grow_from_zero`.
pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000_000u128); pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000u128);
/// Maximum length of block. Up to 5MB. /// Maximum length of block. Up to 5MB.
pub BlockLength: limits::BlockLength = pub BlockLength: limits::BlockLength =
limits::BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO); limits::BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
@@ -243,7 +243,7 @@ mod multiplier_tests {
type SS58Prefix = (); type SS58Prefix = ();
} }
fn run_with_system_weight<F>(w: Weight, assertions: F) where F: Fn() -> () { fn run_with_system_weight<F>(w: Weight, mut assertions: F) where F: FnMut() -> () {
let mut t: sp_io::TestExternalities = let mut t: sp_io::TestExternalities =
frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap().into(); frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap().into();
t.execute_with(|| { t.execute_with(|| {
@@ -258,10 +258,32 @@ mod multiplier_tests {
let target = TargetBlockFullness::get() * let target = TargetBlockFullness::get() *
BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap(); BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap();
// if the min is too small, then this will not change, and we are doomed forever. // 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, || { run_with_system_weight(target * 101 / 100, || {
let next = SlowAdjustingFeeUpdate::<Runtime>::convert(minimum_multiplier); let next = SlowAdjustingFeeUpdate::<Runtime>::convert(minimum_multiplier);
assert!(next > minimum_multiplier, "{:?} !>= {:?}", next, 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::<Runtime>::convert(multiplier);
// ensure that it is growing as well.
assert!(next > multiplier, "{:?} !>= {:?}", next, multiplier);
multiplier = next;
});
blocks += 1;
println!("block = {} multiplier {:?}", blocks, multiplier);
}
}
} }
+17 -33
View File
@@ -1377,39 +1377,23 @@ mod test_fees {
let len = call.using_encoded(|e| e.len()) as u32; let len = call.using_encoded(|e| e.len()) as u32;
let mut ext = sp_io::TestExternalities::new_empty(); let mut ext = sp_io::TestExternalities::new_empty();
ext.execute_with(|| { let mut test_with_multiplier = |m| {
pallet_transaction_payment::NextFeeMultiplier::put(min_multiplier); ext.execute_with(|| {
let fee = TransactionPayment::compute_fee(len, &info, 0); pallet_transaction_payment::NextFeeMultiplier::put(m);
println!( let fee = TransactionPayment::compute_fee(len, &info, 0);
"weight = {:?} // multiplier = {:?} // full transfer fee = {:?}", println!(
info.weight.separated_string(), "weight = {:?} // multiplier = {:?} // full transfer fee = {:?}",
pallet_transaction_payment::NextFeeMultiplier::get(), info.weight.separated_string(),
fee.separated_string(), pallet_transaction_payment::NextFeeMultiplier::get(),
); fee.separated_string(),
}); );
});
};
ext.execute_with(|| { test_with_multiplier(min_multiplier);
let mul = Multiplier::saturating_from_rational(1, 1000_000_000u128); test_with_multiplier(Multiplier::saturating_from_rational(1, 1u128));
pallet_transaction_payment::NextFeeMultiplier::put(mul); test_with_multiplier(Multiplier::saturating_from_rational(1, 1_000u128));
let fee = TransactionPayment::compute_fee(len, &info, 0); test_with_multiplier(Multiplier::saturating_from_rational(1, 1_000_000u128));
println!( test_with_multiplier(Multiplier::saturating_from_rational(1, 1_000_000_000u128));
"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(),
);
});
} }
} }
+1
View File
@@ -81,6 +81,7 @@ keyring = { package = "sp-keyring", git = "https://github.com/paritytech/substra
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" }
trie-db = "0.22.3" trie-db = "0.22.3"
serde_json = "1.0.61" serde_json = "1.0.61"
separator = "0.4.1"
[build-dependencies] [build-dependencies]
substrate-wasm-builder = "3.0.0" substrate-wasm-builder = "3.0.0"
+57
View File
@@ -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 = <pallet_balances::Call<Runtime>>::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));
}
}