@@ -14,7 +14,9 @@ mod hooks {
1414 // # Args:
1515 // * 'n': (BlockNumberFor<T>):
1616 // - The number of the block we are initializing.
17- fn on_initialize ( _block_number : BlockNumberFor < T > ) -> Weight {
17+ fn on_initialize ( block_number : BlockNumberFor < T > ) -> Weight {
18+ let hotkey_swap_clean_up_weight = Self :: clean_up_hotkey_swap_records ( block_number) ;
19+
1820 let block_step_result = Self :: block_step ( ) ;
1921 match block_step_result {
2022 Ok ( _) => {
@@ -23,13 +25,15 @@ mod hooks {
2325 Weight :: from_parts ( 110_634_229_000_u64 , 0 )
2426 . saturating_add ( T :: DbWeight :: get ( ) . reads ( 8304_u64 ) )
2527 . saturating_add ( T :: DbWeight :: get ( ) . writes ( 110_u64 ) )
28+ . saturating_add ( hotkey_swap_clean_up_weight)
2629 }
2730 Err ( e) => {
2831 // --- If the block step was unsuccessful, return the weight anyway.
2932 log:: error!( "Error while stepping block: {:?}" , e) ;
3033 Weight :: from_parts ( 110_634_229_000_u64 , 0 )
3134 . saturating_add ( T :: DbWeight :: get ( ) . reads ( 8304_u64 ) )
3235 . saturating_add ( T :: DbWeight :: get ( ) . writes ( 110_u64 ) )
36+ . saturating_add ( hotkey_swap_clean_up_weight)
3337 }
3438 }
3539 }
@@ -125,4 +129,42 @@ mod hooks {
125129 Ok ( ( ) )
126130 }
127131 }
132+
133+ impl < T : Config > Pallet < T > {
134+ // This function is to clean up the old hotkey swap records
135+ // It just clean up for one subnet at a time, according to the block number
136+ fn clean_up_hotkey_swap_records ( block_number : BlockNumberFor < T > ) -> Weight {
137+ let mut weight = Weight :: from_parts ( 0 , 0 ) ;
138+ let hotkey_swap_on_subnet_interval = T :: HotkeySwapOnSubnetInterval :: get ( ) ;
139+ let block_number: u64 = TryInto :: try_into ( block_number)
140+ . ok ( )
141+ . expect ( "blockchain will not exceed 2^64 blocks; QED." ) ;
142+ weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads ( 2_u64 ) ) ;
143+
144+ let netuids = Self :: get_all_subnet_netuids ( ) ;
145+ weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads ( netuids. len ( ) as u64 ) ) ;
146+
147+ if let Some ( slot) = block_number. checked_rem ( hotkey_swap_on_subnet_interval) {
148+ // only handle the subnet with the same residue as current block number by HotkeySwapOnSubnetInterval
149+ for netuid in netuids. iter ( ) . filter ( |netuid| {
150+ ( * * netuid as u64 ) . checked_rem ( hotkey_swap_on_subnet_interval) == Some ( slot)
151+ } ) {
152+ // Iterate over all the coldkeys in the subnet
153+ for ( coldkey, swap_block_number) in
154+ LastHotkeySwapOnNetuid :: < T > :: iter_prefix ( netuid)
155+ {
156+ // Clean up out of date swap records
157+ if swap_block_number. saturating_add ( hotkey_swap_on_subnet_interval)
158+ < block_number
159+ {
160+ LastHotkeySwapOnNetuid :: < T > :: remove ( netuid, coldkey) ;
161+ weight. saturating_accrue ( T :: DbWeight :: get ( ) . writes ( 1_u64 ) ) ;
162+ }
163+ weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads ( 1_u64 ) ) ;
164+ }
165+ }
166+ }
167+ weight
168+ }
169+ }
128170}
0 commit comments