@@ -201,8 +201,13 @@ impl<R: MessageReceiver> Network<R> {
201
201
self . handle_handshake_result( result) ;
202
202
}
203
203
}
204
- AnchorBehaviourEvent :: PeerManager ( peer_manager:: Event :: ConnectActions ( actions) ) => {
205
- self . handle_connect_actions( actions) ;
204
+ AnchorBehaviourEvent :: PeerManager ( peer_manager:: Event :: Heartbeat ( heartbeat) ) => {
205
+ if let Some ( actions) = heartbeat. connect_actions {
206
+ self . handle_connect_actions( actions) ;
207
+ }
208
+ if heartbeat. check_peer_scores {
209
+ self . check_and_block_peers_by_score( ) ;
210
+ }
206
211
}
207
212
_ => {
208
213
trace!( event = ?behaviour_event, "Unhandled behaviour event" ) ;
@@ -413,6 +418,40 @@ impl<R: MessageReceiver> Network<R> {
413
418
}
414
419
}
415
420
}
421
+
422
+ /// Get the list of currently blocked peers.
423
+ pub fn blocked_peers ( & self ) -> & std:: collections:: HashSet < PeerId > {
424
+ self . swarm . behaviour ( ) . peer_manager . blocked_peers ( )
425
+ }
426
+
427
+ /// Check gossipsub peer scores and block peers with scores below graylist threshold
428
+ pub fn check_and_block_peers_by_score ( & mut self ) {
429
+ use crate :: scoring:: peer_score_config:: GRAYLIST_THRESHOLD ;
430
+
431
+ let gossipsub = & self . swarm . behaviour ( ) . gossipsub ;
432
+
433
+ // Get all peers with poor scores that should be blocked
434
+ let peers_to_block: Vec < PeerId > = self
435
+ . swarm
436
+ . connected_peers ( )
437
+ . filter_map ( |peer_id| {
438
+ if let Some ( score) = gossipsub. peer_score ( peer_id) {
439
+ if score < GRAYLIST_THRESHOLD {
440
+ Some ( * peer_id)
441
+ } else {
442
+ None
443
+ }
444
+ } else {
445
+ None
446
+ }
447
+ } )
448
+ . collect ( ) ;
449
+
450
+ // Block the peers (connections will be closed automatically)
451
+ for peer_id in peers_to_block {
452
+ self . swarm . behaviour_mut ( ) . peer_manager . block_peer ( peer_id) ;
453
+ }
454
+ }
416
455
}
417
456
418
457
fn build_swarm (
0 commit comments