|
| 1 | +use super::*; |
| 2 | +use sp_std::collections::{btree_map::BTreeMap, btree_set::BTreeSet}; |
| 3 | + |
| 4 | +use subtensor_swap_interface::SwapHandler; |
| 5 | + |
| 6 | +pub fn get_unactive_sn_netuids<T: Config>( |
| 7 | + pool_initial_alpha: AlphaCurrency, |
| 8 | +) -> (Vec<NetUid>, Weight) { |
| 9 | + // Loop over all subnets, if the AlphaIssuance is > pool_initial_alpha |
| 10 | + // but FirstEmissionBlockNumber is None |
| 11 | + // then this subnet should be reset |
| 12 | + let mut weight = T::DbWeight::get().reads(1); |
| 13 | + let unactive_netuids = Pallet::<T>::get_all_subnet_netuids() |
| 14 | + .iter() |
| 15 | + .filter(|&netuid| !netuid.is_root()) |
| 16 | + .filter(|&netuid| { |
| 17 | + let alpha_issuance = Pallet::<T>::get_alpha_issuance(*netuid); |
| 18 | + let first_emission_block_number = FirstEmissionBlockNumber::<T>::get(*netuid); |
| 19 | + alpha_issuance != pool_initial_alpha && first_emission_block_number.is_none() |
| 20 | + }) |
| 21 | + .copied() |
| 22 | + .collect::<Vec<_>>(); |
| 23 | + weight = weight |
| 24 | + .saturating_add(T::DbWeight::get().reads(unactive_netuids.len().saturating_mul(3) as u64)); |
| 25 | + |
| 26 | + (unactive_netuids, weight) |
| 27 | +} |
| 28 | + |
| 29 | +pub fn migrate_reset_unactive_sn<T: Config>() -> Weight { |
| 30 | + let migration_name = b"migrate_reset_unactive_sn".to_vec(); |
| 31 | + let mut weight: Weight = T::DbWeight::get().reads(1); |
| 32 | + |
| 33 | + // Skip if already executed |
| 34 | + if HasMigrationRun::<T>::get(&migration_name) { |
| 35 | + log::info!( |
| 36 | + target: "runtime", |
| 37 | + "Migration '{}' already run - skipping.", |
| 38 | + String::from_utf8_lossy(&migration_name) |
| 39 | + ); |
| 40 | + return weight; |
| 41 | + } |
| 42 | + log::info!( |
| 43 | + "Running migration '{}'", |
| 44 | + String::from_utf8_lossy(&migration_name) |
| 45 | + ); |
| 46 | + |
| 47 | + // From init_new_network |
| 48 | + let pool_initial_tao: TaoCurrency = Pallet::<T>::get_network_min_lock(); |
| 49 | + let pool_initial_alpha: AlphaCurrency = pool_initial_tao.to_u64().into(); |
| 50 | + |
| 51 | + let (unactive_netuids, w) = get_unactive_sn_netuids::<T>(pool_initial_alpha); |
| 52 | + weight = weight.saturating_add(w); |
| 53 | + |
| 54 | + // Collect the hotkeys to remove for each subnet |
| 55 | + let mut to_remove_alpha_hotkeys: BTreeMap<NetUid, Vec<T::AccountId>> = BTreeMap::new(); |
| 56 | + let mut to_remove_alpha_coldkeys: BTreeMap<NetUid, Vec<(T::AccountId, T::AccountId)>> = |
| 57 | + BTreeMap::new(); |
| 58 | + let mut all_hotkeys_set = BTreeSet::new(); |
| 59 | + for (hotkey, netuid_i, _) in TotalHotkeyAlpha::<T>::iter() { |
| 60 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 61 | + if unactive_netuids.contains(&netuid_i) { |
| 62 | + // Only for unactive subnets |
| 63 | + to_remove_alpha_hotkeys |
| 64 | + .entry(netuid_i) |
| 65 | + .or_insert(Vec::new()) |
| 66 | + .push(hotkey.clone()); |
| 67 | + all_hotkeys_set.insert(hotkey); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Collect the coldkeys to remove for each subnet |
| 72 | + for hotkey in all_hotkeys_set.iter() { |
| 73 | + for ((coldkey, netuid_i), _) in Alpha::<T>::iter_prefix((&hotkey,)) { |
| 74 | + weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
| 75 | + if unactive_netuids.contains(&netuid_i) { |
| 76 | + // Only for unactive subnets |
| 77 | + to_remove_alpha_coldkeys |
| 78 | + .entry(netuid_i) |
| 79 | + .or_insert(Vec::new()) |
| 80 | + .push((hotkey.clone(), coldkey)); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + for netuid in unactive_netuids.iter() { |
| 86 | + // Reset the subnet as it shouldn't have any emissions |
| 87 | + PendingServerEmission::<T>::remove(*netuid); |
| 88 | + PendingValidatorEmission::<T>::remove(*netuid); |
| 89 | + PendingRootAlphaDivs::<T>::remove(*netuid); |
| 90 | + PendingOwnerCut::<T>::remove(*netuid); |
| 91 | + SubnetTaoInEmission::<T>::remove(*netuid); |
| 92 | + SubnetAlphaInEmission::<T>::remove(*netuid); |
| 93 | + SubnetAlphaOutEmission::<T>::remove(*netuid); |
| 94 | + weight = weight.saturating_add(T::DbWeight::get().writes(7)); |
| 95 | + |
| 96 | + // Reset v3 pool |
| 97 | + let burned_tao = match T::SwapInterface::clear_protocol_liquidity(*netuid) { |
| 98 | + Ok((_tao, fee_tao, _alpha, _fee_alpha)) => fee_tao, |
| 99 | + Err(e) => { |
| 100 | + log::error!("Failed to clear protocol liquidity for netuid {netuid:?}: {e:?}"); |
| 101 | + TaoCurrency::ZERO |
| 102 | + } |
| 103 | + }; |
| 104 | + Pallet::<T>::recycle_tao(burned_tao); |
| 105 | + // might be based on ticks but this is a rough estimate |
| 106 | + weight = weight.saturating_add(T::DbWeight::get().reads_writes(6, 14)); |
| 107 | + |
| 108 | + // Recycle already emitted TAO |
| 109 | + // or mint 1 TAO to the pool |
| 110 | + let subnet_tao = SubnetTAO::<T>::get(*netuid); |
| 111 | + if subnet_tao > pool_initial_tao { |
| 112 | + let tao_to_recycle = subnet_tao.saturating_sub(pool_initial_tao); |
| 113 | + Pallet::<T>::recycle_tao(tao_to_recycle); |
| 114 | + TotalStake::<T>::mutate(|total| { |
| 115 | + *total = total.saturating_sub(tao_to_recycle); |
| 116 | + }); |
| 117 | + SubnetTAO::<T>::mutate(*netuid, |amount| { |
| 118 | + *amount = amount.saturating_sub(tao_to_recycle); |
| 119 | + }); |
| 120 | + weight = weight.saturating_add(T::DbWeight::get().reads_writes(3, 3)); |
| 121 | + } else if subnet_tao < pool_initial_tao { |
| 122 | + let tao_to_add = pool_initial_tao.saturating_sub(subnet_tao); |
| 123 | + TotalStake::<T>::mutate(|total| { |
| 124 | + *total = total.saturating_add(tao_to_add); |
| 125 | + }); |
| 126 | + SubnetTAO::<T>::mutate(*netuid, |amount| { |
| 127 | + *amount = amount.saturating_add(tao_to_add); |
| 128 | + }); |
| 129 | + TotalIssuance::<T>::mutate(|total| { |
| 130 | + *total = total.saturating_add(tao_to_add); |
| 131 | + }); |
| 132 | + weight = weight.saturating_add(T::DbWeight::get().reads_writes(3, 3)); |
| 133 | + } |
| 134 | + |
| 135 | + // Reset pool alpha |
| 136 | + SubnetAlphaIn::<T>::insert(*netuid, pool_initial_alpha); |
| 137 | + SubnetAlphaOut::<T>::insert(*netuid, AlphaCurrency::ZERO); |
| 138 | + // Reset volume |
| 139 | + SubnetVolume::<T>::insert(*netuid, 0u128); |
| 140 | + weight = weight.saturating_add(T::DbWeight::get().writes(4)); |
| 141 | + |
| 142 | + // Reset Alpha stake entries for this subnet |
| 143 | + let to_reset: Vec<T::AccountId> = match to_remove_alpha_hotkeys.get(netuid) { |
| 144 | + Some(hotkeys) => hotkeys.clone(), |
| 145 | + None => Vec::new(), |
| 146 | + }; |
| 147 | + |
| 148 | + for hotkey in to_reset { |
| 149 | + TotalHotkeyAlpha::<T>::remove(&hotkey, *netuid); |
| 150 | + TotalHotkeyShares::<T>::remove(&hotkey, *netuid); |
| 151 | + TotalHotkeyAlphaLastEpoch::<T>::remove(&hotkey, *netuid); |
| 152 | + weight = weight.saturating_add(T::DbWeight::get().writes(3)); |
| 153 | + |
| 154 | + // Reset root claimable and claimed |
| 155 | + RootClaimable::<T>::mutate(&hotkey, |claimable| { |
| 156 | + claimable.remove(netuid); |
| 157 | + }); |
| 158 | + weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1)); |
| 159 | + let removal_result = RootClaimed::<T>::clear_prefix((*netuid, &hotkey), u32::MAX, None); |
| 160 | + weight = weight.saturating_add( |
| 161 | + T::DbWeight::get() |
| 162 | + .reads_writes(removal_result.loops as u64, removal_result.backend as u64), |
| 163 | + ); |
| 164 | + |
| 165 | + let to_reset_alpha: Vec<(T::AccountId, T::AccountId)> = |
| 166 | + match to_remove_alpha_coldkeys.get(netuid) { |
| 167 | + Some(coldkeys) => coldkeys.clone(), |
| 168 | + None => Vec::new(), |
| 169 | + }; |
| 170 | + for (hotkey, coldkey) in to_reset_alpha { |
| 171 | + Alpha::<T>::remove((hotkey, coldkey, netuid)); |
| 172 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + // Run total issuance migration |
| 178 | + crate::migrations::migrate_init_total_issuance::migrate_init_total_issuance::<T>(); |
| 179 | + |
| 180 | + // Mark Migration as Completed |
| 181 | + HasMigrationRun::<T>::insert(&migration_name, true); |
| 182 | + weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
| 183 | + |
| 184 | + log::info!( |
| 185 | + "Migration '{:?}' completed successfully.", |
| 186 | + String::from_utf8_lossy(&migration_name) |
| 187 | + ); |
| 188 | + |
| 189 | + weight |
| 190 | +} |
0 commit comments