Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions evm-tests/src/subtensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,24 +233,6 @@ export async function setActivityCutoff(api: TypedApi<typeof devnet>, netuid: nu
assert.equal(activityCutoff, await api.query.SubtensorModule.ActivityCutoff.getValue(netuid))
}

export async function setMaxAllowedUids(api: TypedApi<typeof devnet>, netuid: number, maxAllowedUids: number) {
const value = await api.query.SubtensorModule.MaxAllowedUids.getValue(netuid)
if (value === maxAllowedUids) {
return;
}

const alice = getAliceSigner()

const internalCall = api.tx.AdminUtils.sudo_set_max_allowed_uids({
netuid: netuid,
max_allowed_uids: maxAllowedUids
})
const tx = api.tx.Sudo.sudo({ call: internalCall.decodedCall })

await waitForTransactionWithRetry(api, tx, alice)
assert.equal(maxAllowedUids, await api.query.SubtensorModule.MaxAllowedUids.getValue(netuid))
}

export async function setMinDelegateTake(api: TypedApi<typeof devnet>, minDelegateTake: number) {
const value = await api.query.SubtensorModule.MinDelegateTake.getValue()
if (value === minDelegateTake) {
Expand Down
3 changes: 1 addition & 2 deletions evm-tests/test/staking.precompile.reward.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { convertPublicKeyToSs58 } from "../src/address-utils"
import { tao } from "../src/balance-math"
import {
forceSetBalanceToSs58Address, addNewSubnetwork, burnedRegister,
setTxRateLimit, setTempo, setWeightsSetRateLimit, setSubnetOwnerCut, setMaxAllowedUids,
setTxRateLimit, setTempo, setWeightsSetRateLimit, setSubnetOwnerCut,
setMinDelegateTake, setActivityCutoff, addStake, setWeight, rootRegister,
startCall,
disableAdminFreezeWindowAndOwnerHyperparamRateLimit
Expand Down Expand Up @@ -52,7 +52,6 @@ describe("Test neuron precompile reward", () => {
await burnedRegister(api, netuid, convertPublicKeyToSs58(nominator.publicKey), coldkey)
await setSubnetOwnerCut(api, 0)
await setActivityCutoff(api, netuid, 65535)
await setMaxAllowedUids(api, netuid, 65535)
await setMinDelegateTake(api, 0)
})

Expand Down
2 changes: 1 addition & 1 deletion pallets/admin-utils/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ mod benchmarks {
);

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

#[benchmark]
Expand Down
38 changes: 31 additions & 7 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ pub mod pallet {
use frame_support::{dispatch::DispatchResult, pallet_prelude::StorageMap};
use frame_system::pallet_prelude::*;
use pallet_evm_chain_id::{self, ChainId};
use pallet_subtensor::utils::rate_limiting::{Hyperparameter, TransactionType};
use pallet_subtensor::{
DefaultMaxAllowedUids,
utils::rate_limiting::{Hyperparameter, TransactionType},
};
use sp_runtime::BoundedVec;
use substrate_fixed::types::I96F32;
use subtensor_runtime_common::{MechId, NetUid, TaoCurrency};
Expand Down Expand Up @@ -110,6 +113,10 @@ pub mod pallet {
MinAllowedUidsGreaterThanCurrentUids,
/// The minimum allowed UIDs must be less than the maximum allowed UIDs.
MinAllowedUidsGreaterThanMaxAllowedUids,
/// The maximum allowed UIDs must be greater than the minimum allowed UIDs.
MaxAllowedUidsLessThanMinAllowedUids,
/// The maximum allowed UIDs must be less than the default maximum allowed UIDs.
MaxAllowedUidsGreaterThanDefaultMaxAllowedUids,
}
/// Enum for specifying the type of precompile operation.
#[derive(
Expand Down Expand Up @@ -517,27 +524,44 @@ pub mod pallet {
}

/// The extrinsic sets the maximum allowed UIDs for a subnet.
/// It is only callable by the root account.
/// It is only callable by the root account and subnet owner.
/// The extrinsic will call the Subtensor pallet to set the maximum allowed UIDs for a subnet.
#[pallet::call_index(15)]
#[pallet::weight(Weight::from_parts(18_800_000, 0)
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(2_u64))
#[pallet::weight(Weight::from_parts(32_140_000, 0)
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(5_u64))
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
pub fn sudo_set_max_allowed_uids(
origin: OriginFor<T>,
netuid: NetUid,
max_allowed_uids: u16,
) -> DispatchResult {
ensure_root(origin)?;
let maybe_owner = pallet_subtensor::Pallet::<T>::ensure_sn_owner_or_root_with_limits(
origin,
netuid,
&[Hyperparameter::MaxAllowedUids.into()],
)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
ensure!(
pallet_subtensor::Pallet::<T>::get_subnetwork_n(netuid) < max_allowed_uids,
max_allowed_uids >= pallet_subtensor::Pallet::<T>::get_min_allowed_uids(netuid),
Error::<T>::MaxAllowedUidsLessThanMinAllowedUids
);
ensure!(
pallet_subtensor::Pallet::<T>::get_subnetwork_n(netuid) <= max_allowed_uids,
Error::<T>::MaxAllowedUIdsLessThanCurrentUIds
);
ensure!(
max_allowed_uids <= DefaultMaxAllowedUids::<T>::get(),
Error::<T>::MaxAllowedUidsGreaterThanDefaultMaxAllowedUids
);
pallet_subtensor::Pallet::<T>::set_max_allowed_uids(netuid, max_allowed_uids);
pallet_subtensor::Pallet::<T>::record_owner_rl(
maybe_owner,
netuid,
&[Hyperparameter::MaxAllowedUids.into()],
);
log::debug!(
"MaxAllowedUidsSet( netuid: {netuid:?} max_allowed_uids: {max_allowed_uids:?} ) "
);
Expand Down Expand Up @@ -813,7 +837,7 @@ pub mod pallet {
/// It is only callable by the root account or subnet owner.
/// The extrinsic will call the Subtensor pallet to set the difficulty.
#[pallet::call_index(24)]
#[pallet::weight(Weight::from_parts(26_230_000, 0)
#[pallet::weight(Weight::from_parts(38_500_000, 0)
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(3_u64))
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
pub fn sudo_set_difficulty(
Expand Down
2 changes: 1 addition & 1 deletion pallets/admin-utils/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ parameter_types! {
pub const SelfOwnership: u64 = 2;
pub const InitialImmunityPeriod: u16 = 2;
pub const InitialMinAllowedUids: u16 = 2;
pub const InitialMaxAllowedUids: u16 = 4;
pub const InitialMaxAllowedUids: u16 = 16;
pub const InitialBondsMovingAverage: u64 = 900_000;
pub const InitialBondsPenalty: u16 = u16::MAX;
pub const InitialBondsResetOn: bool = false;
Expand Down
106 changes: 72 additions & 34 deletions pallets/admin-utils/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,69 +513,107 @@ fn test_sudo_set_min_allowed_weights() {
fn test_sudo_set_max_allowed_uids() {
new_test_ext().execute_with(|| {
let netuid = NetUid::from(1);
let to_be_set: u16 = 10;
let to_be_set: u16 = 12;
add_network(netuid, 10);
let init_value: u16 = SubtensorModule::get_max_allowed_uids(netuid);
assert_eq!(
MaxRegistrationsPerBlock::<Test>::insert(netuid, 256);
TargetRegistrationsPerInterval::<Test>::insert(netuid, 256);

// Register some neurons
for i in 0..=8 {
register_ok_neuron(netuid, U256::from(i * 1000), U256::from(i * 1000 + i), 0);
}

// Bad origin that is not root or subnet owner
assert_noop!(
AdminUtils::sudo_set_max_allowed_uids(
<<Test as Config>::RuntimeOrigin>::signed(U256::from(0)),
<<Test as Config>::RuntimeOrigin>::signed(U256::from(42)),
netuid,
to_be_set
),
Err(DispatchError::BadOrigin)
DispatchError::BadOrigin
);
assert_eq!(

// Random netuid that doesn't exist
assert_noop!(
AdminUtils::sudo_set_max_allowed_uids(
<<Test as Config>::RuntimeOrigin>::root(),
netuid.next(),
NetUid::from(42),
to_be_set
),
Err(Error::<Test>::SubnetDoesNotExist.into())
Error::<Test>::SubnetDoesNotExist
);
assert_eq!(SubtensorModule::get_max_allowed_uids(netuid), init_value);
assert_ok!(AdminUtils::sudo_set_max_allowed_uids(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
));
assert_eq!(SubtensorModule::get_max_allowed_uids(netuid), to_be_set);
});
}

#[test]
fn test_sudo_set_and_decrease_max_allowed_uids() {
new_test_ext().execute_with(|| {
let netuid = NetUid::from(1);
let to_be_set: u16 = 10;
add_network(netuid, 10);
let init_value: u16 = SubtensorModule::get_max_allowed_uids(netuid);
assert_eq!(
// Trying to set max allowed uids less than min allowed uids
assert_noop!(
AdminUtils::sudo_set_max_allowed_uids(
<<Test as Config>::RuntimeOrigin>::signed(U256::from(0)),
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
SubtensorModule::get_min_allowed_uids(netuid) - 1
),
Err(DispatchError::BadOrigin)
Error::<Test>::MaxAllowedUidsLessThanMinAllowedUids
);
assert_eq!(

// Trying to set max allowed uids less than current uids
assert_noop!(
AdminUtils::sudo_set_max_allowed_uids(
<<Test as Config>::RuntimeOrigin>::root(),
netuid.next(),
to_be_set
netuid,
SubtensorModule::get_subnetwork_n(netuid) - 1
),
Err(Error::<Test>::SubnetDoesNotExist.into())
Error::<Test>::MaxAllowedUIdsLessThanCurrentUIds
);
assert_eq!(SubtensorModule::get_max_allowed_uids(netuid), init_value);

// Trying to set max allowed uids greater than default max allowed uids
assert_noop!(
AdminUtils::sudo_set_max_allowed_uids(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
DefaultMaxAllowedUids::<Test>::get() + 1
),
Error::<Test>::MaxAllowedUidsGreaterThanDefaultMaxAllowedUids
);

// Normal case
assert_ok!(AdminUtils::sudo_set_max_allowed_uids(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
));
assert_eq!(SubtensorModule::get_max_allowed_uids(netuid), to_be_set);

// Exact current case
assert_ok!(AdminUtils::sudo_set_max_allowed_uids(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
SubtensorModule::get_subnetwork_n(netuid)
));
assert_eq!(
SubtensorModule::get_max_allowed_uids(netuid),
SubtensorModule::get_subnetwork_n(netuid)
);

// Lower bound case
SubtensorModule::set_min_allowed_uids(netuid, SubtensorModule::get_subnetwork_n(netuid));
assert_ok!(AdminUtils::sudo_set_max_allowed_uids(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set - 1
SubtensorModule::get_min_allowed_uids(netuid)
));
assert_eq!(
SubtensorModule::get_max_allowed_uids(netuid),
SubtensorModule::get_min_allowed_uids(netuid)
);

// Upper bound case
assert_ok!(AdminUtils::sudo_set_max_allowed_uids(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
DefaultMaxAllowedUids::<Test>::get(),
));
assert_eq!(
SubtensorModule::get_max_allowed_uids(netuid),
DefaultMaxAllowedUids::<Test>::get()
);
});
}

Expand Down
1 change: 1 addition & 0 deletions pallets/subtensor/src/utils/rate_limiting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub enum Hyperparameter {
BondsResetEnabled = 22,
ImmuneNeuronLimit = 23,
RecycleOrBurn = 24,
MaxAllowedUids = 25,
}

impl<T: Config> Pallet<T> {
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 323,
spec_version: 324,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading