Skip to content

Commit 73c352c

Browse files
committed
Merge branch 'feat/rao-2025-01' of github.com:opentensor/subtensor into feat/rao-2025-01
2 parents c61ff6f + f1b794d commit 73c352c

File tree

1 file changed

+107
-40
lines changed

1 file changed

+107
-40
lines changed

pallets/subtensor/src/tests/swap_coldkey.rs

Lines changed: 107 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -454,16 +454,44 @@ fn test_swap_concurrent_modifications() {
454454
let new_coldkey = U256::from(2);
455455
let hotkey = U256::from(3);
456456
let netuid: u16 = 1;
457-
let initial_stake = 100;
458-
let additional_stake = 50;
457+
let initial_stake = 1_000_000_000_000;
458+
let additional_stake = 500_000_000_000;
459459

460-
StakingHotkeys::<Test>::insert(old_coldkey, vec![hotkey]);
461-
Stake::<Test>::insert(hotkey, old_coldkey, initial_stake);
462-
463-
// Simulate concurrent stake addition
460+
// Setup initial state
464461
add_network(netuid, 1, 1);
465-
SubtensorModule::add_balance_to_coldkey_account(&new_coldkey, additional_stake);
462+
SubtensorModule::add_balance_to_coldkey_account(
463+
&new_coldkey,
464+
initial_stake + additional_stake + 1000_000,
465+
);
466466
register_ok_neuron(netuid, hotkey, new_coldkey, 1001000);
467+
assert_ok!(SubtensorModule::add_stake(
468+
<<Test as Config>::RuntimeOrigin>::signed(new_coldkey),
469+
hotkey,
470+
netuid,
471+
initial_stake
472+
));
473+
474+
// Verify initial stake
475+
assert_eq!(
476+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
477+
&hotkey,
478+
&new_coldkey,
479+
netuid
480+
),
481+
initial_stake
482+
);
483+
484+
// Wait some blocks
485+
step_block(10);
486+
487+
// Get stake before swap
488+
let stake_before_swap = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
489+
&hotkey,
490+
&new_coldkey,
491+
netuid,
492+
);
493+
494+
// Simulate concurrent stake addition
467495
assert_ok!(SubtensorModule::add_stake(
468496
<<Test as Config>::RuntimeOrigin>::signed(new_coldkey),
469497
hotkey,
@@ -478,13 +506,16 @@ fn test_swap_concurrent_modifications() {
478506
&mut weight
479507
));
480508

481-
assert_eq!(
482-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
509+
let eps = 500; // RAO
510+
assert!(
511+
(SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
483512
&hotkey,
484513
&new_coldkey,
485514
netuid
486-
),
487-
initial_stake + additional_stake - 1
515+
) as i64
516+
- (stake_before_swap + additional_stake) as i64)
517+
.abs()
518+
<= eps
488519
);
489520
assert!(!Alpha::<Test>::contains_key((hotkey, old_coldkey, netuid)));
490521
});
@@ -843,43 +874,79 @@ fn test_swap_stake_for_coldkey() {
843874
});
844875
}
845876

846-
// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap_coldkey -- test_swap_staking_hotkeys_for_coldkey --exact --nocapture
877+
// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --package pallet-subtensor --lib -- tests::swap_coldkey::test_swap_staking_hotkeys_for_coldkey --exact --show-output
847878
#[test]
848879
fn test_swap_staking_hotkeys_for_coldkey() {
849880
new_test_ext(1).execute_with(|| {
850-
assert!(false);
881+
let old_coldkey = U256::from(1);
882+
let new_coldkey = U256::from(2);
883+
let other_coldkey = U256::from(3);
884+
let hotkey1 = U256::from(4);
885+
let hotkey2 = U256::from(5);
886+
let stake_amount1 = 1000u64;
887+
let stake_amount2 = 2000u64;
888+
let total_stake = stake_amount1 + stake_amount2;
889+
let mut weight = Weight::zero();
851890

852-
// let old_coldkey = U256::from(1);
853-
// let new_coldkey = U256::from(2);
854-
// let hotkey1 = U256::from(3);
855-
// let hotkey2 = U256::from(4);
856-
// let stake_amount1 = 1000u64;
857-
// let stake_amount2 = 2000u64;
858-
// let total_stake = stake_amount1 + stake_amount2;
859-
// let mut weight = Weight::zero();
891+
// Setup initial state
892+
// Add a network
893+
let netuid = 1u16;
894+
add_network(netuid, 1, 0);
895+
// Give some balance to old coldkey
896+
SubtensorModule::add_balance_to_coldkey_account(
897+
&old_coldkey,
898+
stake_amount1 + stake_amount2 + 1000_000,
899+
);
900+
// Register hotkeys
901+
register_ok_neuron(netuid, hotkey1, old_coldkey, 0);
902+
register_ok_neuron(netuid, hotkey2, other_coldkey, 0);
903+
// Make hotkey2 a delegate
904+
assert_ok!(SubtensorModule::become_delegate(
905+
<<Test as Config>::RuntimeOrigin>::signed(other_coldkey),
906+
hotkey2
907+
));
860908

861-
// // Setup initial state
862-
// OwnedHotkeys::<Test>::insert(old_coldkey, vec![hotkey1, hotkey2]);
863-
// Stake::<Test>::insert(hotkey1, old_coldkey, stake_amount1);
864-
// Stake::<Test>::insert(hotkey2, old_coldkey, stake_amount2);
865-
// StakingHotkeys::<Test>::insert(old_coldkey, vec![hotkey1, hotkey2]);
866-
// TotalHotkeyStake::<Test>::insert(hotkey1, stake_amount1);
867-
// TotalHotkeyStake::<Test>::insert(hotkey2, stake_amount2);
868-
// TotalColdkeyStake::<Test>::insert(old_coldkey, total_stake);
909+
// Stake to hotkeys
910+
assert_ok!(SubtensorModule::add_stake(
911+
<<Test as Config>::RuntimeOrigin>::signed(old_coldkey),
912+
hotkey1,
913+
netuid,
914+
stake_amount1
915+
));
916+
assert_ok!(SubtensorModule::add_stake(
917+
<<Test as Config>::RuntimeOrigin>::signed(old_coldkey),
918+
hotkey2,
919+
netuid,
920+
stake_amount2
921+
));
869922

870-
// // Set up total issuance
871-
// TotalIssuance::<Test>::put(total_stake);
872-
// TotalStake::<Test>::put(total_stake);
923+
// Verify stakes
924+
assert_eq!(
925+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
926+
&hotkey1,
927+
&old_coldkey,
928+
netuid
929+
),
930+
stake_amount1
931+
);
932+
assert_eq!(
933+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
934+
&hotkey2,
935+
&old_coldkey,
936+
netuid
937+
),
938+
stake_amount2
939+
);
873940

874-
// // Perform the swap
875-
// SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight);
941+
// Perform the swap
942+
SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight);
876943

877-
// // Verify StakingHotkeys transfer
878-
// assert_eq!(
879-
// StakingHotkeys::<Test>::get(new_coldkey),
880-
// vec![hotkey1, hotkey2]
881-
// );
882-
// assert_eq!(StakingHotkeys::<Test>::get(old_coldkey), vec![]);
944+
// Verify StakingHotkeys transfer
945+
assert_eq!(
946+
StakingHotkeys::<Test>::get(new_coldkey),
947+
vec![hotkey1, hotkey2]
948+
);
949+
assert_eq!(StakingHotkeys::<Test>::get(old_coldkey), vec![]);
883950
});
884951
}
885952

0 commit comments

Comments
 (0)