Skip to content

Commit

Permalink
fix missing swarm config on bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
sh3ll3x3c committed Nov 5, 2024
1 parent b981172 commit f919efa
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions bootstrap/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.")?;

Expand Down
14 changes: 12 additions & 2 deletions bootstrap/src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,17 @@ pub struct Behaviour {
blocked_peers: allow_block_list::Behaviour<BlockedPeers>,
}

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)> {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -103,6 +112,7 @@ pub async fn init(
})?
.with_dns()?
.with_behaviour(behaviour)?
.with_swarm_config(|c| generate_config(c, cfg))
.build()
}

Expand Down
19 changes: 19 additions & 0 deletions bootstrap/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
use std::{
fmt::{self, Display},
net::SocketAddr,
num::{NonZero, NonZeroU8, NonZeroUsize},
str::FromStr,
time::Duration,
};
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -114,6 +119,11 @@ pub struct LibP2PConfig {
pub identify: IdentifyConfig,
pub kademlia: KademliaConfig,
pub secret_key: Option<SecretKey>,
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 {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f919efa

Please sign in to comment.