From df5cb507f88a606b4890e268bea2359ffd1834d7 Mon Sep 17 00:00:00 2001 From: sh3ll3x3c Date: Tue, 5 Nov 2024 12:18:24 +0100 Subject: [PATCH] fix missing swarm config on bootstrap --- bootstrap/CHANGELOG.md | 1 + bootstrap/src/main.rs | 2 +- bootstrap/src/p2p.rs | 14 ++++++++++++-- bootstrap/src/types.rs | 19 +++++++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/bootstrap/CHANGELOG.md b/bootstrap/CHANGELOG.md index 93c285900..d21c99d16 100644 --- a/bootstrap/CHANGELOG.md +++ b/bootstrap/CHANGELOG.md @@ -2,6 +2,7 @@ ## [0.4.0] +- Fix missing swarm config - Integrate upstream `rust-libp2p` `0.54` changes to the bootstrap process - Add `/p2p/local/info` endpoint - Add webrtc support to bootstrap diff --git a/bootstrap/src/main.rs b/bootstrap/src/main.rs index eea1b03c6..194a7968d 100644 --- a/bootstrap/src/main.rs +++ b/bootstrap/src/main.rs @@ -82,7 +82,7 @@ async fn run() -> Result<()> { let (id_keys, peer_id) = p2p::keypair((&cfg).into())?; let (network_client, network_event_loop) = - p2p::init(cfg_libp2p, id_keys, cfg.ws_transport_enable) + p2p::init(&cfg_libp2p, id_keys, cfg.ws_transport_enable) .await .context("Failed to initialize P2P Network Service.")?; diff --git a/bootstrap/src/p2p.rs b/bootstrap/src/p2p.rs index 9a4bfd34a..74c589d52 100644 --- a/bootstrap/src/p2p.rs +++ b/bootstrap/src/p2p.rs @@ -34,8 +34,17 @@ pub struct Behaviour { blocked_peers: allow_block_list::Behaviour, } +fn generate_config(config: libp2p::swarm::Config, cfg: &LibP2PConfig) -> libp2p::swarm::Config { + config + .with_idle_connection_timeout(cfg.connection_idle_timeout) + .with_max_negotiating_inbound_streams(cfg.max_negotiating_inbound_streams) + .with_notify_handler_buffer_size(cfg.task_command_buffer_size) + .with_dial_concurrency_factor(cfg.dial_concurrency_factor) + .with_per_connection_event_buffer_size(cfg.per_connection_event_buffer_size) +} + pub async fn init( - cfg: LibP2PConfig, + cfg: &LibP2PConfig, id_keys: Keypair, is_ws_transport: bool, ) -> Result<(Client, EventLoop)> { @@ -63,7 +72,7 @@ pub async fn init( // create new Kademlia Memory Store let kad_store = MemoryStore::new(id_keys.public().to_peer_id()); // create Kademlia Config - let mut kad_cfg = kad::Config::new(cfg.kademlia.protocol_name); + let mut kad_cfg = kad::Config::new(cfg.kademlia.protocol_name.clone()); kad_cfg .set_query_timeout(cfg.kademlia.query_timeout) .set_periodic_bootstrap_interval(Some(cfg.kademlia.bootstrap_interval)); @@ -103,6 +112,7 @@ pub async fn init( })? .with_dns()? .with_behaviour(behaviour)? + .with_swarm_config(|c| generate_config(c, cfg)) .build() } diff --git a/bootstrap/src/types.rs b/bootstrap/src/types.rs index 6cc53d723..3008f07c4 100644 --- a/bootstrap/src/types.rs +++ b/bootstrap/src/types.rs @@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize}; use std::{ fmt::{self, Display}, net::SocketAddr, + num::{NonZero, NonZeroU8, NonZeroUsize}, str::FromStr, time::Duration, }; @@ -80,6 +81,10 @@ pub struct RuntimeConfig { /// Sets the amount of time to keep connections alive when they're idle. (default: 30s). /// NOTE: libp2p default value is 10s, but because of Avail block time of 20s the value has been increased pub connection_idle_timeout: u64, + pub max_negotiating_inbound_streams: usize, + pub task_command_buffer_size: NonZeroUsize, + pub per_connection_event_buffer_size: usize, + pub dial_concurrency_factor: NonZeroU8, /// Autonat server config - max total dial requests (Default: 30). pub autonat_throttle_clients_global_max: usize, /// Autonat server config - max dial requests for a single peer (Default: 3). @@ -114,6 +119,11 @@ pub struct LibP2PConfig { pub identify: IdentifyConfig, pub kademlia: KademliaConfig, pub secret_key: Option, + pub connection_idle_timeout: Duration, + pub max_negotiating_inbound_streams: usize, + pub task_command_buffer_size: NonZeroUsize, + pub per_connection_event_buffer_size: usize, + pub dial_concurrency_factor: NonZeroU8, } impl From<&RuntimeConfig> for LibP2PConfig { @@ -123,6 +133,11 @@ impl From<&RuntimeConfig> for LibP2PConfig { identify: IdentifyConfig::new(), kademlia: rtcfg.into(), secret_key: rtcfg.secret_key.clone(), + connection_idle_timeout: Duration::from_secs(rtcfg.connection_idle_timeout), + max_negotiating_inbound_streams: rtcfg.max_negotiating_inbound_streams, + task_command_buffer_size: rtcfg.task_command_buffer_size, + per_connection_event_buffer_size: rtcfg.per_connection_event_buffer_size, + dial_concurrency_factor: rtcfg.dial_concurrency_factor, } } } @@ -191,6 +206,10 @@ impl Default for RuntimeConfig { autonat_throttle_clients_period: 1, autonat_only_global_ips: true, connection_idle_timeout: 30, + max_negotiating_inbound_streams: 20, + task_command_buffer_size: NonZero::new(30000).unwrap(), + per_connection_event_buffer_size: 10000, + dial_concurrency_factor: NonZero::new(5).unwrap(), kad_query_timeout: 60, bootstraps: vec![], bootstrap_period: 300,