11use super :: * ;
2- use sp_std:: collections:: { btree_map:: BTreeMap , btree_set:: BTreeSet } ;
3-
4- use subtensor_swap_interface:: SwapHandler ;
52
63pub fn get_unactive_sn_netuids < T : Config > (
74 pool_initial_alpha : AlphaCurrency ,
@@ -51,37 +48,6 @@ pub fn migrate_reset_unactive_sn<T: Config>() -> Weight {
5148 let ( unactive_netuids, w) = get_unactive_sn_netuids :: < T > ( pool_initial_alpha) ;
5249 weight = weight. saturating_add ( w) ;
5350
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-
8551 for netuid in unactive_netuids. iter ( ) {
8652 // Reset the subnet as it shouldn't have any emissions
8753 PendingServerEmission :: < T > :: remove ( * netuid) ;
@@ -92,91 +58,8 @@ pub fn migrate_reset_unactive_sn<T: Config>() -> Weight {
9258 SubnetAlphaInEmission :: < T > :: remove ( * netuid) ;
9359 SubnetAlphaOutEmission :: < T > :: remove ( * netuid) ;
9460 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- }
17561 }
17662
177- // Run total issuance migration
178- crate :: migrations:: migrate_init_total_issuance:: migrate_init_total_issuance :: < T > ( ) ;
179-
18063 // Mark Migration as Completed
18164 HasMigrationRun :: < T > :: insert ( & migration_name, true ) ;
18265 weight = weight. saturating_add ( T :: DbWeight :: get ( ) . writes ( 1 ) ) ;
0 commit comments