Skip to content

Commit 069a33d

Browse files
committed
adding comments
1 parent c34d72b commit 069a33d

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

pallets/subtensor/src/registration.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ impl<T: Config> Pallet<T> {
434434
let mut earliest_registration_in_immunity: u64 = u64::MAX;
435435
let mut uid_to_prune: u16 = 0;
436436
let mut uid_to_prune_in_immunity: u16 = 0;
437+
438+
// This boolean is used instead of checking if min_score == u16::MAX, to avoid the case
439+
// where all non-immune neurons have pruning score u16::MAX
440+
// This may be unlikely in practice.
437441
let mut found_non_immune = false;
438442

439443
let neurons_n = Self::get_subnetwork_n(netuid);
@@ -448,6 +452,9 @@ impl<T: Config> Pallet<T> {
448452
let is_immune = Self::get_neuron_is_immune(netuid, neuron_uid);
449453

450454
if is_immune {
455+
// if the immune neuron has a lower pruning score than the minimum for immune neurons,
456+
// or, if the pruning scores are equal and the immune neuron was registered earlier than the current minimum for immune neurons,
457+
// then update the minimum pruning score and the uid to prune for immune neurons
451458
if pruning_score < min_score_in_immunity
452459
|| (pruning_score == min_score_in_immunity
453460
&& block_at_registration < earliest_registration_in_immunity)
@@ -458,6 +465,9 @@ impl<T: Config> Pallet<T> {
458465
}
459466
} else {
460467
found_non_immune = true;
468+
// if the non-immune neuron has a lower pruning score than the minimum for non-immune neurons,
469+
// or, if the pruning scores are equal and the non-immune neuron was registered earlier than the current minimum for non-immune neurons,
470+
// then update the minimum pruning score and the uid to prune for non-immune neurons
461471
if pruning_score < min_score
462472
|| (pruning_score == min_score && block_at_registration < earliest_registration)
463473
{

pallets/subtensor/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ impl<T: Config> Pallet<T> {
460460
ImmunityPeriod::<T>::insert(netuid, immunity_period);
461461
Self::deposit_event(Event::ImmunityPeriodSet(netuid, immunity_period));
462462
}
463-
463+
/// Check if a neuron is in immunity based on the current block
464464
pub fn get_neuron_is_immune(netuid: u16, uid: u16) -> bool {
465465
let registered_at = Self::get_neuron_block_at_registration(netuid, uid);
466466
let current_block = Self::get_current_block_as_u64();

pallets/subtensor/tests/registration.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -551,13 +551,12 @@ fn test_burn_registration_pruning_scenarios() {
551551
let max_allowed_uids = 6;
552552
let immunity_period = 5000;
553553

554+
// Initial setup
554555
SubtensorModule::set_burn(netuid, burn_cost);
555556
SubtensorModule::set_max_allowed_uids(netuid, max_allowed_uids);
556557
SubtensorModule::set_target_registrations_per_interval(netuid, max_allowed_uids);
557558
SubtensorModule::set_immunity_period(netuid, immunity_period);
558559

559-
// SubtensorModule::set_immunity_period(netuid, immunity_period);
560-
561560
add_network(netuid, tempo, 0);
562561

563562
let mint_balance = burn_cost * u64::from(max_allowed_uids) + 1_000_000_000;
@@ -575,7 +574,7 @@ fn test_burn_registration_pruning_scenarios() {
575574

576575
// Note: pruning score is set to u16::MAX after getting neuron to prune
577576

578-
// 1. Test all immune neurons
577+
// 1. Test if all immune neurons
579578
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 0), true);
580579
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 1), true);
581580
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 2), true);
@@ -591,12 +590,13 @@ fn test_burn_registration_pruning_scenarios() {
591590
SubtensorModule::set_pruning_score_for_uid(netuid, 1, 50);
592591
SubtensorModule::set_pruning_score_for_uid(netuid, 2, 50);
593592

594-
// Should get the oldest neuron
593+
// Should get the oldest neuron (i.e., neuron that was registered first)
595594
assert_eq!(SubtensorModule::get_neuron_to_prune(netuid), 1);
596595

597-
// 3. Test no immune neurons
596+
// 3. Test if no immune neurons
598597
step_block(immunity_period);
599598

599+
// ensure all neurons are non-immune
600600
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 0), false);
601601
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 1), false);
602602
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 2), false);
@@ -626,6 +626,7 @@ fn test_burn_registration_pruning_scenarios() {
626626
step_block(1);
627627
}
628628

629+
// Ensure all new neurons are immune
629630
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 3), true);
630631
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 4), true);
631632
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 5), true);

0 commit comments

Comments
 (0)