Skip to content

Commit 86c636a

Browse files
reset neuron data on deregistration
1 parent 2069a48 commit 86c636a

File tree

2 files changed

+86
-7
lines changed

2 files changed

+86
-7
lines changed

pallets/subtensor/src/subnets/uids.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ impl<T: Config> Pallet<T> {
99
SubnetworkN::<T>::get(netuid)
1010
}
1111

12+
/// Returns a callback that sets the element at the given position to zero, doing nothing if the
13+
/// position is out of bounds
14+
fn clear_element_at<N>(position: u16) -> impl Fn(&mut Vec<N>)
15+
where
16+
N: From<u8>,
17+
{
18+
move |vec: &mut Vec<N>| {
19+
if vec.len() > position as usize {
20+
vec[position as usize] = N::from(0);
21+
};
22+
}
23+
}
24+
25+
/// Resets the trust, emission, consensus, incentive, dividends of the neuron to default
26+
pub fn clear_neuron(netuid: u16, neuron_uid: u16) {
27+
Emission::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
28+
Trust::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
29+
Consensus::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
30+
Incentive::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
31+
Dividends::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
32+
}
33+
1234
/// Replace the neuron under this uid.
1335
pub fn replace_neuron(
1436
netuid: u16,
@@ -45,6 +67,12 @@ impl<T: Config> Pallet<T> {
4567
Uids::<T>::insert(netuid, new_hotkey.clone(), uid_to_replace); // Make uid - hotkey association.
4668
BlockAtRegistration::<T>::insert(netuid, uid_to_replace, block_number); // Fill block at registration.
4769
IsNetworkMember::<T>::insert(new_hotkey.clone(), netuid, true); // Fill network is member.
70+
71+
// 4. Reset new neuron's values.
72+
Self::clear_neuron(netuid, uid_to_replace);
73+
74+
// 4a. reset axon info for the new uid.
75+
Axons::<T>::remove(netuid, old_hotkey);
4876
}
4977

5078
/// Appends the uid to the network.

pallets/subtensor/tests/uids.rs

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::mock::*;
44
use frame_support::assert_ok;
55
use frame_system::Config;
6+
use pallet_subtensor::*;
67
use sp_core::U256;
78

89
mod mock;
@@ -50,22 +51,45 @@ fn test_replace_neuron() {
5051
// Get UID
5152
let neuron_uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey_account_id);
5253
assert_ok!(neuron_uid);
54+
let neuron_uid = neuron_uid.unwrap();
55+
56+
// set non-default values
57+
Trust::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u16);
58+
Emission::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u64);
59+
Consensus::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u16);
60+
Incentive::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u16);
61+
Dividends::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u16);
62+
63+
// serve axon mock address
64+
let ip: u128 = 1676056785;
65+
let port: u16 = 9999;
66+
let ip_type: u8 = 4;
67+
let hotkey = SubtensorModule::get_hotkey_for_net_and_uid(netuid, neuron_uid).unwrap();
68+
assert!(SubtensorModule::serve_axon(
69+
<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id),
70+
netuid,
71+
0,
72+
ip,
73+
port,
74+
ip_type,
75+
0,
76+
0,
77+
0
78+
)
79+
.is_ok());
5380

5481
// Replace the neuron.
55-
SubtensorModule::replace_neuron(
56-
netuid,
57-
neuron_uid.unwrap(),
58-
&new_hotkey_account_id,
59-
block_number,
60-
);
82+
SubtensorModule::replace_neuron(netuid, neuron_uid, &new_hotkey_account_id, block_number);
83+
84+
assert!(!SubtensorModule::has_axon_info(netuid, &hotkey));
6185

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

68-
let curr_hotkey = SubtensorModule::get_hotkey_for_net_and_uid(netuid, neuron_uid.unwrap());
92+
let curr_hotkey = SubtensorModule::get_hotkey_for_net_and_uid(netuid, neuron_uid);
6993
assert_ok!(curr_hotkey);
7094
assert_ne!(curr_hotkey.unwrap(), hotkey_account_id);
7195

@@ -77,6 +101,33 @@ fn test_replace_neuron() {
77101
&new_hotkey_account_id
78102
));
79103
assert_eq!(curr_hotkey.unwrap(), new_hotkey_account_id);
104+
105+
// Check trust, emission, consensus, incentive, dividends have been reset to 0.
106+
assert_eq!(SubtensorModule::get_trust_for_uid(netuid, neuron_uid), 0);
107+
assert_eq!(SubtensorModule::get_emission_for_uid(netuid, neuron_uid), 0);
108+
assert_eq!(
109+
SubtensorModule::get_consensus_for_uid(netuid, neuron_uid),
110+
0
111+
);
112+
assert_eq!(
113+
SubtensorModule::get_incentive_for_uid(netuid, neuron_uid),
114+
0
115+
);
116+
assert_eq!(
117+
SubtensorModule::get_dividends_for_uid(netuid, neuron_uid),
118+
0
119+
);
120+
121+
assert!(!SubtensorModule::has_axon_info(
122+
netuid,
123+
&new_hotkey_account_id
124+
));
125+
126+
// Check axon info is reset.
127+
let axon_info = SubtensorModule::get_axon_info(netuid, &curr_hotkey.unwrap());
128+
assert_eq!(axon_info.ip, 0);
129+
assert_eq!(axon_info.port, 0);
130+
assert_eq!(axon_info.ip_type, 0);
80131
});
81132
}
82133

0 commit comments

Comments
 (0)