Skip to content

Commit e00abe3

Browse files
authored
Merge pull request #2085 from opentensor/hotfix/trim-uids-doesnt-renumber-uids
update trim to renumber UIDs
2 parents 40945a9 + 82b3f31 commit e00abe3

File tree

5 files changed

+69
-12
lines changed

5 files changed

+69
-12
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
@@ -2498,6 +2498,28 @@ fn test_trim_to_max_allowed_uids() {
24982498
Keys::<Test>::insert(netuid, 14, sn_owner_hotkey2);
24992499
Uids::<Test>::insert(netuid, sn_owner_hotkey2, 14);
25002500

2501+
// Set some evm addresses
2502+
AssociatedEvmAddress::<Test>::insert(
2503+
netuid,
2504+
6,
2505+
(sp_core::H160::from_slice(b"12345678901234567891"), now),
2506+
);
2507+
AssociatedEvmAddress::<Test>::insert(
2508+
netuid,
2509+
10,
2510+
(sp_core::H160::from_slice(b"12345678901234567892"), now),
2511+
);
2512+
AssociatedEvmAddress::<Test>::insert(
2513+
netuid,
2514+
12,
2515+
(sp_core::H160::from_slice(b"12345678901234567893"), now),
2516+
);
2517+
AssociatedEvmAddress::<Test>::insert(
2518+
netuid,
2519+
14,
2520+
(sp_core::H160::from_slice(b"12345678901234567894"), now),
2521+
);
2522+
25012523
// Populate Weights and Bonds storage items to test trimming
25022524
// Create weights and bonds that span across the range that will be trimmed
25032525
for uid in 0..max_n {
@@ -2574,6 +2596,7 @@ fn test_trim_to_max_allowed_uids() {
25742596
for uid in new_max_n..max_n {
25752597
assert!(!Keys::<Test>::contains_key(netuid, uid));
25762598
assert!(!BlockAtRegistration::<Test>::contains_key(netuid, uid));
2599+
assert!(!AssociatedEvmAddress::<Test>::contains_key(netuid, uid));
25772600
for mecid in 0..mechanism_count.into() {
25782601
let netuid_index =
25792602
SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(mecid));
@@ -2631,6 +2654,23 @@ fn test_trim_to_max_allowed_uids() {
26312654
// Actual number of neurons on the network updated after trimming
26322655
assert_eq!(SubnetworkN::<Test>::get(netuid), new_max_n);
26332656

2657+
// Uids match enumeration order
2658+
for i in 0..new_max_n.into() {
2659+
let hotkey = Keys::<Test>::get(netuid, i);
2660+
let uid = Uids::<Test>::get(netuid, hotkey);
2661+
assert_eq!(uid, Some(i));
2662+
}
2663+
2664+
// EVM association have been remapped correctly (uids: 7 -> 2, 14 -> 7)
2665+
assert_eq!(
2666+
AssociatedEvmAddress::<Test>::get(netuid, 2),
2667+
Some((sp_core::H160::from_slice(b"12345678901234567891"), now))
2668+
);
2669+
assert_eq!(
2670+
AssociatedEvmAddress::<Test>::get(netuid, 7),
2671+
Some((sp_core::H160::from_slice(b"12345678901234567894"), now))
2672+
);
2673+
26342674
// Non existent subnet
26352675
assert_err!(
26362676
AdminUtils::sudo_trim_to_max_allowed_uids(
@@ -2673,7 +2713,7 @@ fn test_trim_to_max_allowed_uids_too_many_immune() {
26732713
MaxRegistrationsPerBlock::<Test>::insert(netuid, 256);
26742714
TargetRegistrationsPerInterval::<Test>::insert(netuid, 256);
26752715
ImmuneOwnerUidsLimit::<Test>::insert(netuid, 2);
2676-
MinAllowedUids::<Test>::set(netuid, 4);
2716+
MinAllowedUids::<Test>::set(netuid, 2);
26772717

26782718
// Add 5 neurons
26792719
let max_n = 5;
@@ -2711,7 +2751,7 @@ fn test_trim_to_max_allowed_uids_too_many_immune() {
27112751
netuid,
27122752
4
27132753
),
2714-
pallet_subtensor::Error::<Test>::InvalidValue
2754+
pallet_subtensor::Error::<Test>::TrimmingWouldExceedMaxImmunePercentage
27152755
);
27162756

27172757
// Try to trim to 3 UIDs - this should also fail because 4/3 > 80% immune (>= 80%)
@@ -2721,7 +2761,7 @@ fn test_trim_to_max_allowed_uids_too_many_immune() {
27212761
netuid,
27222762
3
27232763
),
2724-
pallet_subtensor::Error::<Test>::InvalidValue
2764+
pallet_subtensor::Error::<Test>::TrimmingWouldExceedMaxImmunePercentage
27252765
);
27262766

27272767
// Now test a scenario where trimming should succeed
@@ -2733,10 +2773,6 @@ fn test_trim_to_max_allowed_uids_too_many_immune() {
27332773
Uids::<Test>::remove(netuid, hotkey_to_remove);
27342774
BlockAtRegistration::<Test>::remove(netuid, uid_to_remove);
27352775

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

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: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<T: Config> Pallet<T> {
162162
let immune_percentage = Percent::from_rational(immune_count, max_n);
163163
ensure!(
164164
immune_percentage < T::MaxImmuneUidsPercentage::get(),
165-
Error::<T>::InvalidValue
165+
Error::<T>::TrimmingWouldExceedMaxImmunePercentage
166166
);
167167

168168
// Get all emissions with their UIDs and sort by emission (descending)
@@ -209,6 +209,7 @@ impl<T: Config> Pallet<T> {
209209
#[allow(unknown_lints)]
210210
Keys::<T>::remove(netuid, neuron_uid);
211211
BlockAtRegistration::<T>::remove(netuid, neuron_uid);
212+
AssociatedEvmAddress::<T>::remove(netuid, neuron_uid);
212213
for mecid in 0..mechanisms_count {
213214
let netuid_index = Self::get_mechanism_storage_index(netuid, mecid.into());
214215
Weights::<T>::remove(netuid_index, neuron_uid);
@@ -315,6 +316,7 @@ impl<T: Config> Pallet<T> {
315316

316317
// Swap uid specific storage items to new compressed positions
317318
Keys::<T>::swap(netuid, old_neuron_uid, netuid, new_neuron_uid);
319+
AssociatedEvmAddress::<T>::swap(netuid, old_neuron_uid, netuid, new_neuron_uid);
318320
BlockAtRegistration::<T>::swap(netuid, old_neuron_uid, netuid, new_neuron_uid);
319321

320322
for mecid in 0..mechanisms_count {
@@ -352,6 +354,21 @@ impl<T: Config> Pallet<T> {
352354
}
353355
}
354356

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

runtime/src/lib.rs

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

0 commit comments

Comments
 (0)