Skip to content

Commit 636ae16

Browse files
committed
Cleanup swap and unstake all code around StakingHotkeys
1 parent 5b5ac3a commit 636ae16

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

pallets/subtensor/src/staking.rs

-28
Original file line numberDiff line numberDiff line change
@@ -890,34 +890,6 @@ impl<T: Config> Pallet<T> {
890890
// Ensure the new coldkey is different from the current one
891891
ensure!(current_coldkey != new_coldkey, Error::<T>::SameColdkey);
892892

893-
// Get all the hotkeys associated with this coldkey
894-
let hotkeys: Vec<T::AccountId> = OwnedHotkeys::<T>::get(&current_coldkey);
895-
896-
// iterate over all hotkeys.
897-
for next_hotkey in hotkeys {
898-
ensure!(
899-
Self::hotkey_account_exists(&next_hotkey),
900-
Error::<T>::HotKeyAccountNotExists
901-
);
902-
ensure!(
903-
Self::coldkey_owns_hotkey(&current_coldkey, &next_hotkey),
904-
Error::<T>::NonAssociatedColdKey
905-
);
906-
907-
// Get the current stake
908-
let current_stake: u64 =
909-
Self::get_stake_for_coldkey_and_hotkey(&current_coldkey, &next_hotkey);
910-
911-
// Unstake all balance if there's any stake
912-
if current_stake > 0 {
913-
Self::do_remove_stake(
914-
RawOrigin::Signed(current_coldkey.clone()).into(),
915-
next_hotkey.clone(),
916-
current_stake,
917-
)?;
918-
}
919-
}
920-
921893
// Unstake all delegate stake make by this coldkey to non-owned hotkeys
922894
let staking_hotkeys = StakingHotkeys::<T>::get(&current_coldkey);
923895

pallets/subtensor/src/swap.rs

+29-5
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ impl<T: Config> Pallet<T> {
139139
);
140140
Self::swap_subnet_owner_for_coldkey(old_coldkey, new_coldkey, &mut weight);
141141
Self::swap_owned_for_coldkey(old_coldkey, new_coldkey, &mut weight);
142+
Self::swap_staking_hotkeys_for_coldkey(old_coldkey, new_coldkey, &mut weight);
142143

143144
// Transfer any remaining balance from old_coldkey to new_coldkey
144145
let remaining_balance = Self::get_coldkey_balance(old_coldkey);
@@ -531,13 +532,36 @@ impl<T: Config> Pallet<T> {
531532
let stake = Stake::<T>::get(&hotkey, old_coldkey);
532533
Stake::<T>::remove(&hotkey, old_coldkey);
533534
Stake::<T>::insert(&hotkey, new_coldkey, stake);
535+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
536+
}
537+
}
534538

535-
// Update StakingHotkeys map
536-
let staking_hotkeys = StakingHotkeys::<T>::get(old_coldkey);
537-
StakingHotkeys::<T>::insert(new_coldkey.clone(), staking_hotkeys);
539+
/// Swaps all staking hotkeys associated with a coldkey from the old coldkey to the new coldkey.
540+
///
541+
/// # Arguments
542+
///
543+
/// * `old_coldkey` - The AccountId of the old coldkey.
544+
/// * `new_coldkey` - The AccountId of the new coldkey.
545+
/// * `weight` - Mutable reference to the weight of the transaction.
546+
///
547+
/// # Effects
548+
///
549+
/// * Removes all stakes associated with the old coldkey.
550+
/// * Inserts all stakes for the new coldkey.
551+
/// * Updates the transaction weight.
552+
pub fn swap_staking_hotkeys_for_coldkey(
553+
old_coldkey: &T::AccountId,
554+
new_coldkey: &T::AccountId,
555+
weight: &mut Weight,
556+
) {
557+
// Find all hotkeys for this coldkey
558+
let hotkeys = OwnedHotkeys::<T>::get(old_coldkey);
538559

539-
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 3));
540-
}
560+
// Update StakingHotkeys map
561+
OwnedHotkeys::<T>::remove(old_coldkey);
562+
StakingHotkeys::<T>::insert(new_coldkey.clone(), hotkeys);
563+
564+
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
541565
}
542566

543567
/// Swaps the owner of all hotkeys from the old coldkey to the new coldkey.

pallets/subtensor/tests/swap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ fn test_swap_stake_for_coldkey() {
12131213
assert_eq!(TotalIssuance::<Test>::get(), stake_amount1 + stake_amount2);
12141214

12151215
// Verify weight update
1216-
let expected_weight = <Test as frame_system::Config>::DbWeight::get().reads_writes(5, 6);
1216+
let expected_weight = <Test as frame_system::Config>::DbWeight::get().reads_writes(3, 4);
12171217
assert_eq!(weight, expected_weight);
12181218
});
12191219
}

0 commit comments

Comments
 (0)