|
| 1 | +use alloc::string::String; |
| 2 | + |
| 3 | +use frame_support::{traits::Get, weights::Weight}; |
| 4 | +use substrate_fixed::types::I96F32; |
| 5 | + |
| 6 | +use super::*; |
| 7 | + |
| 8 | +pub fn migrate_dissolve_sn73<T: Config>() -> Weight { |
| 9 | + let migration_name = b"migrate_dissolve_sn73".to_vec(); |
| 10 | + let this_netuid = 73; |
| 11 | + |
| 12 | + // Initialize the weight with one read operation. |
| 13 | + let mut weight = T::DbWeight::get().reads(1); |
| 14 | + |
| 15 | + // Check if the migration has already run |
| 16 | + if HasMigrationRun::<T>::get(&migration_name) { |
| 17 | + log::info!( |
| 18 | + "Migration '{:?}' has already run. Skipping.", |
| 19 | + migration_name |
| 20 | + ); |
| 21 | + return weight; |
| 22 | + } |
| 23 | + log::info!( |
| 24 | + "Running migration '{}'", |
| 25 | + String::from_utf8_lossy(&migration_name) |
| 26 | + ); |
| 27 | + |
| 28 | + if NetworksAdded::<T>::get(this_netuid) { |
| 29 | + // Subnet exists, skip |
| 30 | + log::info!("Subnet was already added, skipping"); |
| 31 | + } else { |
| 32 | + // ======== Migration Logic ======== |
| 33 | + |
| 34 | + // Get the subnet TAO |
| 35 | + let subnet_tao = I96F32::from_num(SubnetTAO::<T>::get(this_netuid)); |
| 36 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 37 | + log::debug!("Subnet TAO: {}", subnet_tao); |
| 38 | + |
| 39 | + let mut total_alpha: I96F32 = I96F32::from_num(0); |
| 40 | + // Iterate over every hotkey and sum up the total alpha |
| 41 | + let mut hotkeys_to_remove: Vec<T::AccountId> = Vec::new(); |
| 42 | + for (hotkey, netuid_i, total_hotkey_alpha) in TotalHotkeyAlpha::<T>::iter() { |
| 43 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 44 | + if netuid_i != this_netuid { |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + hotkeys_to_remove.push(hotkey); |
| 49 | + total_alpha = total_alpha.saturating_add(I96F32::from_num(total_hotkey_alpha)); |
| 50 | + } |
| 51 | + log::debug!("Total alpha: {}", total_alpha); |
| 52 | + |
| 53 | + // Iterate over every hotkey and distribute the TAO from the pool |
| 54 | + // using previous total alpha as the denominator |
| 55 | + for hotkey in hotkeys_to_remove.iter() { |
| 56 | + log::debug!("Hotkey: {:?}", hotkey.clone()); |
| 57 | + |
| 58 | + let total_hotkey_alpha_i = TotalHotkeyAlpha::<T>::get(hotkey.clone(), this_netuid); |
| 59 | + let total_hotkey_alpha = I96F32::from_num(total_hotkey_alpha_i); |
| 60 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 61 | + |
| 62 | + // Get the total hotkey shares |
| 63 | + let total_hotkey_shares = |
| 64 | + I96F32::from_num(TotalHotkeyShares::<T>::get(hotkey.clone(), this_netuid)); |
| 65 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 66 | + log::debug!("Total hotkey shares: {}", total_hotkey_shares); |
| 67 | + |
| 68 | + // Get the equivalent amount of TAO |
| 69 | + let hotkey_tao: I96F32 = total_hotkey_alpha |
| 70 | + .saturating_div(total_alpha) |
| 71 | + .saturating_mul(subnet_tao); |
| 72 | + log::debug!("Total hotkey alpha: {}", total_hotkey_alpha); |
| 73 | + log::debug!("Hotkey TAO: {}", hotkey_tao); |
| 74 | + |
| 75 | + let mut coldkeys_to_remove: Vec<T::AccountId> = Vec::new(); |
| 76 | + // Distribute the TAO to each of the stakers to the hotkey |
| 77 | + for ((coldkey, netuid_i), alpha_i) in Alpha::<T>::iter_prefix((&hotkey,)) { |
| 78 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 79 | + if netuid_i != this_netuid { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + coldkeys_to_remove.push(coldkey.clone()); |
| 84 | + |
| 85 | + let alpha_shares = I96F32::from_num(alpha_i); |
| 86 | + let coldkey_share: I96F32 = alpha_shares.saturating_div(total_hotkey_shares); |
| 87 | + let coldkey_tao = coldkey_share.saturating_mul(hotkey_tao); |
| 88 | + let coldkey_alpha = coldkey_share.saturating_mul(total_hotkey_alpha); |
| 89 | + log::debug!("Alpha shares: {}", alpha_shares); |
| 90 | + log::debug!("Coldkey share: {}", coldkey_share); |
| 91 | + log::debug!("Coldkey TAO: {}", coldkey_tao); |
| 92 | + |
| 93 | + // Distribute the TAO to the coldkey |
| 94 | + let as_tao: u64 = coldkey_tao.saturating_to_num::<u64>(); |
| 95 | + let as_alpha: u64 = coldkey_alpha.saturating_to_num::<u64>(); |
| 96 | + |
| 97 | + if as_tao > 0 { |
| 98 | + Pallet::<T>::add_balance_to_coldkey_account(&coldkey, as_tao); |
| 99 | + weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1)); |
| 100 | + |
| 101 | + // Emit event |
| 102 | + Pallet::<T>::deposit_event(Event::StakeRemoved( |
| 103 | + coldkey.clone(), |
| 104 | + hotkey.clone(), |
| 105 | + as_tao, |
| 106 | + as_alpha, |
| 107 | + this_netuid, |
| 108 | + )); |
| 109 | + } |
| 110 | + } |
| 111 | + // Clear coldkeys |
| 112 | + for coldkey in coldkeys_to_remove { |
| 113 | + Alpha::<T>::remove((&hotkey, coldkey, this_netuid)); |
| 114 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // === Clear storage entries === |
| 119 | + // Clear subnet owner and hotkey |
| 120 | + SubnetOwner::<T>::remove(this_netuid); |
| 121 | + SubnetOwnerHotkey::<T>::remove(this_netuid); |
| 122 | + weight = weight.saturating_add(T::DbWeight::get().writes(2)); |
| 123 | + |
| 124 | + // Clear hotkeys |
| 125 | + for hotkey in hotkeys_to_remove { |
| 126 | + TotalHotkeyAlpha::<T>::remove(hotkey.clone(), this_netuid); |
| 127 | + TotalHotkeyShares::<T>::remove(hotkey.clone(), this_netuid); |
| 128 | + weight = weight.saturating_add(T::DbWeight::get().writes(2)); |
| 129 | + } |
| 130 | + |
| 131 | + // Clear pool |
| 132 | + SubnetTAO::<T>::remove(this_netuid); |
| 133 | + SubnetAlphaIn::<T>::remove(this_netuid); |
| 134 | + weight = weight.saturating_add(T::DbWeight::get().writes(2)); |
| 135 | + |
| 136 | + // Clear AlphaOut |
| 137 | + SubnetAlphaOut::<T>::remove(this_netuid); |
| 138 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 139 | + |
| 140 | + // Clear pending emissions |
| 141 | + SubnetTaoInEmission::<T>::remove(this_netuid); |
| 142 | + SubnetAlphaInEmission::<T>::remove(this_netuid); |
| 143 | + SubnetAlphaOutEmission::<T>::remove(this_netuid); |
| 144 | + PendingEmission::<T>::remove(this_netuid); |
| 145 | + PendingRootDivs::<T>::remove(this_netuid); |
| 146 | + PendingAlphaSwapped::<T>::remove(this_netuid); |
| 147 | + PendingOwnerCut::<T>::remove(this_netuid); |
| 148 | + weight = weight.saturating_add(T::DbWeight::get().writes(7)); |
| 149 | + |
| 150 | + // Clear trackers |
| 151 | + let clear_results_0 = |
| 152 | + AlphaDividendsPerSubnet::<T>::clear_prefix(this_netuid, u32::MAX, None); |
| 153 | + weight = weight.saturating_add(T::DbWeight::get().writes(clear_results_0.unique.into())); |
| 154 | + let clear_results_1 = TaoDividendsPerSubnet::<T>::clear_prefix(this_netuid, u32::MAX, None); |
| 155 | + weight = weight.saturating_add(T::DbWeight::get().writes(clear_results_1.unique.into())); |
| 156 | + |
| 157 | + // Adjust total stake |
| 158 | + TotalStake::<T>::mutate(|total| { |
| 159 | + *total = total.saturating_sub(subnet_tao.saturating_to_num::<u64>()); |
| 160 | + }); |
| 161 | + weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1)); |
| 162 | + |
| 163 | + // Clear subnet volume |
| 164 | + SubnetVolume::<T>::remove(this_netuid); |
| 165 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 166 | + |
| 167 | + // Clear child keys |
| 168 | + let clear_results_2 = PendingChildKeys::<T>::clear_prefix(this_netuid, u32::MAX, None); |
| 169 | + weight = weight.saturating_add(T::DbWeight::get().writes(clear_results_2.unique.into())); |
| 170 | + |
| 171 | + let mut childkeys_to_remove: Vec<T::AccountId> = Vec::new(); |
| 172 | + for (childkey, netuid_i, _parents) in ParentKeys::<T>::iter() { |
| 173 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 174 | + if netuid_i != this_netuid { |
| 175 | + continue; |
| 176 | + } |
| 177 | + |
| 178 | + childkeys_to_remove.push(childkey); |
| 179 | + } |
| 180 | + |
| 181 | + let mut parent_keys_to_remove: Vec<T::AccountId> = Vec::new(); |
| 182 | + for (parent_key, netuid_i, _children) in ChildKeys::<T>::iter() { |
| 183 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 184 | + if netuid_i != this_netuid { |
| 185 | + continue; |
| 186 | + } |
| 187 | + |
| 188 | + parent_keys_to_remove.push(parent_key); |
| 189 | + } |
| 190 | + |
| 191 | + for child_key in childkeys_to_remove { |
| 192 | + ParentKeys::<T>::remove(child_key, this_netuid); |
| 193 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 194 | + } |
| 195 | + |
| 196 | + for parent_key in parent_keys_to_remove { |
| 197 | + ChildKeys::<T>::remove(parent_key, this_netuid); |
| 198 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 199 | + } |
| 200 | + |
| 201 | + // Clear reg allowed maps |
| 202 | + NetworkRegistrationAllowed::<T>::remove(this_netuid); |
| 203 | + NetworkPowRegistrationAllowed::<T>::remove(this_netuid); |
| 204 | + weight = weight.saturating_add(T::DbWeight::get().writes(2)); |
| 205 | + // ======== End Migration Logic ======== |
| 206 | + } |
| 207 | + |
| 208 | + // Mark the migration as completed |
| 209 | + HasMigrationRun::<T>::insert(&migration_name, true); |
| 210 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 211 | + |
| 212 | + log::info!( |
| 213 | + "Migration '{:?}' completed.", |
| 214 | + String::from_utf8_lossy(&migration_name) |
| 215 | + ); |
| 216 | + |
| 217 | + // Return the migration weight. |
| 218 | + weight |
| 219 | +} |
0 commit comments