Skip to content

Commit 826a446

Browse files
committed
refactor set max allowed uids
1 parent 40945a9 commit 826a446

File tree

4 files changed

+71
-42
lines changed

4 files changed

+71
-42
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ pub mod pallet {
2525
use frame_support::{dispatch::DispatchResult, pallet_prelude::StorageMap};
2626
use frame_system::pallet_prelude::*;
2727
use pallet_evm_chain_id::{self, ChainId};
28-
use pallet_subtensor::utils::rate_limiting::{Hyperparameter, TransactionType};
28+
use pallet_subtensor::{
29+
DefaultMaxAllowedUids,
30+
utils::rate_limiting::{Hyperparameter, TransactionType},
31+
};
2932
use sp_runtime::BoundedVec;
3033
use substrate_fixed::types::I96F32;
3134
use subtensor_runtime_common::{MechId, NetUid, TaoCurrency};
@@ -110,6 +113,10 @@ pub mod pallet {
110113
MinAllowedUidsGreaterThanCurrentUids,
111114
/// The minimum allowed UIDs must be less than the maximum allowed UIDs.
112115
MinAllowedUidsGreaterThanMaxAllowedUids,
116+
/// The maximum allowed UIDs must be greater than the minimum allowed UIDs.
117+
MaxAllowedUidsLessThanMinAllowedUids,
118+
/// The maximum allowed UIDs must be less than the default maximum allowed UIDs.
119+
MaxAllowedUidsGreaterThanDefaultMaxAllowedUids,
113120
}
114121
/// Enum for specifying the type of precompile operation.
115122
#[derive(
@@ -517,7 +524,7 @@ pub mod pallet {
517524
}
518525

519526
/// The extrinsic sets the maximum allowed UIDs for a subnet.
520-
/// It is only callable by the root account.
527+
/// It is only callable by the root account and subnet owner.
521528
/// The extrinsic will call the Subtensor pallet to set the maximum allowed UIDs for a subnet.
522529
#[pallet::call_index(15)]
523530
#[pallet::weight(Weight::from_parts(18_800_000, 0)
@@ -528,16 +535,33 @@ pub mod pallet {
528535
netuid: NetUid,
529536
max_allowed_uids: u16,
530537
) -> DispatchResult {
531-
ensure_root(origin)?;
538+
let maybe_owner = pallet_subtensor::Pallet::<T>::ensure_sn_owner_or_root_with_limits(
539+
origin,
540+
netuid,
541+
&[Hyperparameter::MaxAllowedUids.into()],
542+
)?;
532543
ensure!(
533544
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
534545
Error::<T>::SubnetDoesNotExist
535546
);
547+
ensure!(
548+
max_allowed_uids > pallet_subtensor::Pallet::<T>::get_min_allowed_uids(netuid),
549+
Error::<T>::MaxAllowedUidsLessThanMinAllowedUids
550+
);
536551
ensure!(
537552
pallet_subtensor::Pallet::<T>::get_subnetwork_n(netuid) < max_allowed_uids,
538553
Error::<T>::MaxAllowedUIdsLessThanCurrentUIds
539554
);
555+
ensure!(
556+
max_allowed_uids <= DefaultMaxAllowedUids::<T>::get(),
557+
Error::<T>::MaxAllowedUidsGreaterThanDefaultMaxAllowedUids
558+
);
540559
pallet_subtensor::Pallet::<T>::set_max_allowed_uids(netuid, max_allowed_uids);
560+
pallet_subtensor::Pallet::<T>::record_owner_rl(
561+
maybe_owner,
562+
netuid,
563+
&[Hyperparameter::MaxAllowedUids.into()],
564+
);
541565
log::debug!(
542566
"MaxAllowedUidsSet( netuid: {netuid:?} max_allowed_uids: {max_allowed_uids:?} ) "
543567
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ parameter_types! {
9292
pub const SelfOwnership: u64 = 2;
9393
pub const InitialImmunityPeriod: u16 = 2;
9494
pub const InitialMinAllowedUids: u16 = 2;
95-
pub const InitialMaxAllowedUids: u16 = 4;
95+
pub const InitialMaxAllowedUids: u16 = 16;
9696
pub const InitialBondsMovingAverage: u64 = 900_000;
9797
pub const InitialBondsPenalty: u16 = u16::MAX;
9898
pub const InitialBondsResetOn: bool = false;

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

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -513,69 +513,73 @@ fn test_sudo_set_min_allowed_weights() {
513513
fn test_sudo_set_max_allowed_uids() {
514514
new_test_ext().execute_with(|| {
515515
let netuid = NetUid::from(1);
516-
let to_be_set: u16 = 10;
516+
let to_be_set: u16 = 12;
517517
add_network(netuid, 10);
518-
let init_value: u16 = SubtensorModule::get_max_allowed_uids(netuid);
519-
assert_eq!(
518+
MaxRegistrationsPerBlock::<Test>::insert(netuid, 256);
519+
TargetRegistrationsPerInterval::<Test>::insert(netuid, 256);
520+
521+
// Register some neurons
522+
for i in 0..=8 {
523+
register_ok_neuron(netuid, U256::from(i * 1000), U256::from(i * 1000 + i), 0);
524+
}
525+
526+
// Bad origin that is not root or subnet owner
527+
assert_noop!(
520528
AdminUtils::sudo_set_max_allowed_uids(
521-
<<Test as Config>::RuntimeOrigin>::signed(U256::from(0)),
529+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(42)),
522530
netuid,
523531
to_be_set
524532
),
525-
Err(DispatchError::BadOrigin)
533+
DispatchError::BadOrigin
526534
);
527-
assert_eq!(
535+
536+
// Random netuid that doesn't exist
537+
assert_noop!(
528538
AdminUtils::sudo_set_max_allowed_uids(
529539
<<Test as Config>::RuntimeOrigin>::root(),
530-
netuid.next(),
540+
NetUid::from(42),
531541
to_be_set
532542
),
533-
Err(Error::<Test>::SubnetDoesNotExist.into())
543+
Error::<Test>::SubnetDoesNotExist
534544
);
535-
assert_eq!(SubtensorModule::get_max_allowed_uids(netuid), init_value);
536-
assert_ok!(AdminUtils::sudo_set_max_allowed_uids(
537-
<<Test as Config>::RuntimeOrigin>::root(),
538-
netuid,
539-
to_be_set
540-
));
541-
assert_eq!(SubtensorModule::get_max_allowed_uids(netuid), to_be_set);
542-
});
543-
}
544545

545-
#[test]
546-
fn test_sudo_set_and_decrease_max_allowed_uids() {
547-
new_test_ext().execute_with(|| {
548-
let netuid = NetUid::from(1);
549-
let to_be_set: u16 = 10;
550-
add_network(netuid, 10);
551-
let init_value: u16 = SubtensorModule::get_max_allowed_uids(netuid);
552-
assert_eq!(
546+
// Trying to set max allowed uids less than min allowed uids
547+
assert_noop!(
553548
AdminUtils::sudo_set_max_allowed_uids(
554-
<<Test as Config>::RuntimeOrigin>::signed(U256::from(0)),
549+
<<Test as Config>::RuntimeOrigin>::root(),
555550
netuid,
556-
to_be_set
551+
SubtensorModule::get_min_allowed_uids(netuid) - 1
557552
),
558-
Err(DispatchError::BadOrigin)
553+
Error::<Test>::MaxAllowedUidsLessThanMinAllowedUids
559554
);
560-
assert_eq!(
555+
556+
// Trying to set max allowed uids less than current uids
557+
assert_noop!(
561558
AdminUtils::sudo_set_max_allowed_uids(
562559
<<Test as Config>::RuntimeOrigin>::root(),
563-
netuid.next(),
564-
to_be_set
560+
netuid,
561+
SubtensorModule::get_subnetwork_n(netuid) - 1
565562
),
566-
Err(Error::<Test>::SubnetDoesNotExist.into())
563+
Error::<Test>::MaxAllowedUIdsLessThanCurrentUIds
564+
);
565+
566+
// Trying to set max allowed uids greater than default max allowed uids
567+
assert_noop!(
568+
AdminUtils::sudo_set_max_allowed_uids(
569+
<<Test as Config>::RuntimeOrigin>::root(),
570+
netuid,
571+
DefaultMaxAllowedUids::<Test>::get() + 1
572+
),
573+
Error::<Test>::MaxAllowedUidsGreaterThanDefaultMaxAllowedUids
567574
);
568-
assert_eq!(SubtensorModule::get_max_allowed_uids(netuid), init_value);
575+
576+
// Normal case
569577
assert_ok!(AdminUtils::sudo_set_max_allowed_uids(
570578
<<Test as Config>::RuntimeOrigin>::root(),
571579
netuid,
572580
to_be_set
573581
));
574-
assert_ok!(AdminUtils::sudo_set_max_allowed_uids(
575-
<<Test as Config>::RuntimeOrigin>::root(),
576-
netuid,
577-
to_be_set - 1
578-
));
582+
assert_eq!(SubtensorModule::get_max_allowed_uids(netuid), to_be_set);
579583
});
580584
}
581585

pallets/subtensor/src/utils/rate_limiting.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ pub enum Hyperparameter {
197197
BondsResetEnabled = 22,
198198
ImmuneNeuronLimit = 23,
199199
RecycleOrBurn = 24,
200+
MaxAllowedUids = 25,
200201
}
201202

202203
impl<T: Config> Pallet<T> {

0 commit comments

Comments
 (0)