Skip to content

Commit 9b557c4

Browse files
author
Samuel Dare
committed
feat: fix child take tests
1 parent 1a88e73 commit 9b557c4

File tree

8 files changed

+143
-127
lines changed

8 files changed

+143
-127
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,11 @@ pub mod pallet {
11371137
pub type LastTxBlock<T: Config> =
11381138
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
11391139
#[pallet::storage]
1140-
/// --- MAP ( key ) --> last_block
1140+
/// --- MAP ( key ) --> last_tx_block_childkey_take
1141+
pub type LastTxBlockChildKeyTake<T: Config> =
1142+
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
1143+
#[pallet::storage]
1144+
/// --- MAP ( key ) --> last_tx_block_delegate_take
11411145
pub type LastTxBlockDelegateTake<T: Config> =
11421146
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
11431147
#[pallet::storage]

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ mod dispatches {
737737
/// * `TxChildkeyTakeRateLimitExceeded`:
738738
/// - The rate limit for changing childkey take has been exceeded.
739739
///
740-
#[pallet::call_index(68)]
740+
#[pallet::call_index(75)]
741741
#[pallet::weight((
742742
Weight::from_parts(34_000, 0)
743743
.saturating_add(T::DbWeight::get().reads(4))

pallets/subtensor/src/staking/set_children.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -236,26 +236,28 @@ impl<T: Config> Pallet<T> {
236236
Error::<T>::InvalidChildkeyTake
237237
);
238238

239-
// Ensure the hotkey passes the rate limit
240-
ensure!(
241-
Self::passes_rate_limit_on_subnet(&TransactionType::SetChildkeyTake, &hotkey, netuid),
242-
Error::<T>::TxChildkeyTakeRateLimitExceeded
239+
// Check if the rate limit has been exceeded
240+
let current_block = Self::get_current_block_as_u64();
241+
let last_tx_block =
242+
Self::get_last_transaction_block(&hotkey, netuid, &TransactionType::SetChildkeyTake);
243+
let rate_limit = TxChildkeyTakeRateLimit::<T>::get();
244+
let passes =
245+
Self::passes_rate_limit_on_subnet(&TransactionType::SetChildkeyTake, &hotkey, netuid);
246+
247+
log::info!(
248+
"Rate limit check: current_block: {}, last_tx_block: {}, rate_limit: {}, passes: {}",
249+
current_block,
250+
last_tx_block,
251+
rate_limit,
252+
passes
243253
);
244254

255+
ensure!(passes, Error::<T>::TxChildkeyTakeRateLimitExceeded);
256+
245257
// Set the new childkey take value for the given hotkey and network
246258
ChildkeyTake::<T>::insert(hotkey.clone(), netuid, take);
247259

248-
// TODO: Consider adding a check to ensure the hotkey is registered on the specified network (netuid)
249-
// before setting the childkey take. This could prevent setting takes for non-existent or
250-
// unregistered hotkeys.
251-
252-
// NOTE: The childkey take is now associated with both the hotkey and the network ID.
253-
// This allows for different take values across different networks for the same hotkey.
254-
255260
// Update the last transaction block
256-
let current_block: u64 = <frame_system::Pallet<T>>::block_number()
257-
.try_into()
258-
.unwrap_or(0);
259261
Self::set_last_transaction_block(
260262
&hotkey,
261263
netuid,

pallets/subtensor/src/utils/misc.rs

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,6 @@ use sp_core::U256;
88
use sp_runtime::Saturating;
99
use substrate_fixed::types::I32F32;
1010

11-
/// Enum representing different types of transactions
12-
#[derive(Copy, Clone)]
13-
pub enum TransactionType {
14-
SetChildren,
15-
Unknown,
16-
}
17-
18-
/// Implement conversion from TransactionType to u16
19-
impl From<TransactionType> for u16 {
20-
fn from(tx_type: TransactionType) -> Self {
21-
match tx_type {
22-
TransactionType::SetChildren => 0,
23-
TransactionType::Unknown => 1,
24-
}
25-
}
26-
}
27-
28-
/// Implement conversion from u16 to TransactionType
29-
impl From<u16> for TransactionType {
30-
fn from(value: u16) -> Self {
31-
match value {
32-
0 => TransactionType::SetChildren,
33-
_ => TransactionType::Unknown,
34-
}
35-
}
36-
}
37-
3811
impl<T: Config> Pallet<T> {
3912
pub fn ensure_subnet_owner_or_root(
4013
o: T::RuntimeOrigin,
@@ -312,17 +285,7 @@ impl<T: Config> Pallet<T> {
312285
pub fn coinbase(amount: u64) {
313286
TotalIssuance::<T>::put(TotalIssuance::<T>::get().saturating_add(amount));
314287
}
315-
pub fn get_default_take() -> u16 {
316-
// Default to maximum
317-
MaxTake::<T>::get()
318-
}
319-
pub fn set_max_take(default_take: u16) {
320-
MaxTake::<T>::put(default_take);
321-
Self::deposit_event(Event::DefaultTakeSet(default_take));
322-
}
323-
pub fn get_min_take() -> u16 {
324-
MinTake::<T>::get()
325-
}
288+
326289
pub fn set_subnet_locked_balance(netuid: u16, amount: u64) {
327290
SubnetLocked::<T>::insert(netuid, amount);
328291
}
@@ -357,18 +320,49 @@ impl<T: Config> Pallet<T> {
357320
Self::deposit_event(Event::TxDelegateTakeRateLimitSet(tx_rate_limit));
358321
}
359322
pub fn set_min_delegate_take(take: u16) {
360-
MinTake::<T>::put(take);
323+
MinDelegateTake::<T>::put(take);
361324
Self::deposit_event(Event::MinDelegateTakeSet(take));
362325
}
363326
pub fn set_max_delegate_take(take: u16) {
364-
MaxTake::<T>::put(take);
327+
MaxDelegateTake::<T>::put(take);
365328
Self::deposit_event(Event::MaxDelegateTakeSet(take));
366329
}
367330
pub fn get_min_delegate_take() -> u16 {
368-
MinTake::<T>::get()
331+
MinDelegateTake::<T>::get()
369332
}
370333
pub fn get_max_delegate_take() -> u16 {
371-
MaxTake::<T>::get()
334+
MaxDelegateTake::<T>::get()
335+
}
336+
pub fn get_default_delegate_take() -> u16 {
337+
// Default to maximum
338+
MaxDelegateTake::<T>::get()
339+
}
340+
// get_default_childkey_take
341+
pub fn get_default_childkey_take() -> u16 {
342+
// Default to maximum
343+
MinChildkeyTake::<T>::get()
344+
}
345+
pub fn get_tx_childkey_take_rate_limit() -> u64 {
346+
TxChildkeyTakeRateLimit::<T>::get()
347+
}
348+
pub fn set_tx_childkey_take_rate_limit(tx_rate_limit: u64) {
349+
TxChildkeyTakeRateLimit::<T>::put(tx_rate_limit);
350+
Self::deposit_event(Event::TxChildKeyTakeRateLimitSet(tx_rate_limit));
351+
}
352+
pub fn set_min_childkey_take(take: u16) {
353+
MinChildkeyTake::<T>::put(take);
354+
Self::deposit_event(Event::MinChildKeyTakeSet(take));
355+
}
356+
pub fn set_max_childkey_take(take: u16) {
357+
MaxChildkeyTake::<T>::put(take);
358+
Self::deposit_event(Event::MaxChildKeyTakeSet(take));
359+
}
360+
pub fn get_min_childkey_take() -> u16 {
361+
MinChildkeyTake::<T>::get()
362+
}
363+
364+
pub fn get_max_childkey_take() -> u16 {
365+
MaxChildkeyTake::<T>::get()
372366
}
373367

374368
pub fn get_serving_rate_limit(netuid: u16) -> u64 {

pallets/subtensor/src/utils/rate_limiting.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use sp_core::Get;
55
#[derive(Copy, Clone)]
66
pub enum TransactionType {
77
SetChildren,
8+
SetChildkeyTake,
89
Unknown,
910
}
1011

@@ -13,7 +14,8 @@ impl From<TransactionType> for u16 {
1314
fn from(tx_type: TransactionType) -> Self {
1415
match tx_type {
1516
TransactionType::SetChildren => 0,
16-
TransactionType::Unknown => 1,
17+
TransactionType::SetChildkeyTake => 1,
18+
TransactionType::Unknown => 2,
1719
}
1820
}
1921
}
@@ -23,6 +25,7 @@ impl From<u16> for TransactionType {
2325
fn from(value: u16) -> Self {
2426
match value {
2527
0 => TransactionType::SetChildren,
28+
1 => TransactionType::SetChildkeyTake,
2629
_ => TransactionType::Unknown,
2730
}
2831
}
@@ -35,6 +38,7 @@ impl<T: Config> Pallet<T> {
3538
pub fn get_rate_limit(tx_type: &TransactionType) -> u64 {
3639
match tx_type {
3740
TransactionType::SetChildren => (DefaultTempo::<T>::get().saturating_mul(2)).into(), // Cannot set children twice within the default tempo period.
41+
TransactionType::SetChildkeyTake => TxChildkeyTakeRateLimit::<T>::get(),
3842
TransactionType::Unknown => 0, // Default to no limit for unknown types (no limit)
3943
}
4044
}
@@ -48,7 +52,9 @@ impl<T: Config> Pallet<T> {
4852
let block: u64 = Self::get_current_block_as_u64();
4953
let limit: u64 = Self::get_rate_limit(tx_type);
5054
let last_block: u64 = Self::get_last_transaction_block(hotkey, netuid, tx_type);
51-
block.saturating_sub(last_block) < limit
55+
56+
// Allow the first transaction (when last_block is 0) or if the rate limit has passed
57+
last_block == 0 || block.saturating_sub(last_block) >= limit
5258
}
5359

5460
/// Check if a transaction should be rate limited globally
@@ -93,6 +99,13 @@ impl<T: Config> Pallet<T> {
9399
pub fn get_last_tx_block_delegate_take(key: &T::AccountId) -> u64 {
94100
LastTxBlockDelegateTake::<T>::get(key)
95101
}
102+
103+
pub fn set_last_tx_block_childkey_take(key: &T::AccountId, block: u64) {
104+
LastTxBlockChildKeyTake::<T>::insert(key, block)
105+
}
106+
pub fn get_last_tx_block_childkey_take(key: &T::AccountId) -> u64 {
107+
LastTxBlockChildKeyTake::<T>::get(key)
108+
}
96109
pub fn exceeds_tx_rate_limit(prev_tx_block: u64, current_block: u64) -> bool {
97110
let rate_limit: u64 = Self::get_tx_rate_limit();
98111
if rate_limit == 0 || prev_tx_block == 0 {

0 commit comments

Comments
 (0)