use crate::KaspaClient;
use crate::types::{KaspaError, Result};
use std::collections::{HashMap, VecDeque};
use std::time::{Duration, Instant};
pub struct NetworkMonitor {
client: KaspaClient,
metrics: NetworkMetrics,
history: VecDeque<NetworkSnapshot>,
max_history_size: usize,
}
#[derive(Debug, Clone)]
pub struct NetworkMetrics {
pub block_rate: f64, pub transaction_rate: f64, pub peer_count: usize,
pub network_hashrate: f64, pub difficulty: f64,
}
pub struct NetworkSnapshot {
pub timestamp: Instant,
pub block_count: u64,
pub mempool_size: usize,
pub peer_count: usize,
}
impl NetworkMonitor {
pub fn new(client: KaspaClient) -> Self {
Self {
client,
metrics: NetworkMetrics::default(),
history: VecDeque::new(),
max_history_size: 1000,
}
}
pub async fn start_monitoring(&mut self) -> Result<()> {
loop {
if let Err(e) = self.update_metrics().await {
println!("Monitoring error: {}", e);
}
tokio::time::sleep(Duration::from_secs(10)).await;
}
}
async fn update_metrics(&mut self) -> Result<()> {
let snapshot = self.create_snapshot().await?;
self.history.push_back(snapshot);
if self.history.len() > self.max_history_size {
self.history.pop_front();
}
self.calculate_metrics().await?;
Ok(())
}
async fn create_snapshot(&self) -> Result<NetworkSnapshot> {
let info = self.client.get_info().await?;
let mempool_entries = self.client.get_mempool_entries(None).await?;
Ok(NetworkSnapshot {
timestamp: Instant::now(),
block_count: info.block_count,
mempool_size: mempool_entries.len(),
peer_count: self.client.get_connected_peer_info().await.unwrap().len(),
})
}
async fn calculate_metrics(&mut self) -> Result<()> {
if self.history.len() < 2 {
return Ok(());
}
let latest = self.history.back().unwrap();
let previous = self.history.front().unwrap();
let time_diff = latest
.timestamp
.duration_since(previous.timestamp)
.as_secs_f64();
let block_diff = latest.block_count - previous.block_count;
self.metrics.block_rate = block_diff as f64 / time_diff.max(1.0);
self.metrics.peer_count = latest.peer_count;
self.metrics.network_hashrate = self.estimate_network_hashrate().await?;
Ok(())
}
async fn estimate_network_hashrate(&self) -> Result<f64> {
let hashrate_response = self
.client
.estimate_network_hashes_per_second(Some(100))
.await?;
hashrate_response
.network_hashes_per_second
.parse::<f64>()
.map_err(|e| KaspaError::Custom(format!("Failed to parse hashrate: {}", e)))
}
pub fn generate_health_report(&self) -> NetworkHealthReport {
NetworkHealthReport {
metrics: self.metrics.clone(),
is_healthy: self.metrics.block_rate > 0.1,
recommendation: if self.metrics.peer_count < 5 {
"Low peer count, consider adding more connections".to_string()
} else {
"Network healthy".to_string()
},
}
}
}
impl Default for NetworkMetrics {
fn default() -> Self {
Self {
block_rate: 0.0,
transaction_rate: 0.0,
peer_count: 0,
network_hashrate: 0.0,
difficulty: 0.0,
}
}
}
pub struct DetailedNetworkMetrics {
pub inbound_peers: usize,
pub outbound_peers: usize,
pub average_ping: f64,
pub user_agents: HashMap<String, usize>, pub protocol_versions: HashMap<u32, usize>, }
pub struct NetworkHealthReport {
pub metrics: NetworkMetrics,
pub is_healthy: bool,
pub recommendation: String,
}
pub struct DetailedNetworkReport {
pub basic_metrics: NetworkMetrics,
pub peer_metrics: DetailedNetworkMetrics,
pub health_status: bool,
pub recommendations: Vec<String>,
pub timestamp: Instant,
}
impl DetailedNetworkReport {
pub fn display(&self) {
println!("=== Kaspa Network Report ===");
println!(
"Health Status: {}",
if self.health_status {
"✅ Healthy"
} else {
"❌ Unhealthy"
}
);
println!(
"Block Rate: {:.2} blocks/sec",
self.basic_metrics.block_rate
);
println!(
"Transaction Rate: {:.2} tx/sec",
self.basic_metrics.transaction_rate
);
println!(
"Network Hashrate: {:.2} H/s",
self.basic_metrics.network_hashrate
);
println!(
"Connected Peers: {} (In: {}, Out: {})",
self.basic_metrics.peer_count,
self.peer_metrics.inbound_peers,
self.peer_metrics.outbound_peers
);
println!("Average Ping: {:.2} ms", self.peer_metrics.average_ping);
if !self.recommendations.is_empty() {
println!("Recommendations:");
for rec in &self.recommendations {
println!(" - {}", rec);
}
}
}
}