Skip to content

Commit ece47af

Browse files
authored
Merge pull request #1998 from opentensor/trim_uids
Trim uids
2 parents 01c90a7 + d794795 commit ece47af

File tree

15 files changed

+819
-37
lines changed

15 files changed

+819
-37
lines changed

pallets/admin-utils/src/benchmarking.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use frame_benchmarking::v1::account;
1111
use frame_benchmarking::v2::*;
1212
use frame_support::BoundedVec;
1313
use frame_system::RawOrigin;
14+
use pallet_subtensor::SubnetworkN;
15+
use subtensor_runtime_common::NetUid;
1416

1517
use super::*;
1618

@@ -238,6 +240,19 @@ mod benchmarks {
238240
_(RawOrigin::Root, 1u16.into()/*netuid*/, 3u16/*kappa*/)/*set_kappa*/;
239241
}
240242

243+
#[benchmark]
244+
fn sudo_set_min_allowed_uids() {
245+
let netuid = NetUid::from(1);
246+
pallet_subtensor::Pallet::<T>::set_admin_freeze_window(0);
247+
pallet_subtensor::Pallet::<T>::init_new_network(netuid, 1u16 /*tempo*/);
248+
249+
// Artificially set that some neurons are already registered
250+
SubnetworkN::<T>::set(netuid, 32);
251+
252+
#[extrinsic_call]
253+
_(RawOrigin::Root, netuid, 16u16/*min_allowed_uids*/)/*sudo_set_min_allowed_uids*/;
254+
}
255+
241256
#[benchmark]
242257
fn sudo_set_max_allowed_uids() {
243258
// disable admin freeze window
@@ -432,5 +447,17 @@ mod benchmarks {
432447
_(RawOrigin::Root, 1u16.into()/*netuid*/, 5u16/*immune_neurons*/)/*sudo_set_owner_immune_neuron_limit()*/;
433448
}
434449

450+
#[benchmark]
451+
fn sudo_trim_to_max_allowed_uids() {
452+
pallet_subtensor::Pallet::<T>::set_admin_freeze_window(0);
453+
pallet_subtensor::Pallet::<T>::init_new_network(
454+
1u16.into(), /*netuid*/
455+
1u16, /*sudo_tempo*/
456+
);
457+
458+
#[extrinsic_call]
459+
_(RawOrigin::Root, 1u16.into()/*netuid*/, 256u16/*max_n*/)/*sudo_trim_to_max_allowed_uids()*/;
460+
}
461+
435462
//impl_benchmark_test_suite!(AdminUtils, crate::mock::new_test_ext(), crate::mock::Test);
436463
}

pallets/admin-utils/src/lib.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ pub mod pallet {
106106
NegativeSigmoidSteepness,
107107
/// Value not in allowed bounds.
108108
ValueNotInBounds,
109+
/// The minimum allowed UIDs must be less than the current number of UIDs in the subnet.
110+
MinAllowedUidsGreaterThanCurrentUids,
111+
/// The minimum allowed UIDs must be less than the maximum allowed UIDs.
112+
MinAllowedUidsGreaterThanMaxAllowedUids,
109113
}
110114
/// Enum for specifying the type of precompile operation.
111115
#[derive(
@@ -1902,6 +1906,70 @@ pub mod pallet {
19021906
);
19031907
Ok(())
19041908
}
1909+
1910+
/// Trims the maximum number of UIDs for a subnet.
1911+
///
1912+
/// The trimming is done by sorting the UIDs by emission descending and then trimming
1913+
/// the lowest emitters while preserving temporally and owner immune UIDs. The UIDs are
1914+
/// then compressed to the left and storage is migrated to the new compressed UIDs.
1915+
#[pallet::call_index(78)]
1916+
#[pallet::weight(Weight::from_parts(32_880_000, 0)
1917+
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(6_u64))
1918+
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
1919+
pub fn sudo_trim_to_max_allowed_uids(
1920+
origin: OriginFor<T>,
1921+
netuid: NetUid,
1922+
max_n: u16,
1923+
) -> DispatchResult {
1924+
let maybe_owner = pallet_subtensor::Pallet::<T>::ensure_sn_owner_or_root_with_limits(
1925+
origin.clone(),
1926+
netuid,
1927+
&[TransactionType::MaxUidsTrimming],
1928+
)?;
1929+
1930+
pallet_subtensor::Pallet::<T>::trim_to_max_allowed_uids(netuid, max_n)?;
1931+
1932+
pallet_subtensor::Pallet::<T>::record_owner_rl(
1933+
maybe_owner,
1934+
netuid,
1935+
&[TransactionType::MaxUidsTrimming],
1936+
);
1937+
Ok(())
1938+
}
1939+
1940+
/// The extrinsic sets the minimum allowed UIDs for a subnet.
1941+
/// It is only callable by the root account.
1942+
#[pallet::call_index(79)]
1943+
#[pallet::weight(Weight::from_parts(31_550_000, 0)
1944+
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(5_u64))
1945+
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
1946+
pub fn sudo_set_min_allowed_uids(
1947+
origin: OriginFor<T>,
1948+
netuid: NetUid,
1949+
min_allowed_uids: u16,
1950+
) -> DispatchResult {
1951+
pallet_subtensor::Pallet::<T>::ensure_root_with_rate_limit(origin, netuid)?;
1952+
1953+
ensure!(
1954+
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
1955+
Error::<T>::SubnetDoesNotExist
1956+
);
1957+
ensure!(
1958+
min_allowed_uids < pallet_subtensor::Pallet::<T>::get_max_allowed_uids(netuid),
1959+
Error::<T>::MinAllowedUidsGreaterThanMaxAllowedUids
1960+
);
1961+
ensure!(
1962+
min_allowed_uids < pallet_subtensor::Pallet::<T>::get_subnetwork_n(netuid),
1963+
Error::<T>::MinAllowedUidsGreaterThanCurrentUids
1964+
);
1965+
1966+
pallet_subtensor::Pallet::<T>::set_min_allowed_uids(netuid, min_allowed_uids);
1967+
1968+
log::debug!(
1969+
"MinAllowedUidsSet( netuid: {netuid:?} min_allowed_uids: {min_allowed_uids:?} ) "
1970+
);
1971+
Ok(())
1972+
}
19051973
}
19061974
}
19071975

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use sp_consensus_grandpa::AuthorityList as GrandpaAuthorityList;
1313
use sp_core::U256;
1414
use sp_core::{ConstU64, H256};
1515
use sp_runtime::{
16-
BuildStorage, KeyTypeId, Perbill,
16+
BuildStorage, KeyTypeId, Perbill, Percent,
1717
testing::TestXt,
1818
traits::{BlakeTwo256, ConstU32, IdentityLookup},
1919
};
@@ -91,7 +91,8 @@ parameter_types! {
9191
pub const InitialTempo: u16 = 0;
9292
pub const SelfOwnership: u64 = 2;
9393
pub const InitialImmunityPeriod: u16 = 2;
94-
pub const InitialMaxAllowedUids: u16 = 2;
94+
pub const InitialMinAllowedUids: u16 = 2;
95+
pub const InitialMaxAllowedUids: u16 = 4;
9596
pub const InitialBondsMovingAverage: u64 = 900_000;
9697
pub const InitialBondsPenalty: u16 = u16::MAX;
9798
pub const InitialBondsResetOn: bool = false;
@@ -129,7 +130,6 @@ parameter_types! {
129130
pub const InitialRAORecycledForRegistration: u64 = 0;
130131
pub const InitialSenateRequiredStakePercentage: u64 = 2; // 2 percent of total stake
131132
pub const InitialNetworkImmunityPeriod: u64 = 7200 * 7;
132-
pub const InitialNetworkMinAllowedUids: u16 = 128;
133133
pub const InitialNetworkMinLockCost: u64 = 100_000_000_000;
134134
pub const InitialSubnetOwnerCut: u16 = 0; // 0%. 100% of rewards go to validators + miners.
135135
pub const InitialNetworkLockReductionInterval: u64 = 2; // 2 blocks.
@@ -151,6 +151,7 @@ parameter_types! {
151151
pub const InitialKeySwapOnSubnetCost: u64 = 10_000_000;
152152
pub const HotkeySwapOnSubnetInterval: u64 = 7 * 24 * 60 * 60 / 12; // 7 days
153153
pub const LeaseDividendsDistributionInterval: u32 = 100; // 100 blocks
154+
pub const MaxImmuneUidsPercentage: Percent = Percent::from_percent(80);
154155
}
155156

156157
impl pallet_subtensor::Config for Test {
@@ -174,6 +175,7 @@ impl pallet_subtensor::Config for Test {
174175
type InitialRho = InitialRho;
175176
type InitialAlphaSigmoidSteepness = InitialAlphaSigmoidSteepness;
176177
type InitialKappa = InitialKappa;
178+
type InitialMinAllowedUids = InitialMinAllowedUids;
177179
type InitialMaxAllowedUids = InitialMaxAllowedUids;
178180
type InitialValidatorPruneLen = InitialValidatorPruneLen;
179181
type InitialScalingLawPower = InitialScalingLawPower;
@@ -205,7 +207,6 @@ impl pallet_subtensor::Config for Test {
205207
type InitialRAORecycledForRegistration = InitialRAORecycledForRegistration;
206208
type InitialSenateRequiredStakePercentage = InitialSenateRequiredStakePercentage;
207209
type InitialNetworkImmunityPeriod = InitialNetworkImmunityPeriod;
208-
type InitialNetworkMinAllowedUids = InitialNetworkMinAllowedUids;
209210
type InitialNetworkMinLockCost = InitialNetworkMinLockCost;
210211
type InitialSubnetOwnerCut = InitialSubnetOwnerCut;
211212
type InitialNetworkLockReductionInterval = InitialNetworkLockReductionInterval;
@@ -228,6 +229,7 @@ impl pallet_subtensor::Config for Test {
228229
type ProxyInterface = ();
229230
type LeaseDividendsDistributionInterval = LeaseDividendsDistributionInterval;
230231
type GetCommitments = ();
232+
type MaxImmuneUidsPercentage = MaxImmuneUidsPercentage;
231233
}
232234

233235
parameter_types! {

0 commit comments

Comments
 (0)