Skip to content

Commit

Permalink
deprecated allow_block_list, enable cached
Browse files Browse the repository at this point in the history
  • Loading branch information
getong committed Feb 10, 2025
1 parent 490a0ed commit 63fa791
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
alloy = "0.11"
cached = "0.54"
dotenv = "0.15"
futures-util = "0.3"
hex = "0.4"
Expand Down
10 changes: 0 additions & 10 deletions src/mod_libp2p/behavior.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashSet;

use libp2p::{
allow_block_list::{AllowedPeers, Behaviour as AllowListBehaviour},
identify::{Behaviour as IdentifyBehavior, Event as IdentifyEvent},
kad::{
store::MemoryStore as KademliaInMemory, Behaviour as KademliaBehavior,
Expand All @@ -18,7 +17,6 @@ pub(crate) struct AgentBehavior {
pub identify: IdentifyBehavior,
pub kad: KademliaBehavior<KademliaInMemory>,
pub ping: ping::Behaviour,
pub allowed_peers: AllowListBehaviour<AllowedPeers>,
}

impl AgentBehavior {
Expand All @@ -27,12 +25,10 @@ impl AgentBehavior {
identify: IdentifyBehavior,
ping: PingBehaviour,
) -> Self {
let allowed_peers = AllowListBehaviour::default();
Self {
kad,
identify,
ping,
allowed_peers,
}
}

Expand Down Expand Up @@ -74,9 +70,3 @@ impl From<PingEvent> for AgentEvent {
Self::Ping(value)
}
}

impl From<std::convert::Infallible> for AgentEvent {
fn from(_: std::convert::Infallible) -> Self {
panic!("NodeBehaviour is not Infallible!")
}
}
15 changes: 12 additions & 3 deletions src/mod_libp2p/network.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::mod_libp2p::behavior::{AgentBehavior, AgentEvent};
use alloy::primitives::{keccak256, Address};
use cached::{stores::SizedCache, Cached};
use futures_util::StreamExt;
use libp2p::{
identify::{
Expand All @@ -19,6 +20,7 @@ use libp2p::{
use once_cell::sync::Lazy;
use serde_json::{json, Value};
use std::{collections::HashSet, error::Error, time::Duration};
use tokio::sync::Mutex;
use tracing::{error, info};

pub const TEST_BOOSTNODE_PEER_ID_LIST: [&str; 3] = [
Expand Down Expand Up @@ -53,6 +55,9 @@ static LAZY_BOOTNODE_METRICS_LIST: Lazy<Vec<&str>> = Lazy::new(|| {
list
});

static GLOBAL_INDEXER_CACHE: Lazy<Mutex<SizedCache<PeerId, ()>>> =
Lazy::new(|| Mutex::new(SizedCache::with_size(2000)));

pub(crate) struct EventLoop {
swarm: Swarm<AgentBehavior>,
}
Expand Down Expand Up @@ -96,6 +101,9 @@ impl EventLoop {
..
} => {
if num_established == 0 {
let mut indexer_cache = GLOBAL_INDEXER_CACHE.lock().await;
indexer_cache.cache_remove(&peer_id);
drop(indexer_cache);
self.swarm.behaviour_mut().kad.remove_peer(&peer_id);
}
}
Expand Down Expand Up @@ -124,10 +132,11 @@ impl EventLoop {
},
} = event
{
let allow_peers = self.swarm.behaviour_mut().allowed_peers.allowed_peers();
if allow_peers.get(&peer_id).is_none() {
let mut indexer_cache = GLOBAL_INDEXER_CACHE.lock().await;
if indexer_cache.cache_get(&peer_id).is_none() {
if LAZY_BOOTNODE_METRICS_LIST.contains(&peer_id.to_base58().as_str()) {
self.swarm.behaviour_mut().allowed_peers.allow_peer(peer_id);
indexer_cache.cache_set(peer_id, ());
drop(indexer_cache);
for addr in listen_addrs {
self.swarm.behaviour_mut().kad.add_address(&peer_id, addr);
}
Expand Down

0 comments on commit 63fa791

Please sign in to comment.