Skip to content

Commit 99154e8

Browse files
authored
Merge pull request #355 from opentensor/modify-stake-rate-limit-devnet
modify stake rate limit
2 parents a014285 + ab9a046 commit 99154e8

File tree

4 files changed

+67
-128
lines changed

4 files changed

+67
-128
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,17 @@ pub mod pallet {
262262
pub type TotalColdkeyStake<T: Config> =
263263
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultAccountTake<T>>;
264264
#[pallet::storage]
265-
// --- MAP (hot) --> stake | Returns a tuple (u64: stakes, u64: block_number)
266-
pub type TotalHotkeyStakesThisInterval<T: Config> =
267-
StorageMap<_, Identity, T::AccountId, (u64, u64), ValueQuery, DefaultStakesPerInterval<T>>;
268-
265+
// --- MAP (hot, cold) --> stake | Returns a tuple (u64: stakes, u64: block_number)
266+
pub type TotalHotkeyColdkeyStakesThisInterval<T: Config> = StorageDoubleMap<
267+
_,
268+
Identity,
269+
T::AccountId,
270+
Identity,
271+
T::AccountId,
272+
(u64, u64),
273+
ValueQuery,
274+
DefaultStakesPerInterval<T>,
275+
>;
269276
#[pallet::storage] // --- MAP ( hot ) --> cold | Returns the controlling coldkey for a hotkey.
270277
pub type Owner<T: Config> =
271278
StorageMap<_, Blake2_128Concat, T::AccountId, T::AccountId, ValueQuery, DefaultAccount<T>>;
@@ -1848,32 +1855,14 @@ where
18481855
return Err(InvalidTransaction::Call.into());
18491856
}
18501857
}
1851-
Some(Call::add_stake { hotkey, .. }) => {
1852-
let stakes_this_interval = Pallet::<T>::get_stakes_this_interval_for_hotkey(hotkey);
1853-
let max_stakes_per_interval = Pallet::<T>::get_target_stakes_per_interval();
1854-
1855-
if stakes_this_interval >= max_stakes_per_interval {
1856-
return InvalidTransaction::ExhaustsResources.into();
1857-
}
1858-
1859-
Ok(ValidTransaction {
1860-
priority: Self::get_priority_vanilla(),
1861-
..Default::default()
1862-
})
1863-
}
1864-
Some(Call::remove_stake { hotkey, .. }) => {
1865-
let stakes_this_interval = Pallet::<T>::get_stakes_this_interval_for_hotkey(hotkey);
1866-
let max_stakes_per_interval = Pallet::<T>::get_target_stakes_per_interval();
1867-
1868-
if stakes_this_interval >= max_stakes_per_interval {
1869-
return InvalidTransaction::ExhaustsResources.into();
1870-
}
1871-
1872-
Ok(ValidTransaction {
1873-
priority: Self::get_priority_vanilla(),
1874-
..Default::default()
1875-
})
1876-
}
1858+
Some(Call::add_stake { .. }) => Ok(ValidTransaction {
1859+
priority: Self::get_priority_vanilla(),
1860+
..Default::default()
1861+
}),
1862+
Some(Call::remove_stake { .. }) => Ok(ValidTransaction {
1863+
priority: Self::get_priority_vanilla(),
1864+
..Default::default()
1865+
}),
18771866
Some(Call::register { netuid, .. } | Call::burned_register { netuid, .. }) => {
18781867
let registrations_this_interval =
18791868
Pallet::<T>::get_registrations_this_interval(*netuid);

pallets/subtensor/src/staking.rs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,8 @@ impl<T: Config> Pallet<T> {
157157
Error::<T>::NonAssociatedColdKey
158158
);
159159

160-
// --- 6. Ensure we don't exceed tx rate limit
161-
let block: u64 = Self::get_current_block_as_u64();
162-
ensure!(
163-
!Self::exceeds_tx_rate_limit(Self::get_last_tx_block(&coldkey), block),
164-
Error::<T>::TxRateLimitExceeded
165-
);
166-
167-
// --- 7. Ensure we don't exceed stake rate limit
168-
let stakes_this_interval = Self::get_stakes_this_interval_for_hotkey(&hotkey);
160+
// --- 6. Ensure we don't exceed stake rate limit
161+
let stakes_this_interval = Self::get_stakes_this_interval_for_coldkey_hotkey(&coldkey, &hotkey);
169162
ensure!(
170163
stakes_this_interval < Self::get_target_stakes_per_interval(),
171164
Error::<T>::StakeRateLimitExceeded
@@ -181,10 +174,12 @@ impl<T: Config> Pallet<T> {
181174
Self::increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake_to_be_added);
182175

183176
// Set last block for rate limiting
177+
let block: u64 = Self::get_current_block_as_u64();
184178
Self::set_last_tx_block(&coldkey, block);
185179

186-
// --- 10. Emit the staking event.
187-
Self::set_stakes_this_interval_for_hotkey(
180+
// --- 9. Emit the staking event.
181+
Self::set_stakes_this_interval_for_coldkey_hotkey(
182+
&coldkey,
188183
&hotkey,
189184
stakes_this_interval + 1,
190185
block,
@@ -196,7 +191,7 @@ impl<T: Config> Pallet<T> {
196191
);
197192
Self::deposit_event(Event::StakeAdded(hotkey, stake_to_be_added));
198193

199-
// --- 11. Ok and return.
194+
// --- 10. Ok and return.
200195
Ok(())
201196
}
202197

@@ -278,31 +273,26 @@ impl<T: Config> Pallet<T> {
278273
Error::<T>::CouldNotConvertToBalance
279274
);
280275

281-
// --- 6. Ensure we don't exceed tx rate limit
282-
let block: u64 = Self::get_current_block_as_u64();
283-
ensure!(
284-
!Self::exceeds_tx_rate_limit(Self::get_last_tx_block(&coldkey), block),
285-
Error::<T>::TxRateLimitExceeded
286-
);
287-
288-
// --- 7. Ensure we don't exceed stake rate limit
289-
let unstakes_this_interval = Self::get_stakes_this_interval_for_hotkey(&hotkey);
276+
// --- 6. Ensure we don't exceed stake rate limit
277+
let unstakes_this_interval = Self::get_stakes_this_interval_for_coldkey_hotkey(&coldkey, &hotkey);
290278
ensure!(
291279
unstakes_this_interval < Self::get_target_stakes_per_interval(),
292280
Error::<T>::UnstakeRateLimitExceeded
293281
);
294282

295-
// --- 8. We remove the balance from the hotkey.
283+
// --- 7. We remove the balance from the hotkey.
296284
Self::decrease_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake_to_be_removed);
297285

298-
// --- 9. We add the balancer to the coldkey. If the above fails we will not credit this coldkey.
286+
// --- 8. We add the balancer to the coldkey. If the above fails we will not credit this coldkey.
299287
Self::add_balance_to_coldkey_account(&coldkey, stake_to_be_added_as_currency.unwrap());
300288

301289
// Set last block for rate limiting
290+
let block: u64 = Self::get_current_block_as_u64();
302291
Self::set_last_tx_block(&coldkey, block);
303292

304-
// --- 10. Emit the unstaking event.
305-
Self::set_stakes_this_interval_for_hotkey(
293+
// --- 9. Emit the unstaking event.
294+
Self::set_stakes_this_interval_for_coldkey_hotkey(
295+
&coldkey,
306296
&hotkey,
307297
unstakes_this_interval + 1,
308298
block,
@@ -314,7 +304,7 @@ impl<T: Config> Pallet<T> {
314304
);
315305
Self::deposit_event(Event::StakeRemoved(hotkey, stake_to_be_removed));
316306

317-
// --- 11. Done and ok.
307+
// --- 10. Done and ok.
318308
Ok(())
319309
}
320310

@@ -367,15 +357,15 @@ impl<T: Config> Pallet<T> {
367357
}
368358

369359
// Retrieves the total stakes for a given hotkey (account ID) for the current staking interval.
370-
pub fn get_stakes_this_interval_for_hotkey(hotkey: &T::AccountId) -> u64 {
360+
pub fn get_stakes_this_interval_for_coldkey_hotkey(coldkey: &T::AccountId, hotkey: &T::AccountId) -> u64 {
371361
// Retrieve the configured stake interval duration from storage.
372362
let stake_interval = StakeInterval::<T>::get();
373363

374364
// Obtain the current block number as an unsigned 64-bit integer.
375365
let current_block = Self::get_current_block_as_u64();
376366

377367
// Fetch the total stakes and the last block number when stakes were made for the hotkey.
378-
let (stakes, block_last_staked_at) = TotalHotkeyStakesThisInterval::<T>::get(hotkey);
368+
let (stakes, block_last_staked_at) = TotalHotkeyColdkeyStakesThisInterval::<T>::get(coldkey, hotkey);
379369

380370
// Calculate the block number after which the stakes for the hotkey should be reset.
381371
let block_to_reset_after = block_last_staked_at + stake_interval;
@@ -384,7 +374,7 @@ impl<T: Config> Pallet<T> {
384374
// it indicates the end of the staking interval for the hotkey.
385375
if block_to_reset_after <= current_block {
386376
// Reset the stakes for this hotkey for the current interval.
387-
Self::set_stakes_this_interval_for_hotkey(hotkey, 0, block_last_staked_at);
377+
Self::set_stakes_this_interval_for_coldkey_hotkey(coldkey, hotkey, 0, block_last_staked_at);
388378
// Return 0 as the stake amount since we've just reset the stakes.
389379
return 0;
390380
}

pallets/subtensor/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ impl<T: Config> Pallet<T> {
140140
pub fn set_target_stakes_per_interval(target_stakes_per_interval: u64) {
141141
TargetStakesPerInterval::<T>::set(target_stakes_per_interval)
142142
}
143-
pub fn set_stakes_this_interval_for_hotkey(hotkey: &T::AccountId, stakes_this_interval: u64, last_staked_block_number: u64) {
144-
TotalHotkeyStakesThisInterval::<T>::insert(hotkey, (stakes_this_interval, last_staked_block_number));
143+
pub fn set_stakes_this_interval_for_coldkey_hotkey(coldkey: &T::AccountId, hotkey: &T::AccountId, stakes_this_interval: u64, last_staked_block_number: u64) {
144+
TotalHotkeyColdkeyStakesThisInterval::<T>::insert(coldkey, hotkey, (stakes_this_interval, last_staked_block_number));
145145
}
146146
pub fn set_stake_interval(block: u64) {
147147
StakeInterval::<T>::set(block);

0 commit comments

Comments
 (0)