Skip to content

Commit

Permalink
Exposed relaychain TREASURY_PALLET_ID constant (as a preparation fo…
Browse files Browse the repository at this point in the history
…r XcmFeeManager and paritytech/polkadot-sdk#1979) + moved tests to separate file
  • Loading branch information
bkontur committed Nov 8, 2023
1 parent 96f6566 commit 69a4396
Show file tree
Hide file tree
Showing 6 changed files with 494 additions and 472 deletions.
3 changes: 3 additions & 0 deletions relay/kusama/constants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ pub mod system_parachain {
pub const BRIDGE_HUB_ID: u32 = 1002;
}

/// Kusama Treasury pallet instance.
pub const TREASURY_PALLET_ID: u8 = 18;

#[cfg(test)]
mod tests {
use super::{
Expand Down
143 changes: 0 additions & 143 deletions relay/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2471,149 +2471,6 @@ sp_api::impl_runtime_apis! {
}
}

#[cfg(test)]
mod fees_tests {
use super::*;
use sp_runtime::assert_eq_error_rate;

#[test]
fn signed_deposit_is_sensible() {
// ensure this number does not change, or that it is checked after each change.
// a 1 MB solution should need around 0.16 KSM deposit
let deposit = SignedDepositBase::get() + (SignedDepositByte::get() * 1024 * 1024);
assert_eq_error_rate!(deposit, UNITS * 167 / 100, UNITS / 100);
}
}

#[cfg(test)]
mod multiplier_tests {
use super::*;
use frame_support::{dispatch::DispatchInfo, traits::OnFinalize};
use runtime_common::{MinimumMultiplier, TargetBlockFullness};
use separator::Separatable;
use sp_runtime::traits::Convert;

fn run_with_system_weight<F>(w: Weight, mut assertions: F)
where
F: FnMut() -> (),
{
let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::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.saturating_mul(101) / 100, || {
let next = SlowAdjustingFeeUpdate::<Runtime>::convert(minimum_multiplier);
assert!(next > minimum_multiplier, "{:?} !>= {:?}", next, minimum_multiplier);
})
}

#[test]
fn fast_unstake_estimate() {
use pallet_fast_unstake::WeightInfo;
let block_time = BlockWeights::get().max_block.ref_time() as f32;
let on_idle = weights::pallet_fast_unstake::WeightInfo::<Runtime>::on_idle_check(
1000,
<Runtime as pallet_fast_unstake::Config>::BatchSize::get(),
)
.ref_time() as f32;
println!("ratio of block weight for full batch fast-unstake {}", on_idle / block_time);
assert!(on_idle / block_time <= 0.5f32)
}

#[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 = BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap();
let mut blocks = 0;
let mut fees_paid = 0;

frame_system::Pallet::<Runtime>::set_block_consumed_resources(Weight::MAX, 0);
let info = DispatchInfo { weight: Weight::MAX, ..Default::default() };

let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap()
.into();
// set the minimum
t.execute_with(|| {
pallet_transaction_payment::NextFeeMultiplier::<Runtime>::set(MinimumMultiplier::get());
});

while multiplier <= Multiplier::from_u32(1) {
t.execute_with(|| {
// imagine this tx was called.
let fee = TransactionPayment::compute_fee(0, &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]
#[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.
let mut multiplier = Multiplier::from_u32(2);
let mut blocks = 0;

let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap()
.into();
// set the minimum
t.execute_with(|| {
pallet_transaction_payment::NextFeeMultiplier::<Runtime>::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;
}
}
}

#[cfg(all(test, feature = "try-runtime"))]
mod remote_tests {
use super::*;
Expand Down
149 changes: 149 additions & 0 deletions relay/kusama/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,152 @@ fn check_whitelist() {
// XcmPallet SafeXcmVersion
assert!(whitelist.contains("1405f2411d0af5a7ff397e7c9dc68d196323ae84c43568be0d1394d5d0d522c4"));
}

#[test]
fn check_treasury_pallet_id() {
assert_eq!(
<Treasury as frame_support::traits::PalletInfoAccess>::index() as u8,
kusama_runtime_constants::TREASURY_PALLET_ID
);
}

mod fees_tests {
use super::*;
use sp_runtime::assert_eq_error_rate;

#[test]
fn signed_deposit_is_sensible() {
// ensure this number does not change, or that it is checked after each change.
// a 1 MB solution should need around 0.16 KSM deposit
let deposit = SignedDepositBase::get() + (SignedDepositByte::get() * 1024 * 1024);
assert_eq_error_rate!(deposit, UNITS * 167 / 100, UNITS / 100);
}
}

mod multiplier_tests {
use super::*;
use frame_support::{dispatch::DispatchInfo, traits::OnFinalize};
use runtime_common::{MinimumMultiplier, TargetBlockFullness};
use separator::Separatable;
use sp_runtime::traits::Convert;

fn run_with_system_weight<F>(w: Weight, mut assertions: F)
where
F: FnMut() -> (),
{
let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::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.saturating_mul(101) / 100, || {
let next = SlowAdjustingFeeUpdate::<Runtime>::convert(minimum_multiplier);
assert!(next > minimum_multiplier, "{:?} !>= {:?}", next, minimum_multiplier);
})
}

#[test]
fn fast_unstake_estimate() {
use pallet_fast_unstake::WeightInfo;
let block_time = BlockWeights::get().max_block.ref_time() as f32;
let on_idle = weights::pallet_fast_unstake::WeightInfo::<Runtime>::on_idle_check(
1000,
<Runtime as pallet_fast_unstake::Config>::BatchSize::get(),
)
.ref_time() as f32;
println!("ratio of block weight for full batch fast-unstake {}", on_idle / block_time);
assert!(on_idle / block_time <= 0.5f32)
}

#[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 = BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap();
let mut blocks = 0;
let mut fees_paid = 0;

frame_system::Pallet::<Runtime>::set_block_consumed_resources(Weight::MAX, 0);
let info = DispatchInfo { weight: Weight::MAX, ..Default::default() };

let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap()
.into();
// set the minimum
t.execute_with(|| {
pallet_transaction_payment::NextFeeMultiplier::<Runtime>::set(MinimumMultiplier::get());
});

while multiplier <= Multiplier::from_u32(1) {
t.execute_with(|| {
// imagine this tx was called.
let fee = TransactionPayment::compute_fee(0, &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]
#[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.
let mut multiplier = Multiplier::from_u32(2);
let mut blocks = 0;

let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap()
.into();
// set the minimum
t.execute_with(|| {
pallet_transaction_payment::NextFeeMultiplier::<Runtime>::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;
}
}
}
3 changes: 3 additions & 0 deletions relay/polkadot/constants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ pub mod system_parachain {
pub const BRIDGE_HUB_ID: u32 = 1002;
}

/// Polkadot Treasury pallet instance.
pub const TREASURY_PALLET_ID: u8 = 19;

#[cfg(test)]
mod tests {
use super::{
Expand Down
Loading

0 comments on commit 69a4396

Please sign in to comment.