Skip to content

Commit a308cd4

Browse files
camfairchildkeithtensor
authored andcommitted
Hotfix/clear stake delta on drain (#934)
* add comment also * chore: fmt * add back delta handling in helpers * track removals in stake delta * handle stake delta in ck swap func * add tests back * add tests for staking back * add back test for ck swap
1 parent c2aba47 commit a308cd4

File tree

7 files changed

+1470
-8
lines changed

7 files changed

+1470
-8
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ impl<T: Config> Pallet<T> {
303303
// --- 8 Iterate over each nominator.
304304
if total_viable_nominator_stake != 0 {
305305
for (nominator, nominator_stake) in Stake::<T>::iter_prefix(hotkey) {
306-
// --- 9 Check if the stake was manually increased by the user since the last emission drain for this hotkey.
307-
// If it was, skip this nominator as they will not receive their proportion of the emission.
306+
// --- 9 Skip emission for any stake the was added by the nominator since the last emission drain.
307+
// This means the nominator will get emission on existing stake, but not on new stake, until the next emission drain.
308308
let viable_nominator_stake =
309309
nominator_stake.saturating_sub(Self::get_nonviable_stake(hotkey, &nominator));
310310

@@ -331,7 +331,10 @@ impl<T: Config> Pallet<T> {
331331
let hotkey_new_tao: u64 = hotkey_take.saturating_add(remainder);
332332
Self::increase_stake_on_hotkey_account(hotkey, hotkey_new_tao);
333333

334-
// --- 14 Record new tao creation event and return the amount created.
334+
// --- 14 Reset the stake delta for the hotkey.
335+
let _ = StakeDeltaSinceLastEmissionDrain::<T>::clear_prefix(hotkey, u32::MAX, None);
336+
337+
// --- 15 Record new tao creation event and return the amount created.
335338
total_new_tao = total_new_tao.saturating_add(hotkey_new_tao);
336339
total_new_tao
337340
}

pallets/subtensor/src/staking/helpers.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ impl<T: Config> Pallet<T> {
297297
staking_hotkeys.retain(|h| h != hotkey);
298298
StakingHotkeys::<T>::insert(coldkey, staking_hotkeys);
299299

300+
// Update stake delta
301+
StakeDeltaSinceLastEmissionDrain::<T>::remove(hotkey, coldkey);
302+
300303
current_stake
301304
}
302305

@@ -431,6 +434,9 @@ impl<T: Config> Pallet<T> {
431434

432435
// Add the balance to the coldkey account.
433436
Self::add_balance_to_coldkey_account(&delegate_coldkey_i, stake_i);
437+
438+
// Remove stake delta
439+
StakeDeltaSinceLastEmissionDrain::<T>::remove(hotkey, &delegate_coldkey_i);
434440
}
435441
}
436442
}

pallets/subtensor/src/staking/remove_stake.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ impl<T: Config> Pallet<T> {
7676
// We remove the balance from the hotkey.
7777
Self::decrease_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake_to_be_removed);
7878

79+
// Track this removal in the stake delta.
80+
StakeDeltaSinceLastEmissionDrain::<T>::mutate(&hotkey, &coldkey, |stake_delta| {
81+
*stake_delta = stake_delta.saturating_sub_unsigned(stake_to_be_removed as u128);
82+
});
83+
7984
// We add the balance to the coldkey. If the above fails we will not credit this coldkey.
8085
Self::add_balance_to_coldkey_account(&coldkey, stake_to_be_removed);
8186

pallets/subtensor/src/swap/swap_coldkey.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,20 @@ impl<T: Config> Pallet<T> {
169169
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
170170
}
171171

172-
// 4. Swap total coldkey stake.
172+
// 4. Swap StakeDeltaSinceLastEmissionDrain
173+
for hotkey in StakingHotkeys::<T>::get(old_coldkey) {
174+
let old_stake_delta = StakeDeltaSinceLastEmissionDrain::<T>::get(&hotkey, old_coldkey);
175+
let new_stake_delta = StakeDeltaSinceLastEmissionDrain::<T>::get(&hotkey, new_coldkey);
176+
StakeDeltaSinceLastEmissionDrain::<T>::insert(
177+
&hotkey,
178+
new_coldkey,
179+
new_stake_delta.saturating_add(old_stake_delta),
180+
);
181+
StakeDeltaSinceLastEmissionDrain::<T>::remove(&hotkey, old_coldkey);
182+
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
183+
}
184+
185+
// 5. Swap total coldkey stake.
173186
// TotalColdkeyStake: MAP ( coldkey ) --> u64 | Total stake of the coldkey.
174187
let old_coldkey_stake: u64 = TotalColdkeyStake::<T>::get(old_coldkey);
175188
// Get the stake of the new coldkey.
@@ -183,7 +196,7 @@ impl<T: Config> Pallet<T> {
183196
);
184197
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
185198

186-
// 5. Swap StakingHotkeys.
199+
// 6. Swap StakingHotkeys.
187200
// StakingHotkeys: MAP ( coldkey ) --> Vec<hotkeys> | Hotkeys staking for the coldkey.
188201
let old_staking_hotkeys: Vec<T::AccountId> = StakingHotkeys::<T>::get(old_coldkey);
189202
let mut new_staking_hotkeys: Vec<T::AccountId> = StakingHotkeys::<T>::get(new_coldkey);
@@ -197,7 +210,7 @@ impl<T: Config> Pallet<T> {
197210
StakingHotkeys::<T>::insert(new_coldkey, new_staking_hotkeys);
198211
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
199212

200-
// 6. Swap hotkey owners.
213+
// 7. Swap hotkey owners.
201214
// Owner: MAP ( hotkey ) --> coldkey | Owner of the hotkey.
202215
// OwnedHotkeys: MAP ( coldkey ) --> Vec<hotkeys> | Hotkeys owned by the coldkey.
203216
let old_owned_hotkeys: Vec<T::AccountId> = OwnedHotkeys::<T>::get(old_coldkey);
@@ -216,7 +229,7 @@ impl<T: Config> Pallet<T> {
216229
OwnedHotkeys::<T>::insert(new_coldkey, new_owned_hotkeys);
217230
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
218231

219-
// 7. Transfer remaining balance.
232+
// 8. Transfer remaining balance.
220233
// Balance: MAP ( coldkey ) --> u64 | Balance of the coldkey.
221234
// Transfer any remaining balance from old_coldkey to new_coldkey
222235
let remaining_balance = Self::get_coldkey_balance(old_coldkey);

0 commit comments

Comments
 (0)