|
| 1 | +use super::*; |
| 2 | +use alloc::string::String; |
| 3 | +use frame_support::{traits::Get, weights::Weight}; |
| 4 | +use log; |
| 5 | + |
| 6 | +pub fn migrate_fix_is_network_member<T: Config>() -> Weight { |
| 7 | + let migration_name = b"migrate_fix_is_network_member".to_vec(); |
| 8 | + |
| 9 | + // Initialize the weight with one read operation. |
| 10 | + let mut weight = T::DbWeight::get().reads(1); |
| 11 | + |
| 12 | + // Check if the migration has already run |
| 13 | + if HasMigrationRun::<T>::get(&migration_name) { |
| 14 | + log::info!( |
| 15 | + "Migration '{:?}' has already run. Skipping.", |
| 16 | + migration_name |
| 17 | + ); |
| 18 | + return weight; |
| 19 | + } |
| 20 | + log::info!( |
| 21 | + "Running migration '{}'", |
| 22 | + String::from_utf8_lossy(&migration_name) |
| 23 | + ); |
| 24 | + |
| 25 | + weight = do_fix_is_network_member::<T>(weight); |
| 26 | + |
| 27 | + // Mark the migration as completed |
| 28 | + HasMigrationRun::<T>::insert(&migration_name, true); |
| 29 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 30 | + |
| 31 | + log::info!( |
| 32 | + "Migration '{:?}' completed. Storage version set to 7.", |
| 33 | + String::from_utf8_lossy(&migration_name) |
| 34 | + ); |
| 35 | + |
| 36 | + // Return the migration weight. |
| 37 | + weight |
| 38 | +} |
| 39 | + |
| 40 | +fn do_fix_is_network_member<T: Config>(weight: Weight) -> Weight { |
| 41 | + // Clear the IsNetworkMember storage |
| 42 | + let mut curr = IsNetworkMember::<T>::clear(U32::MAX, None); |
| 43 | + weight = weight.saturating_add(T::DbWeight::get().reads_writes(curr.loops, curr.unique)); |
| 44 | + while curr.maybe_cursor.is_some() { |
| 45 | + // Clear until empty |
| 46 | + curr = IsNetworkMember::<T>::clear(U32::MAX, curr.maybe_cursor); |
| 47 | + weight = weight.saturating_add(T::DbWeight::get().reads_writes(curr.loops, curr.unique)); |
| 48 | + } |
| 49 | + // Repopulate the IsNetworkMember storage using the Keys map |
| 50 | + let netuids = Subtensor::<T>::get_all_subnet_netuids(); |
| 51 | + for netuid in netuids { |
| 52 | + for key in Keys::<T>::iter_prefix(netuid) { |
| 53 | + IsNetworkMember::<T>::insert(key, netuid, true); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + weight |
| 58 | +} |
0 commit comments