|
| 1 | +use super::*; |
| 2 | +use crate::{AccountIdOf, ReasonOf}; |
| 3 | +use core::marker::PhantomData; |
| 4 | +use frame_support::{migrations::VersionedMigration, traits::UncheckedOnRuntimeUpgrade}; |
| 5 | +use sp_runtime::{traits::BlockNumberProvider, Saturating, Weight}; |
| 6 | + |
| 7 | +pub type Values<T> = BoundedVec<VestingInfoOf<T>, MaxVestingSchedulesGet<T>>; |
| 8 | + |
| 9 | +const LOG: &str = "linear_release::migration::v1"; |
| 10 | +pub struct LinearReleaseVestingInfoMigration; |
| 11 | + |
| 12 | +pub struct UncheckedMigrationToV1<T: Config>(PhantomData<T>); |
| 13 | + |
| 14 | +impl<T: crate::Config> UncheckedOnRuntimeUpgrade for UncheckedMigrationToV1<T> { |
| 15 | + #[cfg(feature = "try-runtime")] |
| 16 | + fn pre_upgrade() -> Result<Vec<u8>, DispatchError> { |
| 17 | + let migration_count = crate::Vesting::<T>::iter().count() as u32; |
| 18 | + log::info!(target: LOG, "Pre-upgrade: {} UserMigrations entries", migration_count); |
| 19 | + |
| 20 | + let vestings = crate::Vesting::<T>::iter().collect::<Vec<_>>(); |
| 21 | + |
| 22 | + Ok((migration_count, vestings).encode()) |
| 23 | + } |
| 24 | + |
| 25 | + fn on_runtime_upgrade() -> Weight { |
| 26 | + let mut items = 0u64; |
| 27 | + let translate_vesting_info = |
| 28 | + |_: AccountIdOf<T>, _: ReasonOf<T>, vesting_info: Values<T>| -> Option<Values<T>> { |
| 29 | + let migrated: Vec<_> = vesting_info |
| 30 | + .iter() |
| 31 | + .map(|vesting| { |
| 32 | + items = items.saturating_add(1); |
| 33 | + |
| 34 | + // adjust starting block to relay chain block number |
| 35 | + let relay_chain_now = T::BlockNumberProvider::current_block_number(); |
| 36 | + |
| 37 | + let polimec_now = frame_system::Pallet::<T>::current_block_number(); |
| 38 | + let two = 2_u32.into(); |
| 39 | + |
| 40 | + let relay_chain_starting_block = if polimec_now < vesting.starting_block() { |
| 41 | + let blocks_diff = vesting.starting_block().saturating_sub(polimec_now); |
| 42 | + relay_chain_now.saturating_add(blocks_diff.saturating_mul(two)) |
| 43 | + } else { |
| 44 | + let blocks_passed = polimec_now.saturating_sub(vesting.starting_block()); |
| 45 | + relay_chain_now.saturating_sub(blocks_passed.saturating_mul(two)) |
| 46 | + }; |
| 47 | + |
| 48 | + let adjusted_per_block = vesting.per_block.saturating_mul(2_u32.into()); |
| 49 | + |
| 50 | + VestingInfo { |
| 51 | + locked: vesting.locked, |
| 52 | + per_block: adjusted_per_block, |
| 53 | + starting_block: relay_chain_starting_block, |
| 54 | + } |
| 55 | + }) |
| 56 | + .collect(); |
| 57 | + |
| 58 | + Values::<T>::try_from(migrated).ok() |
| 59 | + }; |
| 60 | + |
| 61 | + log::info!(target: LOG, "Starting linear release vesting time migration to V1"); |
| 62 | + |
| 63 | + crate::Vesting::<T>::translate(translate_vesting_info); |
| 64 | + |
| 65 | + log::info!(target: LOG, "Migrated {} linear release vesting entries", items); |
| 66 | + |
| 67 | + T::DbWeight::get().reads_writes(items, items) |
| 68 | + } |
| 69 | + |
| 70 | + #[cfg(feature = "try-runtime")] |
| 71 | + fn post_upgrade(pre_state: Vec<u8>) -> Result<(), DispatchError> { |
| 72 | + let (pre_migration_count, pre_vestings): (u32, Vec<((AccountIdOf<T>, ReasonOf<T>), Values<T>)>) = |
| 73 | + Decode::decode(&mut &pre_state[..]).expect("Failed to decode pre-migration state"); |
| 74 | + |
| 75 | + let post_migration_count = crate::Vesting::<T>::iter().count() as u32; |
| 76 | + |
| 77 | + if pre_migration_count != post_migration_count { |
| 78 | + return Err("Migration count mismatch".into()); |
| 79 | + } |
| 80 | + |
| 81 | + for ((account, reason), pre_vesting) in pre_vestings { |
| 82 | + let post_vesting = crate::Vesting::<T>::get(&account, &reason).unwrap_or_default(); |
| 83 | + |
| 84 | + // check that the starting block has been adjusted |
| 85 | + let relay_chain_now = T::BlockNumberProvider::current_block_number(); |
| 86 | + |
| 87 | + for (pre_vesting_info, post_vesting_info) in pre_vesting.iter().zip(post_vesting.iter()) { |
| 88 | + assert_ne!( |
| 89 | + pre_vesting_info.starting_block, post_vesting_info.starting_block, |
| 90 | + "Starting block not adjusted" |
| 91 | + ); |
| 92 | + assert!( |
| 93 | + post_vesting_info.starting_block <= relay_chain_now.try_into().ok().expect("safe to convert; qed"), |
| 94 | + "Starting block not adjusted correctly" |
| 95 | + ); |
| 96 | + |
| 97 | + assert!( |
| 98 | + post_vesting_info.per_block == |
| 99 | + pre_vesting_info |
| 100 | + .per_block |
| 101 | + .saturating_mul(2_u32.try_into().ok().expect("safe to convert; qed")), |
| 102 | + "Per block not adjusted" |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + Ok(()) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +pub type LinearReleaseVestingMigrationV1<T> = |
| 112 | + VersionedMigration<0, 1, UncheckedMigrationToV1<T>, crate::Pallet<T>, <T as frame_system::Config>::DbWeight>; |
| 113 | + |
| 114 | +#[cfg(test)] |
| 115 | +mod test { |
| 116 | + use super::*; |
| 117 | + use crate::{ |
| 118 | + mock::{ExtBuilder, MockRuntimeHoldReason, System, Test, MOCK_BLOCK_NUMBER}, |
| 119 | + pallet::Vesting, |
| 120 | + AccountIdOf, BalanceOf, BlockNumberFor, |
| 121 | + }; |
| 122 | + use frame_support::weights::RuntimeDbWeight; |
| 123 | + use sp_runtime::bounded_vec; |
| 124 | + |
| 125 | + // Helper to calculate expected results concisely |
| 126 | + // Now takes the *actual* polimec_now and the *fixed* relay_chain_now from the mock provider |
| 127 | + fn calculate_expected( |
| 128 | + vesting: &VestingInfo<BalanceOf<Test>, BlockNumberFor<Test>>, |
| 129 | + polimec_now: BlockNumberFor<Test>, |
| 130 | + relay_chain_now: BlockNumberFor<Test>, // This will be MOCK_BLOCK_NUMBER |
| 131 | + ) -> VestingInfo<BalanceOf<Test>, BlockNumberFor<Test>> { |
| 132 | + let two: BlockNumberFor<Test> = 2u32.into(); |
| 133 | + let expected_relay_start = if polimec_now < vesting.starting_block { |
| 134 | + let blocks_diff = vesting.starting_block.saturating_sub(polimec_now); |
| 135 | + relay_chain_now.saturating_add(blocks_diff.saturating_mul(two)) |
| 136 | + } else { |
| 137 | + let blocks_passed = polimec_now.saturating_sub(vesting.starting_block); |
| 138 | + relay_chain_now.saturating_sub(blocks_passed.saturating_mul(two)) |
| 139 | + }; |
| 140 | + let expected_per_block = vesting.per_block.saturating_mul(two); |
| 141 | + |
| 142 | + VestingInfo { |
| 143 | + locked: vesting.locked, // Stays the same |
| 144 | + per_block: expected_per_block, |
| 145 | + starting_block: expected_relay_start, |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + #[test] |
| 150 | + fn migration_v1_adjusts_schedules_correctly() { |
| 151 | + ExtBuilder::default().existential_deposit(256).build().execute_with(|| { |
| 152 | + let polimec_now: BlockNumberFor<Test> = 100; |
| 153 | + System::set_block_number(polimec_now); |
| 154 | + |
| 155 | + // The relay chain block number is now fixed by the mock provider |
| 156 | + let relay_chain_now: BlockNumberFor<Test> = MOCK_BLOCK_NUMBER; |
| 157 | + |
| 158 | + let account1: AccountIdOf<Test> = 3; |
| 159 | + let account2: AccountIdOf<Test> = 4; |
| 160 | + |
| 161 | + let reason1 = MockRuntimeHoldReason::Reason; |
| 162 | + let reason2 = MockRuntimeHoldReason::Reason2; |
| 163 | + |
| 164 | + // Schedule starting in the past relative to polimec_now |
| 165 | + let v_past = VestingInfo { locked: 1000, per_block: 10, starting_block: 50 }; |
| 166 | + // Schedule starting in the future relative to polimec_now |
| 167 | + let v_future = VestingInfo { locked: 2000, per_block: 20, starting_block: 150 }; |
| 168 | + // Schedule starting exactly now relative to polimec_now |
| 169 | + let v_now = VestingInfo { locked: 500, per_block: 5, starting_block: polimec_now }; |
| 170 | + // Schedule starting at block 0 (edge case) |
| 171 | + let v_zero = VestingInfo { locked: 100, per_block: 1, starting_block: 0 }; |
| 172 | + |
| 173 | + // Entry 1: Acc1, Reason1 -> Multiple schedules, covering past and future |
| 174 | + let schedules1: Values<Test> = bounded_vec![v_past.clone(), v_future.clone()]; |
| 175 | + Vesting::<Test>::insert(account1, reason1.clone(), schedules1.clone()); |
| 176 | + |
| 177 | + // Entry 2: Acc2, Reason1 -> Single schedule, covering 'now' case |
| 178 | + let schedules2: Values<Test> = bounded_vec![v_now.clone()]; |
| 179 | + Vesting::<Test>::insert(account2, reason1.clone(), schedules2.clone()); |
| 180 | + |
| 181 | + // Entry 3: Acc1, Reason2 -> Single schedule, edge case start block |
| 182 | + let schedules3: Values<Test> = bounded_vec![v_zero.clone()]; |
| 183 | + Vesting::<Test>::insert(account1, reason2.clone(), schedules3.clone()); |
| 184 | + |
| 185 | + // Verify initial counts |
| 186 | + let initial_storage_entries = Vesting::<Test>::iter_keys().count(); // Counts distinct (Acc, Reason) pairs |
| 187 | + let initial_schedules_count: u64 = Vesting::<Test>::iter_values().map(|v| v.len() as u64).sum(); |
| 188 | + assert_eq!(initial_storage_entries, 6); // default already adds 3 entries |
| 189 | + assert_eq!(initial_schedules_count, 7); // 3 from default, 4 from our setup |
| 190 | + |
| 191 | + let weight = UncheckedMigrationToV1::<Test>::on_runtime_upgrade(); |
| 192 | + |
| 193 | + assert_eq!( |
| 194 | + Vesting::<Test>::iter_keys().count(), |
| 195 | + initial_storage_entries, |
| 196 | + "Number of storage entries should not change" |
| 197 | + ); |
| 198 | + |
| 199 | + let migrated1 = Vesting::<Test>::get(account1, reason1.clone()).unwrap(); |
| 200 | + assert_eq!(migrated1.len(), 2); |
| 201 | + |
| 202 | + assert_eq!(migrated1[0], calculate_expected(&v_past, polimec_now, relay_chain_now)); |
| 203 | + assert_eq!(migrated1[1], calculate_expected(&v_future, polimec_now, relay_chain_now)); |
| 204 | + |
| 205 | + let migrated2 = Vesting::<Test>::get(account2, reason1.clone()).unwrap(); |
| 206 | + assert_eq!(migrated2.len(), 1); |
| 207 | + assert_eq!(migrated2[0], calculate_expected(&v_now, polimec_now, relay_chain_now)); |
| 208 | + |
| 209 | + let migrated3 = Vesting::<Test>::get(account1, reason2.clone()).unwrap(); |
| 210 | + assert_eq!(migrated3.len(), 1); |
| 211 | + assert_eq!(migrated3[0], calculate_expected(&v_zero, polimec_now, relay_chain_now)); |
| 212 | + |
| 213 | + let db_weight: RuntimeDbWeight = <Test as frame_system::Config>::DbWeight::get(); |
| 214 | + let expected_weight = db_weight.reads_writes(initial_schedules_count, initial_schedules_count); |
| 215 | + |
| 216 | + assert_eq!(weight, expected_weight, "Weight should match items processed"); |
| 217 | + |
| 218 | + // also verify default vesting entries that are migrated |
| 219 | + let default_vesting = Vesting::<Test>::get(1, MockRuntimeHoldReason::Reason).unwrap(); |
| 220 | + assert_eq!( |
| 221 | + default_vesting[0], |
| 222 | + calculate_expected( |
| 223 | + &VestingInfo { starting_block: 0, per_block: 128, locked: 5 * 256 }, |
| 224 | + polimec_now, |
| 225 | + relay_chain_now |
| 226 | + ), |
| 227 | + ); |
| 228 | + |
| 229 | + let default_vesting_2 = Vesting::<Test>::get(2, MockRuntimeHoldReason::Reason).unwrap(); |
| 230 | + assert_eq!( |
| 231 | + default_vesting_2[0], |
| 232 | + calculate_expected( |
| 233 | + &VestingInfo { starting_block: 10, per_block: 256, locked: 20 * 256 }, |
| 234 | + polimec_now, |
| 235 | + relay_chain_now |
| 236 | + ), |
| 237 | + ); |
| 238 | + }); |
| 239 | + } |
| 240 | +} |
0 commit comments