Skip to content

Commit

Permalink
adding comments
Browse files Browse the repository at this point in the history
  • Loading branch information
VectorChat committed Aug 8, 2024
1 parent c34d72b commit 069a33d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
10 changes: 10 additions & 0 deletions pallets/subtensor/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,10 @@ impl<T: Config> Pallet<T> {
let mut earliest_registration_in_immunity: u64 = u64::MAX;
let mut uid_to_prune: u16 = 0;
let mut uid_to_prune_in_immunity: u16 = 0;

// This boolean is used instead of checking if min_score == u16::MAX, to avoid the case
// where all non-immune neurons have pruning score u16::MAX
// This may be unlikely in practice.
let mut found_non_immune = false;

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

if is_immune {
// if the immune neuron has a lower pruning score than the minimum for immune neurons,
// or, if the pruning scores are equal and the immune neuron was registered earlier than the current minimum for immune neurons,
// then update the minimum pruning score and the uid to prune for immune neurons
if pruning_score < min_score_in_immunity
|| (pruning_score == min_score_in_immunity
&& block_at_registration < earliest_registration_in_immunity)
Expand All @@ -458,6 +465,9 @@ impl<T: Config> Pallet<T> {
}
} else {
found_non_immune = true;
// if the non-immune neuron has a lower pruning score than the minimum for non-immune neurons,
// or, if the pruning scores are equal and the non-immune neuron was registered earlier than the current minimum for non-immune neurons,
// then update the minimum pruning score and the uid to prune for non-immune neurons
if pruning_score < min_score
|| (pruning_score == min_score && block_at_registration < earliest_registration)
{
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ impl<T: Config> Pallet<T> {
ImmunityPeriod::<T>::insert(netuid, immunity_period);
Self::deposit_event(Event::ImmunityPeriodSet(netuid, immunity_period));
}

/// Check if a neuron is in immunity based on the current block
pub fn get_neuron_is_immune(netuid: u16, uid: u16) -> bool {
let registered_at = Self::get_neuron_block_at_registration(netuid, uid);
let current_block = Self::get_current_block_as_u64();
Expand Down
11 changes: 6 additions & 5 deletions pallets/subtensor/tests/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,13 +551,12 @@ fn test_burn_registration_pruning_scenarios() {
let max_allowed_uids = 6;
let immunity_period = 5000;

// Initial setup
SubtensorModule::set_burn(netuid, burn_cost);
SubtensorModule::set_max_allowed_uids(netuid, max_allowed_uids);
SubtensorModule::set_target_registrations_per_interval(netuid, max_allowed_uids);
SubtensorModule::set_immunity_period(netuid, immunity_period);

// SubtensorModule::set_immunity_period(netuid, immunity_period);

add_network(netuid, tempo, 0);

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

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

// 1. Test all immune neurons
// 1. Test if all immune neurons
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 0), true);
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 1), true);
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 2), true);
Expand All @@ -591,12 +590,13 @@ fn test_burn_registration_pruning_scenarios() {
SubtensorModule::set_pruning_score_for_uid(netuid, 1, 50);
SubtensorModule::set_pruning_score_for_uid(netuid, 2, 50);

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

// 3. Test no immune neurons
// 3. Test if no immune neurons
step_block(immunity_period);

// ensure all neurons are non-immune
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 0), false);
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 1), false);
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 2), false);
Expand Down Expand Up @@ -626,6 +626,7 @@ fn test_burn_registration_pruning_scenarios() {
step_block(1);
}

// Ensure all new neurons are immune
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 3), true);
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 4), true);
assert_eq!(SubtensorModule::get_neuron_is_immune(netuid, 5), true);
Expand Down

0 comments on commit 069a33d

Please sign in to comment.