@@ -1058,11 +1058,12 @@ fn test_do_swap_coldkey_success() {
10581058 let netuid = 1u16 ;
10591059 let stake_amount = 1000u64 ;
10601060 let swap_cost = SubtensorModule :: get_coldkey_swap_cost ( ) ;
1061+ let free_balance = 12345 ;
10611062
10621063 // Setup initial state
10631064 add_network ( netuid, 13 , 0 ) ;
10641065 register_ok_neuron ( netuid, hotkey, old_coldkey, 0 ) ;
1065- SubtensorModule :: add_balance_to_coldkey_account ( & old_coldkey, stake_amount + swap_cost) ;
1066+ SubtensorModule :: add_balance_to_coldkey_account ( & old_coldkey, stake_amount + swap_cost + free_balance ) ;
10661067
10671068 // Add stake to the neuron
10681069 assert_ok ! ( SubtensorModule :: add_stake(
@@ -1072,12 +1073,22 @@ fn test_do_swap_coldkey_success() {
10721073 ) ) ;
10731074
10741075 log:: info!( "TotalColdkeyStake::<Test>::get(old_coldkey): {:?}" , TotalColdkeyStake :: <Test >:: get( old_coldkey) ) ;
1075- log:: info!( "Stake::<Test>::get(old_coldkey, hotkey): {:?}" , Stake :: <Test >:: get( old_coldkey , hotkey ) ) ;
1076+ log:: info!( "Stake::<Test>::get(old_coldkey, hotkey): {:?}" , Stake :: <Test >:: get( hotkey , old_coldkey ) ) ;
10761077
10771078 // Verify initial stake
10781079 assert_eq ! ( TotalColdkeyStake :: <Test >:: get( old_coldkey) , stake_amount) ;
1079- // assert_eq!(Stake::<Test>::get(old_coldkey, hotkey), stake_amount);
1080+ assert_eq ! ( Stake :: <Test >:: get( hotkey, old_coldkey) , stake_amount) ;
1081+
1082+
1083+ assert_eq ! ( Owned :: <Test >:: get( old_coldkey) , vec![ hotkey] ) ;
1084+ assert ! ( !Owned :: <Test >:: contains_key( new_coldkey) ) ;
1085+
1086+
1087+
10801088
1089+ // Get coldkey free balance before swap
1090+ let balance = SubtensorModule :: get_coldkey_balance ( & old_coldkey) ;
1091+ assert_eq ! ( balance, free_balance + swap_cost) ;
10811092
10821093 // Perform the swap
10831094 assert_ok ! ( SubtensorModule :: do_swap_coldkey(
@@ -1086,19 +1097,19 @@ fn test_do_swap_coldkey_success() {
10861097 & new_coldkey
10871098 ) ) ;
10881099
1089- // Verify the swap
1100+ // Verify the swap
10901101 assert_eq ! ( Owner :: <Test >:: get( hotkey) , new_coldkey) ;
10911102 assert_eq ! ( TotalColdkeyStake :: <Test >:: get( new_coldkey) , stake_amount) ;
10921103 assert ! ( !TotalColdkeyStake :: <Test >:: contains_key( old_coldkey) ) ;
1093- assert_eq ! ( Stake :: <Test >:: get( new_coldkey, hotkey) , stake_amount) ;
1094- assert ! ( !Stake :: <Test >:: contains_key( old_coldkey, hotkey) ) ;
1104+ assert_eq ! ( Stake :: <Test >:: get( hotkey, new_coldkey) , stake_amount) ;
1105+ assert ! ( !Stake :: <Test >:: contains_key( hotkey, old_coldkey) ) ;
1106+ assert_eq ! ( Owned :: <Test >:: get( new_coldkey) , vec![ hotkey] ) ;
1107+ assert ! ( !Owned :: <Test >:: contains_key( old_coldkey) ) ;
10951108
10961109 // Verify balance transfer
1097- assert_eq ! ( SubtensorModule :: get_coldkey_balance( & new_coldkey) , stake_amount ) ;
1110+ assert_eq ! ( SubtensorModule :: get_coldkey_balance( & new_coldkey) , balance - swap_cost ) ;
10981111 assert_eq ! ( SubtensorModule :: get_coldkey_balance( & old_coldkey) , 0 ) ;
10991112
1100-
1101-
11021113 // Verify event emission
11031114 System :: assert_last_event ( Event :: ColdkeySwapped {
11041115 old_coldkey : old_coldkey,
@@ -1244,6 +1255,9 @@ fn test_swap_owner_for_coldkey() {
12441255 Owner :: < Test > :: insert ( hotkey1, old_coldkey) ;
12451256 Owner :: < Test > :: insert ( hotkey2, old_coldkey) ;
12461257
1258+ // Initialize Owned map
1259+ Owned :: < Test > :: insert ( old_coldkey, vec ! [ hotkey1, hotkey2] ) ;
1260+
12471261 // Perform the swap
12481262 SubtensorModule :: swap_owner_for_coldkey ( & old_coldkey, & new_coldkey, & mut weight) ;
12491263
@@ -1269,8 +1283,11 @@ fn test_swap_total_hotkey_coldkey_stakes_this_interval_for_coldkey() {
12691283 let mut weight = Weight :: zero ( ) ;
12701284
12711285 // Initialize TotalHotkeyColdkeyStakesThisInterval for old_coldkey
1272- TotalHotkeyColdkeyStakesThisInterval :: < Test > :: insert ( old_coldkey, hotkey1, stake1) ;
1273- TotalHotkeyColdkeyStakesThisInterval :: < Test > :: insert ( old_coldkey, hotkey2, stake2) ;
1286+ TotalHotkeyColdkeyStakesThisInterval :: < Test > :: insert ( hotkey1, old_coldkey, stake1) ;
1287+ TotalHotkeyColdkeyStakesThisInterval :: < Test > :: insert ( hotkey2, old_coldkey, stake2) ;
1288+
1289+ // Populate Owned map
1290+ Owned :: < Test > :: insert ( old_coldkey, vec ! [ hotkey1, hotkey2] ) ;
12741291
12751292 // Perform the swap
12761293 SubtensorModule :: swap_total_hotkey_coldkey_stakes_this_interval_for_coldkey (
@@ -1281,11 +1298,11 @@ fn test_swap_total_hotkey_coldkey_stakes_this_interval_for_coldkey() {
12811298
12821299 // Verify the swap
12831300 assert_eq ! (
1284- TotalHotkeyColdkeyStakesThisInterval :: <Test >:: get( new_coldkey , hotkey1 ) ,
1301+ TotalHotkeyColdkeyStakesThisInterval :: <Test >:: get( hotkey1 , new_coldkey ) ,
12851302 stake1
12861303 ) ;
12871304 assert_eq ! (
1288- TotalHotkeyColdkeyStakesThisInterval :: <Test >:: get( new_coldkey , hotkey2 ) ,
1305+ TotalHotkeyColdkeyStakesThisInterval :: <Test >:: get( hotkey2 , new_coldkey ) ,
12891306 stake2
12901307 ) ;
12911308 assert ! ( !TotalHotkeyColdkeyStakesThisInterval :: <Test >:: contains_key(
@@ -1298,42 +1315,7 @@ fn test_swap_total_hotkey_coldkey_stakes_this_interval_for_coldkey() {
12981315 ) ) ;
12991316
13001317 // Verify weight update
1301- let expected_weight = <Test as frame_system:: Config >:: DbWeight :: get ( ) . reads_writes ( 1 , 4 ) ;
1302- assert_eq ! ( weight, expected_weight) ;
1303- } ) ;
1304- }
1305-
1306- #[ test]
1307- fn test_swap_keys_for_coldkey ( ) {
1308- new_test_ext ( 1 ) . execute_with ( || {
1309- let old_coldkey = U256 :: from ( 1 ) ;
1310- let new_coldkey = U256 :: from ( 2 ) ;
1311- let hotkey1 = U256 :: from ( 3 ) ;
1312- let hotkey2 = U256 :: from ( 4 ) ;
1313- let netuid1 = 1u16 ;
1314- let netuid2 = 2u16 ;
1315- let uid1 = 10u16 ;
1316- let uid2 = 20u16 ;
1317- let mut weight = Weight :: zero ( ) ;
1318-
1319- // Initialize Keys and Owner for old_coldkey
1320- Keys :: < Test > :: insert ( netuid1, uid1, hotkey1) ;
1321- Keys :: < Test > :: insert ( netuid2, uid2, hotkey2) ;
1322- Owner :: < Test > :: insert ( hotkey1, old_coldkey) ;
1323- Owner :: < Test > :: insert ( hotkey2, old_coldkey) ;
1324-
1325- // Set up TotalNetworks
1326- TotalNetworks :: < Test > :: put ( 3 ) ;
1327-
1328- // Perform the swap
1329- SubtensorModule :: swap_keys_for_coldkey ( & old_coldkey, & new_coldkey, & mut weight) ;
1330-
1331- // Verify the swap
1332- assert_eq ! ( Keys :: <Test >:: get( netuid1, uid1) , hotkey1) ;
1333- assert_eq ! ( Keys :: <Test >:: get( netuid2, uid2) , hotkey2) ;
1334-
1335- // Verify weight update
1336- let expected_weight = <Test as frame_system:: Config >:: DbWeight :: get ( ) . reads_writes ( 3 , 2 ) ;
1318+ let expected_weight = <Test as frame_system:: Config >:: DbWeight :: get ( ) . reads_writes ( 5 , 4 ) ;
13371319 assert_eq ! ( weight, expected_weight) ;
13381320 } ) ;
13391321}
@@ -1380,9 +1362,16 @@ fn test_do_swap_coldkey_with_subnet_ownership() {
13801362 // Setup initial state
13811363 add_network ( netuid, 13 , 0 ) ;
13821364 register_ok_neuron ( netuid, hotkey, old_coldkey, 0 ) ;
1365+
1366+ // Set TotalNetworks because swap relies on it
1367+ pallet_subtensor:: TotalNetworks :: < Test > :: set ( 1 ) ;
1368+
13831369 SubtensorModule :: add_balance_to_coldkey_account ( & old_coldkey, stake_amount + swap_cost) ;
13841370 SubnetOwner :: < Test > :: insert ( netuid, old_coldkey) ;
13851371
1372+ // Populate Owned map
1373+ Owned :: < Test > :: insert ( old_coldkey, vec ! [ hotkey] ) ;
1374+
13861375 // Perform the swap
13871376 assert_ok ! ( SubtensorModule :: do_swap_coldkey(
13881377 <<Test as Config >:: RuntimeOrigin >:: signed( old_coldkey) ,
@@ -1402,8 +1391,12 @@ fn test_do_swap_coldkey_tx_rate_limit() {
14021391 let new_coldkey = U256 :: from ( 2 ) ;
14031392 let swap_cost = SubtensorModule :: get_coldkey_swap_cost ( ) ;
14041393
1394+ // Set non-zero tx rate limit
1395+ SubtensorModule :: set_tx_rate_limit ( 1 ) ;
1396+
14051397 // Setup initial state
14061398 SubtensorModule :: add_balance_to_coldkey_account ( & old_coldkey, swap_cost * 2 ) ;
1399+ SubtensorModule :: add_balance_to_coldkey_account ( & new_coldkey, swap_cost * 2 ) ;
14071400
14081401 // Perform first swap
14091402 assert_ok ! ( SubtensorModule :: do_swap_coldkey(
0 commit comments