Skip to content

Commit 5ef64f8

Browse files
authored
Merge pull request #2088 from opentensor/feat/untrim-uids
"Untrimming" of UIDs
2 parents 2f41708 + 45ecced commit 5ef64f8

File tree

7 files changed

+107
-63
lines changed

7 files changed

+107
-63
lines changed

evm-tests/src/subtensor.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -233,24 +233,6 @@ export async function setActivityCutoff(api: TypedApi<typeof devnet>, netuid: nu
233233
assert.equal(activityCutoff, await api.query.SubtensorModule.ActivityCutoff.getValue(netuid))
234234
}
235235

236-
export async function setMaxAllowedUids(api: TypedApi<typeof devnet>, netuid: number, maxAllowedUids: number) {
237-
const value = await api.query.SubtensorModule.MaxAllowedUids.getValue(netuid)
238-
if (value === maxAllowedUids) {
239-
return;
240-
}
241-
242-
const alice = getAliceSigner()
243-
244-
const internalCall = api.tx.AdminUtils.sudo_set_max_allowed_uids({
245-
netuid: netuid,
246-
max_allowed_uids: maxAllowedUids
247-
})
248-
const tx = api.tx.Sudo.sudo({ call: internalCall.decodedCall })
249-
250-
await waitForTransactionWithRetry(api, tx, alice)
251-
assert.equal(maxAllowedUids, await api.query.SubtensorModule.MaxAllowedUids.getValue(netuid))
252-
}
253-
254236
export async function setMinDelegateTake(api: TypedApi<typeof devnet>, minDelegateTake: number) {
255237
const value = await api.query.SubtensorModule.MinDelegateTake.getValue()
256238
if (value === minDelegateTake) {

evm-tests/test/staking.precompile.reward.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { convertPublicKeyToSs58 } from "../src/address-utils"
66
import { tao } from "../src/balance-math"
77
import {
88
forceSetBalanceToSs58Address, addNewSubnetwork, burnedRegister,
9-
setTxRateLimit, setTempo, setWeightsSetRateLimit, setSubnetOwnerCut, setMaxAllowedUids,
9+
setTxRateLimit, setTempo, setWeightsSetRateLimit, setSubnetOwnerCut,
1010
setMinDelegateTake, setActivityCutoff, addStake, setWeight, rootRegister,
1111
startCall,
1212
disableAdminFreezeWindowAndOwnerHyperparamRateLimit
@@ -52,7 +52,6 @@ describe("Test neuron precompile reward", () => {
5252
await burnedRegister(api, netuid, convertPublicKeyToSs58(nominator.publicKey), coldkey)
5353
await setSubnetOwnerCut(api, 0)
5454
await setActivityCutoff(api, netuid, 65535)
55-
await setMaxAllowedUids(api, netuid, 65535)
5655
await setMinDelegateTake(api, 0)
5756
})
5857

pallets/admin-utils/src/benchmarking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ mod benchmarks {
263263
);
264264

265265
#[extrinsic_call]
266-
_(RawOrigin::Root, 1u16.into()/*netuid*/, 4097u16/*max_allowed_uids*/)/*sudo_set_max_allowed_uids*/;
266+
_(RawOrigin::Root, 1u16.into()/*netuid*/, 2048u16/*max_allowed_uids*/)/*sudo_set_max_allowed_uids*/;
267267
}
268268

269269
#[benchmark]

pallets/admin-utils/src/lib.rs

Lines changed: 31 additions & 7 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,27 +524,44 @@ 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)]
523-
#[pallet::weight(Weight::from_parts(18_800_000, 0)
524-
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(2_u64))
530+
#[pallet::weight(Weight::from_parts(32_140_000, 0)
531+
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(5_u64))
525532
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
526533
pub fn sudo_set_max_allowed_uids(
527534
origin: OriginFor<T>,
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
);
536547
ensure!(
537-
pallet_subtensor::Pallet::<T>::get_subnetwork_n(netuid) < max_allowed_uids,
548+
max_allowed_uids >= pallet_subtensor::Pallet::<T>::get_min_allowed_uids(netuid),
549+
Error::<T>::MaxAllowedUidsLessThanMinAllowedUids
550+
);
551+
ensure!(
552+
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
);
@@ -813,7 +837,7 @@ pub mod pallet {
813837
/// It is only callable by the root account or subnet owner.
814838
/// The extrinsic will call the Subtensor pallet to set the difficulty.
815839
#[pallet::call_index(24)]
816-
#[pallet::weight(Weight::from_parts(26_230_000, 0)
840+
#[pallet::weight(Weight::from_parts(38_500_000, 0)
817841
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(3_u64))
818842
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
819843
pub fn sudo_set_difficulty(

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: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -513,69 +513,107 @@ 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
567564
);
568-
assert_eq!(SubtensorModule::get_max_allowed_uids(netuid), init_value);
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
574+
);
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
));
582+
assert_eq!(SubtensorModule::get_max_allowed_uids(netuid), to_be_set);
583+
584+
// Exact current case
585+
assert_ok!(AdminUtils::sudo_set_max_allowed_uids(
586+
<<Test as Config>::RuntimeOrigin>::root(),
587+
netuid,
588+
SubtensorModule::get_subnetwork_n(netuid)
589+
));
590+
assert_eq!(
591+
SubtensorModule::get_max_allowed_uids(netuid),
592+
SubtensorModule::get_subnetwork_n(netuid)
593+
);
594+
595+
// Lower bound case
596+
SubtensorModule::set_min_allowed_uids(netuid, SubtensorModule::get_subnetwork_n(netuid));
574597
assert_ok!(AdminUtils::sudo_set_max_allowed_uids(
575598
<<Test as Config>::RuntimeOrigin>::root(),
576599
netuid,
577-
to_be_set - 1
600+
SubtensorModule::get_min_allowed_uids(netuid)
578601
));
602+
assert_eq!(
603+
SubtensorModule::get_max_allowed_uids(netuid),
604+
SubtensorModule::get_min_allowed_uids(netuid)
605+
);
606+
607+
// Upper bound case
608+
assert_ok!(AdminUtils::sudo_set_max_allowed_uids(
609+
<<Test as Config>::RuntimeOrigin>::root(),
610+
netuid,
611+
DefaultMaxAllowedUids::<Test>::get(),
612+
));
613+
assert_eq!(
614+
SubtensorModule::get_max_allowed_uids(netuid),
615+
DefaultMaxAllowedUids::<Test>::get()
616+
);
579617
});
580618
}
581619

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)