Skip to content

Commit 33cbc8b

Browse files
committed
fix + tests
1 parent 469bfb0 commit 33cbc8b

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ pub mod pallet {
538538
);
539539
// Prevent chain bloat: Require max UIDs to be limited
540540
let mechanism_count = pallet_subtensor::MechanismCountCurrent::<T>::get(netuid);
541-
pallet_subtensor::Pallet::<T>::check_max_uids_vs_mechanism_count(
541+
pallet_subtensor::Pallet::<T>::ensure_max_uids_over_all_mechanisms(
542542
max_allowed_uids,
543543
mechanism_count.into(),
544544
)?;

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use pallet_subtensor::{
1010
TargetRegistrationsPerInterval, Tempo, WeightsVersionKeyRateLimit, *,
1111
};
1212
// use pallet_subtensor::{migrations, Event};
13-
use pallet_subtensor::{Event, utils::rate_limiting::TransactionType};
13+
use pallet_subtensor::{
14+
Event, subnets::mechanism::MAX_MECHANISM_COUNT_PER_SUBNET,
15+
utils::rate_limiting::TransactionType,
16+
};
1417
use sp_consensus_grandpa::AuthorityId as GrandpaId;
1518
use sp_core::{Get, Pair, U256, ed25519};
1619
use substrate_fixed::types::I96F32;
@@ -540,10 +543,10 @@ fn test_sudo_set_max_allowed_uids() {
540543
Error::<Test>::MaxAllowedUidsGreaterThanDefaultMaxAllowedUids
541544
);
542545

543-
// Chain bloat check against mechanism count
544-
// Set MechanismCountCurrent to exceed 256 / DefaultMaxAllowedUids (16)
546+
// Trying to set max allowed uids that would cause max_allowed_uids * mechanism_count > 256
547+
MaxAllowedUids::<Test>::insert(netuid, 8);
545548
MechanismCountCurrent::<Test>::insert(netuid, MechId::from(32));
546-
let large_max_uids = 16_u16;
549+
let large_max_uids = 16;
547550
assert_noop!(
548551
AdminUtils::sudo_set_max_allowed_uids(
549552
<<Test as Config>::RuntimeOrigin>::root(),
@@ -2888,3 +2891,32 @@ fn test_sudo_set_min_allowed_uids() {
28882891
);
28892892
});
28902893
}
2894+
2895+
#[test]
2896+
fn test_sudo_set_max_mechanism_count() {
2897+
new_test_ext().execute_with(|| {
2898+
// Normal case
2899+
assert_ok!(AdminUtils::sudo_set_max_mechanism_count(
2900+
<<Test as Config>::RuntimeOrigin>::root(),
2901+
MechId::from(10)
2902+
));
2903+
2904+
// Zero fails
2905+
assert_noop!(
2906+
AdminUtils::sudo_set_max_mechanism_count(
2907+
<<Test as Config>::RuntimeOrigin>::root(),
2908+
MechId::from(0)
2909+
),
2910+
pallet_subtensor::Error::<Test>::InvalidValue
2911+
);
2912+
2913+
// Over max bound fails
2914+
assert_noop!(
2915+
AdminUtils::sudo_set_max_mechanism_count(
2916+
<<Test as Config>::RuntimeOrigin>::root(),
2917+
MechId::from(MAX_MECHANISM_COUNT_PER_SUBNET + 1)
2918+
),
2919+
pallet_subtensor::Error::<Test>::InvalidValue
2920+
);
2921+
});
2922+
}

pallets/subtensor/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,15 +2310,17 @@ pub mod pallet {
23102310
MechId::from(1)
23112311
}
23122312

2313+
/// -- ITEM (Maximum number of mechanisms)
23132314
#[pallet::type_value]
2314-
/// -- ITEM (Maximum number of sub-subnets)
23152315
pub fn DefaultMaxMechanismCount<T: Config>() -> MechId {
23162316
MechId::from(2)
23172317
}
2318-
#[pallet::storage]
2318+
23192319
/// ITEM( max_mechanism_count )
2320+
#[pallet::storage]
23202321
pub type MaxMechanismCount<T> =
23212322
StorageValue<_, MechId, ValueQuery, DefaultMaxMechanismCount<T>>;
2323+
23222324
/// -- ITEM (Rate limit for mechanism count updates)
23232325
#[pallet::type_value]
23242326
pub fn MechanismCountSetRateLimit<T: Config>() -> u64 {

pallets/subtensor/src/subnets/mechanism.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,18 @@ impl<T: Config> Pallet<T> {
9595
Ok(())
9696
}
9797

98-
pub fn check_max_uids_vs_mechanism_count(
98+
pub fn ensure_max_uids_over_all_mechanisms(
9999
max_uids: u16,
100100
mechanism_count: MechId,
101101
) -> DispatchResult {
102-
let max_uids_for_one_mechanism = 256_u16;
103-
let max_uids_for_mechanism_count =
104-
max_uids_for_one_mechanism.safe_div(u8::from(mechanism_count) as u16);
105102
ensure!(
106-
max_uids_for_mechanism_count >= max_uids,
103+
max_uids * u8::from(mechanism_count) as u16 <= 256,
107104
Error::<T>::TooManyUIDsPerMechanism
108105
);
109106
Ok(())
110107
}
111108

112-
/// Set the desired value of sub-subnet count for a subnet identified
109+
/// Set the desired value of mechanism count for a subnet identified
113110
/// by netuid
114111
pub fn do_set_mechanism_count(netuid: NetUid, mechanism_count: MechId) -> DispatchResult {
115112
// Make sure the subnet exists
@@ -129,7 +126,7 @@ impl<T: Config> Pallet<T> {
129126

130127
// Prevent chain bloat: Require max UIDs to be limited
131128
let max_uids = MaxAllowedUids::<T>::get(netuid);
132-
Self::check_max_uids_vs_mechanism_count(max_uids, mechanism_count)?;
129+
Self::ensure_max_uids_over_all_mechanisms(max_uids, mechanism_count)?;
133130

134131
// Make sure we are not allowing numbers that will break the math
135132
ensure!(

0 commit comments

Comments
 (0)