Skip to content

Commit

Permalink
add relay stats to status line
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Nov 8, 2024
1 parent 420e3e5 commit ead5e56
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 6 deletions.
20 changes: 19 additions & 1 deletion applications/minotari_node/src/commands/command/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl CommandContext {
);

let avg_latency = self.network.get_average_peer_latency().await?;
let relay_stats = self.network.get_relay_stats().await?;

match self.network.get_autonat_status() {
AutonatStatus::ConfiguredPrivate => {
Expand All @@ -137,13 +138,30 @@ impl CommandContext {
status_line.add(avg_latency.to_string());
},
AutonatStatus::Private => {
status_line.add(format!("️🔌 {avg_latency}"));
if relay_stats.current_relay_peer.is_some() {
status_line.add(avg_latency.to_string());
} else {
status_line.add(format!("️🔌 {avg_latency}"));
}
},
AutonatStatus::Public => {
status_line.add(format!("⚡️ {avg_latency}"));
},
}

if relay_stats.num_active_relay_reservations > 0 || relay_stats.num_active_circuits > 0 {
status_line.add("relay");
}
if relay_stats.num_active_relay_reservations > 0 {
status_line.add_field("rsrv", relay_stats.num_active_relay_reservations.to_string());
}
if relay_stats.num_active_circuits > 0 {
status_line.add_field("circ", relay_stats.num_active_circuits.to_string());
}
if relay_stats.current_relay_peer.is_some() {
status_line.add("⚡️");
}

if full_log {
status_line.add_field(
"RandomX",
Expand Down
13 changes: 13 additions & 0 deletions network/core/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ use crate::{
error::NetworkingHandleError,
event::NetworkEvent,
peer::{Peer, PeerInfo},
relay_state::RelayStats,
BannedPeer,
DialWaiter,
DiscoveryResult,
Expand Down Expand Up @@ -172,6 +173,9 @@ pub enum NetworkingRequest {
GetSeedPeers {
reply: Reply<Vec<Peer>>,
},
GetRelayStats {
reply: Reply<RelayStats>,
},
}

#[derive(Debug)]
Expand Down Expand Up @@ -490,6 +494,15 @@ impl NetworkHandle {
rx.await?
}

pub async fn get_relay_stats(&self) -> Result<RelayStats, NetworkError> {
let (tx, rx) = oneshot::channel();
self.tx_request
.send(NetworkingRequest::GetRelayStats { reply: tx })
.await
.map_err(|_| NetworkingHandleError::ServiceHasShutdown)?;
rx.await?
}

pub async fn wait_until_shutdown(&self) {
self.tx_request.closed().await;
}
Expand Down
1 change: 1 addition & 0 deletions network/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub use handle::*;
pub use message::*;
pub use messaging::*;
pub use peer::*;
pub use relay_state::*;
pub use service_trait::*;
pub use spawn::*;
pub use tari_swarm::{
Expand Down
11 changes: 9 additions & 2 deletions network/core/src/relay_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tari_swarm::libp2p::{multiaddr::Protocol, Multiaddr, PeerId};
use crate::Peer;

#[derive(Debug, Clone, Default)]
pub struct RelayState {
pub(crate) struct RelayState {
selected_relay: Option<RelayPeer>,
possible_relays: HashMap<PeerId, HashSet<Multiaddr>>,
}
Expand Down Expand Up @@ -99,9 +99,16 @@ impl RelayState {
}

#[derive(Debug, Clone)]
pub struct RelayPeer {
pub(crate) struct RelayPeer {
pub peer_id: PeerId,
pub addresses: Vec<Multiaddr>,
pub is_circuit_established: bool,
pub remote_address: Option<Multiaddr>,
}

#[derive(Debug, Clone, Copy, Default)]
pub struct RelayStats {
pub num_active_relay_reservations: usize,
pub num_active_circuits: usize,
pub current_relay_peer: Option<PeerId>,
}
35 changes: 34 additions & 1 deletion network/core/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use crate::{
handle::NetworkingRequest,
messaging::MessagingRequest,
notify::Notifiers,
relay_state::RelayState,
relay_state::{RelayState, RelayStats},
AveragePeerLatency,
BannedPeer,
ConnectionDirection,
Expand Down Expand Up @@ -102,6 +102,7 @@ where
gossipsub_outbound_rx: Option<mpsc::Receiver<(IdentTopic, Vec<u8>)>>,
config: crate::Config,
relays: RelayState,
relay_stats: RelayStats,
seed_peers: Vec<Peer>,
added_peers: HashMap<PeerId, Peer>,
autonat_status_sender: watch::Sender<AutonatStatus>,
Expand Down Expand Up @@ -140,6 +141,7 @@ where
pending_dial_requests: HashMap::new(),
pending_kad_queries: HashMap::new(),
relays: RelayState::new(known_relay_nodes),
relay_stats: RelayStats::default(),
seed_peers,
added_peers: HashMap::new(),
swarm,
Expand Down Expand Up @@ -543,6 +545,9 @@ where
NetworkingRequest::GetSeedPeers { reply } => {
let _ignore = reply.send(Ok(self.seed_peers.clone()));
},
NetworkingRequest::GetRelayStats { reply } => {
let _ignore = reply.send(Ok(self.relay_stats));
},
}
}

Expand Down Expand Up @@ -707,6 +712,10 @@ where
}
shrink_hashmap_if_required(&mut self.active_connections);

if self.relay_stats.current_relay_peer == Some(peer_id) {
self.relay_stats.current_relay_peer = None;
}

self.publish_event(NetworkEvent::PeerDisconnected { peer_id });
},
SwarmEvent::OutgoingConnectionError {
Expand Down Expand Up @@ -806,13 +815,15 @@ where
"🌍️ Relay accepted our reservation request: peer_id={}, renewal={:?}, limit={:?}",
relay_peer_id, renewal, limit
);
self.relay_stats.current_relay_peer = Some(relay_peer_id)
},

RelayClient(event) => {
info!(target: LOG_TARGET, "🌎️ RelayClient event: {:?}", event);
},
Relay(event) => {
info!(target: LOG_TARGET, "ℹ️ Relay event: {:?}", event);
self.on_relay_event(&event);
},
Gossipsub(gossipsub::Event::Message {
message_id,
Expand Down Expand Up @@ -1186,6 +1197,28 @@ where
Ok(())
}

fn on_relay_event(&mut self, event: &relay::Event) {
#[allow(clippy::enum_glob_use)]
use relay::Event::*;
match event {
ReservationReqAccepted { .. } => {
self.relay_stats.num_active_relay_reservations += 1;
},
ReservationReqDenied { .. } => {},
ReservationTimedOut { .. } => {
self.relay_stats.num_active_relay_reservations -= 1;
},
CircuitReqDenied { .. } => {},
CircuitReqAccepted { .. } => {
self.relay_stats.num_active_circuits += 1;
},
CircuitClosed { .. } => {
self.relay_stats.num_active_circuits -= 1;
},
_ => {},
}
}

fn update_connected_peers(
&mut self,
peer_id: &PeerId,
Expand Down
4 changes: 2 additions & 2 deletions network/swarm/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl Default for Config {
relay_reservation_limits: RelayReservationLimits::default(),
// This is the default for identify
identify_interval: Duration::from_secs(5 * 60),
// Double the libp2p default
gossipsub_max_message_size: 128 * 1024,
// 64Kib is the libp2p default
gossipsub_max_message_size: 64 * 1024,
}
}
}
Expand Down

0 comments on commit ead5e56

Please sign in to comment.