Skip to content

Commit cba4f41

Browse files
committed
Add alpha/ths v2 tests for swap hotkey
1 parent daf0b54 commit cba4f41

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

pallets/subtensor/src/tests/swap_hotkey.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,97 @@ fn test_swap_stake_success() {
942942
});
943943
}
944944

945+
#[test]
946+
fn test_swap_stake_v2_success() {
947+
new_test_ext(1).execute_with(|| {
948+
let old_hotkey = U256::from(1);
949+
let new_hotkey = U256::from(2);
950+
let coldkey = U256::from(3);
951+
let subnet_owner_coldkey = U256::from(1001);
952+
let subnet_owner_hotkey = U256::from(1002);
953+
let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
954+
let amount = 10_000;
955+
let shares = U64F64::from_num(123456);
956+
let mut weight = Weight::zero();
957+
958+
// Initialize staking variables for old_hotkey
959+
TotalHotkeyAlpha::<Test>::insert(old_hotkey, netuid, AlphaCurrency::from(amount));
960+
TotalHotkeyAlphaLastEpoch::<Test>::insert(
961+
old_hotkey,
962+
netuid,
963+
AlphaCurrency::from(amount * 2),
964+
);
965+
TotalHotkeySharesV2::<Test>::insert(
966+
old_hotkey,
967+
netuid,
968+
SafeFloatSerializable::from(&SafeFloat::from(shares)),
969+
);
970+
AlphaV2::<Test>::insert(
971+
(old_hotkey, coldkey, netuid),
972+
SafeFloatSerializable::from(&SafeFloat::from(U64F64::from_num(amount))),
973+
);
974+
AlphaDividendsPerSubnet::<Test>::insert(netuid, old_hotkey, AlphaCurrency::from(amount));
975+
976+
// Perform the swap
977+
SubtensorModule::perform_hotkey_swap_on_all_subnets(
978+
&old_hotkey,
979+
&new_hotkey,
980+
&coldkey,
981+
&mut weight,
982+
);
983+
984+
// Verify the swap
985+
assert_eq!(
986+
TotalHotkeyAlpha::<Test>::get(old_hotkey, netuid),
987+
AlphaCurrency::ZERO
988+
);
989+
assert_eq!(
990+
TotalHotkeyAlpha::<Test>::get(new_hotkey, netuid),
991+
amount.into()
992+
);
993+
assert_eq!(
994+
TotalHotkeyAlphaLastEpoch::<Test>::get(old_hotkey, netuid),
995+
AlphaCurrency::ZERO
996+
);
997+
assert_eq!(
998+
TotalHotkeyAlphaLastEpoch::<Test>::get(new_hotkey, netuid),
999+
AlphaCurrency::from(amount * 2)
1000+
);
1001+
assert_eq!(
1002+
f64::from(SafeFloat::from(&TotalHotkeySharesV2::<Test>::get(
1003+
old_hotkey, netuid
1004+
))),
1005+
0_f64
1006+
);
1007+
assert_eq!(
1008+
f64::from(SafeFloat::from(&TotalHotkeySharesV2::<Test>::get(
1009+
new_hotkey, netuid
1010+
))),
1011+
shares.to_num::<f64>()
1012+
);
1013+
assert_eq!(
1014+
f64::from(SafeFloat::from(&AlphaV2::<Test>::get((
1015+
old_hotkey, coldkey, netuid
1016+
)))),
1017+
0_f64
1018+
);
1019+
assert_eq!(
1020+
f64::from(SafeFloat::from(&AlphaV2::<Test>::get((
1021+
new_hotkey, coldkey, netuid
1022+
)))),
1023+
amount as f64
1024+
);
1025+
assert_eq!(
1026+
AlphaDividendsPerSubnet::<Test>::get(netuid, old_hotkey),
1027+
AlphaCurrency::ZERO
1028+
);
1029+
assert_eq!(
1030+
AlphaDividendsPerSubnet::<Test>::get(netuid, new_hotkey),
1031+
amount.into()
1032+
);
1033+
});
1034+
}
1035+
9451036
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_stake_old_hotkey_not_exist --exact --nocapture
9461037
#[test]
9471038
fn test_swap_stake_old_hotkey_not_exist() {

pallets/subtensor/src/tests/swap_hotkey_with_subnet.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,98 @@ fn test_swap_stake_success() {
997997
});
998998
}
999999

1000+
#[test]
1001+
fn test_swap_stake_v2_success() {
1002+
new_test_ext(1).execute_with(|| {
1003+
let old_hotkey = U256::from(1);
1004+
let new_hotkey = U256::from(2);
1005+
let coldkey = U256::from(3);
1006+
let subnet_owner_coldkey = U256::from(1001);
1007+
let subnet_owner_hotkey = U256::from(1002);
1008+
let netuid = add_dynamic_network(&old_hotkey, &coldkey);
1009+
SubtensorModule::add_balance_to_coldkey_account(&coldkey, u64::MAX);
1010+
let amount = 10_000;
1011+
let shares = U64F64::from_num(123456);
1012+
1013+
// Initialize staking variables for old_hotkey
1014+
TotalHotkeyAlpha::<Test>::insert(old_hotkey, netuid, AlphaCurrency::from(amount));
1015+
TotalHotkeyAlphaLastEpoch::<Test>::insert(
1016+
old_hotkey,
1017+
netuid,
1018+
AlphaCurrency::from(amount * 2),
1019+
);
1020+
TotalHotkeySharesV2::<Test>::insert(
1021+
old_hotkey,
1022+
netuid,
1023+
SafeFloatSerializable::from(&SafeFloat::from(shares)),
1024+
);
1025+
AlphaV2::<Test>::insert(
1026+
(old_hotkey, coldkey, netuid),
1027+
SafeFloatSerializable::from(&SafeFloat::from(U64F64::from_num(amount))),
1028+
);
1029+
AlphaDividendsPerSubnet::<Test>::insert(netuid, old_hotkey, AlphaCurrency::from(amount));
1030+
1031+
// Perform the swap
1032+
System::set_block_number(System::block_number() + HotkeySwapOnSubnetInterval::get());
1033+
assert_ok!(SubtensorModule::do_swap_hotkey(
1034+
RuntimeOrigin::signed(coldkey),
1035+
&old_hotkey,
1036+
&new_hotkey,
1037+
Some(netuid)
1038+
),);
1039+
1040+
// Verify the swap
1041+
assert_eq!(
1042+
TotalHotkeyAlpha::<Test>::get(old_hotkey, netuid),
1043+
AlphaCurrency::ZERO
1044+
);
1045+
assert_eq!(
1046+
TotalHotkeyAlpha::<Test>::get(new_hotkey, netuid),
1047+
AlphaCurrency::from(amount)
1048+
);
1049+
assert_eq!(
1050+
TotalHotkeyAlphaLastEpoch::<Test>::get(old_hotkey, netuid),
1051+
AlphaCurrency::ZERO
1052+
);
1053+
assert_eq!(
1054+
TotalHotkeyAlphaLastEpoch::<Test>::get(new_hotkey, netuid),
1055+
AlphaCurrency::from(amount * 2)
1056+
);
1057+
assert_eq!(
1058+
f64::from(SafeFloat::from(&TotalHotkeySharesV2::<Test>::get(
1059+
old_hotkey, netuid
1060+
))),
1061+
0_f64
1062+
);
1063+
assert_eq!(
1064+
f64::from(SafeFloat::from(&TotalHotkeySharesV2::<Test>::get(
1065+
new_hotkey, netuid
1066+
))),
1067+
shares.to_num::<f64>()
1068+
);
1069+
assert_eq!(
1070+
f64::from(SafeFloat::from(&AlphaV2::<Test>::get((
1071+
old_hotkey, coldkey, netuid
1072+
)))),
1073+
0_f64
1074+
);
1075+
assert_eq!(
1076+
f64::from(SafeFloat::from(&AlphaV2::<Test>::get((
1077+
new_hotkey, coldkey, netuid
1078+
)))),
1079+
amount as f64
1080+
);
1081+
assert_eq!(
1082+
AlphaDividendsPerSubnet::<Test>::get(netuid, old_hotkey),
1083+
AlphaCurrency::ZERO
1084+
);
1085+
assert_eq!(
1086+
AlphaDividendsPerSubnet::<Test>::get(netuid, new_hotkey),
1087+
AlphaCurrency::from(amount)
1088+
);
1089+
});
1090+
}
1091+
10001092
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey_with_subnet -- test_swap_hotkey_error_cases --exact --nocapture
10011093
#[test]
10021094
fn test_swap_hotkey_error_cases() {

0 commit comments

Comments
 (0)