From b43bed6f0834ba307b64b2654d2722f396131ba3 Mon Sep 17 00:00:00 2001 From: Samuel Dare Date: Tue, 9 Jul 2024 22:37:13 +0400 Subject: [PATCH 1/8] draft: hotkey swap for senate --- pallets/subtensor/src/swap.rs | 14 +++++ pallets/subtensor/tests/swap.rs | 93 +++++++++++++++++++++++++-------- 2 files changed, 85 insertions(+), 22 deletions(-) diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index 24d67aeb49..2073aa5e28 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -76,6 +76,7 @@ impl Pallet { Self::swap_prometheus(old_hotkey, new_hotkey, &netuid_is_member, &mut weight); Self::swap_total_hotkey_coldkey_stakes_this_interval(old_hotkey, new_hotkey, &mut weight); + Self::swap_senate_member(old_hotkey, new_hotkey, &mut weight)?; Self::set_last_tx_block(&coldkey, block); weight.saturating_accrue(T::DbWeight::get().writes(1)); @@ -948,4 +949,17 @@ impl Pallet { } weight.saturating_accrue(T::DbWeight::get().reads(TotalNetworks::::get() as u64)); } + + /// Swaps the Senate membership from the old hotkey to the new hotkey if applicable. + pub fn swap_senate_member( + old_hotkey: &T::AccountId, + new_hotkey: &T::AccountId, + weight: &mut Weight, + ) -> DispatchResult { + if T::SenateMembers::is_member(old_hotkey) { + T::SenateMembers::swap_member(old_hotkey, new_hotkey).map_err(|e| e.error)?; + weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); + } + Ok(()) + } } diff --git a/pallets/subtensor/tests/swap.rs b/pallets/subtensor/tests/swap.rs index 90ebdfdcbe..7445470f83 100644 --- a/pallets/subtensor/tests/swap.rs +++ b/pallets/subtensor/tests/swap.rs @@ -2,7 +2,7 @@ use codec::Encode; use frame_support::weights::Weight; -use frame_support::{assert_err, assert_ok}; +use frame_support::{assert_err, assert_noop, assert_ok}; use frame_system::Config; mod mock; use mock::*; @@ -1666,24 +1666,73 @@ fn test_coldkey_swap_total() { }); } -// #[test] -// fn test_coldkey_arbitrated_sw() { -// new_test_ext(1).execute_with(|| { -// let coldkey = U256::from(1); -// let hotkey = U256::from(2); -// let netuid = 1u16; - -// // Setup initial state -// add_network(netuid, 13, 0); -// register_ok_neuron(netuid, hotkey, coldkey, 0); - -// // Check if coldkey has associated hotkeys -// assert!(SubtensorModule::coldkey_has_associated_hotkeys(&coldkey)); - -// // Check for a coldkey without associated hotkeys -// let unassociated_coldkey = U256::from(3); -// assert!(!SubtensorModule::coldkey_has_associated_hotkeys( -// &unassociated_coldkey -// )); -// }); -// } +#[test] +fn test_swap_senate_member() { + new_test_ext(1).execute_with(|| { + let old_hotkey = U256::from(1); + let new_hotkey = U256::from(2); + let non_member_hotkey = U256::from(3); + let mut weight = Weight::zero(); + + // Setup: Add old_hotkey as a Senate member + assert_ok!(Senate::set_members( + RuntimeOrigin::root(), + vec![old_hotkey], + None, + 0 + )); + + // Test 1: Successful swap + assert_ok!(SubtensorModule::swap_senate_member( + &old_hotkey, + &new_hotkey, + &mut weight + )); + assert!(Senate::is_member(&new_hotkey)); + assert!(!Senate::is_member(&old_hotkey)); + + // Verify weight update + let expected_weight = ::DbWeight::get().reads_writes(2, 2); + assert_eq!(weight, expected_weight); + + // Reset weight for next test + weight = Weight::zero(); + + // Test 2: Swap with non-member (should not change anything) + assert_ok!(SubtensorModule::swap_senate_member( + &non_member_hotkey, + &new_hotkey, + &mut weight + )); + assert!(Senate::is_member(&new_hotkey)); + assert!(!Senate::is_member(&non_member_hotkey)); + + // Verify weight update (should only have read operations) + let expected_weight = ::DbWeight::get().reads(1); + assert_eq!(weight, expected_weight); + + // Reset weight for next test + weight = Weight::zero(); + + // Test 3: Setup for swap to an existing member + let another_member = U256::from(4); + assert_ok!(Senate::set_members( + RuntimeOrigin::root(), + vec![new_hotkey, another_member], + None, + 1 + )); + + // Test 4: Setup for swap with maximum members reached + let mut members = vec![new_hotkey, another_member]; + for i in 5..=SenateMaxMembers::get() { + members.push(U256::from(i as u64)); + } + assert_ok!(Senate::set_members( + RuntimeOrigin::root(), + members.clone(), + None, + 2 + )); + }); +} From 9d1f6c9cbd9b1bbf2d498b0a5025351ab87f0c8d Mon Sep 17 00:00:00 2001 From: Samuel Dare Date: Fri, 12 Jul 2024 21:19:24 +0400 Subject: [PATCH 2/8] feat: remove schedule coldkey swap --- pallets/subtensor/src/benchmarks.rs | 26 ----------------------- pallets/subtensor/src/lib.rs | 2 +- pallets/subtensor/src/swap.rs | 32 +++++++++++++++++------------ runtime/src/lib.rs | 3 +-- 4 files changed, 21 insertions(+), 42 deletions(-) diff --git a/pallets/subtensor/src/benchmarks.rs b/pallets/subtensor/src/benchmarks.rs index 6da83c5afb..80a375d103 100644 --- a/pallets/subtensor/src/benchmarks.rs +++ b/pallets/subtensor/src/benchmarks.rs @@ -429,30 +429,4 @@ reveal_weights { }: reveal_weights(RawOrigin::Signed(hotkey.clone()), netuid, uids, weight_values, salt, version_key) - schedule_coldkey_swap { - let seed: u32 = 1; - let old_coldkey: T::AccountId = account("OldColdkey", 0, seed); - let new_coldkey: T::AccountId = account("NewColdkey", 0, seed + 1); - let hotkey: T::AccountId = account("Hotkey", 0, seed); - - let netuid = 1u16; - let tempo = 1u16; - let block_number: u64 = Subtensor::::get_current_block_as_u64(); - let nonce = 0; - - // Initialize the network - Subtensor::::init_new_network(netuid, tempo); - Subtensor::::set_network_registration_allowed(netuid, true); - - // Add balance to the old coldkey account - let amount_to_be_staked: u64 = 1000000u32.into(); - Subtensor::::add_balance_to_coldkey_account(&old_coldkey.clone(), amount_to_be_staked+1000000000); - // Burned register the hotkey with the old coldkey - assert_ok!(Subtensor::::burned_register( - RawOrigin::Signed(old_coldkey.clone()).into(), - netuid, - hotkey.clone() - )); - - }: schedule_coldkey_swap(RawOrigin::Signed(old_coldkey.clone()), new_coldkey.clone(), vec![], block_number, nonce) } diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index d4def0c27d..c4ac1f3e58 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -2089,7 +2089,6 @@ pub mod pallet { ) -> DispatchResultWithPostInfo { Self::do_swap_coldkey(origin, &new_coldkey) } - /// Unstakes all tokens associated with a hotkey and transfers them to a new coldkey. /// /// # Arguments @@ -2105,6 +2104,7 @@ pub mod pallet { /// # Weight /// /// Weight is calculated based on the number of database reads and writes. + #[cfg(test)] #[pallet::call_index(72)] #[pallet::weight((Weight::from_parts(21_000_000, 0) .saturating_add(T::DbWeight::get().reads(3)) diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index 24d67aeb49..6c914f855f 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -31,10 +31,6 @@ impl Pallet { new_hotkey: &T::AccountId, ) -> DispatchResultWithPostInfo { let coldkey = ensure_signed(origin)?; - ensure!( - !Self::coldkey_in_arbitration(&coldkey), - Error::::ColdkeyIsInArbitration - ); let mut weight = T::DbWeight::get().reads(2); @@ -60,6 +56,16 @@ impl Pallet { T::DbWeight::get().reads((TotalNetworks::::get().saturating_add(1u16)) as u64), ); + let swap_cost = Self::get_hotkey_swap_cost(); + log::debug!("Swap cost: {:?}", swap_cost); + + ensure!( + Self::can_remove_balance_from_coldkey_account(&coldkey, swap_cost), + Error::::NotEnoughBalanceToPaySwapHotKey + ); + let actual_burn_amount = Self::remove_balance_from_coldkey_account(&coldkey, swap_cost)?; + Self::burn_tokens(actual_burn_amount); + Self::swap_owner(old_hotkey, new_hotkey, &coldkey, &mut weight); Self::swap_total_hotkey_stake(old_hotkey, new_hotkey, &mut weight); Self::swap_delegates(old_hotkey, new_hotkey, &mut weight); @@ -189,15 +195,15 @@ impl Pallet { /// /// This function calculates the remaining arbitration period by subtracting the current block number /// from the arbitration block number of the coldkey. - // pub fn get_remaining_arbitration_period(coldkey: &T::AccountId) -> u64 { - // let current_block: u64 = Self::get_current_block_as_u64(); - // let arbitration_block: u64 = ColdkeyArbitrationBlock::::get(coldkey); - // if arbitration_block > current_block { - // arbitration_block.saturating_sub(current_block) - // } else { - // 0 - // } - // } + pub fn get_remaining_arbitration_period(coldkey: &T::AccountId) -> u64 { + let current_block: u64 = Self::get_current_block_as_u64(); + let arbitration_block: u64 = ColdkeyArbitrationBlock::::get(coldkey); + if arbitration_block > current_block { + arbitration_block.saturating_sub(current_block) + } else { + 0 + } + } pub fn meets_min_allowed_coldkey_balance(coldkey: &T::AccountId) -> bool { let all_staked_keys: Vec = StakingHotkeys::::get(coldkey); diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index de8be6e61f..0e171e9ead 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -312,8 +312,7 @@ impl Contains for SafeModeWhitelistedCalls { | RuntimeCall::SafeMode(_) | RuntimeCall::Timestamp(_) | RuntimeCall::SubtensorModule( - pallet_subtensor::Call::schedule_coldkey_swap { .. } - | pallet_subtensor::Call::set_weights { .. } + pallet_subtensor::Call::set_weights { .. } | pallet_subtensor::Call::set_root_weights { .. } | pallet_subtensor::Call::serve_axon { .. } ) From 90512c28279dcb0b4a233b61351ece84284a1fdd Mon Sep 17 00:00:00 2001 From: Samuel Dare Date: Fri, 12 Jul 2024 21:41:33 +0400 Subject: [PATCH 3/8] chore: fix tests --- pallets/subtensor/src/swap.rs | 15 ++++++++++++--- pallets/subtensor/tests/swap.rs | 34 ++++----------------------------- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index 2073aa5e28..02abda0d5b 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -60,6 +60,16 @@ impl Pallet { T::DbWeight::get().reads((TotalNetworks::::get().saturating_add(1u16)) as u64), ); + let swap_cost = Self::get_hotkey_swap_cost(); + log::debug!("Swap cost: {:?}", swap_cost); + + ensure!( + Self::can_remove_balance_from_coldkey_account(&coldkey, swap_cost), + Error::::NotEnoughBalanceToPaySwapHotKey + ); + let actual_burn_amount = Self::remove_balance_from_coldkey_account(&coldkey, swap_cost)?; + Self::burn_tokens(actual_burn_amount); + Self::swap_owner(old_hotkey, new_hotkey, &coldkey, &mut weight); Self::swap_total_hotkey_stake(old_hotkey, new_hotkey, &mut weight); Self::swap_delegates(old_hotkey, new_hotkey, &mut weight); @@ -76,7 +86,6 @@ impl Pallet { Self::swap_prometheus(old_hotkey, new_hotkey, &netuid_is_member, &mut weight); Self::swap_total_hotkey_coldkey_stakes_this_interval(old_hotkey, new_hotkey, &mut weight); - Self::swap_senate_member(old_hotkey, new_hotkey, &mut weight)?; Self::set_last_tx_block(&coldkey, block); weight.saturating_accrue(T::DbWeight::get().writes(1)); @@ -950,15 +959,15 @@ impl Pallet { weight.saturating_accrue(T::DbWeight::get().reads(TotalNetworks::::get() as u64)); } - /// Swaps the Senate membership from the old hotkey to the new hotkey if applicable. pub fn swap_senate_member( old_hotkey: &T::AccountId, new_hotkey: &T::AccountId, weight: &mut Weight, ) -> DispatchResult { + weight.saturating_accrue(T::DbWeight::get().reads(1)); if T::SenateMembers::is_member(old_hotkey) { T::SenateMembers::swap_member(old_hotkey, new_hotkey).map_err(|e| e.error)?; - weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2)); + weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2)); } Ok(()) } diff --git a/pallets/subtensor/tests/swap.rs b/pallets/subtensor/tests/swap.rs index 7445470f83..488f2e1d00 100644 --- a/pallets/subtensor/tests/swap.rs +++ b/pallets/subtensor/tests/swap.rs @@ -3,7 +3,7 @@ use codec::Encode; use frame_support::weights::Weight; use frame_support::{assert_err, assert_noop, assert_ok}; -use frame_system::Config; +use frame_system::{Config, RawOrigin}; mod mock; use mock::*; use pallet_subtensor::*; @@ -1675,11 +1675,9 @@ fn test_swap_senate_member() { let mut weight = Weight::zero(); // Setup: Add old_hotkey as a Senate member - assert_ok!(Senate::set_members( - RuntimeOrigin::root(), - vec![old_hotkey], - None, - 0 + assert_ok!(SenateMembers::add_member( + RawOrigin::Root.into(), + old_hotkey )); // Test 1: Successful swap @@ -1710,29 +1708,5 @@ fn test_swap_senate_member() { // Verify weight update (should only have read operations) let expected_weight = ::DbWeight::get().reads(1); assert_eq!(weight, expected_weight); - - // Reset weight for next test - weight = Weight::zero(); - - // Test 3: Setup for swap to an existing member - let another_member = U256::from(4); - assert_ok!(Senate::set_members( - RuntimeOrigin::root(), - vec![new_hotkey, another_member], - None, - 1 - )); - - // Test 4: Setup for swap with maximum members reached - let mut members = vec![new_hotkey, another_member]; - for i in 5..=SenateMaxMembers::get() { - members.push(U256::from(i as u64)); - } - assert_ok!(Senate::set_members( - RuntimeOrigin::root(), - members.clone(), - None, - 2 - )); }); } From 606b771f7ebc1810bdb124c5170c9c5d2a726c89 Mon Sep 17 00:00:00 2001 From: Samuel Dare Date: Fri, 12 Jul 2024 22:02:12 +0400 Subject: [PATCH 4/8] fixes for network resumption --- pallets/admin-utils/tests/mock.rs | 4 ++-- pallets/subtensor/src/lib.rs | 2 +- pallets/subtensor/src/swap.rs | 16 +++++++++++++++- pallets/subtensor/src/utils.rs | 4 ++-- pallets/subtensor/tests/mock.rs | 4 ++-- pallets/subtensor/tests/swap.rs | 8 +++++--- runtime/src/lib.rs | 4 ++-- 7 files changed, 29 insertions(+), 13 deletions(-) diff --git a/pallets/admin-utils/tests/mock.rs b/pallets/admin-utils/tests/mock.rs index 5f71626adf..dbf88bdfa4 100644 --- a/pallets/admin-utils/tests/mock.rs +++ b/pallets/admin-utils/tests/mock.rs @@ -110,7 +110,7 @@ parameter_types! { pub const InitialSubnetLimit: u16 = 10; // Max 10 subnets. pub const InitialNetworkRateLimit: u64 = 0; pub const InitialTargetStakesPerInterval: u16 = 1; - pub const InitialHotkeySwapCost: u64 = 1_000_000_000; + pub const InitialKeySwapCost: u64 = 1_000_000_000; pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn @@ -166,7 +166,7 @@ impl pallet_subtensor::Config for Test { type InitialSubnetLimit = InitialSubnetLimit; type InitialNetworkRateLimit = InitialNetworkRateLimit; type InitialTargetStakesPerInterval = InitialTargetStakesPerInterval; - type HotkeySwapCost = InitialHotkeySwapCost; + type KeySwapCost = InitialKeySwapCost; type AlphaHigh = InitialAlphaHigh; type AlphaLow = InitialAlphaLow; type LiquidAlphaOn = InitialLiquidAlphaOn; diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index c4ac1f3e58..fc802a78a3 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -247,7 +247,7 @@ pub mod pallet { type InitialTargetStakesPerInterval: Get; /// Cost of swapping a hotkey. #[pallet::constant] - type HotkeySwapCost: Get; + type KeySwapCost: Get; /// The upper bound for the alpha parameter. Used for Liquid Alpha. #[pallet::constant] type AlphaHigh: Get; diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index c9e01b008e..6e91f205b2 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -56,7 +56,7 @@ impl Pallet { T::DbWeight::get().reads((TotalNetworks::::get().saturating_add(1u16)) as u64), ); - let swap_cost = Self::get_hotkey_swap_cost(); + let swap_cost = Self::get_key_swap_cost(); log::debug!("Swap cost: {:?}", swap_cost); ensure!( @@ -146,6 +146,20 @@ impl Pallet { Error::::ColdKeyAlreadyAssociated ); + // Calculate and charge the swap fee + let swap_cost = Self::get_key_swap_cost(); + log::debug!("Coldkey swap cost: {:?}", swap_cost); + + ensure!( + Self::can_remove_balance_from_coldkey_account(&old_coldkey, swap_cost), + Error::::NotEnoughBalanceToPaySwapColdKey + ); + let actual_burn_amount = + Self::remove_balance_from_coldkey_account(&old_coldkey, swap_cost)?; + Self::burn_tokens(actual_burn_amount); + + weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); + // Actually do the swap. weight = weight.saturating_add( Self::perform_swap_coldkey(&old_coldkey, new_coldkey) diff --git a/pallets/subtensor/src/utils.rs b/pallets/subtensor/src/utils.rs index 94f18dfbf8..c61133e947 100644 --- a/pallets/subtensor/src/utils.rs +++ b/pallets/subtensor/src/utils.rs @@ -671,8 +671,8 @@ impl Pallet { NominatorMinRequiredStake::::put(min_stake); } - pub fn get_hotkey_swap_cost() -> u64 { - T::HotkeySwapCost::get() + pub fn get_key_swap_cost() -> u64 { + T::KeySwapCost::get() } pub fn get_alpha_values(netuid: u16) -> (u16, u16) { diff --git a/pallets/subtensor/tests/mock.rs b/pallets/subtensor/tests/mock.rs index c3a8640754..46e08e96ad 100644 --- a/pallets/subtensor/tests/mock.rs +++ b/pallets/subtensor/tests/mock.rs @@ -164,7 +164,7 @@ parameter_types! { pub const InitialSubnetLimit: u16 = 10; // Max 10 subnets. pub const InitialNetworkRateLimit: u64 = 0; pub const InitialTargetStakesPerInterval: u16 = 2; - pub const InitialHotkeySwapCost: u64 = 1_000_000_000; + pub const InitialKeySwapCost: u64 = 1_000_000_000; pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn @@ -369,7 +369,7 @@ impl pallet_subtensor::Config for Test { type InitialSubnetLimit = InitialSubnetLimit; type InitialNetworkRateLimit = InitialNetworkRateLimit; type InitialTargetStakesPerInterval = InitialTargetStakesPerInterval; - type HotkeySwapCost = InitialHotkeySwapCost; + type KeySwapCost = InitialKeySwapCost; type AlphaHigh = InitialAlphaHigh; type AlphaLow = InitialAlphaLow; type LiquidAlphaOn = InitialLiquidAlphaOn; diff --git a/pallets/subtensor/tests/swap.rs b/pallets/subtensor/tests/swap.rs index 488f2e1d00..4e2b80a470 100644 --- a/pallets/subtensor/tests/swap.rs +++ b/pallets/subtensor/tests/swap.rs @@ -1059,7 +1059,8 @@ fn test_do_swap_coldkey_success() { let netuid = 1u16; let stake_amount1 = 1000u64; let stake_amount2 = 2000u64; - let free_balance_old = 12345u64 + MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP; + let swap_cost = SubtensorModule::get_key_swap_cost(); + let free_balance_old = 12345u64 + swap_cost; // Setup initial state add_network(netuid, 13, 0); @@ -1158,7 +1159,7 @@ fn test_do_swap_coldkey_success() { // Verify balance transfer assert_eq!( SubtensorModule::get_coldkey_balance(&new_coldkey), - free_balance_old + free_balance_old - swap_cost ); assert_eq!(SubtensorModule::get_coldkey_balance(&old_coldkey), 0); @@ -1332,6 +1333,7 @@ fn test_do_swap_coldkey_with_subnet_ownership() { let hotkey = U256::from(3); let netuid = 1u16; let stake_amount: u64 = 1000u64; + let swap_cost = SubtensorModule::get_key_swap_cost(); // Setup initial state add_network(netuid, 13, 0); @@ -1340,7 +1342,7 @@ fn test_do_swap_coldkey_with_subnet_ownership() { // Set TotalNetworks because swap relies on it pallet_subtensor::TotalNetworks::::set(1); - SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, stake_amount); + SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, stake_amount + swap_cost); SubnetOwner::::insert(netuid, old_coldkey); // Populate OwnedHotkeys map diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 0e171e9ead..45c7cd9da0 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -876,7 +876,7 @@ parameter_types! { pub const SubtensorInitialNetworkLockReductionInterval: u64 = 14 * 7200; pub const SubtensorInitialNetworkRateLimit: u64 = 7200; pub const SubtensorInitialTargetStakesPerInterval: u16 = 1; - pub const SubtensorInitialHotkeySwapCost: u64 = 1_000_000_000; + pub const SubtensorInitialKeySwapCost: u64 = 1_000_000_000; pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn @@ -932,7 +932,7 @@ impl pallet_subtensor::Config for Runtime { type InitialSubnetLimit = SubtensorInitialSubnetLimit; type InitialNetworkRateLimit = SubtensorInitialNetworkRateLimit; type InitialTargetStakesPerInterval = SubtensorInitialTargetStakesPerInterval; - type HotkeySwapCost = SubtensorInitialHotkeySwapCost; + type KeySwapCost = SubtensorInitialKeySwapCost; type AlphaHigh = InitialAlphaHigh; type AlphaLow = InitialAlphaLow; type LiquidAlphaOn = InitialLiquidAlphaOn; From 8ebcf2dd7e1810b49500d70d9e2a0917e62533b0 Mon Sep 17 00:00:00 2001 From: Samuel Dare Date: Fri, 12 Jul 2024 22:08:59 +0400 Subject: [PATCH 5/8] chore: bump spec --- 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 45c7cd9da0..b343ad0f06 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: 194, + spec_version: 162, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From 7b84d3c95f3096659e9d92bcdc006db3d1a7b237 Mon Sep 17 00:00:00 2001 From: Samuel Dare Date: Fri, 12 Jul 2024 23:08:23 +0400 Subject: [PATCH 6/8] fix: add back arbitration check --- pallets/subtensor/src/swap.rs | 6 ++++++ pallets/subtensor/tests/swap.rs | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/pallets/subtensor/src/swap.rs b/pallets/subtensor/src/swap.rs index 6e91f205b2..2e7be51224 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -32,6 +32,11 @@ impl Pallet { ) -> DispatchResultWithPostInfo { let coldkey = ensure_signed(origin)?; + ensure!( + !Self::coldkey_in_arbitration(&old_coldkey), + Error::::ColdkeyIsInArbitration + ); + let mut weight = T::DbWeight::get().reads(2); ensure!(old_hotkey != new_hotkey, Error::::NewHotKeyIsSameWithOld); @@ -80,6 +85,7 @@ impl Pallet { Self::swap_loaded_emission(old_hotkey, new_hotkey, &netuid_is_member, &mut weight); Self::swap_uids(old_hotkey, new_hotkey, &netuid_is_member, &mut weight); Self::swap_prometheus(old_hotkey, new_hotkey, &netuid_is_member, &mut weight); + Self::swap_senate_member(old_hotkey, new_hotkey, &mut weight)?; Self::swap_total_hotkey_coldkey_stakes_this_interval(old_hotkey, new_hotkey, &mut weight); diff --git a/pallets/subtensor/tests/swap.rs b/pallets/subtensor/tests/swap.rs index 4e2b80a470..2355dc960d 100644 --- a/pallets/subtensor/tests/swap.rs +++ b/pallets/subtensor/tests/swap.rs @@ -167,6 +167,22 @@ fn test_do_swap_hotkey_ok_robust() { SubtensorModule::add_balance_to_coldkey_account(coldkey, swap_cost); } + // Add old_hotkeys[0] and old_hotkeys[1] to Senate + assert_ok!(SenateMembers::add_member( + RawOrigin::Root.into(), + old_hotkeys[0] + )); + assert_ok!(SenateMembers::add_member( + RawOrigin::Root.into(), + old_hotkeys[1] + )); + + // Verify initial Senate membership + assert!(Senate::is_member(&old_hotkeys[0])); + assert!(Senate::is_member(&old_hotkeys[1])); + assert!(!Senate::is_member(&new_hotkeys[0])); + assert!(!Senate::is_member(&new_hotkeys[1])); + // Perform the swaps for only two hotkeys assert_ok!(SubtensorModule::do_swap_hotkey( <::RuntimeOrigin>::signed(coldkeys[0]), @@ -268,6 +284,10 @@ fn test_do_swap_hotkey_ok_robust() { assert_eq!(Keys::::get(netuid, uid), new_hotkeys[i]); } } + + // Verify Senate membership swap + assert!(!Senate::is_member(&old_hotkeys[i])); + assert!(Senate::is_member(&new_hotkeys[i])); } else { // Ensure other hotkeys remain unchanged assert_eq!( @@ -278,6 +298,10 @@ fn test_do_swap_hotkey_ok_robust() { SubtensorModule::get_owning_coldkey_for_hotkey(&new_hotkeys[i]), coldkeys[i] ); + + // Verify Senate membership remains unchanged for other hotkeys + assert!(!Senate::is_member(&old_hotkeys[i])); + assert!(!Senate::is_member(&new_hotkeys[i])); } } } From 2455082e007069e6cd3ced93a2bee10774cebc44 Mon Sep 17 00:00:00 2001 From: Samuel Dare Date: Fri, 12 Jul 2024 23:19:53 +0400 Subject: [PATCH 7/8] fix: hotkey --- 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 2e7be51224..ff93a1dbc0 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -33,7 +33,7 @@ impl Pallet { let coldkey = ensure_signed(origin)?; ensure!( - !Self::coldkey_in_arbitration(&old_coldkey), + !Self::coldkey_in_arbitration(&old_hotkey), Error::::ColdkeyIsInArbitration ); From f49bb55de0605ee47f9a1883f098ca00afe62926 Mon Sep 17 00:00:00 2001 From: Samuel Dare Date: Fri, 12 Jul 2024 23:24:21 +0400 Subject: [PATCH 8/8] fix: coldkey arb swap hotkey --- 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 ff93a1dbc0..307aeb6411 100644 --- a/pallets/subtensor/src/swap.rs +++ b/pallets/subtensor/src/swap.rs @@ -33,7 +33,7 @@ impl Pallet { let coldkey = ensure_signed(origin)?; ensure!( - !Self::coldkey_in_arbitration(&old_hotkey), + !Self::coldkey_in_arbitration(&coldkey), Error::::ColdkeyIsInArbitration );