Skip to content

Commit acd7dfa

Browse files
committed
deprecated allow_block_list, enable cached
1 parent 490a0ed commit acd7dfa

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
alloy = "0.11"
8+
cached = "0.54.0"
89
dotenv = "0.15"
910
futures-util = "0.3"
1011
hex = "0.4"

src/mod_libp2p/behavior.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::collections::HashSet;
22

33
use libp2p::{
4-
allow_block_list::{AllowedPeers, Behaviour as AllowListBehaviour},
54
identify::{Behaviour as IdentifyBehavior, Event as IdentifyEvent},
65
kad::{
76
store::MemoryStore as KademliaInMemory, Behaviour as KademliaBehavior,
@@ -18,7 +17,6 @@ pub(crate) struct AgentBehavior {
1817
pub identify: IdentifyBehavior,
1918
pub kad: KademliaBehavior<KademliaInMemory>,
2019
pub ping: ping::Behaviour,
21-
pub allowed_peers: AllowListBehaviour<AllowedPeers>,
2220
}
2321

2422
impl AgentBehavior {
@@ -27,12 +25,10 @@ impl AgentBehavior {
2725
identify: IdentifyBehavior,
2826
ping: PingBehaviour,
2927
) -> Self {
30-
let allowed_peers = AllowListBehaviour::default();
3128
Self {
3229
kad,
3330
identify,
3431
ping,
35-
allowed_peers,
3632
}
3733
}
3834

@@ -74,9 +70,3 @@ impl From<PingEvent> for AgentEvent {
7470
Self::Ping(value)
7571
}
7672
}
77-
78-
impl From<std::convert::Infallible> for AgentEvent {
79-
fn from(_: std::convert::Infallible) -> Self {
80-
panic!("NodeBehaviour is not Infallible!")
81-
}
82-
}

src/mod_libp2p/network.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::mod_libp2p::behavior::{AgentBehavior, AgentEvent};
22
use alloy::primitives::{keccak256, Address};
3+
use cached::{stores::SizedCache, Cached};
34
use futures_util::StreamExt;
45
use libp2p::{
56
identify::{
@@ -19,6 +20,7 @@ use libp2p::{
1920
use once_cell::sync::Lazy;
2021
use serde_json::{json, Value};
2122
use std::{collections::HashSet, error::Error, time::Duration};
23+
use tokio::sync::Mutex;
2224
use tracing::{error, info};
2325

2426
pub 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+
5661
pub(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

Comments
 (0)