Skip to content

Commit 2fed26e

Browse files
committed
refactor rate limit check
1 parent b2db7ab commit 2fed26e

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

pallets/subtensor/src/staking/set_children.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<T: Config> Pallet<T> {
6161

6262
// Set last transaction block
6363
let current_block = Self::get_current_block_as_u64();
64-
Self::set_last_transaction_block(
64+
Self::set_last_transaction_block_on_subnet(
6565
&hotkey,
6666
netuid,
6767
&TransactionType::SetChildren,
@@ -325,7 +325,7 @@ impl<T: Config> Pallet<T> {
325325

326326
// Set last transaction block
327327
let current_block = Self::get_current_block_as_u64();
328-
Self::set_last_transaction_block(
328+
Self::set_last_transaction_block_on_subnet(
329329
&hotkey,
330330
netuid,
331331
&TransactionType::SetChildkeyTake,
@@ -336,7 +336,7 @@ impl<T: Config> Pallet<T> {
336336
ChildkeyTake::<T>::insert(hotkey.clone(), netuid, take);
337337

338338
// Update the last transaction block
339-
Self::set_last_transaction_block(
339+
Self::set_last_transaction_block_on_subnet(
340340
&hotkey,
341341
netuid,
342342
&TransactionType::SetChildkeyTake,

pallets/subtensor/src/tests/children.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ fn test_childkey_take_rate_limiting() {
937937
// Helper function to log rate limit information
938938
let log_rate_limit_info = || {
939939
let current_block = SubtensorModule::get_current_block_as_u64();
940-
let last_block = SubtensorModule::get_last_transaction_block(
940+
let last_block = SubtensorModule::get_last_transaction_block_on_subnet(
941941
&hotkey,
942942
netuid,
943943
&TransactionType::SetChildkeyTake,
@@ -947,7 +947,7 @@ fn test_childkey_take_rate_limiting() {
947947
&hotkey,
948948
netuid,
949949
);
950-
let limit = SubtensorModule::get_rate_limit(&TransactionType::SetChildkeyTake, 0);
950+
let limit = SubtensorModule::get_rate_limit_on_subnet(&TransactionType::SetChildkeyTake, netuid);
951951
log::info!(
952952
"Rate limit info: current_block: {}, last_block: {}, limit: {}, passes: {}, diff: {}",
953953
current_block,
@@ -3609,7 +3609,8 @@ fn test_set_children_rate_limit_fail_then_succeed() {
36093609

36103610
// Try again after rate limit period has passed
36113611
// Check rate limit
3612-
let limit = SubtensorModule::get_rate_limit(&TransactionType::SetChildren, netuid);
3612+
let limit =
3613+
SubtensorModule::get_rate_limit_on_subnet(&TransactionType::SetChildren, netuid);
36133614

36143615
// Step that many blocks
36153616
step_block(limit as u16);

pallets/subtensor/src/tests/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ pub fn mock_set_children(coldkey: &U256, parent: &U256, netuid: u16, child_vec:
709709
#[allow(dead_code)]
710710
pub fn step_rate_limit(transaction_type: &TransactionType, netuid: u16) {
711711
// Check rate limit
712-
let limit = SubtensorModule::get_rate_limit(transaction_type, netuid);
712+
let limit = SubtensorModule::get_rate_limit_on_subnet(transaction_type, netuid);
713713

714714
// Step that many blocks
715715
step_block(limit as u16);

pallets/subtensor/src/utils/rate_limiting.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,33 @@ impl<T: Config> Pallet<T> {
3434
// ==== Rate Limiting =====
3535
// ========================
3636
/// Get the rate limit for a specific transaction type
37-
pub fn get_rate_limit(tx_type: &TransactionType, _netuid: u16) -> u64 {
37+
pub fn get_rate_limit(tx_type: &TransactionType) -> u64 {
3838
match tx_type {
3939
TransactionType::SetChildren => 7200, // Cannot set children twice within a day
4040
TransactionType::SetChildkeyTake => TxChildkeyTakeRateLimit::<T>::get(),
4141
TransactionType::Unknown => 0, // Default to no limit for unknown types (no limit)
4242
}
43+
}
44+
}
45+
46+
pub fn get_rate_limit_on_subnet(tx_type: &TransactionType, _netuid: u16) -> u64 {
47+
#[allow(clippy::match_single_binding)]
48+
match tx_type {
49+
_ => Self::get_rate_limit(tx_type),
50+
}
51+
}
52+
53+
pub fn check_passes_rate_limit(limit: u64, block: u64, last_block: u64) -> bool {
54+
// Allow the first transaction (when last_block is 0) or if the rate limit has passed
55+
last_block == 0 || block.saturating_sub(last_block) >= limit
56+
}
57+
58+
pub fn passes_rate_limit(tx_type: &TransactionType, key: &T::AccountId) -> bool {
59+
let block: u64 = Self::get_current_block_as_u64();
60+
let limit: u64 = Self::get_rate_limit(tx_type);
61+
let last_block: u64 = Self::get_last_transaction_block(key, tx_type);
62+
63+
Self::check_passes_rate_limit(limit, block, last_block)
4364
}
4465

4566
/// Check if a transaction should be rate limited on a specific subnet
@@ -49,15 +70,19 @@ impl<T: Config> Pallet<T> {
4970
netuid: u16,
5071
) -> bool {
5172
let block: u64 = Self::get_current_block_as_u64();
52-
let limit: u64 = Self::get_rate_limit(tx_type, netuid);
53-
let last_block: u64 = Self::get_last_transaction_block(hotkey, netuid, tx_type);
73+
let limit: u64 = Self::get_rate_limit_on_subnet(tx_type, netuid);
74+
let last_block: u64 = Self::get_last_transaction_block_on_subnet(hotkey, netuid, tx_type);
5475

55-
// Allow the first transaction (when last_block is 0) or if the rate limit has passed
56-
last_block == 0 || block.saturating_sub(last_block) >= limit
76+
Self::check_passes_rate_limit(limit, block, last_block)
77+
}
78+
79+
/// Get the block number of the last transaction for a specific key, and transaction type
80+
pub fn get_last_transaction_block(key: &T::AccountId, tx_type: &TransactionType) -> u64 {
81+
Self::get_last_transaction_block_on_subnet(key, 0, tx_type)
5782
}
5883

5984
/// Get the block number of the last transaction for a specific hotkey, network, and transaction type
60-
pub fn get_last_transaction_block(
85+
pub fn get_last_transaction_block_on_subnet(
6186
hotkey: &T::AccountId,
6287
netuid: u16,
6388
tx_type: &TransactionType,
@@ -66,15 +91,20 @@ impl<T: Config> Pallet<T> {
6691
TransactionKeyLastBlock::<T>::get((hotkey, netuid, tx_as_u16))
6792
}
6893

94+
/// Set the block number of the last transaction for a specific key, and transaction type
95+
pub fn set_last_transaction_block(key: &T::AccountId, tx_type: &TransactionType, block: u64) {
96+
Self::set_last_transaction_block_on_subnet(key, 0, tx_type, block)
97+
}
98+
6999
/// Set the block number of the last transaction for a specific hotkey, network, and transaction type
70-
pub fn set_last_transaction_block(
71-
hotkey: &T::AccountId,
100+
pub fn set_last_transaction_block_on_subnet(
101+
key: &T::AccountId,
72102
netuid: u16,
73103
tx_type: &TransactionType,
74104
block: u64,
75105
) {
76-
let tx_as_u16: u16 = (*tx_type).into();
77-
TransactionKeyLastBlock::<T>::insert((hotkey, netuid, tx_as_u16), block);
106+
let tx_as_u16: u16 = (*tx_type).into();
107+
TransactionKeyLastBlock::<T>::insert((key, netuid, tx_as_u16), block);
78108
}
79109

80110
pub fn set_last_tx_block(key: &T::AccountId, block: u64) {

0 commit comments

Comments
 (0)