From 6b6460d4fc7864543af574d7390c53fa3556691d Mon Sep 17 00:00:00 2001 From: const Date: Mon, 22 Jul 2024 15:21:13 -0500 Subject: [PATCH] clippy and fmt --- pallets/subtensor/src/lib.rs | 6 +- pallets/subtensor/src/swap.rs | 4 - pallets/subtensor/src/swap_hotkey.rs | 57 +++--- pallets/subtensor/tests/swap.rs | 1 - pallets/subtensor/tests/swap_hotkey.rs | 248 ++++++++++++++++++++----- 5 files changed, 234 insertions(+), 82 deletions(-) diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index d492b5477..6a353eafb 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -1088,11 +1088,9 @@ pub mod pallet { pub type Active = StorageMap<_, Identity, u16, Vec, ValueQuery, EmptyBoolVec>; #[pallet::storage] // --- DMAP ( netuid ) --> rank - pub type Rank = - StorageMap<_, Identity, u16, Vec, ValueQuery, EmptyU16Vec>; + pub type Rank = StorageMap<_, Identity, u16, Vec, ValueQuery, EmptyU16Vec>; #[pallet::storage] // --- DMAP ( netuid ) --> trust - pub type Trust = - StorageMap<_, Identity, u16, Vec, ValueQuery, EmptyU16Vec>; + pub type Trust = StorageMap<_, Identity, u16, Vec, ValueQuery, EmptyU16Vec>; #[pallet::storage] // --- DMAP ( netuid ) --> consensus pub type Consensus = StorageMap<_, Identity, u16, Vec, ValueQuery, EmptyU16Vec>; diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index 5d8f66c68..b6b0e9ba7 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -6,7 +6,6 @@ use frame_support::{storage::IterableStorageDoubleMap, 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 @@ -378,7 +377,6 @@ impl Pallet { netuid_is_member } - /// Swaps the total stake associated with a coldkey from the old coldkey to the new coldkey. /// /// # Arguments @@ -635,6 +633,4 @@ impl Pallet { } weight.saturating_accrue(T::DbWeight::get().reads(TotalNetworks::::get() as u64)); } - - } diff --git a/pallets/subtensor/src/swap_hotkey.rs b/pallets/subtensor/src/swap_hotkey.rs index 02052e4dd..5c3fecbba 100644 --- a/pallets/subtensor/src/swap_hotkey.rs +++ b/pallets/subtensor/src/swap_hotkey.rs @@ -139,8 +139,12 @@ impl Pallet { /// # Note /// This function performs extensive storage reads and writes, which can be computationally expensive. /// The accumulated weight should be carefully considered in the context of block limits. - pub fn perform_hotkey_swap( old_hotkey: &T::AccountId, new_hotkey: &T::AccountId, coldkey: &T::AccountId, weight: &mut Weight ) -> DispatchResult { - + pub fn perform_hotkey_swap( + old_hotkey: &T::AccountId, + new_hotkey: &T::AccountId, + coldkey: &T::AccountId, + weight: &mut Weight, + ) -> DispatchResult { // 1. Swap owner. // Owner( hotkey ) -> coldkey -- the coldkey that owns the hotkey. Owner::::remove(old_hotkey); @@ -153,7 +157,7 @@ impl Pallet { // Add the new key if needed. if !hotkeys.contains(new_hotkey) { hotkeys.push(new_hotkey.clone()); - } + } // Remove the old key. hotkeys.retain(|hk| *hk != *old_hotkey); OwnedHotkeys::::insert(coldkey, hotkeys); @@ -161,15 +165,19 @@ impl Pallet { // 3. Swap total hotkey stake. // TotalHotkeyStake( hotkey ) -> stake -- the total stake that the hotkey has across all delegates. - let old_total_hotkey_stake = TotalHotkeyStake::::get( old_hotkey ); // Get the old total hotkey stake. - let new_total_hotkey_stake = TotalHotkeyStake::::get( new_hotkey ); // Get the new total hotkey stake. - TotalHotkeyStake::::remove( old_hotkey ); // Remove the old total hotkey stake. - TotalHotkeyStake::::insert( new_hotkey, old_total_hotkey_stake.saturating_add( new_total_hotkey_stake ) ); // Insert the new total hotkey stake via the addition. + let old_total_hotkey_stake = TotalHotkeyStake::::get(old_hotkey); // Get the old total hotkey stake. + let new_total_hotkey_stake = TotalHotkeyStake::::get(new_hotkey); // Get the new total hotkey stake. + TotalHotkeyStake::::remove(old_hotkey); // Remove the old total hotkey stake. + TotalHotkeyStake::::insert( + new_hotkey, + old_total_hotkey_stake.saturating_add(new_total_hotkey_stake), + ); // Insert the new total hotkey stake via the addition. weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); // 4. Swap total hotkey stakes. // TotalHotkeyColdkeyStakesThisInterval( hotkey ) --> (u64: stakes, u64: block_number) - let stake_tuples: Vec<(T::AccountId, (u64, u64))> = TotalHotkeyColdkeyStakesThisInterval::::iter_prefix(old_hotkey).collect(); + let stake_tuples: Vec<(T::AccountId, (u64, u64))> = + TotalHotkeyColdkeyStakesThisInterval::::iter_prefix(old_hotkey).collect(); for (coldkey, stake_tup) in stake_tuples { // NOTE: You could use this to increase your allowed stake operations but this would cost. TotalHotkeyColdkeyStakesThisInterval::::insert(new_hotkey, &coldkey, stake_tup); @@ -179,14 +187,14 @@ impl Pallet { // 5. Swap LastTxBlock // LastTxBlock( hotkey ) --> u64 -- the last transaction block for the hotkey. - LastTxBlock::::remove( old_hotkey ); - LastTxBlock::::insert( new_hotkey, Self::get_current_block_as_u64() ); + LastTxBlock::::remove(old_hotkey); + LastTxBlock::::insert(new_hotkey, Self::get_current_block_as_u64()); weight.saturating_accrue(T::DbWeight::get().reads_writes(0, 2)); // 6. Swap LastTxBlockDelegateTake // LastTxBlockDelegateTake( hotkey ) --> u64 -- the last transaction block for the hotkey delegate take. - LastTxBlockDelegateTake::::remove( old_hotkey ); - LastTxBlockDelegateTake::::insert( new_hotkey, Self::get_current_block_as_u64() ); + LastTxBlockDelegateTake::::remove(old_hotkey); + LastTxBlockDelegateTake::::insert(new_hotkey, Self::get_current_block_as_u64()); weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2)); // 7. Swap Senate members. @@ -198,19 +206,19 @@ impl Pallet { // 8. Swap delegates. // Delegates( hotkey ) -> take value -- the hotkey delegate take value. - let old_delegate_take = Delegates::::get( old_hotkey ); - Delegates::::remove( old_hotkey ); // Remove the old delegate take. - Delegates::::insert( new_hotkey, old_delegate_take ); // Insert the new delegate take. + let old_delegate_take = Delegates::::get(old_hotkey); + Delegates::::remove(old_hotkey); // Remove the old delegate take. + Delegates::::insert(new_hotkey, old_delegate_take); // Insert the new delegate take. weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2)); // 9. Swap all subnet specific info. let all_netuids: Vec = Self::get_all_subnet_netuids(); - for netuid in all_netuids { + for netuid in all_netuids { // 9.1 Remove the previous hotkey and insert the new hotkey from membership. // IsNetworkMember( hotkey, netuid ) -> bool -- is the hotkey a subnet member. - let is_network_member: bool = IsNetworkMember::::get( old_hotkey, netuid ); - IsNetworkMember::::remove( old_hotkey, netuid ); - IsNetworkMember::::insert( new_hotkey, netuid, is_network_member ); + let is_network_member: bool = IsNetworkMember::::get(old_hotkey, netuid); + IsNetworkMember::::remove(old_hotkey, netuid); + IsNetworkMember::::insert(new_hotkey, netuid, is_network_member); weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2)); // 9.2 Swap Uids + Keys. @@ -249,7 +257,7 @@ impl Pallet { } } - // 9.5 Swap WeightCommits + // 9.5 Swap WeightCommits // WeightCommits( hotkey ) --> Vec -- the weight commits for the hotkey. if is_network_member { if let Ok(old_weight_commits) = WeightCommits::::try_get(netuid, old_hotkey) { @@ -273,14 +281,13 @@ impl Pallet { weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2)); } } - } // 10. Swap Stake. // Stake( hotkey, coldkey ) -> stake -- the stake that the hotkey controls on behalf of the coldkey. let stakes: Vec<(T::AccountId, u64)> = Stake::::iter_prefix(old_hotkey).collect(); // Clear the entire old prefix here. - let _ = Stake::::clear_prefix( old_hotkey, stakes.len() as u32, None ); + let _ = Stake::::clear_prefix(old_hotkey, stakes.len() as u32, None); // Iterate over all the staking rows and insert them into the new hotkey. for (coldkey, old_stake_amount) in stakes { weight.saturating_accrue(T::DbWeight::get().reads(1)); @@ -290,7 +297,11 @@ impl Pallet { // Get the new stake value. let new_stake_value: u64 = Stake::::get(new_hotkey, &coldkey); // Insert the new stake value. - Stake::::insert(new_hotkey, &coldkey, new_stake_value.saturating_add(old_stake_amount)); + Stake::::insert( + new_hotkey, + &coldkey, + new_stake_value.saturating_add(old_stake_amount), + ); weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); // Swap StakingHotkeys. diff --git a/pallets/subtensor/tests/swap.rs b/pallets/subtensor/tests/swap.rs index f7f288cc2..0a3a85dff 100644 --- a/pallets/subtensor/tests/swap.rs +++ b/pallets/subtensor/tests/swap.rs @@ -9,7 +9,6 @@ use mock::*; use pallet_subtensor::*; use sp_core::U256; - #[test] fn test_do_swap_coldkey_success() { new_test_ext(1).execute_with(|| { diff --git a/pallets/subtensor/tests/swap_hotkey.rs b/pallets/subtensor/tests/swap_hotkey.rs index 35227d3b1..68f4ec49c 100644 --- a/pallets/subtensor/tests/swap_hotkey.rs +++ b/pallets/subtensor/tests/swap_hotkey.rs @@ -7,9 +7,8 @@ use frame_system::{Config, RawOrigin}; mod mock; use mock::*; use pallet_subtensor::*; -use sp_core::U256; use sp_core::H256; - +use sp_core::U256; // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_owner --exact --nocapture #[test] @@ -21,7 +20,12 @@ fn test_swap_owner() { let mut weight = Weight::zero(); Owner::::insert(&old_hotkey, &coldkey); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); assert!(!Owner::::contains_key(&old_hotkey)); assert_eq!(Owner::::get(&new_hotkey), coldkey); @@ -38,7 +42,12 @@ fn test_swap_owned_hotkeys() { let mut weight = Weight::zero(); OwnedHotkeys::::insert(&coldkey, vec![old_hotkey]); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); let hotkeys = OwnedHotkeys::::get(&coldkey); assert!(!hotkeys.contains(&old_hotkey)); @@ -57,7 +66,12 @@ fn test_swap_total_hotkey_stake() { TotalHotkeyStake::::insert(&old_hotkey, 100); TotalHotkeyStake::::insert(&new_hotkey, 50); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); assert!(!TotalHotkeyStake::::contains_key(&old_hotkey)); assert_eq!(TotalHotkeyStake::::get(&new_hotkey), 150); @@ -74,10 +88,21 @@ fn test_swap_total_hotkey_coldkey_stakes_this_interval() { let mut weight = Weight::zero(); TotalHotkeyColdkeyStakesThisInterval::::insert(&old_hotkey, &coldkey, (100, 1000)); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); - assert!(!TotalHotkeyColdkeyStakesThisInterval::::contains_key(&old_hotkey, &coldkey)); - assert_eq!(TotalHotkeyColdkeyStakesThisInterval::::get(&new_hotkey, &coldkey), (100, 1000)); + assert!(!TotalHotkeyColdkeyStakesThisInterval::::contains_key( + &old_hotkey, + &coldkey + )); + assert_eq!( + TotalHotkeyColdkeyStakesThisInterval::::get(&new_hotkey, &coldkey), + (100, 1000) + ); }); } @@ -91,10 +116,18 @@ fn test_swap_last_tx_block() { let mut weight = Weight::zero(); LastTxBlock::::insert(&old_hotkey, 1000); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); assert!(!LastTxBlock::::contains_key(&old_hotkey)); - assert_eq!(LastTxBlock::::get(&new_hotkey), SubtensorModule::get_current_block_as_u64()); + assert_eq!( + LastTxBlock::::get(&new_hotkey), + SubtensorModule::get_current_block_as_u64() + ); }); } @@ -108,10 +141,18 @@ fn test_swap_last_tx_block_delegate_take() { let mut weight = Weight::zero(); pallet_subtensor::LastTxBlockDelegateTake::::insert(&old_hotkey, 1000); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); assert!(!LastTxBlockDelegateTake::::contains_key(&old_hotkey)); - assert_eq!(LastTxBlockDelegateTake::::get(&new_hotkey), SubtensorModule::get_current_block_as_u64()); + assert_eq!( + LastTxBlockDelegateTake::::get(&new_hotkey), + SubtensorModule::get_current_block_as_u64() + ); }); } @@ -126,7 +167,12 @@ fn test_swap_senate_members() { // Assuming there's a way to add a member to the senate // SenateMembers::add_member(&old_hotkey); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); // Assert that the old_hotkey is no longer a member and new_hotkey is now a member // assert!(!SenateMembers::is_member(&old_hotkey)); @@ -144,7 +190,12 @@ fn test_swap_delegates() { let mut weight = Weight::zero(); Delegates::::insert(&old_hotkey, 100); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); assert!(!Delegates::::contains_key(&old_hotkey)); assert_eq!(Delegates::::get(&new_hotkey), 100); @@ -163,7 +214,12 @@ fn test_swap_subnet_membership() { add_network(netuid, 0, 1); IsNetworkMember::::insert(&old_hotkey, netuid, true); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); assert!(!IsNetworkMember::::contains_key(&old_hotkey, netuid)); assert!(IsNetworkMember::::get(&new_hotkey, netuid)); @@ -186,7 +242,12 @@ fn test_swap_uids_and_keys() { Uids::::insert(netuid, &old_hotkey, uid); Keys::::insert(netuid, uid, old_hotkey); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); assert_eq!(Uids::::get(netuid, &old_hotkey), None); assert_eq!(Uids::::get(netuid, &new_hotkey), Some(uid)); @@ -209,10 +270,18 @@ fn test_swap_prometheus() { IsNetworkMember::::insert(&old_hotkey, netuid, true); Prometheus::::insert(netuid, &old_hotkey, prometheus_info.clone()); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); assert!(!Prometheus::::contains_key(netuid, &old_hotkey)); - assert_eq!(Prometheus::::get(netuid, &new_hotkey), Some(prometheus_info)); + assert_eq!( + Prometheus::::get(netuid, &new_hotkey), + Some(prometheus_info) + ); }); } @@ -231,13 +300,18 @@ fn test_swap_axons() { IsNetworkMember::::insert(&old_hotkey, netuid, true); Axons::::insert(netuid, &old_hotkey, axon_info.clone()); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); assert!(!Axons::::contains_key(netuid, &old_hotkey)); assert_eq!(Axons::::get(netuid, &new_hotkey), Some(axon_info)); }); } - + // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_weight_commits --exact --nocapture #[test] fn test_swap_weight_commits() { @@ -253,10 +327,18 @@ fn test_swap_weight_commits() { IsNetworkMember::::insert(&old_hotkey, netuid, true); WeightCommits::::insert(netuid, &old_hotkey, weight_commits.clone()); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); assert!(!WeightCommits::::contains_key(netuid, &old_hotkey)); - assert_eq!(WeightCommits::::get(netuid, &new_hotkey), Some(weight_commits)); + assert_eq!( + WeightCommits::::get(netuid, &new_hotkey), + Some(weight_commits) + ); }); } @@ -274,12 +356,23 @@ fn test_swap_loaded_emission() { add_network(netuid, 0, 1); IsNetworkMember::::insert(&old_hotkey, netuid, true); - LoadedEmission::::insert(netuid, vec![(old_hotkey, server_emission, validator_emission)]); + LoadedEmission::::insert( + netuid, + vec![(old_hotkey, server_emission, validator_emission)], + ); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); let new_loaded_emission = LoadedEmission::::get(netuid); - assert_eq!(new_loaded_emission, Some(vec![(new_hotkey, server_emission, validator_emission)])); + assert_eq!( + new_loaded_emission, + Some(vec![(new_hotkey, server_emission, validator_emission)]) + ); }); } @@ -295,7 +388,12 @@ fn test_swap_stake() { Stake::::insert(&old_hotkey, &coldkey, stake_amount); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); assert!(!Stake::::contains_key(&old_hotkey, &coldkey)); assert_eq!(Stake::::get(&new_hotkey, &coldkey), stake_amount); @@ -314,7 +412,12 @@ fn test_swap_staking_hotkeys() { Stake::::insert(&old_hotkey, &coldkey, 100); StakingHotkeys::::insert(&coldkey, vec![old_hotkey]); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); let staking_hotkeys = StakingHotkeys::::get(&coldkey); assert!(!staking_hotkeys.contains(&old_hotkey)); @@ -337,7 +440,12 @@ fn test_swap_hotkey_with_multiple_coldkeys() { StakingHotkeys::::insert(&coldkey1, vec![old_hotkey]); StakingHotkeys::::insert(&coldkey2, vec![old_hotkey]); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey1, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey1, + &mut weight + )); assert_eq!(Stake::::get(&new_hotkey, &coldkey1), 100); assert_eq!(Stake::::get(&new_hotkey, &coldkey2), 200); @@ -358,7 +466,12 @@ fn test_swap_hotkey_with_existing_stake() { Stake::::insert(&old_hotkey, &coldkey, 100); Stake::::insert(&new_hotkey, &coldkey, 50); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); assert_eq!(Stake::::get(&new_hotkey, &coldkey), 150); }); @@ -380,7 +493,12 @@ fn test_swap_hotkey_with_multiple_subnets() { IsNetworkMember::::insert(&old_hotkey, netuid1, true); IsNetworkMember::::insert(&old_hotkey, netuid2, true); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); assert!(IsNetworkMember::::get(&new_hotkey, netuid1)); assert!(IsNetworkMember::::get(&new_hotkey, netuid2)); @@ -405,7 +523,12 @@ fn test_swap_staking_hotkeys_multiple_coldkeys() { StakingHotkeys::::insert(&coldkey1, vec![old_hotkey]); StakingHotkeys::::insert(&coldkey2, vec![old_hotkey, U256::from(5)]); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey1, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey1, + &mut weight + )); // Check if new_hotkey replaced old_hotkey in StakingHotkeys assert!(StakingHotkeys::::get(&coldkey1).contains(&new_hotkey)); @@ -414,7 +537,8 @@ fn test_swap_staking_hotkeys_multiple_coldkeys() { // 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))); // Other hotkeys should remain + assert!(StakingHotkeys::::get(&coldkey2).contains(&U256::from(5))); + // Other hotkeys should remain }); } @@ -430,7 +554,12 @@ fn test_swap_hotkey_with_no_stake() { // Set up initial state with no stake Owner::::insert(&old_hotkey, &coldkey); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey, + &mut weight + )); // Check if ownership transferred assert!(!Owner::::contains_key(&old_hotkey)); @@ -464,7 +593,12 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { IsNetworkMember::::insert(&old_hotkey, netuid2, true); TotalHotkeyStake::::insert(&old_hotkey, 300); - assert_ok!(SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey1, &mut weight)); + assert_ok!(SubtensorModule::perform_hotkey_swap( + &old_hotkey, + &new_hotkey, + &coldkey1, + &mut weight + )); // Check ownership transfer assert!(!Owner::::contains_key(&old_hotkey)); @@ -637,7 +771,6 @@ fn test_swap_owner_new_hotkey_already_exists() { }); } - // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_total_hotkey_stake_success --exact --nocapture #[test] fn test_swap_total_hotkey_stake_success() { @@ -660,7 +793,6 @@ fn test_swap_total_hotkey_stake_success() { }); } - // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_delegates_success --exact --nocapture #[test] fn test_swap_delegates_success() { @@ -683,7 +815,6 @@ fn test_swap_delegates_success() { }); } - // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey -- test_swap_stake_success --exact --nocapture #[test] fn test_swap_stake_success() { @@ -745,12 +876,7 @@ fn test_swap_total_hotkey_coldkey_stakes_this_interval_success() { TotalHotkeyColdkeyStakesThisInterval::::insert(old_hotkey, coldkey, stake); // Perform the swap - SubtensorModule::perform_hotkey_swap( - &old_hotkey, - &new_hotkey, - &coldkey, - &mut weight, - ); + SubtensorModule::perform_hotkey_swap(&old_hotkey, &new_hotkey, &coldkey, &mut weight); // Verify the swap assert_eq!( @@ -780,7 +906,11 @@ fn test_swap_hotkey_error_cases() { // Test not enough balance let swap_cost = SubtensorModule::get_key_swap_cost(); assert_noop!( - SubtensorModule::do_swap_hotkey(RuntimeOrigin::signed(coldkey), &old_hotkey, &new_hotkey), + SubtensorModule::do_swap_hotkey( + RuntimeOrigin::signed(coldkey), + &old_hotkey, + &new_hotkey + ), Error::::NotEnoughBalanceToPaySwapHotKey ); @@ -789,29 +919,47 @@ fn test_swap_hotkey_error_cases() { // Test new hotkey same as old assert_noop!( - SubtensorModule::do_swap_hotkey(RuntimeOrigin::signed(coldkey), &old_hotkey, &old_hotkey), + SubtensorModule::do_swap_hotkey( + RuntimeOrigin::signed(coldkey), + &old_hotkey, + &old_hotkey + ), Error::::NewHotKeyIsSameWithOld ); // Test new hotkey already registered IsNetworkMember::::insert(&new_hotkey, 0, true); assert_noop!( - SubtensorModule::do_swap_hotkey(RuntimeOrigin::signed(coldkey), &old_hotkey, &new_hotkey), + SubtensorModule::do_swap_hotkey( + RuntimeOrigin::signed(coldkey), + &old_hotkey, + &new_hotkey + ), Error::::HotKeyAlreadyRegisteredInSubNet ); IsNetworkMember::::remove(&new_hotkey, 0); // Test non-associated coldkey assert_noop!( - SubtensorModule::do_swap_hotkey(RuntimeOrigin::signed(wrong_coldkey), &old_hotkey, &new_hotkey), + SubtensorModule::do_swap_hotkey( + RuntimeOrigin::signed(wrong_coldkey), + &old_hotkey, + &new_hotkey + ), Error::::NonAssociatedColdKey ); - // Run the successful swap - assert_ok!(SubtensorModule::do_swap_hotkey(RuntimeOrigin::signed(coldkey), &old_hotkey, &new_hotkey)); + assert_ok!(SubtensorModule::do_swap_hotkey( + RuntimeOrigin::signed(coldkey), + &old_hotkey, + &new_hotkey + )); // Check balance after swap - assert_eq!(Balances::free_balance(&coldkey), initial_balance - swap_cost); + assert_eq!( + Balances::free_balance(&coldkey), + initial_balance - swap_cost + ); }); }