From a00d130be871055631df53eb426021b46e0b2463 Mon Sep 17 00:00:00 2001 From: const Date: Mon, 22 Jul 2024 16:29:54 -0500 Subject: [PATCH 1/7] bring back hotkey swap --- pallets/subtensor/src/lib.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index 6a353eafbb..daaf20b001 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -2065,17 +2065,17 @@ pub mod pallet { } /// The extrinsic for user to change its hotkey - ///#[pallet::call_index(70)] - ///#[pallet::weight((Weight::from_parts(1_940_000_000, 0) - ///.saturating_add(T::DbWeight::get().reads(272)) - ///.saturating_add(T::DbWeight::get().writes(527)), DispatchClass::Operational, Pays::No))] - ///pub fn swap_hotkey( - /// origin: OriginFor, - /// hotkey: T::AccountId, - /// new_hotkey: T::AccountId, - ///) -> DispatchResultWithPostInfo { - /// Self::do_swap_hotkey(origin, &hotkey, &new_hotkey) - ///} + #[pallet::call_index(70)] + #[pallet::weight((Weight::from_parts(1_940_000_000, 0) + .saturating_add(T::DbWeight::get().reads(272)) + .saturating_add(T::DbWeight::get().writes(527)), DispatchClass::Operational, Pays::No))] + pub fn swap_hotkey( + origin: OriginFor, + hotkey: T::AccountId, + new_hotkey: T::AccountId, + ) -> DispatchResultWithPostInfo { + Self::do_swap_hotkey(origin, &hotkey, &new_hotkey) + } /// The extrinsic for user to change the coldkey associated with their account. /// From 9b9c594c58ec5d4540b9f20c07796673ddccce5e Mon Sep 17 00:00:00 2001 From: const Date: Mon, 22 Jul 2024 16:30:19 -0500 Subject: [PATCH 2/7] pump to 189 --- runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index a4abd124ff..db81344230 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -139,7 +139,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // `spec_version`, and `authoring_version` are the same between Wasm and native. // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use // the compatible custom types. - spec_version: 165, + spec_version: 189, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From 385f0bf1efc00e58b13c86cf15a0a2b22909b8a2 Mon Sep 17 00:00:00 2001 From: Samuel Dare Date: Tue, 23 Jul 2024 01:36:41 +0400 Subject: [PATCH 3/7] chore: lint --- pallets/subtensor/tests/swap_hotkey.rs | 204 ++++++++++++------------- 1 file changed, 102 insertions(+), 102 deletions(-) diff --git a/pallets/subtensor/tests/swap_hotkey.rs b/pallets/subtensor/tests/swap_hotkey.rs index 68f4ec49cd..9903829186 100644 --- a/pallets/subtensor/tests/swap_hotkey.rs +++ b/pallets/subtensor/tests/swap_hotkey.rs @@ -19,7 +19,7 @@ fn test_swap_owner() { let coldkey = U256::from(3); let mut weight = Weight::zero(); - Owner::::insert(&old_hotkey, &coldkey); + Owner::::insert(old_hotkey, coldkey); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, &new_hotkey, @@ -27,8 +27,8 @@ fn test_swap_owner() { &mut weight )); - assert!(!Owner::::contains_key(&old_hotkey)); - assert_eq!(Owner::::get(&new_hotkey), coldkey); + assert!(!Owner::::contains_key(old_hotkey)); + assert_eq!(Owner::::get(new_hotkey), coldkey); }); } @@ -41,7 +41,7 @@ fn test_swap_owned_hotkeys() { let coldkey = U256::from(3); let mut weight = Weight::zero(); - OwnedHotkeys::::insert(&coldkey, vec![old_hotkey]); + OwnedHotkeys::::insert(coldkey, vec![old_hotkey]); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, &new_hotkey, @@ -49,7 +49,7 @@ fn test_swap_owned_hotkeys() { &mut weight )); - let hotkeys = OwnedHotkeys::::get(&coldkey); + let hotkeys = OwnedHotkeys::::get(coldkey); assert!(!hotkeys.contains(&old_hotkey)); assert!(hotkeys.contains(&new_hotkey)); }); @@ -64,8 +64,8 @@ fn test_swap_total_hotkey_stake() { let coldkey = U256::from(3); let mut weight = Weight::zero(); - TotalHotkeyStake::::insert(&old_hotkey, 100); - TotalHotkeyStake::::insert(&new_hotkey, 50); + TotalHotkeyStake::::insert(old_hotkey, 100); + TotalHotkeyStake::::insert(new_hotkey, 50); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, &new_hotkey, @@ -73,8 +73,8 @@ fn test_swap_total_hotkey_stake() { &mut weight )); - assert!(!TotalHotkeyStake::::contains_key(&old_hotkey)); - assert_eq!(TotalHotkeyStake::::get(&new_hotkey), 150); + assert!(!TotalHotkeyStake::::contains_key(old_hotkey)); + assert_eq!(TotalHotkeyStake::::get(new_hotkey), 150); }); } @@ -87,7 +87,7 @@ fn test_swap_total_hotkey_coldkey_stakes_this_interval() { let coldkey = U256::from(3); let mut weight = Weight::zero(); - TotalHotkeyColdkeyStakesThisInterval::::insert(&old_hotkey, &coldkey, (100, 1000)); + TotalHotkeyColdkeyStakesThisInterval::::insert(old_hotkey, coldkey, (100, 1000)); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, &new_hotkey, @@ -96,11 +96,11 @@ fn test_swap_total_hotkey_coldkey_stakes_this_interval() { )); assert!(!TotalHotkeyColdkeyStakesThisInterval::::contains_key( - &old_hotkey, - &coldkey + old_hotkey, + coldkey )); assert_eq!( - TotalHotkeyColdkeyStakesThisInterval::::get(&new_hotkey, &coldkey), + TotalHotkeyColdkeyStakesThisInterval::::get(new_hotkey, coldkey), (100, 1000) ); }); @@ -115,7 +115,7 @@ fn test_swap_last_tx_block() { let coldkey = U256::from(3); let mut weight = Weight::zero(); - LastTxBlock::::insert(&old_hotkey, 1000); + LastTxBlock::::insert(old_hotkey, 1000); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, &new_hotkey, @@ -123,9 +123,9 @@ fn test_swap_last_tx_block() { &mut weight )); - assert!(!LastTxBlock::::contains_key(&old_hotkey)); + assert!(!LastTxBlock::::contains_key(old_hotkey)); assert_eq!( - LastTxBlock::::get(&new_hotkey), + LastTxBlock::::get(new_hotkey), SubtensorModule::get_current_block_as_u64() ); }); @@ -140,7 +140,7 @@ fn test_swap_last_tx_block_delegate_take() { let coldkey = U256::from(3); let mut weight = Weight::zero(); - pallet_subtensor::LastTxBlockDelegateTake::::insert(&old_hotkey, 1000); + pallet_subtensor::LastTxBlockDelegateTake::::insert(old_hotkey, 1000); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, &new_hotkey, @@ -148,9 +148,9 @@ fn test_swap_last_tx_block_delegate_take() { &mut weight )); - assert!(!LastTxBlockDelegateTake::::contains_key(&old_hotkey)); + assert!(!LastTxBlockDelegateTake::::contains_key(old_hotkey)); assert_eq!( - LastTxBlockDelegateTake::::get(&new_hotkey), + LastTxBlockDelegateTake::::get(new_hotkey), SubtensorModule::get_current_block_as_u64() ); }); @@ -189,7 +189,7 @@ fn test_swap_delegates() { let coldkey = U256::from(3); let mut weight = Weight::zero(); - Delegates::::insert(&old_hotkey, 100); + Delegates::::insert(old_hotkey, 100); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, &new_hotkey, @@ -197,8 +197,8 @@ fn test_swap_delegates() { &mut weight )); - assert!(!Delegates::::contains_key(&old_hotkey)); - assert_eq!(Delegates::::get(&new_hotkey), 100); + assert!(!Delegates::::contains_key(old_hotkey)); + assert_eq!(Delegates::::get(new_hotkey), 100); }); } @@ -213,7 +213,7 @@ fn test_swap_subnet_membership() { let mut weight = Weight::zero(); add_network(netuid, 0, 1); - IsNetworkMember::::insert(&old_hotkey, netuid, true); + IsNetworkMember::::insert(old_hotkey, netuid, true); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, &new_hotkey, @@ -221,8 +221,8 @@ fn test_swap_subnet_membership() { &mut weight )); - assert!(!IsNetworkMember::::contains_key(&old_hotkey, netuid)); - assert!(IsNetworkMember::::get(&new_hotkey, netuid)); + assert!(!IsNetworkMember::::contains_key(old_hotkey, netuid)); + assert!(IsNetworkMember::::get(new_hotkey, netuid)); }); } @@ -238,8 +238,8 @@ fn test_swap_uids_and_keys() { let mut weight = Weight::zero(); add_network(netuid, 0, 1); - IsNetworkMember::::insert(&old_hotkey, netuid, true); - Uids::::insert(netuid, &old_hotkey, uid); + IsNetworkMember::::insert(old_hotkey, netuid, true); + Uids::::insert(netuid, old_hotkey, uid); Keys::::insert(netuid, uid, old_hotkey); assert_ok!(SubtensorModule::perform_hotkey_swap( @@ -249,8 +249,8 @@ fn test_swap_uids_and_keys() { &mut weight )); - assert_eq!(Uids::::get(netuid, &old_hotkey), None); - assert_eq!(Uids::::get(netuid, &new_hotkey), Some(uid)); + assert_eq!(Uids::::get(netuid, old_hotkey), None); + assert_eq!(Uids::::get(netuid, new_hotkey), Some(uid)); assert_eq!(Keys::::get(netuid, uid), new_hotkey); }); } @@ -267,8 +267,8 @@ fn test_swap_prometheus() { let mut weight = Weight::zero(); add_network(netuid, 0, 1); - IsNetworkMember::::insert(&old_hotkey, netuid, true); - Prometheus::::insert(netuid, &old_hotkey, prometheus_info.clone()); + IsNetworkMember::::insert(old_hotkey, netuid, true); + Prometheus::::insert(netuid, old_hotkey, prometheus_info.clone()); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, @@ -277,9 +277,9 @@ fn test_swap_prometheus() { &mut weight )); - assert!(!Prometheus::::contains_key(netuid, &old_hotkey)); + assert!(!Prometheus::::contains_key(netuid, old_hotkey)); assert_eq!( - Prometheus::::get(netuid, &new_hotkey), + Prometheus::::get(netuid, new_hotkey), Some(prometheus_info) ); }); @@ -297,8 +297,8 @@ fn test_swap_axons() { let mut weight = Weight::zero(); add_network(netuid, 0, 1); - IsNetworkMember::::insert(&old_hotkey, netuid, true); - Axons::::insert(netuid, &old_hotkey, axon_info.clone()); + IsNetworkMember::::insert(old_hotkey, netuid, true); + Axons::::insert(netuid, old_hotkey, axon_info.clone()); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, @@ -307,8 +307,8 @@ fn test_swap_axons() { &mut weight )); - assert!(!Axons::::contains_key(netuid, &old_hotkey)); - assert_eq!(Axons::::get(netuid, &new_hotkey), Some(axon_info)); + assert!(!Axons::::contains_key(netuid, old_hotkey)); + assert_eq!(Axons::::get(netuid, new_hotkey), Some(axon_info)); }); } @@ -324,8 +324,8 @@ fn test_swap_weight_commits() { let mut weight = Weight::zero(); add_network(netuid, 0, 1); - IsNetworkMember::::insert(&old_hotkey, netuid, true); - WeightCommits::::insert(netuid, &old_hotkey, weight_commits.clone()); + IsNetworkMember::::insert(old_hotkey, netuid, true); + WeightCommits::::insert(netuid, old_hotkey, weight_commits); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, @@ -334,9 +334,9 @@ fn test_swap_weight_commits() { &mut weight )); - assert!(!WeightCommits::::contains_key(netuid, &old_hotkey)); + assert!(!WeightCommits::::contains_key(netuid, old_hotkey)); assert_eq!( - WeightCommits::::get(netuid, &new_hotkey), + WeightCommits::::get(netuid, new_hotkey), Some(weight_commits) ); }); @@ -355,7 +355,7 @@ fn test_swap_loaded_emission() { let mut weight = Weight::zero(); add_network(netuid, 0, 1); - IsNetworkMember::::insert(&old_hotkey, netuid, true); + IsNetworkMember::::insert(old_hotkey, netuid, true); LoadedEmission::::insert( netuid, vec![(old_hotkey, server_emission, validator_emission)], @@ -386,7 +386,7 @@ fn test_swap_stake() { let stake_amount = 100u64; let mut weight = Weight::zero(); - Stake::::insert(&old_hotkey, &coldkey, stake_amount); + Stake::::insert(old_hotkey, coldkey, stake_amount); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, @@ -395,8 +395,8 @@ fn test_swap_stake() { &mut weight )); - assert!(!Stake::::contains_key(&old_hotkey, &coldkey)); - assert_eq!(Stake::::get(&new_hotkey, &coldkey), stake_amount); + assert!(!Stake::::contains_key(old_hotkey, coldkey)); + assert_eq!(Stake::::get(new_hotkey, coldkey), stake_amount); }); } @@ -409,8 +409,8 @@ fn test_swap_staking_hotkeys() { let coldkey = U256::from(3); let mut weight = Weight::zero(); - Stake::::insert(&old_hotkey, &coldkey, 100); - StakingHotkeys::::insert(&coldkey, vec![old_hotkey]); + Stake::::insert(old_hotkey, coldkey, 100); + StakingHotkeys::::insert(coldkey, vec![old_hotkey]); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, @@ -419,7 +419,7 @@ fn test_swap_staking_hotkeys() { &mut weight )); - let staking_hotkeys = StakingHotkeys::::get(&coldkey); + let staking_hotkeys = StakingHotkeys::::get(coldkey); assert!(!staking_hotkeys.contains(&old_hotkey)); assert!(staking_hotkeys.contains(&new_hotkey)); }); @@ -435,10 +435,10 @@ fn test_swap_hotkey_with_multiple_coldkeys() { let coldkey2 = U256::from(4); let mut weight = Weight::zero(); - Stake::::insert(&old_hotkey, &coldkey1, 100); - Stake::::insert(&old_hotkey, &coldkey2, 200); - StakingHotkeys::::insert(&coldkey1, vec![old_hotkey]); - StakingHotkeys::::insert(&coldkey2, vec![old_hotkey]); + Stake::::insert(old_hotkey, coldkey1, 100); + Stake::::insert(old_hotkey, coldkey2, 200); + StakingHotkeys::::insert(coldkey1, vec![old_hotkey]); + StakingHotkeys::::insert(coldkey2, vec![old_hotkey]); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, @@ -447,10 +447,10 @@ fn test_swap_hotkey_with_multiple_coldkeys() { &mut weight )); - assert_eq!(Stake::::get(&new_hotkey, &coldkey1), 100); - assert_eq!(Stake::::get(&new_hotkey, &coldkey2), 200); - assert!(StakingHotkeys::::get(&coldkey1).contains(&new_hotkey)); - assert!(StakingHotkeys::::get(&coldkey2).contains(&new_hotkey)); + assert_eq!(Stake::::get(new_hotkey, coldkey1), 100); + assert_eq!(Stake::::get(new_hotkey, coldkey2), 200); + assert!(StakingHotkeys::::get(coldkey1).contains(&new_hotkey)); + assert!(StakingHotkeys::::get(coldkey2).contains(&new_hotkey)); }); } @@ -463,8 +463,8 @@ fn test_swap_hotkey_with_existing_stake() { let coldkey = U256::from(3); let mut weight = Weight::zero(); - Stake::::insert(&old_hotkey, &coldkey, 100); - Stake::::insert(&new_hotkey, &coldkey, 50); + Stake::::insert(old_hotkey, coldkey, 100); + Stake::::insert(new_hotkey, coldkey, 50); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, @@ -473,7 +473,7 @@ fn test_swap_hotkey_with_existing_stake() { &mut weight )); - assert_eq!(Stake::::get(&new_hotkey, &coldkey), 150); + assert_eq!(Stake::::get(new_hotkey, coldkey), 150); }); } @@ -490,8 +490,8 @@ fn test_swap_hotkey_with_multiple_subnets() { add_network(netuid1, 0, 1); add_network(netuid2, 0, 1); - IsNetworkMember::::insert(&old_hotkey, netuid1, true); - IsNetworkMember::::insert(&old_hotkey, netuid2, true); + IsNetworkMember::::insert(old_hotkey, netuid1, true); + IsNetworkMember::::insert(old_hotkey, netuid2, true); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, @@ -500,10 +500,10 @@ fn test_swap_hotkey_with_multiple_subnets() { &mut weight )); - assert!(IsNetworkMember::::get(&new_hotkey, netuid1)); - assert!(IsNetworkMember::::get(&new_hotkey, netuid2)); - assert!(!IsNetworkMember::::get(&old_hotkey, netuid1)); - assert!(!IsNetworkMember::::get(&old_hotkey, netuid2)); + assert!(IsNetworkMember::::get(new_hotkey, netuid1)); + assert!(IsNetworkMember::::get(new_hotkey, netuid2)); + assert!(!IsNetworkMember::::get(old_hotkey, netuid1)); + assert!(!IsNetworkMember::::get(old_hotkey, netuid2)); }); } @@ -518,10 +518,10 @@ fn test_swap_staking_hotkeys_multiple_coldkeys() { let mut weight = Weight::zero(); // Set up initial state - Stake::::insert(&old_hotkey, &coldkey1, 100); - Stake::::insert(&old_hotkey, &coldkey2, 200); - StakingHotkeys::::insert(&coldkey1, vec![old_hotkey]); - StakingHotkeys::::insert(&coldkey2, vec![old_hotkey, U256::from(5)]); + Stake::::insert(old_hotkey, coldkey1, 100); + Stake::::insert(old_hotkey, coldkey2, 200); + StakingHotkeys::::insert(coldkey1, vec![old_hotkey]); + StakingHotkeys::::insert(coldkey2, vec![old_hotkey, U256::from(5)]); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, @@ -531,13 +531,13 @@ fn test_swap_staking_hotkeys_multiple_coldkeys() { )); // Check if new_hotkey replaced old_hotkey in StakingHotkeys - assert!(StakingHotkeys::::get(&coldkey1).contains(&new_hotkey)); - assert!(!StakingHotkeys::::get(&coldkey1).contains(&old_hotkey)); + assert!(StakingHotkeys::::get(coldkey1).contains(&new_hotkey)); + assert!(!StakingHotkeys::::get(coldkey1).contains(&old_hotkey)); // Check if new_hotkey replaced old_hotkey for coldkey2 as well - assert!(StakingHotkeys::::get(&coldkey2).contains(&new_hotkey)); - assert!(!StakingHotkeys::::get(&coldkey2).contains(&old_hotkey)); - assert!(StakingHotkeys::::get(&coldkey2).contains(&U256::from(5))); + assert!(StakingHotkeys::::get(coldkey2).contains(&new_hotkey)); + assert!(!StakingHotkeys::::get(coldkey2).contains(&old_hotkey)); + assert!(StakingHotkeys::::get(coldkey2).contains(&U256::from(5))); // Other hotkeys should remain }); } @@ -552,7 +552,7 @@ fn test_swap_hotkey_with_no_stake() { let mut weight = Weight::zero(); // Set up initial state with no stake - Owner::::insert(&old_hotkey, &coldkey); + Owner::::insert(old_hotkey, coldkey); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, @@ -562,12 +562,12 @@ fn test_swap_hotkey_with_no_stake() { )); // Check if ownership transferred - assert!(!Owner::::contains_key(&old_hotkey)); - assert_eq!(Owner::::get(&new_hotkey), coldkey); + assert!(!Owner::::contains_key(old_hotkey)); + assert_eq!(Owner::::get(new_hotkey), coldkey); // Ensure no unexpected changes in Stake - assert!(!Stake::::contains_key(&old_hotkey, &coldkey)); - assert!(!Stake::::contains_key(&new_hotkey, &coldkey)); + assert!(!Stake::::contains_key(old_hotkey, coldkey)); + assert!(!Stake::::contains_key(new_hotkey, coldkey)); }); } @@ -586,12 +586,12 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { // Set up initial state add_network(netuid1, 0, 1); add_network(netuid2, 0, 1); - Owner::::insert(&old_hotkey, &coldkey1); - Stake::::insert(&old_hotkey, &coldkey1, 100); - Stake::::insert(&old_hotkey, &coldkey2, 200); - IsNetworkMember::::insert(&old_hotkey, netuid1, true); - IsNetworkMember::::insert(&old_hotkey, netuid2, true); - TotalHotkeyStake::::insert(&old_hotkey, 300); + Owner::::insert(old_hotkey, coldkey1); + Stake::::insert(old_hotkey, coldkey1, 100); + Stake::::insert(old_hotkey, coldkey2, 200); + IsNetworkMember::::insert(old_hotkey, netuid1, true); + IsNetworkMember::::insert(old_hotkey, netuid2, true); + TotalHotkeyStake::::insert(old_hotkey, 300); assert_ok!(SubtensorModule::perform_hotkey_swap( &old_hotkey, @@ -601,24 +601,24 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { )); // Check ownership transfer - assert!(!Owner::::contains_key(&old_hotkey)); - assert_eq!(Owner::::get(&new_hotkey), coldkey1); + assert!(!Owner::::contains_key(old_hotkey)); + assert_eq!(Owner::::get(new_hotkey), coldkey1); // Check stake transfer - assert_eq!(Stake::::get(&new_hotkey, &coldkey1), 100); - assert_eq!(Stake::::get(&new_hotkey, &coldkey2), 200); - assert!(!Stake::::contains_key(&old_hotkey, &coldkey1)); - assert!(!Stake::::contains_key(&old_hotkey, &coldkey2)); + assert_eq!(Stake::::get(new_hotkey, coldkey1), 100); + assert_eq!(Stake::::get(new_hotkey, coldkey2), 200); + assert!(!Stake::::contains_key(old_hotkey, coldkey1)); + assert!(!Stake::::contains_key(old_hotkey, coldkey2)); // Check subnet membership transfer - assert!(IsNetworkMember::::get(&new_hotkey, netuid1)); - assert!(IsNetworkMember::::get(&new_hotkey, netuid2)); - assert!(!IsNetworkMember::::get(&old_hotkey, netuid1)); - assert!(!IsNetworkMember::::get(&old_hotkey, netuid2)); + assert!(IsNetworkMember::::get(new_hotkey, netuid1)); + assert!(IsNetworkMember::::get(new_hotkey, netuid2)); + assert!(!IsNetworkMember::::get(old_hotkey, netuid1)); + assert!(!IsNetworkMember::::get(old_hotkey, netuid2)); // Check total stake transfer - assert_eq!(TotalHotkeyStake::::get(&new_hotkey), 300); - assert!(!TotalHotkeyStake::::contains_key(&old_hotkey)); + assert_eq!(TotalHotkeyStake::::get(new_hotkey), 300); + assert!(!TotalHotkeyStake::::contains_key(old_hotkey)); }); } @@ -899,9 +899,9 @@ fn test_swap_hotkey_error_cases() { let wrong_coldkey = U256::from(4); // Set up initial state - Owner::::insert(&old_hotkey, &coldkey); + Owner::::insert(old_hotkey, coldkey); TotalNetworks::::put(1); - LastTxBlock::::insert(&coldkey, 0); + LastTxBlock::::insert(coldkey, 0); // Test not enough balance let swap_cost = SubtensorModule::get_key_swap_cost(); @@ -928,7 +928,7 @@ fn test_swap_hotkey_error_cases() { ); // Test new hotkey already registered - IsNetworkMember::::insert(&new_hotkey, 0, true); + IsNetworkMember::::insert(new_hotkey, 0, true); assert_noop!( SubtensorModule::do_swap_hotkey( RuntimeOrigin::signed(coldkey), @@ -937,7 +937,7 @@ fn test_swap_hotkey_error_cases() { ), Error::::HotKeyAlreadyRegisteredInSubNet ); - IsNetworkMember::::remove(&new_hotkey, 0); + IsNetworkMember::::remove(new_hotkey, 0); // Test non-associated coldkey assert_noop!( @@ -958,7 +958,7 @@ fn test_swap_hotkey_error_cases() { // Check balance after swap assert_eq!( - Balances::free_balance(&coldkey), + Balances::free_balance(coldkey), initial_balance - swap_cost ); }); From 336321f122c3a7dc6f867cac18c34890665c937d Mon Sep 17 00:00:00 2001 From: const Date: Mon, 22 Jul 2024 18:00:34 -0500 Subject: [PATCH 4/7] clean coldkey swap --- pallets/subtensor/src/lib.rs | 8 +- .../src/{swap.rs => swap_coldkey.rs} | 673 +++++++++--------- pallets/subtensor/src/swap_hotkey.rs | 1 + .../tests/{swap.rs => swap_coldkey.rs} | 418 ++++++++++- 4 files changed, 733 insertions(+), 367 deletions(-) rename pallets/subtensor/src/{swap.rs => swap_coldkey.rs} (50%) rename pallets/subtensor/tests/{swap.rs => swap_coldkey.rs} (63%) diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index daaf20b001..baed738ea6 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -44,7 +44,7 @@ mod registration; mod root; mod serving; mod staking; -mod swap; +mod swap_coldkey; mod swap_hotkey; mod uids; mod utils; @@ -726,7 +726,7 @@ pub mod pallet { #[pallet::storage] // --- MAP ( netuid ) --> last_mechanism_step_block pub type LastMechansimStepBlock = StorageMap<_, Identity, u16, u64, ValueQuery, DefaultLastMechanismStepBlock>; - #[pallet::storage] // --- MAP ( netuid ) --> subnet_owner + #[pallet::storage] // --- MAP ( netuid ) --> (cold) subnet_owner pub type SubnetOwner = StorageMap<_, Identity, u16, T::AccountId, ValueQuery, DefaultSubnetOwner>; #[pallet::storage] // --- MAP ( netuid ) --> subnet_locked @@ -801,10 +801,10 @@ pub mod pallet { #[pallet::storage] // --- ITEM ( tx_rate_limit ) pub type TxDelegateTakeRateLimit = StorageValue<_, u64, ValueQuery, DefaultTxDelegateTakeRateLimit>; - #[pallet::storage] // --- MAP ( key ) --> last_block + #[pallet::storage] // --- MAP ( hotkey ) --> last_block pub type LastTxBlock = StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock>; - #[pallet::storage] // --- MAP ( key ) --> last_block + #[pallet::storage] // --- MAP ( hotkey ) --> last_block pub type LastTxBlockDelegateTake = StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock>; diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap_coldkey.rs similarity index 50% rename from pallets/subtensor/src/swap.rs rename to pallets/subtensor/src/swap_coldkey.rs index b6b0e9ba73..9a5ad515b4 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap_coldkey.rs @@ -1,17 +1,16 @@ + use super::*; use crate::MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP; -use frame_support::traits::fungible::Mutate; -use frame_support::traits::tokens::Preservation; -use frame_support::{storage::IterableStorageDoubleMap, weights::Weight}; +use frame_support::weights::Weight; use sp_core::{Get, U256}; impl Pallet { + /// Swaps the coldkey associated with a set of hotkeys from an old coldkey to a new coldkey. /// /// # Arguments /// /// * `origin` - The origin of the call, which must be signed by the old coldkey. - /// * `old_coldkey` - The account ID of the old coldkey. /// * `new_coldkey` - The account ID of the new coldkey. /// /// # Returns @@ -21,10 +20,9 @@ impl Pallet { /// # Errors /// /// This function will return an error if: - /// - The caller is not the old coldkey. - /// - The new coldkey is the same as the old coldkey. - /// - The new coldkey is already associated with other hotkeys. - /// - The transaction rate limit for coldkey swaps has been exceeded. + /// - The caller is not a valid signed origin. + /// - The old coldkey (caller) is in arbitration. + /// - The new coldkey is already associated with other hotkeys or is a hotkey itself. /// - There's not enough balance to pay for the swap. /// /// # Events @@ -38,56 +36,201 @@ impl Pallet { origin: T::RuntimeOrigin, new_coldkey: &T::AccountId, ) -> DispatchResultWithPostInfo { + // 1. Ensure the origin is signed and get the old coldkey let old_coldkey = ensure_signed(origin)?; + + // 2. Check if the old coldkey is in arbitration ensure!( !Self::coldkey_in_arbitration(&old_coldkey), Error::::ColdkeyIsInArbitration ); + // 3. Initialize the weight for this operation let mut weight: Weight = T::DbWeight::get().reads(2); - // Check that the coldkey is a new key (does not exist elsewhere.) + // 4. Ensure the new coldkey is not associated with any hotkeys ensure!( - !Self::coldkey_has_associated_hotkeys(new_coldkey), + StakingHotkeys::::get(new_coldkey).is_empty(), Error::::ColdKeyAlreadyAssociated ); - // Check that the new coldkey is not a hotkey. + + // 5. Ensure the new coldkey is not a hotkey ensure!( !Self::hotkey_account_exists(new_coldkey), Error::::ColdKeyAlreadyAssociated ); - // Calculate and charge the swap fee + // 6. Calculate the swap cost and ensure sufficient balance let swap_cost = Self::get_key_swap_cost(); log::debug!("Coldkey swap cost: {:?}", swap_cost); - ensure!( Self::can_remove_balance_from_coldkey_account(&old_coldkey, swap_cost), Error::::NotEnoughBalanceToPaySwapColdKey ); - let actual_burn_amount = - Self::remove_balance_from_coldkey_account(&old_coldkey, swap_cost)?; + + // 7. Remove and burn the swap cost from the old coldkey's account + let actual_burn_amount = Self::remove_balance_from_coldkey_account(&old_coldkey, swap_cost)?; Self::burn_tokens(actual_burn_amount); + // 8. Update the weight for the balance operations weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); - // Actually do the swap. - weight = weight.saturating_add( - Self::perform_swap_coldkey(&old_coldkey, new_coldkey) - .map_err(|_| Error::::ColdkeySwapError)?, - ); + // 9. Perform the actual coldkey swap + let _ = Self::perform_swap_coldkey(&old_coldkey, new_coldkey, &mut weight); + // 10. Update the last transaction block for the new coldkey Self::set_last_tx_block(new_coldkey, Self::get_current_block_as_u64()); weight.saturating_accrue(T::DbWeight::get().writes(1)); + // 11. Emit the ColdkeySwapped event Self::deposit_event(Event::ColdkeySwapped { old_coldkey: old_coldkey.clone(), new_coldkey: new_coldkey.clone(), }); + // 12. Return the result with the updated weight Ok(Some(weight).into()) } + + /// Performs the actual coldkey swap operation, transferring all associated data and balances from the old coldkey to the new coldkey. + /// + /// # Arguments + /// + /// * `old_coldkey` - The account ID of the old coldkey. + /// * `new_coldkey` - The account ID of the new coldkey. + /// * `weight` - A mutable reference to the current transaction weight. + /// + /// # Returns + /// + /// Returns a `DispatchResult` indicating success or failure of the operation. + /// + /// # Steps + /// + /// 1. Swap TotalHotkeyColdkeyStakesThisInterval: + /// - For each hotkey owned by the old coldkey, transfer its stake and block data to the new coldkey. + /// + /// 2. Swap subnet ownership: + /// - For each subnet, if the old coldkey is the owner, transfer ownership to the new coldkey. + /// + /// 3. Swap Stakes: + /// - For each hotkey staking for the old coldkey, transfer its stake to the new coldkey. + /// + /// 4. Swap total coldkey stake: + /// - Transfer the total stake from the old coldkey to the new coldkey. + /// + /// 5. Swap StakingHotkeys: + /// - Transfer the list of staking hotkeys from the old coldkey to the new coldkey. + /// + /// 6. Swap hotkey owners: + /// - For each hotkey owned by the old coldkey, transfer ownership to the new coldkey. + /// - Update the list of owned hotkeys for both old and new coldkeys. + /// + /// 7. Transfer remaining balance: + /// - Transfer any remaining balance from the old coldkey to the new coldkey. + /// + /// Throughout the process, the function updates the transaction weight to reflect the operations performed. + /// + /// # Notes + /// + /// This function is a critical part of the coldkey swap process and should be called only after all necessary checks and validations have been performed. + pub fn perform_swap_coldkey( old_coldkey: &T::AccountId, new_coldkey: &T::AccountId, weight: &mut Weight ) -> DispatchResult { + + // 1. Swap TotalHotkeyColdkeyStakesThisInterval + // TotalHotkeyColdkeyStakesThisInterval: MAP ( hotkey, coldkey ) --> ( stake, block ) | Stake of the hotkey for the coldkey. + for hotkey in OwnedHotkeys::::get(old_coldkey).iter() { + let (stake, block) = TotalHotkeyColdkeyStakesThisInterval::::get(&hotkey, old_coldkey); + TotalHotkeyColdkeyStakesThisInterval::::remove(&hotkey, old_coldkey); + TotalHotkeyColdkeyStakesThisInterval::::insert(&hotkey, new_coldkey, (stake, block)); + weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2)); + } + + // 2. Swap subnet owner. + // SubnetOwner: MAP ( netuid ) --> (coldkey) | Owner of the subnet. + for netuid in Self::get_all_subnet_netuids() { + let subnet_owner = SubnetOwner::::get(netuid); + if subnet_owner == *old_coldkey { + SubnetOwner::::insert(netuid, new_coldkey.clone()); + } + weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); + } + + // 3. Swap Stake. + // Stake: MAP ( hotkey, coldkey ) --> u64 | Stake of the hotkey for the coldkey. + for hotkey in StakingHotkeys::::get( old_coldkey ) { + // Get the stake on the old (hot,coldkey) account. + let old_stake: u64 = Stake::::get( &hotkey, old_coldkey ); + // Get the stake on the new (hot,coldkey) account. + let new_stake: u64 = Stake::::get( &hotkey, new_coldkey ); + // Add the stake to new account. + Stake::::insert(&hotkey, new_coldkey, new_stake.saturating_add(old_stake)); + // Remove the value from the old account. + Stake::::remove(&hotkey, old_coldkey); + // Add the weight for the read and write. + weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); + } + + // 4. Swap total coldkey stake. + // TotalColdkeyStake: MAP ( coldkey ) --> u64 | Total stake of the coldkey. + let old_coldkey_stake: u64 = TotalColdkeyStake::::get(old_coldkey); + // Get the stake of the new coldkey. + let new_coldkey_stake: u64 = TotalColdkeyStake::::get(new_coldkey); + // Remove the value from the old account. + TotalColdkeyStake::::insert(old_coldkey, 0); + // Add the stake to new account. + TotalColdkeyStake::::insert(new_coldkey, new_coldkey_stake.saturating_add(old_coldkey_stake) ); + weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); + + // 5. Swap StakingHotkeys. + // StakingHotkeys: MAP ( coldkey ) --> Vec | Hotkeys staking for the coldkey. + let old_staking_hotkeys: Vec = StakingHotkeys::::get(old_coldkey); + let mut new_staking_hotkeys: Vec = StakingHotkeys::::get( new_coldkey ); + for hotkey in old_staking_hotkeys { + // If the hotkey is not already in the new coldkey, add it. + if !new_staking_hotkeys.contains(&hotkey) { + new_staking_hotkeys.push(hotkey); + } + } + StakingHotkeys::::remove(old_coldkey); + StakingHotkeys::::insert(new_coldkey, new_staking_hotkeys); + weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); + + // 6. Swap hotkey owners. + // Owner: MAP ( hotkey ) --> coldkey | Owner of the hotkey. + // OwnedHotkeys: MAP ( coldkey ) --> Vec | Hotkeys owned by the coldkey. + let old_owned_hotkeys: Vec = OwnedHotkeys::::get(old_coldkey); + let mut new_owned_hotkeys: Vec = OwnedHotkeys::::get(new_coldkey); + for owned_hotkey in old_owned_hotkeys.iter() { + // Remove the hotkey from the old coldkey. + Owner::::remove(owned_hotkey); + // Add the hotkey to the new coldkey. + Owner::::insert(owned_hotkey, new_coldkey.clone()); + // Addd the owned hotkey to the new set of owned hotkeys. + if !new_owned_hotkeys.contains(owned_hotkey) { + new_owned_hotkeys.push(owned_hotkey.clone()); + } + } + OwnedHotkeys::::remove(old_coldkey); + OwnedHotkeys::::insert(new_coldkey, new_owned_hotkeys); + weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); + + // 7. Transfer remaining balance. + // Balance: MAP ( coldkey ) --> u64 | Balance of the coldkey. + // Transfer any remaining balance from old_coldkey to new_coldkey + let remaining_balance = Self::get_coldkey_balance(old_coldkey); + if remaining_balance > 0 { + if let Err(e) = Self::kill_coldkey_account(old_coldkey, remaining_balance) { + return Err(e.into()); + } + Self::add_balance_to_coldkey_account(new_coldkey, remaining_balance); + } + weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); + + // Return ok. + Ok(()) + } + + /// Checks if a coldkey is currently in arbitration. /// /// # Arguments @@ -303,334 +446,178 @@ impl Pallet { // Only remove ColdkeySwapDestinations if there's a single destination ColdkeySwapDestinations::::remove(&coldkey_i); weight_used = weight_used.saturating_add(T::DbWeight::get().writes(1)); - Self::perform_swap_coldkey(coldkey_i, new_coldkey).map(|weight| { - weight_used = weight_used.saturating_add(weight); - keys_swapped = keys_swapped.saturating_add(1); - })?; + keys_swapped = keys_swapped.saturating_add(1); + let _ = Self::perform_swap_coldkey(coldkey_i, new_coldkey, &mut weight_used); } } Ok(weight_used) } - pub fn perform_swap_coldkey( - old_coldkey: &T::AccountId, - new_coldkey: &T::AccountId, - ) -> Result { - log::info!( - "Performing swap for coldkey: {:?} to {:?}", - old_coldkey, - new_coldkey - ); - // Init the weight. - let mut weight = frame_support::weights::Weight::from_parts(0, 0); - - // Swap coldkey references in storage maps - // NOTE The order of these calls is important - Self::swap_stake_for_coldkey(old_coldkey, new_coldkey, &mut weight); - Self::swap_total_hotkey_coldkey_stakes_this_interval_for_coldkey( - old_coldkey, - new_coldkey, - &mut weight, - ); - Self::swap_subnet_owner_for_coldkey(old_coldkey, new_coldkey, &mut weight); - - // Transfer any remaining balance from old_coldkey to new_coldkey - let remaining_balance = Self::get_coldkey_balance(old_coldkey); - if remaining_balance > 0 { - if let Err(e) = Self::kill_coldkey_account(old_coldkey, remaining_balance) { - return Err(e.into()); - } - Self::add_balance_to_coldkey_account(new_coldkey, remaining_balance); - } - - // Swap the coldkey. - let total_balance: u64 = Self::get_coldkey_balance(old_coldkey); - if total_balance > 0 { - // Attempt to transfer the entire total balance to new_coldkey. - T::Currency::transfer( - old_coldkey, - new_coldkey, - total_balance, - Preservation::Expendable, - )?; - } - Ok(weight) - } - /// Retrieves the network membership status for a given hotkey. - /// - /// # Arguments - /// - /// * `old_hotkey` - The hotkey to check for network membership. - /// - /// # Returns - /// - /// * `Vec` - A vector of network IDs where the hotkey is a member. - pub fn get_netuid_is_member(old_hotkey: &T::AccountId, weight: &mut Weight) -> Vec { - let netuid_is_member: Vec = - as IterableStorageDoubleMap<_, _, _>>::iter_prefix(old_hotkey) - .map(|(netuid, _)| netuid) - .collect(); - weight.saturating_accrue(T::DbWeight::get().reads(netuid_is_member.len() as u64)); - netuid_is_member - } - - /// Swaps the total stake associated with a coldkey from the old coldkey to the new coldkey. - /// - /// # Arguments - /// - /// * `old_coldkey` - The AccountId of the old coldkey. - /// * `new_coldkey` - The AccountId of the new coldkey. - /// * `weight` - Mutable reference to the weight of the transaction. - /// - /// # Effects - /// - /// * Removes the total stake from the old coldkey. - /// * Inserts the total stake for the new coldkey. - /// * Updates the transaction weight. - pub fn swap_total_coldkey_stake( - old_coldkey: &T::AccountId, - new_coldkey: &T::AccountId, - weight: &mut Weight, - ) { - let stake = TotalColdkeyStake::::get(old_coldkey); - TotalColdkeyStake::::remove(old_coldkey); - TotalColdkeyStake::::insert(new_coldkey, stake); - weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2)); - } - - /// Swaps the stake associated with a coldkey from the old coldkey to the new coldkey. - /// - /// # Arguments - /// - /// * `old_coldkey` - The AccountId of the old coldkey. - /// * `new_coldkey` - The AccountId of the new coldkey. - /// * `weight` - Mutable reference to the weight of the transaction. - /// - /// # Effects - /// - /// * Transfers all stakes from the old coldkey to the new coldkey. - /// * Updates the ownership of hotkeys. - /// * Updates the total stake for both old and new coldkeys. - /// * Updates the transaction weight. - /// - - pub fn swap_stake_for_coldkey( - old_coldkey: &T::AccountId, - new_coldkey: &T::AccountId, - weight: &mut Weight, - ) { - // Retrieve the list of hotkeys owned by the old coldkey - let old_owned_hotkeys: Vec = OwnedHotkeys::::get(old_coldkey); - - // Initialize the total transferred stake to zero - let mut total_transferred_stake: u64 = 0u64; - - // Log the total stake of old and new coldkeys before the swap - log::info!( - "Before swap - Old coldkey total stake: {}", - TotalColdkeyStake::::get(old_coldkey) - ); - log::info!( - "Before swap - New coldkey total stake: {}", - TotalColdkeyStake::::get(new_coldkey) - ); - - // Iterate over each hotkey owned by the old coldkey - for hotkey in old_owned_hotkeys.iter() { - // Retrieve and remove the stake associated with the hotkey and old coldkey - let stake: u64 = Stake::::take(hotkey, old_coldkey); - log::info!("Transferring stake for hotkey {:?}: {}", hotkey, stake); - if stake > 0 { - // Insert the stake for the hotkey and new coldkey - let old_stake = Stake::::get(hotkey, new_coldkey); - Stake::::insert(hotkey, new_coldkey, stake.saturating_add(old_stake)); - total_transferred_stake = total_transferred_stake.saturating_add(stake); - - // Update the owner of the hotkey to the new coldkey - Owner::::insert(hotkey, new_coldkey); - - // Update the transaction weight - weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); - } - } - log::info!( - "Starting transfer of delegated stakes for old coldkey: {:?}", - old_coldkey - ); - - for staking_hotkey in StakingHotkeys::::get(old_coldkey) { - log::info!("Processing staking hotkey: {:?}", staking_hotkey); - if Stake::::contains_key(staking_hotkey.clone(), old_coldkey) { - let hotkey = &staking_hotkey; - // Retrieve and remove the stake associated with the hotkey and old coldkey - let stake: u64 = Stake::::get(hotkey, old_coldkey); - Stake::::remove(hotkey, old_coldkey); - log::info!( - "Transferring delegated stake for hotkey {:?}: {}", - hotkey, - stake - ); - if stake > 0 { - // Insert the stake for the hotkey and new coldkey - let old_stake = Stake::::get(hotkey, new_coldkey); - Stake::::insert(hotkey, new_coldkey, stake.saturating_add(old_stake)); - total_transferred_stake = total_transferred_stake.saturating_add(stake); - log::info!( - "Updated stake for hotkey {:?} under new coldkey {:?}: {}", - hotkey, - new_coldkey, - stake.saturating_add(old_stake) - ); - - // Update the transaction weight - weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 1)); - } - } else { - log::info!( - "No stake found for staking hotkey {:?} under old coldkey {:?}", - staking_hotkey, - old_coldkey - ); - weight.saturating_accrue(T::DbWeight::get().reads(1)); - } - } - - log::info!( - "Completed transfer of delegated stakes for old coldkey: {:?}", - old_coldkey - ); - - // Log the total transferred stake - log::info!("Total transferred stake: {}", total_transferred_stake); - - // Update the total stake for both old and new coldkeys if any stake was transferred - if total_transferred_stake > 0 { - let old_coldkey_stake: u64 = TotalColdkeyStake::::take(old_coldkey); // Remove it here. - let new_coldkey_stake: u64 = TotalColdkeyStake::::get(new_coldkey); - - TotalColdkeyStake::::insert(old_coldkey, 0); - TotalColdkeyStake::::insert( - new_coldkey, - new_coldkey_stake.saturating_add(old_coldkey_stake), - ); - - log::info!("Updated old coldkey stake from {} to 0", old_coldkey_stake); - log::info!( - "Updated new coldkey stake from {} to {}", - new_coldkey_stake, - new_coldkey_stake.saturating_add(old_coldkey_stake) - ); - - // Update the transaction weight - weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); - } - - // Update the list of owned hotkeys for both old and new coldkeys - - let mut new_owned_hotkeys = OwnedHotkeys::::get(new_coldkey); - for hotkey in old_owned_hotkeys { - if !new_owned_hotkeys.contains(&hotkey) { - new_owned_hotkeys.push(hotkey); - } - } - - OwnedHotkeys::::insert(new_coldkey, new_owned_hotkeys); - OwnedHotkeys::::remove(old_coldkey); - weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2)); - - // Update the staking hotkeys for both old and new coldkeys - let staking_hotkeys: Vec = StakingHotkeys::::get(old_coldkey); - - let mut existing_staking_hotkeys = StakingHotkeys::::get(new_coldkey); - for hotkey in staking_hotkeys { - if !existing_staking_hotkeys.contains(&hotkey) { - existing_staking_hotkeys.push(hotkey); - } - } - - StakingHotkeys::::remove(old_coldkey); - StakingHotkeys::::insert(new_coldkey, existing_staking_hotkeys); - weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); - - // Log the total stake of old and new coldkeys after the swap - log::info!( - "After swap - Old coldkey total stake: {}", - TotalColdkeyStake::::get(old_coldkey) - ); - log::info!( - "After swap - New coldkey total stake: {}", - TotalColdkeyStake::::get(new_coldkey) - ); - } - - /// Swaps the total hotkey-coldkey stakes for the current interval from the old coldkey to the new coldkey. - /// - /// # Arguments - /// - /// * `old_coldkey` - The AccountId of the old coldkey. - /// * `new_coldkey` - The AccountId of the new coldkey. - /// * `weight` - Mutable reference to the weight of the transaction. - /// - /// # Effects - /// - /// * Removes all total hotkey-coldkey stakes for the current interval associated with the old coldkey. - /// * Inserts all total hotkey-coldkey stakes for the current interval for the new coldkey. - /// * Updates the transaction weight. - pub fn swap_total_hotkey_coldkey_stakes_this_interval_for_coldkey( - old_coldkey: &T::AccountId, - new_coldkey: &T::AccountId, - weight: &mut Weight, - ) { - weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 0)); - for hotkey in OwnedHotkeys::::get(old_coldkey).iter() { - let (stake, block) = - TotalHotkeyColdkeyStakesThisInterval::::get(&hotkey, old_coldkey); - TotalHotkeyColdkeyStakesThisInterval::::remove(&hotkey, old_coldkey); - TotalHotkeyColdkeyStakesThisInterval::::insert(&hotkey, new_coldkey, (stake, block)); - weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); - } - } - - /// Checks if a coldkey has any associated hotkeys. - /// - /// # Arguments - /// - /// * `coldkey` - The AccountId of the coldkey to check. - /// - /// # Returns - /// - /// * `bool` - True if the coldkey has any associated hotkeys, false otherwise. - pub fn coldkey_has_associated_hotkeys(coldkey: &T::AccountId) -> bool { - !StakingHotkeys::::get(coldkey).is_empty() - } - - /// Swaps the subnet owner from the old coldkey to the new coldkey for all networks where the old coldkey is the owner. - /// - /// # Arguments - /// - /// * `old_coldkey` - The AccountId of the old coldkey. - /// * `new_coldkey` - The AccountId of the new coldkey. - /// * `weight` - Mutable reference to the weight of the transaction. - /// - /// # Effects - /// - /// * Updates the subnet owner to the new coldkey for all networks where the old coldkey was the owner. - /// * Updates the transaction weight. - pub fn swap_subnet_owner_for_coldkey( - old_coldkey: &T::AccountId, - new_coldkey: &T::AccountId, - weight: &mut Weight, - ) { - for netuid in 0..=TotalNetworks::::get() { - let subnet_owner = SubnetOwner::::get(netuid); - if subnet_owner == *old_coldkey { - SubnetOwner::::insert(netuid, new_coldkey.clone()); - weight.saturating_accrue(T::DbWeight::get().writes(1)); - } - } - weight.saturating_accrue(T::DbWeight::get().reads(TotalNetworks::::get() as u64)); - } -} + // /// Swaps the stake associated with a coldkey from the old coldkey to the new coldkey. + // /// + // /// # Arguments + // /// + // /// * `old_coldkey` - The AccountId of the old coldkey. + // /// * `new_coldkey` - The AccountId of the new coldkey. + // /// * `weight` - Mutable reference to the weight of the transaction. + // /// + // /// # Effects + // /// + // /// * Transfers all stakes from the old coldkey to the new coldkey. + // /// * Updates the ownership of hotkeys. + // /// * Updates the total stake for both old and new coldkeys. + // /// * Updates the transaction weight. + // /// + + // pub fn swap_stake_for_coldkey( + // old_coldkey: &T::AccountId, + // new_coldkey: &T::AccountId, + // weight: &mut Weight, + // ) { + // // Retrieve the list of hotkeys owned by the old coldkey + // let old_owned_hotkeys: Vec = OwnedHotkeys::::get(old_coldkey); + + // // Initialize the total transferred stake to zero + // let mut total_transferred_stake: u64 = 0u64; + + // // Log the total stake of old and new coldkeys before the swap + // log::info!( + // "Before swap - Old coldkey total stake: {}", + // TotalColdkeyStake::::get(old_coldkey) + // ); + // log::info!( + // "Before swap - New coldkey total stake: {}", + // TotalColdkeyStake::::get(new_coldkey) + // ); + + // // Iterate over each hotkey owned by the old coldkey + // for hotkey in old_owned_hotkeys.iter() { + // // Retrieve and remove the stake associated with the hotkey and old coldkey + // let stake: u64 = Stake::::take(hotkey, old_coldkey); + // log::info!("Transferring stake for hotkey {:?}: {}", hotkey, stake); + // if stake > 0 { + // // Insert the stake for the hotkey and new coldkey + // let old_stake = Stake::::get(hotkey, new_coldkey); + // Stake::::insert(hotkey, new_coldkey, stake.saturating_add(old_stake)); + // total_transferred_stake = total_transferred_stake.saturating_add(stake); + + // // Update the owner of the hotkey to the new coldkey + // Owner::::insert(hotkey, new_coldkey); + + // // Update the transaction weight + // weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); + // } + // } + // log::info!( + // "Starting transfer of delegated stakes for old coldkey: {:?}", + // old_coldkey + // ); + + // for staking_hotkey in StakingHotkeys::::get(old_coldkey) { + // log::info!("Processing staking hotkey: {:?}", staking_hotkey); + // if Stake::::contains_key(staking_hotkey.clone(), old_coldkey) { + // let hotkey = &staking_hotkey; + // // Retrieve and remove the stake associated with the hotkey and old coldkey + // let stake: u64 = Stake::::get(hotkey, old_coldkey); + // Stake::::remove(hotkey, old_coldkey); + // log::info!( + // "Transferring delegated stake for hotkey {:?}: {}", + // hotkey, + // stake + // ); + // if stake > 0 { + // // Insert the stake for the hotkey and new coldkey + // let old_stake = Stake::::get(hotkey, new_coldkey); + // Stake::::insert(hotkey, new_coldkey, stake.saturating_add(old_stake)); + // total_transferred_stake = total_transferred_stake.saturating_add(stake); + // log::info!( + // "Updated stake for hotkey {:?} under new coldkey {:?}: {}", + // hotkey, + // new_coldkey, + // stake.saturating_add(old_stake) + // ); + + // // Update the transaction weight + // weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 1)); + // } + // } else { + // log::info!( + // "No stake found for staking hotkey {:?} under old coldkey {:?}", + // staking_hotkey, + // old_coldkey + // ); + // weight.saturating_accrue(T::DbWeight::get().reads(1)); + // } + // } + + // log::info!( + // "Completed transfer of delegated stakes for old coldkey: {:?}", + // old_coldkey + // ); + + // // Log the total transferred stake + // log::info!("Total transferred stake: {}", total_transferred_stake); + + // // Update the total stake for both old and new coldkeys if any stake was transferred + // if total_transferred_stake > 0 { + // let old_coldkey_stake: u64 = TotalColdkeyStake::::take(old_coldkey); // Remove it here. + // let new_coldkey_stake: u64 = TotalColdkeyStake::::get(new_coldkey); + + // TotalColdkeyStake::::insert(old_coldkey, 0); + // TotalColdkeyStake::::insert( + // new_coldkey, + // new_coldkey_stake.saturating_add(old_coldkey_stake), + // ); + + // log::info!("Updated old coldkey stake from {} to 0", old_coldkey_stake); + // log::info!( + // "Updated new coldkey stake from {} to {}", + // new_coldkey_stake, + // new_coldkey_stake.saturating_add(old_coldkey_stake) + // ); + + // // Update the transaction weight + // weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); + // } + + // // Update the list of owned hotkeys for both old and new coldkeys + + // let mut new_owned_hotkeys = OwnedHotkeys::::get(new_coldkey); + // for hotkey in old_owned_hotkeys { + // if !new_owned_hotkeys.contains(&hotkey) { + // new_owned_hotkeys.push(hotkey); + // } + // } + + // OwnedHotkeys::::insert(new_coldkey, new_owned_hotkeys); + // OwnedHotkeys::::remove(old_coldkey); + // weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2)); + + // // Update the staking hotkeys for both old and new coldkeys + // let staking_hotkeys: Vec = StakingHotkeys::::get(old_coldkey); + + // let mut existing_staking_hotkeys = StakingHotkeys::::get(new_coldkey); + // for hotkey in staking_hotkeys { + // if !existing_staking_hotkeys.contains(&hotkey) { + // existing_staking_hotkeys.push(hotkey); + // } + // } + + // StakingHotkeys::::remove(old_coldkey); + // StakingHotkeys::::insert(new_coldkey, existing_staking_hotkeys); + // weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); + + // // Log the total stake of old and new coldkeys after the swap + // log::info!( + // "After swap - Old coldkey total stake: {}", + // TotalColdkeyStake::::get(old_coldkey) + // ); + // log::info!( + // "After swap - New coldkey total stake: {}", + // TotalColdkeyStake::::get(new_coldkey) + // ); + // } +} \ No newline at end of file diff --git a/pallets/subtensor/src/swap_hotkey.rs b/pallets/subtensor/src/swap_hotkey.rs index 5c3fecbba5..46ba4546ad 100644 --- a/pallets/subtensor/src/swap_hotkey.rs +++ b/pallets/subtensor/src/swap_hotkey.rs @@ -145,6 +145,7 @@ impl Pallet { coldkey: &T::AccountId, weight: &mut Weight, ) -> DispatchResult { + // 1. Swap owner. // Owner( hotkey ) -> coldkey -- the coldkey that owns the hotkey. Owner::::remove(old_hotkey); diff --git a/pallets/subtensor/tests/swap.rs b/pallets/subtensor/tests/swap_coldkey.rs similarity index 63% rename from pallets/subtensor/tests/swap.rs rename to pallets/subtensor/tests/swap_coldkey.rs index 0a3a85dff3..0d1e652e60 100644 --- a/pallets/subtensor/tests/swap.rs +++ b/pallets/subtensor/tests/swap_coldkey.rs @@ -1,5 +1,4 @@ #![allow(unused, clippy::indexing_slicing, clippy::panic, clippy::unwrap_used)] - use codec::Encode; use frame_support::weights::Weight; use frame_support::{assert_err, assert_noop, assert_ok}; @@ -7,8 +6,383 @@ use frame_system::{Config, RawOrigin}; mod mock; use mock::*; use pallet_subtensor::*; +use sp_core::H256; use sp_core::U256; +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_total_hotkey_coldkey_stakes_this_interval --exact --nocapture +#[test] +fn test_swap_total_hotkey_coldkey_stakes_this_interval() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let hotkey = U256::from(3); + let stake = 100; + let block = 42; + + OwnedHotkeys::::insert(&old_coldkey, vec![hotkey]); + TotalHotkeyColdkeyStakesThisInterval::::insert(&hotkey, &old_coldkey, (stake, block)); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert!(!TotalHotkeyColdkeyStakesThisInterval::::contains_key(&hotkey, &old_coldkey)); + assert_eq!(TotalHotkeyColdkeyStakesThisInterval::::get(&hotkey, &new_coldkey), (stake, block)); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_subnet_owner --exact --nocapture +#[test] +fn test_swap_subnet_owner() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let netuid = 1u16; + + add_network(netuid, 1, 0); + SubnetOwner::::insert(netuid, old_coldkey); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert_eq!(SubnetOwner::::get(netuid), new_coldkey); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_stake --exact --nocapture +#[test] +fn test_swap_stake() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let hotkey = U256::from(3); + let stake = 100; + + StakingHotkeys::::insert(&old_coldkey, vec![hotkey]); + Stake::::insert(&hotkey, &old_coldkey, stake); + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert!(!Stake::::contains_key(&hotkey, &old_coldkey)); + assert_eq!(Stake::::get(&hotkey, &new_coldkey), stake); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_total_coldkey_stake --exact --nocapture +#[test] +fn test_swap_total_coldkey_stake() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let stake = 100; + + TotalColdkeyStake::::insert(&old_coldkey, stake); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); + assert_eq!(TotalColdkeyStake::::get(&new_coldkey), stake); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_staking_hotkeys --exact --nocapture +#[test] +fn test_swap_staking_hotkeys() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let hotkey = U256::from(3); + + StakingHotkeys::::insert(&old_coldkey, vec![hotkey]); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert!(StakingHotkeys::::get(&old_coldkey).is_empty()); + assert_eq!(StakingHotkeys::::get(&new_coldkey), vec![hotkey]); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_hotkey_owners --exact --nocapture +#[test] +fn test_swap_hotkey_owners() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let hotkey = U256::from(3); + + Owner::::insert(&hotkey, &old_coldkey); + OwnedHotkeys::::insert(&old_coldkey, vec![hotkey]); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert_eq!(Owner::::get(&hotkey), new_coldkey); + assert!(OwnedHotkeys::::get(&old_coldkey).is_empty()); + assert_eq!(OwnedHotkeys::::get(&new_coldkey), vec![hotkey]); + }); +} +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_transfer_remaining_balance --exact --nocapture +#[test] +fn test_transfer_remaining_balance() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let balance = 100; + + SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, balance); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert_eq!(SubtensorModule::get_coldkey_balance(&old_coldkey), 0); + assert_eq!(SubtensorModule::get_coldkey_balance(&new_coldkey), balance); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_with_no_stake --exact --nocapture +#[test] +fn test_swap_with_no_stake() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); + assert_eq!(TotalColdkeyStake::::get(&new_coldkey), 0); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_with_multiple_hotkeys --exact --nocapture +#[test] +fn test_swap_with_multiple_hotkeys() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let hotkey1 = U256::from(3); + let hotkey2 = U256::from(4); + + OwnedHotkeys::::insert(&old_coldkey, vec![hotkey1, hotkey2]); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert!(OwnedHotkeys::::get(&old_coldkey).is_empty()); + assert_eq!(OwnedHotkeys::::get(&new_coldkey), vec![hotkey1, hotkey2]); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_with_multiple_subnets --exact --nocapture +#[test] +fn test_swap_with_multiple_subnets() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let netuid1 = 1u16; + let netuid2 = 2u16; + + add_network(netuid1, 1, 0); + add_network(netuid2, 1, 0); + SubnetOwner::::insert(netuid1, &old_coldkey); + SubnetOwner::::insert(netuid2, &old_coldkey); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert_eq!(SubnetOwner::::get(netuid1), new_coldkey); + assert_eq!(SubnetOwner::::get(netuid2), new_coldkey); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_with_zero_balance --exact --nocapture +#[test] +fn test_swap_with_zero_balance() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert_eq!(Balances::free_balance(&old_coldkey), 0); + assert_eq!(Balances::free_balance(&new_coldkey), 0); + }); +} + + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_idempotency --exact --nocapture +#[test] +fn test_swap_idempotency() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let stake = 100; + + TotalColdkeyStake::::insert(&old_coldkey, stake); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); + assert_eq!(TotalColdkeyStake::::get(&new_coldkey), stake); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_with_max_values --exact --nocapture +#[test] +fn test_swap_with_max_values() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let max_stake = u64::MAX; + + TotalColdkeyStake::::insert(&old_coldkey, max_stake); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); + assert_eq!(TotalColdkeyStake::::get(&new_coldkey), max_stake); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_with_non_existent_new_coldkey --exact --nocapture +#[test] +fn test_swap_with_non_existent_new_coldkey() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let stake = 100; + + TotalColdkeyStake::::insert(&old_coldkey, stake); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); + assert_eq!(TotalColdkeyStake::::get(&new_coldkey), stake); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_with_overflow_in_stake_addition --exact --nocapture +#[test] +fn test_swap_with_overflow_in_stake_addition() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let max_stake = u64::MAX; + + TotalColdkeyStake::::insert(&old_coldkey, max_stake); + TotalColdkeyStake::::insert(&new_coldkey, 1); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); + assert_eq!(TotalColdkeyStake::::get(&new_coldkey), max_stake); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_with_max_hotkeys --exact --nocapture +#[test] +fn test_swap_with_max_hotkeys() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let max_hotkeys = 1000; + let hotkeys: Vec = (0..max_hotkeys).map(U256::from).collect(); + + OwnedHotkeys::::insert(&old_coldkey, hotkeys.clone()); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert!(OwnedHotkeys::::get(&old_coldkey).is_empty()); + assert_eq!(OwnedHotkeys::::get(&new_coldkey), hotkeys); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_effect_on_delegated_stake --exact --nocapture +#[test] +fn test_swap_effect_on_delegated_stake() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let delegator = U256::from(3); + let hotkey = U256::from(4); + let stake = 100; + + StakingHotkeys::::insert(&old_coldkey, vec![hotkey]); + StakingHotkeys::::insert(&delegator, vec![hotkey]); + Stake::::insert(&hotkey, &old_coldkey, stake); + Stake::::insert(&hotkey, &delegator, stake); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert_eq!(Stake::::get(&hotkey, &new_coldkey), stake); + assert_eq!(Stake::::get(&hotkey, &delegator), stake); + assert_eq!(Stake::::get(&hotkey, &old_coldkey), 0); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_concurrent_modifications --exact --nocapture +#[test] +fn test_swap_concurrent_modifications() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let hotkey = U256::from(3); + let netuid: u16 = 1; + let initial_stake = 100; + let additional_stake = 50; + + StakingHotkeys::::insert(&old_coldkey, vec![hotkey]); + Stake::::insert(&hotkey, &old_coldkey, initial_stake); + + // Simulate concurrent stake addition + add_network(netuid, 1,1); + SubtensorModule::add_balance_to_coldkey_account(&new_coldkey, additional_stake); + register_ok_neuron(netuid, hotkey, new_coldkey, 1001000); + assert_ok!(SubtensorModule::add_stake( + <::RuntimeOrigin>::signed(new_coldkey), + hotkey, + additional_stake + )); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + assert_eq!(Stake::::get(&hotkey, &new_coldkey), initial_stake + additional_stake - 1); + assert!(!Stake::::contains_key(&hotkey, &old_coldkey)); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_with_invalid_subnet_ownership --exact --nocapture +#[test] +fn test_swap_with_invalid_subnet_ownership() { + new_test_ext(1).execute_with(|| { + let old_coldkey = U256::from(1); + let new_coldkey = U256::from(2); + let netuid = 1u16; + + SubnetOwner::::insert(netuid, old_coldkey); + + // Simulate an invalid state where the subnet owner doesn't match the old_coldkey + SubnetOwner::::insert(netuid, U256::from(3)); + + let mut weight = Weight::zero(); + assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + + // The swap should not affect the mismatched subnet ownership + assert_eq!(SubnetOwner::::get(netuid), U256::from(3)); + }); +} + + #[test] fn test_do_swap_coldkey_success() { new_test_ext(1).execute_with(|| { @@ -180,7 +554,7 @@ fn test_swap_stake_for_coldkey() { let initial_total_stake = SubtensorModule::get_total_stake(); // Perform the swap - SubtensorModule::swap_stake_for_coldkey(&old_coldkey, &new_coldkey, &mut weight); + SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight); // Verify stake is additive, not replaced assert_eq!( @@ -222,6 +596,7 @@ fn test_swap_stake_for_coldkey() { }); } +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap_coldkey -- test_swap_staking_hotkeys_for_coldkey --exact --nocapture #[test] fn test_swap_staking_hotkeys_for_coldkey() { new_test_ext(1).execute_with(|| { @@ -248,7 +623,7 @@ fn test_swap_staking_hotkeys_for_coldkey() { TotalStake::::put(total_stake); // Perform the swap - SubtensorModule::swap_stake_for_coldkey(&old_coldkey, &new_coldkey, &mut weight); + SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight); // Verify StakingHotkeys transfer assert_eq!( @@ -259,6 +634,7 @@ fn test_swap_staking_hotkeys_for_coldkey() { }); } +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap_coldkey -- test_swap_delegated_stake_for_coldkey --exact --nocapture #[test] fn test_swap_delegated_stake_for_coldkey() { new_test_ext(1).execute_with(|| { @@ -291,7 +667,7 @@ fn test_swap_delegated_stake_for_coldkey() { let initial_total_stake = SubtensorModule::get_total_stake(); // Perform the swap - SubtensorModule::swap_stake_for_coldkey(&old_coldkey, &new_coldkey, &mut weight); + SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight); // Verify stake transfer assert_eq!(Stake::::get(hotkey1, new_coldkey), stake_amount1); @@ -321,6 +697,7 @@ fn test_swap_delegated_stake_for_coldkey() { }); } +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap_coldkey -- test_swap_total_hotkey_coldkey_stakes_this_interval_for_coldkey --exact --nocapture #[test] fn test_swap_total_hotkey_coldkey_stakes_this_interval_for_coldkey() { new_test_ext(1).execute_with(|| { @@ -340,7 +717,7 @@ fn test_swap_total_hotkey_coldkey_stakes_this_interval_for_coldkey() { OwnedHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); // Perform the swap - SubtensorModule::swap_total_hotkey_coldkey_stakes_this_interval_for_coldkey( + SubtensorModule::perform_swap_coldkey( &old_coldkey, &new_coldkey, &mut weight, @@ -363,13 +740,10 @@ fn test_swap_total_hotkey_coldkey_stakes_this_interval_for_coldkey() { old_coldkey, hotkey2 )); - - // Verify weight update - let expected_weight = ::DbWeight::get().reads_writes(5, 4); - assert_eq!(weight, expected_weight); }); } +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap_coldkey -- test_swap_subnet_owner_for_coldkey --exact --nocapture #[test] fn test_swap_subnet_owner_for_coldkey() { new_test_ext(1).execute_with(|| { @@ -380,6 +754,8 @@ fn test_swap_subnet_owner_for_coldkey() { let mut weight = Weight::zero(); // Initialize SubnetOwner for old_coldkey + add_network(netuid1, 13, 0); + add_network(netuid2, 14, 0); SubnetOwner::::insert(netuid1, old_coldkey); SubnetOwner::::insert(netuid2, old_coldkey); @@ -387,18 +763,15 @@ fn test_swap_subnet_owner_for_coldkey() { TotalNetworks::::put(3); // Perform the swap - SubtensorModule::swap_subnet_owner_for_coldkey(&old_coldkey, &new_coldkey, &mut weight); + SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight); // Verify the swap assert_eq!(SubnetOwner::::get(netuid1), new_coldkey); assert_eq!(SubnetOwner::::get(netuid2), new_coldkey); - - // Verify weight update - let expected_weight = ::DbWeight::get().reads_writes(3, 2); - assert_eq!(weight, expected_weight); }); } +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap_coldkey -- test_do_swap_coldkey_with_subnet_ownership --exact --nocapture #[test] fn test_do_swap_coldkey_with_subnet_ownership() { new_test_ext(1).execute_with(|| { @@ -432,7 +805,7 @@ fn test_do_swap_coldkey_with_subnet_ownership() { assert_eq!(SubnetOwner::::get(netuid), new_coldkey); }); } - +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap_coldkey -- test_coldkey_has_associated_hotkeys --exact --nocapture #[test] fn test_coldkey_has_associated_hotkeys() { new_test_ext(1).execute_with(|| { @@ -447,7 +820,7 @@ fn test_coldkey_has_associated_hotkeys() { }); } -// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap -- test_coldkey_swap_total --exact --nocapture +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap_coldkey -- test_coldkey_swap_total --exact --nocapture #[test] fn test_coldkey_swap_total() { new_test_ext(1).execute_with(|| { @@ -669,9 +1042,11 @@ fn test_coldkey_swap_total() { // Perform the swap let new_coldkey = U256::from(1100); assert_eq!(SubtensorModule::get_total_stake_for_coldkey(&coldkey), 600); + let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( &coldkey, - &new_coldkey + &new_coldkey, + &mut weight )); assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&new_coldkey), @@ -741,7 +1116,7 @@ fn test_coldkey_swap_total() { ); }); } - +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap_coldkey -- test_swap_senate_member --exact --nocapture #[test] fn test_swap_senate_member() { new_test_ext(1).execute_with(|| { @@ -787,7 +1162,7 @@ fn test_swap_senate_member() { }); } -// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap -- test_coldkey_delegations --exact --nocapture +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap_coldkey -- test_coldkey_delegations --exact --nocapture #[test] fn test_coldkey_delegations() { new_test_ext(1).execute_with(|| { @@ -809,9 +1184,11 @@ fn test_coldkey_delegations() { delegate, 100 )); + let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( &coldkey, - &new_coldkey + &new_coldkey, + &mut weight )); assert_eq!(SubtensorModule::get_total_stake_for_hotkey(&delegate), 100); assert_eq!(SubtensorModule::get_total_stake_for_coldkey(&coldkey), 0); @@ -823,3 +1200,4 @@ fn test_coldkey_delegations() { assert_eq!(Stake::::get(delegate, coldkey), 0); }); } + From cf9869d480c6bc982acfe63f64279c53b4679805 Mon Sep 17 00:00:00 2001 From: const Date: Tue, 23 Jul 2024 13:23:43 -0500 Subject: [PATCH 5/7] pre merge --- pallets/subtensor/src/swap_coldkey.rs | 38 ++--- pallets/subtensor/src/swap_hotkey.rs | 1 - pallets/subtensor/tests/swap_coldkey.rs | 181 +++++++++++++++++------- pallets/subtensor/tests/swap_hotkey.rs | 8 +- 4 files changed, 155 insertions(+), 73 deletions(-) diff --git a/pallets/subtensor/src/swap_coldkey.rs b/pallets/subtensor/src/swap_coldkey.rs index 9a5ad515b4..f8049cd6f7 100644 --- a/pallets/subtensor/src/swap_coldkey.rs +++ b/pallets/subtensor/src/swap_coldkey.rs @@ -1,11 +1,9 @@ - use super::*; use crate::MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP; use frame_support::weights::Weight; use sp_core::{Get, U256}; impl Pallet { - /// Swaps the coldkey associated with a set of hotkeys from an old coldkey to a new coldkey. /// /// # Arguments @@ -69,7 +67,8 @@ impl Pallet { ); // 7. Remove and burn the swap cost from the old coldkey's account - let actual_burn_amount = Self::remove_balance_from_coldkey_account(&old_coldkey, swap_cost)?; + let actual_burn_amount = + Self::remove_balance_from_coldkey_account(&old_coldkey, swap_cost)?; Self::burn_tokens(actual_burn_amount); // 8. Update the weight for the balance operations @@ -92,7 +91,6 @@ impl Pallet { Ok(Some(weight).into()) } - /// Performs the actual coldkey swap operation, transferring all associated data and balances from the old coldkey to the new coldkey. /// /// # Arguments @@ -134,12 +132,16 @@ impl Pallet { /// # Notes /// /// This function is a critical part of the coldkey swap process and should be called only after all necessary checks and validations have been performed. - pub fn perform_swap_coldkey( old_coldkey: &T::AccountId, new_coldkey: &T::AccountId, weight: &mut Weight ) -> DispatchResult { - + pub fn perform_swap_coldkey( + old_coldkey: &T::AccountId, + new_coldkey: &T::AccountId, + weight: &mut Weight, + ) -> DispatchResult { // 1. Swap TotalHotkeyColdkeyStakesThisInterval // TotalHotkeyColdkeyStakesThisInterval: MAP ( hotkey, coldkey ) --> ( stake, block ) | Stake of the hotkey for the coldkey. for hotkey in OwnedHotkeys::::get(old_coldkey).iter() { - let (stake, block) = TotalHotkeyColdkeyStakesThisInterval::::get(&hotkey, old_coldkey); + let (stake, block) = + TotalHotkeyColdkeyStakesThisInterval::::get(&hotkey, old_coldkey); TotalHotkeyColdkeyStakesThisInterval::::remove(&hotkey, old_coldkey); TotalHotkeyColdkeyStakesThisInterval::::insert(&hotkey, new_coldkey, (stake, block)); weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2)); @@ -157,11 +159,11 @@ impl Pallet { // 3. Swap Stake. // Stake: MAP ( hotkey, coldkey ) --> u64 | Stake of the hotkey for the coldkey. - for hotkey in StakingHotkeys::::get( old_coldkey ) { + for hotkey in StakingHotkeys::::get(old_coldkey) { // Get the stake on the old (hot,coldkey) account. - let old_stake: u64 = Stake::::get( &hotkey, old_coldkey ); + let old_stake: u64 = Stake::::get(&hotkey, old_coldkey); // Get the stake on the new (hot,coldkey) account. - let new_stake: u64 = Stake::::get( &hotkey, new_coldkey ); + let new_stake: u64 = Stake::::get(&hotkey, new_coldkey); // Add the stake to new account. Stake::::insert(&hotkey, new_coldkey, new_stake.saturating_add(old_stake)); // Remove the value from the old account. @@ -169,7 +171,7 @@ impl Pallet { // Add the weight for the read and write. weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); } - + // 4. Swap total coldkey stake. // TotalColdkeyStake: MAP ( coldkey ) --> u64 | Total stake of the coldkey. let old_coldkey_stake: u64 = TotalColdkeyStake::::get(old_coldkey); @@ -178,13 +180,16 @@ impl Pallet { // Remove the value from the old account. TotalColdkeyStake::::insert(old_coldkey, 0); // Add the stake to new account. - TotalColdkeyStake::::insert(new_coldkey, new_coldkey_stake.saturating_add(old_coldkey_stake) ); + TotalColdkeyStake::::insert( + new_coldkey, + new_coldkey_stake.saturating_add(old_coldkey_stake), + ); weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); // 5. Swap StakingHotkeys. // StakingHotkeys: MAP ( coldkey ) --> Vec | Hotkeys staking for the coldkey. let old_staking_hotkeys: Vec = StakingHotkeys::::get(old_coldkey); - let mut new_staking_hotkeys: Vec = StakingHotkeys::::get( new_coldkey ); + let mut new_staking_hotkeys: Vec = StakingHotkeys::::get(new_coldkey); for hotkey in old_staking_hotkeys { // If the hotkey is not already in the new coldkey, add it. if !new_staking_hotkeys.contains(&hotkey) { @@ -220,7 +225,7 @@ impl Pallet { let remaining_balance = Self::get_coldkey_balance(old_coldkey); if remaining_balance > 0 { if let Err(e) = Self::kill_coldkey_account(old_coldkey, remaining_balance) { - return Err(e.into()); + return Err(e); } Self::add_balance_to_coldkey_account(new_coldkey, remaining_balance); } @@ -230,7 +235,6 @@ impl Pallet { Ok(()) } - /// Checks if a coldkey is currently in arbitration. /// /// # Arguments @@ -454,8 +458,6 @@ impl Pallet { Ok(weight_used) } - - // /// Swaps the stake associated with a coldkey from the old coldkey to the new coldkey. // /// // /// # Arguments @@ -620,4 +622,4 @@ impl Pallet { // TotalColdkeyStake::::get(new_coldkey) // ); // } -} \ No newline at end of file +} diff --git a/pallets/subtensor/src/swap_hotkey.rs b/pallets/subtensor/src/swap_hotkey.rs index 46ba4546ad..5c3fecbba5 100644 --- a/pallets/subtensor/src/swap_hotkey.rs +++ b/pallets/subtensor/src/swap_hotkey.rs @@ -145,7 +145,6 @@ impl Pallet { coldkey: &T::AccountId, weight: &mut Weight, ) -> DispatchResult { - // 1. Swap owner. // Owner( hotkey ) -> coldkey -- the coldkey that owns the hotkey. Owner::::remove(old_hotkey); diff --git a/pallets/subtensor/tests/swap_coldkey.rs b/pallets/subtensor/tests/swap_coldkey.rs index 0d1e652e60..b85b6c60fd 100644 --- a/pallets/subtensor/tests/swap_coldkey.rs +++ b/pallets/subtensor/tests/swap_coldkey.rs @@ -21,12 +21,22 @@ fn test_swap_total_hotkey_coldkey_stakes_this_interval() { OwnedHotkeys::::insert(&old_coldkey, vec![hotkey]); TotalHotkeyColdkeyStakesThisInterval::::insert(&hotkey, &old_coldkey, (stake, block)); - + let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); - assert!(!TotalHotkeyColdkeyStakesThisInterval::::contains_key(&hotkey, &old_coldkey)); - assert_eq!(TotalHotkeyColdkeyStakesThisInterval::::get(&hotkey, &new_coldkey), (stake, block)); + assert!(!TotalHotkeyColdkeyStakesThisInterval::::contains_key( + &hotkey, + &old_coldkey + )); + assert_eq!( + TotalHotkeyColdkeyStakesThisInterval::::get(&hotkey, &new_coldkey), + (stake, block) + ); }); } @@ -40,9 +50,13 @@ fn test_swap_subnet_owner() { add_network(netuid, 1, 0); SubnetOwner::::insert(netuid, old_coldkey); - + let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert_eq!(SubnetOwner::::get(netuid), new_coldkey); }); @@ -60,7 +74,11 @@ fn test_swap_stake() { StakingHotkeys::::insert(&old_coldkey, vec![hotkey]); Stake::::insert(&hotkey, &old_coldkey, stake); let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert!(!Stake::::contains_key(&hotkey, &old_coldkey)); assert_eq!(Stake::::get(&hotkey, &new_coldkey), stake); @@ -76,9 +94,13 @@ fn test_swap_total_coldkey_stake() { let stake = 100; TotalColdkeyStake::::insert(&old_coldkey, stake); - + let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); assert_eq!(TotalColdkeyStake::::get(&new_coldkey), stake); @@ -94,9 +116,13 @@ fn test_swap_staking_hotkeys() { let hotkey = U256::from(3); StakingHotkeys::::insert(&old_coldkey, vec![hotkey]); - + let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert!(StakingHotkeys::::get(&old_coldkey).is_empty()); assert_eq!(StakingHotkeys::::get(&new_coldkey), vec![hotkey]); @@ -113,9 +139,13 @@ fn test_swap_hotkey_owners() { Owner::::insert(&hotkey, &old_coldkey); OwnedHotkeys::::insert(&old_coldkey, vec![hotkey]); - + let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert_eq!(Owner::::get(&hotkey), new_coldkey); assert!(OwnedHotkeys::::get(&old_coldkey).is_empty()); @@ -131,9 +161,13 @@ fn test_transfer_remaining_balance() { let balance = 100; SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, balance); - + let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert_eq!(SubtensorModule::get_coldkey_balance(&old_coldkey), 0); assert_eq!(SubtensorModule::get_coldkey_balance(&new_coldkey), balance); @@ -148,7 +182,11 @@ fn test_swap_with_no_stake() { let new_coldkey = U256::from(2); let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); assert_eq!(TotalColdkeyStake::::get(&new_coldkey), 0); @@ -165,12 +203,19 @@ fn test_swap_with_multiple_hotkeys() { let hotkey2 = U256::from(4); OwnedHotkeys::::insert(&old_coldkey, vec![hotkey1, hotkey2]); - + let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert!(OwnedHotkeys::::get(&old_coldkey).is_empty()); - assert_eq!(OwnedHotkeys::::get(&new_coldkey), vec![hotkey1, hotkey2]); + assert_eq!( + OwnedHotkeys::::get(&new_coldkey), + vec![hotkey1, hotkey2] + ); }); } @@ -187,9 +232,13 @@ fn test_swap_with_multiple_subnets() { add_network(netuid2, 1, 0); SubnetOwner::::insert(netuid1, &old_coldkey); SubnetOwner::::insert(netuid2, &old_coldkey); - + let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert_eq!(SubnetOwner::::get(netuid1), new_coldkey); assert_eq!(SubnetOwner::::get(netuid2), new_coldkey); @@ -204,14 +253,17 @@ fn test_swap_with_zero_balance() { let new_coldkey = U256::from(2); let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert_eq!(Balances::free_balance(&old_coldkey), 0); assert_eq!(Balances::free_balance(&new_coldkey), 0); }); } - // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_swap_idempotency --exact --nocapture #[test] fn test_swap_idempotency() { @@ -221,10 +273,18 @@ fn test_swap_idempotency() { let stake = 100; TotalColdkeyStake::::insert(&old_coldkey, stake); - + let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); assert_eq!(TotalColdkeyStake::::get(&new_coldkey), stake); @@ -240,9 +300,13 @@ fn test_swap_with_max_values() { let max_stake = u64::MAX; TotalColdkeyStake::::insert(&old_coldkey, max_stake); - + let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); assert_eq!(TotalColdkeyStake::::get(&new_coldkey), max_stake); @@ -258,9 +322,13 @@ fn test_swap_with_non_existent_new_coldkey() { let stake = 100; TotalColdkeyStake::::insert(&old_coldkey, stake); - + let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); assert_eq!(TotalColdkeyStake::::get(&new_coldkey), stake); @@ -277,9 +345,13 @@ fn test_swap_with_overflow_in_stake_addition() { TotalColdkeyStake::::insert(&old_coldkey, max_stake); TotalColdkeyStake::::insert(&new_coldkey, 1); - + let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); assert_eq!(TotalColdkeyStake::::get(&new_coldkey), max_stake); @@ -296,9 +368,13 @@ fn test_swap_with_max_hotkeys() { let hotkeys: Vec = (0..max_hotkeys).map(U256::from).collect(); OwnedHotkeys::::insert(&old_coldkey, hotkeys.clone()); - + let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert!(OwnedHotkeys::::get(&old_coldkey).is_empty()); assert_eq!(OwnedHotkeys::::get(&new_coldkey), hotkeys); @@ -319,9 +395,13 @@ fn test_swap_effect_on_delegated_stake() { StakingHotkeys::::insert(&delegator, vec![hotkey]); Stake::::insert(&hotkey, &old_coldkey, stake); Stake::::insert(&hotkey, &delegator, stake); - + let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); assert_eq!(Stake::::get(&hotkey, &new_coldkey), stake); assert_eq!(Stake::::get(&hotkey, &delegator), stake); @@ -344,7 +424,7 @@ fn test_swap_concurrent_modifications() { Stake::::insert(&hotkey, &old_coldkey, initial_stake); // Simulate concurrent stake addition - add_network(netuid, 1,1); + add_network(netuid, 1, 1); SubtensorModule::add_balance_to_coldkey_account(&new_coldkey, additional_stake); register_ok_neuron(netuid, hotkey, new_coldkey, 1001000); assert_ok!(SubtensorModule::add_stake( @@ -354,9 +434,16 @@ fn test_swap_concurrent_modifications() { )); let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); - assert_eq!(Stake::::get(&hotkey, &new_coldkey), initial_stake + additional_stake - 1); + assert_eq!( + Stake::::get(&hotkey, &new_coldkey), + initial_stake + additional_stake - 1 + ); assert!(!Stake::::contains_key(&hotkey, &old_coldkey)); }); } @@ -370,19 +457,22 @@ fn test_swap_with_invalid_subnet_ownership() { let netuid = 1u16; SubnetOwner::::insert(netuid, old_coldkey); - + // Simulate an invalid state where the subnet owner doesn't match the old_coldkey SubnetOwner::::insert(netuid, U256::from(3)); let mut weight = Weight::zero(); - assert_ok!(SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &old_coldkey, + &new_coldkey, + &mut weight + )); // The swap should not affect the mismatched subnet ownership assert_eq!(SubnetOwner::::get(netuid), U256::from(3)); }); } - #[test] fn test_do_swap_coldkey_success() { new_test_ext(1).execute_with(|| { @@ -717,11 +807,7 @@ fn test_swap_total_hotkey_coldkey_stakes_this_interval_for_coldkey() { OwnedHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); // Perform the swap - SubtensorModule::perform_swap_coldkey( - &old_coldkey, - &new_coldkey, - &mut weight, - ); + SubtensorModule::perform_swap_coldkey(&old_coldkey, &new_coldkey, &mut weight); // Verify the swap assert_eq!( @@ -1200,4 +1286,3 @@ fn test_coldkey_delegations() { assert_eq!(Stake::::get(delegate, coldkey), 0); }); } - diff --git a/pallets/subtensor/tests/swap_hotkey.rs b/pallets/subtensor/tests/swap_hotkey.rs index 9903829186..c6a05f2b6f 100644 --- a/pallets/subtensor/tests/swap_hotkey.rs +++ b/pallets/subtensor/tests/swap_hotkey.rs @@ -96,8 +96,7 @@ fn test_swap_total_hotkey_coldkey_stakes_this_interval() { )); assert!(!TotalHotkeyColdkeyStakesThisInterval::::contains_key( - old_hotkey, - coldkey + old_hotkey, coldkey )); assert_eq!( TotalHotkeyColdkeyStakesThisInterval::::get(new_hotkey, coldkey), @@ -957,9 +956,6 @@ fn test_swap_hotkey_error_cases() { )); // Check balance after swap - assert_eq!( - Balances::free_balance(coldkey), - initial_balance - swap_cost - ); + assert_eq!(Balances::free_balance(coldkey), initial_balance - swap_cost); }); } From 9b18c41695a6f177776d55765a1213ddd9f48312 Mon Sep 17 00:00:00 2001 From: const Date: Tue, 23 Jul 2024 13:25:51 -0500 Subject: [PATCH 6/7] fmt and clippy --- pallets/subtensor/src/swap_coldkey.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pallets/subtensor/src/swap_coldkey.rs b/pallets/subtensor/src/swap_coldkey.rs index f8049cd6f7..04d9a22dc7 100644 --- a/pallets/subtensor/src/swap_coldkey.rs +++ b/pallets/subtensor/src/swap_coldkey.rs @@ -224,9 +224,7 @@ impl Pallet { // Transfer any remaining balance from old_coldkey to new_coldkey let remaining_balance = Self::get_coldkey_balance(old_coldkey); if remaining_balance > 0 { - if let Err(e) = Self::kill_coldkey_account(old_coldkey, remaining_balance) { - return Err(e); - } + Self::kill_coldkey_account(old_coldkey, remaining_balance)?; Self::add_balance_to_coldkey_account(new_coldkey, remaining_balance); } weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); From 6f923c92d5ac6bcad26374776acd272aa9cda46a Mon Sep 17 00:00:00 2001 From: const Date: Tue, 23 Jul 2024 13:37:02 -0500 Subject: [PATCH 7/7] clippy --- pallets/subtensor/tests/swap_coldkey.rs | 112 ++++++++++++------------ 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/pallets/subtensor/tests/swap_coldkey.rs b/pallets/subtensor/tests/swap_coldkey.rs index b85b6c60fd..9203e2b3f9 100644 --- a/pallets/subtensor/tests/swap_coldkey.rs +++ b/pallets/subtensor/tests/swap_coldkey.rs @@ -19,8 +19,8 @@ fn test_swap_total_hotkey_coldkey_stakes_this_interval() { let stake = 100; let block = 42; - OwnedHotkeys::::insert(&old_coldkey, vec![hotkey]); - TotalHotkeyColdkeyStakesThisInterval::::insert(&hotkey, &old_coldkey, (stake, block)); + OwnedHotkeys::::insert(old_coldkey, vec![hotkey]); + TotalHotkeyColdkeyStakesThisInterval::::insert(hotkey, old_coldkey, (stake, block)); let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( @@ -30,11 +30,11 @@ fn test_swap_total_hotkey_coldkey_stakes_this_interval() { )); assert!(!TotalHotkeyColdkeyStakesThisInterval::::contains_key( - &hotkey, - &old_coldkey + hotkey, + old_coldkey )); assert_eq!( - TotalHotkeyColdkeyStakesThisInterval::::get(&hotkey, &new_coldkey), + TotalHotkeyColdkeyStakesThisInterval::::get(hotkey, new_coldkey), (stake, block) ); }); @@ -71,8 +71,8 @@ fn test_swap_stake() { let hotkey = U256::from(3); let stake = 100; - StakingHotkeys::::insert(&old_coldkey, vec![hotkey]); - Stake::::insert(&hotkey, &old_coldkey, stake); + StakingHotkeys::::insert(old_coldkey, vec![hotkey]); + Stake::::insert(hotkey, old_coldkey, stake); let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( &old_coldkey, @@ -80,8 +80,8 @@ fn test_swap_stake() { &mut weight )); - assert!(!Stake::::contains_key(&hotkey, &old_coldkey)); - assert_eq!(Stake::::get(&hotkey, &new_coldkey), stake); + assert!(!Stake::::contains_key(hotkey, old_coldkey)); + assert_eq!(Stake::::get(hotkey, new_coldkey), stake); }); } @@ -93,7 +93,7 @@ fn test_swap_total_coldkey_stake() { let new_coldkey = U256::from(2); let stake = 100; - TotalColdkeyStake::::insert(&old_coldkey, stake); + TotalColdkeyStake::::insert(old_coldkey, stake); let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( @@ -102,8 +102,8 @@ fn test_swap_total_coldkey_stake() { &mut weight )); - assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); - assert_eq!(TotalColdkeyStake::::get(&new_coldkey), stake); + assert_eq!(TotalColdkeyStake::::get(old_coldkey), 0); + assert_eq!(TotalColdkeyStake::::get(new_coldkey), stake); }); } @@ -115,7 +115,7 @@ fn test_swap_staking_hotkeys() { let new_coldkey = U256::from(2); let hotkey = U256::from(3); - StakingHotkeys::::insert(&old_coldkey, vec![hotkey]); + StakingHotkeys::::insert(old_coldkey, vec![hotkey]); let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( @@ -124,8 +124,8 @@ fn test_swap_staking_hotkeys() { &mut weight )); - assert!(StakingHotkeys::::get(&old_coldkey).is_empty()); - assert_eq!(StakingHotkeys::::get(&new_coldkey), vec![hotkey]); + assert!(StakingHotkeys::::get(old_coldkey).is_empty()); + assert_eq!(StakingHotkeys::::get(new_coldkey), vec![hotkey]); }); } @@ -137,8 +137,8 @@ fn test_swap_hotkey_owners() { let new_coldkey = U256::from(2); let hotkey = U256::from(3); - Owner::::insert(&hotkey, &old_coldkey); - OwnedHotkeys::::insert(&old_coldkey, vec![hotkey]); + Owner::::insert(hotkey, old_coldkey); + OwnedHotkeys::::insert(old_coldkey, vec![hotkey]); let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( @@ -147,9 +147,9 @@ fn test_swap_hotkey_owners() { &mut weight )); - assert_eq!(Owner::::get(&hotkey), new_coldkey); - assert!(OwnedHotkeys::::get(&old_coldkey).is_empty()); - assert_eq!(OwnedHotkeys::::get(&new_coldkey), vec![hotkey]); + assert_eq!(Owner::::get(hotkey), new_coldkey); + assert!(OwnedHotkeys::::get(old_coldkey).is_empty()); + assert_eq!(OwnedHotkeys::::get(new_coldkey), vec![hotkey]); }); } // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_coldkey -- test_transfer_remaining_balance --exact --nocapture @@ -188,8 +188,8 @@ fn test_swap_with_no_stake() { &mut weight )); - assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); - assert_eq!(TotalColdkeyStake::::get(&new_coldkey), 0); + assert_eq!(TotalColdkeyStake::::get(old_coldkey), 0); + assert_eq!(TotalColdkeyStake::::get(new_coldkey), 0); }); } @@ -202,7 +202,7 @@ fn test_swap_with_multiple_hotkeys() { let hotkey1 = U256::from(3); let hotkey2 = U256::from(4); - OwnedHotkeys::::insert(&old_coldkey, vec![hotkey1, hotkey2]); + OwnedHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( @@ -211,9 +211,9 @@ fn test_swap_with_multiple_hotkeys() { &mut weight )); - assert!(OwnedHotkeys::::get(&old_coldkey).is_empty()); + assert!(OwnedHotkeys::::get(old_coldkey).is_empty()); assert_eq!( - OwnedHotkeys::::get(&new_coldkey), + OwnedHotkeys::::get(new_coldkey), vec![hotkey1, hotkey2] ); }); @@ -230,8 +230,8 @@ fn test_swap_with_multiple_subnets() { add_network(netuid1, 1, 0); add_network(netuid2, 1, 0); - SubnetOwner::::insert(netuid1, &old_coldkey); - SubnetOwner::::insert(netuid2, &old_coldkey); + SubnetOwner::::insert(netuid1, old_coldkey); + SubnetOwner::::insert(netuid2, old_coldkey); let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( @@ -259,8 +259,8 @@ fn test_swap_with_zero_balance() { &mut weight )); - assert_eq!(Balances::free_balance(&old_coldkey), 0); - assert_eq!(Balances::free_balance(&new_coldkey), 0); + assert_eq!(Balances::free_balance(old_coldkey), 0); + assert_eq!(Balances::free_balance(new_coldkey), 0); }); } @@ -272,7 +272,7 @@ fn test_swap_idempotency() { let new_coldkey = U256::from(2); let stake = 100; - TotalColdkeyStake::::insert(&old_coldkey, stake); + TotalColdkeyStake::::insert(old_coldkey, stake); let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( @@ -286,8 +286,8 @@ fn test_swap_idempotency() { &mut weight )); - assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); - assert_eq!(TotalColdkeyStake::::get(&new_coldkey), stake); + assert_eq!(TotalColdkeyStake::::get(old_coldkey), 0); + assert_eq!(TotalColdkeyStake::::get(new_coldkey), stake); }); } @@ -299,7 +299,7 @@ fn test_swap_with_max_values() { let new_coldkey = U256::from(2); let max_stake = u64::MAX; - TotalColdkeyStake::::insert(&old_coldkey, max_stake); + TotalColdkeyStake::::insert(old_coldkey, max_stake); let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( @@ -308,8 +308,8 @@ fn test_swap_with_max_values() { &mut weight )); - assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); - assert_eq!(TotalColdkeyStake::::get(&new_coldkey), max_stake); + assert_eq!(TotalColdkeyStake::::get(old_coldkey), 0); + assert_eq!(TotalColdkeyStake::::get(new_coldkey), max_stake); }); } @@ -321,7 +321,7 @@ fn test_swap_with_non_existent_new_coldkey() { let new_coldkey = U256::from(2); let stake = 100; - TotalColdkeyStake::::insert(&old_coldkey, stake); + TotalColdkeyStake::::insert(old_coldkey, stake); let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( @@ -330,8 +330,8 @@ fn test_swap_with_non_existent_new_coldkey() { &mut weight )); - assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); - assert_eq!(TotalColdkeyStake::::get(&new_coldkey), stake); + assert_eq!(TotalColdkeyStake::::get(old_coldkey), 0); + assert_eq!(TotalColdkeyStake::::get(new_coldkey), stake); }); } @@ -343,8 +343,8 @@ fn test_swap_with_overflow_in_stake_addition() { let new_coldkey = U256::from(2); let max_stake = u64::MAX; - TotalColdkeyStake::::insert(&old_coldkey, max_stake); - TotalColdkeyStake::::insert(&new_coldkey, 1); + TotalColdkeyStake::::insert(old_coldkey, max_stake); + TotalColdkeyStake::::insert(new_coldkey, 1); let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( @@ -353,8 +353,8 @@ fn test_swap_with_overflow_in_stake_addition() { &mut weight )); - assert_eq!(TotalColdkeyStake::::get(&old_coldkey), 0); - assert_eq!(TotalColdkeyStake::::get(&new_coldkey), max_stake); + assert_eq!(TotalColdkeyStake::::get(old_coldkey), 0); + assert_eq!(TotalColdkeyStake::::get(new_coldkey), max_stake); }); } @@ -367,7 +367,7 @@ fn test_swap_with_max_hotkeys() { let max_hotkeys = 1000; let hotkeys: Vec = (0..max_hotkeys).map(U256::from).collect(); - OwnedHotkeys::::insert(&old_coldkey, hotkeys.clone()); + OwnedHotkeys::::insert(old_coldkey, hotkeys.clone()); let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( @@ -376,8 +376,8 @@ fn test_swap_with_max_hotkeys() { &mut weight )); - assert!(OwnedHotkeys::::get(&old_coldkey).is_empty()); - assert_eq!(OwnedHotkeys::::get(&new_coldkey), hotkeys); + assert!(OwnedHotkeys::::get(old_coldkey).is_empty()); + assert_eq!(OwnedHotkeys::::get(new_coldkey), hotkeys); }); } @@ -391,10 +391,10 @@ fn test_swap_effect_on_delegated_stake() { let hotkey = U256::from(4); let stake = 100; - StakingHotkeys::::insert(&old_coldkey, vec![hotkey]); - StakingHotkeys::::insert(&delegator, vec![hotkey]); - Stake::::insert(&hotkey, &old_coldkey, stake); - Stake::::insert(&hotkey, &delegator, stake); + StakingHotkeys::::insert(old_coldkey, vec![hotkey]); + StakingHotkeys::::insert(delegator, vec![hotkey]); + Stake::::insert(hotkey, old_coldkey, stake); + Stake::::insert(hotkey, delegator, stake); let mut weight = Weight::zero(); assert_ok!(SubtensorModule::perform_swap_coldkey( @@ -403,9 +403,9 @@ fn test_swap_effect_on_delegated_stake() { &mut weight )); - assert_eq!(Stake::::get(&hotkey, &new_coldkey), stake); - assert_eq!(Stake::::get(&hotkey, &delegator), stake); - assert_eq!(Stake::::get(&hotkey, &old_coldkey), 0); + assert_eq!(Stake::::get(hotkey, new_coldkey), stake); + assert_eq!(Stake::::get(hotkey, delegator), stake); + assert_eq!(Stake::::get(hotkey, old_coldkey), 0); }); } @@ -420,8 +420,8 @@ fn test_swap_concurrent_modifications() { let initial_stake = 100; let additional_stake = 50; - StakingHotkeys::::insert(&old_coldkey, vec![hotkey]); - Stake::::insert(&hotkey, &old_coldkey, initial_stake); + StakingHotkeys::::insert(old_coldkey, vec![hotkey]); + Stake::::insert(hotkey, old_coldkey, initial_stake); // Simulate concurrent stake addition add_network(netuid, 1, 1); @@ -441,10 +441,10 @@ fn test_swap_concurrent_modifications() { )); assert_eq!( - Stake::::get(&hotkey, &new_coldkey), + Stake::::get(hotkey, new_coldkey), initial_stake + additional_stake - 1 ); - assert!(!Stake::::contains_key(&hotkey, &old_coldkey)); + assert!(!Stake::::contains_key(hotkey, old_coldkey)); }); }