Skip to content

Commit ef3ce2d

Browse files
reset neuron data on registration
1 parent 4b45cd3 commit ef3ce2d

File tree

5 files changed

+103
-20
lines changed

5 files changed

+103
-20
lines changed

pallets/subtensor/src/subnets/uids.rs

+23
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ impl<T: Config> Pallet<T> {
99
SubnetworkN::<T>::get(netuid)
1010
}
1111

12+
/// Sets value for the element at the given position if it exists.
13+
pub fn set_element_at<N>(vec: &mut [N], position: usize, value: N) {
14+
if let Some(element) = vec.get_mut(position) {
15+
*element = value;
16+
}
17+
}
18+
19+
/// Resets the trust, emission, consensus, incentive, dividends of the neuron to default
20+
pub fn clear_neuron(netuid: u16, neuron_uid: u16) {
21+
let neuron_index: usize = neuron_uid.into();
22+
Emission::<T>::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0));
23+
Trust::<T>::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0));
24+
Consensus::<T>::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0));
25+
Incentive::<T>::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0));
26+
Dividends::<T>::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0));
27+
}
28+
1229
/// Replace the neuron under this uid.
1330
pub fn replace_neuron(
1431
netuid: u16,
@@ -48,6 +65,12 @@ impl<T: Config> Pallet<T> {
4865

4966
// 4. Clear neuron certificates
5067
NeuronCertificates::<T>::remove(netuid, old_hotkey.clone());
68+
69+
// 5. Reset new neuron's values.
70+
Self::clear_neuron(netuid, uid_to_replace);
71+
72+
// 5a. reset axon info for the new uid.
73+
Axons::<T>::remove(netuid, old_hotkey);
5174
}
5275

5376
/// Appends the uid to the network.

pallets/subtensor/tests/uids.rs

+67-7
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,58 @@ fn test_replace_neuron() {
5252
// Get UID
5353
let neuron_uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey_account_id);
5454
assert_ok!(neuron_uid);
55+
let neuron_uid = neuron_uid.unwrap();
56+
57+
// set non-default values
58+
Trust::<Test>::mutate(netuid, |v| {
59+
SubtensorModule::set_element_at(v, neuron_uid as usize, 5u16)
60+
});
61+
Emission::<Test>::mutate(netuid, |v| {
62+
SubtensorModule::set_element_at(v, neuron_uid as usize, 5u64)
63+
});
64+
Consensus::<Test>::mutate(netuid, |v| {
65+
SubtensorModule::set_element_at(v, neuron_uid as usize, 5u16)
66+
});
67+
Incentive::<Test>::mutate(netuid, |v| {
68+
SubtensorModule::set_element_at(v, neuron_uid as usize, 5u16)
69+
});
70+
Dividends::<Test>::mutate(netuid, |v| {
71+
SubtensorModule::set_element_at(v, neuron_uid as usize, 5u16)
72+
});
73+
74+
// serve axon mock address
75+
let ip: u128 = 1676056785;
76+
let port: u16 = 9999;
77+
let ip_type: u8 = 4;
78+
let hotkey = SubtensorModule::get_hotkey_for_net_and_uid(netuid, neuron_uid).unwrap();
79+
assert!(SubtensorModule::serve_axon(
80+
<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id),
81+
netuid,
82+
0,
83+
ip,
84+
port,
85+
ip_type,
86+
0,
87+
0,
88+
0
89+
)
90+
.is_ok());
5591

5692
// Set a neuron certificate for it
5793
NeuronCertificates::<Test>::insert(netuid, hotkey_account_id, certificate);
5894

5995
// Replace the neuron.
60-
SubtensorModule::replace_neuron(
61-
netuid,
62-
neuron_uid.unwrap(),
63-
&new_hotkey_account_id,
64-
block_number,
65-
);
96+
SubtensorModule::replace_neuron(netuid, neuron_uid, &new_hotkey_account_id, block_number);
97+
98+
assert!(!SubtensorModule::has_axon_info(netuid, &hotkey));
6699

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

73-
let curr_hotkey = SubtensorModule::get_hotkey_for_net_and_uid(netuid, neuron_uid.unwrap());
106+
let curr_hotkey = SubtensorModule::get_hotkey_for_net_and_uid(netuid, neuron_uid);
74107
assert_ok!(curr_hotkey);
75108
assert_ne!(curr_hotkey.unwrap(), hotkey_account_id);
76109

@@ -86,6 +119,33 @@ fn test_replace_neuron() {
86119
// Check neuron certificate was reset
87120
let certificate = NeuronCertificates::<Test>::get(netuid, hotkey_account_id);
88121
assert_eq!(certificate, None);
122+
123+
// Check trust, emission, consensus, incentive, dividends have been reset to 0.
124+
assert_eq!(SubtensorModule::get_trust_for_uid(netuid, neuron_uid), 0);
125+
assert_eq!(SubtensorModule::get_emission_for_uid(netuid, neuron_uid), 0);
126+
assert_eq!(
127+
SubtensorModule::get_consensus_for_uid(netuid, neuron_uid),
128+
0
129+
);
130+
assert_eq!(
131+
SubtensorModule::get_incentive_for_uid(netuid, neuron_uid),
132+
0
133+
);
134+
assert_eq!(
135+
SubtensorModule::get_dividends_for_uid(netuid, neuron_uid),
136+
0
137+
);
138+
139+
assert!(!SubtensorModule::has_axon_info(
140+
netuid,
141+
&new_hotkey_account_id
142+
));
143+
144+
// Check axon info is reset.
145+
let axon_info = SubtensorModule::get_axon_info(netuid, &curr_hotkey.unwrap());
146+
assert_eq!(axon_info.ip, 0);
147+
assert_eq!(axon_info.port, 0);
148+
assert_eq!(axon_info.ip_type, 0);
89149
});
90150
}
91151

runtime/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
160160
// `spec_version`, and `authoring_version` are the same between Wasm and native.
161161
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
162162
// the compatible custom types.
163-
spec_version: 208,
163+
spec_version: 209,
164164
impl_version: 1,
165165
apis: RUNTIME_API_VERSIONS,
166166
transaction_version: 1,

support/procedural-fork/src/pallet/parse/storage.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -718,11 +718,11 @@ fn process_generics(
718718
"CountedStorageNMap" => StorageKind::CountedNMap,
719719
found => {
720720
let msg = format!(
721-
"Invalid pallet::storage, expected ident: `StorageValue` or \
721+
"Invalid pallet::storage, expected ident: `StorageValue` or \
722722
`StorageMap` or `CountedStorageMap` or `StorageDoubleMap` or `StorageNMap` or `CountedStorageNMap` \
723723
in order to expand metadata, found `{}`.",
724-
found,
725-
);
724+
found,
725+
);
726726
return Err(syn::Error::new(segment.ident.span(), msg));
727727
}
728728
};

support/procedural-fork/src/runtime/parse/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -255,19 +255,19 @@ impl Def {
255255
};
256256

257257
let def = Def {
258-
input,
259-
runtime_struct: runtime_struct.ok_or_else(|| {
260-
syn::Error::new(item_span,
258+
input,
259+
runtime_struct: runtime_struct.ok_or_else(|| {
260+
syn::Error::new(item_span,
261261
"Missing Runtime. Please add a struct inside the module and annotate it with `#[runtime::runtime]`"
262262
)
263-
})?,
264-
pallets,
265-
runtime_types: runtime_types.ok_or_else(|| {
266-
syn::Error::new(item_span,
263+
})?,
264+
pallets,
265+
runtime_types: runtime_types.ok_or_else(|| {
266+
syn::Error::new(item_span,
267267
"Missing Runtime Types. Please annotate the runtime struct with `#[runtime::derive]`"
268268
)
269-
})?,
270-
};
269+
})?,
270+
};
271271

272272
Ok(def)
273273
}

0 commit comments

Comments
 (0)