Skip to content

Commit 86cce6f

Browse files
committed
add test + small refacto
1 parent 11c281f commit 86cce6f

File tree

4 files changed

+120
-43
lines changed

4 files changed

+120
-43
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ pub mod pallet {
107107
BondsMovingAverageMaxReached,
108108
/// Only root can set negative sigmoid steepness values
109109
NegativeSigmoidSteepness,
110-
/// Value not in allowed bounds.
111-
ValueNotInBounds,
112110
}
113111
/// Enum for specifying the type of precompile operation.
114112
#[derive(
@@ -667,22 +665,12 @@ pub mod pallet {
667665
netuid: NetUid,
668666
min_burn: TaoCurrency,
669667
) -> DispatchResult {
670-
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin.clone(), netuid)?;
671-
// Allow set min_burn but only up to 1 TAO for spamming.
668+
ensure_root(origin)?;
669+
672670
ensure!(
673671
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
674672
Error::<T>::SubnetDoesNotExist
675673
);
676-
// Min burn must be less than 1 TAO.
677-
ensure!(
678-
min_burn < TaoCurrency::from(1_000_000_000),
679-
Error::<T>::ValueNotInBounds
680-
);
681-
// Min burn must be less than max burn
682-
ensure!(
683-
min_burn <= pallet_subtensor::Pallet::<T>::get_max_burn(netuid),
684-
Error::<T>::ValueNotInBounds
685-
);
686674
pallet_subtensor::Pallet::<T>::set_min_burn(netuid, min_burn);
687675
log::debug!("MinBurnSet( netuid: {netuid:?} min_burn: {min_burn:?} ) ");
688676
Ok(())
@@ -700,21 +688,12 @@ pub mod pallet {
700688
netuid: NetUid,
701689
max_burn: TaoCurrency,
702690
) -> DispatchResult {
703-
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin.clone(), netuid)?;
691+
ensure_root(origin)?;
692+
704693
ensure!(
705694
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
706695
Error::<T>::SubnetDoesNotExist
707696
);
708-
// Max burn must be greater than 0.1 TAO.
709-
ensure!(
710-
max_burn >= TaoCurrency::from(100_000_000),
711-
Error::<T>::ValueNotInBounds
712-
);
713-
// Max burn must be greater than min burn
714-
ensure!(
715-
max_burn > pallet_subtensor::Pallet::<T>::get_min_burn(netuid),
716-
Error::<T>::ValueNotInBounds
717-
);
718697
pallet_subtensor::Pallet::<T>::set_max_burn(netuid, max_burn);
719698
log::debug!("MaxBurnSet( netuid: {netuid:?} max_burn: {max_burn:?} ) ");
720699
Ok(())

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

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use frame_support::{
55
traits::Hooks,
66
};
77
use frame_system::Config;
8-
use pallet_subtensor::{Error as SubtensorError, SubnetOwner, Tempo, WeightsVersionKeyRateLimit};
8+
use pallet_subtensor::{
9+
Error as SubtensorError, MaxRegistrationsPerBlock, Rank, SubnetOwner,
10+
TargetRegistrationsPerInterval, Tempo, WeightsVersionKeyRateLimit, *,
11+
};
912
// use pallet_subtensor::{migrations, Event};
1013
use pallet_subtensor::Event;
1114
use sp_consensus_grandpa::AuthorityId as GrandpaId;
@@ -1951,3 +1954,106 @@ fn test_sudo_set_commit_reveal_version() {
19511954
);
19521955
});
19531956
}
1957+
1958+
#[test]
1959+
fn test_trim_to_max_allowed_uids() {
1960+
new_test_ext().execute_with(|| {
1961+
let netuid = NetUid::from(1);
1962+
add_network(netuid, 10);
1963+
MaxRegistrationsPerBlock::<Test>::insert(netuid, 256);
1964+
TargetRegistrationsPerInterval::<Test>::insert(netuid, 256);
1965+
1966+
// Add some neurons
1967+
let max_n = 32;
1968+
for i in 1..=max_n {
1969+
let n = i * 1000;
1970+
register_ok_neuron(netuid, U256::from(n), U256::from(n + i), 0);
1971+
}
1972+
1973+
// Run some block to ensure stake weights are set
1974+
run_to_block(20);
1975+
1976+
// Normal case
1977+
let new_max_n = 20;
1978+
assert_ok!(AdminUtils::sudo_trim_to_max_allowed_uids(
1979+
<<Test as Config>::RuntimeOrigin>::root(),
1980+
netuid,
1981+
new_max_n
1982+
));
1983+
1984+
// Ensure storage has been trimmed
1985+
assert_eq!(MaxAllowedUids::<Test>::get(netuid), new_max_n);
1986+
assert_eq!(Rank::<Test>::get(netuid).len(), new_max_n as usize);
1987+
assert_eq!(Trust::<Test>::get(netuid).len(), new_max_n as usize);
1988+
assert_eq!(Active::<Test>::get(netuid).len(), new_max_n as usize);
1989+
assert_eq!(Emission::<Test>::get(netuid).len(), new_max_n as usize);
1990+
assert_eq!(Consensus::<Test>::get(netuid).len(), new_max_n as usize);
1991+
assert_eq!(Incentive::<Test>::get(netuid).len(), new_max_n as usize);
1992+
assert_eq!(Dividends::<Test>::get(netuid).len(), new_max_n as usize);
1993+
assert_eq!(LastUpdate::<Test>::get(netuid).len(), new_max_n as usize);
1994+
assert_eq!(PruningScores::<Test>::get(netuid).len(), new_max_n as usize);
1995+
assert_eq!(
1996+
ValidatorTrust::<Test>::get(netuid).len(),
1997+
new_max_n as usize
1998+
);
1999+
assert_eq!(
2000+
ValidatorPermit::<Test>::get(netuid).len(),
2001+
new_max_n as usize
2002+
);
2003+
assert_eq!(StakeWeight::<Test>::get(netuid).len(), new_max_n as usize);
2004+
2005+
for uid in max_n..new_max_n {
2006+
assert!(!Keys::<Test>::contains_key(netuid, uid));
2007+
assert!(!BlockAtRegistration::<Test>::contains_key(netuid, uid));
2008+
assert!(!Weights::<Test>::contains_key(netuid, uid));
2009+
assert!(!Bonds::<Test>::contains_key(netuid, uid));
2010+
}
2011+
2012+
for uid in 0..max_n {
2013+
assert!(
2014+
Weights::<Test>::get(netuid, uid)
2015+
.iter()
2016+
.all(|(target_uid, _)| *target_uid < new_max_n),
2017+
"Found a weight with target_uid >= new_max_n"
2018+
);
2019+
assert!(
2020+
Bonds::<Test>::get(netuid, uid)
2021+
.iter()
2022+
.all(|(target_uid, _)| *target_uid < new_max_n),
2023+
"Found a bond with target_uid >= new_max_n"
2024+
);
2025+
}
2026+
2027+
assert_eq!(SubnetworkN::<Test>::get(netuid), new_max_n);
2028+
2029+
// Non existent subnet
2030+
assert_err!(
2031+
AdminUtils::sudo_trim_to_max_allowed_uids(
2032+
<<Test as Config>::RuntimeOrigin>::root(),
2033+
NetUid::from(42),
2034+
new_max_n
2035+
),
2036+
pallet_subtensor::Error::<Test>::SubNetworkDoesNotExist
2037+
);
2038+
2039+
// New max n less than lower bound
2040+
assert_err!(
2041+
AdminUtils::sudo_trim_to_max_allowed_uids(
2042+
<<Test as Config>::RuntimeOrigin>::root(),
2043+
netuid,
2044+
15
2045+
),
2046+
pallet_subtensor::Error::<Test>::InvalidValue
2047+
);
2048+
2049+
// New max n greater than upper bound
2050+
assert_err!(
2051+
AdminUtils::sudo_trim_to_max_allowed_uids(
2052+
<<Test as Config>::RuntimeOrigin>::root(),
2053+
netuid,
2054+
SubtensorModule::get_max_allowed_uids(netuid) + 1
2055+
),
2056+
pallet_subtensor::Error::<Test>::InvalidValue
2057+
);
2058+
});
2059+
}

pallets/subtensor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ pub mod pallet {
14861486
/// ==== Subnetwork Consensus Storage ====
14871487
/// =======================================
14881488
#[pallet::storage] // --- DMAP ( netuid ) --> stake_weight | weight for stake used in YC.
1489-
pub(super) type StakeWeight<T: Config> =
1489+
pub type StakeWeight<T: Config> =
14901490
StorageMap<_, Identity, NetUid, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
14911491
#[pallet::storage]
14921492
/// --- DMAP ( netuid, hotkey ) --> uid

pallets/subtensor/src/subnets/uids.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<T: Config> Pallet<T> {
9696
IsNetworkMember::<T>::insert(new_hotkey.clone(), netuid, true); // Fill network is member.
9797
}
9898

99-
/// Clears (sets to default) the neuron map values fot a neuron when it is
99+
/// Clears (sets to default) the neuron map values fot a neuron when it is
100100
/// removed from the subnet
101101
pub fn clear_neuron(netuid: NetUid, neuron_uid: u16) {
102102
let neuron_index: usize = neuron_uid.into();
@@ -180,9 +180,9 @@ impl<T: Config> Pallet<T> {
180180
stake_weight.into_iter().take(max_n as usize).collect();
181181
StakeWeight::<T>::insert(netuid, trimmed_stake_weight);
182182

183-
// Trim UIDs and Keys by removing entries with UID >= max_n (since UIDs are 0-indexed)
184-
// UIDs range from 0 to current_n-1, so we remove UIDs from max_n to current_n-1
185183
for uid in max_n..current_n {
184+
// Trim UIDs and Keys by removing entries with UID >= max_n (since UIDs are 0-indexed)
185+
// UIDs range from 0 to current_n-1, so we remove UIDs from max_n to current_n-1
186186
if let Ok(hotkey) = Keys::<T>::try_get(netuid, uid) {
187187
Uids::<T>::remove(netuid, &hotkey);
188188
// Remove IsNetworkMember association for the hotkey
@@ -193,28 +193,20 @@ impl<T: Config> Pallet<T> {
193193
AlphaDividendsPerSubnet::<T>::remove(netuid, &hotkey);
194194
// Remove tao dividends for the hotkey
195195
TaoDividendsPerSubnet::<T>::remove(netuid, &hotkey);
196+
// Trim axons, certificates, and prometheus info for removed hotkeys
197+
Axons::<T>::remove(netuid, &hotkey);
198+
NeuronCertificates::<T>::remove(netuid, &hotkey);
199+
Prometheus::<T>::remove(netuid, &hotkey);
196200
}
197201
#[allow(unknown_lints)]
198202
Keys::<T>::remove(netuid, uid);
199203
// Remove block at registration for the uid
200204
BlockAtRegistration::<T>::remove(netuid, uid);
201-
}
202-
203-
// Trim weights and bonds for removed UIDs
204-
for uid in max_n..current_n {
205+
// Trim weights and bonds for removed UIDs
205206
Weights::<T>::remove(netuid, uid);
206207
Bonds::<T>::remove(netuid, uid);
207208
}
208209

209-
// Trim axons, certificates, and prometheus info for removed hotkeys
210-
for uid in max_n..current_n {
211-
if let Ok(hotkey) = Keys::<T>::try_get(netuid, uid) {
212-
Axons::<T>::remove(netuid, &hotkey);
213-
NeuronCertificates::<T>::remove(netuid, &hotkey);
214-
Prometheus::<T>::remove(netuid, &hotkey);
215-
}
216-
}
217-
218210
// Trim weight and bond connections to removed UIDs for remaining neurons
219211
// UIDs 0 to max_n-1 are kept, so we iterate through these valid UIDs
220212
for uid in 0..max_n {

0 commit comments

Comments
 (0)