Skip to content

Commit b34c052

Browse files
committedJul 22, 2024
fix TI handling in empty_stake_on_coldkey_hotkey_account
1 parent 354b828 commit b34c052

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed
 

‎pallets/subtensor/src/staking.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use frame_support::{
66
fungible::{Balanced as _, Inspect as _, Mutate as _},
77
Fortitude, Precision, Preservation,
88
},
9-
Imbalance,
9+
Defensive, Imbalance,
1010
},
1111
};
1212

@@ -704,11 +704,14 @@ impl<T: Config> Pallet<T> {
704704
// TODO: Tech debt: Remove StakingHotkeys entry if stake goes to 0
705705
}
706706

707-
/// Empties the stake associated with a given coldkey-hotkey account pairing.
707+
/// Empties the stake associated with a given coldkey-hotkey account pairing, and moves it to
708+
/// the coldkey free balance.
709+
///
708710
/// This function retrieves the current stake for the specified coldkey-hotkey pairing,
709711
/// then subtracts this stake amount from both the TotalColdkeyStake and TotalHotkeyStake.
710-
/// It also removes the stake entry for the hotkey-coldkey pairing and adjusts the TotalStake
711-
/// and TotalIssuance by subtracting the removed stake amount.
712+
///
713+
/// It also removes the stake entry for the hotkey-coldkey pairing and increases the coldkey
714+
/// free balance by the amount of stake removed.
712715
///
713716
/// Returns the amount of stake that was removed.
714717
///
@@ -725,13 +728,15 @@ impl<T: Config> Pallet<T> {
725728
TotalHotkeyStake::<T>::mutate(hotkey, |stake| *stake = stake.saturating_sub(current_stake));
726729
Stake::<T>::remove(hotkey, coldkey);
727730
TotalStake::<T>::mutate(|stake| *stake = stake.saturating_sub(current_stake));
728-
TotalIssuance::<T>::mutate(|issuance| *issuance = issuance.saturating_sub(current_stake));
729731

730732
// Update StakingHotkeys map
731733
let mut staking_hotkeys = StakingHotkeys::<T>::get(coldkey);
732734
staking_hotkeys.retain(|h| h != hotkey);
733735
StakingHotkeys::<T>::insert(coldkey, staking_hotkeys);
734736

737+
// Add the amount to the coldkey free balance
738+
Self::add_balance_to_coldkey_account(coldkey, current_stake);
739+
735740
current_stake
736741
}
737742

@@ -745,11 +750,9 @@ impl<T: Config> Pallet<T> {
745750
if !Self::coldkey_owns_hotkey(coldkey, hotkey) {
746751
// If the stake is below the minimum required, it's considered a small nomination and needs to be cleared.
747752
if stake < Self::get_nominator_min_required_stake() {
748-
// Remove the stake from the nominator account. (this is a more forceful unstake operation which )
749-
// Actually deletes the staking account.
750-
let cleared_stake = Self::empty_stake_on_coldkey_hotkey_account(coldkey, hotkey);
751-
// Add the stake to the coldkey account.
752-
Self::add_balance_to_coldkey_account(coldkey, cleared_stake);
753+
// Remove the stake from the nominator account, and moves it to the free balance.
754+
// This is a more forceful unstake operation which actually deletes the staking account.
755+
Self::empty_stake_on_coldkey_hotkey_account(coldkey, hotkey);
753756
}
754757
}
755758
}
@@ -769,8 +772,8 @@ impl<T: Config> Pallet<T> {
769772
coldkey: &T::AccountId,
770773
amount: <<T as Config>::Currency as fungible::Inspect<<T as system::Config>::AccountId>>::Balance,
771774
) {
772-
// infallible
773-
let _ = T::Currency::deposit(coldkey, amount, Precision::BestEffort);
775+
let _ = T::Currency::deposit(coldkey, amount, Precision::BestEffort)
776+
.defensive_proof("Account balance should never exceed max balance");
774777
}
775778

776779
pub fn set_balance_on_coldkey_account(

0 commit comments

Comments
 (0)