Skip to content

Commit 5126a38

Browse files
authored
Merge pull request #2212 from opentensor/clear-axon-prom
Clear unknown neuron axon/prom/cert
2 parents 9580005 + 5ad09d1 commit 5126a38

File tree

6 files changed

+173
-31
lines changed

6 files changed

+173
-31
lines changed

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ mod hooks {
162162
// Migrate pending emissions
163163
.saturating_add(migrations::migrate_pending_emissions::migrate_pending_emissions::<T>())
164164
// Reset unactive subnets
165-
.saturating_add(migrations::migrate_reset_unactive_sn::migrate_reset_unactive_sn::<T>());
165+
.saturating_add(migrations::migrate_reset_unactive_sn::migrate_reset_unactive_sn::<T>())
166+
// Remove unknown neuron axon, certificate prom
167+
.saturating_add(migrations::migrate_remove_unknown_neuron_axon_cert_prom::migrate_remove_unknown_neuron_axon_cert_prom::<T>());
166168
weight
167169
}
168170

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use super::*;
2+
use crate::HasMigrationRun;
3+
use frame_support::{traits::Get, weights::Weight};
4+
use scale_info::prelude::string::String;
5+
use sp_std::collections::btree_set::BTreeSet;
6+
7+
pub fn migrate_remove_unknown_neuron_axon_cert_prom<T: Config>() -> Weight {
8+
let migration_name = b"migrate_remove_neuron_axon_cert_prom".to_vec();
9+
let mut weight: Weight = T::DbWeight::get().reads(1);
10+
11+
// Skip if already executed
12+
if HasMigrationRun::<T>::get(&migration_name) {
13+
log::info!(
14+
target: "runtime",
15+
"Migration '{}' already run - skipping.",
16+
String::from_utf8_lossy(&migration_name)
17+
);
18+
return weight;
19+
}
20+
log::info!(
21+
"Running migration '{}'",
22+
String::from_utf8_lossy(&migration_name)
23+
);
24+
25+
for network in NetworksAdded::<T>::iter_keys() {
26+
weight.saturating_accrue(T::DbWeight::get().reads(1));
27+
28+
let hotkeys = BTreeSet::from_iter(Uids::<T>::iter_key_prefix(network));
29+
weight.saturating_accrue(T::DbWeight::get().reads(hotkeys.len() as u64));
30+
31+
// Axons
32+
let axons = Axons::<T>::iter_key_prefix(network).collect::<Vec<_>>();
33+
weight.saturating_accrue(T::DbWeight::get().reads(axons.len() as u64));
34+
let mut cleaned_axons: u32 = 0;
35+
for axon_hotkey in axons {
36+
if !hotkeys.contains(&axon_hotkey) {
37+
Axons::<T>::remove(network, &axon_hotkey);
38+
cleaned_axons = cleaned_axons.saturating_add(1);
39+
}
40+
}
41+
weight.saturating_accrue(T::DbWeight::get().writes(cleaned_axons as u64));
42+
43+
// Neuron Certificates
44+
let certificates = NeuronCertificates::<T>::iter_key_prefix(network).collect::<Vec<_>>();
45+
weight.saturating_accrue(T::DbWeight::get().reads(certificates.len() as u64));
46+
let mut cleaned_certificates: u32 = 0;
47+
for certificate_hotkey in certificates {
48+
if !hotkeys.contains(&certificate_hotkey) {
49+
NeuronCertificates::<T>::remove(network, &certificate_hotkey);
50+
cleaned_certificates = cleaned_certificates.saturating_add(1);
51+
}
52+
}
53+
weight.saturating_accrue(T::DbWeight::get().writes(cleaned_certificates as u64));
54+
55+
// Prometheus
56+
let prometheus = Prometheus::<T>::iter_key_prefix(network).collect::<Vec<_>>();
57+
weight.saturating_accrue(T::DbWeight::get().reads(prometheus.len() as u64));
58+
let mut cleaned_prometheus: u32 = 0;
59+
for prometheus_hotkey in prometheus {
60+
if !hotkeys.contains(&prometheus_hotkey) {
61+
Prometheus::<T>::remove(network, &prometheus_hotkey);
62+
cleaned_prometheus = cleaned_prometheus.saturating_add(1);
63+
}
64+
}
65+
weight.saturating_accrue(T::DbWeight::get().writes(cleaned_prometheus as u64));
66+
67+
log::info!(
68+
"Cleaned {cleaned_axons} axons, {cleaned_certificates} neuron certificates, {cleaned_prometheus} prometheus for network {network}"
69+
);
70+
}
71+
72+
HasMigrationRun::<T>::insert(&migration_name, true);
73+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
74+
75+
log::info!(
76+
"Migration '{:?}' completed successfully.",
77+
String::from_utf8_lossy(&migration_name)
78+
);
79+
80+
log::info!("{weight:#?} weight");
81+
82+
weight
83+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub mod migrate_remove_network_modality;
3737
pub mod migrate_remove_stake_map;
3838
pub mod migrate_remove_tao_dividends;
3939
pub mod migrate_remove_total_hotkey_coldkey_stakes_this_interval;
40+
pub mod migrate_remove_unknown_neuron_axon_cert_prom;
4041
pub mod migrate_remove_unused_maps_and_values;
4142
pub mod migrate_remove_zero_total_hotkey_alpha;
4243
pub mod migrate_reset_bonds_moving_average;

pallets/subtensor/src/subnets/uids.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,13 @@ impl<T: Config> Pallet<T> {
8888
BlockAtRegistration::<T>::insert(netuid, uid_to_replace, block_number); // Fill block at registration.
8989
IsNetworkMember::<T>::insert(new_hotkey.clone(), netuid, true); // Fill network is member.
9090

91-
// 4. Clear neuron certificates
92-
NeuronCertificates::<T>::remove(netuid, old_hotkey.clone());
91+
// 4. Clear neuron axons, certificates and prometheus info
92+
Axons::<T>::remove(netuid, &old_hotkey);
93+
NeuronCertificates::<T>::remove(netuid, &old_hotkey);
94+
Prometheus::<T>::remove(netuid, &old_hotkey);
9395

9496
// 5. Reset new neuron's values.
9597
Self::clear_neuron(netuid, uid_to_replace);
96-
97-
// 5a. reset axon info for the new uid.
98-
Axons::<T>::remove(netuid, old_hotkey);
9998
}
10099

101100
/// Appends the uid to the network.

pallets/subtensor/src/tests/migration.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,3 +2724,69 @@ fn test_migrate_reset_unactive_sn_idempotence() {
27242724
assert_eq!(TotalIssuance::<Test>::get(), total_issuance_before);
27252725
});
27262726
}
2727+
2728+
#[test]
2729+
fn test_migrate_remove_unknown_neuron_axon_cert_prom() {
2730+
use crate::migrations::migrate_remove_unknown_neuron_axon_cert_prom::*;
2731+
const MIGRATION_NAME: &[u8] = b"migrate_remove_neuron_axon_cert_prom";
2732+
2733+
new_test_ext(1).execute_with(|| {
2734+
setup_for(NetUid::from(2), 64, 1231);
2735+
setup_for(NetUid::from(42), 256, 15151);
2736+
setup_for(NetUid::from(99), 1024, 32323);
2737+
assert!(!HasMigrationRun::<Test>::get(MIGRATION_NAME));
2738+
2739+
let w = migrate_remove_unknown_neuron_axon_cert_prom::<Test>();
2740+
assert!(!w.is_zero(), "Weight must be non-zero");
2741+
2742+
assert!(HasMigrationRun::<Test>::get(MIGRATION_NAME));
2743+
assert_for(NetUid::from(2), 64, 1231);
2744+
assert_for(NetUid::from(42), 256, 15151);
2745+
assert_for(NetUid::from(99), 1024, 32323);
2746+
});
2747+
2748+
fn setup_for(netuid: NetUid, uids: u32, items: u32) {
2749+
NetworksAdded::<Test>::insert(netuid, true);
2750+
2751+
for i in 1u32..=uids {
2752+
let hk = U256::from(netuid.inner() as u32 * 1000 + i);
2753+
Uids::<Test>::insert(netuid, hk, i as u16);
2754+
}
2755+
2756+
for i in 1u32..=items {
2757+
let hk = U256::from(netuid.inner() as u32 * 1000 + i);
2758+
Axons::<Test>::insert(netuid, hk, AxonInfo::default());
2759+
NeuronCertificates::<Test>::insert(netuid, hk, NeuronCertificate::default());
2760+
Prometheus::<Test>::insert(netuid, hk, PrometheusInfo::default());
2761+
}
2762+
}
2763+
2764+
fn assert_for(netuid: NetUid, uids: u32, items: u32) {
2765+
assert_eq!(
2766+
Axons::<Test>::iter_key_prefix(netuid).count(),
2767+
uids as usize
2768+
);
2769+
assert_eq!(
2770+
NeuronCertificates::<Test>::iter_key_prefix(netuid).count(),
2771+
uids as usize
2772+
);
2773+
assert_eq!(
2774+
Prometheus::<Test>::iter_key_prefix(netuid).count(),
2775+
uids as usize
2776+
);
2777+
2778+
for i in 1u32..=uids {
2779+
let hk = U256::from(netuid.inner() as u32 * 1000 + i);
2780+
assert!(Axons::<Test>::contains_key(netuid, hk));
2781+
assert!(NeuronCertificates::<Test>::contains_key(netuid, hk));
2782+
assert!(Prometheus::<Test>::contains_key(netuid, hk));
2783+
}
2784+
2785+
for i in uids + 1u32..=items {
2786+
let hk = U256::from(netuid.inner() as u32 * 1000 + i);
2787+
assert!(!Axons::<Test>::contains_key(netuid, hk));
2788+
assert!(!NeuronCertificates::<Test>::contains_key(netuid, hk));
2789+
assert!(!Prometheus::<Test>::contains_key(netuid, hk));
2790+
}
2791+
}
2792+
}

pallets/subtensor/src/tests/uids.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ fn test_replace_neuron() {
3232

3333
let new_hotkey_account_id = U256::from(2);
3434
let _new_colkey_account_id = U256::from(12345);
35-
let certificate = NeuronCertificate::try_from(vec![1, 2, 3]).unwrap();
3635
let evm_address = H160::from_slice(&[1_u8; 20]);
3736
//add network
3837
add_network(netuid, tempo, 0);
@@ -71,28 +70,15 @@ fn test_replace_neuron() {
7170
});
7271
Bonds::<Test>::insert(NetUidStorageIndex::from(netuid), neuron_uid, vec![(0, 1)]);
7372

74-
// serve axon mock address
75-
let ip: u128 = 1676056785;
76-
let port: u16 = 9999;
77-
let ip_type: u8 = 4;
78-
assert!(
79-
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()
73+
Axons::<Test>::insert(netuid, hotkey_account_id, AxonInfoOf::default());
74+
NeuronCertificates::<Test>::insert(
75+
netuid,
76+
hotkey_account_id,
77+
NeuronCertificateOf::default(),
9178
);
92-
93-
// Set a neuron certificate for it
94-
NeuronCertificates::<Test>::insert(netuid, hotkey_account_id, certificate);
79+
Prometheus::<Test>::insert(netuid, hotkey_account_id, PrometheusInfoOf::default());
9580
AssociatedEvmAddress::<Test>::insert(netuid, neuron_uid, (evm_address, 1));
81+
9682
// Replace the neuron.
9783
SubtensorModule::replace_neuron(netuid, neuron_uid, &new_hotkey_account_id, block_number);
9884

@@ -139,10 +125,15 @@ fn test_replace_neuron() {
139125
);
140126

141127
// Check axon info is reset.
142-
let axon_info = SubtensorModule::get_axon_info(netuid, &curr_hotkey.unwrap());
143-
assert_eq!(axon_info.ip, 0);
144-
assert_eq!(axon_info.port, 0);
145-
assert_eq!(axon_info.ip_type, 0);
128+
assert!(!Axons::<Test>::contains_key(netuid, curr_hotkey.unwrap()));
129+
assert!(!NeuronCertificates::<Test>::contains_key(
130+
netuid,
131+
curr_hotkey.unwrap()
132+
));
133+
assert!(!Prometheus::<Test>::contains_key(
134+
netuid,
135+
curr_hotkey.unwrap()
136+
));
146137

147138
// Check bonds are cleared.
148139
assert_eq!(

0 commit comments

Comments
 (0)