Skip to content
11 changes: 7 additions & 4 deletions pallets/subtensor/src/swap/swap_hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,20 @@ impl<T: Config> Pallet<T> {
Error::<T>::NonAssociatedColdKey
);

// 3. If the new hotkey already exists globally, ensure the coldkey owns it
// 3. Initialize the weight for this operation. The coldkey/old_hotkey
// ownership check above reads Owner twice.
let mut weight = T::DbWeight::get().reads(2);

// 4. If the new hotkey already exists globally, ensure the coldkey owns it
weight.saturating_accrue(T::DbWeight::get().reads(1));
if Self::hotkey_account_exists(new_hotkey) {
weight.saturating_accrue(T::DbWeight::get().reads(2));
ensure!(
Self::coldkey_owns_hotkey(&coldkey, new_hotkey),
Error::<T>::NonAssociatedColdKey
);
}

// 4. Initialize the weight for this operation
let mut weight = T::DbWeight::get().reads(2);

// 5. Ensure the new hotkey is different from the old one
ensure!(old_hotkey != new_hotkey, Error::<T>::NewHotKeyIsSameWithOld);

Expand Down
9 changes: 5 additions & 4 deletions pallets/subtensor/src/tests/swap_hotkey_with_subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,22 +900,23 @@ fn test_swap_owner_old_hotkey_not_exist() {
});
}

// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --test swap_hotkey_with_subnet -- test_swap_owner_new_hotkey_already_exists --exact --nocapture
// SKIP_WASM_BUILD=1 cargo test --package pallet-subtensor --lib -- tests::swap_hotkey_with_subnet::test_swap_owner_new_hotkey_already_exists --exact --nocapture
#[test]
fn test_swap_owner_new_hotkey_already_exists() {
new_test_ext(1).execute_with(|| {
let old_hotkey = U256::from(1);
let new_hotkey = U256::from(2);
let coldkey = U256::from(3);
let another_coldkey = U256::from(4);

let netuid = add_dynamic_network(&new_hotkey, &coldkey);
let netuid = add_dynamic_network(&old_hotkey, &coldkey);
add_balance_to_coldkey_account(&coldkey, 1_000_000_000_000_u64.into());

// old_hotkey is owned by coldkey; new_hotkey was already registered on `netuid`
// by add_dynamic_network (the condition under test). Do NOT reassign new_hotkey to
// a foreign coldkey — the new_hotkey-ownership check (NonAssociatedColdKey) would
// then fire before the already-registered-in-subnet check this test targets.
Owner::<Test>::insert(old_hotkey, coldkey);
Owner::<Test>::insert(new_hotkey, another_coldkey);

// Perform the swap
System::set_block_number(System::block_number() + HotkeySwapOnSubnetInterval::get());
Expand All @@ -927,7 +928,7 @@ fn test_swap_owner_new_hotkey_already_exists() {
Some(netuid),
false
),
Error::<Test>::HotKeyAlreadyRegisteredInSubNet
Error::<Test>::NonAssociatedColdKey
);

// Verify the swap
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,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: 419,
spec_version: 420,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading