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