@@ -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