Skip to content

Commit 00d83cb

Browse files
authored
Merge pull request #2060 from opentensor/fix/subsubnet-rate-limits
Fix subsubnet rate limiting and add tests
2 parents 69e41a3 + f0e3a11 commit 00d83cb

File tree

5 files changed

+80
-10
lines changed

5 files changed

+80
-10
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,15 +1870,15 @@ pub mod pallet {
18701870
let maybe_owner = pallet_subtensor::Pallet::<T>::ensure_sn_owner_or_root_with_limits(
18711871
origin,
18721872
netuid,
1873-
&[TransactionType::SubsubnetParameterUpdate],
1873+
&[TransactionType::SubsubnetCountUpdate],
18741874
)?;
18751875

18761876
pallet_subtensor::Pallet::<T>::do_set_subsubnet_count(netuid, subsub_count)?;
18771877

18781878
pallet_subtensor::Pallet::<T>::record_owner_rl(
18791879
maybe_owner,
18801880
netuid,
1881-
&[TransactionType::SubsubnetParameterUpdate],
1881+
&[TransactionType::SubsubnetCountUpdate],
18821882
);
18831883
Ok(())
18841884
}
@@ -1896,15 +1896,15 @@ pub mod pallet {
18961896
let maybe_owner = pallet_subtensor::Pallet::<T>::ensure_sn_owner_or_root_with_limits(
18971897
origin,
18981898
netuid,
1899-
&[TransactionType::SubsubnetParameterUpdate],
1899+
&[TransactionType::SubsubnetEmission],
19001900
)?;
19011901

19021902
pallet_subtensor::Pallet::<T>::do_set_emission_split(netuid, maybe_split)?;
19031903

19041904
pallet_subtensor::Pallet::<T>::record_owner_rl(
19051905
maybe_owner,
19061906
netuid,
1907-
&[TransactionType::SubsubnetParameterUpdate],
1907+
&[TransactionType::SubsubnetEmission],
19081908
);
19091909
Ok(())
19101910
}

pallets/admin-utils/src/tests/mod.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,3 +2269,64 @@ fn test_sudo_set_subsubnet_count() {
22692269
));
22702270
});
22712271
}
2272+
2273+
// cargo test --package pallet-admin-utils --lib -- tests::test_sudo_set_subsubnet_count_and_emissions --exact --show-output
2274+
#[test]
2275+
fn test_sudo_set_subsubnet_count_and_emissions() {
2276+
new_test_ext().execute_with(|| {
2277+
let netuid = NetUid::from(1);
2278+
let ss_count_ok = SubId::from(2);
2279+
2280+
let sn_owner = U256::from(1324);
2281+
add_network(netuid, 10);
2282+
// Set the Subnet Owner
2283+
SubnetOwner::<Test>::insert(netuid, sn_owner);
2284+
2285+
assert_ok!(AdminUtils::sudo_set_subsubnet_count(
2286+
<<Test as Config>::RuntimeOrigin>::signed(sn_owner),
2287+
netuid,
2288+
ss_count_ok
2289+
));
2290+
2291+
// Cannot set emission split with wrong number of entries
2292+
// With two subsubnets the size of the split vector should be 2, not 3
2293+
assert_noop!(
2294+
AdminUtils::sudo_set_subsubnet_emission_split(
2295+
<<Test as Config>::RuntimeOrigin>::signed(sn_owner),
2296+
netuid,
2297+
Some(vec![0xFFFF / 5 * 2, 0xFFFF / 5 * 2, 0xFFFF / 5])
2298+
),
2299+
pallet_subtensor::Error::<Test>::InvalidValue
2300+
);
2301+
2302+
// Cannot set emission split with wrong total of entries
2303+
// Split vector entries should sum up to exactly 0xFFFF
2304+
assert_noop!(
2305+
AdminUtils::sudo_set_subsubnet_emission_split(
2306+
<<Test as Config>::RuntimeOrigin>::signed(sn_owner),
2307+
netuid,
2308+
Some(vec![0xFFFF / 5 * 4, 0xFFFF / 5 - 1])
2309+
),
2310+
pallet_subtensor::Error::<Test>::InvalidValue
2311+
);
2312+
2313+
// Can set good split ok
2314+
// We also verify here that it can happen in the same block as setting subsubnet counts
2315+
// or soon, without rate limiting
2316+
assert_ok!(AdminUtils::sudo_set_subsubnet_emission_split(
2317+
<<Test as Config>::RuntimeOrigin>::signed(sn_owner),
2318+
netuid,
2319+
Some(vec![0xFFFF / 5, 0xFFFF / 5 * 4])
2320+
));
2321+
2322+
// Cannot set it again due to rate limits
2323+
assert_noop!(
2324+
AdminUtils::sudo_set_subsubnet_emission_split(
2325+
<<Test as Config>::RuntimeOrigin>::signed(sn_owner),
2326+
netuid,
2327+
Some(vec![0xFFFF / 5 * 4, 0xFFFF / 5])
2328+
),
2329+
pallet_subtensor::Error::<Test>::TxRateLimitExceeded
2330+
);
2331+
});
2332+
}

pallets/subtensor/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1840,7 +1840,12 @@ pub mod pallet {
18401840
#[pallet::type_value]
18411841
/// -- ITEM (Rate limit for subsubnet count updates)
18421842
pub fn SubsubnetCountSetRateLimit<T: Config>() -> u64 {
1843-
prod_or_fast!(7_200, 0)
1843+
prod_or_fast!(7_200, 1)
1844+
}
1845+
#[pallet::type_value]
1846+
/// -- ITEM (Rate limit for subsubnet emission distribution updates)
1847+
pub fn SubsubnetEmissionRateLimit<T: Config>() -> u64 {
1848+
prod_or_fast!(7_200, 1)
18441849
}
18451850
#[pallet::storage]
18461851
/// --- MAP ( netuid ) --> Current number of sub-subnets

pallets/subtensor/src/subnets/subsubnet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl<T: Config> Pallet<T> {
172172

173173
// Check that values add up to 65535
174174
let total: u64 = split.iter().map(|s| *s as u64).sum();
175-
ensure!(total <= u16::MAX as u64, Error::<T>::InvalidValue);
175+
ensure!(total == u16::MAX as u64, Error::<T>::InvalidValue);
176176

177177
SubsubnetEmissionSplit::<T>::insert(netuid, split);
178178
} else {

pallets/subtensor/src/utils/rate_limiting.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ pub enum TransactionType {
1212
SetWeightsVersionKey,
1313
SetSNOwnerHotkey,
1414
OwnerHyperparamUpdate,
15-
SubsubnetParameterUpdate,
15+
SubsubnetCountUpdate,
16+
SubsubnetEmission,
1617
}
1718

1819
/// Implement conversion from TransactionType to u16
@@ -26,7 +27,8 @@ impl From<TransactionType> for u16 {
2627
TransactionType::SetWeightsVersionKey => 4,
2728
TransactionType::SetSNOwnerHotkey => 5,
2829
TransactionType::OwnerHyperparamUpdate => 6,
29-
TransactionType::SubsubnetParameterUpdate => 7,
30+
TransactionType::SubsubnetCountUpdate => 7,
31+
TransactionType::SubsubnetEmission => 8,
3032
}
3133
}
3234
}
@@ -41,7 +43,8 @@ impl From<u16> for TransactionType {
4143
4 => TransactionType::SetWeightsVersionKey,
4244
5 => TransactionType::SetSNOwnerHotkey,
4345
6 => TransactionType::OwnerHyperparamUpdate,
44-
7 => TransactionType::SubsubnetParameterUpdate,
46+
7 => TransactionType::SubsubnetCountUpdate,
47+
8 => TransactionType::SubsubnetEmission,
4548
_ => TransactionType::Unknown,
4649
}
4750
}
@@ -57,7 +60,8 @@ impl<T: Config> Pallet<T> {
5760
TransactionType::SetChildkeyTake => TxChildkeyTakeRateLimit::<T>::get(),
5861
TransactionType::RegisterNetwork => NetworkRateLimit::<T>::get(),
5962
TransactionType::OwnerHyperparamUpdate => OwnerHyperparamRateLimit::<T>::get(),
60-
TransactionType::SubsubnetParameterUpdate => SubsubnetCountSetRateLimit::<T>::get(),
63+
TransactionType::SubsubnetCountUpdate => SubsubnetCountSetRateLimit::<T>::get(),
64+
TransactionType::SubsubnetEmission => SubsubnetEmissionRateLimit::<T>::get(),
6165

6266
TransactionType::Unknown => 0, // Default to no limit for unknown types (no limit)
6367
_ => 0,

0 commit comments

Comments
 (0)