Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
8c3ec67
try dissolve_network benchmark
open-junius Feb 10, 2026
9030e64
remove collect
open-junius Feb 10, 2026
f6293d7
add on idle
open-junius Feb 11, 2026
fb3dc04
add weight meter
open-junius Feb 12, 2026
e7d60f1
on_idle hook
open-junius Feb 13, 2026
860d84d
add macro
open-junius Feb 13, 2026
5161299
need handle remove all lp
open-junius Feb 13, 2026
890f685
fix typo
open-junius Feb 13, 2026
07da3b5
update interface
open-junius Feb 13, 2026
7b0cca7
merge devnet-ready
open-junius Feb 13, 2026
4b9a6a0
cargo clippy
open-junius Feb 13, 2026
e9a67b8
add weigth for all db ops
open-junius Feb 16, 2026
8f17e84
commit Cargo.lock
open-junius Feb 16, 2026
f932ae6
commit Cargo.lock
open-junius Feb 16, 2026
71668de
commit Cargo.lock
open-junius Feb 16, 2026
85d33d6
commit Cargo.lock
open-junius Feb 16, 2026
dc8b744
commit Cargo.lock
open-junius Feb 16, 2026
08d2d34
optimize some db ops
open-junius Feb 16, 2026
50cd88c
add unit test for macro
open-junius Feb 16, 2026
2dc8630
cargo clippy
open-junius Feb 16, 2026
f3eb5bd
commit Cargo.lock
open-junius Feb 16, 2026
cc033e6
exclude the dissolved network id when registration
open-junius Feb 16, 2026
8f2add8
fix unit test
open-junius Feb 16, 2026
c9c66d8
Merge branch 'devnet-ready' into benchmark-dissolve-network
open-junius Feb 24, 2026
cbc441b
fix unit test
open-junius Feb 25, 2026
21d8e4f
fix clippy
open-junius Feb 25, 2026
f2ed907
reduce benchmark for dissolve_network
open-junius Feb 25, 2026
d489382
fix unit test
open-junius Feb 25, 2026
aabec8c
update purge netuid in mock
open-junius Feb 25, 2026
06f5cdc
fix bug, missed bonds removal
open-junius Feb 25, 2026
203628c
correct weights for dissolve network
open-junius Feb 25, 2026
984f14c
fix benchmark
open-junius Feb 26, 2026
39205fd
merge with devnet ready
open-junius Feb 26, 2026
9361813
commit Cargo.lock
open-junius Feb 26, 2026
694b833
commit Cargo.lock
open-junius Feb 26, 2026
ed7b2e3
commit Cargo.lock
open-junius Feb 26, 2026
951d04a
commit Cargo.lock
open-junius Feb 26, 2026
453db3f
fix unit test
open-junius Feb 26, 2026
7639d7e
Merge branch 'devnet-ready' into benchmark-dissolve-network
open-junius Feb 27, 2026
a861672
add subnet exist check in admin util
open-junius Feb 27, 2026
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
4 changes: 3 additions & 1 deletion chain-extensions/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,9 @@ impl PrivilegeCmp<OriginCaller> for OriginPrivilegeCmp {

pub struct CommitmentsI;
impl CommitmentsInterface for CommitmentsI {
fn purge_netuid(_netuid: NetUid) {}
fn purge_netuid(_netuid: NetUid, remaining_weight: Weight) -> Weight {
remaining_weight
}
}

parameter_types! {
Expand Down
109 changes: 109 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,121 @@ impl TypeInfo for NetUidStorageIndex {
}
}

#[macro_export]
macro_rules! WeightMeterWrapper {
( $meter:expr, $weight:expr ) => {{
if !$meter.can_consume($weight) {
return $meter.consumed();
}
$meter.consume($weight);
}};
}

#[macro_export]
macro_rules! LoopRemovePrefixWithWeightMeter {
( $meter:expr, $weight:expr, $body:expr ) => {{
loop {
if !$meter.can_consume($weight.saturating_mul(1024)) {
return $meter.consumed();
}
let result = $body;

$meter.consume($weight.saturating_mul(result.backend as u64));
if result.maybe_cursor.is_none() {
break;
}
}
$meter.consumed()
}};
}

#[cfg(test)]
mod tests {
use super::*;
use frame_support::weights::WeightMeter;

struct TestBody {
count: u64,
}

struct TestResult {
backend: u64,
maybe_cursor: Option<()>,
}

impl TestBody {
fn new(count: u64) -> Self {
Self { count }
}

fn execute(&mut self, number: u64) -> TestResult {
if self.count >= number {
self.count = self.count.saturating_sub(number);
TestResult {
backend: number,
maybe_cursor: Some(()),
}
} else {
let tmp = self.count;
self.count = 0;
TestResult {
backend: tmp,
maybe_cursor: None,
}
}
}
}

#[test]
fn netuid_has_u16_bin_repr() {
assert_eq!(NetUid(5).encode(), 5u16.encode());
}

fn test_weight(remaining_weight: Weight, weight: Weight) -> Weight {
let mut weight_meter = WeightMeter::with_limit(remaining_weight);
WeightMeterWrapper!(weight_meter, weight);
weight_meter.consumed()
}

fn test_loop(
remaining_weight: Weight,
weight: Weight,
mut body: TestBody,
number: u64,
) -> Weight {
let mut weight_meter = WeightMeter::with_limit(remaining_weight);
LoopRemovePrefixWithWeightMeter!(weight_meter, weight, body.execute(number));
weight_meter.consumed()
}

#[test]
fn test_weight_meter_wrapper() {
let remaining_weight = Weight::from_parts(200, 200);
let used_weight = test_weight(remaining_weight, Weight::from_parts(100, 100));
assert_eq!(used_weight, Weight::from_parts(100, 100));

let used_weight = test_weight(remaining_weight, Weight::from_parts(300, 300));
assert_eq!(used_weight, Weight::from_parts(0, 0));
}

#[test]
fn test_loop_remove_prefix_with_weight_meter() {
// remaining weight matches the body count
let body = TestBody::new(1024);
let remaining_weight = Weight::from_parts(100 * 1024, 100 * 1024);
let used_weight = test_loop(remaining_weight, Weight::from_parts(100, 100), body, 1024);
assert_eq!(used_weight, Weight::from_parts(100, 100) * 1024);

// remaining weight is less than the body count
let body = TestBody::new(1025);
let remaining_weight = Weight::from_parts(100 * 1024, 100 * 1024);
let used_weight = test_loop(remaining_weight, Weight::from_parts(100, 100), body, 1024);
assert_eq!(used_weight, Weight::from_parts(100, 100) * 1024);

// remaining weight is more than the body count
let body = TestBody::new(1025);
let remaining_weight = Weight::from_parts(100 * 1024 * 2, 100 * 1024 * 2);
let used_weight = test_loop(remaining_weight, Weight::from_parts(100, 100), body, 1024);
assert_eq!(used_weight, Weight::from_parts(100, 100) * 1025);
}
}
53 changes: 53 additions & 0 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,11 @@ pub mod pallet {
registration_allowed: bool,
) -> DispatchResult {
ensure_root(origin)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
pallet_subtensor::Pallet::<T>::set_network_registration_allowed(
netuid,
registration_allowed,
Expand Down Expand Up @@ -1258,6 +1263,10 @@ pub mod pallet {
netuid,
&[Hyperparameter::LiquidAlphaEnabled.into()],
)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
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:?} ) ");
Expand Down Expand Up @@ -1285,6 +1294,10 @@ pub mod pallet {
netuid,
&[Hyperparameter::AlphaValues.into()],
)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
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 @@ -1458,6 +1471,10 @@ pub mod pallet {
netuid,
&[Hyperparameter::TransferEnabled.into()],
)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
let res = pallet_subtensor::Pallet::<T>::toggle_transfer(netuid, toggle);
if res.is_ok() {
Expand Down Expand Up @@ -1493,6 +1510,10 @@ pub mod pallet {
netuid,
&[Hyperparameter::RecycleOrBurn.into()],
)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

pallet_subtensor::Pallet::<T>::set_recycle_or_burn(netuid, recycle_or_burn);
Expand Down Expand Up @@ -1604,6 +1625,10 @@ pub mod pallet {
ema_halving: u64,
) -> DispatchResult {
ensure_root(origin)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
pallet_subtensor::EMAPriceHalvingBlocks::<T>::set(netuid, ema_halving);

log::debug!(
Expand Down Expand Up @@ -1688,6 +1713,10 @@ pub mod pallet {
netuid,
&[Hyperparameter::Yuma3Enabled.into()],
)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
pallet_subtensor::Pallet::<T>::set_yuma3_enabled(netuid, enabled);

Expand Down Expand Up @@ -1724,6 +1753,10 @@ pub mod pallet {
netuid,
&[Hyperparameter::BondsResetEnabled.into()],
)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
pallet_subtensor::Pallet::<T>::set_bonds_reset(netuid, enabled);

Expand Down Expand Up @@ -1839,6 +1872,10 @@ pub mod pallet {
netuid,
&[Hyperparameter::ImmuneNeuronLimit.into()],
)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
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(
Expand Down Expand Up @@ -1907,6 +1944,10 @@ pub mod pallet {
netuid,
&[TransactionType::MechanismCountUpdate],
)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

pallet_subtensor::Pallet::<T>::do_set_mechanism_count(netuid, mechanism_count)?;
Expand Down Expand Up @@ -1934,6 +1975,10 @@ pub mod pallet {
netuid,
&[TransactionType::MechanismEmission],
)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

pallet_subtensor::Pallet::<T>::do_set_emission_split(netuid, maybe_split)?;
Expand Down Expand Up @@ -1965,6 +2010,10 @@ pub mod pallet {
netuid,
&[TransactionType::MaxUidsTrimming],
)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;

pallet_subtensor::Pallet::<T>::trim_to_max_allowed_uids(netuid, max_n)?;
Expand Down Expand Up @@ -2090,6 +2139,10 @@ pub mod pallet {
min: u16,
) -> DispatchResult {
ensure_root(origin)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
pallet_subtensor::Pallet::<T>::set_min_non_immune_uids(netuid, min);
Ok(())
}
Expand Down
4 changes: 3 additions & 1 deletion pallets/admin-utils/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,9 @@ impl PrivilegeCmp<OriginCaller> for OriginPrivilegeCmp {

pub struct CommitmentsI;
impl pallet_subtensor::CommitmentsInterface for CommitmentsI {
fn purge_netuid(_netuid: NetUid) {}
fn purge_netuid(_netuid: NetUid, remaining_weight: Weight) -> Weight {
remaining_weight
}
}

pub struct GrandpaInterfaceImpl;
Expand Down
43 changes: 36 additions & 7 deletions pallets/commitments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod weights;
use ark_serialize::CanonicalDeserialize;
use codec::Encode;
use frame_support::IterableStorageDoubleMap;
use frame_support::weights::WeightMeter;
use frame_support::{
BoundedVec,
traits::{Currency, Get},
Expand All @@ -23,7 +24,7 @@ use scale_info::prelude::collections::BTreeSet;
use sp_runtime::SaturatedConversion;
use sp_runtime::{Saturating, Weight, traits::Zero};
use sp_std::{boxed::Box, vec::Vec};
use subtensor_runtime_common::NetUid;
use subtensor_runtime_common::{LoopRemovePrefixWithWeightMeter, NetUid, WeightMeterWrapper};
use tle::{
curves::drand::TinyBLS381,
stream_ciphers::AESGCMStreamCipherProvider,
Expand Down Expand Up @@ -563,16 +564,44 @@ impl<T: Config> Pallet<T> {
commitments
}

pub fn purge_netuid(netuid: NetUid) {
let _ = CommitmentOf::<T>::clear_prefix(netuid, u32::MAX, None);
let _ = LastCommitment::<T>::clear_prefix(netuid, u32::MAX, None);
let _ = LastBondsReset::<T>::clear_prefix(netuid, u32::MAX, None);
let _ = RevealedCommitments::<T>::clear_prefix(netuid, u32::MAX, None);
let _ = UsedSpaceOf::<T>::clear_prefix(netuid, u32::MAX, None);
pub fn purge_netuid(netuid: NetUid, remaining_weight: Weight) -> Weight {
let mut weight_meter = WeightMeter::with_limit(remaining_weight);
LoopRemovePrefixWithWeightMeter!(
weight_meter,
T::DbWeight::get().writes(1),
CommitmentOf::<T>::clear_prefix(netuid, 1024, None)
);

LoopRemovePrefixWithWeightMeter!(
weight_meter,
T::DbWeight::get().writes(1),
LastCommitment::<T>::clear_prefix(netuid, 1024, None)
);

LoopRemovePrefixWithWeightMeter!(
weight_meter,
T::DbWeight::get().writes(1),
LastBondsReset::<T>::clear_prefix(netuid, 1024, None)
);

LoopRemovePrefixWithWeightMeter!(
weight_meter,
T::DbWeight::get().writes(1),
RevealedCommitments::<T>::clear_prefix(netuid, 1024, None)
);

LoopRemovePrefixWithWeightMeter!(
weight_meter,
T::DbWeight::get().writes(1),
UsedSpaceOf::<T>::clear_prefix(netuid, 1024, None)
);

WeightMeterWrapper!(weight_meter, T::DbWeight::get().writes(1));

TimelockedIndex::<T>::mutate(|index| {
index.retain(|(n, _)| *n != netuid);
});
weight_meter.consumed()
}
}

Expand Down
5 changes: 3 additions & 2 deletions pallets/commitments/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use frame_support::pallet_prelude::Hooks;
use frame_support::{
BoundedVec, assert_noop, assert_ok,
traits::{Currency, Get, ReservableCurrency},
weights::Weight,
};
use frame_system::{Pallet as System, RawOrigin};

Expand Down Expand Up @@ -2265,7 +2266,7 @@ fn purge_netuid_clears_only_that_netuid() {
assert!(TimelockedIndex::<Test>::get().contains(&(net_a, who_a1)));

// Act
Pallet::<Test>::purge_netuid(net_a);
Pallet::<Test>::purge_netuid(net_a, Weight::from_parts(u64::MAX, u64::MAX));

// NET A: everything cleared
assert_eq!(CommitmentOf::<Test>::iter_prefix(net_a).count(), 0);
Expand Down Expand Up @@ -2298,7 +2299,7 @@ fn purge_netuid_clears_only_that_netuid() {
assert!(idx_after.contains(&(net_b, who_b)));

// Idempotency
Pallet::<Test>::purge_netuid(net_a);
Pallet::<Test>::purge_netuid(net_a, Weight::from_parts(u64::MAX, u64::MAX));
assert_eq!(CommitmentOf::<Test>::iter_prefix(net_a).count(), 0);
assert!(!TimelockedIndex::<Test>::get().contains(&(net_a, who_a1)));
});
Expand Down
Loading
Loading