Skip to content

Commit cbe1912

Browse files
authored
Merge pull request #1418 from opentensor/feat/sn-73-dissolve-migration
Feat/sn 73 dissolve migration
2 parents dc2b0eb + 1e40637 commit cbe1912

File tree

7 files changed

+912
-274
lines changed

7 files changed

+912
-274
lines changed

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,40 +1389,42 @@ mod dispatches {
13891389
.saturating_add(T::DbWeight::get().reads(6))
13901390
.saturating_add(T::DbWeight::get().writes(31)), DispatchClass::Operational, Pays::Yes))]
13911391
pub fn schedule_dissolve_network(
1392-
origin: OriginFor<T>,
1393-
netuid: u16,
1392+
_origin: OriginFor<T>,
1393+
_netuid: u16,
13941394
) -> DispatchResultWithPostInfo {
1395-
let who = ensure_signed(origin)?;
1396-
1397-
let current_block: BlockNumberFor<T> = <frame_system::Pallet<T>>::block_number();
1398-
let duration: BlockNumberFor<T> = DissolveNetworkScheduleDuration::<T>::get();
1399-
let when: BlockNumberFor<T> = current_block.saturating_add(duration);
1400-
1401-
let call = Call::<T>::dissolve_network {
1402-
coldkey: who.clone(),
1403-
netuid,
1404-
};
1405-
1406-
let bound_call = T::Preimages::bound(LocalCallOf::<T>::from(call.clone()))
1407-
.map_err(|_| Error::<T>::FailedToSchedule)?;
1408-
1409-
T::Scheduler::schedule(
1410-
DispatchTime::At(when),
1411-
None,
1412-
63,
1413-
frame_system::RawOrigin::Root.into(),
1414-
bound_call,
1415-
)
1416-
.map_err(|_| Error::<T>::FailedToSchedule)?;
1417-
1418-
// Emit the SwapScheduled event
1419-
Self::deposit_event(Event::DissolveNetworkScheduled {
1420-
account: who.clone(),
1421-
netuid,
1422-
execution_block: when,
1423-
});
1424-
1425-
Ok(().into())
1395+
Err(Error::<T>::CallDisabled.into())
1396+
1397+
// let who = ensure_signed(origin)?;
1398+
1399+
// let current_block: BlockNumberFor<T> = <frame_system::Pallet<T>>::block_number();
1400+
// let duration: BlockNumberFor<T> = DissolveNetworkScheduleDuration::<T>::get();
1401+
// let when: BlockNumberFor<T> = current_block.saturating_add(duration);
1402+
1403+
// let call = Call::<T>::dissolve_network {
1404+
// coldkey: who.clone(),
1405+
// netuid,
1406+
// };
1407+
1408+
// let bound_call = T::Preimages::bound(LocalCallOf::<T>::from(call.clone()))
1409+
// .map_err(|_| Error::<T>::FailedToSchedule)?;
1410+
1411+
// T::Scheduler::schedule(
1412+
// DispatchTime::At(when),
1413+
// None,
1414+
// 63,
1415+
// frame_system::RawOrigin::Root.into(),
1416+
// bound_call,
1417+
// )
1418+
// .map_err(|_| Error::<T>::FailedToSchedule)?;
1419+
1420+
// // Emit the SwapScheduled event
1421+
// Self::deposit_event(Event::DissolveNetworkScheduled {
1422+
// account: who.clone(),
1423+
// netuid,
1424+
// execution_block: when,
1425+
// });
1426+
1427+
// Ok(().into())
14261428
}
14271429

14281430
/// ---- Set prometheus information for the neuron.

pallets/subtensor/src/macros/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,7 @@ mod errors {
193193
TransferDisallowed,
194194
/// Activity cutoff is being set too low.
195195
ActivityCutoffTooLow,
196+
/// Call is disabled
197+
CallDisabled,
196198
}
197199
}

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ mod hooks {
8181
// Remove Stake map entries
8282
.saturating_add(migrations::migrate_remove_stake_map::migrate_remove_stake_map::<T>())
8383
// Remove unused maps entries
84-
.saturating_add(migrations::migrate_remove_unused_maps_and_values::migrate_remove_unused_maps_and_values::<T>());
84+
.saturating_add(migrations::migrate_remove_unused_maps_and_values::migrate_remove_unused_maps_and_values::<T>())
85+
// Migrate dissolve sn73
86+
.saturating_add(migrations::migrate_dissolve_sn73::migrate_dissolve_sn73::<T>());
8587
weight
8688
}
8789

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
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+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod migrate_commit_reveal_v2;
44
pub mod migrate_create_root_network;
55
pub mod migrate_delete_subnet_21;
66
pub mod migrate_delete_subnet_3;
7+
pub mod migrate_dissolve_sn73;
78
pub mod migrate_fix_is_network_member;
89
pub mod migrate_identities_v2;
910
pub mod migrate_init_total_issuance;

0 commit comments

Comments
 (0)