From 490a0ed7c20f6e90aa91f2260e2f94b42319a187 Mon Sep 17 00:00:00 2001 From: gerald <3949379+getong@users.noreply.github.com> Date: Mon, 10 Feb 2025 17:34:24 +0800 Subject: [PATCH 1/2] deprecated pnet, enable tls, quic, allow_block_list --- .env-template | 3 +- Cargo.toml | 5 ++- src/mod_libp2p/behavior.rs | 10 ++++++ src/mod_libp2p/network.rs | 71 ++++++++++---------------------------- 4 files changed, 33 insertions(+), 56 deletions(-) diff --git a/.env-template b/.env-template index 190c8ed..716c17f 100644 --- a/.env-template +++ b/.env-template @@ -1,3 +1,4 @@ ACCOUNT_SK="" -PRIVITE_NET_ADDRESS="/ip4/0.0.0.0/tcp/8002" +LIBP2P_TCP_LISTEN_ADDRESS="/ip4/0.0.0.0/tcp/8002" +LIBP2P_QUIC_LISTEN_ADDRESS="/ip4/0.0.0.0/udp/8003/quic-v1" NETWORK="testnet" diff --git a/Cargo.toml b/Cargo.toml index 956df85..3f3c75c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,6 @@ edition = "2021" [dependencies] alloy = "0.11" -base64 = "0.22" -cached = "0.54" dotenv = "0.15" futures-util = "0.3" hex = "0.4" @@ -17,11 +15,12 @@ libp2p = { version = "0.55", features = [ "json", "kad", "ping", - "pnet", + "tls", "request-response", "secp256k1", "serde", "tcp", + "quic", "yamux", "noise", ] } diff --git a/src/mod_libp2p/behavior.rs b/src/mod_libp2p/behavior.rs index bb65d8d..fe74f56 100644 --- a/src/mod_libp2p/behavior.rs +++ b/src/mod_libp2p/behavior.rs @@ -1,6 +1,7 @@ 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, @@ -17,6 +18,7 @@ pub(crate) struct AgentBehavior { pub identify: IdentifyBehavior, pub kad: KademliaBehavior, pub ping: ping::Behaviour, + pub allowed_peers: AllowListBehaviour, } impl AgentBehavior { @@ -25,10 +27,12 @@ impl AgentBehavior { identify: IdentifyBehavior, ping: PingBehaviour, ) -> Self { + let allowed_peers = AllowListBehaviour::default(); Self { kad, identify, ping, + allowed_peers, } } @@ -70,3 +74,9 @@ impl From for AgentEvent { Self::Ping(value) } } + +impl From for AgentEvent { + fn from(_: std::convert::Infallible) -> Self { + panic!("NodeBehaviour is not Infallible!") + } +} diff --git a/src/mod_libp2p/network.rs b/src/mod_libp2p/network.rs index 78432d3..780c9c1 100644 --- a/src/mod_libp2p/network.rs +++ b/src/mod_libp2p/network.rs @@ -1,11 +1,7 @@ use crate::mod_libp2p::behavior::{AgentBehavior, AgentEvent}; use alloy::primitives::{keccak256, Address}; -use base64::{engine::general_purpose::STANDARD, Engine}; -use cached::{stores::SizedCache, Cached}; use futures_util::StreamExt; use libp2p::{ - core::transport::upgrade::Version, - dns, identify::{ Behaviour as IdentifyBehavior, Config as IdentifyConfig, Event as IdentifyEvent, Info as IdentifyInfo, @@ -17,14 +13,12 @@ use libp2p::{ }, noise, ping::{self, Event as PingEvent}, - pnet::{PnetConfig, PreSharedKey}, swarm::SwarmEvent, - tcp, yamux, PeerId, StreamProtocol, Swarm, Transport, + tls, yamux, PeerId, StreamProtocol, Swarm, }; 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] = [ @@ -39,7 +33,6 @@ pub const PRODUCTION_BOOSTNODE_PEER_ID_LIST: [&str; 3] = [ ]; pub const METRICS_PEER_ID: &str = "16Uiu2HAmNa64mzMD6Uq4EhUTdHKoZE7MLiEh7hCK3ACN5F5MgJoL"; -const PRIVATE_NETWORK_KEY: &str = "wiwlLGQ8g6zu0mcckkROzeeAU7xN+Adz40ELWSH3f1M="; pub static QUERY_INDEXER_URL: Lazy<&str> = Lazy::new(|| { if std::env::var("NETWORK").as_deref() == Ok("testnet") { @@ -60,9 +53,6 @@ static LAZY_BOOTNODE_METRICS_LIST: Lazy> = Lazy::new(|| { list }); -static GLOBAL_INDEXER_CACHE: Lazy>> = - Lazy::new(|| Mutex::new(SizedCache::with_size(2000))); - pub(crate) struct EventLoop { swarm: Swarm, } @@ -106,9 +96,6 @@ 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); } } @@ -137,11 +124,10 @@ impl EventLoop { }, } = event { - let mut indexer_cache = GLOBAL_INDEXER_CACHE.lock().await; - if indexer_cache.cache_get(&peer_id).is_none() { + let allow_peers = self.swarm.behaviour_mut().allowed_peers.allowed_peers(); + if allow_peers.get(&peer_id).is_none() { if LAZY_BOOTNODE_METRICS_LIST.contains(&peer_id.to_base58().as_str()) { - indexer_cache.cache_set(peer_id, ()); - drop(indexer_cache); + self.swarm.behaviour_mut().allowed_peers.allow_peer(peer_id); for addr in listen_addrs { self.swarm.behaviour_mut().kad.add_address(&peer_id, addr); } @@ -179,28 +165,14 @@ impl EventLoop { let secret_key = identity::secp256k1::SecretKey::try_from_bytes(private_key_bytes)?; let libp2p_keypair: Keypair = identity::secp256k1::Keypair::from(secret_key).into(); - let psk = Self::get_psk(); - - // info!("using swarm key with fingerprint: {}", psk.fingerprint()); - let mut swarm = libp2p::SwarmBuilder::with_existing_identity(libp2p_keypair.clone()) .with_tokio() - .with_other_transport(|key| { - let noise_config = noise::Config::new(key).unwrap(); - let mut yamux_config = yamux::Config::default(); - yamux_config.set_max_num_streams(1024 * 1024); - let base_transport = - tcp::tokio::Transport::new(tcp::Config::default().nodelay(true)); - let base_transport = dns::tokio::Transport::system(base_transport) - .expect("DNS") - .boxed(); - let maybe_encrypted = base_transport - .and_then(move |socket, _| PnetConfig::new(psk).handshake(socket)); - maybe_encrypted - .upgrade(Version::V1Lazy) - .authenticate(noise_config) - .multiplex(yamux_config) - })? + .with_tcp( + Default::default(), + (tls::Config::new, noise::Config::new), + yamux::Config::default, + )? + .with_quic() .with_dns()? .with_behaviour(|key| { let local_peer_id = PeerId::from(key.clone().public()); @@ -231,21 +203,16 @@ impl EventLoop { swarm.behaviour_mut().kad.set_mode(Some(kad::Mode::Server)); - let private_net_address = - std::env::var("PRIVITE_NET_ADDRESS").unwrap_or("/ip4/0.0.0.0/tcp/8000".to_string()); - let private_net_address = private_net_address.parse()?; - swarm.listen_on(private_net_address)?; - Ok(swarm) - } + let libp2p_tcp_listen_address = std::env::var("LIBP2P_TCP_LISTEN_ADDRESS") + .unwrap_or("/ip4/0.0.0.0/tcp/8000".to_string()); + let libp2p_tcp_listen_address = libp2p_tcp_listen_address.parse()?; + swarm.listen_on(libp2p_tcp_listen_address)?; - /// Read the pre shared key file from the given ipfs directory - fn get_psk() -> PreSharedKey { - let bytes = STANDARD.decode(PRIVATE_NETWORK_KEY).unwrap(); - let key: [u8; 32] = bytes - .try_into() - .map_err(|_| "Decoded key must be 32 bytes long") - .unwrap(); - PreSharedKey::new(key) + let libp2p_quic_listen_address = std::env::var("LIBP2P_QUIC_LISTEN_ADDRESS") + .unwrap_or("/ip4/0.0.0.0/udp/8003/quic-v1".to_string()); + let libp2p_quic_listen_address = libp2p_quic_listen_address.parse()?; + swarm.listen_on(libp2p_quic_listen_address)?; + Ok(swarm) } async fn libp2p_publickey_to_eth_address( From 63fa7919a735d505145ac7120b04dc8a6a39e648 Mon Sep 17 00:00:00 2001 From: gerald <3949379+getong@users.noreply.github.com> Date: Mon, 10 Feb 2025 18:26:22 +0800 Subject: [PATCH 2/2] deprecated allow_block_list, enable cached --- Cargo.toml | 1 + src/mod_libp2p/behavior.rs | 10 ---------- src/mod_libp2p/network.rs | 15 ++++++++++++--- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3f3c75c..56e1a09 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] alloy = "0.11" +cached = "0.54" dotenv = "0.15" futures-util = "0.3" hex = "0.4" diff --git a/src/mod_libp2p/behavior.rs b/src/mod_libp2p/behavior.rs index fe74f56..bb65d8d 100644 --- a/src/mod_libp2p/behavior.rs +++ b/src/mod_libp2p/behavior.rs @@ -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, @@ -18,7 +17,6 @@ pub(crate) struct AgentBehavior { pub identify: IdentifyBehavior, pub kad: KademliaBehavior, pub ping: ping::Behaviour, - pub allowed_peers: AllowListBehaviour, } impl AgentBehavior { @@ -27,12 +25,10 @@ impl AgentBehavior { identify: IdentifyBehavior, ping: PingBehaviour, ) -> Self { - let allowed_peers = AllowListBehaviour::default(); Self { kad, identify, ping, - allowed_peers, } } @@ -74,9 +70,3 @@ impl From for AgentEvent { Self::Ping(value) } } - -impl From for AgentEvent { - fn from(_: std::convert::Infallible) -> Self { - panic!("NodeBehaviour is not Infallible!") - } -} diff --git a/src/mod_libp2p/network.rs b/src/mod_libp2p/network.rs index 780c9c1..3b845c7 100644 --- a/src/mod_libp2p/network.rs +++ b/src/mod_libp2p/network.rs @@ -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::{ @@ -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] = [ @@ -53,6 +55,9 @@ static LAZY_BOOTNODE_METRICS_LIST: Lazy> = Lazy::new(|| { list }); +static GLOBAL_INDEXER_CACHE: Lazy>> = + Lazy::new(|| Mutex::new(SizedCache::with_size(2000))); + pub(crate) struct EventLoop { swarm: Swarm, } @@ -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); } } @@ -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); }