NAC_Blockchain/ops/nac-daemon/src/network.rs

49 lines
1.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// nac-daemon/src/network.rs
// NAC 网络模块 - NAC_lens 协议节点发现和连接
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeerInfo {
pub node_id: String,
pub address: String,
pub port: u16,
pub version: String,
pub latency_ms: u64,
pub connected: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkStats {
pub connected_peers: usize,
pub total_known_peers: usize,
pub inbound: usize,
pub outbound: usize,
pub bandwidth_in_kbps: f64,
pub bandwidth_out_kbps: f64,
}
/// 获取已知引导节点列表NAC_lens 协议)
pub fn get_bootstrap_nodes() -> Vec<PeerInfo> {
vec![
PeerInfo {
node_id: "NAC_NODE_BOOTSTRAP_01".to_string(),
address: "103.96.148.7".to_string(),
port: 9090,
version: "NAC_lens/4.0-alpha".to_string(),
latency_ms: 0,
connected: false,
},
]
}
/// 检查节点连通性
pub fn ping_node(address: &str, port: u16) -> bool {
use std::net::TcpStream;
use std::time::Duration;
TcpStream::connect_timeout(
&format!("{}:{}", address, port).parse().expect("mainnet: handle error"),
Duration::from_secs(2),
).is_ok()
}