Skip to content

Commit bc44314

Browse files
committed
Add RunToBlockHooks
Signed-off-by: Xavier Lau <x@acg.box>
1 parent 2f17958 commit bc44314

30 files changed

Lines changed: 660 additions & 720 deletions

File tree

cumulus/pallets/dmp-queue/src/tests.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@
2121
use super::{migration::*, mock::*};
2222
use crate::*;
2323

24-
use frame_support::{
25-
pallet_prelude::*,
26-
traits::{OnFinalize, OnIdle, OnInitialize},
27-
StorageNoopGuard,
28-
};
24+
use frame_support::{pallet_prelude::*, traits::OnIdle, StorageNoopGuard};
2925

3026
#[test]
3127
fn migration_works() {
@@ -183,14 +179,12 @@ fn migration_too_long_ignored() {
183179
}
184180

185181
fn run_to_block(n: u64) {
186-
assert!(n > System::block_number(), "Cannot go back in time");
187-
188-
while System::block_number() < n {
189-
AllPalletsWithSystem::on_finalize(System::block_number());
190-
System::set_block_number(System::block_number() + 1);
191-
AllPalletsWithSystem::on_initialize(System::block_number());
192-
AllPalletsWithSystem::on_idle(System::block_number(), Weight::MAX);
193-
}
182+
System::run_to_block_with::<AllPalletsWithSystem>(
183+
n,
184+
frame_system::RunToBlockHooks::default().after_initialize(|bn| {
185+
AllPalletsWithSystem::on_idle(bn, Weight::MAX);
186+
}),
187+
);
194188
}
195189

196190
fn assert_only_event(e: Event<Runtime>) {

polkadot/runtime/common/src/assigned_slots/mod.rs

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -788,47 +788,22 @@ mod tests {
788788
t.into()
789789
}
790790

791-
fn run_to_block(n: BlockNumber) {
792-
while System::block_number() < n {
793-
let mut block = System::block_number();
794-
// on_finalize hooks
795-
AssignedSlots::on_finalize(block);
796-
Slots::on_finalize(block);
797-
Parachains::on_finalize(block);
798-
ParasShared::on_finalize(block);
799-
Configuration::on_finalize(block);
800-
Balances::on_finalize(block);
801-
System::on_finalize(block);
802-
// Set next block
803-
System::set_block_number(block + 1);
804-
block = System::block_number();
805-
// on_initialize hooks
806-
System::on_initialize(block);
807-
Balances::on_initialize(block);
808-
Configuration::on_initialize(block);
809-
ParasShared::on_initialize(block);
810-
Parachains::on_initialize(block);
811-
Slots::on_initialize(block);
812-
AssignedSlots::on_initialize(block);
813-
}
814-
}
815-
816791
#[test]
817792
fn basic_setup_works() {
818793
new_test_ext().execute_with(|| {
819-
run_to_block(1);
794+
System::run_to_block::<AllPalletsWithSystem>(1);
820795
assert_eq!(AssignedSlots::current_lease_period_index(), 0);
821796
assert_eq!(Slots::deposit_held(1.into(), &1), 0);
822797

823-
run_to_block(3);
798+
System::run_to_block::<AllPalletsWithSystem>(3);
824799
assert_eq!(AssignedSlots::current_lease_period_index(), 1);
825800
});
826801
}
827802

828803
#[test]
829804
fn assign_perm_slot_fails_for_unknown_para() {
830805
new_test_ext().execute_with(|| {
831-
run_to_block(1);
806+
System::run_to_block::<AllPalletsWithSystem>(1);
832807

833808
assert_noop!(
834809
AssignedSlots::assign_perm_parachain_slot(
@@ -843,7 +818,7 @@ mod tests {
843818
#[test]
844819
fn assign_perm_slot_fails_for_invalid_origin() {
845820
new_test_ext().execute_with(|| {
846-
run_to_block(1);
821+
System::run_to_block::<AllPalletsWithSystem>(1);
847822

848823
assert_noop!(
849824
AssignedSlots::assign_perm_parachain_slot(
@@ -858,7 +833,7 @@ mod tests {
858833
#[test]
859834
fn assign_perm_slot_fails_when_not_parathread() {
860835
new_test_ext().execute_with(|| {
861-
run_to_block(1);
836+
System::run_to_block::<AllPalletsWithSystem>(1);
862837

863838
assert_ok!(TestRegistrar::<Test>::register(
864839
1,
@@ -881,7 +856,7 @@ mod tests {
881856
#[test]
882857
fn assign_perm_slot_fails_when_existing_lease() {
883858
new_test_ext().execute_with(|| {
884-
run_to_block(1);
859+
System::run_to_block::<AllPalletsWithSystem>(1);
885860

886861
assert_ok!(TestRegistrar::<Test>::register(
887862
1,
@@ -920,7 +895,7 @@ mod tests {
920895
#[test]
921896
fn assign_perm_slot_fails_when_max_perm_slots_exceeded() {
922897
new_test_ext().execute_with(|| {
923-
run_to_block(1);
898+
System::run_to_block::<AllPalletsWithSystem>(1);
924899

925900
assert_ok!(TestRegistrar::<Test>::register(
926901
1,
@@ -967,7 +942,7 @@ mod tests {
967942
fn assign_perm_slot_succeeds_for_parathread() {
968943
new_test_ext().execute_with(|| {
969944
let mut block = 1;
970-
run_to_block(block);
945+
System::run_to_block::<AllPalletsWithSystem>(block);
971946
assert_ok!(TestRegistrar::<Test>::register(
972947
1,
973948
ParaId::from(1_u32),
@@ -1000,7 +975,7 @@ mod tests {
1000975
assert_eq!(Slots::already_leased(ParaId::from(1_u32), 0, 2), true);
1001976

1002977
block += 1;
1003-
run_to_block(block);
978+
System::run_to_block::<AllPalletsWithSystem>(block);
1004979
}
1005980

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

1017992
assert_noop!(
1018993
AssignedSlots::assign_temp_parachain_slot(
@@ -1028,7 +1003,7 @@ mod tests {
10281003
#[test]
10291004
fn assign_temp_slot_fails_for_invalid_origin() {
10301005
new_test_ext().execute_with(|| {
1031-
run_to_block(1);
1006+
System::run_to_block::<AllPalletsWithSystem>(1);
10321007

10331008
assert_noop!(
10341009
AssignedSlots::assign_temp_parachain_slot(
@@ -1044,7 +1019,7 @@ mod tests {
10441019
#[test]
10451020
fn assign_temp_slot_fails_when_not_parathread() {
10461021
new_test_ext().execute_with(|| {
1047-
run_to_block(1);
1022+
System::run_to_block::<AllPalletsWithSystem>(1);
10481023

10491024
assert_ok!(TestRegistrar::<Test>::register(
10501025
1,
@@ -1068,7 +1043,7 @@ mod tests {
10681043
#[test]
10691044
fn assign_temp_slot_fails_when_existing_lease() {
10701045
new_test_ext().execute_with(|| {
1071-
run_to_block(1);
1046+
System::run_to_block::<AllPalletsWithSystem>(1);
10721047

10731048
assert_ok!(TestRegistrar::<Test>::register(
10741049
1,
@@ -1109,7 +1084,7 @@ mod tests {
11091084
#[test]
11101085
fn assign_temp_slot_fails_when_max_temp_slots_exceeded() {
11111086
new_test_ext().execute_with(|| {
1112-
run_to_block(1);
1087+
System::run_to_block::<AllPalletsWithSystem>(1);
11131088

11141089
// Register 6 paras & a temp slot for each
11151090
for n in 0..=5 {
@@ -1151,7 +1126,7 @@ mod tests {
11511126
fn assign_temp_slot_succeeds_for_single_parathread() {
11521127
new_test_ext().execute_with(|| {
11531128
let mut block = 1;
1154-
run_to_block(block);
1129+
System::run_to_block::<AllPalletsWithSystem>(block);
11551130
assert_ok!(TestRegistrar::<Test>::register(
11561131
1,
11571132
ParaId::from(1_u32),
@@ -1195,7 +1170,7 @@ mod tests {
11951170
assert_eq!(Slots::already_leased(ParaId::from(1_u32), 0, 1), true);
11961171

11971172
block += 1;
1198-
run_to_block(block);
1173+
System::run_to_block::<AllPalletsWithSystem>(block);
11991174
}
12001175

12011176
// Block 6
@@ -1210,7 +1185,7 @@ mod tests {
12101185

12111186
// Block 12
12121187
// Para should get a turn after TemporarySlotLeasePeriodLength * LeasePeriod blocks
1213-
run_to_block(12);
1188+
System::run_to_block::<AllPalletsWithSystem>(12);
12141189
println!("block #{}", block);
12151190
println!("lease period #{}", AssignedSlots::current_lease_period_index());
12161191
println!("lease {:?}", slots::Leases::<Test>::get(ParaId::from(1_u32)));
@@ -1225,7 +1200,7 @@ mod tests {
12251200
fn assign_temp_slot_succeeds_for_multiple_parathreads() {
12261201
new_test_ext().execute_with(|| {
12271202
// Block 1, Period 0
1228-
run_to_block(1);
1203+
System::run_to_block::<AllPalletsWithSystem>(1);
12291204

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

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

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

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

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

13131288
// Block 30-35, Period 10-11
13141289
for n in 30..=35 {
1315-
run_to_block(n);
1290+
System::run_to_block::<AllPalletsWithSystem>(n);
13161291
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(0)), false);
13171292
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(1_u32)), false);
13181293
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(2_u32)), false);
@@ -1327,7 +1302,7 @@ mod tests {
13271302
#[test]
13281303
fn unassign_slot_fails_for_unknown_para() {
13291304
new_test_ext().execute_with(|| {
1330-
run_to_block(1);
1305+
System::run_to_block::<AllPalletsWithSystem>(1);
13311306

13321307
assert_noop!(
13331308
AssignedSlots::unassign_parachain_slot(RuntimeOrigin::root(), ParaId::from(1_u32),),
@@ -1339,7 +1314,7 @@ mod tests {
13391314
#[test]
13401315
fn unassign_slot_fails_for_invalid_origin() {
13411316
new_test_ext().execute_with(|| {
1342-
run_to_block(1);
1317+
System::run_to_block::<AllPalletsWithSystem>(1);
13431318

13441319
assert_noop!(
13451320
AssignedSlots::assign_perm_parachain_slot(
@@ -1354,7 +1329,7 @@ mod tests {
13541329
#[test]
13551330
fn unassign_perm_slot_succeeds() {
13561331
new_test_ext().execute_with(|| {
1357-
run_to_block(1);
1332+
System::run_to_block::<AllPalletsWithSystem>(1);
13581333

13591334
assert_ok!(TestRegistrar::<Test>::register(
13601335
1,
@@ -1386,7 +1361,7 @@ mod tests {
13861361
#[test]
13871362
fn unassign_temp_slot_succeeds() {
13881363
new_test_ext().execute_with(|| {
1389-
run_to_block(1);
1364+
System::run_to_block::<AllPalletsWithSystem>(1);
13901365

13911366
assert_ok!(TestRegistrar::<Test>::register(
13921367
1,
@@ -1419,7 +1394,7 @@ mod tests {
14191394
#[test]
14201395
fn set_max_permanent_slots_fails_for_no_root_origin() {
14211396
new_test_ext().execute_with(|| {
1422-
run_to_block(1);
1397+
System::run_to_block::<AllPalletsWithSystem>(1);
14231398

14241399
assert_noop!(
14251400
AssignedSlots::set_max_permanent_slots(RuntimeOrigin::signed(1), 5),
@@ -1430,7 +1405,7 @@ mod tests {
14301405
#[test]
14311406
fn set_max_permanent_slots_succeeds() {
14321407
new_test_ext().execute_with(|| {
1433-
run_to_block(1);
1408+
System::run_to_block::<AllPalletsWithSystem>(1);
14341409

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

14461421
assert_noop!(
14471422
AssignedSlots::set_max_temporary_slots(RuntimeOrigin::signed(1), 5),
@@ -1452,7 +1427,7 @@ mod tests {
14521427
#[test]
14531428
fn set_max_temporary_slots_succeeds() {
14541429
new_test_ext().execute_with(|| {
1455-
run_to_block(1);
1430+
System::run_to_block::<AllPalletsWithSystem>(1);
14561431

14571432
assert_eq!(MaxTemporarySlots::<Test>::get(), 6);
14581433
assert_ok!(AssignedSlots::set_max_temporary_slots(RuntimeOrigin::root(), 12),);

polkadot/runtime/common/src/auctions/mock.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
use super::*;
2121
use crate::{auctions, mock::TestRegistrar};
2222
use frame_support::{
23-
assert_ok, derive_impl, ord_parameter_types, parameter_types,
24-
traits::{EitherOfDiverse, OnFinalize, OnInitialize},
23+
assert_ok, derive_impl, ord_parameter_types, parameter_types, traits::EitherOfDiverse,
2524
};
2625
use frame_system::{EnsureRoot, EnsureSignedBy};
2726
use pallet_balances;
@@ -244,15 +243,3 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
244243
});
245244
ext
246245
}
247-
248-
pub fn run_to_block(n: BlockNumber) {
249-
while System::block_number() < n {
250-
Auctions::on_finalize(System::block_number());
251-
Balances::on_finalize(System::block_number());
252-
System::on_finalize(System::block_number());
253-
System::set_block_number(System::block_number() + 1);
254-
System::on_initialize(System::block_number());
255-
Balances::on_initialize(System::block_number());
256-
Auctions::on_initialize(System::block_number());
257-
}
258-
}

0 commit comments

Comments
 (0)