From d4f445084a6ebbcf189c643ffbd9dec20e4cee53 Mon Sep 17 00:00:00 2001 From: open-junius Date: Thu, 14 Nov 2024 21:36:42 +0800 Subject: [PATCH] fix several test cases and compilation error --- pallets/subtensor/tests/swap_coldkey.rs | 78 +++++++++++++------------ pallets/subtensor/tests/swap_hotkey.rs | 49 ++++++++++------ 2 files changed, 74 insertions(+), 53 deletions(-) diff --git a/pallets/subtensor/tests/swap_coldkey.rs b/pallets/subtensor/tests/swap_coldkey.rs index 88883dea0..f05a99785 100644 --- a/pallets/subtensor/tests/swap_coldkey.rs +++ b/pallets/subtensor/tests/swap_coldkey.rs @@ -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::::insert(hotkey, old_coldkey, 123); -// // Give the new coldkey a stake delta on hotkey -// StakeDeltaSinceLastEmissionDrain::::insert(hotkey, new_coldkey, 456); -// let expected_stake_delta = 123 + 456; -// // Add StakingHotkeys entry -// StakingHotkeys::::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::::get(hotkey, new_coldkey), -// expected_stake_delta -// ); -// assert_eq!( -// StakeDeltaSinceLastEmissionDrain::::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::::insert(hotkey, old_coldkey, 123); + TotalColdkeyAlpha::::insert(old_coldkey, netuid, 1234); + // Give the new coldkey a stake delta on hotkey + Stake::::insert(hotkey, new_coldkey, 456); + TotalColdkeyAlpha::::insert(new_coldkey, netuid, 5678); + let expected_stake_delta = 123 + 456; + let expected_alpha_delta = 1234 + 5678; + // Add StakingHotkeys entry + StakingHotkeys::::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::::get(hotkey, new_coldkey), + expected_stake_delta + ); + assert_eq!(Stake::::get(hotkey, old_coldkey), 0); + assert_eq!( + TotalColdkeyAlpha::::get(new_coldkey, netuid), + expected_alpha_delta + ); + assert_eq!(TotalColdkeyAlpha::::get(old_coldkey, netuid), 0); + }); +} diff --git a/pallets/subtensor/tests/swap_hotkey.rs b/pallets/subtensor/tests/swap_hotkey.rs index 532a1ce2e..b8cb3e976 100644 --- a/pallets/subtensor/tests/swap_hotkey.rs +++ b/pallets/subtensor/tests/swap_hotkey.rs @@ -1104,6 +1104,7 @@ 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); @@ -1111,21 +1112,23 @@ fn test_hotkey_swap_stake_delta() { 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::::insert( - old_hotkey, - coldkey, - (123 + coldkey.saturated_into::()), + Stake::::insert(old_hotkey, coldkey, 123 + coldkey.saturated_into::()); + Alpha::::insert( + (old_hotkey, coldkey, netuid), + 1234 + coldkey.saturated_into::(), ); StakingHotkeys::::insert(coldkey, vec![old_hotkey]); } // Add stake delta for one coldkey and the new_hotkey - StakeDeltaSinceLastEmissionDrain::::insert(new_hotkey, coldkeys[0], 456); + Stake::::insert(new_hotkey, coldkeys[0], 456); + Alpha::::insert((new_hotkey, coldkeys[0], netuid), 5678); // Add corresponding StakingHotkeys StakingHotkeys::::insert(coldkeys[0], vec![old_hotkey, new_hotkey]); @@ -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::::get(new_hotkey, coldkeys[0]), - 123 + coldkeys[0].saturated_into::() + 456 + Stake::::get(new_hotkey, coldkeys[0]), + 123 + coldkeys[0].saturated_into::() + 456 + ); + + assert_eq!( + Alpha::::get((new_hotkey, coldkeys[0], netuid)), + 1234 + coldkeys[0].saturated_into::() + 5678 ); // -- coldkey[1..] maintains its stake delta from the old_hotkey for &coldkey in coldkeys[1..].iter() { assert_eq!( - StakeDeltaSinceLastEmissionDrain::::get(new_hotkey, coldkey), - 123 + coldkey.saturated_into::() + Stake::::get(new_hotkey, coldkey), + 123 + coldkey.saturated_into::() ); - assert!(!StakeDeltaSinceLastEmissionDrain::::contains_key( - old_hotkey, coldkey - )); + assert_eq!( + Alpha::::get((new_hotkey, coldkey, netuid)), + 1234 + coldkey.saturated_into::() + ); + assert!(!Stake::::contains_key(old_hotkey, coldkey)); + assert!(!Alpha::::contains_key((old_hotkey, coldkey, 0))); } }); } @@ -1167,24 +1178,28 @@ fn test_swap_hotkey_with_pending_emissions() { add_network(netuid, 0, 1); // Set up pending emissions - PendingdHotkeyEmission::::insert(old_hotkey, pending_emission); + PendingHotkeyEmissionOnNetuid::::insert(old_hotkey, netuid, pending_emission); // Verify the pending emissions are set assert_eq!( - PendingdHotkeyEmission::::get(old_hotkey), + PendingHotkeyEmissionOnNetuid::::get(old_hotkey, netuid,), pending_emission ); // Verify the new hotkey does not have any pending emissions - assert!(!PendingdHotkeyEmission::::contains_key(new_hotkey)); + assert!(!PendingHotkeyEmissionOnNetuid::::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::::get(new_hotkey), + PendingHotkeyEmissionOnNetuid::::get(new_hotkey, netuid,), pending_emission ); - assert!(!PendingdHotkeyEmission::::contains_key(old_hotkey)); + assert!(!PendingHotkeyEmissionOnNetuid::::contains_key( + old_hotkey, netuid, + )); }); }