@@ -3,21 +3,24 @@ use crate::{
3
3
server:: { DiscoveryServer , DiscoveryServerError } ,
4
4
side_car:: { DiscoverySideCar , DiscoverySideCarError } ,
5
5
} ,
6
- kademlia:: Kademlia ,
6
+ kademlia:: { Kademlia , PeerData } ,
7
7
metrics:: METRICS ,
8
8
rlpx:: {
9
9
connection:: server:: { RLPxConnBroadcastSender , RLPxConnection } ,
10
10
initiator:: { RLPxInitiator , RLPxInitiatorError } ,
11
11
l2:: l2_connection:: P2PBasedContext ,
12
12
message:: Message ,
13
+ p2p:: SUPPORTED_SNAP_CAPABILITIES ,
13
14
} ,
14
15
tx_broadcaster:: { TxBroadcaster , TxBroadcasterError } ,
15
16
types:: { Node , NodeRecord } ,
16
17
} ;
17
18
use ethrex_blockchain:: Blockchain ;
19
+ use ethrex_common:: H256 ;
18
20
use ethrex_storage:: Store ;
19
21
use secp256k1:: SecretKey ;
20
22
use std:: {
23
+ collections:: BTreeMap ,
21
24
io,
22
25
net:: SocketAddr ,
23
26
sync:: Arc ,
@@ -175,9 +178,20 @@ fn listener(tcp_addr: SocketAddr) -> Result<TcpListener, io::Error> {
175
178
tcp_socket. listen ( 50 )
176
179
}
177
180
178
- 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 > ) {
179
190
let start = std:: time:: Instant :: now ( ) ;
180
191
loop {
192
+ if blockchain. is_synced ( ) {
193
+ return ;
194
+ }
181
195
let metrics_enabled = * METRICS . enabled . lock ( ) . await ;
182
196
// Show the metrics only when these are enabled
183
197
if !metrics_enabled {
@@ -424,6 +438,33 @@ bytecodes progress: {bytecodes_download_progress} (total: {bytecodes_to_download
424
438
}
425
439
}
426
440
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
+
427
468
fn format_duration ( duration : Duration ) -> String {
428
469
let total_seconds = duration. as_secs ( ) ;
429
470
let hours = total_seconds / 3600 ;
0 commit comments