Skip to content

Commit

Permalink
reset neuron data on deregistration
Browse files Browse the repository at this point in the history
  • Loading branch information
andreea-popescu-reef committed Sep 15, 2024
1 parent 2069a48 commit 86c636a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 7 deletions.
28 changes: 28 additions & 0 deletions pallets/subtensor/src/subnets/uids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@ impl<T: Config> Pallet<T> {
SubnetworkN::<T>::get(netuid)
}

/// Returns a callback that sets the element at the given position to zero, doing nothing if the
/// position is out of bounds
fn clear_element_at<N>(position: u16) -> impl Fn(&mut Vec<N>)
where
N: From<u8>,
{
move |vec: &mut Vec<N>| {
if vec.len() > position as usize {
vec[position as usize] = N::from(0);
};
}
}

/// Resets the trust, emission, consensus, incentive, dividends of the neuron to default
pub fn clear_neuron(netuid: u16, neuron_uid: u16) {
Emission::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
Trust::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
Consensus::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
Incentive::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
Dividends::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
}

/// Replace the neuron under this uid.
pub fn replace_neuron(
netuid: u16,
Expand Down Expand Up @@ -45,6 +67,12 @@ impl<T: Config> Pallet<T> {
Uids::<T>::insert(netuid, new_hotkey.clone(), uid_to_replace); // Make uid - hotkey association.
BlockAtRegistration::<T>::insert(netuid, uid_to_replace, block_number); // Fill block at registration.
IsNetworkMember::<T>::insert(new_hotkey.clone(), netuid, true); // Fill network is member.

// 4. Reset new neuron's values.
Self::clear_neuron(netuid, uid_to_replace);

// 4a. reset axon info for the new uid.
Axons::<T>::remove(netuid, old_hotkey);
}

/// Appends the uid to the network.
Expand Down
65 changes: 58 additions & 7 deletions pallets/subtensor/tests/uids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::mock::*;
use frame_support::assert_ok;
use frame_system::Config;
use pallet_subtensor::*;
use sp_core::U256;

mod mock;
Expand Down Expand Up @@ -50,22 +51,45 @@ fn test_replace_neuron() {
// Get UID
let neuron_uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey_account_id);
assert_ok!(neuron_uid);
let neuron_uid = neuron_uid.unwrap();

// set non-default values
Trust::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u16);
Emission::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u64);
Consensus::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u16);
Incentive::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u16);
Dividends::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u16);

// serve axon mock address
let ip: u128 = 1676056785;
let port: u16 = 9999;
let ip_type: u8 = 4;
let hotkey = SubtensorModule::get_hotkey_for_net_and_uid(netuid, neuron_uid).unwrap();
assert!(SubtensorModule::serve_axon(
<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id),
netuid,
0,
ip,
port,
ip_type,
0,
0,
0
)
.is_ok());

// Replace the neuron.
SubtensorModule::replace_neuron(
netuid,
neuron_uid.unwrap(),
&new_hotkey_account_id,
block_number,
);
SubtensorModule::replace_neuron(netuid, neuron_uid, &new_hotkey_account_id, block_number);

assert!(!SubtensorModule::has_axon_info(netuid, &hotkey));

// Check old hotkey is not registered on any network.
assert!(SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey_account_id).is_err());
assert!(!SubtensorModule::is_hotkey_registered_on_any_network(
&hotkey_account_id
));

let curr_hotkey = SubtensorModule::get_hotkey_for_net_and_uid(netuid, neuron_uid.unwrap());
let curr_hotkey = SubtensorModule::get_hotkey_for_net_and_uid(netuid, neuron_uid);
assert_ok!(curr_hotkey);
assert_ne!(curr_hotkey.unwrap(), hotkey_account_id);

Expand All @@ -77,6 +101,33 @@ fn test_replace_neuron() {
&new_hotkey_account_id
));
assert_eq!(curr_hotkey.unwrap(), new_hotkey_account_id);

// Check trust, emission, consensus, incentive, dividends have been reset to 0.
assert_eq!(SubtensorModule::get_trust_for_uid(netuid, neuron_uid), 0);
assert_eq!(SubtensorModule::get_emission_for_uid(netuid, neuron_uid), 0);
assert_eq!(
SubtensorModule::get_consensus_for_uid(netuid, neuron_uid),
0
);
assert_eq!(
SubtensorModule::get_incentive_for_uid(netuid, neuron_uid),
0
);
assert_eq!(
SubtensorModule::get_dividends_for_uid(netuid, neuron_uid),
0
);

assert!(!SubtensorModule::has_axon_info(
netuid,
&new_hotkey_account_id
));

// Check axon info is reset.
let axon_info = SubtensorModule::get_axon_info(netuid, &curr_hotkey.unwrap());
assert_eq!(axon_info.ip, 0);
assert_eq!(axon_info.port, 0);
assert_eq!(axon_info.ip_type, 0);
});
}

Expand Down

0 comments on commit 86c636a

Please sign in to comment.