11use crate :: mod_libp2p:: behavior:: { AgentBehavior , AgentEvent } ;
22use alloy:: primitives:: { keccak256, Address } ;
3+ use cached:: { stores:: SizedCache , Cached } ;
34use futures_util:: StreamExt ;
45use libp2p:: {
56 identify:: {
@@ -19,6 +20,7 @@ use libp2p::{
1920use once_cell:: sync:: Lazy ;
2021use serde_json:: { json, Value } ;
2122use std:: { collections:: HashSet , error:: Error , time:: Duration } ;
23+ use tokio:: sync:: Mutex ;
2224use tracing:: { error, info} ;
2325
2426pub const TEST_BOOSTNODE_PEER_ID_LIST : [ & str ; 3 ] = [
@@ -53,6 +55,9 @@ static LAZY_BOOTNODE_METRICS_LIST: Lazy<Vec<&str>> = Lazy::new(|| {
5355 list
5456} ) ;
5557
58+ static GLOBAL_INDEXER_CACHE : Lazy < Mutex < SizedCache < PeerId , ( ) > > > =
59+ Lazy :: new ( || Mutex :: new ( SizedCache :: with_size ( 2000 ) ) ) ;
60+
5661pub ( crate ) struct EventLoop {
5762 swarm : Swarm < AgentBehavior > ,
5863}
@@ -96,6 +101,9 @@ impl EventLoop {
96101 ..
97102 } => {
98103 if num_established == 0 {
104+ let mut indexer_cache = GLOBAL_INDEXER_CACHE . lock ( ) . await ;
105+ indexer_cache. cache_remove ( & peer_id) ;
106+ drop ( indexer_cache) ;
99107 self . swarm . behaviour_mut ( ) . kad . remove_peer ( & peer_id) ;
100108 }
101109 }
@@ -124,10 +132,11 @@ impl EventLoop {
124132 } ,
125133 } = event
126134 {
127- let allow_peers = self . swarm . behaviour_mut ( ) . allowed_peers . allowed_peers ( ) ;
128- if allow_peers . get ( & peer_id) . is_none ( ) {
135+ let mut indexer_cache = GLOBAL_INDEXER_CACHE . lock ( ) . await ;
136+ if indexer_cache . cache_get ( & peer_id) . is_none ( ) {
129137 if LAZY_BOOTNODE_METRICS_LIST . contains ( & peer_id. to_base58 ( ) . as_str ( ) ) {
130- self . swarm . behaviour_mut ( ) . allowed_peers . allow_peer ( peer_id) ;
138+ indexer_cache. cache_set ( peer_id, ( ) ) ;
139+ drop ( indexer_cache) ;
131140 for addr in listen_addrs {
132141 self . swarm . behaviour_mut ( ) . kad . add_address ( & peer_id, addr) ;
133142 }
0 commit comments