Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "run to block" tools #7109

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions cumulus/pallets/dmp-queue/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
use super::{migration::*, mock::*};
use crate::*;

use frame_support::{
pallet_prelude::*,
traits::{OnFinalize, OnIdle, OnInitialize},
StorageNoopGuard,
};
use frame_support::{pallet_prelude::*, traits::OnIdle, StorageNoopGuard};

#[test]
fn migration_works() {
Expand Down Expand Up @@ -183,14 +179,12 @@ fn migration_too_long_ignored() {
}

fn run_to_block(n: u64) {
assert!(n > System::block_number(), "Cannot go back in time");

while System::block_number() < n {
AllPalletsWithSystem::on_finalize(System::block_number());
System::set_block_number(System::block_number() + 1);
AllPalletsWithSystem::on_initialize(System::block_number());
AllPalletsWithSystem::on_idle(System::block_number(), Weight::MAX);
}
System::run_to_block_with::<AllPalletsWithSystem>(
n,
frame_system::RunToBlockHooks::default().after_initialize(|bn| {
AllPalletsWithSystem::on_idle(bn, Weight::MAX);
}),
);
}

fn assert_only_event(e: Event<Runtime>) {
Expand Down
89 changes: 32 additions & 57 deletions polkadot/runtime/common/src/assigned_slots/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,47 +788,22 @@ mod tests {
t.into()
}

fn run_to_block(n: BlockNumber) {
while System::block_number() < n {
let mut block = System::block_number();
// on_finalize hooks
AssignedSlots::on_finalize(block);
Slots::on_finalize(block);
Parachains::on_finalize(block);
ParasShared::on_finalize(block);
Configuration::on_finalize(block);
Balances::on_finalize(block);
System::on_finalize(block);
// Set next block
System::set_block_number(block + 1);
block = System::block_number();
// on_initialize hooks
System::on_initialize(block);
Balances::on_initialize(block);
Configuration::on_initialize(block);
ParasShared::on_initialize(block);
Parachains::on_initialize(block);
Slots::on_initialize(block);
AssignedSlots::on_initialize(block);
}
}

#[test]
fn basic_setup_works() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);
assert_eq!(AssignedSlots::current_lease_period_index(), 0);
assert_eq!(Slots::deposit_held(1.into(), &1), 0);

run_to_block(3);
System::run_to_block::<AllPalletsWithSystem>(3);
assert_eq!(AssignedSlots::current_lease_period_index(), 1);
});
}

#[test]
fn assign_perm_slot_fails_for_unknown_para() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_noop!(
AssignedSlots::assign_perm_parachain_slot(
Expand All @@ -843,7 +818,7 @@ mod tests {
#[test]
fn assign_perm_slot_fails_for_invalid_origin() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_noop!(
AssignedSlots::assign_perm_parachain_slot(
Expand All @@ -858,7 +833,7 @@ mod tests {
#[test]
fn assign_perm_slot_fails_when_not_parathread() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_ok!(TestRegistrar::<Test>::register(
1,
Expand All @@ -881,7 +856,7 @@ mod tests {
#[test]
fn assign_perm_slot_fails_when_existing_lease() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_ok!(TestRegistrar::<Test>::register(
1,
Expand Down Expand Up @@ -920,7 +895,7 @@ mod tests {
#[test]
fn assign_perm_slot_fails_when_max_perm_slots_exceeded() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_ok!(TestRegistrar::<Test>::register(
1,
Expand Down Expand Up @@ -967,7 +942,7 @@ mod tests {
fn assign_perm_slot_succeeds_for_parathread() {
new_test_ext().execute_with(|| {
let mut block = 1;
run_to_block(block);
System::run_to_block::<AllPalletsWithSystem>(block);
assert_ok!(TestRegistrar::<Test>::register(
1,
ParaId::from(1_u32),
Expand Down Expand Up @@ -1000,7 +975,7 @@ mod tests {
assert_eq!(Slots::already_leased(ParaId::from(1_u32), 0, 2), true);

block += 1;
run_to_block(block);
System::run_to_block::<AllPalletsWithSystem>(block);
}

// Para lease ended, downgraded back to parathread (on-demand parachain)
Expand All @@ -1012,7 +987,7 @@ mod tests {
#[test]
fn assign_temp_slot_fails_for_unknown_para() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_noop!(
AssignedSlots::assign_temp_parachain_slot(
Expand All @@ -1028,7 +1003,7 @@ mod tests {
#[test]
fn assign_temp_slot_fails_for_invalid_origin() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_noop!(
AssignedSlots::assign_temp_parachain_slot(
Expand All @@ -1044,7 +1019,7 @@ mod tests {
#[test]
fn assign_temp_slot_fails_when_not_parathread() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_ok!(TestRegistrar::<Test>::register(
1,
Expand All @@ -1068,7 +1043,7 @@ mod tests {
#[test]
fn assign_temp_slot_fails_when_existing_lease() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_ok!(TestRegistrar::<Test>::register(
1,
Expand Down Expand Up @@ -1109,7 +1084,7 @@ mod tests {
#[test]
fn assign_temp_slot_fails_when_max_temp_slots_exceeded() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

// Register 6 paras & a temp slot for each
for n in 0..=5 {
Expand Down Expand Up @@ -1151,7 +1126,7 @@ mod tests {
fn assign_temp_slot_succeeds_for_single_parathread() {
new_test_ext().execute_with(|| {
let mut block = 1;
run_to_block(block);
System::run_to_block::<AllPalletsWithSystem>(block);
assert_ok!(TestRegistrar::<Test>::register(
1,
ParaId::from(1_u32),
Expand Down Expand Up @@ -1195,7 +1170,7 @@ mod tests {
assert_eq!(Slots::already_leased(ParaId::from(1_u32), 0, 1), true);

block += 1;
run_to_block(block);
System::run_to_block::<AllPalletsWithSystem>(block);
}

// Block 6
Expand All @@ -1210,7 +1185,7 @@ mod tests {

// Block 12
// Para should get a turn after TemporarySlotLeasePeriodLength * LeasePeriod blocks
run_to_block(12);
System::run_to_block::<AllPalletsWithSystem>(12);
println!("block #{}", block);
println!("lease period #{}", AssignedSlots::current_lease_period_index());
println!("lease {:?}", slots::Leases::<Test>::get(ParaId::from(1_u32)));
Expand All @@ -1225,7 +1200,7 @@ mod tests {
fn assign_temp_slot_succeeds_for_multiple_parathreads() {
new_test_ext().execute_with(|| {
// Block 1, Period 0
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

// Register 6 paras & a temp slot for each
// (3 slots in current lease period, 3 in the next one)
Expand All @@ -1251,7 +1226,7 @@ mod tests {
// Block 1-5, Period 0-1
for n in 1..=5 {
if n > 1 {
run_to_block(n);
System::run_to_block::<AllPalletsWithSystem>(n);
}
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(0)), true);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(1_u32)), false);
Expand All @@ -1264,7 +1239,7 @@ mod tests {

// Block 6-11, Period 2-3
for n in 6..=11 {
run_to_block(n);
System::run_to_block::<AllPalletsWithSystem>(n);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(0)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(1_u32)), true);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(2_u32)), false);
Expand All @@ -1276,7 +1251,7 @@ mod tests {

// Block 12-17, Period 4-5
for n in 12..=17 {
run_to_block(n);
System::run_to_block::<AllPalletsWithSystem>(n);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(0)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(1_u32)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(2_u32)), false);
Expand All @@ -1288,7 +1263,7 @@ mod tests {

// Block 18-23, Period 6-7
for n in 18..=23 {
run_to_block(n);
System::run_to_block::<AllPalletsWithSystem>(n);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(0)), true);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(1_u32)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(2_u32)), true);
Expand All @@ -1300,7 +1275,7 @@ mod tests {

// Block 24-29, Period 8-9
for n in 24..=29 {
run_to_block(n);
System::run_to_block::<AllPalletsWithSystem>(n);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(0)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(1_u32)), true);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(2_u32)), false);
Expand All @@ -1312,7 +1287,7 @@ mod tests {

// Block 30-35, Period 10-11
for n in 30..=35 {
run_to_block(n);
System::run_to_block::<AllPalletsWithSystem>(n);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(0)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(1_u32)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(2_u32)), false);
Expand All @@ -1327,7 +1302,7 @@ mod tests {
#[test]
fn unassign_slot_fails_for_unknown_para() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_noop!(
AssignedSlots::unassign_parachain_slot(RuntimeOrigin::root(), ParaId::from(1_u32),),
Expand All @@ -1339,7 +1314,7 @@ mod tests {
#[test]
fn unassign_slot_fails_for_invalid_origin() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_noop!(
AssignedSlots::assign_perm_parachain_slot(
Expand All @@ -1354,7 +1329,7 @@ mod tests {
#[test]
fn unassign_perm_slot_succeeds() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_ok!(TestRegistrar::<Test>::register(
1,
Expand Down Expand Up @@ -1386,7 +1361,7 @@ mod tests {
#[test]
fn unassign_temp_slot_succeeds() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_ok!(TestRegistrar::<Test>::register(
1,
Expand Down Expand Up @@ -1419,7 +1394,7 @@ mod tests {
#[test]
fn set_max_permanent_slots_fails_for_no_root_origin() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_noop!(
AssignedSlots::set_max_permanent_slots(RuntimeOrigin::signed(1), 5),
Expand All @@ -1430,7 +1405,7 @@ mod tests {
#[test]
fn set_max_permanent_slots_succeeds() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_eq!(MaxPermanentSlots::<Test>::get(), 2);
assert_ok!(AssignedSlots::set_max_permanent_slots(RuntimeOrigin::root(), 10),);
Expand All @@ -1441,7 +1416,7 @@ mod tests {
#[test]
fn set_max_temporary_slots_fails_for_no_root_origin() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_noop!(
AssignedSlots::set_max_temporary_slots(RuntimeOrigin::signed(1), 5),
Expand All @@ -1452,7 +1427,7 @@ mod tests {
#[test]
fn set_max_temporary_slots_succeeds() {
new_test_ext().execute_with(|| {
run_to_block(1);
System::run_to_block::<AllPalletsWithSystem>(1);

assert_eq!(MaxTemporarySlots::<Test>::get(), 6);
assert_ok!(AssignedSlots::set_max_temporary_slots(RuntimeOrigin::root(), 12),);
Expand Down
15 changes: 1 addition & 14 deletions polkadot/runtime/common/src/auctions/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
use super::*;
use crate::{auctions, mock::TestRegistrar};
use frame_support::{
assert_ok, derive_impl, ord_parameter_types, parameter_types,
traits::{EitherOfDiverse, OnFinalize, OnInitialize},
assert_ok, derive_impl, ord_parameter_types, parameter_types, traits::EitherOfDiverse,
};
use frame_system::{EnsureRoot, EnsureSignedBy};
use pallet_balances;
Expand Down Expand Up @@ -244,15 +243,3 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
});
ext
}

pub fn run_to_block(n: BlockNumber) {
while System::block_number() < n {
Auctions::on_finalize(System::block_number());
Balances::on_finalize(System::block_number());
System::on_finalize(System::block_number());
System::set_block_number(System::block_number() + 1);
System::on_initialize(System::block_number());
Balances::on_initialize(System::block_number());
Auctions::on_initialize(System::block_number());
}
}
Loading
Loading