|
| 1 | +use super::*; |
| 2 | +use crate::AccountIdOf; |
| 3 | +use frame_support::{ |
| 4 | + IterableStorageMap, |
| 5 | + pallet_prelude::{Blake2_128Concat, OptionQuery}, |
| 6 | + storage_alias, |
| 7 | + traits::Get, |
| 8 | + weights::Weight, |
| 9 | +}; |
| 10 | +use scale_info::prelude::string::String; |
| 11 | + |
| 12 | +/// Module containing deprecated storage format for AutoStakeDestination |
| 13 | +pub mod deprecated_auto_stake_destination_format { |
| 14 | + use super::*; |
| 15 | + |
| 16 | + #[storage_alias] |
| 17 | + pub(super) type AutoStakeDestination<T: Config> = |
| 18 | + StorageMap<Pallet<T>, Blake2_128Concat, AccountIdOf<T>, AccountIdOf<T>, OptionQuery>; |
| 19 | +} |
| 20 | + |
| 21 | +/// Migrate the AutoStakeDestination map from single map to double map format |
| 22 | +pub fn migrate_auto_stake_destination<T: Config>() -> Weight { |
| 23 | + use deprecated_auto_stake_destination_format as old; |
| 24 | + |
| 25 | + let migration_name = b"migrate_auto_stake_destination".to_vec(); |
| 26 | + let mut weight = T::DbWeight::get().reads(1); |
| 27 | + |
| 28 | + if HasMigrationRun::<T>::get(&migration_name) { |
| 29 | + log::info!( |
| 30 | + "Migration '{:?}' has already run. Skipping.", |
| 31 | + String::from_utf8_lossy(&migration_name) |
| 32 | + ); |
| 33 | + return weight; |
| 34 | + } |
| 35 | + |
| 36 | + log::info!( |
| 37 | + "Running migration '{}'", |
| 38 | + String::from_utf8_lossy(&migration_name) |
| 39 | + ); |
| 40 | + |
| 41 | + // ------------------------------ |
| 42 | + // Step 1: Migrate AutoStakeDestination entries |
| 43 | + // ------------------------------ |
| 44 | + |
| 45 | + let curr_keys: Vec<AccountIdOf<T>> = old::AutoStakeDestination::<T>::iter_keys().collect(); |
| 46 | + let root_netuid = NetUid::ROOT; |
| 47 | + let netuids: Vec<NetUid> = <NetworksAdded<T> as IterableStorageMap<NetUid, bool>>::iter() |
| 48 | + .map(|(netuid, _)| netuid) |
| 49 | + .collect(); |
| 50 | + |
| 51 | + for coldkey in &curr_keys { |
| 52 | + weight.saturating_accrue(T::DbWeight::get().reads(1)); |
| 53 | + |
| 54 | + if let Some(hotkey) = old::AutoStakeDestination::<T>::get(coldkey) { |
| 55 | + for netuid in netuids.iter() { |
| 56 | + if *netuid == root_netuid { |
| 57 | + continue; |
| 58 | + } |
| 59 | + AutoStakeDestination::<T>::insert(coldkey, netuid, hotkey.clone()); |
| 60 | + } |
| 61 | + |
| 62 | + old::AutoStakeDestination::<T>::remove(coldkey); |
| 63 | + |
| 64 | + weight.saturating_accrue(T::DbWeight::get().writes(netuids.len() as u64)); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // ------------------------------ |
| 69 | + // Step 2: Mark Migration as Completed |
| 70 | + // ------------------------------ |
| 71 | + |
| 72 | + HasMigrationRun::<T>::insert(&migration_name, true); |
| 73 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 74 | + |
| 75 | + log::info!( |
| 76 | + "Migration '{:?}' completed successfully. {} entries migrated.", |
| 77 | + String::from_utf8_lossy(&migration_name), |
| 78 | + curr_keys.len() |
| 79 | + ); |
| 80 | + |
| 81 | + weight |
| 82 | +} |
0 commit comments