Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reset neuron data on registration #522

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,19 +1001,18 @@ pub mod pallet {
pub(super) type Rank<T: Config> =
StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> trust
pub(super) type Trust<T: Config> =
StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
pub type Trust<T: Config> = StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> consensus
pub(super) type Consensus<T: Config> =
pub type Consensus<T: Config> =
StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> incentive
pub(super) type Incentive<T: Config> =
pub type Incentive<T: Config> =
StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> dividends
pub(super) type Dividends<T: Config> =
pub type Dividends<T: Config> =
StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> emission
pub(super) type Emission<T: Config> =
pub type Emission<T: Config> =
StorageMap<_, Identity, u16, Vec<u64>, ValueQuery, EmptyU64Vec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> last_update
pub(super) type LastUpdate<T: Config> =
Expand Down
28 changes: 28 additions & 0 deletions pallets/subtensor/src/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>)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are almost there! can you please add doc comments to this , as it would break some lints down the line.

I will get this approved today , and move it through our deployment process. I estimate this change would be on testnet in about 2 weeks

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, added.

thanks!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@distributedstatemachine could you take a look at this again?

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
@@ -1,6 +1,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 @@ -48,22 +49,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 @@ -75,6 +99,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
Loading