Skip to content

Commit 14bde42

Browse files
ricomateojrchatruc
andauthored
show peer stats after sync (#4086)
**Motivation** Currently, the peer stats are only logged during the sync phase. However we also want to see the peer stats after the node has finished syncing. **Description** This PR updates the `periodically_show_peer_stats` function to improve peer stats logs: * **During sync:** peer stats are logged together with the sync progress. * **After sync:** peer stats continue to be logged independently. --------- Co-authored-by: Javier Chatruc <jrchatruc@gmail.com>
1 parent f0abb23 commit 14bde42

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

cmd/ethrex/initializers.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub async fn init_network(
191191
signer,
192192
peer_table.clone(),
193193
store,
194-
blockchain,
194+
blockchain.clone(),
195195
get_client_version(),
196196
based_context,
197197
);
@@ -200,7 +200,10 @@ pub async fn init_network(
200200
.await
201201
.expect("Network starts");
202202

203-
tracker.spawn(ethrex_p2p::periodically_show_peer_stats());
203+
tracker.spawn(ethrex_p2p::periodically_show_peer_stats(
204+
blockchain,
205+
peer_table.peers.clone(),
206+
));
204207
}
205208

206209
#[cfg(feature = "dev")]

crates/networking/p2p/network.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@ use crate::{
22
discv4::{
33
server::{DiscoveryServer, DiscoveryServerError},
44
side_car::{DiscoverySideCar, DiscoverySideCarError},
5-
}, kademlia::Kademlia, metrics::METRICS, rlpx::{
5+
},
6+
kademlia::{Kademlia, PeerData},
7+
metrics::METRICS,
8+
rlpx::{
69
connection::server::{RLPxConnBroadcastSender, RLPxConnection},
710
initiator::{RLPxInitiator, RLPxInitiatorError},
811
l2::l2_connection::P2PBasedContext,
912
message::Message,
10-
}, tx_broadcaster::{TxBroadcaster, TxBroadcasterError}, types::{Node, NodeRecord}
13+
p2p::SUPPORTED_SNAP_CAPABILITIES,
14+
},
15+
tx_broadcaster::{TxBroadcaster, TxBroadcasterError},
16+
types::{Node, NodeRecord},
1117
};
1218
use ethrex_blockchain::Blockchain;
19+
use ethrex_common::H256;
1320
use ethrex_storage::Store;
1421
use secp256k1::SecretKey;
1522
use std::{
23+
collections::BTreeMap,
1624
io,
1725
net::SocketAddr,
1826
sync::Arc,
@@ -170,9 +178,20 @@ fn listener(tcp_addr: SocketAddr) -> Result<TcpListener, io::Error> {
170178
tcp_socket.listen(50)
171179
}
172180

173-
pub async fn periodically_show_peer_stats() {
181+
pub async fn periodically_show_peer_stats(
182+
blockchain: Arc<Blockchain>,
183+
peers: Arc<Mutex<BTreeMap<H256, PeerData>>>,
184+
) {
185+
periodically_show_peer_stats_during_syncing(blockchain).await;
186+
periodically_show_peer_stats_after_sync(peers).await;
187+
}
188+
189+
pub async fn periodically_show_peer_stats_during_syncing(blockchain: Arc<Blockchain>) {
174190
let start = std::time::Instant::now();
175191
loop {
192+
if blockchain.is_synced() {
193+
return;
194+
}
176195
let metrics_enabled = *METRICS.enabled.lock().await;
177196
// Show the metrics only when these are enabled
178197
if !metrics_enabled {
@@ -419,6 +438,33 @@ bytecodes progress: {bytecodes_download_progress} (total: {bytecodes_to_download
419438
}
420439
}
421440

441+
/// Shows the amount of connected peers, active peers, and peers suitable for snap sync on a set interval
442+
pub async fn periodically_show_peer_stats_after_sync(peers: Arc<Mutex<BTreeMap<H256, PeerData>>>) {
443+
const INTERVAL_DURATION: tokio::time::Duration = tokio::time::Duration::from_secs(60);
444+
let mut interval = tokio::time::interval(INTERVAL_DURATION);
445+
loop {
446+
// clone peers to keep the lock short
447+
let peers: Vec<PeerData> = peers.lock().await.values().cloned().collect();
448+
let active_peers = peers
449+
.iter()
450+
.filter(|peer| -> bool { peer.channels.as_ref().is_some() })
451+
.count();
452+
let snap_active_peers = peers
453+
.iter()
454+
.filter(|peer| -> bool {
455+
peer.channels.as_ref().is_some()
456+
&& SUPPORTED_SNAP_CAPABILITIES
457+
.iter()
458+
.any(|cap| peer.supported_capabilities.contains(cap))
459+
})
460+
.count();
461+
info!(
462+
"Snap Peers: {snap_active_peers} / Total Peers: {active_peers}"
463+
);
464+
interval.tick().await;
465+
}
466+
}
467+
422468
fn format_duration(duration: Duration) -> String {
423469
let total_seconds = duration.as_secs();
424470
let hours = total_seconds / 3600;

0 commit comments

Comments
 (0)