Skip to content

Commit 884a179

Browse files
Merge pull request #631 from opentensor/feat/remove_schedule_coldkey_swap
Resuming the network
2 parents bcb696b + f49bb55 commit 884a179

File tree

8 files changed

+135
-74
lines changed

8 files changed

+135
-74
lines changed

pallets/admin-utils/tests/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ parameter_types! {
110110
pub const InitialSubnetLimit: u16 = 10; // Max 10 subnets.
111111
pub const InitialNetworkRateLimit: u64 = 0;
112112
pub const InitialTargetStakesPerInterval: u16 = 1;
113-
pub const InitialHotkeySwapCost: u64 = 1_000_000_000;
113+
pub const InitialKeySwapCost: u64 = 1_000_000_000;
114114
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
115115
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
116116
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
@@ -166,7 +166,7 @@ impl pallet_subtensor::Config for Test {
166166
type InitialSubnetLimit = InitialSubnetLimit;
167167
type InitialNetworkRateLimit = InitialNetworkRateLimit;
168168
type InitialTargetStakesPerInterval = InitialTargetStakesPerInterval;
169-
type HotkeySwapCost = InitialHotkeySwapCost;
169+
type KeySwapCost = InitialKeySwapCost;
170170
type AlphaHigh = InitialAlphaHigh;
171171
type AlphaLow = InitialAlphaLow;
172172
type LiquidAlphaOn = InitialLiquidAlphaOn;

pallets/subtensor/src/benchmarks.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -429,30 +429,4 @@ reveal_weights {
429429

430430
}: reveal_weights(RawOrigin::Signed(hotkey.clone()), netuid, uids, weight_values, salt, version_key)
431431

432-
schedule_coldkey_swap {
433-
let seed: u32 = 1;
434-
let old_coldkey: T::AccountId = account("OldColdkey", 0, seed);
435-
let new_coldkey: T::AccountId = account("NewColdkey", 0, seed + 1);
436-
let hotkey: T::AccountId = account("Hotkey", 0, seed);
437-
438-
let netuid = 1u16;
439-
let tempo = 1u16;
440-
let block_number: u64 = Subtensor::<T>::get_current_block_as_u64();
441-
let nonce = 0;
442-
443-
// Initialize the network
444-
Subtensor::<T>::init_new_network(netuid, tempo);
445-
Subtensor::<T>::set_network_registration_allowed(netuid, true);
446-
447-
// Add balance to the old coldkey account
448-
let amount_to_be_staked: u64 = 1000000u32.into();
449-
Subtensor::<T>::add_balance_to_coldkey_account(&old_coldkey.clone(), amount_to_be_staked+1000000000);
450-
// Burned register the hotkey with the old coldkey
451-
assert_ok!(Subtensor::<T>::burned_register(
452-
RawOrigin::Signed(old_coldkey.clone()).into(),
453-
netuid,
454-
hotkey.clone()
455-
));
456-
457-
}: schedule_coldkey_swap(RawOrigin::Signed(old_coldkey.clone()), new_coldkey.clone(), vec![], block_number, nonce)
458432
}

pallets/subtensor/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub mod pallet {
247247
type InitialTargetStakesPerInterval: Get<u64>;
248248
/// Cost of swapping a hotkey.
249249
#[pallet::constant]
250-
type HotkeySwapCost: Get<u64>;
250+
type KeySwapCost: Get<u64>;
251251
/// The upper bound for the alpha parameter. Used for Liquid Alpha.
252252
#[pallet::constant]
253253
type AlphaHigh: Get<u16>;
@@ -2098,7 +2098,6 @@ pub mod pallet {
20982098
) -> DispatchResultWithPostInfo {
20992099
Self::do_swap_coldkey(origin, &new_coldkey)
21002100
}
2101-
21022101
/// Unstakes all tokens associated with a hotkey and transfers them to a new coldkey.
21032102
///
21042103
/// # Arguments
@@ -2114,6 +2113,7 @@ pub mod pallet {
21142113
/// # Weight
21152114
///
21162115
/// Weight is calculated based on the number of database reads and writes.
2116+
#[cfg(test)]
21172117
#[pallet::call_index(72)]
21182118
#[pallet::weight((Weight::from_parts(21_000_000, 0)
21192119
.saturating_add(T::DbWeight::get().reads(3))

pallets/subtensor/src/swap.rs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ impl<T: Config> Pallet<T> {
3131
new_hotkey: &T::AccountId,
3232
) -> DispatchResultWithPostInfo {
3333
let coldkey = ensure_signed(origin)?;
34+
3435
ensure!(
3536
!Self::coldkey_in_arbitration(&coldkey),
3637
Error::<T>::ColdkeyIsInArbitration
@@ -60,6 +61,16 @@ impl<T: Config> Pallet<T> {
6061
T::DbWeight::get().reads((TotalNetworks::<T>::get().saturating_add(1u16)) as u64),
6162
);
6263

64+
let swap_cost = Self::get_key_swap_cost();
65+
log::debug!("Swap cost: {:?}", swap_cost);
66+
67+
ensure!(
68+
Self::can_remove_balance_from_coldkey_account(&coldkey, swap_cost),
69+
Error::<T>::NotEnoughBalanceToPaySwapHotKey
70+
);
71+
let actual_burn_amount = Self::remove_balance_from_coldkey_account(&coldkey, swap_cost)?;
72+
Self::burn_tokens(actual_burn_amount);
73+
6374
Self::swap_owner(old_hotkey, new_hotkey, &coldkey, &mut weight);
6475
Self::swap_total_hotkey_stake(old_hotkey, new_hotkey, &mut weight);
6576
Self::swap_delegates(old_hotkey, new_hotkey, &mut weight);
@@ -74,6 +85,7 @@ impl<T: Config> Pallet<T> {
7485
Self::swap_loaded_emission(old_hotkey, new_hotkey, &netuid_is_member, &mut weight);
7586
Self::swap_uids(old_hotkey, new_hotkey, &netuid_is_member, &mut weight);
7687
Self::swap_prometheus(old_hotkey, new_hotkey, &netuid_is_member, &mut weight);
88+
Self::swap_senate_member(old_hotkey, new_hotkey, &mut weight)?;
7789

7890
Self::swap_total_hotkey_coldkey_stakes_this_interval(old_hotkey, new_hotkey, &mut weight);
7991

@@ -140,6 +152,20 @@ impl<T: Config> Pallet<T> {
140152
Error::<T>::ColdKeyAlreadyAssociated
141153
);
142154

155+
// Calculate and charge the swap fee
156+
let swap_cost = Self::get_key_swap_cost();
157+
log::debug!("Coldkey swap cost: {:?}", swap_cost);
158+
159+
ensure!(
160+
Self::can_remove_balance_from_coldkey_account(&old_coldkey, swap_cost),
161+
Error::<T>::NotEnoughBalanceToPaySwapColdKey
162+
);
163+
let actual_burn_amount =
164+
Self::remove_balance_from_coldkey_account(&old_coldkey, swap_cost)?;
165+
Self::burn_tokens(actual_burn_amount);
166+
167+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
168+
143169
// Actually do the swap.
144170
weight = weight.saturating_add(
145171
Self::perform_swap_coldkey(&old_coldkey, new_coldkey)
@@ -189,15 +215,15 @@ impl<T: Config> Pallet<T> {
189215
///
190216
/// This function calculates the remaining arbitration period by subtracting the current block number
191217
/// from the arbitration block number of the coldkey.
192-
// pub fn get_remaining_arbitration_period(coldkey: &T::AccountId) -> u64 {
193-
// let current_block: u64 = Self::get_current_block_as_u64();
194-
// let arbitration_block: u64 = ColdkeyArbitrationBlock::<T>::get(coldkey);
195-
// if arbitration_block > current_block {
196-
// arbitration_block.saturating_sub(current_block)
197-
// } else {
198-
// 0
199-
// }
200-
// }
218+
pub fn get_remaining_arbitration_period(coldkey: &T::AccountId) -> u64 {
219+
let current_block: u64 = Self::get_current_block_as_u64();
220+
let arbitration_block: u64 = ColdkeyArbitrationBlock::<T>::get(coldkey);
221+
if arbitration_block > current_block {
222+
arbitration_block.saturating_sub(current_block)
223+
} else {
224+
0
225+
}
226+
}
201227

202228
pub fn meets_min_allowed_coldkey_balance(coldkey: &T::AccountId) -> bool {
203229
let all_staked_keys: Vec<T::AccountId> = StakingHotkeys::<T>::get(coldkey);
@@ -948,4 +974,17 @@ impl<T: Config> Pallet<T> {
948974
}
949975
weight.saturating_accrue(T::DbWeight::get().reads(TotalNetworks::<T>::get() as u64));
950976
}
977+
978+
pub fn swap_senate_member(
979+
old_hotkey: &T::AccountId,
980+
new_hotkey: &T::AccountId,
981+
weight: &mut Weight,
982+
) -> DispatchResult {
983+
weight.saturating_accrue(T::DbWeight::get().reads(1));
984+
if T::SenateMembers::is_member(old_hotkey) {
985+
T::SenateMembers::swap_member(old_hotkey, new_hotkey).map_err(|e| e.error)?;
986+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
987+
}
988+
Ok(())
989+
}
951990
}

pallets/subtensor/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,8 @@ impl<T: Config> Pallet<T> {
671671
NominatorMinRequiredStake::<T>::put(min_stake);
672672
}
673673

674-
pub fn get_hotkey_swap_cost() -> u64 {
675-
T::HotkeySwapCost::get()
674+
pub fn get_key_swap_cost() -> u64 {
675+
T::KeySwapCost::get()
676676
}
677677

678678
pub fn get_alpha_values(netuid: u16) -> (u16, u16) {

pallets/subtensor/tests/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ parameter_types! {
164164
pub const InitialSubnetLimit: u16 = 10; // Max 10 subnets.
165165
pub const InitialNetworkRateLimit: u64 = 0;
166166
pub const InitialTargetStakesPerInterval: u16 = 2;
167-
pub const InitialHotkeySwapCost: u64 = 1_000_000_000;
167+
pub const InitialKeySwapCost: u64 = 1_000_000_000;
168168
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
169169
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
170170
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
@@ -375,7 +375,7 @@ impl pallet_subtensor::Config for Test {
375375
type InitialSubnetLimit = InitialSubnetLimit;
376376
type InitialNetworkRateLimit = InitialNetworkRateLimit;
377377
type InitialTargetStakesPerInterval = InitialTargetStakesPerInterval;
378-
type HotkeySwapCost = InitialHotkeySwapCost;
378+
type KeySwapCost = InitialKeySwapCost;
379379
type AlphaHigh = InitialAlphaHigh;
380380
type AlphaLow = InitialAlphaLow;
381381
type LiquidAlphaOn = InitialLiquidAlphaOn;

pallets/subtensor/tests/swap.rs

Lines changed: 75 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use codec::Encode;
44
use frame_support::weights::Weight;
5-
use frame_support::{assert_err, assert_ok};
6-
use frame_system::Config;
5+
use frame_support::{assert_err, assert_noop, assert_ok};
6+
use frame_system::{Config, RawOrigin};
77
mod mock;
88
use mock::*;
99
use pallet_subtensor::*;
@@ -167,6 +167,22 @@ fn test_do_swap_hotkey_ok_robust() {
167167
SubtensorModule::add_balance_to_coldkey_account(coldkey, swap_cost);
168168
}
169169

170+
// Add old_hotkeys[0] and old_hotkeys[1] to Senate
171+
assert_ok!(SenateMembers::add_member(
172+
RawOrigin::Root.into(),
173+
old_hotkeys[0]
174+
));
175+
assert_ok!(SenateMembers::add_member(
176+
RawOrigin::Root.into(),
177+
old_hotkeys[1]
178+
));
179+
180+
// Verify initial Senate membership
181+
assert!(Senate::is_member(&old_hotkeys[0]));
182+
assert!(Senate::is_member(&old_hotkeys[1]));
183+
assert!(!Senate::is_member(&new_hotkeys[0]));
184+
assert!(!Senate::is_member(&new_hotkeys[1]));
185+
170186
// Perform the swaps for only two hotkeys
171187
assert_ok!(SubtensorModule::do_swap_hotkey(
172188
<<Test as Config>::RuntimeOrigin>::signed(coldkeys[0]),
@@ -268,6 +284,10 @@ fn test_do_swap_hotkey_ok_robust() {
268284
assert_eq!(Keys::<Test>::get(netuid, uid), new_hotkeys[i]);
269285
}
270286
}
287+
288+
// Verify Senate membership swap
289+
assert!(!Senate::is_member(&old_hotkeys[i]));
290+
assert!(Senate::is_member(&new_hotkeys[i]));
271291
} else {
272292
// Ensure other hotkeys remain unchanged
273293
assert_eq!(
@@ -278,6 +298,10 @@ fn test_do_swap_hotkey_ok_robust() {
278298
SubtensorModule::get_owning_coldkey_for_hotkey(&new_hotkeys[i]),
279299
coldkeys[i]
280300
);
301+
302+
// Verify Senate membership remains unchanged for other hotkeys
303+
assert!(!Senate::is_member(&old_hotkeys[i]));
304+
assert!(!Senate::is_member(&new_hotkeys[i]));
281305
}
282306
}
283307
}
@@ -1059,7 +1083,8 @@ fn test_do_swap_coldkey_success() {
10591083
let netuid = 1u16;
10601084
let stake_amount1 = 1000u64;
10611085
let stake_amount2 = 2000u64;
1062-
let free_balance_old = 12345u64 + MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP;
1086+
let swap_cost = SubtensorModule::get_key_swap_cost();
1087+
let free_balance_old = 12345u64 + swap_cost;
10631088

10641089
// Setup initial state
10651090
add_network(netuid, 13, 0);
@@ -1158,7 +1183,7 @@ fn test_do_swap_coldkey_success() {
11581183
// Verify balance transfer
11591184
assert_eq!(
11601185
SubtensorModule::get_coldkey_balance(&new_coldkey),
1161-
free_balance_old
1186+
free_balance_old - swap_cost
11621187
);
11631188
assert_eq!(SubtensorModule::get_coldkey_balance(&old_coldkey), 0);
11641189

@@ -1332,6 +1357,7 @@ fn test_do_swap_coldkey_with_subnet_ownership() {
13321357
let hotkey = U256::from(3);
13331358
let netuid = 1u16;
13341359
let stake_amount: u64 = 1000u64;
1360+
let swap_cost = SubtensorModule::get_key_swap_cost();
13351361

13361362
// Setup initial state
13371363
add_network(netuid, 13, 0);
@@ -1340,7 +1366,7 @@ fn test_do_swap_coldkey_with_subnet_ownership() {
13401366
// Set TotalNetworks because swap relies on it
13411367
pallet_subtensor::TotalNetworks::<Test>::set(1);
13421368

1343-
SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, stake_amount);
1369+
SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, stake_amount + swap_cost);
13441370
SubnetOwner::<Test>::insert(netuid, old_coldkey);
13451371

13461372
// Populate OwnedHotkeys map
@@ -1666,24 +1692,47 @@ fn test_coldkey_swap_total() {
16661692
});
16671693
}
16681694

1669-
// #[test]
1670-
// fn test_coldkey_arbitrated_sw() {
1671-
// new_test_ext(1).execute_with(|| {
1672-
// let coldkey = U256::from(1);
1673-
// let hotkey = U256::from(2);
1674-
// let netuid = 1u16;
1675-
1676-
// // Setup initial state
1677-
// add_network(netuid, 13, 0);
1678-
// register_ok_neuron(netuid, hotkey, coldkey, 0);
1679-
1680-
// // Check if coldkey has associated hotkeys
1681-
// assert!(SubtensorModule::coldkey_has_associated_hotkeys(&coldkey));
1682-
1683-
// // Check for a coldkey without associated hotkeys
1684-
// let unassociated_coldkey = U256::from(3);
1685-
// assert!(!SubtensorModule::coldkey_has_associated_hotkeys(
1686-
// &unassociated_coldkey
1687-
// ));
1688-
// });
1689-
// }
1695+
#[test]
1696+
fn test_swap_senate_member() {
1697+
new_test_ext(1).execute_with(|| {
1698+
let old_hotkey = U256::from(1);
1699+
let new_hotkey = U256::from(2);
1700+
let non_member_hotkey = U256::from(3);
1701+
let mut weight = Weight::zero();
1702+
1703+
// Setup: Add old_hotkey as a Senate member
1704+
assert_ok!(SenateMembers::add_member(
1705+
RawOrigin::Root.into(),
1706+
old_hotkey
1707+
));
1708+
1709+
// Test 1: Successful swap
1710+
assert_ok!(SubtensorModule::swap_senate_member(
1711+
&old_hotkey,
1712+
&new_hotkey,
1713+
&mut weight
1714+
));
1715+
assert!(Senate::is_member(&new_hotkey));
1716+
assert!(!Senate::is_member(&old_hotkey));
1717+
1718+
// Verify weight update
1719+
let expected_weight = <Test as frame_system::Config>::DbWeight::get().reads_writes(2, 2);
1720+
assert_eq!(weight, expected_weight);
1721+
1722+
// Reset weight for next test
1723+
weight = Weight::zero();
1724+
1725+
// Test 2: Swap with non-member (should not change anything)
1726+
assert_ok!(SubtensorModule::swap_senate_member(
1727+
&non_member_hotkey,
1728+
&new_hotkey,
1729+
&mut weight
1730+
));
1731+
assert!(Senate::is_member(&new_hotkey));
1732+
assert!(!Senate::is_member(&non_member_hotkey));
1733+
1734+
// Verify weight update (should only have read operations)
1735+
let expected_weight = <Test as frame_system::Config>::DbWeight::get().reads(1);
1736+
assert_eq!(weight, expected_weight);
1737+
});
1738+
}

runtime/src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
139139
// `spec_version`, and `authoring_version` are the same between Wasm and native.
140140
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
141141
// the compatible custom types.
142-
spec_version: 194,
142+
spec_version: 162,
143143
impl_version: 1,
144144
apis: RUNTIME_API_VERSIONS,
145145
transaction_version: 1,
@@ -312,8 +312,7 @@ impl Contains<RuntimeCall> for SafeModeWhitelistedCalls {
312312
| RuntimeCall::SafeMode(_)
313313
| RuntimeCall::Timestamp(_)
314314
| RuntimeCall::SubtensorModule(
315-
pallet_subtensor::Call::schedule_coldkey_swap { .. }
316-
| pallet_subtensor::Call::set_weights { .. }
315+
pallet_subtensor::Call::set_weights { .. }
317316
| pallet_subtensor::Call::set_root_weights { .. }
318317
| pallet_subtensor::Call::serve_axon { .. }
319318
)
@@ -877,7 +876,7 @@ parameter_types! {
877876
pub const SubtensorInitialNetworkLockReductionInterval: u64 = 14 * 7200;
878877
pub const SubtensorInitialNetworkRateLimit: u64 = 7200;
879878
pub const SubtensorInitialTargetStakesPerInterval: u16 = 1;
880-
pub const SubtensorInitialHotkeySwapCost: u64 = 1_000_000_000;
879+
pub const SubtensorInitialKeySwapCost: u64 = 1_000_000_000;
881880
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
882881
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
883882
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
@@ -933,7 +932,7 @@ impl pallet_subtensor::Config for Runtime {
933932
type InitialSubnetLimit = SubtensorInitialSubnetLimit;
934933
type InitialNetworkRateLimit = SubtensorInitialNetworkRateLimit;
935934
type InitialTargetStakesPerInterval = SubtensorInitialTargetStakesPerInterval;
936-
type HotkeySwapCost = SubtensorInitialHotkeySwapCost;
935+
type KeySwapCost = SubtensorInitialKeySwapCost;
937936
type AlphaHigh = InitialAlphaHigh;
938937
type AlphaLow = InitialAlphaLow;
939938
type LiquidAlphaOn = InitialLiquidAlphaOn;

0 commit comments

Comments
 (0)