|
1 | 1 | use super::*; |
| 2 | +use sp_std::collections::{btree_map::BTreeMap, btree_set::BTreeSet}; |
2 | 3 |
|
3 | 4 | use subtensor_swap_interface::SwapHandler; |
4 | 5 |
|
@@ -50,6 +51,37 @@ pub fn migrate_reset_unactive_sn<T: Config>() -> Weight { |
50 | 51 | let (unactive_netuids, w) = get_unactive_sn_netuids::<T>(pool_initial_alpha); |
51 | 52 | weight = weight.saturating_add(w); |
52 | 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 | + |
53 | 85 | for netuid in unactive_netuids.iter() { |
54 | 86 | // Reset the subnet as it shouldn't have any emissions |
55 | 87 | PendingServerEmission::<T>::remove(*netuid); |
@@ -89,47 +121,35 @@ pub fn migrate_reset_unactive_sn<T: Config>() -> Weight { |
89 | 121 | weight = weight.saturating_add(T::DbWeight::get().reads_writes(6, 14)); |
90 | 122 |
|
91 | 123 | // Reset Alpha stake entries for this subnet |
92 | | - let mut to_reset = Vec::new(); |
93 | | - for (hotkey, netuid_, alpha) in TotalHotkeyAlpha::<T>::iter() { |
94 | | - weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
95 | | - if netuid_ != *netuid { |
96 | | - // skip netuids that are not the subnet we are resetting |
97 | | - continue; |
98 | | - } |
99 | | - |
100 | | - if alpha > AlphaCurrency::from(0) { |
101 | | - to_reset.push((hotkey, netuid, alpha)); |
102 | | - } |
103 | | - } |
104 | | - |
105 | | - for (hotkey, netuid_i, _) in to_reset { |
106 | | - TotalHotkeyAlpha::<T>::remove(&hotkey, netuid_i); |
107 | | - TotalHotkeyShares::<T>::remove(&hotkey, netuid_i); |
108 | | - TotalHotkeyAlphaLastEpoch::<T>::remove(&hotkey, netuid_i); |
| 124 | + let to_reset: Vec<T::AccountId> = match to_remove_alpha_hotkeys.get(netuid) { |
| 125 | + Some(hotkeys) => hotkeys.clone(), |
| 126 | + None => Vec::new(), |
| 127 | + }; |
| 128 | + |
| 129 | + for hotkey in to_reset { |
| 130 | + TotalHotkeyAlpha::<T>::remove(&hotkey, *netuid); |
| 131 | + TotalHotkeyShares::<T>::remove(&hotkey, *netuid); |
| 132 | + TotalHotkeyAlphaLastEpoch::<T>::remove(&hotkey, *netuid); |
109 | 133 | weight = weight.saturating_add(T::DbWeight::get().writes(3)); |
110 | 134 |
|
111 | 135 | // Reset root claimable and claimed |
112 | 136 | RootClaimable::<T>::mutate(&hotkey, |claimable| { |
113 | | - claimable.remove(netuid_i); |
| 137 | + claimable.remove(netuid); |
114 | 138 | }); |
115 | 139 | weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1)); |
116 | | - let removal_result = |
117 | | - RootClaimed::<T>::clear_prefix((netuid_i, &hotkey), u32::MAX, None); |
| 140 | + let removal_result = RootClaimed::<T>::clear_prefix((*netuid, &hotkey), u32::MAX, None); |
118 | 141 | weight = weight.saturating_add( |
119 | 142 | T::DbWeight::get() |
120 | 143 | .reads_writes(removal_result.loops as u64, removal_result.backend as u64), |
121 | 144 | ); |
122 | 145 |
|
123 | | - let mut to_reset_alpha: Vec<(&T::AccountId, T::AccountId, NetUid)> = Vec::new(); |
124 | | - for ((coldkey, netuid_j), _) in Alpha::<T>::iter_prefix((&hotkey,)) { |
125 | | - weight = weight.saturating_add(T::DbWeight::get().reads(1)); |
126 | | - if netuid_j != *netuid_i { |
127 | | - continue; |
128 | | - } |
129 | | - to_reset_alpha.push((&hotkey, coldkey, netuid_j)); |
130 | | - } |
131 | | - for (hotkey, coldkey, netuid_j) in to_reset_alpha { |
132 | | - Alpha::<T>::remove((hotkey, coldkey, netuid_j)); |
| 146 | + let to_reset_alpha: Vec<(T::AccountId, T::AccountId)> = |
| 147 | + match to_remove_alpha_coldkeys.get(netuid) { |
| 148 | + Some(coldkeys) => coldkeys.clone(), |
| 149 | + None => Vec::new(), |
| 150 | + }; |
| 151 | + for (hotkey, coldkey) in to_reset_alpha { |
| 152 | + Alpha::<T>::remove((hotkey, coldkey, netuid)); |
133 | 153 | weight = weight.saturating_add(T::DbWeight::get().writes(1)); |
134 | 154 | } |
135 | 155 | } |
|
0 commit comments