|
| 1 | +// storage_migrations.rs |
| 2 | + |
| 3 | +use crate::Config; |
| 4 | +use alloc::vec::Vec; |
| 5 | +use frame_support::{pallet_prelude::*, traits::UncheckedOnRuntimeUpgrade, weights::Weight}; |
| 6 | +use polimec_common::migration_types::{Migration, MigrationInfo, MigrationStatus}; |
| 7 | +use sp_runtime::{traits::ConstU32, WeakBoundedVec}; |
| 8 | + |
| 9 | +pub mod v7 { |
| 10 | + use crate::{AccountIdOf, ProjectId, UserMigrations}; |
| 11 | + |
| 12 | + use super::*; |
| 13 | + |
| 14 | + const LOG: &str = "funding::migration::v7"; |
| 15 | + pub const MAX_PARTICIPATIONS_PER_USER: u32 = 10_000; |
| 16 | + |
| 17 | + type UserMigrationsKey<T> = (ProjectId, AccountIdOf<T>); |
| 18 | + type UserMigrationsValue = (MigrationStatus, WeakBoundedVec<Migration, ConstU32<MAX_PARTICIPATIONS_PER_USER>>); |
| 19 | + |
| 20 | + pub struct UncheckedMigrationToV7<T: Config>(PhantomData<T>); |
| 21 | + impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrationToV7<T> { |
| 22 | + #[cfg(feature = "try-runtime")] |
| 23 | + fn pre_upgrade() -> Result<Vec<u8>, DispatchError> { |
| 24 | + let migration_count = UserMigrations::<T>::iter().count() as u32; |
| 25 | + log::info!(target: LOG, "Pre-upgrade: {} UserMigrations entries", migration_count); |
| 26 | + |
| 27 | + // Encode counts and sample pre-migration vesting times for validation |
| 28 | + let user_migrations = UserMigrations::<T>::iter().collect::<Vec<_>>(); |
| 29 | + |
| 30 | + Ok((migration_count, user_migrations).encode()) |
| 31 | + } |
| 32 | + |
| 33 | + fn on_runtime_upgrade() -> Weight { |
| 34 | + let mut items = 0u64; |
| 35 | + log::info!(target: LOG, "Starting vesting time migration to V7"); |
| 36 | + |
| 37 | + let translate_migrations = |
| 38 | + |_: UserMigrationsKey<T>, migration_value: UserMigrationsValue| -> Option<UserMigrationsValue> { |
| 39 | + let (status, migrations) = migration_value; |
| 40 | + let migrated: Vec<_> = migrations |
| 41 | + .iter() |
| 42 | + .map(|migration| { |
| 43 | + items = items.saturating_add(1); |
| 44 | + |
| 45 | + Migration { |
| 46 | + info: MigrationInfo { |
| 47 | + vesting_time: migration.info.vesting_time.saturating_div(2), |
| 48 | + ..migration.info |
| 49 | + }, |
| 50 | + ..migration.clone() |
| 51 | + } |
| 52 | + }) |
| 53 | + .collect(); |
| 54 | + |
| 55 | + let bounded = WeakBoundedVec::try_from(migrated) |
| 56 | + .unwrap_or_else(|_| panic!("MaxParticipationsPerUser exceeded during migration")); |
| 57 | + |
| 58 | + Some((status, bounded)) |
| 59 | + }; |
| 60 | + |
| 61 | + UserMigrations::<T>::translate(translate_migrations); |
| 62 | + |
| 63 | + log::info!(target: LOG, "Migrated {} vesting entries", items); |
| 64 | + T::DbWeight::get().reads_writes(items, items) |
| 65 | + } |
| 66 | + |
| 67 | + #[cfg(feature = "try-runtime")] |
| 68 | + fn post_upgrade(pre_state: Vec<u8>) -> Result<(), DispatchError> { |
| 69 | + let (pre_migration_count, pre_user_migrations): (u32, Vec<(UserMigrationsKey<T>, UserMigrationsValue)>) = |
| 70 | + Decode::decode(&mut &pre_state[..]).expect("Failed to decode pre-migration state"); |
| 71 | + |
| 72 | + let post_migration_count = UserMigrations::<T>::iter().count() as u32; |
| 73 | + |
| 74 | + if pre_migration_count != post_migration_count { |
| 75 | + return Err("Migration count mismatch".into()); |
| 76 | + } |
| 77 | + |
| 78 | + for (key, pre_value) in pre_user_migrations { |
| 79 | + let post_value = UserMigrations::<T>::get(key.clone()) |
| 80 | + .unwrap_or_else(|| panic!("Post-migration UserMigrations entry not found for {:?}", key)); |
| 81 | + |
| 82 | + for (pre_migration, post_migration) in pre_value.1.iter().zip(post_value.1.iter()) { |
| 83 | + if pre_migration.info.vesting_time.saturating_div(2) != post_migration.info.vesting_time { |
| 84 | + return Err("Migration vesting time mismatch".into()); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + Ok(()) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + pub type MigrationToV8<T> = frame_support::migrations::VersionedMigration< |
| 94 | + 6, |
| 95 | + 7, |
| 96 | + UncheckedMigrationToV7<T>, |
| 97 | + crate::Pallet<T>, |
| 98 | + <T as frame_system::Config>::DbWeight, |
| 99 | + >; |
| 100 | +} |
| 101 | + |
| 102 | +#[cfg(test)] |
| 103 | +mod tests { |
| 104 | + use super::*; |
| 105 | + use crate::{ |
| 106 | + mock::{new_test_ext, TestRuntime as Test}, |
| 107 | + UserMigrations, |
| 108 | + }; |
| 109 | + use cumulus_primitives_core::Junction; |
| 110 | + use polimec_common::migration_types::MigrationOrigin; |
| 111 | + use v7::{UncheckedMigrationToV7, MAX_PARTICIPATIONS_PER_USER}; |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn migration_to_v7() { |
| 115 | + let mut ext = new_test_ext(); |
| 116 | + ext.execute_with(|| { |
| 117 | + assert_eq!(UserMigrations::<Test>::iter().count(), 0); |
| 118 | + |
| 119 | + let mut migrations = Vec::new(); |
| 120 | + for i in 0..MAX_PARTICIPATIONS_PER_USER { |
| 121 | + migrations.push(Migration { |
| 122 | + info: MigrationInfo { vesting_time: i as u64, contribution_token_amount: 1_u128 }, |
| 123 | + origin: MigrationOrigin { |
| 124 | + user: Junction::OnlyChild, |
| 125 | + participation_type: polimec_common::migration_types::ParticipationType::Bid, |
| 126 | + }, |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + let bounded_migrations = WeakBoundedVec::try_from(migrations).unwrap(); |
| 131 | + |
| 132 | + UserMigrations::<Test>::insert((1, 1), (MigrationStatus::Confirmed, bounded_migrations)); |
| 133 | + |
| 134 | + assert_eq!(UserMigrations::<Test>::iter().count(), 1); |
| 135 | + |
| 136 | + let weight = UncheckedMigrationToV7::<Test>::on_runtime_upgrade(); |
| 137 | + assert_eq!(UserMigrations::<Test>::iter().count(), 1); |
| 138 | + |
| 139 | + for (_, (_, migrations)) in UserMigrations::<Test>::iter() { |
| 140 | + for (i, migration) in migrations.iter().enumerate() { |
| 141 | + assert_eq!(migration.info.vesting_time, i as u64 / 2); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + assert!(weight.is_zero()); |
| 146 | + }); |
| 147 | + } |
| 148 | +} |
0 commit comments