Skip to content

Commit 6326a1a

Browse files
authored
Merge pull request #2180 from opentensor/chore/separate-admin-freeze-window
Separate admin freeze window check from rate limit check
2 parents 71455aa + 34dc6e7 commit 6326a1a

File tree

4 files changed

+42
-35
lines changed

4 files changed

+42
-35
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ pub mod pallet {
236236
netuid,
237237
&[Hyperparameter::ServingRateLimit.into()],
238238
)?;
239+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
239240
pallet_subtensor::Pallet::<T>::set_serving_rate_limit(netuid, serving_rate_limit);
240241
log::debug!("ServingRateLimitSet( serving_rate_limit: {serving_rate_limit:?} ) ");
241242
pallet_subtensor::Pallet::<T>::record_owner_rl(
@@ -288,6 +289,7 @@ pub mod pallet {
288289
netuid,
289290
&[Hyperparameter::MaxDifficulty.into()],
290291
)?;
292+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
291293

292294
ensure!(
293295
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
@@ -322,6 +324,7 @@ pub mod pallet {
322324
netuid,
323325
&[TransactionType::SetWeightsVersionKey],
324326
)?;
327+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
325328

326329
ensure!(
327330
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
@@ -413,6 +416,7 @@ pub mod pallet {
413416
netuid,
414417
&[Hyperparameter::AdjustmentAlpha.into()],
415418
)?;
419+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
416420

417421
ensure!(
418422
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
@@ -445,6 +449,7 @@ pub mod pallet {
445449
netuid,
446450
&[Hyperparameter::ImmunityPeriod.into()],
447451
)?;
452+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
448453
ensure!(
449454
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
450455
Error::<T>::SubnetDoesNotExist
@@ -479,6 +484,7 @@ pub mod pallet {
479484
netuid,
480485
&[Hyperparameter::MinAllowedWeights.into()],
481486
)?;
487+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
482488

483489
ensure!(
484490
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
@@ -513,6 +519,7 @@ pub mod pallet {
513519
netuid,
514520
&[Hyperparameter::MaxAllowedUids.into()],
515521
)?;
522+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
516523
ensure!(
517524
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
518525
Error::<T>::SubnetDoesNotExist
@@ -572,6 +579,7 @@ pub mod pallet {
572579
netuid,
573580
&[Hyperparameter::Rho.into()],
574581
)?;
582+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
575583

576584
ensure!(
577585
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
@@ -604,6 +612,7 @@ pub mod pallet {
604612
netuid,
605613
&[Hyperparameter::ActivityCutoff.into()],
606614
)?;
615+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
607616

608617
ensure!(
609618
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
@@ -672,6 +681,7 @@ pub mod pallet {
672681
netuid,
673682
&[Hyperparameter::PowRegistrationAllowed.into()],
674683
)?;
684+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
675685

676686
pallet_subtensor::Pallet::<T>::set_network_pow_registration_allowed(
677687
netuid,
@@ -733,6 +743,7 @@ pub mod pallet {
733743
netuid,
734744
&[Hyperparameter::MinBurn.into()],
735745
)?;
746+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
736747
ensure!(
737748
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
738749
Error::<T>::SubnetDoesNotExist
@@ -773,6 +784,7 @@ pub mod pallet {
773784
netuid,
774785
&[Hyperparameter::MaxBurn.into()],
775786
)?;
787+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
776788
ensure!(
777789
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
778790
Error::<T>::SubnetDoesNotExist
@@ -868,6 +880,7 @@ pub mod pallet {
868880
netuid,
869881
&[Hyperparameter::BondsMovingAverage.into()],
870882
)?;
883+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
871884
if maybe_owner.is_some() {
872885
ensure!(
873886
bonds_moving_average <= 975000,
@@ -908,6 +921,7 @@ pub mod pallet {
908921
netuid,
909922
&[Hyperparameter::BondsPenalty.into()],
910923
)?;
924+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
911925

912926
ensure!(
913927
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
@@ -1245,6 +1259,7 @@ pub mod pallet {
12451259
netuid,
12461260
&[Hyperparameter::CommitRevealEnabled.into()],
12471261
)?;
1262+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
12481263

12491264
ensure!(
12501265
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
@@ -1288,6 +1303,7 @@ pub mod pallet {
12881303
netuid,
12891304
&[Hyperparameter::LiquidAlphaEnabled.into()],
12901305
)?;
1306+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
12911307
pallet_subtensor::Pallet::<T>::set_liquid_alpha_enabled(netuid, enabled);
12921308
log::debug!("LiquidAlphaEnableToggled( netuid: {netuid:?}, Enabled: {enabled:?} ) ");
12931309
pallet_subtensor::Pallet::<T>::record_owner_rl(
@@ -1318,6 +1334,7 @@ pub mod pallet {
13181334
netuid,
13191335
&[Hyperparameter::AlphaValues.into()],
13201336
)?;
1337+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
13211338
let res = pallet_subtensor::Pallet::<T>::do_set_alpha_values(
13221339
origin, netuid, alpha_low, alpha_high,
13231340
);
@@ -1437,6 +1454,7 @@ pub mod pallet {
14371454
netuid,
14381455
&[Hyperparameter::WeightCommitInterval.into()],
14391456
)?;
1457+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
14401458

14411459
ensure!(
14421460
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
@@ -1535,6 +1553,7 @@ pub mod pallet {
15351553
netuid,
15361554
&[Hyperparameter::TransferEnabled.into()],
15371555
)?;
1556+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
15381557
let res = pallet_subtensor::Pallet::<T>::toggle_transfer(netuid, toggle);
15391558
if res.is_ok() {
15401559
pallet_subtensor::Pallet::<T>::record_owner_rl(
@@ -1567,6 +1586,7 @@ pub mod pallet {
15671586
netuid,
15681587
&[Hyperparameter::RecycleOrBurn.into()],
15691588
)?;
1589+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
15701590

15711591
pallet_subtensor::Pallet::<T>::set_recycle_or_burn(netuid, recycle_or_burn);
15721592
pallet_subtensor::Pallet::<T>::record_owner_rl(
@@ -1734,6 +1754,7 @@ pub mod pallet {
17341754
netuid,
17351755
&[Hyperparameter::AlphaSigmoidSteepness.into()],
17361756
)?;
1757+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
17371758

17381759
ensure!(
17391760
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
@@ -1784,6 +1805,7 @@ pub mod pallet {
17841805
netuid,
17851806
&[Hyperparameter::Yuma3Enabled.into()],
17861807
)?;
1808+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
17871809
pallet_subtensor::Pallet::<T>::set_yuma3_enabled(netuid, enabled);
17881810

17891811
Self::deposit_event(Event::Yuma3EnableToggled { netuid, enabled });
@@ -1823,6 +1845,7 @@ pub mod pallet {
18231845
netuid,
18241846
&[Hyperparameter::BondsResetEnabled.into()],
18251847
)?;
1848+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
18261849
pallet_subtensor::Pallet::<T>::set_bonds_reset(netuid, enabled);
18271850

18281851
Self::deposit_event(Event::BondsResetToggled { netuid, enabled });
@@ -1948,6 +1971,7 @@ pub mod pallet {
19481971
netuid,
19491972
&[Hyperparameter::ImmuneNeuronLimit.into()],
19501973
)?;
1974+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
19511975
pallet_subtensor::Pallet::<T>::set_owner_immune_neuron_limit(netuid, immune_neurons)?;
19521976
pallet_subtensor::Pallet::<T>::record_owner_rl(
19531977
maybe_owner,
@@ -2021,6 +2045,7 @@ pub mod pallet {
20212045
netuid,
20222046
&[TransactionType::MechanismCountUpdate],
20232047
)?;
2048+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
20242049

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

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

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

@@ -2077,6 +2103,7 @@ pub mod pallet {
20772103
netuid,
20782104
&[TransactionType::MaxUidsTrimming],
20792105
)?;
2106+
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
20802107

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

pallets/subtensor/src/tests/ensure.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ fn ensure_owner_or_root_with_limits_checks_rl_and_freeze() {
111111
)
112112
.expect("should pass");
113113
assert_eq!(res, Some(owner));
114+
assert_ok!(crate::Pallet::<Test>::ensure_admin_window_open(netuid));
114115

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

128129
// Advance beyond RL and ensure passes again
129130
run_to_block(now + 3);
131+
TransactionType::from(Hyperparameter::Kappa)
132+
.set_last_block_on_subnet::<Test>(&owner, netuid, 0);
130133
assert_ok!(crate::Pallet::<Test>::ensure_sn_owner_or_root_with_limits(
131134
<<Test as Config>::RuntimeOrigin>::signed(owner),
132135
netuid,
133136
&[Hyperparameter::Kappa.into()]
134137
));
138+
assert_ok!(crate::Pallet::<Test>::ensure_admin_window_open(netuid));
135139

136140
// Now advance into the freeze window; ensure blocks
137141
// (using loop for clarity, because epoch calculation function uses netuid)
@@ -148,12 +152,13 @@ fn ensure_owner_or_root_with_limits_checks_rl_and_freeze() {
148152
}
149153
run_to_block(cur + 1);
150154
}
155+
assert_ok!(crate::Pallet::<Test>::ensure_sn_owner_or_root_with_limits(
156+
<<Test as Config>::RuntimeOrigin>::signed(owner),
157+
netuid,
158+
&[Hyperparameter::Kappa.into()]
159+
));
151160
assert_noop!(
152-
crate::Pallet::<Test>::ensure_sn_owner_or_root_with_limits(
153-
<<Test as Config>::RuntimeOrigin>::signed(owner),
154-
netuid,
155-
&[Hyperparameter::Kappa.into()],
156-
),
161+
crate::Pallet::<Test>::ensure_admin_window_open(netuid),
157162
crate::Error::<Test>::AdminActionProhibitedDuringWeightsWindow
158163
);
159164
});

pallets/subtensor/src/utils/misc.rs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,16 @@ impl<T: Config> Pallet<T> {
4242
netuid: NetUid,
4343
) -> Result<(), DispatchError> {
4444
ensure_root(o)?;
45-
let now = Self::get_current_block_as_u64();
46-
Self::ensure_not_in_admin_freeze_window(netuid, now)?;
47-
Ok(())
45+
Self::ensure_admin_window_open(netuid)
4846
}
4947

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

73-
/// Ensure the caller is the subnet owner and passes all provided rate limits.
74-
/// This does NOT allow root; it is strictly owner-only.
75-
/// Returns the signer (owner) on success so callers may record last-blocks.
76-
pub fn ensure_sn_owner_with_limits(
77-
o: T::RuntimeOrigin,
78-
netuid: NetUid,
79-
limits: &[crate::utils::rate_limiting::TransactionType],
80-
) -> Result<T::AccountId, DispatchError> {
81-
let who = Self::ensure_subnet_owner(o, netuid)?;
82-
let now = Self::get_current_block_as_u64();
83-
Self::ensure_not_in_admin_freeze_window(netuid, now)?;
84-
for tx in limits.iter() {
85-
ensure!(
86-
tx.passes_rate_limit_on_subnet::<T>(&who, netuid),
87-
Error::<T>::TxRateLimitExceeded
88-
);
89-
}
90-
Ok(who)
91-
}
92-
9366
/// Returns true if the current block is within the terminal freeze window of the tempo for the
9467
/// given subnet. During this window, admin ops are prohibited to avoid interference with
9568
/// validator weight submissions.
@@ -103,7 +76,9 @@ impl<T: Config> Pallet<T> {
10376
remaining < window
10477
}
10578

106-
fn ensure_not_in_admin_freeze_window(netuid: NetUid, now: u64) -> Result<(), DispatchError> {
79+
/// Ensures the admin freeze window is not currently active for the given subnet.
80+
pub fn ensure_admin_window_open(netuid: NetUid) -> Result<(), DispatchError> {
81+
let now = Self::get_current_block_as_u64();
10782
ensure!(
10883
!Self::is_in_admin_freeze_window(netuid, now),
10984
Error::<T>::AdminActionProhibitedDuringWeightsWindow

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
220220
// `spec_version`, and `authoring_version` are the same between Wasm and native.
221221
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
222222
// the compatible custom types.
223-
spec_version: 338,
223+
spec_version: 339,
224224
impl_version: 1,
225225
apis: RUNTIME_API_VERSIONS,
226226
transaction_version: 1,

0 commit comments

Comments
 (0)