Skip to content

Commit 9520e53

Browse files
committed
Fix stake rate limit counter reset to current block
1 parent 552fa1b commit 9520e53

File tree

6 files changed

+159
-211
lines changed

6 files changed

+159
-211
lines changed

pallets/subtensor/src/macros/errors.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ mod errors {
9191
/// A transactor exceeded the rate limit for setting or swapping hotkey.
9292
HotKeySetTxRateLimitExceeded,
9393
/// A transactor exceeded the rate limit for staking.
94-
StakeRateLimitExceeded,
95-
/// A transactor exceeded the rate limit for unstaking.
96-
UnstakeRateLimitExceeded,
94+
StakingRateLimitExceeded,
9795
/// Registration is disabled.
9896
SubNetRegistrationDisabled,
9997
/// The number of registration attempts exceeded the allowed number in the interval.

pallets/subtensor/src/staking/add_stake.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,6 @@ impl<T: Config> Pallet<T> {
6262
Error::<T>::HotKeyNotDelegateAndSignerNotOwnHotKey
6363
);
6464

65-
// Ensure we don't exceed stake rate limit
66-
let stakes_this_interval =
67-
Self::get_stakes_this_interval_for_coldkey_hotkey(&coldkey, &hotkey);
68-
ensure!(
69-
stakes_this_interval < Self::get_target_stakes_per_interval(),
70-
Error::<T>::StakeRateLimitExceeded
71-
);
72-
73-
// Track this addition in the stake delta.
74-
StakeDeltaSinceLastEmissionDrain::<T>::mutate(&hotkey, &coldkey, |stake_delta| {
75-
*stake_delta = stake_delta.saturating_add_unsigned(stake_to_be_added as u128);
76-
});
77-
7865
// If coldkey is not owner of the hotkey, it's a nomination stake.
7966
if !Self::coldkey_owns_hotkey(&coldkey, &hotkey) {
8067
let total_stake_after_add =
@@ -86,24 +73,24 @@ impl<T: Config> Pallet<T> {
8673
);
8774
}
8875

76+
Self::try_increase_staking_counter(&coldkey, &hotkey)?;
77+
8978
// Ensure the remove operation from the coldkey is a success.
9079
let actual_amount_to_stake =
9180
Self::remove_balance_from_coldkey_account(&coldkey, stake_to_be_added)?;
9281

9382
// If we reach here, add the balance to the hotkey.
9483
Self::increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, actual_amount_to_stake);
9584

85+
// Track this addition in the stake delta.
86+
StakeDeltaSinceLastEmissionDrain::<T>::mutate(&hotkey, &coldkey, |stake_delta| {
87+
*stake_delta = stake_delta.saturating_add_unsigned(stake_to_be_added as u128);
88+
});
89+
9690
// Set last block for rate limiting
97-
let block: u64 = Self::get_current_block_as_u64();
91+
let block = Self::get_current_block_as_u64();
9892
Self::set_last_tx_block(&coldkey, block);
9993

100-
// Emit the staking event.
101-
Self::set_stakes_this_interval_for_coldkey_hotkey(
102-
&coldkey,
103-
&hotkey,
104-
stakes_this_interval.saturating_add(1),
105-
block,
106-
);
10794
log::debug!(
10895
"StakeAdded( hotkey:{:?}, stake_to_be_added:{:?} )",
10996
hotkey,

pallets/subtensor/src/staking/helpers.rs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,42 +59,6 @@ impl<T: Config> Pallet<T> {
5959
Stake::<T>::get(hotkey, coldkey)
6060
}
6161

62-
// Retrieves the total stakes for a given hotkey (account ID) for the current staking interval.
63-
pub fn get_stakes_this_interval_for_coldkey_hotkey(
64-
coldkey: &T::AccountId,
65-
hotkey: &T::AccountId,
66-
) -> u64 {
67-
// Retrieve the configured stake interval duration from storage.
68-
let stake_interval = StakeInterval::<T>::get();
69-
70-
// Obtain the current block number as an unsigned 64-bit integer.
71-
let current_block = Self::get_current_block_as_u64();
72-
73-
// Fetch the total stakes and the last block number when stakes were made for the hotkey.
74-
let (stakes, block_last_staked_at) =
75-
TotalHotkeyColdkeyStakesThisInterval::<T>::get(coldkey, hotkey);
76-
77-
// Calculate the block number after which the stakes for the hotkey should be reset.
78-
let block_to_reset_after = block_last_staked_at.saturating_add(stake_interval);
79-
80-
// If the current block number is beyond the reset point,
81-
// it indicates the end of the staking interval for the hotkey.
82-
if block_to_reset_after <= current_block {
83-
// Reset the stakes for this hotkey for the current interval.
84-
Self::set_stakes_this_interval_for_coldkey_hotkey(
85-
coldkey,
86-
hotkey,
87-
0,
88-
block_last_staked_at,
89-
);
90-
// Return 0 as the stake amount since we've just reset the stakes.
91-
return 0;
92-
}
93-
94-
// If the staking interval has not yet ended, return the current stake amount.
95-
stakes
96-
}
97-
9862
pub fn get_target_stakes_per_interval() -> u64 {
9963
TargetStakesPerInterval::<T>::get()
10064
}

pallets/subtensor/src/staking/remove_stake.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,7 @@ impl<T: Config> Pallet<T> {
6565
Error::<T>::NotEnoughStakeToWithdraw
6666
);
6767

68-
// Ensure we don't exceed stake rate limit
69-
let unstakes_this_interval =
70-
Self::get_stakes_this_interval_for_coldkey_hotkey(&coldkey, &hotkey);
71-
ensure!(
72-
unstakes_this_interval < Self::get_target_stakes_per_interval(),
73-
Error::<T>::UnstakeRateLimitExceeded
74-
);
68+
Self::try_increase_staking_counter(&coldkey, &hotkey)?;
7569

7670
// We remove the balance from the hotkey.
7771
Self::decrease_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake_to_be_removed);
@@ -98,16 +92,10 @@ impl<T: Config> Pallet<T> {
9892
}
9993

10094
// Set last block for rate limiting
101-
let block: u64 = Self::get_current_block_as_u64();
95+
let block = Self::get_current_block_as_u64();
10296
Self::set_last_tx_block(&coldkey, block);
10397

10498
// Emit the unstaking event.
105-
Self::set_stakes_this_interval_for_coldkey_hotkey(
106-
&coldkey,
107-
&hotkey,
108-
unstakes_this_interval.saturating_add(1),
109-
block,
110-
);
11199
log::debug!(
112100
"StakeRemoved( hotkey:{:?}, stake_to_be_removed:{:?} )",
113101
hotkey,

0 commit comments

Comments
 (0)