@@ -139,8 +139,12 @@ impl<T: Config> Pallet<T> {
139139 /// # Note
140140 /// This function performs extensive storage reads and writes, which can be computationally expensive.
141141 /// The accumulated weight should be carefully considered in the context of block limits.
142- pub fn perform_hotkey_swap ( old_hotkey : & T :: AccountId , new_hotkey : & T :: AccountId , coldkey : & T :: AccountId , weight : & mut Weight ) -> DispatchResult {
143-
142+ pub fn perform_hotkey_swap (
143+ old_hotkey : & T :: AccountId ,
144+ new_hotkey : & T :: AccountId ,
145+ coldkey : & T :: AccountId ,
146+ weight : & mut Weight ,
147+ ) -> DispatchResult {
144148 // 1. Swap owner.
145149 // Owner( hotkey ) -> coldkey -- the coldkey that owns the hotkey.
146150 Owner :: < T > :: remove ( old_hotkey) ;
@@ -153,23 +157,27 @@ impl<T: Config> Pallet<T> {
153157 // Add the new key if needed.
154158 if !hotkeys. contains ( new_hotkey) {
155159 hotkeys. push ( new_hotkey. clone ( ) ) ;
156- }
160+ }
157161 // Remove the old key.
158162 hotkeys. retain ( |hk| * hk != * old_hotkey) ;
159163 OwnedHotkeys :: < T > :: insert ( coldkey, hotkeys) ;
160164 weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 1 , 1 ) ) ;
161165
162166 // 3. Swap total hotkey stake.
163167 // TotalHotkeyStake( hotkey ) -> stake -- the total stake that the hotkey has across all delegates.
164- let old_total_hotkey_stake = TotalHotkeyStake :: < T > :: get ( old_hotkey ) ; // Get the old total hotkey stake.
165- let new_total_hotkey_stake = TotalHotkeyStake :: < T > :: get ( new_hotkey ) ; // Get the new total hotkey stake.
166- TotalHotkeyStake :: < T > :: remove ( old_hotkey ) ; // Remove the old total hotkey stake.
167- TotalHotkeyStake :: < T > :: insert ( new_hotkey, old_total_hotkey_stake. saturating_add ( new_total_hotkey_stake ) ) ; // Insert the new total hotkey stake via the addition.
168+ let old_total_hotkey_stake = TotalHotkeyStake :: < T > :: get ( old_hotkey) ; // Get the old total hotkey stake.
169+ let new_total_hotkey_stake = TotalHotkeyStake :: < T > :: get ( new_hotkey) ; // Get the new total hotkey stake.
170+ TotalHotkeyStake :: < T > :: remove ( old_hotkey) ; // Remove the old total hotkey stake.
171+ TotalHotkeyStake :: < T > :: insert (
172+ new_hotkey,
173+ old_total_hotkey_stake. saturating_add ( new_total_hotkey_stake) ,
174+ ) ; // Insert the new total hotkey stake via the addition.
168175 weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 2 , 2 ) ) ;
169176
170177 // 4. Swap total hotkey stakes.
171178 // TotalHotkeyColdkeyStakesThisInterval( hotkey ) --> (u64: stakes, u64: block_number)
172- let stake_tuples: Vec < ( T :: AccountId , ( u64 , u64 ) ) > = TotalHotkeyColdkeyStakesThisInterval :: < T > :: iter_prefix ( old_hotkey) . collect ( ) ;
179+ let stake_tuples: Vec < ( T :: AccountId , ( u64 , u64 ) ) > =
180+ TotalHotkeyColdkeyStakesThisInterval :: < T > :: iter_prefix ( old_hotkey) . collect ( ) ;
173181 for ( coldkey, stake_tup) in stake_tuples {
174182 // NOTE: You could use this to increase your allowed stake operations but this would cost.
175183 TotalHotkeyColdkeyStakesThisInterval :: < T > :: insert ( new_hotkey, & coldkey, stake_tup) ;
@@ -179,14 +187,14 @@ impl<T: Config> Pallet<T> {
179187
180188 // 5. Swap LastTxBlock
181189 // LastTxBlock( hotkey ) --> u64 -- the last transaction block for the hotkey.
182- LastTxBlock :: < T > :: remove ( old_hotkey ) ;
183- LastTxBlock :: < T > :: insert ( new_hotkey, Self :: get_current_block_as_u64 ( ) ) ;
190+ LastTxBlock :: < T > :: remove ( old_hotkey) ;
191+ LastTxBlock :: < T > :: insert ( new_hotkey, Self :: get_current_block_as_u64 ( ) ) ;
184192 weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 0 , 2 ) ) ;
185193
186194 // 6. Swap LastTxBlockDelegateTake
187195 // LastTxBlockDelegateTake( hotkey ) --> u64 -- the last transaction block for the hotkey delegate take.
188- LastTxBlockDelegateTake :: < T > :: remove ( old_hotkey ) ;
189- LastTxBlockDelegateTake :: < T > :: insert ( new_hotkey, Self :: get_current_block_as_u64 ( ) ) ;
196+ LastTxBlockDelegateTake :: < T > :: remove ( old_hotkey) ;
197+ LastTxBlockDelegateTake :: < T > :: insert ( new_hotkey, Self :: get_current_block_as_u64 ( ) ) ;
190198 weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 1 , 2 ) ) ;
191199
192200 // 7. Swap Senate members.
@@ -198,19 +206,19 @@ impl<T: Config> Pallet<T> {
198206
199207 // 8. Swap delegates.
200208 // Delegates( hotkey ) -> take value -- the hotkey delegate take value.
201- let old_delegate_take = Delegates :: < T > :: get ( old_hotkey ) ;
202- Delegates :: < T > :: remove ( old_hotkey ) ; // Remove the old delegate take.
203- Delegates :: < T > :: insert ( new_hotkey, old_delegate_take ) ; // Insert the new delegate take.
209+ let old_delegate_take = Delegates :: < T > :: get ( old_hotkey) ;
210+ Delegates :: < T > :: remove ( old_hotkey) ; // Remove the old delegate take.
211+ Delegates :: < T > :: insert ( new_hotkey, old_delegate_take) ; // Insert the new delegate take.
204212 weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 1 , 2 ) ) ;
205213
206214 // 9. Swap all subnet specific info.
207215 let all_netuids: Vec < u16 > = Self :: get_all_subnet_netuids ( ) ;
208- for netuid in all_netuids {
216+ for netuid in all_netuids {
209217 // 9.1 Remove the previous hotkey and insert the new hotkey from membership.
210218 // IsNetworkMember( hotkey, netuid ) -> bool -- is the hotkey a subnet member.
211- let is_network_member: bool = IsNetworkMember :: < T > :: get ( old_hotkey, netuid ) ;
212- IsNetworkMember :: < T > :: remove ( old_hotkey, netuid ) ;
213- IsNetworkMember :: < T > :: insert ( new_hotkey, netuid, is_network_member ) ;
219+ let is_network_member: bool = IsNetworkMember :: < T > :: get ( old_hotkey, netuid) ;
220+ IsNetworkMember :: < T > :: remove ( old_hotkey, netuid) ;
221+ IsNetworkMember :: < T > :: insert ( new_hotkey, netuid, is_network_member) ;
214222 weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 1 , 2 ) ) ;
215223
216224 // 9.2 Swap Uids + Keys.
@@ -249,7 +257,7 @@ impl<T: Config> Pallet<T> {
249257 }
250258 }
251259
252- // 9.5 Swap WeightCommits
260+ // 9.5 Swap WeightCommits
253261 // WeightCommits( hotkey ) --> Vec<u64> -- the weight commits for the hotkey.
254262 if is_network_member {
255263 if let Ok ( old_weight_commits) = WeightCommits :: < T > :: try_get ( netuid, old_hotkey) {
@@ -273,14 +281,13 @@ impl<T: Config> Pallet<T> {
273281 weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 1 , 2 ) ) ;
274282 }
275283 }
276-
277284 }
278285
279286 // 10. Swap Stake.
280287 // Stake( hotkey, coldkey ) -> stake -- the stake that the hotkey controls on behalf of the coldkey.
281288 let stakes: Vec < ( T :: AccountId , u64 ) > = Stake :: < T > :: iter_prefix ( old_hotkey) . collect ( ) ;
282289 // Clear the entire old prefix here.
283- let _ = Stake :: < T > :: clear_prefix ( old_hotkey, stakes. len ( ) as u32 , None ) ;
290+ let _ = Stake :: < T > :: clear_prefix ( old_hotkey, stakes. len ( ) as u32 , None ) ;
284291 // Iterate over all the staking rows and insert them into the new hotkey.
285292 for ( coldkey, old_stake_amount) in stakes {
286293 weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads ( 1 ) ) ;
@@ -290,7 +297,11 @@ impl<T: Config> Pallet<T> {
290297 // Get the new stake value.
291298 let new_stake_value: u64 = Stake :: < T > :: get ( new_hotkey, & coldkey) ;
292299 // Insert the new stake value.
293- Stake :: < T > :: insert ( new_hotkey, & coldkey, new_stake_value. saturating_add ( old_stake_amount) ) ;
300+ Stake :: < T > :: insert (
301+ new_hotkey,
302+ & coldkey,
303+ new_stake_value. saturating_add ( old_stake_amount) ,
304+ ) ;
294305 weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 1 , 1 ) ) ;
295306
296307 // Swap StakingHotkeys.
0 commit comments