Skip to content

Commit c5e0aef

Browse files
committed
check for new reg before running migration
1 parent 0dc9f2d commit c5e0aef

File tree

1 file changed

+153
-147
lines changed

1 file changed

+153
-147
lines changed

pallets/subtensor/src/migrations/migrate_dissolve_sn73.rs

Lines changed: 153 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -25,180 +25,186 @@ pub fn migrate_dissolve_sn73<T: Config>() -> Weight {
2525
String::from_utf8_lossy(&migration_name)
2626
);
2727

28-
// ======== Migration Logic ========
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);
2938

30-
// Get the subnet TAO
31-
let subnet_tao = I96F32::from_num(SubnetTAO::<T>::get(this_netuid));
32-
weight = weight.saturating_add(T::DbWeight::get().reads(1));
33-
log::debug!("Subnet TAO: {}", subnet_tao);
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+
}
3447

35-
let mut total_alpha: I96F32 = I96F32::from_num(0);
36-
// Iterate over every hotkey and sum up the total alpha
37-
let mut hotkeys_to_remove: Vec<T::AccountId> = Vec::new();
38-
for (hotkey, netuid_i, total_hotkey_alpha) in TotalHotkeyAlpha::<T>::iter() {
39-
weight = weight.saturating_add(T::DbWeight::get().reads(1));
40-
if netuid_i != this_netuid {
41-
continue;
48+
hotkeys_to_remove.push(hotkey);
49+
total_alpha = total_alpha.saturating_add(I96F32::from_num(total_hotkey_alpha));
4250
}
51+
log::debug!("Total alpha: {}", total_alpha);
4352

44-
hotkeys_to_remove.push(hotkey);
45-
total_alpha = total_alpha.saturating_add(I96F32::from_num(total_hotkey_alpha));
46-
}
47-
log::debug!("Total alpha: {}", total_alpha);
48-
49-
// Iterate over every hotkey and distribute the TAO from the pool
50-
// using previous total alpha as the denominator
51-
for hotkey in hotkeys_to_remove.iter() {
52-
log::debug!("Hotkey: {:?}", hotkey.clone());
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());
5357

54-
let total_hotkey_alpha_i = TotalHotkeyAlpha::<T>::get(hotkey.clone(), this_netuid);
55-
let total_hotkey_alpha = I96F32::from_num(total_hotkey_alpha_i);
56-
weight = weight.saturating_add(T::DbWeight::get().reads(1));
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));
5761

58-
// Get the total hotkey shares
59-
let total_hotkey_shares =
60-
I96F32::from_num(TotalHotkeyShares::<T>::get(hotkey.clone(), this_netuid));
61-
weight = weight.saturating_add(T::DbWeight::get().reads(1));
62-
log::debug!("Total hotkey shares: {}", total_hotkey_shares);
63-
64-
// Get the equivalent amount of TAO
65-
let hotkey_tao: I96F32 = total_hotkey_alpha
66-
.saturating_div(total_alpha)
67-
.saturating_mul(subnet_tao);
68-
log::debug!("Total hotkey alpha: {}", total_hotkey_alpha);
69-
log::debug!("Hotkey TAO: {}", hotkey_tao);
70-
71-
let mut coldkeys_to_remove: Vec<T::AccountId> = Vec::new();
72-
// Distribute the TAO to each of the stakers to the hotkey
73-
for ((coldkey, netuid_i), alpha_i) in Alpha::<T>::iter_prefix((&hotkey,)) {
62+
// Get the total hotkey shares
63+
let total_hotkey_shares =
64+
I96F32::from_num(TotalHotkeyShares::<T>::get(hotkey.clone(), this_netuid));
7465
weight = weight.saturating_add(T::DbWeight::get().reads(1));
75-
if netuid_i != this_netuid {
76-
continue;
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+
}
77110
}
78-
79-
coldkeys_to_remove.push(coldkey.clone());
80-
81-
let alpha_shares = I96F32::from_num(alpha_i);
82-
let coldkey_share: I96F32 = alpha_shares.saturating_div(total_hotkey_shares);
83-
let coldkey_tao = coldkey_share.saturating_mul(hotkey_tao);
84-
let coldkey_alpha = coldkey_share.saturating_mul(total_hotkey_alpha);
85-
log::debug!("Alpha shares: {}", alpha_shares);
86-
log::debug!("Coldkey share: {}", coldkey_share);
87-
log::debug!("Coldkey TAO: {}", coldkey_tao);
88-
89-
// Distribute the TAO to the coldkey
90-
let as_tao: u64 = coldkey_tao.saturating_to_num::<u64>();
91-
let as_alpha: u64 = coldkey_alpha.saturating_to_num::<u64>();
92-
93-
if as_tao > 0 {
94-
Pallet::<T>::add_balance_to_coldkey_account(&coldkey, as_tao);
95-
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
96-
97-
// Emit event
98-
Pallet::<T>::deposit_event(Event::StakeRemoved(
99-
coldkey.clone(),
100-
hotkey.clone(),
101-
as_tao,
102-
as_alpha,
103-
this_netuid,
104-
));
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));
105115
}
106116
}
107-
// Clear coldkeys
108-
for coldkey in coldkeys_to_remove {
109-
Alpha::<T>::remove((&hotkey, coldkey, this_netuid));
110-
weight = weight.saturating_add(T::DbWeight::get().writes(1));
111-
}
112-
}
113117

114-
// === Clear storage entries ===
115-
// Clear subnet owner and hotkey
116-
SubnetOwner::<T>::remove(this_netuid);
117-
SubnetOwnerHotkey::<T>::remove(this_netuid);
118-
weight = weight.saturating_add(T::DbWeight::get().writes(2));
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));
119123

120-
// Clear hotkeys
121-
for hotkey in hotkeys_to_remove {
122-
TotalHotkeyAlpha::<T>::remove(hotkey.clone(), this_netuid);
123-
TotalHotkeyShares::<T>::remove(hotkey.clone(), this_netuid);
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);
124134
weight = weight.saturating_add(T::DbWeight::get().writes(2));
125-
}
126135

127-
// Clear pool
128-
SubnetTAO::<T>::remove(this_netuid);
129-
SubnetAlphaIn::<T>::remove(this_netuid);
130-
weight = weight.saturating_add(T::DbWeight::get().writes(2));
136+
// Clear AlphaOut
137+
SubnetAlphaOut::<T>::remove(this_netuid);
138+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
131139

132-
// Clear AlphaOut
133-
SubnetAlphaOut::<T>::remove(this_netuid);
134-
weight = weight.saturating_add(T::DbWeight::get().writes(1));
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));
135166

136-
// Clear pending emissions
137-
SubnetTaoInEmission::<T>::remove(this_netuid);
138-
SubnetAlphaInEmission::<T>::remove(this_netuid);
139-
SubnetAlphaOutEmission::<T>::remove(this_netuid);
140-
PendingEmission::<T>::remove(this_netuid);
141-
PendingRootDivs::<T>::remove(this_netuid);
142-
PendingAlphaSwapped::<T>::remove(this_netuid);
143-
PendingOwnerCut::<T>::remove(this_netuid);
144-
weight = weight.saturating_add(T::DbWeight::get().writes(7));
145-
146-
// Clear trackers
147-
let clear_results_0 = AlphaDividendsPerSubnet::<T>::clear_prefix(this_netuid, u32::MAX, None);
148-
weight = weight.saturating_add(T::DbWeight::get().writes(clear_results_0.unique.into()));
149-
let clear_results_1 = TaoDividendsPerSubnet::<T>::clear_prefix(this_netuid, u32::MAX, None);
150-
weight = weight.saturating_add(T::DbWeight::get().writes(clear_results_1.unique.into()));
151-
152-
// Adjust total stake
153-
TotalStake::<T>::mutate(|total| {
154-
*total = total.saturating_sub(subnet_tao.saturating_to_num::<u64>());
155-
});
156-
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
157-
158-
// Clear subnet volume
159-
SubnetVolume::<T>::remove(this_netuid);
160-
weight = weight.saturating_add(T::DbWeight::get().writes(1));
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()));
161170

162-
// Clear child keys
163-
let clear_results_2 = PendingChildKeys::<T>::clear_prefix(this_netuid, u32::MAX, None);
164-
weight = weight.saturating_add(T::DbWeight::get().writes(clear_results_2.unique.into()));
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+
}
165177

166-
let mut childkeys_to_remove: Vec<T::AccountId> = Vec::new();
167-
for (childkey, netuid_i, _parents) in ParentKeys::<T>::iter() {
168-
weight = weight.saturating_add(T::DbWeight::get().reads(1));
169-
if netuid_i != this_netuid {
170-
continue;
178+
childkeys_to_remove.push(childkey);
171179
}
172180

173-
childkeys_to_remove.push(childkey);
174-
}
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+
}
175187

176-
let mut parent_keys_to_remove: Vec<T::AccountId> = Vec::new();
177-
for (parent_key, netuid_i, _children) in ChildKeys::<T>::iter() {
178-
weight = weight.saturating_add(T::DbWeight::get().reads(1));
179-
if netuid_i != this_netuid {
180-
continue;
188+
parent_keys_to_remove.push(parent_key);
181189
}
182190

183-
parent_keys_to_remove.push(parent_key);
184-
}
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+
}
185195

186-
for child_key in childkeys_to_remove {
187-
ParentKeys::<T>::remove(child_key, this_netuid);
188-
weight = weight.saturating_add(T::DbWeight::get().writes(1));
189-
}
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+
}
190200

191-
for parent_key in parent_keys_to_remove {
192-
ChildKeys::<T>::remove(parent_key, this_netuid);
193-
weight = weight.saturating_add(T::DbWeight::get().writes(1));
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 ========
194206
}
195207

196-
// Clear reg allowed maps
197-
NetworkRegistrationAllowed::<T>::remove(this_netuid);
198-
NetworkPowRegistrationAllowed::<T>::remove(this_netuid);
199-
weight = weight.saturating_add(T::DbWeight::get().writes(2));
200-
// ======== End Migration Logic ========
201-
202208
// Mark the migration as completed
203209
HasMigrationRun::<T>::insert(&migration_name, true);
204210
weight = weight.saturating_add(T::DbWeight::get().writes(1));

0 commit comments

Comments
 (0)