|
| 1 | +use super::*; |
| 2 | +use frame_support::{traits::Get, weights::Weight}; |
| 3 | +use log; |
| 4 | +use scale_info::prelude::string::String; |
| 5 | + |
| 6 | +/// The migration name used for the `HasMigrationRun` guard. |
| 7 | +const MIGRATION_NAME: &[u8] = b"migrate_remove_zero_alpha_v2"; |
| 8 | + |
| 9 | +/// Called from `on_runtime_upgrade`. Schedules the cleanup by setting phase = 1 |
| 10 | +/// if the migration hasn't run yet. This is O(1) — no iteration. |
| 11 | +pub fn migrate_remove_zero_alpha<T: Config>() -> Weight { |
| 12 | + let migration_name = MIGRATION_NAME.to_vec(); |
| 13 | + let mut weight = T::DbWeight::get().reads(1); |
| 14 | + |
| 15 | + if HasMigrationRun::<T>::get(&migration_name) { |
| 16 | + log::info!( |
| 17 | + "Migration '{}' already completed. Skipping.", |
| 18 | + String::from_utf8_lossy(&migration_name) |
| 19 | + ); |
| 20 | + return weight; |
| 21 | + } |
| 22 | + |
| 23 | + // Schedule the cleanup to run in on_idle by setting phase to 1 |
| 24 | + ZeroAlphaCleanupPhase::<T>::put(1u8); |
| 25 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 26 | + |
| 27 | + log::info!( |
| 28 | + "Migration '{}' scheduled. Will clean up zero entries via on_idle.", |
| 29 | + String::from_utf8_lossy(&migration_name) |
| 30 | + ); |
| 31 | + |
| 32 | + weight |
| 33 | +} |
| 34 | + |
| 35 | +/// Called from `on_idle` each block. Processes one storage map per block, |
| 36 | +/// removing all zero-valued entries. Advances to the next phase when done. |
| 37 | +/// |
| 38 | +/// Phases: |
| 39 | +/// 0 = inactive/complete |
| 40 | +/// 1 = cleaning Alpha |
| 41 | +/// 2 = cleaning TotalHotkeyShares |
| 42 | +/// 3 = cleaning TotalHotkeyAlphaLastEpoch |
| 43 | +/// 4 = cleaning AlphaDividendsPerSubnet |
| 44 | +pub fn on_idle_remove_zero_alpha<T: Config>(remaining_weight: Weight) -> Weight { |
| 45 | + let phase = ZeroAlphaCleanupPhase::<T>::get(); |
| 46 | + |
| 47 | + // Phase 0 means not active or already completed |
| 48 | + if phase == 0 { |
| 49 | + return Weight::zero(); |
| 50 | + } |
| 51 | + |
| 52 | + // Minimum weight needed: 1 read (phase) + at least some work |
| 53 | + let min_weight = T::DbWeight::get().reads_writes(2, 1); |
| 54 | + if remaining_weight.ref_time() < min_weight.ref_time() { |
| 55 | + return Weight::zero(); |
| 56 | + } |
| 57 | + |
| 58 | + let mut weight = T::DbWeight::get().reads(1); // reading phase |
| 59 | + |
| 60 | + match phase { |
| 61 | + 1 => { |
| 62 | + let (consumed, removed) = clean_alpha::<T>(); |
| 63 | + weight = weight.saturating_add(consumed); |
| 64 | + log::info!("Zero-alpha cleanup: Alpha complete. Removed {removed} zero entries."); |
| 65 | + ZeroAlphaCleanupPhase::<T>::put(2u8); |
| 66 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 67 | + } |
| 68 | + 2 => { |
| 69 | + let (consumed, removed) = clean_total_hotkey_shares::<T>(); |
| 70 | + weight = weight.saturating_add(consumed); |
| 71 | + log::info!( |
| 72 | + "Zero-alpha cleanup: TotalHotkeyShares complete. Removed {removed} zero entries." |
| 73 | + ); |
| 74 | + ZeroAlphaCleanupPhase::<T>::put(3u8); |
| 75 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 76 | + } |
| 77 | + 3 => { |
| 78 | + let (consumed, removed) = clean_total_hotkey_alpha_last_epoch::<T>(); |
| 79 | + weight = weight.saturating_add(consumed); |
| 80 | + log::info!( |
| 81 | + "Zero-alpha cleanup: TotalHotkeyAlphaLastEpoch complete. Removed {removed} zero entries." |
| 82 | + ); |
| 83 | + ZeroAlphaCleanupPhase::<T>::put(4u8); |
| 84 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 85 | + } |
| 86 | + 4 => { |
| 87 | + let (consumed, removed) = clean_alpha_dividends_per_subnet::<T>(); |
| 88 | + weight = weight.saturating_add(consumed); |
| 89 | + log::info!( |
| 90 | + "Zero-alpha cleanup: AlphaDividendsPerSubnet complete. Removed {removed} zero entries." |
| 91 | + ); |
| 92 | + |
| 93 | + // All phases complete — mark migration as done |
| 94 | + HasMigrationRun::<T>::insert(MIGRATION_NAME.to_vec(), true); |
| 95 | + ZeroAlphaCleanupPhase::<T>::put(0u8); |
| 96 | + weight = weight.saturating_add(T::DbWeight::get().writes(2)); |
| 97 | + log::info!("Zero-alpha cleanup: All phases complete. Migration marked as done."); |
| 98 | + } |
| 99 | + _ => { |
| 100 | + // Unknown phase, reset |
| 101 | + ZeroAlphaCleanupPhase::<T>::put(0u8); |
| 102 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + weight |
| 107 | +} |
| 108 | + |
| 109 | +/// Remove all zero-valued entries from Alpha. |
| 110 | +/// Returns (weight_consumed, entries_removed). |
| 111 | +fn clean_alpha<T: Config>() -> (Weight, u64) { |
| 112 | + let mut weight = Weight::zero(); |
| 113 | + let mut removed = 0u64; |
| 114 | + |
| 115 | + let to_remove: Vec<_> = Alpha::<T>::iter() |
| 116 | + .filter_map(|((hotkey, coldkey, netuid), value)| { |
| 117 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 118 | + if value == 0 { |
| 119 | + Some((hotkey, coldkey, netuid)) |
| 120 | + } else { |
| 121 | + None |
| 122 | + } |
| 123 | + }) |
| 124 | + .collect(); |
| 125 | + |
| 126 | + for (hotkey, coldkey, netuid) in &to_remove { |
| 127 | + Alpha::<T>::remove((hotkey, coldkey, *netuid)); |
| 128 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 129 | + removed = removed.saturating_add(1); |
| 130 | + } |
| 131 | + |
| 132 | + (weight, removed) |
| 133 | +} |
| 134 | + |
| 135 | +/// Remove all zero-valued entries from TotalHotkeyShares. |
| 136 | +fn clean_total_hotkey_shares<T: Config>() -> (Weight, u64) { |
| 137 | + let mut weight = Weight::zero(); |
| 138 | + let mut removed = 0u64; |
| 139 | + |
| 140 | + let to_remove: Vec<_> = TotalHotkeyShares::<T>::iter() |
| 141 | + .filter_map(|(hotkey, netuid, value)| { |
| 142 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 143 | + if value == 0 { |
| 144 | + Some((hotkey, netuid)) |
| 145 | + } else { |
| 146 | + None |
| 147 | + } |
| 148 | + }) |
| 149 | + .collect(); |
| 150 | + |
| 151 | + for (hotkey, netuid) in &to_remove { |
| 152 | + TotalHotkeyShares::<T>::remove(hotkey, *netuid); |
| 153 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 154 | + removed = removed.saturating_add(1); |
| 155 | + } |
| 156 | + |
| 157 | + (weight, removed) |
| 158 | +} |
| 159 | + |
| 160 | +/// Remove all zero-valued entries from TotalHotkeyAlphaLastEpoch. |
| 161 | +fn clean_total_hotkey_alpha_last_epoch<T: Config>() -> (Weight, u64) { |
| 162 | + let mut weight = Weight::zero(); |
| 163 | + let mut removed = 0u64; |
| 164 | + |
| 165 | + let to_remove: Vec<_> = TotalHotkeyAlphaLastEpoch::<T>::iter() |
| 166 | + .filter_map(|(hotkey, netuid, value)| { |
| 167 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 168 | + if value.is_zero() { |
| 169 | + Some((hotkey, netuid)) |
| 170 | + } else { |
| 171 | + None |
| 172 | + } |
| 173 | + }) |
| 174 | + .collect(); |
| 175 | + |
| 176 | + for (hotkey, netuid) in &to_remove { |
| 177 | + TotalHotkeyAlphaLastEpoch::<T>::remove(hotkey, *netuid); |
| 178 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 179 | + removed = removed.saturating_add(1); |
| 180 | + } |
| 181 | + |
| 182 | + (weight, removed) |
| 183 | +} |
| 184 | + |
| 185 | +/// Remove all zero-valued entries from AlphaDividendsPerSubnet. |
| 186 | +fn clean_alpha_dividends_per_subnet<T: Config>() -> (Weight, u64) { |
| 187 | + let mut weight = Weight::zero(); |
| 188 | + let mut removed = 0u64; |
| 189 | + |
| 190 | + let to_remove: Vec<_> = AlphaDividendsPerSubnet::<T>::iter() |
| 191 | + .filter_map(|(netuid, hotkey, value)| { |
| 192 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 193 | + if value.is_zero() { |
| 194 | + Some((netuid, hotkey)) |
| 195 | + } else { |
| 196 | + None |
| 197 | + } |
| 198 | + }) |
| 199 | + .collect(); |
| 200 | + |
| 201 | + for (netuid, hotkey) in &to_remove { |
| 202 | + AlphaDividendsPerSubnet::<T>::remove(*netuid, hotkey); |
| 203 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 204 | + removed = removed.saturating_add(1); |
| 205 | + } |
| 206 | + |
| 207 | + (weight, removed) |
| 208 | +} |
0 commit comments