Skip to content

Commit 6c1b39f

Browse files
committed
optimize migration
1 parent 79f5dd9 commit 6c1b39f

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

pallets/subtensor/src/migrations/migrate_reset_unactive_sn.rs

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
use sp_std::collections::{btree_map::BTreeMap, btree_set::BTreeSet};
23

34
use subtensor_swap_interface::SwapHandler;
45

@@ -50,6 +51,37 @@ pub fn migrate_reset_unactive_sn<T: Config>() -> Weight {
5051
let (unactive_netuids, w) = get_unactive_sn_netuids::<T>(pool_initial_alpha);
5152
weight = weight.saturating_add(w);
5253

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+
5385
for netuid in unactive_netuids.iter() {
5486
// Reset the subnet as it shouldn't have any emissions
5587
PendingServerEmission::<T>::remove(*netuid);
@@ -89,47 +121,35 @@ pub fn migrate_reset_unactive_sn<T: Config>() -> Weight {
89121
weight = weight.saturating_add(T::DbWeight::get().reads_writes(6, 14));
90122

91123
// 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);
109133
weight = weight.saturating_add(T::DbWeight::get().writes(3));
110134

111135
// Reset root claimable and claimed
112136
RootClaimable::<T>::mutate(&hotkey, |claimable| {
113-
claimable.remove(netuid_i);
137+
claimable.remove(netuid);
114138
});
115139
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);
118141
weight = weight.saturating_add(
119142
T::DbWeight::get()
120143
.reads_writes(removal_result.loops as u64, removal_result.backend as u64),
121144
);
122145

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));
133153
weight = weight.saturating_add(T::DbWeight::get().writes(1));
134154
}
135155
}

0 commit comments

Comments
 (0)