Skip to content

Commit 6e8229b

Browse files
committed
Merge branch 'devnet-ready' into feat/untrim-uids
2 parents 9e72ab1 + 5ad0e70 commit 6e8229b

File tree

5 files changed

+71
-11
lines changed

5 files changed

+71
-11
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,6 +2502,28 @@ fn test_trim_to_max_allowed_uids() {
25022502
Keys::<Test>::insert(netuid, 14, sn_owner_hotkey2);
25032503
Uids::<Test>::insert(netuid, sn_owner_hotkey2, 14);
25042504

2505+
// Set some evm addresses
2506+
AssociatedEvmAddress::<Test>::insert(
2507+
netuid,
2508+
6,
2509+
(sp_core::H160::from_slice(b"12345678901234567891"), now),
2510+
);
2511+
AssociatedEvmAddress::<Test>::insert(
2512+
netuid,
2513+
10,
2514+
(sp_core::H160::from_slice(b"12345678901234567892"), now),
2515+
);
2516+
AssociatedEvmAddress::<Test>::insert(
2517+
netuid,
2518+
12,
2519+
(sp_core::H160::from_slice(b"12345678901234567893"), now),
2520+
);
2521+
AssociatedEvmAddress::<Test>::insert(
2522+
netuid,
2523+
14,
2524+
(sp_core::H160::from_slice(b"12345678901234567894"), now),
2525+
);
2526+
25052527
// Populate Weights and Bonds storage items to test trimming
25062528
// Create weights and bonds that span across the range that will be trimmed
25072529
for uid in 0..max_n {
@@ -2578,6 +2600,7 @@ fn test_trim_to_max_allowed_uids() {
25782600
for uid in new_max_n..max_n {
25792601
assert!(!Keys::<Test>::contains_key(netuid, uid));
25802602
assert!(!BlockAtRegistration::<Test>::contains_key(netuid, uid));
2603+
assert!(!AssociatedEvmAddress::<Test>::contains_key(netuid, uid));
25812604
for mecid in 0..mechanism_count.into() {
25822605
let netuid_index =
25832606
SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(mecid));
@@ -2635,6 +2658,23 @@ fn test_trim_to_max_allowed_uids() {
26352658
// Actual number of neurons on the network updated after trimming
26362659
assert_eq!(SubnetworkN::<Test>::get(netuid), new_max_n);
26372660

2661+
// Uids match enumeration order
2662+
for i in 0..new_max_n.into() {
2663+
let hotkey = Keys::<Test>::get(netuid, i);
2664+
let uid = Uids::<Test>::get(netuid, hotkey);
2665+
assert_eq!(uid, Some(i));
2666+
}
2667+
2668+
// EVM association have been remapped correctly (uids: 7 -> 2, 14 -> 7)
2669+
assert_eq!(
2670+
AssociatedEvmAddress::<Test>::get(netuid, 2),
2671+
Some((sp_core::H160::from_slice(b"12345678901234567891"), now))
2672+
);
2673+
assert_eq!(
2674+
AssociatedEvmAddress::<Test>::get(netuid, 7),
2675+
Some((sp_core::H160::from_slice(b"12345678901234567894"), now))
2676+
);
2677+
26382678
// Non existent subnet
26392679
assert_err!(
26402680
AdminUtils::sudo_trim_to_max_allowed_uids(
@@ -2677,7 +2717,7 @@ fn test_trim_to_max_allowed_uids_too_many_immune() {
26772717
MaxRegistrationsPerBlock::<Test>::insert(netuid, 256);
26782718
TargetRegistrationsPerInterval::<Test>::insert(netuid, 256);
26792719
ImmuneOwnerUidsLimit::<Test>::insert(netuid, 2);
2680-
MinAllowedUids::<Test>::set(netuid, 4);
2720+
MinAllowedUids::<Test>::set(netuid, 2);
26812721

26822722
// Add 5 neurons
26832723
let max_n = 5;
@@ -2715,7 +2755,7 @@ fn test_trim_to_max_allowed_uids_too_many_immune() {
27152755
netuid,
27162756
4
27172757
),
2718-
pallet_subtensor::Error::<Test>::InvalidValue
2758+
pallet_subtensor::Error::<Test>::TrimmingWouldExceedMaxImmunePercentage
27192759
);
27202760

27212761
// Try to trim to 3 UIDs - this should also fail because 4/3 > 80% immune (>= 80%)
@@ -2725,7 +2765,7 @@ fn test_trim_to_max_allowed_uids_too_many_immune() {
27252765
netuid,
27262766
3
27272767
),
2728-
pallet_subtensor::Error::<Test>::InvalidValue
2768+
pallet_subtensor::Error::<Test>::TrimmingWouldExceedMaxImmunePercentage
27292769
);
27302770

27312771
// Now test a scenario where trimming should succeed
@@ -2737,10 +2777,6 @@ fn test_trim_to_max_allowed_uids_too_many_immune() {
27372777
Uids::<Test>::remove(netuid, hotkey_to_remove);
27382778
BlockAtRegistration::<Test>::remove(netuid, uid_to_remove);
27392779

2740-
// Now we have 3 immune out of 4 total UIDs
2741-
// Try to trim to 3 UIDs - this should succeed because 3/3 = 100% immune, but that's exactly 80%
2742-
// Wait, 100% is > 80%, so this should fail. Let me test with a scenario where we have fewer immune UIDs
2743-
27442780
// Remove another immune UID to make it 2 immune out of 3 total
27452781
let uid_to_remove2 = 2;
27462782
let hotkey_to_remove2 = U256::from(uid_to_remove2 * 1000 + 1000);

pallets/subtensor/src/epoch/run_epoch.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ impl<T: Config> Pallet<T> {
135135
let pruning_score = extract_from_sorted_terms!(terms_sorted, pruning_score);
136136
let validator_trust = extract_from_sorted_terms!(terms_sorted, validator_trust);
137137
let new_validator_permit = extract_from_sorted_terms!(terms_sorted, new_validator_permit);
138+
let stake_weight = extract_from_sorted_terms!(terms_sorted, stake_weight);
138139

139140
Active::<T>::insert(netuid, active.clone());
140141
Emission::<T>::insert(netuid, emission);
@@ -145,6 +146,7 @@ impl<T: Config> Pallet<T> {
145146
PruningScores::<T>::insert(netuid, pruning_score);
146147
ValidatorTrust::<T>::insert(netuid, validator_trust);
147148
ValidatorPermit::<T>::insert(netuid, new_validator_permit);
149+
StakeWeight::<T>::insert(netuid, stake_weight);
148150
}
149151

150152
/// Calculates reward consensus and returns the emissions for uids/hotkeys in a given `netuid`.

pallets/subtensor/src/macros/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,9 @@ mod errors {
256256
CannotAffordLockCost,
257257
/// exceeded the rate limit for associating an EVM key.
258258
EvmKeyAssociateRateLimitExceeded,
259+
/// The UID map for the subnet could not be cleared
260+
UidMapCouldNotBeCleared,
261+
/// Trimming would exceed the max immune neurons percentage
262+
TrimmingWouldExceedMaxImmunePercentage,
259263
}
260264
}

pallets/subtensor/src/subnets/uids.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl<T: Config> Pallet<T> {
4646
}
4747
}
4848
Dividends::<T>::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0));
49+
StakeWeight::<T>::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0));
4950
}
5051

5152
/// Replace the neuron under this uid.
@@ -162,7 +163,7 @@ impl<T: Config> Pallet<T> {
162163
let immune_percentage = Percent::from_rational(immune_count, max_n);
163164
ensure!(
164165
immune_percentage < T::MaxImmuneUidsPercentage::get(),
165-
Error::<T>::InvalidValue
166+
Error::<T>::TrimmingWouldExceedMaxImmunePercentage
166167
);
167168

168169
// Get all emissions with their UIDs and sort by emission (descending)
@@ -209,6 +210,7 @@ impl<T: Config> Pallet<T> {
209210
#[allow(unknown_lints)]
210211
Keys::<T>::remove(netuid, neuron_uid);
211212
BlockAtRegistration::<T>::remove(netuid, neuron_uid);
213+
AssociatedEvmAddress::<T>::remove(netuid, neuron_uid);
212214
for mecid in 0..mechanisms_count {
213215
let netuid_index = Self::get_mechanism_storage_index(netuid, mecid.into());
214216
Weights::<T>::remove(netuid_index, neuron_uid);
@@ -315,6 +317,7 @@ impl<T: Config> Pallet<T> {
315317

316318
// Swap uid specific storage items to new compressed positions
317319
Keys::<T>::swap(netuid, old_neuron_uid, netuid, new_neuron_uid);
320+
AssociatedEvmAddress::<T>::swap(netuid, old_neuron_uid, netuid, new_neuron_uid);
318321
BlockAtRegistration::<T>::swap(netuid, old_neuron_uid, netuid, new_neuron_uid);
319322

320323
for mecid in 0..mechanisms_count {
@@ -352,6 +355,21 @@ impl<T: Config> Pallet<T> {
352355
}
353356
}
354357

358+
// Clear the UID map for the subnet
359+
let clear_result = Uids::<T>::clear_prefix(netuid, u32::MAX, None);
360+
// Shouldn't happen, but possible.
361+
ensure!(
362+
clear_result.maybe_cursor.is_none(),
363+
Error::<T>::UidMapCouldNotBeCleared
364+
);
365+
366+
// Insert the new UIDs
367+
for new_uid in old_to_new_uid.values() {
368+
// Get the hotkey using Keys map and new UID.
369+
let hotkey = Keys::<T>::get(netuid, *new_uid as u16);
370+
Uids::<T>::insert(netuid, hotkey, *new_uid as u16);
371+
}
372+
355373
// Update the subnet's uid count to reflect the new maximum
356374
SubnetworkN::<T>::insert(netuid, max_n);
357375
}

0 commit comments

Comments
 (0)