Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecated pnet, enable tls, quic, allow_block_list #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env-template
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ edition = "2021"

[dependencies]
alloy = "0.11"
base64 = "0.22"
cached = "0.54"
dotenv = "0.15"
futures-util = "0.3"
Expand All @@ -17,11 +16,12 @@ libp2p = { version = "0.55", features = [
"json",
"kad",
"ping",
"pnet",
"tls",
"request-response",
"secp256k1",
"serde",
"tcp",
"quic",
"yamux",
"noise",
] }
Expand Down
56 changes: 16 additions & 40 deletions src/mod_libp2p/network.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
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,
Expand All @@ -17,9 +14,8 @@ 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};
Expand All @@ -39,7 +35,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") {
Expand Down Expand Up @@ -179,28 +174,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());
Expand Down Expand Up @@ -231,21 +212,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(
Expand Down