Skip to content

Commit

Permalink
fix several test cases and compilation error
Browse files Browse the repository at this point in the history
  • Loading branch information
open-junius committed Nov 14, 2024
1 parent fc4fd4c commit d4f4450
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 53 deletions.
78 changes: 42 additions & 36 deletions pallets/subtensor/tests/swap_coldkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,39 +1180,45 @@ fn test_coldkey_swap_no_identity_no_changes_newcoldkey_exists() {
}

// // SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap_coldkey -- test_coldkey_swap_stake_delta --exact --nocapture
// #[test]
// fn test_coldkey_swap_stake_delta() {
// new_test_ext(1).execute_with(|| {
// let old_coldkey = U256::from(3);
// let new_coldkey = U256::from(4);
// let hotkey = U256::from(5);

// let netuid = 1;
// let burn_cost = 10;
// let tempo = 1;

// // Give the old coldkey a stake delta on hotkey
// StakeDeltaSinceLastEmissionDrain::<Test>::insert(hotkey, old_coldkey, 123);
// // Give the new coldkey a stake delta on hotkey
// StakeDeltaSinceLastEmissionDrain::<Test>::insert(hotkey, new_coldkey, 456);
// let expected_stake_delta = 123 + 456;
// // Add StakingHotkeys entry
// StakingHotkeys::<Test>::insert(old_coldkey, vec![hotkey]);

// // Give balance for the swap fees
// SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, 100e9 as u64);

// // Perform the coldkey swap
// assert_ok!(SubtensorModule::do_swap_coldkey(&old_coldkey, &new_coldkey));

// // Ensure the stake delta is correctly transferred
// assert_eq!(
// StakeDeltaSinceLastEmissionDrain::<Test>::get(hotkey, new_coldkey),
// expected_stake_delta
// );
// assert_eq!(
// StakeDeltaSinceLastEmissionDrain::<Test>::get(hotkey, old_coldkey),
// 0
// );
// });
// }
#[test]
fn test_coldkey_swap_stake_delta() {
new_test_ext(1).execute_with(|| {
let old_coldkey = U256::from(3);
let new_coldkey = U256::from(4);
let hotkey = U256::from(5);

let netuid = 1;
let burn_cost = 10;
let tempo = 1;
add_network(netuid, tempo, 0);

// Give the old coldkey a stake delta on hotkey
Stake::<Test>::insert(hotkey, old_coldkey, 123);
TotalColdkeyAlpha::<Test>::insert(old_coldkey, netuid, 1234);
// Give the new coldkey a stake delta on hotkey
Stake::<Test>::insert(hotkey, new_coldkey, 456);
TotalColdkeyAlpha::<Test>::insert(new_coldkey, netuid, 5678);
let expected_stake_delta = 123 + 456;
let expected_alpha_delta = 1234 + 5678;
// Add StakingHotkeys entry
StakingHotkeys::<Test>::insert(old_coldkey, vec![hotkey]);

// Give balance for the swap fees
SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, 100e9 as u64);

// Perform the coldkey swap
assert_ok!(SubtensorModule::do_swap_coldkey(&old_coldkey, &new_coldkey));

// Ensure the stake delta is correctly transferred
assert_eq!(
Stake::<Test>::get(hotkey, new_coldkey),
expected_stake_delta
);
assert_eq!(Stake::<Test>::get(hotkey, old_coldkey), 0);
assert_eq!(
TotalColdkeyAlpha::<Test>::get(new_coldkey, netuid),
expected_alpha_delta
);
assert_eq!(TotalColdkeyAlpha::<Test>::get(old_coldkey, netuid), 0);
});
}
49 changes: 32 additions & 17 deletions pallets/subtensor/tests/swap_hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,28 +1104,31 @@ fn test_swap_complex_parent_child_structure() {
#[test]
fn test_hotkey_swap_stake_delta() {
new_test_ext(1).execute_with(|| {
let netuid = 0;
let old_hotkey = U256::from(3);
let new_hotkey = U256::from(4);
let coldkey = U256::from(7);

let coldkeys = [U256::from(1), U256::from(2), U256::from(5)];

let mut weight = Weight::zero();
add_network(netuid, 1, 0);

// Set up initial state
// Add stake delta for each coldkey and the old_hotkey
for &coldkey in coldkeys.iter() {
StakeDeltaSinceLastEmissionDrain::<Test>::insert(
old_hotkey,
coldkey,
(123 + coldkey.saturated_into::<i128>()),
Stake::<Test>::insert(old_hotkey, coldkey, 123 + coldkey.saturated_into::<u64>());
Alpha::<Test>::insert(
(old_hotkey, coldkey, netuid),
1234 + coldkey.saturated_into::<u64>(),
);

StakingHotkeys::<Test>::insert(coldkey, vec![old_hotkey]);
}

// Add stake delta for one coldkey and the new_hotkey
StakeDeltaSinceLastEmissionDrain::<Test>::insert(new_hotkey, coldkeys[0], 456);
Stake::<Test>::insert(new_hotkey, coldkeys[0], 456);
Alpha::<Test>::insert((new_hotkey, coldkeys[0], netuid), 5678);
// Add corresponding StakingHotkeys
StakingHotkeys::<Test>::insert(coldkeys[0], vec![old_hotkey, new_hotkey]);

Expand All @@ -1135,18 +1138,26 @@ fn test_hotkey_swap_stake_delta() {
// Ensure the stake delta is correctly transferred for each coldkey
// -- coldkey[0] maintains its stake delta from the new_hotkey and the old_hotkey
assert_eq!(
StakeDeltaSinceLastEmissionDrain::<Test>::get(new_hotkey, coldkeys[0]),
123 + coldkeys[0].saturated_into::<i128>() + 456
Stake::<Test>::get(new_hotkey, coldkeys[0]),
123 + coldkeys[0].saturated_into::<u64>() + 456
);

assert_eq!(
Alpha::<Test>::get((new_hotkey, coldkeys[0], netuid)),
1234 + coldkeys[0].saturated_into::<u64>() + 5678
);
// -- coldkey[1..] maintains its stake delta from the old_hotkey
for &coldkey in coldkeys[1..].iter() {
assert_eq!(
StakeDeltaSinceLastEmissionDrain::<Test>::get(new_hotkey, coldkey),
123 + coldkey.saturated_into::<i128>()
Stake::<Test>::get(new_hotkey, coldkey),
123 + coldkey.saturated_into::<u64>()
);
assert!(!StakeDeltaSinceLastEmissionDrain::<Test>::contains_key(
old_hotkey, coldkey
));
assert_eq!(
Alpha::<Test>::get((new_hotkey, coldkey, netuid)),
1234 + coldkey.saturated_into::<u64>()
);
assert!(!Stake::<Test>::contains_key(old_hotkey, coldkey));
assert!(!Alpha::<Test>::contains_key((old_hotkey, coldkey, 0)));
}
});
}
Expand All @@ -1167,24 +1178,28 @@ fn test_swap_hotkey_with_pending_emissions() {
add_network(netuid, 0, 1);

// Set up pending emissions
PendingdHotkeyEmission::<Test>::insert(old_hotkey, pending_emission);
PendingHotkeyEmissionOnNetuid::<Test>::insert(old_hotkey, netuid, pending_emission);
// Verify the pending emissions are set
assert_eq!(
PendingdHotkeyEmission::<Test>::get(old_hotkey),
PendingHotkeyEmissionOnNetuid::<Test>::get(old_hotkey, netuid,),
pending_emission
);
// Verify the new hotkey does not have any pending emissions
assert!(!PendingdHotkeyEmission::<Test>::contains_key(new_hotkey));
assert!(!PendingHotkeyEmissionOnNetuid::<Test>::contains_key(
new_hotkey, netuid,
));

// Perform the swap
SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight);

// Verify the pending emissions are transferred
assert_eq!(
PendingdHotkeyEmission::<Test>::get(new_hotkey),
PendingHotkeyEmissionOnNetuid::<Test>::get(new_hotkey, netuid,),
pending_emission
);
assert!(!PendingdHotkeyEmission::<Test>::contains_key(old_hotkey));
assert!(!PendingHotkeyEmissionOnNetuid::<Test>::contains_key(
old_hotkey, netuid,
));
});
}

Expand Down

0 comments on commit d4f4450

Please sign in to comment.