Skip to content

Commit dfd96f6

Browse files
committed
add tests for swap hotkey
1 parent dcd06f1 commit dfd96f6

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

pallets/subtensor/tests/swap_hotkey.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,3 +1115,82 @@ fn test_swap_complex_parent_child_structure() {
11151115
);
11161116
});
11171117
}
1118+
1119+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_hotkey_with_pending_emissions --exact --nocapture
1120+
#[test]
1121+
fn test_swap_hotkey_with_pending_emissions() {
1122+
new_test_ext(1).execute_with(|| {
1123+
let old_hotkey = U256::from(1);
1124+
let new_hotkey = U256::from(2);
1125+
let coldkey = U256::from(3);
1126+
let netuid = 0u16;
1127+
let mut weight = Weight::zero();
1128+
1129+
let pending_emission = 123_456_789u64;
1130+
1131+
// Set up initial state
1132+
add_network(netuid, 0, 1);
1133+
1134+
// Set up pending emissions
1135+
PendingdHotkeyEmission::<Test>::insert(&old_hotkey, pending_emission);
1136+
// Verify the pending emissions are set
1137+
assert_eq!(
1138+
PendingdHotkeyEmission::<Test>::get(&old_hotkey),
1139+
pending_emission
1140+
);
1141+
// Verify the new hotkey does not have any pending emissions
1142+
assert!(!PendingdHotkeyEmission::<Test>::contains_key(&new_hotkey));
1143+
1144+
// Perform the swap
1145+
SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight);
1146+
1147+
// Verify the pending emissions are transferred
1148+
assert_eq!(
1149+
PendingdHotkeyEmission::<Test>::get(&new_hotkey),
1150+
pending_emission
1151+
);
1152+
assert!(!PendingdHotkeyEmission::<Test>::contains_key(&old_hotkey));
1153+
});
1154+
}
1155+
1156+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_hotkeys_last_add_stake_increase --exact --nocapture
1157+
#[test]
1158+
fn test_swap_hotkeys_last_add_stake_increase() {
1159+
new_test_ext(1).execute_with(|| {
1160+
let old_hotkey = U256::from(1);
1161+
let new_hotkey = U256::from(2);
1162+
let coldkey = U256::from(3);
1163+
let new_coldkeys = vec![U256::from(4), U256::from(5)];
1164+
let netuid = 0u16;
1165+
let mut weight = Weight::zero();
1166+
1167+
let pending_emission = 123_456_789u64;
1168+
1169+
// Set up initial state
1170+
add_network(netuid, 0, 1);
1171+
1172+
// Setup LastAddStakeIncrease map
1173+
for new_coldkey in new_coldkeys.iter() {
1174+
LastAddStakeIncrease::<Test>::insert(&old_hotkey, &new_coldkey, 100);
1175+
}
1176+
// Verify the LastAddStakeIncrease map is empty for the new hotkey
1177+
assert!(LastAddStakeIncrease::<Test>::iter_prefix(&new_hotkey)
1178+
.next()
1179+
.is_none());
1180+
1181+
// Perform the swap
1182+
SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight);
1183+
1184+
// Verify the LastAddStakeIncrease map is transferred
1185+
for new_coldkey in new_coldkeys.iter() {
1186+
assert_eq!(
1187+
LastAddStakeIncrease::<Test>::get(&new_hotkey, &new_coldkey),
1188+
100
1189+
);
1190+
assert!(!LastAddStakeIncrease::<Test>::contains_key(
1191+
&old_hotkey,
1192+
&new_coldkey
1193+
));
1194+
}
1195+
});
1196+
}

0 commit comments

Comments
 (0)