1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! # Neuron Discovery
//!
//! Provides functionality to discover neurons (miners and validators) from the Bittensor metagraph,
//! including their registration status and axon endpoints.
use anyhow::Result;
use std::collections::HashMap;
use std::net::SocketAddr;
use tracing::{debug, info, warn};
use crate::Metagraph;
/// Information about a discovered neuron
#[derive(Debug, Clone)]
pub struct NeuronInfo {
/// The neuron's unique identifier
pub uid: u16,
/// The neuron's hotkey (SS58 address)
pub hotkey: String,
/// The neuron's coldkey (SS58 address)
pub coldkey: String,
/// The neuron's stake amount
pub stake: u64,
/// Whether this neuron is a validator
pub is_validator: bool,
/// The neuron's axon endpoint (if published)
pub axon_info: Option<AxonInfo>,
}
/// Axon endpoint information
#[derive(Debug, Clone)]
pub struct AxonInfo {
/// IP address
pub ip: String,
/// Port number
pub port: u16,
/// Protocol version
pub version: u32,
/// Full socket address
pub socket_addr: SocketAddr,
}
/// Discovers neurons from the metagraph
pub struct NeuronDiscovery<'a> {
metagraph: &'a Metagraph,
}
impl<'a> NeuronDiscovery<'a> {
/// Create a new neuron discovery instance
pub fn new(metagraph: &'a Metagraph) -> Self {
Self { metagraph }
}
/// Get all neurons with their information
pub fn get_all_neurons(&self) -> Result<Vec<NeuronInfo>> {
let mut neurons = Vec::new();
// Iterate through all UIDs based on hotkeys length
for (idx, hotkey) in self.metagraph.hotkeys.iter().enumerate() {
let uid = idx as u16;
if let Some(axon_info) = self.extract_axon_info(uid) {
debug!(
"Found neuron UID {} with axon endpoint {}:{}",
uid, axon_info.ip, axon_info.port
);
}
// Get corresponding data from parallel arrays
let coldkey = self
.metagraph
.coldkeys
.get(idx)
.map(|c| c.to_string())
.unwrap_or_default();
let stake = self
.metagraph
.total_stake
.get(idx)
.map(|s| s.0)
.unwrap_or(0);
let is_validator = self
.metagraph
.validator_permit
.get(idx)
.copied()
.unwrap_or(false);
neurons.push(NeuronInfo {
uid,
hotkey: hotkey.to_string(),
coldkey,
stake,
is_validator,
axon_info: self.extract_axon_info(uid),
});
}
info!("Discovered {} neurons from metagraph", neurons.len());
Ok(neurons)
}
/// Get only validators (neurons with validator permit)
pub fn get_validators(&self) -> Result<Vec<NeuronInfo>> {
let all_neurons = self.get_all_neurons()?;
let validators: Vec<NeuronInfo> =
all_neurons.into_iter().filter(|n| n.is_validator).collect();
info!("Found {} validators in metagraph", validators.len());
Ok(validators)
}
/// Get only miners (neurons without validator permit)
pub fn get_miners(&self) -> Result<Vec<NeuronInfo>> {
let all_neurons = self.get_all_neurons()?;
let miners: Vec<NeuronInfo> = all_neurons
.into_iter()
.filter(|n| !n.is_validator)
.collect();
info!("Found {} miners in metagraph", miners.len());
Ok(miners)
}
/// Get neurons with published axon endpoints
pub fn get_neurons_with_axons(&self) -> Result<Vec<NeuronInfo>> {
let all_neurons = self.get_all_neurons()?;
let with_axons: Vec<NeuronInfo> = all_neurons
.into_iter()
.filter(|n| n.axon_info.is_some())
.collect();
info!("Found {} neurons with axon endpoints", with_axons.len());
Ok(with_axons)
}
/// Find a specific neuron by hotkey
pub fn find_neuron_by_hotkey(&self, hotkey: &str) -> Option<NeuronInfo> {
for (idx, h) in self.metagraph.hotkeys.iter().enumerate() {
if h.to_string() == hotkey {
let uid = idx as u16;
let coldkey = self
.metagraph
.coldkeys
.get(idx)
.map(|c| c.to_string())
.unwrap_or_default();
let stake = self
.metagraph
.total_stake
.get(idx)
.map(|s| s.0)
.unwrap_or(0);
let is_validator = self
.metagraph
.validator_permit
.get(idx)
.copied()
.unwrap_or(false);
return Some(NeuronInfo {
uid,
hotkey: h.to_string(),
coldkey,
stake,
is_validator,
axon_info: self.extract_axon_info(uid),
});
}
}
None
}
/// Find a specific neuron by UID
pub fn find_neuron_by_uid(&self, uid: u16) -> Option<NeuronInfo> {
let idx = uid as usize;
if idx >= self.metagraph.hotkeys.len() {
return None;
}
let hotkey = self.metagraph.hotkeys.get(idx)?;
let coldkey = self
.metagraph
.coldkeys
.get(idx)
.map(|c| c.to_string())
.unwrap_or_default();
let stake = self
.metagraph
.total_stake
.get(idx)
.map(|s| s.0)
.unwrap_or(0);
let is_validator = self
.metagraph
.validator_permit
.get(idx)
.copied()
.unwrap_or(false);
Some(NeuronInfo {
uid,
hotkey: hotkey.to_string(),
coldkey,
stake,
is_validator,
axon_info: self.extract_axon_info(uid),
})
}
/// Check if a hotkey is registered in the metagraph
pub fn is_hotkey_registered(&self, hotkey: &str) -> bool {
self.find_neuron_by_hotkey(hotkey).is_some()
}
/// Get neurons filtered by minimum stake
pub fn get_neurons_by_min_stake(&self, min_stake: u64) -> Result<Vec<NeuronInfo>> {
let all_neurons = self.get_all_neurons()?;
let filtered: Vec<NeuronInfo> = all_neurons
.into_iter()
.filter(|n| n.stake >= min_stake)
.collect();
info!(
"Found {} neurons with stake >= {}",
filtered.len(),
min_stake
);
Ok(filtered)
}
/// Extract axon information for a specific UID
pub fn extract_axon_info(&self, uid: u16) -> Option<AxonInfo> {
self.metagraph.axons.get(uid as usize).and_then(|axon| {
// Check if axon has valid IP and port
if axon.ip == 0 || axon.port == 0 {
return None;
}
// Convert IP to string format based on IP type
let ip_str = if axon.ip_type == 4 {
// IPv4 addresses are stored in the lower 32 bits of the u128
let ipv4_bits = axon.ip as u32;
let ip_bytes = ipv4_bits.to_be_bytes();
format!(
"{}.{}.{}.{}",
ip_bytes[0], ip_bytes[1], ip_bytes[2], ip_bytes[3]
)
} else {
// IPv6 handling - full u128
format!("{:x}", axon.ip)
};
// Validate IP address
if ip_str == "0.0.0.0" || ip_str == "127.0.0.1" {
debug!("Skipping invalid axon IP {} for UID {}", ip_str, uid);
return None;
}
// Create socket address
match format!("{}:{}", ip_str, axon.port).parse::<SocketAddr>() {
Ok(socket_addr) => Some(AxonInfo {
ip: ip_str,
port: axon.port,
version: axon.version,
socket_addr,
}),
Err(e) => {
warn!(
"Failed to parse socket address for UID {}: {}:{} - {}",
uid, ip_str, axon.port, e
);
None
}
}
})
}
}
/// Create a mapping of hotkeys to UIDs for quick lookup
pub fn create_hotkey_to_uid_map(metagraph: &Metagraph) -> HashMap<String, u16> {
let mut map = HashMap::new();
for (idx, hotkey) in metagraph.hotkeys.iter().enumerate() {
map.insert(hotkey.to_string(), idx as u16);
}
map
}
/// Create a mapping of UIDs to axon endpoints
pub fn create_uid_to_axon_map(metagraph: &Metagraph) -> HashMap<u16, SocketAddr> {
let discovery = NeuronDiscovery::new(metagraph);
let mut map = HashMap::new();
for idx in 0..metagraph.hotkeys.len() {
let uid = idx as u16;
if let Some(axon_info) = discovery.extract_axon_info(uid) {
map.insert(uid, axon_info.socket_addr);
}
}
map
}
#[cfg(test)]
mod tests {
#[test]
fn test_ip_conversion() {
// Test IP address conversion from u32 to string
let ip_u32: u32 = 0xC0A80101; // 192.168.1.1
let ip_bytes = ip_u32.to_be_bytes();
let ip_str = format!(
"{}.{}.{}.{}",
ip_bytes[0], ip_bytes[1], ip_bytes[2], ip_bytes[3]
);
assert_eq!(ip_str, "192.168.1.1");
}
}