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
27 changes: 27 additions & 0 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::ServingRateLimit.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
pallet_subtensor::Pallet::<T>::set_serving_rate_limit(netuid, serving_rate_limit);
log::debug!("ServingRateLimitSet( serving_rate_limit: {serving_rate_limit:?} ) ");
pallet_subtensor::Pallet::<T>::record_owner_rl(
Expand Down Expand Up @@ -288,6 +289,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::MaxDifficulty.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Expand Down Expand Up @@ -322,6 +324,7 @@ pub mod pallet {
netuid,
&[TransactionType::SetWeightsVersionKey],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Expand Down Expand Up @@ -413,6 +416,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::AdjustmentAlpha.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Expand Down Expand Up @@ -445,6 +449,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::ImmunityPeriod.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
Expand Down Expand Up @@ -479,6 +484,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::MinAllowedWeights.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Expand Down Expand Up @@ -513,6 +519,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::MaxAllowedUids.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
Expand Down Expand Up @@ -572,6 +579,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::Rho.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Expand Down Expand Up @@ -604,6 +612,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::ActivityCutoff.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Expand Down Expand Up @@ -672,6 +681,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::PowRegistrationAllowed.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

pallet_subtensor::Pallet::<T>::set_network_pow_registration_allowed(
netuid,
Expand Down Expand Up @@ -733,6 +743,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::MinBurn.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
Expand Down Expand Up @@ -773,6 +784,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::MaxBurn.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
Expand Down Expand Up @@ -868,6 +880,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::BondsMovingAverage.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
if maybe_owner.is_some() {
ensure!(
bonds_moving_average <= 975000,
Expand Down Expand Up @@ -908,6 +921,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::BondsPenalty.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Expand Down Expand Up @@ -1245,6 +1259,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::CommitRevealEnabled.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Expand Down Expand Up @@ -1288,6 +1303,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::LiquidAlphaEnabled.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
pallet_subtensor::Pallet::<T>::set_liquid_alpha_enabled(netuid, enabled);
log::debug!("LiquidAlphaEnableToggled( netuid: {netuid:?}, Enabled: {enabled:?} ) ");
pallet_subtensor::Pallet::<T>::record_owner_rl(
Expand Down Expand Up @@ -1318,6 +1334,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::AlphaValues.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
let res = pallet_subtensor::Pallet::<T>::do_set_alpha_values(
origin, netuid, alpha_low, alpha_high,
);
Expand Down Expand Up @@ -1437,6 +1454,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::WeightCommitInterval.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Expand Down Expand Up @@ -1535,6 +1553,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::TransferEnabled.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
let res = pallet_subtensor::Pallet::<T>::toggle_transfer(netuid, toggle);
if res.is_ok() {
pallet_subtensor::Pallet::<T>::record_owner_rl(
Expand Down Expand Up @@ -1567,6 +1586,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::RecycleOrBurn.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

pallet_subtensor::Pallet::<T>::set_recycle_or_burn(netuid, recycle_or_burn);
pallet_subtensor::Pallet::<T>::record_owner_rl(
Expand Down Expand Up @@ -1734,6 +1754,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::AlphaSigmoidSteepness.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Expand Down Expand Up @@ -1784,6 +1805,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::Yuma3Enabled.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
pallet_subtensor::Pallet::<T>::set_yuma3_enabled(netuid, enabled);

Self::deposit_event(Event::Yuma3EnableToggled { netuid, enabled });
Expand Down Expand Up @@ -1823,6 +1845,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::BondsResetEnabled.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
pallet_subtensor::Pallet::<T>::set_bonds_reset(netuid, enabled);

Self::deposit_event(Event::BondsResetToggled { netuid, enabled });
Expand Down Expand Up @@ -1948,6 +1971,7 @@ pub mod pallet {
netuid,
&[Hyperparameter::ImmuneNeuronLimit.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
pallet_subtensor::Pallet::<T>::set_owner_immune_neuron_limit(netuid, immune_neurons)?;
pallet_subtensor::Pallet::<T>::record_owner_rl(
maybe_owner,
Expand Down Expand Up @@ -2021,6 +2045,7 @@ pub mod pallet {
netuid,
&[TransactionType::MechanismCountUpdate],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

pallet_subtensor::Pallet::<T>::do_set_mechanism_count(netuid, mechanism_count)?;

Expand All @@ -2047,6 +2072,7 @@ pub mod pallet {
netuid,
&[TransactionType::MechanismEmission],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

pallet_subtensor::Pallet::<T>::do_set_emission_split(netuid, maybe_split)?;

Expand Down Expand Up @@ -2077,6 +2103,7 @@ pub mod pallet {
netuid,
&[TransactionType::MaxUidsTrimming],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

pallet_subtensor::Pallet::<T>::trim_to_max_allowed_uids(netuid, max_n)?;

Expand Down
15 changes: 10 additions & 5 deletions pallets/subtensor/src/tests/ensure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ fn ensure_owner_or_root_with_limits_checks_rl_and_freeze() {
)
.expect("should pass");
assert_eq!(res, Some(owner));
assert_ok!(crate::Pallet::<Test>::ensure_admin_window_open(netuid));

// Simulate previous update at current block -> next call should fail due to rate limit
let now = crate::Pallet::<Test>::get_current_block_as_u64();
Expand All @@ -127,11 +128,14 @@ fn ensure_owner_or_root_with_limits_checks_rl_and_freeze() {

// Advance beyond RL and ensure passes again
run_to_block(now + 3);
TransactionType::from(Hyperparameter::Kappa)
.set_last_block_on_subnet::<Test>(&owner, netuid, 0);
assert_ok!(crate::Pallet::<Test>::ensure_sn_owner_or_root_with_limits(
<<Test as Config>::RuntimeOrigin>::signed(owner),
netuid,
&[Hyperparameter::Kappa.into()]
));
assert_ok!(crate::Pallet::<Test>::ensure_admin_window_open(netuid));

// Now advance into the freeze window; ensure blocks
// (using loop for clarity, because epoch calculation function uses netuid)
Expand All @@ -148,12 +152,13 @@ fn ensure_owner_or_root_with_limits_checks_rl_and_freeze() {
}
run_to_block(cur + 1);
}
assert_ok!(crate::Pallet::<Test>::ensure_sn_owner_or_root_with_limits(
<<Test as Config>::RuntimeOrigin>::signed(owner),
netuid,
&[Hyperparameter::Kappa.into()]
));
assert_noop!(
crate::Pallet::<Test>::ensure_sn_owner_or_root_with_limits(
<<Test as Config>::RuntimeOrigin>::signed(owner),
netuid,
&[Hyperparameter::Kappa.into()],
),
crate::Pallet::<Test>::ensure_admin_window_open(netuid),
crate::Error::<Test>::AdminActionProhibitedDuringWeightsWindow
);
});
Expand Down
33 changes: 4 additions & 29 deletions pallets/subtensor/src/utils/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,16 @@ impl<T: Config> Pallet<T> {
netuid: NetUid,
) -> Result<(), DispatchError> {
ensure_root(o)?;
let now = Self::get_current_block_as_u64();
Self::ensure_not_in_admin_freeze_window(netuid, now)?;
Ok(())
Self::ensure_admin_window_open(netuid)
}

/// Ensure owner-or-root with a set of TransactionType rate checks (owner only).
/// - Root: only freeze window is enforced; no TransactionType checks.
/// - Owner (Signed): freeze window plus all rate checks in `limits` using signer extracted from
/// origin.
pub fn ensure_sn_owner_or_root_with_limits(
o: T::RuntimeOrigin,
netuid: NetUid,
limits: &[crate::utils::rate_limiting::TransactionType],
) -> Result<Option<T::AccountId>, DispatchError> {
let maybe_who = Self::ensure_subnet_owner_or_root(o, netuid)?;
let now = Self::get_current_block_as_u64();
Self::ensure_not_in_admin_freeze_window(netuid, now)?;
if let Some(who) = maybe_who.as_ref() {
for tx in limits.iter() {
ensure!(
Expand All @@ -70,26 +63,6 @@ impl<T: Config> Pallet<T> {
Ok(maybe_who)
}

/// Ensure the caller is the subnet owner and passes all provided rate limits.
/// This does NOT allow root; it is strictly owner-only.
/// Returns the signer (owner) on success so callers may record last-blocks.
pub fn ensure_sn_owner_with_limits(
o: T::RuntimeOrigin,
netuid: NetUid,
limits: &[crate::utils::rate_limiting::TransactionType],
) -> Result<T::AccountId, DispatchError> {
let who = Self::ensure_subnet_owner(o, netuid)?;
let now = Self::get_current_block_as_u64();
Self::ensure_not_in_admin_freeze_window(netuid, now)?;
for tx in limits.iter() {
ensure!(
tx.passes_rate_limit_on_subnet::<T>(&who, netuid),
Error::<T>::TxRateLimitExceeded
);
}
Ok(who)
}

/// Returns true if the current block is within the terminal freeze window of the tempo for the
/// given subnet. During this window, admin ops are prohibited to avoid interference with
/// validator weight submissions.
Expand All @@ -103,7 +76,9 @@ impl<T: Config> Pallet<T> {
remaining < window
}

fn ensure_not_in_admin_freeze_window(netuid: NetUid, now: u64) -> Result<(), DispatchError> {
/// Ensures the admin freeze window is not currently active for the given subnet.
pub fn ensure_admin_window_open(netuid: NetUid) -> Result<(), DispatchError> {
let now = Self::get_current_block_as_u64();
ensure!(
!Self::is_in_admin_freeze_window(netuid, now),
Error::<T>::AdminActionProhibitedDuringWeightsWindow
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,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: 338,
spec_version: 339,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading