Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions pallets/subtensor/src/swap/swap_hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,24 +190,33 @@ impl<T: Config> Pallet<T> {

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

// 6. Swap LastTxBlockDelegateTake
// LastTxBlockDelegateTake( hotkey ) --> u64 -- the last transaction block for the hotkey delegate take.
let last_tx_block_delegate_take: u64 = LastTxBlockDelegateTake::<T>::get(old_hotkey);
LastTxBlockDelegateTake::<T>::remove(old_hotkey);
LastTxBlockDelegateTake::<T>::insert(new_hotkey, Self::get_current_block_as_u64());
LastTxBlockDelegateTake::<T>::insert(new_hotkey, last_tx_block_delegate_take);
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));

// 7. Swap LastTxBlockChildKeyTake
// LastTxBlockChildKeyTake( hotkey ) --> u64 -- the last transaction block for the hotkey child key take.
let last_tx_block_child_key_take: u64 = LastTxBlockChildKeyTake::<T>::get(old_hotkey);
LastTxBlockChildKeyTake::<T>::remove(old_hotkey);
LastTxBlockChildKeyTake::<T>::insert(new_hotkey, last_tx_block_child_key_take);
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));

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

// 8. Swap delegates.
// 9. Swap delegates.
// Delegates( hotkey ) -> take value -- the hotkey delegate take value.
if Delegates::<T>::contains_key(old_hotkey) {
let old_delegate_take = Delegates::<T>::get(old_hotkey);
Expand Down
86 changes: 36 additions & 50 deletions pallets/subtensor/src/tests/swap_hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,56 +116,6 @@ fn test_swap_total_hotkey_stake() {
});
}

// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_last_tx_block --exact --nocapture
#[test]
fn test_swap_last_tx_block() {
new_test_ext(1).execute_with(|| {
let old_hotkey = U256::from(1);
let new_hotkey = U256::from(2);
let coldkey = U256::from(3);
let mut weight = Weight::zero();

LastTxBlock::<Test>::insert(old_hotkey, 1000);
assert_ok!(SubtensorModule::perform_hotkey_swap(
&old_hotkey,
&new_hotkey,
&coldkey,
&mut weight
));

assert!(!LastTxBlock::<Test>::contains_key(old_hotkey));
assert_eq!(
LastTxBlock::<Test>::get(new_hotkey),
SubtensorModule::get_current_block_as_u64()
);
});
}

// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_last_tx_block_delegate_take --exact --nocapture
#[test]
fn test_swap_last_tx_block_delegate_take() {
new_test_ext(1).execute_with(|| {
let old_hotkey = U256::from(1);
let new_hotkey = U256::from(2);
let coldkey = U256::from(3);
let mut weight = Weight::zero();

crate::LastTxBlockDelegateTake::<Test>::insert(old_hotkey, 1000);
assert_ok!(SubtensorModule::perform_hotkey_swap(
&old_hotkey,
&new_hotkey,
&coldkey,
&mut weight
));

assert!(!LastTxBlockDelegateTake::<Test>::contains_key(old_hotkey));
assert_eq!(
LastTxBlockDelegateTake::<Test>::get(new_hotkey),
SubtensorModule::get_current_block_as_u64()
);
});
}

// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_senate_members --exact --nocapture
#[test]
fn test_swap_senate_members() {
Expand Down Expand Up @@ -1387,3 +1337,39 @@ fn test_swap_hotkey_is_sn_owner_hotkey() {
assert_eq!(SubnetOwnerHotkey::<Test>::get(netuid), new_hotkey);
});
}

// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_hotkey_swap_rate_limits --exact --nocapture
#[test]
fn test_swap_hotkey_swap_rate_limits() {
new_test_ext(1).execute_with(|| {
let old_hotkey = U256::from(1);
let new_hotkey = U256::from(2);
let coldkey = U256::from(3);
let mut weight = Weight::zero();

let last_tx_block = 123;
let delegate_take_block = 4567;
let child_key_take_block = 8910;

// Set the last tx block for the old hotkey
LastTxBlock::<Test>::insert(old_hotkey, last_tx_block);
// Set the last delegate take block for the old hotkey
LastTxBlockDelegateTake::<Test>::insert(old_hotkey, delegate_take_block);
// Set last childkey take block for the old hotkey
LastTxBlockChildKeyTake::<Test>::insert(old_hotkey, child_key_take_block);

// Perform the swap
SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight);

// Check for new hotkey
assert_eq!(LastTxBlock::<Test>::get(new_hotkey), last_tx_block);
assert_eq!(
LastTxBlockDelegateTake::<Test>::get(new_hotkey),
delegate_take_block
);
assert_eq!(
LastTxBlockChildKeyTake::<Test>::get(new_hotkey),
child_key_take_block
);
});
}
Loading