@@ -34,12 +34,33 @@ impl<T: Config> Pallet<T> {
34
34
// ==== Rate Limiting =====
35
35
// ========================
36
36
/// 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 {
38
38
match tx_type {
39
39
TransactionType :: SetChildren => 7200 , // Cannot set children twice within a day
40
40
TransactionType :: SetChildkeyTake => TxChildkeyTakeRateLimit :: < T > :: get ( ) ,
41
41
TransactionType :: Unknown => 0 , // Default to no limit for unknown types (no limit)
42
42
}
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)
43
64
}
44
65
45
66
/// Check if a transaction should be rate limited on a specific subnet
@@ -49,15 +70,19 @@ impl<T: Config> Pallet<T> {
49
70
netuid : u16 ,
50
71
) -> bool {
51
72
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) ;
54
75
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)
57
82
}
58
83
59
84
/// 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 (
61
86
hotkey : & T :: AccountId ,
62
87
netuid : u16 ,
63
88
tx_type : & TransactionType ,
@@ -66,15 +91,20 @@ impl<T: Config> Pallet<T> {
66
91
TransactionKeyLastBlock :: < T > :: get ( ( hotkey, netuid, tx_as_u16) )
67
92
}
68
93
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
+
69
99
/// 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 ,
72
102
netuid : u16 ,
73
103
tx_type : & TransactionType ,
74
104
block : u64 ,
75
105
) {
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) ;
78
108
}
79
109
80
110
pub fn set_last_tx_block ( key : & T :: AccountId , block : u64 ) {
0 commit comments