Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup swap and unstake all code around StakingHotkeys #617

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions pallets/subtensor/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,34 +890,6 @@ impl<T: Config> Pallet<T> {
// Ensure the new coldkey is different from the current one
ensure!(current_coldkey != new_coldkey, Error::<T>::SameColdkey);

// Get all the hotkeys associated with this coldkey
let hotkeys: Vec<T::AccountId> = OwnedHotkeys::<T>::get(&current_coldkey);

// iterate over all hotkeys.
for next_hotkey in hotkeys {
ensure!(
Self::hotkey_account_exists(&next_hotkey),
Error::<T>::HotKeyAccountNotExists
);
ensure!(
Self::coldkey_owns_hotkey(&current_coldkey, &next_hotkey),
Error::<T>::NonAssociatedColdKey
);

// Get the current stake
let current_stake: u64 =
Self::get_stake_for_coldkey_and_hotkey(&current_coldkey, &next_hotkey);

// Unstake all balance if there's any stake
if current_stake > 0 {
Self::do_remove_stake(
RawOrigin::Signed(current_coldkey.clone()).into(),
next_hotkey.clone(),
current_stake,
)?;
}
}

// Unstake all delegate stake make by this coldkey to non-owned hotkeys
let staking_hotkeys = StakingHotkeys::<T>::get(&current_coldkey);

Expand Down
34 changes: 29 additions & 5 deletions pallets/subtensor/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl<T: Config> Pallet<T> {
);
Self::swap_subnet_owner_for_coldkey(old_coldkey, new_coldkey, &mut weight);
Self::swap_owned_for_coldkey(old_coldkey, new_coldkey, &mut weight);
Self::swap_staking_hotkeys_for_coldkey(old_coldkey, new_coldkey, &mut weight);

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

// Update StakingHotkeys map
let staking_hotkeys = StakingHotkeys::<T>::get(old_coldkey);
StakingHotkeys::<T>::insert(new_coldkey.clone(), staking_hotkeys);
/// Swaps all staking hotkeys associated with a coldkey from the old coldkey to the new coldkey.
///
/// # Arguments
///
/// * `old_coldkey` - The AccountId of the old coldkey.
/// * `new_coldkey` - The AccountId of the new coldkey.
/// * `weight` - Mutable reference to the weight of the transaction.
///
/// # Effects
///
/// * Removes all stakes associated with the old coldkey.
/// * Inserts all stakes for the new coldkey.
/// * Updates the transaction weight.
pub fn swap_staking_hotkeys_for_coldkey(
old_coldkey: &T::AccountId,
new_coldkey: &T::AccountId,
weight: &mut Weight,
) {
// Find all hotkeys for this coldkey
let hotkeys = OwnedHotkeys::<T>::get(old_coldkey);

weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 3));
}
// Update StakingHotkeys map
OwnedHotkeys::<T>::remove(old_coldkey);
StakingHotkeys::<T>::insert(new_coldkey.clone(), hotkeys);

weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
}

/// Swaps the owner of all hotkeys from the old coldkey to the new coldkey.
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/tests/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ fn test_swap_stake_for_coldkey() {
assert_eq!(TotalIssuance::<Test>::get(), stake_amount1 + stake_amount2);

// Verify weight update
let expected_weight = <Test as frame_system::Config>::DbWeight::get().reads_writes(5, 6);
let expected_weight = <Test as frame_system::Config>::DbWeight::get().reads_writes(3, 4);
assert_eq!(weight, expected_weight);
});
}
Expand Down
Loading