|
| 1 | +use alloc::string::String; |
| 2 | + |
| 3 | +use frame_support::IterableStorageMap; |
| 4 | +use frame_support::{traits::Get, weights::Weight}; |
| 5 | + |
| 6 | +use super::*; |
| 7 | + |
| 8 | +pub fn migrate_set_min_difficulty<T: Config>() -> Weight { |
| 9 | + let migration_name = b"migrate_set_min_difficulty".to_vec(); |
| 10 | + |
| 11 | + // Initialize the weight with one read operation. |
| 12 | + let mut weight = T::DbWeight::get().reads(1); |
| 13 | + |
| 14 | + // Check if the migration has already run |
| 15 | + if HasMigrationRun::<T>::get(&migration_name) { |
| 16 | + log::info!( |
| 17 | + "Migration '{:?}' has already run. Skipping.", |
| 18 | + migration_name |
| 19 | + ); |
| 20 | + return weight; |
| 21 | + } |
| 22 | + log::info!( |
| 23 | + "Running migration '{}'", |
| 24 | + String::from_utf8_lossy(&migration_name) |
| 25 | + ); |
| 26 | + |
| 27 | + let netuids: Vec<u16> = <NetworksAdded<T> as IterableStorageMap<u16, bool>>::iter() |
| 28 | + .map(|(netuid, _)| netuid) |
| 29 | + .collect(); |
| 30 | + weight = weight.saturating_add(T::DbWeight::get().reads(netuids.len() as u64)); |
| 31 | + |
| 32 | + for netuid in netuids.iter().clone() { |
| 33 | + if *netuid == 0 { |
| 34 | + continue; |
| 35 | + } |
| 36 | + // Set min difficulty to 10 million for all subnets |
| 37 | + Pallet::<T>::set_min_difficulty(*netuid, 10_000_000); |
| 38 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 39 | + } |
| 40 | + |
| 41 | + // Mark the migration as completed |
| 42 | + HasMigrationRun::<T>::insert(&migration_name, true); |
| 43 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 44 | + |
| 45 | + log::info!( |
| 46 | + "Migration '{:?}' completed.", |
| 47 | + String::from_utf8_lossy(&migration_name) |
| 48 | + ); |
| 49 | + |
| 50 | + // Return the migration weight. |
| 51 | + weight |
| 52 | +} |
0 commit comments