Skip to content

Commit 1b38b98

Browse files
committed
add impl and tests
1 parent 0385ffe commit 1b38b98

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

pallets/subtensor/src/swap/swap_hotkey.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,24 +190,33 @@ impl<T: Config> Pallet<T> {
190190

191191
// 5. Swap LastTxBlock
192192
// LastTxBlock( hotkey ) --> u64 -- the last transaction block for the hotkey.
193+
let last_tx_block: u64 = LastTxBlock::<T>::get(old_hotkey);
193194
LastTxBlock::<T>::remove(old_hotkey);
194-
LastTxBlock::<T>::insert(new_hotkey, Self::get_current_block_as_u64());
195-
weight.saturating_accrue(T::DbWeight::get().reads_writes(0, 2));
195+
LastTxBlock::<T>::insert(new_hotkey, last_tx_block);
196+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
196197

197198
// 6. Swap LastTxBlockDelegateTake
198199
// LastTxBlockDelegateTake( hotkey ) --> u64 -- the last transaction block for the hotkey delegate take.
200+
let last_tx_block_delegate_take: u64 = LastTxBlockDelegateTake::<T>::get(old_hotkey);
199201
LastTxBlockDelegateTake::<T>::remove(old_hotkey);
200-
LastTxBlockDelegateTake::<T>::insert(new_hotkey, Self::get_current_block_as_u64());
202+
LastTxBlockDelegateTake::<T>::insert(new_hotkey, last_tx_block_delegate_take);
203+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
204+
205+
// 7. Swap LastTxBlockChildKeyTake
206+
// LastTxBlockChildKeyTake( hotkey ) --> u64 -- the last transaction block for the hotkey child key take.
207+
let last_tx_block_child_key_take: u64 = LastTxBlockChildKeyTake::<T>::get(old_hotkey);
208+
LastTxBlockChildKeyTake::<T>::remove(old_hotkey);
209+
LastTxBlockChildKeyTake::<T>::insert(new_hotkey, last_tx_block_child_key_take);
201210
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
202211

203-
// 7. Swap Senate members.
212+
// 8. Swap Senate members.
204213
// Senate( hotkey ) --> ?
205214
if T::SenateMembers::is_member(old_hotkey) {
206215
T::SenateMembers::swap_member(old_hotkey, new_hotkey).map_err(|e| e.error)?;
207216
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
208217
}
209218

210-
// 8. Swap delegates.
219+
// 9. Swap delegates.
211220
// Delegates( hotkey ) -> take value -- the hotkey delegate take value.
212221
if Delegates::<T>::contains_key(old_hotkey) {
213222
let old_delegate_take = Delegates::<T>::get(old_hotkey);

pallets/subtensor/src/tests/swap_hotkey.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,3 +1387,39 @@ fn test_swap_hotkey_is_sn_owner_hotkey() {
13871387
assert_eq!(SubnetOwnerHotkey::<Test>::get(netuid), new_hotkey);
13881388
});
13891389
}
1390+
1391+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_hotkey_swap_rate_limits --exact --nocapture
1392+
#[test]
1393+
fn test_swap_hotkey_swap_rate_limits() {
1394+
new_test_ext(1).execute_with(|| {
1395+
let old_hotkey = U256::from(1);
1396+
let new_hotkey = U256::from(2);
1397+
let coldkey = U256::from(3);
1398+
let mut weight = Weight::zero();
1399+
1400+
let last_tx_block = 123;
1401+
let delegate_take_block = 4567;
1402+
let child_key_take_block = 8910;
1403+
1404+
// Set the last tx block for the old hotkey
1405+
LastTxBlock::<Test>::insert(old_hotkey, last_tx_block);
1406+
// Set the last delegate take block for the old hotkey
1407+
LastTxBlockDelegateTake::<Test>::insert(old_hotkey, delegate_take_block);
1408+
// Set last childkey take block for the old hotkey
1409+
LastTxBlockChildKeyTake::<Test>::insert(old_hotkey, child_key_take_block);
1410+
1411+
// Perform the swap
1412+
SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight);
1413+
1414+
// Check for new hotkey
1415+
assert_eq!(LastTxBlock::<Test>::get(new_hotkey), last_tx_block);
1416+
assert_eq!(
1417+
LastTxBlockDelegateTake::<Test>::get(new_hotkey),
1418+
delegate_take_block
1419+
);
1420+
assert_eq!(
1421+
LastTxBlockChildKeyTake::<Test>::get(new_hotkey),
1422+
child_key_take_block
1423+
);
1424+
});
1425+
}

0 commit comments

Comments
 (0)