|
| 1 | +use super::*; |
| 2 | +use frame_support::{traits::Get, weights::Weight}; |
| 3 | +use log; |
| 4 | +use scale_info::prelude::string::String; |
| 5 | + |
| 6 | +pub fn migrate_remove_zero_total_hotkey_alpha<T: Config>() -> Weight { |
| 7 | + let migration_name = b"migrate_remove_zero_total_hotkey_alpha".to_vec(); |
| 8 | + let mut weight = T::DbWeight::get().reads(1); |
| 9 | + |
| 10 | + // ------------------------------ |
| 11 | + // Step 0: Check if already run |
| 12 | + // ------------------------------ |
| 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 | + |
| 21 | + log::info!( |
| 22 | + "Running migration '{}'", |
| 23 | + String::from_utf8_lossy(&migration_name) |
| 24 | + ); |
| 25 | + |
| 26 | + // ------------------------------ |
| 27 | + // Step 1: Remove any zero entries in TotalHotkeyAlpha |
| 28 | + // ------------------------------ |
| 29 | + |
| 30 | + let mut removed_entries_count = 0u64; |
| 31 | + |
| 32 | + // For each (hotkey, netuid, alpha) entry, remove if alpha == 0 |
| 33 | + for (hotkey, netuid, alpha) in TotalHotkeyAlpha::<T>::iter() { |
| 34 | + if alpha == 0 { |
| 35 | + TotalHotkeyAlpha::<T>::remove(&hotkey, netuid); |
| 36 | + removed_entries_count = removed_entries_count.saturating_add(1); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + weight = weight.saturating_add(T::DbWeight::get().reads(removed_entries_count)); |
| 41 | + weight = weight.saturating_add(T::DbWeight::get().writes(removed_entries_count)); |
| 42 | + |
| 43 | + log::info!( |
| 44 | + "Removed {} zero entries from TotalHotkeyAlpha.", |
| 45 | + removed_entries_count |
| 46 | + ); |
| 47 | + |
| 48 | + // ------------------------------ |
| 49 | + // Step 2: Mark Migration as Completed |
| 50 | + // ------------------------------ |
| 51 | + HasMigrationRun::<T>::insert(&migration_name, true); |
| 52 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 53 | + |
| 54 | + log::info!( |
| 55 | + "Migration '{:?}' completed successfully.", |
| 56 | + String::from_utf8_lossy(&migration_name) |
| 57 | + ); |
| 58 | + |
| 59 | + weight |
| 60 | +} |
0 commit comments