Skip to content

Commit f892a9c

Browse files
authored
Merge pull request #1755 from opentensor/fix/move-reg-check
move guard check for *any* sn reg
2 parents 6c8df06 + 55b3ea2 commit f892a9c

File tree

2 files changed

+66
-22
lines changed

2 files changed

+66
-22
lines changed

pallets/subtensor/src/swap/swap_hotkey.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,35 +55,36 @@ impl<T: Config> Pallet<T> {
5555

5656
weight.saturating_accrue(T::DbWeight::get().reads(2));
5757

58-
// 7. Swap LastTxBlock
58+
// 7. Ensure the new hotkey is not already registered on any network
59+
ensure!(
60+
!Self::is_hotkey_registered_on_any_network(new_hotkey),
61+
Error::<T>::HotKeyAlreadyRegisteredInSubNet
62+
);
63+
64+
// 8. Swap LastTxBlock
5965
// LastTxBlock( hotkey ) --> u64 -- the last transaction block for the hotkey.
6066
let last_tx_block: u64 = LastTxBlock::<T>::get(old_hotkey);
6167
LastTxBlock::<T>::insert(new_hotkey, last_tx_block);
6268
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
6369

64-
// 8. Swap LastTxBlockDelegateTake
70+
// 9. Swap LastTxBlockDelegateTake
6571
// LastTxBlockDelegateTake( hotkey ) --> u64 -- the last transaction block for the hotkey delegate take.
6672
let last_tx_block_delegate_take: u64 = LastTxBlockDelegateTake::<T>::get(old_hotkey);
6773
LastTxBlockDelegateTake::<T>::insert(new_hotkey, last_tx_block_delegate_take);
6874
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
6975

70-
// 9. Swap LastTxBlockChildKeyTake
76+
// 10. Swap LastTxBlockChildKeyTake
7177
// LastTxBlockChildKeyTake( hotkey ) --> u64 -- the last transaction block for the hotkey child key take.
7278
let last_tx_block_child_key_take: u64 = LastTxBlockChildKeyTake::<T>::get(old_hotkey);
7379
LastTxBlockChildKeyTake::<T>::insert(new_hotkey, last_tx_block_child_key_take);
7480
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
7581

76-
// 10. fork for swap hotkey on a specific subnet case after do the common check
82+
// 11. fork for swap hotkey on a specific subnet case after do the common check
7783
if let Some(netuid) = netuid {
7884
return Self::swap_hotkey_on_subnet(&coldkey, old_hotkey, new_hotkey, netuid, weight);
7985
};
8086

81-
// Start to do everything for swap hotkey on all subnets case
82-
// 11. Ensure the new hotkey is not already registered on any network
83-
ensure!(
84-
!Self::is_hotkey_registered_on_any_network(new_hotkey),
85-
Error::<T>::HotKeyAlreadyRegisteredInSubNet
86-
);
87+
// ==== Start to do everything for swap hotkey on all subnets case ====
8788

8889
// 12. Get the cost for swapping the key
8990
let swap_cost = Self::get_key_swap_cost();

pallets/subtensor/src/tests/swap_hotkey_with_subnet.rs

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,8 @@ fn test_swap_hotkey_with_multiple_subnets() {
497497
new_test_ext(1).execute_with(|| {
498498
let old_hotkey = U256::from(1);
499499
let new_hotkey = U256::from(2);
500-
let coldkey = U256::from(3);
500+
let new_hotkey_2 = U256::from(3);
501+
let coldkey = U256::from(4);
501502

502503
SubtensorModule::add_balance_to_coldkey_account(&coldkey, u64::MAX);
503504

@@ -519,12 +520,12 @@ fn test_swap_hotkey_with_multiple_subnets() {
519520
assert_ok!(SubtensorModule::do_swap_hotkey(
520521
RuntimeOrigin::signed(coldkey),
521522
&old_hotkey,
522-
&new_hotkey,
523+
&new_hotkey_2,
523524
Some(netuid2)
524525
));
525526

526527
assert!(IsNetworkMember::<Test>::get(new_hotkey, netuid1));
527-
assert!(IsNetworkMember::<Test>::get(new_hotkey, netuid2));
528+
assert!(IsNetworkMember::<Test>::get(new_hotkey_2, netuid2));
528529
assert!(!IsNetworkMember::<Test>::get(old_hotkey, netuid1));
529530
assert!(!IsNetworkMember::<Test>::get(old_hotkey, netuid2));
530531
});
@@ -628,8 +629,9 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() {
628629
new_test_ext(1).execute_with(|| {
629630
let old_hotkey = U256::from(1);
630631
let new_hotkey = U256::from(2);
631-
let coldkey1 = U256::from(3);
632-
let coldkey2 = U256::from(4);
632+
let new_hotkey_2 = U256::from(3);
633+
let coldkey1 = U256::from(4);
634+
let coldkey2 = U256::from(5);
633635
let netuid1 = 1;
634636
let netuid2 = 2;
635637
let stake = DefaultMinStake::<Test>::get() * 10;
@@ -687,7 +689,7 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() {
687689
assert_ok!(SubtensorModule::do_swap_hotkey(
688690
RuntimeOrigin::signed(coldkey1),
689691
&old_hotkey,
690-
&new_hotkey,
692+
&new_hotkey_2,
691693
Some(netuid2)
692694
));
693695

@@ -697,6 +699,11 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() {
697699
coldkey1
698700
);
699701
assert!(!SubtensorModule::get_owned_hotkeys(&coldkey2).contains(&new_hotkey));
702+
assert_eq!(
703+
SubtensorModule::get_owning_coldkey_for_hotkey(&new_hotkey_2),
704+
coldkey1
705+
);
706+
assert!(!SubtensorModule::get_owned_hotkeys(&coldkey2).contains(&new_hotkey_2));
700707

701708
// Check stake transfer
702709
assert_eq!(
@@ -709,7 +716,7 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() {
709716
);
710717
assert_eq!(
711718
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
712-
&new_hotkey,
719+
&new_hotkey_2,
713720
&coldkey2,
714721
netuid2
715722
),
@@ -739,7 +746,7 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() {
739746
));
740747
assert!(SubtensorModule::is_hotkey_registered_on_network(
741748
netuid2,
742-
&new_hotkey
749+
&new_hotkey_2
743750
));
744751
assert!(!SubtensorModule::is_hotkey_registered_on_network(
745752
netuid1,
@@ -752,7 +759,8 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() {
752759

753760
// Check total stake transfer
754761
assert_eq!(
755-
SubtensorModule::get_total_stake_for_hotkey(&new_hotkey),
762+
SubtensorModule::get_total_stake_for_hotkey(&new_hotkey)
763+
+ SubtensorModule::get_total_stake_for_hotkey(&new_hotkey_2),
756764
total_hk_stake
757765
);
758766
assert_eq!(SubtensorModule::get_total_stake_for_hotkey(&old_hotkey), 0);
@@ -1143,7 +1151,8 @@ fn test_swap_multiple_subnets() {
11431151
new_test_ext(1).execute_with(|| {
11441152
let old_hotkey = U256::from(1);
11451153
let new_hotkey = U256::from(2);
1146-
let coldkey = U256::from(3);
1154+
let new_hotkey_2 = U256::from(3);
1155+
let coldkey = U256::from(4);
11471156
let netuid1 = add_dynamic_network(&old_hotkey, &coldkey);
11481157
let netuid2 = add_dynamic_network(&old_hotkey, &coldkey);
11491158

@@ -1169,13 +1178,13 @@ fn test_swap_multiple_subnets() {
11691178
assert_ok!(SubtensorModule::do_swap_hotkey(
11701179
RuntimeOrigin::signed(coldkey),
11711180
&old_hotkey,
1172-
&new_hotkey,
1181+
&new_hotkey_2,
11731182
Some(netuid2)
11741183
),);
11751184

11761185
// Verify the swap for both subnets
11771186
assert_eq!(ChildKeys::<Test>::get(new_hotkey, netuid1), children1);
1178-
assert_eq!(ChildKeys::<Test>::get(new_hotkey, netuid2), children2);
1187+
assert_eq!(ChildKeys::<Test>::get(new_hotkey_2, netuid2), children2);
11791188
assert!(ChildKeys::<Test>::get(old_hotkey, netuid1).is_empty());
11801189
assert!(ChildKeys::<Test>::get(old_hotkey, netuid2).is_empty());
11811190
});
@@ -1514,3 +1523,37 @@ fn test_swap_owner_check_swap_record_clean_up() {
15141523
));
15151524
});
15161525
}
1526+
1527+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey_with_subnet -- test_swap_hotkey_error_cases --exact --nocapture
1528+
#[test]
1529+
fn test_swap_hotkey_registered_on_other_subnet() {
1530+
new_test_ext(1).execute_with(|| {
1531+
let old_hotkey = U256::from(1);
1532+
let new_hotkey = U256::from(2);
1533+
let coldkey = U256::from(3);
1534+
let wrong_coldkey = U256::from(4);
1535+
let netuid = add_dynamic_network(&old_hotkey, &coldkey);
1536+
let other_netuid = add_dynamic_network(&old_hotkey, &coldkey);
1537+
1538+
// Set up initial state
1539+
Owner::<Test>::insert(old_hotkey, coldkey);
1540+
TotalNetworks::<Test>::put(1);
1541+
LastTxBlock::<Test>::insert(coldkey, 0);
1542+
1543+
let initial_balance = SubtensorModule::get_key_swap_cost() + 1000;
1544+
SubtensorModule::add_balance_to_coldkey_account(&coldkey, initial_balance);
1545+
1546+
// Test new hotkey already registered on other subnet
1547+
IsNetworkMember::<Test>::insert(new_hotkey, other_netuid, true);
1548+
System::set_block_number(System::block_number() + HotkeySwapOnSubnetInterval::get());
1549+
assert_noop!(
1550+
SubtensorModule::do_swap_hotkey(
1551+
RuntimeOrigin::signed(coldkey),
1552+
&old_hotkey,
1553+
&new_hotkey,
1554+
Some(netuid)
1555+
),
1556+
Error::<Test>::HotKeyAlreadyRegisteredInSubNet
1557+
);
1558+
});
1559+
}

0 commit comments

Comments
 (0)