From 76f20cedc4d9314f28e3437b0b5a6f38d9cd7505 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Fri, 12 Jul 2024 23:38:31 -0400 Subject: [PATCH 01/24] swap delegate stake also --- pallets/subtensor/src/swap.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index 307aeb6411..053112363b 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -862,6 +862,22 @@ impl Pallet { } } + for staking_hotkey in StakingHotkeys::::get(old_coldkey) { + let hotkey = &staking_hotkey; + // Retrieve and remove the stake associated with the hotkey and old coldkey + let stake: u64 = Stake::::take(hotkey, old_coldkey); + log::info!("Transferring delegated stake for hotkey {:?}: {}", hotkey, stake); + if stake > 0 { + // Insert the stake for the hotkey and new coldkey + Stake::::insert(hotkey, new_coldkey, stake); + total_transferred_stake = total_transferred_stake.saturating_add(stake); + + // Update the transaction weight + weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 1)); + } + } + + // Log the total transferred stake log::info!("Total transferred stake: {}", total_transferred_stake); From 4ccb8799eb0454e9581322a56c3e391cd67a9f12 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Fri, 12 Jul 2024 23:38:37 -0400 Subject: [PATCH 02/24] add test for this --- pallets/subtensor/tests/swap.rs | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/pallets/subtensor/tests/swap.rs b/pallets/subtensor/tests/swap.rs index 2355dc960d..ca45956d64 100644 --- a/pallets/subtensor/tests/swap.rs +++ b/pallets/subtensor/tests/swap.rs @@ -1271,6 +1271,67 @@ fn test_swap_stake_for_coldkey() { }); } +#[test] +fn test_swap_delegated_stake_for_coldkey() { + 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); + let stake_amount1 = 1000u64; + let stake_amount2 = 2000u64; + let total_stake = stake_amount1 + stake_amount2; + let mut weight = Weight::zero(); + + // Notice hotkey1 and hotkey2 are not in OwnedHotkeys + // coldkey therefore delegates stake to them + + // Setup initial state + Stake::::insert(hotkey1, old_coldkey, stake_amount1); + Stake::::insert(hotkey2, old_coldkey, stake_amount2); + TotalHotkeyStake::::insert(hotkey1, stake_amount1); + TotalHotkeyStake::::insert(hotkey2, stake_amount2); + TotalColdkeyStake::::insert(old_coldkey, total_stake); + + // Set up total issuance + TotalIssuance::::put(total_stake); + TotalStake::::put(total_stake); + + // Record initial values + let initial_total_issuance = SubtensorModule::get_total_issuance(); + let initial_total_stake = SubtensorModule::get_total_stake(); + + // Perform the swap + SubtensorModule::swap_stake_for_coldkey(&old_coldkey, &new_coldkey, &mut weight); + + // Verify stake transfer + assert_eq!(Stake::::get(hotkey1, new_coldkey), stake_amount1); + assert_eq!(Stake::::get(hotkey2, new_coldkey), stake_amount2); + assert_eq!(Stake::::get(hotkey1, old_coldkey), 0); + assert_eq!(Stake::::get(hotkey2, old_coldkey), 0); + + // Verify TotalColdkeyStake + assert_eq!(TotalColdkeyStake::::get(new_coldkey), total_stake); + assert_eq!(TotalColdkeyStake::::get(old_coldkey), 0); + + // Verify TotalHotkeyStake remains unchanged + assert_eq!(TotalHotkeyStake::::get(hotkey1), stake_amount1); + assert_eq!(TotalHotkeyStake::::get(hotkey2), stake_amount2); + + // Verify total stake and issuance remain unchanged + assert_eq!( + SubtensorModule::get_total_stake(), + initial_total_stake, + "Total stake changed unexpectedly" + ); + assert_eq!( + SubtensorModule::get_total_issuance(), + initial_total_issuance, + "Total issuance changed unexpectedly" + ); + }); +} + #[test] fn test_swap_total_hotkey_coldkey_stakes_this_interval_for_coldkey() { new_test_ext(1).execute_with(|| { From 73533b9bf89899b005a44e1f5932e37110efb229 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 13 Jul 2024 00:53:34 -0400 Subject: [PATCH 03/24] swap over stakinghotkeys map --- pallets/subtensor/src/swap.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index 053112363b..581d617992 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -909,7 +909,8 @@ impl Pallet { 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::::take(old_coldkey); + let staking_hotkeys: Vec = StakingHotkeys::::take(old_coldkey); + StakingHotkeys::::remove(old_coldkey); StakingHotkeys::::insert(new_coldkey, staking_hotkeys); weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); From c370aad5e3f0eda1250740b97fc498c216be049b Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 13 Jul 2024 00:55:11 -0400 Subject: [PATCH 04/24] check map first --- pallets/subtensor/src/swap.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index 581d617992..c6d3a5a8a7 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -863,18 +863,23 @@ impl Pallet { } for staking_hotkey in StakingHotkeys::::get(old_coldkey) { - let hotkey = &staking_hotkey; - // Retrieve and remove the stake associated with the hotkey and old coldkey - let stake: u64 = Stake::::take(hotkey, old_coldkey); - log::info!("Transferring delegated stake for hotkey {:?}: {}", hotkey, stake); - if stake > 0 { - // Insert the stake for the hotkey and new coldkey - Stake::::insert(hotkey, new_coldkey, stake); - total_transferred_stake = total_transferred_stake.saturating_add(stake); - - // Update the transaction weight - weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 1)); - } + 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::::take(hotkey, old_coldkey); + log::info!("Transferring delegated stake for hotkey {:?}: {}", hotkey, stake); + if stake > 0 { + // Insert the stake for the hotkey and new coldkey + Stake::::insert(hotkey, new_coldkey, stake); + total_transferred_stake = total_transferred_stake.saturating_add(stake); + + // Update the transaction weight + weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 1)); + } + } else { + weight.saturating_accrue(T::DbWeight::get().reads(1)); + } } From 9b8c0e4944b971d39519d59340ad132e597c2cf6 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 13 Jul 2024 00:57:37 -0400 Subject: [PATCH 05/24] add staking hotkeys test --- pallets/subtensor/tests/swap.rs | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/pallets/subtensor/tests/swap.rs b/pallets/subtensor/tests/swap.rs index ca45956d64..de5c4212ea 100644 --- a/pallets/subtensor/tests/swap.rs +++ b/pallets/subtensor/tests/swap.rs @@ -1271,6 +1271,40 @@ fn test_swap_stake_for_coldkey() { }); } +#[test] +fn test_swap_staking_hotkeys_for_coldkey() { + 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); + let stake_amount1 = 1000u64; + let stake_amount2 = 2000u64; + let total_stake = stake_amount1 + stake_amount2; + let mut weight = Weight::zero(); + + // Setup initial state + OwnedHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); + Stake::::insert(hotkey1, old_coldkey, stake_amount1); + Stake::::insert(hotkey2, old_coldkey, stake_amount2); + TotalHotkeyStake::::insert(hotkey1, stake_amount1); + TotalHotkeyStake::::insert(hotkey2, stake_amount2); + TotalColdkeyStake::::insert(old_coldkey, total_stake); + + // Set up total issuance + TotalIssuance::::put(total_stake); + TotalStake::::put(total_stake); + + // Perform the swap + SubtensorModule::swap_stake_for_coldkey(&old_coldkey, &new_coldkey, &mut weight); + + + // Verify StakingHotkeys transfer + assert_eq!(StakingHotkeys::::get(new_coldkey), vec![hotkey1, hotkey2]); + assert_eq!(StakingHotkeys::::get(old_coldkey), vec![]); + }); +} + #[test] fn test_swap_delegated_stake_for_coldkey() { new_test_ext(1).execute_with(|| { From baec581eceeb646bd6aefc8bacfc9e3408c31536 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 13 Jul 2024 00:59:42 -0400 Subject: [PATCH 06/24] no take --- pallets/subtensor/src/swap.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index c6d3a5a8a7..5fa92d5317 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -867,7 +867,8 @@ impl Pallet { let hotkey = &staking_hotkey; // Retrieve and remove the stake associated with the hotkey and old coldkey - let stake: u64 = Stake::::take(hotkey, 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 From a84ac6b478ff23148c6ea45170e54458a3596858 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 13 Jul 2024 01:01:38 -0400 Subject: [PATCH 07/24] keep old stake --- pallets/subtensor/src/swap.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index 5fa92d5317..3a557e1787 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -872,7 +872,8 @@ impl Pallet { log::info!("Transferring delegated stake for hotkey {:?}: {}", hotkey, stake); if stake > 0 { // Insert the stake for the hotkey and new coldkey - Stake::::insert(hotkey, new_coldkey, stake); + 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 transaction weight @@ -915,7 +916,7 @@ impl Pallet { 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::::take(old_coldkey); + let staking_hotkeys: Vec = StakingHotkeys::::get(old_coldkey); StakingHotkeys::::remove(old_coldkey); StakingHotkeys::::insert(new_coldkey, staking_hotkeys); weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); From 221d3204235665d0781e3aca52a168cd21814541 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 13 Jul 2024 01:03:12 -0400 Subject: [PATCH 08/24] add check to test --- pallets/subtensor/tests/swap.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pallets/subtensor/tests/swap.rs b/pallets/subtensor/tests/swap.rs index de5c4212ea..60b5b10c18 100644 --- a/pallets/subtensor/tests/swap.rs +++ b/pallets/subtensor/tests/swap.rs @@ -1214,6 +1214,7 @@ fn test_swap_stake_for_coldkey() { let hotkey2 = U256::from(4); let stake_amount1 = 1000u64; let stake_amount2 = 2000u64; + let stake_amount3 = 3000u64; let total_stake = stake_amount1 + stake_amount2; let mut weight = Weight::zero(); @@ -1221,6 +1222,10 @@ fn test_swap_stake_for_coldkey() { OwnedHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); Stake::::insert(hotkey1, old_coldkey, stake_amount1); Stake::::insert(hotkey2, old_coldkey, stake_amount2); + + // Insert existing for same hotkey1 + Stake::::insert(hotkey1, new_coldkey, stake_amount3); + TotalHotkeyStake::::insert(hotkey1, stake_amount1); TotalHotkeyStake::::insert(hotkey2, stake_amount2); TotalColdkeyStake::::insert(old_coldkey, total_stake); @@ -1249,6 +1254,9 @@ fn test_swap_stake_for_coldkey() { assert_eq!(Stake::::get(hotkey1, old_coldkey), 0); assert_eq!(Stake::::get(hotkey2, old_coldkey), 0); + // Verify stake is additive, not replaced + assert_eq!(Stake::::get(hotkey1, new_coldkey), stake_amount1 + stake_amount3); + // Verify TotalColdkeyStake assert_eq!(TotalColdkeyStake::::get(new_coldkey), total_stake); assert_eq!(TotalColdkeyStake::::get(old_coldkey), 0); From f9164fc1a023d497779163e58755edf39fed2ebb Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 13 Jul 2024 01:27:56 -0400 Subject: [PATCH 09/24] fix some tests --- pallets/subtensor/tests/swap.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pallets/subtensor/tests/swap.rs b/pallets/subtensor/tests/swap.rs index 60b5b10c18..637a0b4a94 100644 --- a/pallets/subtensor/tests/swap.rs +++ b/pallets/subtensor/tests/swap.rs @@ -1220,11 +1220,13 @@ fn test_swap_stake_for_coldkey() { // Setup initial state OwnedHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); + StakingHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); Stake::::insert(hotkey1, old_coldkey, stake_amount1); Stake::::insert(hotkey2, old_coldkey, stake_amount2); // Insert existing for same hotkey1 Stake::::insert(hotkey1, new_coldkey, stake_amount3); + StakingHotkeys::::insert(new_coldkey, vec![hotkey1]); TotalHotkeyStake::::insert(hotkey1, stake_amount1); TotalHotkeyStake::::insert(hotkey2, stake_amount2); @@ -1295,6 +1297,7 @@ fn test_swap_staking_hotkeys_for_coldkey() { OwnedHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); Stake::::insert(hotkey1, old_coldkey, stake_amount1); Stake::::insert(hotkey2, old_coldkey, stake_amount2); + StakingHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); TotalHotkeyStake::::insert(hotkey1, stake_amount1); TotalHotkeyStake::::insert(hotkey2, stake_amount2); TotalColdkeyStake::::insert(old_coldkey, total_stake); @@ -1329,6 +1332,7 @@ fn test_swap_delegated_stake_for_coldkey() { // coldkey therefore delegates stake to them // Setup initial state + StakingHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); Stake::::insert(hotkey1, old_coldkey, stake_amount1); Stake::::insert(hotkey2, old_coldkey, stake_amount2); TotalHotkeyStake::::insert(hotkey1, stake_amount1); From 2a14df766e19655915f4c79b8e6cccbd81a947b7 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 13 Jul 2024 01:28:07 -0400 Subject: [PATCH 10/24] fix delegate test --- pallets/subtensor/tests/swap.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/pallets/subtensor/tests/swap.rs b/pallets/subtensor/tests/swap.rs index 637a0b4a94..0aecdbbac4 100644 --- a/pallets/subtensor/tests/swap.rs +++ b/pallets/subtensor/tests/swap.rs @@ -1251,7 +1251,6 @@ fn test_swap_stake_for_coldkey() { assert_eq!(SubtensorModule::get_owned_hotkeys(&old_coldkey), vec![]); // Verify stake transfer - assert_eq!(Stake::::get(hotkey1, new_coldkey), stake_amount1); assert_eq!(Stake::::get(hotkey2, new_coldkey), stake_amount2); assert_eq!(Stake::::get(hotkey1, old_coldkey), 0); assert_eq!(Stake::::get(hotkey2, old_coldkey), 0); From b970f50be23a731542494f5212011bcee9dcbf77 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 13 Jul 2024 01:28:18 -0400 Subject: [PATCH 11/24] update staking hotekys maps --- pallets/subtensor/src/swap.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index 3a557e1787..3cddff97e0 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -917,8 +917,11 @@ impl Pallet { // Update the staking hotkeys for both old and new coldkeys let staking_hotkeys: Vec = StakingHotkeys::::get(old_coldkey); + let old_staking_hotkeys: Vec = StakingHotkeys::::get(new_coldkey); + + let combined_staking_hotkeys: Vec = staking_hotkeys.into_iter().chain(old_staking_hotkeys.into_iter()).collect(); StakingHotkeys::::remove(old_coldkey); - StakingHotkeys::::insert(new_coldkey, staking_hotkeys); + StakingHotkeys::::insert(new_coldkey, combined_staking_hotkeys); weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); // Log the total stake of old and new coldkeys after the swap From ad57e2bb50e7a55d5edbd9b84a7cba8fb4a98a98 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 13 Jul 2024 01:52:02 -0400 Subject: [PATCH 12/24] init --- pallets/subtensor/src/migration.rs | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/pallets/subtensor/src/migration.rs b/pallets/subtensor/src/migration.rs index 35b3c42603..72caa07437 100644 --- a/pallets/subtensor/src/migration.rs +++ b/pallets/subtensor/src/migration.rs @@ -603,3 +603,55 @@ pub fn migrate_populate_staking_hotkeys() -> Weight { Weight::zero() } } + +const LOG_TARGET_6: &str = "swapdelegate"; + +const ALREADY_SWAPPED_COLDKEYS = { + ("coldkey", "coldkey"), +}; + +pub fn migrate_to_v6_swap_delegate_stake_also() -> Weight { + let new_storage_version = 6; + + // Check storage version + let mut weight = T::DbWeight::get().reads(1); + + // Grab current version + let onchain_version = Pallet::::on_chain_storage_version(); + + // Only runs if we haven't already updated version past above new_storage_version. + if onchain_version < new_storage_version { + info!(target: LOG_TARGET_6, ">>> Swapping delegate stake for an already-swapped coldkey {:?}", onchain_version); + + for (old_coldkey, _) in ColdkeyArbitrationBlock::::iter() { + // Get new coldkey + let new_coldkey = ALREADY_SWAPPED_COLDKEYS.find(|x| x.0 == old_coldkey); + // Grab hotkeys + let staking_hotkeys = StakingHotkeys::::get(&old_coldkey); + // Iterate over hotkeys + for hotkey in staking_hotkeys { + // Get the stake + let stake = Stake::::get(&hotkey, &old_coldkey); + Stake::::remove(&hotkey, &old_coldkey); + + let old_stake = Stake::::get(&hotkey, &new_coldkey); + // Add the stake to the new coldkey + Stake::::insert(&hotkey, &new_coldkey, stake.saturating_add(old_stake)); + weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); + } + + StakingHotkeys::::remove(&old_coldkey); + weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); + } + + // Update storage version. + StorageVersion::new(new_storage_version).put::>(); // Update to version so we don't run this again. + // One write to storage version + weight.saturating_accrue(T::DbWeight::get().writes(1)); + + weight + } else { + info!(target: LOG_TARGET_6, "Migration to v6 already done!"); + Weight::zero() + } +} From 1cc7d3cb8ca23d40295d1da2ebc53f7bd9185e88 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 13 Jul 2024 02:05:39 -0400 Subject: [PATCH 13/24] comment out --- 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 aa7cafde0d..de551096b8 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -2061,17 +2061,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 6dcd0a13eecbfca7a45961966408632902099f16 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 13 Jul 2024 02:13:49 -0400 Subject: [PATCH 14/24] add admin swap --- pallets/admin-utils/src/lib.rs | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 9a8744dc68..48b003a4b7 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -17,6 +17,7 @@ pub mod pallet { use frame_support::pallet_prelude::*; use frame_support::traits::tokens::Balance; use frame_system::pallet_prelude::*; + use pallet_subtensor::StakingHotkeys; use sp_runtime::BoundedVec; /// The main data structure of the module. @@ -1034,6 +1035,43 @@ pub mod pallet { ) -> DispatchResult { T::Subtensor::ensure_subnet_owner_or_root(origin.clone(), netuid)?; T::Subtensor::do_set_alpha_values(origin, netuid, alpha_low, alpha_high) + } + + /// Sets values for liquid alpha + #[pallet::call_index(52)] + #[pallet::weight((0, DispatchClass::Operational, Pays::No))] + pub fn sudo_hotfix_swap_coldkey_delegates( + origin: OriginFor, + old_coldkey: T::AccountId, + new_coldkey: T::AccountId, + ) -> DispatchResult { + T::Subtensor::ensure_root(origin.clone())?; + + let weight = T::DbWeight::get().reads_writes(2, 1); + let staking_hotkeys = StakingHotkeys::::get(old_coldkey); + for staking_hotkey in staking_hotkeys { + 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); + 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)); + } + } + } + + 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::::insert(new_coldkey, existing_staking_hotkeys); + StakingHotkeys::::remove(old_coldkey); } } } From f474112da9ea61e5cb605ca4889f1d14db9cb4a7 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 13 Jul 2024 02:13:54 -0400 Subject: [PATCH 15/24] fix swap --- pallets/subtensor/src/swap.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index 3cddff97e0..1a636f6768 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -911,17 +911,30 @@ impl Pallet { } // Update the list of owned hotkeys for both old and new coldkeys - OwnedHotkeys::::remove(old_coldkey); - OwnedHotkeys::::insert(new_coldkey, old_owned_hotkeys); + + 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 old_staking_hotkeys: Vec = StakingHotkeys::::get(new_coldkey); - let combined_staking_hotkeys: Vec = staking_hotkeys.into_iter().chain(old_staking_hotkeys.into_iter()).collect(); + 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, combined_staking_hotkeys); + 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 From b708c1d38b070144bc351122642dcb74991a2deb Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Sat, 13 Jul 2024 02:23:31 -0400 Subject: [PATCH 16/24] .. --- pallets/admin-utils/src/lib.rs | 30 ++-------- pallets/admin-utils/tests/tests.rs | 18 ++++++ pallets/subtensor/src/migration.rs | 96 +++++++++++++++--------------- pallets/subtensor/src/swap.rs | 33 ++++++++++ pallets/subtensor/tests/swap.rs | 79 ++++++++++++++++++++++++ 5 files changed, 181 insertions(+), 75 deletions(-) diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 48b003a4b7..a471a83876 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -1047,32 +1047,10 @@ pub mod pallet { ) -> DispatchResult { T::Subtensor::ensure_root(origin.clone())?; - let weight = T::DbWeight::get().reads_writes(2, 1); - let staking_hotkeys = StakingHotkeys::::get(old_coldkey); - for staking_hotkey in staking_hotkeys { - 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); - 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)); - } - } - } - - 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::::insert(new_coldkey, existing_staking_hotkeys); - StakingHotkeys::::remove(old_coldkey); - } + T::Subtensor::swap_hotfix(old_coldkey, new_coldkey); + + Ok(()) + } } } diff --git a/pallets/admin-utils/tests/tests.rs b/pallets/admin-utils/tests/tests.rs index 9df59978f7..95ea846435 100644 --- a/pallets/admin-utils/tests/tests.rs +++ b/pallets/admin-utils/tests/tests.rs @@ -1361,3 +1361,21 @@ fn test_sudo_get_set_alpha() { )); }); } + + +#[test] +fn test_sudo_hotfix_swap_coldkey_delegates() { + new_test_ext().execute_with(|| { + let new_coldkey = U256::from(0); + let owner = U256::from(1); + let coldkey = U256::from(4); + + assert_ok!(AdminUtils::sudo_hotfix_swap_coldkey_delegates( + <::RuntimeOrigin>::root(), + coldkey, + new_coldkey + )); + + + }); +} diff --git a/pallets/subtensor/src/migration.rs b/pallets/subtensor/src/migration.rs index 72caa07437..277c8e5311 100644 --- a/pallets/subtensor/src/migration.rs +++ b/pallets/subtensor/src/migration.rs @@ -606,52 +606,50 @@ pub fn migrate_populate_staking_hotkeys() -> Weight { const LOG_TARGET_6: &str = "swapdelegate"; -const ALREADY_SWAPPED_COLDKEYS = { - ("coldkey", "coldkey"), -}; - -pub fn migrate_to_v6_swap_delegate_stake_also() -> Weight { - let new_storage_version = 6; - - // Check storage version - let mut weight = T::DbWeight::get().reads(1); - - // Grab current version - let onchain_version = Pallet::::on_chain_storage_version(); - - // Only runs if we haven't already updated version past above new_storage_version. - if onchain_version < new_storage_version { - info!(target: LOG_TARGET_6, ">>> Swapping delegate stake for an already-swapped coldkey {:?}", onchain_version); - - for (old_coldkey, _) in ColdkeyArbitrationBlock::::iter() { - // Get new coldkey - let new_coldkey = ALREADY_SWAPPED_COLDKEYS.find(|x| x.0 == old_coldkey); - // Grab hotkeys - let staking_hotkeys = StakingHotkeys::::get(&old_coldkey); - // Iterate over hotkeys - for hotkey in staking_hotkeys { - // Get the stake - let stake = Stake::::get(&hotkey, &old_coldkey); - Stake::::remove(&hotkey, &old_coldkey); - - let old_stake = Stake::::get(&hotkey, &new_coldkey); - // Add the stake to the new coldkey - Stake::::insert(&hotkey, &new_coldkey, stake.saturating_add(old_stake)); - weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); - } - - StakingHotkeys::::remove(&old_coldkey); - weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); - } - - // Update storage version. - StorageVersion::new(new_storage_version).put::>(); // Update to version so we don't run this again. - // One write to storage version - weight.saturating_accrue(T::DbWeight::get().writes(1)); - - weight - } else { - info!(target: LOG_TARGET_6, "Migration to v6 already done!"); - Weight::zero() - } -} +// const ALREADY_SWAPPED_COLDKEYS = []; + +// pub fn migrate_to_v6_swap_delegate_stake_also() -> Weight { +// let new_storage_version = 6; + +// // Check storage version +// let mut weight = T::DbWeight::get().reads(1); + +// // Grab current version +// let onchain_version = Pallet::::on_chain_storage_version(); + +// // Only runs if we haven't already updated version past above new_storage_version. +// if onchain_version < new_storage_version { +// info!(target: LOG_TARGET_6, ">>> Swapping delegate stake for an already-swapped coldkey {:?}", onchain_version); + +// for (old_coldkey, _) in ColdkeyArbitrationBlock::::iter() { +// // Get new coldkey +// let new_coldkey = ALREADY_SWAPPED_COLDKEYS.find(|x| x.0 == old_coldkey); +// // Grab hotkeys +// let staking_hotkeys = StakingHotkeys::::get(&old_coldkey); +// // Iterate over hotkeys +// for hotkey in staking_hotkeys { +// // Get the stake +// let stake = Stake::::get(&hotkey, &old_coldkey); +// Stake::::remove(&hotkey, &old_coldkey); + +// let old_stake = Stake::::get(&hotkey, &new_coldkey); +// // Add the stake to the new coldkey +// Stake::::insert(&hotkey, &new_coldkey, stake.saturating_add(old_stake)); +// weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); +// } + +// StakingHotkeys::::remove(&old_coldkey); +// weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); +// } + +// // Update storage version. +// StorageVersion::new(new_storage_version).put::>(); // Update to version so we don't run this again. +// // One write to storage version +// weight.saturating_accrue(T::DbWeight::get().writes(1)); + +// weight +// } else { +// info!(target: LOG_TARGET_6, "Migration to v6 already done!"); +// Weight::zero() +// } +// } diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index 1a636f6768..d53175f925 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -947,6 +947,39 @@ impl Pallet { TotalColdkeyStake::::get(new_coldkey) ); } + + + pub fn swap_hotfix(old_coldkey: &T::AccountId, new_coldkey: &T::AccountId) { + + let weight = T::DbWeight::get().reads_writes(2, 1); + let staking_hotkeys = StakingHotkeys::::get(old_coldkey); + for staking_hotkey in staking_hotkeys { + 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); + 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)); + } + } + } + + let mut existing_staking_hotkeys = StakingHotkeys::::get(new_coldkey); + + let staking_hotkeys = StakingHotkeys::::get(old_coldkey); + for hotkey in staking_hotkeys { + if !existing_staking_hotkeys.contains(&hotkey) { + existing_staking_hotkeys.push(hotkey); + } + } + StakingHotkeys::::insert(new_coldkey, existing_staking_hotkeys); + StakingHotkeys::::remove(old_coldkey); + } + /// Swaps the total hotkey-coldkey stakes for the current interval from the old coldkey to the new coldkey. /// /// # Arguments diff --git a/pallets/subtensor/tests/swap.rs b/pallets/subtensor/tests/swap.rs index 0aecdbbac4..97347fe4dd 100644 --- a/pallets/subtensor/tests/swap.rs +++ b/pallets/subtensor/tests/swap.rs @@ -1842,3 +1842,82 @@ fn test_swap_senate_member() { assert_eq!(weight, expected_weight); }); } + +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap -- test_coldkey_delegations --exact --nocapture +#[test] +fn test_coldkey_delegations() { + new_test_ext(1).execute_with(|| { + let new_coldkey = U256::from(0); + let owner = U256::from(1); + let coldkey = U256::from(4); + let delegate = U256::from(2); + let netuid = 1u16; + add_network(netuid, 13, 0); + register_ok_neuron(netuid, delegate, owner, 0); + SubtensorModule::add_balance_to_coldkey_account(&coldkey, 1000); + assert_ok!(SubtensorModule::do_become_delegate( + <::RuntimeOrigin>::signed(owner), + delegate, + u16::MAX / 10 + )); + assert_ok!(SubtensorModule::add_stake( + <::RuntimeOrigin>::signed(coldkey), + delegate, + 100 + )); + assert_ok!(SubtensorModule::perform_swap_coldkey( + &coldkey, + &new_coldkey + )); + assert_eq!( SubtensorModule::get_total_stake_for_hotkey( &delegate), 100 ); + assert_eq!( SubtensorModule::get_total_stake_for_coldkey( &coldkey), 0 ); + assert_eq!( SubtensorModule::get_total_stake_for_coldkey( &new_coldkey), 100 ); + assert_eq!( Stake::::get( delegate, new_coldkey ), 100 ); + assert_eq!( Stake::::get( delegate, coldkey ), 0 ); + }); +} + +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap -- test_sudo_hotfix_swap_coldkey_delegates --exact --nocapture +#[test] +fn test_sudo_hotfix_swap_coldkey_delegates() { + new_test_ext(1).execute_with(|| { + let new_coldkey = U256::from(0); + let owner = U256::from(1); + let coldkey = U256::from(4); + let delegate = U256::from(2); + let netuid = 1u16; + add_network(netuid, 13, 0); + register_ok_neuron(netuid, delegate, owner, 0); + SubtensorModule::add_balance_to_coldkey_account(&coldkey, 1000); + assert_ok!(SubtensorModule::do_become_delegate( + <::RuntimeOrigin>::signed(owner), + delegate, + u16::MAX / 10 + )); + assert_ok!(SubtensorModule::add_stake( + <::RuntimeOrigin>::signed(coldkey), + delegate, + 100 + )); + + assert_ok!(SubtensorModule::perform_swap_coldkey( + &coldkey, + &new_coldkey + )); + + + assert_ok!(AdminUtils::sudo_hotfix_swap_coldkey_delegates( + <::RuntimeOrigin>::root(), + to_be_set + )); + + assert_eq!( SubtensorModule::get_total_stake_for_hotkey( &delegate), 100 ); + assert_eq!( SubtensorModule::get_total_stake_for_coldkey( &coldkey), 0 ); + assert_eq!( SubtensorModule::get_total_stake_for_coldkey( &new_coldkey), 100 ); + assert_eq!( Stake::::get( delegate, new_coldkey ), 100 ); + assert_eq!( Stake::::get( delegate, coldkey ), 0 ); + + + }); +} + From 5b30256fae2fe3a80146a76c80af839d092fb554 Mon Sep 17 00:00:00 2001 From: const Date: Sat, 13 Jul 2024 02:04:04 -0500 Subject: [PATCH 17/24] hotkey staking maps fix --- pallets/admin-utils/src/lib.rs | 15 -- pallets/admin-utils/tests/tests.rs | 18 -- pallets/subtensor/src/lib.rs | 14 ++ pallets/subtensor/src/swap.rs | 9 +- pallets/subtensor/tests/registration.rs | 284 ++++++++++++------------ pallets/subtensor/tests/swap.rs | 119 +++++++--- runtime/src/lib.rs | 2 +- 7 files changed, 246 insertions(+), 215 deletions(-) diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index a471a83876..71b709a10e 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -1036,21 +1036,6 @@ pub mod pallet { T::Subtensor::ensure_subnet_owner_or_root(origin.clone(), netuid)?; T::Subtensor::do_set_alpha_values(origin, netuid, alpha_low, alpha_high) } - - /// Sets values for liquid alpha - #[pallet::call_index(52)] - #[pallet::weight((0, DispatchClass::Operational, Pays::No))] - pub fn sudo_hotfix_swap_coldkey_delegates( - origin: OriginFor, - old_coldkey: T::AccountId, - new_coldkey: T::AccountId, - ) -> DispatchResult { - T::Subtensor::ensure_root(origin.clone())?; - - T::Subtensor::swap_hotfix(old_coldkey, new_coldkey); - - Ok(()) - } } } diff --git a/pallets/admin-utils/tests/tests.rs b/pallets/admin-utils/tests/tests.rs index 95ea846435..9df59978f7 100644 --- a/pallets/admin-utils/tests/tests.rs +++ b/pallets/admin-utils/tests/tests.rs @@ -1361,21 +1361,3 @@ fn test_sudo_get_set_alpha() { )); }); } - - -#[test] -fn test_sudo_hotfix_swap_coldkey_delegates() { - new_test_ext().execute_with(|| { - let new_coldkey = U256::from(0); - let owner = U256::from(1); - let coldkey = U256::from(4); - - assert_ok!(AdminUtils::sudo_hotfix_swap_coldkey_delegates( - <::RuntimeOrigin>::root(), - coldkey, - new_coldkey - )); - - - }); -} diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index de551096b8..60d6c06d1b 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -6,6 +6,7 @@ // pub use pallet::*; +use crate::system::ensure_root; use frame_system::{self as system, ensure_signed}; use frame_support::{ @@ -2253,6 +2254,19 @@ pub mod pallet { pub fn dissolve_network(origin: OriginFor, netuid: u16) -> DispatchResult { Self::user_remove_network(origin, netuid) } + + /// Sets values for liquid alpha + #[pallet::call_index(64)] + #[pallet::weight((0, DispatchClass::Operational, Pays::No))] + pub fn sudo_hotfix_swap_coldkey_delegates( + origin: OriginFor, + old_coldkey: T::AccountId, + new_coldkey: T::AccountId, + ) -> DispatchResult { + ensure_root(origin)?; + Self::swap_hotfix(&old_coldkey, &new_coldkey); + Ok(()) + } } // ---- Subtensor helper functions. diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index d53175f925..b8b0cc459f 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -851,7 +851,8 @@ impl Pallet { log::info!("Transferring stake for hotkey {:?}: {}", hotkey, stake); if stake > 0 { // Insert the stake for the hotkey and new coldkey - Stake::::insert(hotkey, new_coldkey, stake); + 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 @@ -861,10 +862,11 @@ impl Pallet { 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); @@ -875,15 +877,18 @@ impl Pallet { 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); diff --git a/pallets/subtensor/tests/registration.rs b/pallets/subtensor/tests/registration.rs index 676d49a449..bd95ae3b14 100644 --- a/pallets/subtensor/tests/registration.rs +++ b/pallets/subtensor/tests/registration.rs @@ -1868,153 +1868,153 @@ fn test_registration_disabled() { }); } -#[ignore] -#[test] -fn test_hotkey_swap_ok() { - new_test_ext(1).execute_with(|| { - let netuid: u16 = 1; - let tempo: u16 = 13; - let hotkey_account_id = U256::from(1); - let burn_cost = 1000; - let coldkey_account_id = U256::from(667); - - SubtensorModule::set_burn(netuid, burn_cost); - add_network(netuid, tempo, 0); - - // Give it some $$$ in his coldkey balance - SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10_000_000_000); - - // Subscribe and check extrinsic output - assert_ok!(SubtensorModule::burned_register( - <::RuntimeOrigin>::signed(coldkey_account_id), - netuid, - hotkey_account_id - )); - - let new_hotkey = U256::from(1337); - assert_ok!(SubtensorModule::swap_hotkey( - <::RuntimeOrigin>::signed(coldkey_account_id), - hotkey_account_id, - new_hotkey - )); - assert_ne!( - SubtensorModule::get_owning_coldkey_for_hotkey(&hotkey_account_id), - coldkey_account_id - ); - assert_eq!( - SubtensorModule::get_owning_coldkey_for_hotkey(&new_hotkey), - coldkey_account_id - ); - }); -} - -#[ignore] -#[test] -fn test_hotkey_swap_not_owner() { - new_test_ext(1).execute_with(|| { - let netuid: u16 = 1; - let tempo: u16 = 13; - let hotkey_account_id = U256::from(1); - let burn_cost = 1000; - let coldkey_account_id = U256::from(2); - let not_owner_coldkey = U256::from(3); - - SubtensorModule::set_burn(netuid, burn_cost); - add_network(netuid, tempo, 0); - - // Give it some $$$ in his coldkey balance - SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); - - // Subscribe and check extrinsic output - assert_ok!(SubtensorModule::burned_register( - <::RuntimeOrigin>::signed(coldkey_account_id), - netuid, - hotkey_account_id - )); - - let new_hotkey = U256::from(4); - assert_err!( - SubtensorModule::swap_hotkey( - <::RuntimeOrigin>::signed(not_owner_coldkey), - hotkey_account_id, - new_hotkey - ), - Error::::NonAssociatedColdKey - ); - }); -} - -#[ignore] -#[test] -fn test_hotkey_swap_same_key() { - new_test_ext(1).execute_with(|| { - let netuid: u16 = 1; - let tempo: u16 = 13; - let hotkey_account_id = U256::from(1); - let burn_cost = 1000; - let coldkey_account_id = U256::from(2); - - SubtensorModule::set_burn(netuid, burn_cost); - add_network(netuid, tempo, 0); - - // Give it some $$$ in his coldkey balance - SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); +// #[ignore] +// #[test] +// fn test_hotkey_swap_ok() { +// new_test_ext(1).execute_with(|| { +// let netuid: u16 = 1; +// let tempo: u16 = 13; +// let hotkey_account_id = U256::from(1); +// let burn_cost = 1000; +// let coldkey_account_id = U256::from(667); + +// SubtensorModule::set_burn(netuid, burn_cost); +// add_network(netuid, tempo, 0); + +// // Give it some $$$ in his coldkey balance +// SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10_000_000_000); + +// // Subscribe and check extrinsic output +// assert_ok!(SubtensorModule::burned_register( +// <::RuntimeOrigin>::signed(coldkey_account_id), +// netuid, +// hotkey_account_id +// )); - // Subscribe and check extrinsic output - assert_ok!(SubtensorModule::burned_register( - <::RuntimeOrigin>::signed(coldkey_account_id), - netuid, - hotkey_account_id - )); +// let new_hotkey = U256::from(1337); +// assert_ok!(SubtensorModule::swap_hotkey( +// <::RuntimeOrigin>::signed(coldkey_account_id), +// hotkey_account_id, +// new_hotkey +// )); +// assert_ne!( +// SubtensorModule::get_owning_coldkey_for_hotkey(&hotkey_account_id), +// coldkey_account_id +// ); +// assert_eq!( +// SubtensorModule::get_owning_coldkey_for_hotkey(&new_hotkey), +// coldkey_account_id +// ); +// }); +// } - assert_err!( - SubtensorModule::swap_hotkey( - <::RuntimeOrigin>::signed(coldkey_account_id), - hotkey_account_id, - hotkey_account_id - ), - Error::::HotKeyAlreadyRegisteredInSubNet - ); - }); -} +// #[ignore] +// #[test] +// fn test_hotkey_swap_not_owner() { +// new_test_ext(1).execute_with(|| { +// let netuid: u16 = 1; +// let tempo: u16 = 13; +// let hotkey_account_id = U256::from(1); +// let burn_cost = 1000; +// let coldkey_account_id = U256::from(2); +// let not_owner_coldkey = U256::from(3); + +// SubtensorModule::set_burn(netuid, burn_cost); +// add_network(netuid, tempo, 0); + +// // Give it some $$$ in his coldkey balance +// SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); + +// // Subscribe and check extrinsic output +// assert_ok!(SubtensorModule::burned_register( +// <::RuntimeOrigin>::signed(coldkey_account_id), +// netuid, +// hotkey_account_id +// )); -#[ignore] -#[test] -fn test_hotkey_swap_registered_key() { - new_test_ext(1).execute_with(|| { - let netuid: u16 = 1; - let tempo: u16 = 13; - let hotkey_account_id = U256::from(1); - let burn_cost = 1000; - let coldkey_account_id = U256::from(2); +// let new_hotkey = U256::from(4); +// assert_err!( +// SubtensorModule::swap_hotkey( +// <::RuntimeOrigin>::signed(not_owner_coldkey), +// hotkey_account_id, +// new_hotkey +// ), +// Error::::NonAssociatedColdKey +// ); +// }); +// } - SubtensorModule::set_burn(netuid, burn_cost); - add_network(netuid, tempo, 0); +// #[ignore] +// #[test] +// fn test_hotkey_swap_same_key() { +// new_test_ext(1).execute_with(|| { +// let netuid: u16 = 1; +// let tempo: u16 = 13; +// let hotkey_account_id = U256::from(1); +// let burn_cost = 1000; +// let coldkey_account_id = U256::from(2); + +// SubtensorModule::set_burn(netuid, burn_cost); +// add_network(netuid, tempo, 0); + +// // Give it some $$$ in his coldkey balance +// SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); + +// // Subscribe and check extrinsic output +// assert_ok!(SubtensorModule::burned_register( +// <::RuntimeOrigin>::signed(coldkey_account_id), +// netuid, +// hotkey_account_id +// )); - // Give it some $$$ in his coldkey balance - SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 100_000_000_000); +// assert_err!( +// SubtensorModule::swap_hotkey( +// <::RuntimeOrigin>::signed(coldkey_account_id), +// hotkey_account_id, +// hotkey_account_id +// ), +// Error::::HotKeyAlreadyRegisteredInSubNet +// ); +// }); +// } - // Subscribe and check extrinsic output - assert_ok!(SubtensorModule::burned_register( - <::RuntimeOrigin>::signed(coldkey_account_id), - netuid, - hotkey_account_id - )); +// #[ignore] +// #[test] +// fn test_hotkey_swap_registered_key() { +// new_test_ext(1).execute_with(|| { +// let netuid: u16 = 1; +// let tempo: u16 = 13; +// let hotkey_account_id = U256::from(1); +// let burn_cost = 1000; +// let coldkey_account_id = U256::from(2); + +// SubtensorModule::set_burn(netuid, burn_cost); +// add_network(netuid, tempo, 0); + +// // Give it some $$$ in his coldkey balance +// SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 100_000_000_000); + +// // Subscribe and check extrinsic output +// assert_ok!(SubtensorModule::burned_register( +// <::RuntimeOrigin>::signed(coldkey_account_id), +// netuid, +// hotkey_account_id +// )); - let new_hotkey = U256::from(3); - assert_ok!(SubtensorModule::burned_register( - <::RuntimeOrigin>::signed(coldkey_account_id), - netuid, - new_hotkey - )); +// let new_hotkey = U256::from(3); +// assert_ok!(SubtensorModule::burned_register( +// <::RuntimeOrigin>::signed(coldkey_account_id), +// netuid, +// new_hotkey +// )); - assert_err!( - SubtensorModule::swap_hotkey( - <::RuntimeOrigin>::signed(coldkey_account_id), - hotkey_account_id, - new_hotkey - ), - Error::::HotKeyAlreadyRegisteredInSubNet - ); - }); -} +// assert_err!( +// SubtensorModule::swap_hotkey( +// <::RuntimeOrigin>::signed(coldkey_account_id), +// hotkey_account_id, +// new_hotkey +// ), +// Error::::HotKeyAlreadyRegisteredInSubNet +// ); +// }); +// } diff --git a/pallets/subtensor/tests/swap.rs b/pallets/subtensor/tests/swap.rs index 97347fe4dd..2f075f729b 100644 --- a/pallets/subtensor/tests/swap.rs +++ b/pallets/subtensor/tests/swap.rs @@ -1205,6 +1205,7 @@ fn test_do_swap_coldkey_success() { }); } +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap -- test_swap_stake_for_coldkey --exact --nocaptur #[test] fn test_swap_stake_for_coldkey() { new_test_ext(1).execute_with(|| { @@ -1223,6 +1224,8 @@ fn test_swap_stake_for_coldkey() { StakingHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); Stake::::insert(hotkey1, old_coldkey, stake_amount1); Stake::::insert(hotkey2, old_coldkey, stake_amount2); + assert_eq!(Stake::::get(hotkey1, old_coldkey), stake_amount1 ); + assert_eq!(Stake::::get(hotkey1, old_coldkey), stake_amount1 ); // Insert existing for same hotkey1 Stake::::insert(hotkey1, new_coldkey, stake_amount3); @@ -1243,6 +1246,9 @@ fn test_swap_stake_for_coldkey() { // Perform the swap SubtensorModule::swap_stake_for_coldkey(&old_coldkey, &new_coldkey, &mut weight); + // Verify stake is additive, not replaced + assert_eq!(Stake::::get(hotkey1, new_coldkey), stake_amount1 + stake_amount3); + // Verify ownership transfer assert_eq!( SubtensorModule::get_owned_hotkeys(&new_coldkey), @@ -1255,9 +1261,6 @@ fn test_swap_stake_for_coldkey() { assert_eq!(Stake::::get(hotkey1, old_coldkey), 0); assert_eq!(Stake::::get(hotkey2, old_coldkey), 0); - // Verify stake is additive, not replaced - assert_eq!(Stake::::get(hotkey1, new_coldkey), stake_amount1 + stake_amount3); - // Verify TotalColdkeyStake assert_eq!(TotalColdkeyStake::::get(new_coldkey), total_stake); assert_eq!(TotalColdkeyStake::::get(old_coldkey), 0); @@ -1877,47 +1880,89 @@ fn test_coldkey_delegations() { }); } +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap -- test_sudo_hotfix_swap_coldkey_delegates --exact --nocapture +// #[test] +// fn test_sudo_hotfix_swap_coldkey_delegates() { +// new_test_ext(1).execute_with(|| { +// let new_coldkey = U256::from(0); +// let owner = U256::from(1); +// let coldkey = U256::from(4); +// let delegate = U256::from(2); +// let netuid = 1u16; +// add_network(netuid, 13, 0); +// register_ok_neuron(netuid, delegate, owner, 0); +// SubtensorModule::add_balance_to_coldkey_account(&coldkey, 1000); +// assert_ok!(SubtensorModule::do_become_delegate( +// <::RuntimeOrigin>::signed(owner), +// delegate, +// u16::MAX / 10 +// )); +// assert_ok!(SubtensorModule::add_stake( +// <::RuntimeOrigin>::signed(coldkey), +// delegate, +// 100 +// )); + +// assert_ok!(SubtensorModule::perform_swap_coldkey( +// &coldkey, +// &new_coldkey +// )); + + +// assert_ok!(AdminUtils::sudo_hotfix_swap_coldkey_delegates( +// <::RuntimeOrigin>::root(), +// to_be_set +// )); + +// assert_eq!( SubtensorModule::get_total_stake_for_hotkey( &delegate), 100 ); +// assert_eq!( SubtensorModule::get_total_stake_for_coldkey( &coldkey), 0 ); +// assert_eq!( SubtensorModule::get_total_stake_for_coldkey( &new_coldkey), 100 ); +// assert_eq!( Stake::::get( delegate, new_coldkey ), 100 ); +// assert_eq!( Stake::::get( delegate, coldkey ), 0 ); + + +// }); +// } + + + // SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap -- test_sudo_hotfix_swap_coldkey_delegates --exact --nocapture #[test] fn test_sudo_hotfix_swap_coldkey_delegates() { new_test_ext(1).execute_with(|| { let new_coldkey = U256::from(0); - let owner = U256::from(1); let coldkey = U256::from(4); - let delegate = U256::from(2); - let netuid = 1u16; - add_network(netuid, 13, 0); - register_ok_neuron(netuid, delegate, owner, 0); - SubtensorModule::add_balance_to_coldkey_account(&coldkey, 1000); - assert_ok!(SubtensorModule::do_become_delegate( - <::RuntimeOrigin>::signed(owner), - delegate, - u16::MAX / 10 - )); - assert_ok!(SubtensorModule::add_stake( - <::RuntimeOrigin>::signed(coldkey), - delegate, - 100 - )); - - assert_ok!(SubtensorModule::perform_swap_coldkey( - &coldkey, - &new_coldkey - )); - - - assert_ok!(AdminUtils::sudo_hotfix_swap_coldkey_delegates( - <::RuntimeOrigin>::root(), - to_be_set - )); - - assert_eq!( SubtensorModule::get_total_stake_for_hotkey( &delegate), 100 ); - assert_eq!( SubtensorModule::get_total_stake_for_coldkey( &coldkey), 0 ); - assert_eq!( SubtensorModule::get_total_stake_for_coldkey( &new_coldkey), 100 ); - assert_eq!( Stake::::get( delegate, new_coldkey ), 100 ); - assert_eq!( Stake::::get( delegate, coldkey ), 0 ); + assert_ok!(SubtensorModule::sudo_hotfix_swap_coldkey_delegates(<::RuntimeOrigin>::root(),coldkey,new_coldkey)); + }); +} +// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap -- test_sudo_hotfix_swap_coldkey_delegates_with_broken_stake --exact --nocapture +#[test] +fn test_sudo_hotfix_swap_coldkey_delegates_with_broken_stake() { + new_test_ext(1).execute_with(|| { + let new_coldkey = U256::from(0); + let old_coldkey = U256::from(4); + let h1 = U256::from(5); + let h2 = U256::from(6); + let h3 = U256::from(7); + Stake::::insert( h3, old_coldkey, 100 ); + Stake::::insert( h2, old_coldkey, 100 ); + assert_eq!(Stake::::get( h3, old_coldkey ), 100 ); + assert_eq!(Stake::::get( h2, old_coldkey ), 100 ); + StakingHotkeys::::insert( new_coldkey, vec![h1, h2 ] ); + StakingHotkeys::::insert( old_coldkey, vec![ h3, h2] ); + assert_ok!(SubtensorModule::sudo_hotfix_swap_coldkey_delegates(<::RuntimeOrigin>::root(), old_coldkey, new_coldkey)); + let hotkeys = StakingHotkeys::::get( new_coldkey); + assert_eq!(hotkeys.len(), 3); + assert_eq!(hotkeys[0], h1); + assert_eq!(hotkeys[1], h2); + assert_eq!(hotkeys[2], h3); + let hotkeys_old = StakingHotkeys::::get( old_coldkey); + assert_eq!(hotkeys_old.len(), 0); + assert_eq!(Stake::::get( h3, old_coldkey ), 0 ); + assert_eq!(Stake::::get( h2, old_coldkey ), 0 ); + assert_eq!(Stake::::get( h3, new_coldkey ), 100 ); + assert_eq!(Stake::::get( h2, new_coldkey ), 100 ); }); } - diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index b343ad0f06..d1fe2e0c9d 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: 162, + spec_version: 164, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From fc245f813114b13485f9d2dab9d7f981641ca487 Mon Sep 17 00:00:00 2001 From: const Date: Sat, 13 Jul 2024 02:05:15 -0500 Subject: [PATCH 18/24] remove staking hotkeys --- pallets/admin-utils/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 71b709a10e..9a8744dc68 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -17,7 +17,6 @@ pub mod pallet { use frame_support::pallet_prelude::*; use frame_support::traits::tokens::Balance; use frame_system::pallet_prelude::*; - use pallet_subtensor::StakingHotkeys; use sp_runtime::BoundedVec; /// The main data structure of the module. From 494796e59a305a00683ba55dab07bbc6526420b8 Mon Sep 17 00:00:00 2001 From: const Date: Sat, 13 Jul 2024 02:07:07 -0500 Subject: [PATCH 19/24] remove commented code --- pallets/subtensor/src/migration.rs | 50 ------------------------------ 1 file changed, 50 deletions(-) diff --git a/pallets/subtensor/src/migration.rs b/pallets/subtensor/src/migration.rs index 277c8e5311..35b3c42603 100644 --- a/pallets/subtensor/src/migration.rs +++ b/pallets/subtensor/src/migration.rs @@ -603,53 +603,3 @@ pub fn migrate_populate_staking_hotkeys() -> Weight { Weight::zero() } } - -const LOG_TARGET_6: &str = "swapdelegate"; - -// const ALREADY_SWAPPED_COLDKEYS = []; - -// pub fn migrate_to_v6_swap_delegate_stake_also() -> Weight { -// let new_storage_version = 6; - -// // Check storage version -// let mut weight = T::DbWeight::get().reads(1); - -// // Grab current version -// let onchain_version = Pallet::::on_chain_storage_version(); - -// // Only runs if we haven't already updated version past above new_storage_version. -// if onchain_version < new_storage_version { -// info!(target: LOG_TARGET_6, ">>> Swapping delegate stake for an already-swapped coldkey {:?}", onchain_version); - -// for (old_coldkey, _) in ColdkeyArbitrationBlock::::iter() { -// // Get new coldkey -// let new_coldkey = ALREADY_SWAPPED_COLDKEYS.find(|x| x.0 == old_coldkey); -// // Grab hotkeys -// let staking_hotkeys = StakingHotkeys::::get(&old_coldkey); -// // Iterate over hotkeys -// for hotkey in staking_hotkeys { -// // Get the stake -// let stake = Stake::::get(&hotkey, &old_coldkey); -// Stake::::remove(&hotkey, &old_coldkey); - -// let old_stake = Stake::::get(&hotkey, &new_coldkey); -// // Add the stake to the new coldkey -// Stake::::insert(&hotkey, &new_coldkey, stake.saturating_add(old_stake)); -// weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); -// } - -// StakingHotkeys::::remove(&old_coldkey); -// weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); -// } - -// // Update storage version. -// StorageVersion::new(new_storage_version).put::>(); // Update to version so we don't run this again. -// // One write to storage version -// weight.saturating_accrue(T::DbWeight::get().writes(1)); - -// weight -// } else { -// info!(target: LOG_TARGET_6, "Migration to v6 already done!"); -// Weight::zero() -// } -// } From f041ed902c37f6040c0cee41e7643c844c8190bf Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Sat, 13 Jul 2024 10:40:19 -0400 Subject: [PATCH 20/24] cargo fmt --- pallets/subtensor/src/lib.rs | 16 ++-- pallets/subtensor/src/swap.rs | 138 ++++++++++++++++++-------------- pallets/subtensor/tests/swap.rs | 89 +++++++++++--------- 3 files changed, 135 insertions(+), 108 deletions(-) diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index 60d6c06d1b..bded1705f2 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -2064,8 +2064,8 @@ 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))] + ///.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, @@ -2255,18 +2255,18 @@ pub mod pallet { Self::user_remove_network(origin, netuid) } - /// Sets values for liquid alpha + /// Sets values for liquid alpha #[pallet::call_index(64)] #[pallet::weight((0, DispatchClass::Operational, Pays::No))] pub fn sudo_hotfix_swap_coldkey_delegates( origin: OriginFor, - old_coldkey: T::AccountId, - new_coldkey: T::AccountId, + old_coldkey: T::AccountId, + new_coldkey: T::AccountId, ) -> DispatchResult { ensure_root(origin)?; - Self::swap_hotfix(&old_coldkey, &new_coldkey); - Ok(()) - } + Self::swap_hotfix(&old_coldkey, &new_coldkey); + Ok(()) + } } // ---- Subtensor helper functions. diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index b8b0cc459f..50e3ed944d 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -862,33 +862,52 @@ impl Pallet { 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::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); @@ -917,7 +936,7 @@ impl Pallet { // Update the list of owned hotkeys for both old and new coldkeys - let mut new_owned_hotkeys = OwnedHotkeys::::get(new_coldkey); + 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); @@ -925,13 +944,13 @@ impl Pallet { } OwnedHotkeys::::insert(new_coldkey, new_owned_hotkeys); - OwnedHotkeys::::remove(old_coldkey); + 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 staking_hotkeys: Vec = StakingHotkeys::::get(old_coldkey); - let mut existing_staking_hotkeys = StakingHotkeys::::get(new_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); @@ -953,37 +972,34 @@ impl Pallet { ); } + pub fn swap_hotfix(old_coldkey: &T::AccountId, new_coldkey: &T::AccountId) { + let weight = T::DbWeight::get().reads_writes(2, 1); + let staking_hotkeys = StakingHotkeys::::get(old_coldkey); + for staking_hotkey in staking_hotkeys { + 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); + 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)); + } + } + } + + let mut existing_staking_hotkeys = StakingHotkeys::::get(new_coldkey); - pub fn swap_hotfix(old_coldkey: &T::AccountId, new_coldkey: &T::AccountId) { - - let weight = T::DbWeight::get().reads_writes(2, 1); - let staking_hotkeys = StakingHotkeys::::get(old_coldkey); - for staking_hotkey in staking_hotkeys { - 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); - 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)); - } - } - } - - let mut existing_staking_hotkeys = StakingHotkeys::::get(new_coldkey); - - let staking_hotkeys = StakingHotkeys::::get(old_coldkey); - for hotkey in staking_hotkeys { - if !existing_staking_hotkeys.contains(&hotkey) { - existing_staking_hotkeys.push(hotkey); - } - } - StakingHotkeys::::insert(new_coldkey, existing_staking_hotkeys); - StakingHotkeys::::remove(old_coldkey); - } + let staking_hotkeys = StakingHotkeys::::get(old_coldkey); + for hotkey in staking_hotkeys { + if !existing_staking_hotkeys.contains(&hotkey) { + existing_staking_hotkeys.push(hotkey); + } + } + StakingHotkeys::::insert(new_coldkey, existing_staking_hotkeys); + StakingHotkeys::::remove(old_coldkey); + } /// Swaps the total hotkey-coldkey stakes for the current interval from the old coldkey to the new coldkey. /// diff --git a/pallets/subtensor/tests/swap.rs b/pallets/subtensor/tests/swap.rs index 2f075f729b..6acf0b8200 100644 --- a/pallets/subtensor/tests/swap.rs +++ b/pallets/subtensor/tests/swap.rs @@ -1215,21 +1215,21 @@ fn test_swap_stake_for_coldkey() { let hotkey2 = U256::from(4); let stake_amount1 = 1000u64; let stake_amount2 = 2000u64; - let stake_amount3 = 3000u64; + let stake_amount3 = 3000u64; let total_stake = stake_amount1 + stake_amount2; let mut weight = Weight::zero(); // Setup initial state OwnedHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); - StakingHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); + StakingHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); Stake::::insert(hotkey1, old_coldkey, stake_amount1); Stake::::insert(hotkey2, old_coldkey, stake_amount2); - assert_eq!(Stake::::get(hotkey1, old_coldkey), stake_amount1 ); - assert_eq!(Stake::::get(hotkey1, old_coldkey), stake_amount1 ); + assert_eq!(Stake::::get(hotkey1, old_coldkey), stake_amount1); + assert_eq!(Stake::::get(hotkey1, old_coldkey), stake_amount1); - // Insert existing for same hotkey1 - Stake::::insert(hotkey1, new_coldkey, stake_amount3); - StakingHotkeys::::insert(new_coldkey, vec![hotkey1]); + // Insert existing for same hotkey1 + Stake::::insert(hotkey1, new_coldkey, stake_amount3); + StakingHotkeys::::insert(new_coldkey, vec![hotkey1]); TotalHotkeyStake::::insert(hotkey1, stake_amount1); TotalHotkeyStake::::insert(hotkey2, stake_amount2); @@ -1247,7 +1247,10 @@ fn test_swap_stake_for_coldkey() { SubtensorModule::swap_stake_for_coldkey(&old_coldkey, &new_coldkey, &mut weight); // Verify stake is additive, not replaced - assert_eq!(Stake::::get(hotkey1, new_coldkey), stake_amount1 + stake_amount3); + assert_eq!( + Stake::::get(hotkey1, new_coldkey), + stake_amount1 + stake_amount3 + ); // Verify ownership transfer assert_eq!( @@ -1299,7 +1302,7 @@ fn test_swap_staking_hotkeys_for_coldkey() { OwnedHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); Stake::::insert(hotkey1, old_coldkey, stake_amount1); Stake::::insert(hotkey2, old_coldkey, stake_amount2); - StakingHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); + StakingHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); TotalHotkeyStake::::insert(hotkey1, stake_amount1); TotalHotkeyStake::::insert(hotkey2, stake_amount2); TotalColdkeyStake::::insert(old_coldkey, total_stake); @@ -1311,10 +1314,12 @@ fn test_swap_staking_hotkeys_for_coldkey() { // Perform the swap SubtensorModule::swap_stake_for_coldkey(&old_coldkey, &new_coldkey, &mut weight); - // Verify StakingHotkeys transfer - assert_eq!(StakingHotkeys::::get(new_coldkey), vec![hotkey1, hotkey2]); - assert_eq!(StakingHotkeys::::get(old_coldkey), vec![]); + assert_eq!( + StakingHotkeys::::get(new_coldkey), + vec![hotkey1, hotkey2] + ); + assert_eq!(StakingHotkeys::::get(old_coldkey), vec![]); }); } @@ -1330,11 +1335,11 @@ fn test_swap_delegated_stake_for_coldkey() { let total_stake = stake_amount1 + stake_amount2; let mut weight = Weight::zero(); - // Notice hotkey1 and hotkey2 are not in OwnedHotkeys - // coldkey therefore delegates stake to them + // Notice hotkey1 and hotkey2 are not in OwnedHotkeys + // coldkey therefore delegates stake to them // Setup initial state - StakingHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); + StakingHotkeys::::insert(old_coldkey, vec![hotkey1, hotkey2]); Stake::::insert(hotkey1, old_coldkey, stake_amount1); Stake::::insert(hotkey2, old_coldkey, stake_amount2); TotalHotkeyStake::::insert(hotkey1, stake_amount1); @@ -1872,11 +1877,14 @@ fn test_coldkey_delegations() { &coldkey, &new_coldkey )); - assert_eq!( SubtensorModule::get_total_stake_for_hotkey( &delegate), 100 ); - assert_eq!( SubtensorModule::get_total_stake_for_coldkey( &coldkey), 0 ); - assert_eq!( SubtensorModule::get_total_stake_for_coldkey( &new_coldkey), 100 ); - assert_eq!( Stake::::get( delegate, new_coldkey ), 100 ); - assert_eq!( Stake::::get( delegate, coldkey ), 0 ); + assert_eq!(SubtensorModule::get_total_stake_for_hotkey(&delegate), 100); + assert_eq!(SubtensorModule::get_total_stake_for_coldkey(&coldkey), 0); + assert_eq!( + SubtensorModule::get_total_stake_for_coldkey(&new_coldkey), + 100 + ); + assert_eq!(Stake::::get(delegate, new_coldkey), 100); + assert_eq!(Stake::::get(delegate, coldkey), 0); }); } @@ -1908,7 +1916,6 @@ fn test_coldkey_delegations() { // &new_coldkey // )); - // assert_ok!(AdminUtils::sudo_hotfix_swap_coldkey_delegates( // <::RuntimeOrigin>::root(), // to_be_set @@ -1920,19 +1927,20 @@ fn test_coldkey_delegations() { // assert_eq!( Stake::::get( delegate, new_coldkey ), 100 ); // assert_eq!( Stake::::get( delegate, coldkey ), 0 ); - // }); // } - - // SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap -- test_sudo_hotfix_swap_coldkey_delegates --exact --nocapture #[test] fn test_sudo_hotfix_swap_coldkey_delegates() { new_test_ext(1).execute_with(|| { let new_coldkey = U256::from(0); let coldkey = U256::from(4); - assert_ok!(SubtensorModule::sudo_hotfix_swap_coldkey_delegates(<::RuntimeOrigin>::root(),coldkey,new_coldkey)); + assert_ok!(SubtensorModule::sudo_hotfix_swap_coldkey_delegates( + <::RuntimeOrigin>::root(), + coldkey, + new_coldkey + )); }); } @@ -1945,24 +1953,27 @@ fn test_sudo_hotfix_swap_coldkey_delegates_with_broken_stake() { let h1 = U256::from(5); let h2 = U256::from(6); let h3 = U256::from(7); - Stake::::insert( h3, old_coldkey, 100 ); - Stake::::insert( h2, old_coldkey, 100 ); - assert_eq!(Stake::::get( h3, old_coldkey ), 100 ); - assert_eq!(Stake::::get( h2, old_coldkey ), 100 ); - StakingHotkeys::::insert( new_coldkey, vec![h1, h2 ] ); - StakingHotkeys::::insert( old_coldkey, vec![ h3, h2] ); - assert_ok!(SubtensorModule::sudo_hotfix_swap_coldkey_delegates(<::RuntimeOrigin>::root(), old_coldkey, new_coldkey)); - let hotkeys = StakingHotkeys::::get( new_coldkey); + Stake::::insert(h3, old_coldkey, 100); + Stake::::insert(h2, old_coldkey, 100); + assert_eq!(Stake::::get(h3, old_coldkey), 100); + assert_eq!(Stake::::get(h2, old_coldkey), 100); + StakingHotkeys::::insert(new_coldkey, vec![h1, h2]); + StakingHotkeys::::insert(old_coldkey, vec![h3, h2]); + assert_ok!(SubtensorModule::sudo_hotfix_swap_coldkey_delegates( + <::RuntimeOrigin>::root(), + old_coldkey, + new_coldkey + )); + let hotkeys = StakingHotkeys::::get(new_coldkey); assert_eq!(hotkeys.len(), 3); assert_eq!(hotkeys[0], h1); assert_eq!(hotkeys[1], h2); assert_eq!(hotkeys[2], h3); - let hotkeys_old = StakingHotkeys::::get( old_coldkey); + let hotkeys_old = StakingHotkeys::::get(old_coldkey); assert_eq!(hotkeys_old.len(), 0); - assert_eq!(Stake::::get( h3, old_coldkey ), 0 ); - assert_eq!(Stake::::get( h2, old_coldkey ), 0 ); - assert_eq!(Stake::::get( h3, new_coldkey ), 100 ); - assert_eq!(Stake::::get( h2, new_coldkey ), 100 ); - + assert_eq!(Stake::::get(h3, old_coldkey), 0); + assert_eq!(Stake::::get(h2, old_coldkey), 0); + assert_eq!(Stake::::get(h3, new_coldkey), 100); + assert_eq!(Stake::::get(h2, new_coldkey), 100); }); } From 54ec7502e32caf259a2470e5c8f20c5e9a0fe25a Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Sat, 13 Jul 2024 10:41:18 -0400 Subject: [PATCH 21/24] cargo fix --workspace --- pallets/subtensor/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index bded1705f2..83b6a4005f 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -6,7 +6,6 @@ // pub use pallet::*; -use crate::system::ensure_root; use frame_system::{self as system, ensure_signed}; use frame_support::{ From ca51109e8136c562c3e9f9a0142df9b3ecfb7c23 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Sat, 13 Jul 2024 10:42:39 -0400 Subject: [PATCH 22/24] bump spec_version to 165 --- 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 d1fe2e0c9d..a4abd124ff 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: 164, + spec_version: 165, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From 4dafd88d31cd84b97078820a0d83bf473e4e7b6c Mon Sep 17 00:00:00 2001 From: const Date: Mon, 15 Jul 2024 10:07:24 -0500 Subject: [PATCH 23/24] swap hotkey benchmark removed --- pallets/subtensor/src/benchmarks.rs | 54 ++++++++++++++--------------- runtime/src/lib.rs | 2 +- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/pallets/subtensor/src/benchmarks.rs b/pallets/subtensor/src/benchmarks.rs index 80a375d103..03e087a929 100644 --- a/pallets/subtensor/src/benchmarks.rs +++ b/pallets/subtensor/src/benchmarks.rs @@ -314,33 +314,33 @@ benchmarks! { assert_ok!(Subtensor::::register_network(RawOrigin::Signed(coldkey.clone()).into())); }: dissolve_network(RawOrigin::Signed(coldkey), 1) - swap_hotkey { - let seed: u32 = 1; - let coldkey: T::AccountId = account("Alice", 0, seed); - let old_hotkey: T::AccountId = account("Bob", 0, seed); - let new_hotkey: T::AccountId = account("Charlie", 0, seed); - - let netuid = 1u16; - Subtensor::::init_new_network(netuid, 100); - Subtensor::::set_min_burn(netuid, 1); - Subtensor::::set_max_burn(netuid, 1); - Subtensor::::set_target_registrations_per_interval(netuid, 256); - Subtensor::::set_max_registrations_per_block(netuid, 256); - - Subtensor::::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64); - assert_ok!(Subtensor::::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, old_hotkey.clone())); - assert_ok!(Subtensor::::become_delegate(RawOrigin::Signed(coldkey.clone()).into(), old_hotkey.clone())); - - let max_uids = Subtensor::::get_max_allowed_uids(netuid) as u32; - for i in 0..max_uids - 1 { - let coldkey: T::AccountId = account("Axon", 0, i); - let hotkey: T::AccountId = account("Hotkey", 0, i); - - Subtensor::::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64); - assert_ok!(Subtensor::::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey)); - assert_ok!(Subtensor::::add_stake(RawOrigin::Signed(coldkey).into(), old_hotkey.clone(), 1_000_000_000)); - } - }: _(RawOrigin::Signed(coldkey), old_hotkey, new_hotkey) + // swap_hotkey { + // let seed: u32 = 1; + // let coldkey: T::AccountId = account("Alice", 0, seed); + // let old_hotkey: T::AccountId = account("Bob", 0, seed); + // let new_hotkey: T::AccountId = account("Charlie", 0, seed); + + // let netuid = 1u16; + // Subtensor::::init_new_network(netuid, 100); + // Subtensor::::set_min_burn(netuid, 1); + // Subtensor::::set_max_burn(netuid, 1); + // Subtensor::::set_target_registrations_per_interval(netuid, 256); + // Subtensor::::set_max_registrations_per_block(netuid, 256); + + // Subtensor::::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64); + // assert_ok!(Subtensor::::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, old_hotkey.clone())); + // assert_ok!(Subtensor::::become_delegate(RawOrigin::Signed(coldkey.clone()).into(), old_hotkey.clone())); + + // let max_uids = Subtensor::::get_max_allowed_uids(netuid) as u32; + // for i in 0..max_uids - 1 { + // let coldkey: T::AccountId = account("Axon", 0, i); + // let hotkey: T::AccountId = account("Hotkey", 0, i); + + // Subtensor::::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64); + // assert_ok!(Subtensor::::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey)); + // assert_ok!(Subtensor::::add_stake(RawOrigin::Signed(coldkey).into(), old_hotkey.clone(), 1_000_000_000)); + // } + // }: _(RawOrigin::Signed(coldkey), old_hotkey, new_hotkey) commit_weights { let tempo: u16 = 1; diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index a4abd124ff..d1fe2e0c9d 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: 164, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From 7a91a42adbaeeffde7d5549e7be16448142ca13f Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Mon, 15 Jul 2024 11:25:17 -0400 Subject: [PATCH 24/24] comment out unused variable --- pallets/subtensor/src/swap.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index 50e3ed944d..ce9646d88a 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -973,7 +973,7 @@ impl Pallet { } pub fn swap_hotfix(old_coldkey: &T::AccountId, new_coldkey: &T::AccountId) { - let weight = T::DbWeight::get().reads_writes(2, 1); + // let weight = T::DbWeight::get().reads_writes(2, 1); let staking_hotkeys = StakingHotkeys::::get(old_coldkey); for staking_hotkey in staking_hotkeys { if Stake::::contains_key(staking_hotkey.clone(), old_coldkey) {