|
| 1 | +#![cfg(all(test, not(feature = "runtime-benchmarks")))] |
| 2 | + |
| 3 | +use codec::Decode; |
| 4 | +use frame_support::traits::OnRuntimeUpgrade; |
| 5 | +use frame_system::ensure_signed; |
| 6 | +use native_api::Api; |
| 7 | +use pallet_migrations::WeightInfo as _; |
| 8 | +use sp_core::Hasher; |
| 9 | +use sxt_core::indexing::{BatchId, SubmittersByScope}; |
| 10 | +use sxt_core::tables::QuorumScope; |
| 11 | + |
| 12 | +use crate::migrations::v1; |
| 13 | +use crate::mock::{ |
| 14 | + new_test_ext, |
| 15 | + run_to_block, |
| 16 | + AllPalletsWithSystem, |
| 17 | + MigratorServiceWeight, |
| 18 | + RuntimeOrigin, |
| 19 | + System, |
| 20 | + Test, |
| 21 | +}; |
| 22 | +use crate::weights::WeightInfo as _; |
| 23 | + |
| 24 | +fn submissions_from_seed( |
| 25 | + seed: u64, |
| 26 | +) -> ( |
| 27 | + BatchId, |
| 28 | + <Test as frame_system::Config>::Hash, |
| 29 | + SubmittersByScope<<Test as frame_system::Config>::AccountId>, |
| 30 | +) { |
| 31 | + let batch_id = BatchId::try_from(seed.to_le_bytes().to_vec()).unwrap(); |
| 32 | + let data_hash = <<Test as frame_system::Config>::Hashing as Hasher>::hash(&seed.to_le_bytes()); |
| 33 | + |
| 34 | + let num_privileged_submissions = seed % 32; |
| 35 | + |
| 36 | + // this math is chosen to |
| 37 | + // - have some variety with num_privileged_submissions |
| 38 | + // - always have at least 1 public submission (simplifies test logic) |
| 39 | + let num_public_submissions = ((seed * 2) % 31) + 1; |
| 40 | + |
| 41 | + let privileged_submitters_by_scope = (0..num_privileged_submissions) |
| 42 | + .map(|submitter_seed_index| { |
| 43 | + let submitter_index = seed * 32 + submitter_seed_index; |
| 44 | + ensure_signed(RuntimeOrigin::signed(submitter_index)).unwrap() |
| 45 | + }) |
| 46 | + .fold(SubmittersByScope::default(), |acc, submitter| { |
| 47 | + acc.with_submitter(submitter, &QuorumScope::Privileged) |
| 48 | + .unwrap() |
| 49 | + }); |
| 50 | + let submitters_by_scope = (0..num_public_submissions) |
| 51 | + .map(|submitter_seed_index| { |
| 52 | + let submitter_index = seed * 32 + submitter_seed_index; |
| 53 | + ensure_signed(RuntimeOrigin::signed(submitter_index)).unwrap() |
| 54 | + }) |
| 55 | + .fold(privileged_submitters_by_scope, |acc, submitter| { |
| 56 | + acc.with_submitter(submitter, &QuorumScope::Public).unwrap() |
| 57 | + }); |
| 58 | + |
| 59 | + (batch_id, data_hash, submitters_by_scope) |
| 60 | +} |
| 61 | + |
| 62 | +#[test] |
| 63 | +fn lazy_migration_works() { |
| 64 | + new_test_ext().execute_with(|| { |
| 65 | + (0..64u64) |
| 66 | + .map(submissions_from_seed) |
| 67 | + .for_each(|(batch_id, data_hash, submitters)| { |
| 68 | + submitters |
| 69 | + .iter_scope(&QuorumScope::Public) |
| 70 | + .for_each(|submitter| { |
| 71 | + crate::SubmissionsV1::<Test, Api>::insert( |
| 72 | + (&batch_id, QuorumScope::Public, &submitter), |
| 73 | + &data_hash, |
| 74 | + ); |
| 75 | + }); |
| 76 | + submitters |
| 77 | + .iter_scope(&QuorumScope::Privileged) |
| 78 | + .for_each(|submitter| { |
| 79 | + crate::SubmissionsV1::<Test, Api>::insert( |
| 80 | + (&batch_id, QuorumScope::Privileged, &submitter), |
| 81 | + &data_hash, |
| 82 | + ); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + // Give it enough weight do do exactly 8 iterations: |
| 87 | + let limit = <Test as pallet_migrations::Config>::WeightInfo::progress_mbms_none() |
| 88 | + + pallet_migrations::Pallet::<Test>::exec_migration_max_weight() |
| 89 | + + crate::weights::SubstrateWeight::<Test>::migration_v1_v2_step() * 8; |
| 90 | + MigratorServiceWeight::set(&limit); |
| 91 | + |
| 92 | + System::set_block_number(1); |
| 93 | + AllPalletsWithSystem::on_runtime_upgrade(); // onboard MBMs |
| 94 | + |
| 95 | + // check migration progress across many blocks |
| 96 | + let mut last_num_migrated = 0; |
| 97 | + for block in 2..=10 { |
| 98 | + run_to_block(block); |
| 99 | + |
| 100 | + let num_migrated = (0..64) |
| 101 | + .map(submissions_from_seed) |
| 102 | + .map(|(seeded_batch_id, _, _)| { |
| 103 | + if crate::BatchQueue::<Test, Api>::iter() |
| 104 | + .any(|(_, batch_id)| seeded_batch_id == batch_id) |
| 105 | + { |
| 106 | + 1usize |
| 107 | + } else { |
| 108 | + 0usize |
| 109 | + } |
| 110 | + }) |
| 111 | + .sum(); |
| 112 | + |
| 113 | + // part of the first block of migration is completing lazy migration v1 (noop), but |
| 114 | + // this costs some weight so we cannot migrate 8 batches in the first block. |
| 115 | + let expected_migrated_number = if block == 2 || block == 10 { 4 } else { 8 }; |
| 116 | + |
| 117 | + assert_eq!(num_migrated, last_num_migrated + expected_migrated_number); |
| 118 | + last_num_migrated = num_migrated; |
| 119 | + } |
| 120 | + |
| 121 | + // Check that everything is migrated now |
| 122 | + (0..64u64) |
| 123 | + .map(submissions_from_seed) |
| 124 | + .for_each(|(seeded_batch_id, _, _)| { |
| 125 | + assert!(crate::BatchQueue::<Test, Api>::iter() |
| 126 | + .any(|(_, batch_id)| seeded_batch_id == batch_id)) |
| 127 | + }); |
| 128 | + |
| 129 | + assert_eq!(crate::BatchQueue::<Test, Api>::count(), 64); |
| 130 | + }); |
| 131 | +} |
0 commit comments