From fa5261950f961c5c0d7fb0af56e31dce9b0a5a27 Mon Sep 17 00:00:00 2001 From: "[[nodiscard]]" <53374818+vbhattaccmu@users.noreply.github.com> Date: Thu, 17 Oct 2024 05:24:50 +0530 Subject: [PATCH] web rtc support on bootstrap (#716) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Aleksandar Terentić --- Cargo.lock | 4 +++- Cargo.toml | 2 ++ bootstrap/CHANGELOG.md | 4 ++++ bootstrap/Cargo.toml | 4 +++- bootstrap/src/main.rs | 21 ++++++++++++++++++--- bootstrap/src/p2p.rs | 7 +++++++ bootstrap/src/p2p/event_loop.rs | 4 ++-- bootstrap/src/types.rs | 3 +++ 8 files changed, 42 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8452cf7e0..374e9b025 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -920,7 +920,7 @@ dependencies = [ [[package]] name = "avail-light-bootstrap" -version = "0.3.0" +version = "0.4.0" dependencies = [ "anyhow", "async-std", @@ -930,10 +930,12 @@ dependencies = [ "hex", "libp2p", "libp2p-allow-block-list", + "libp2p-webrtc", "multihash 0.14.0", "opentelemetry", "opentelemetry-otlp", "opentelemetry_sdk", + "rand", "semver", "serde", "tokio", diff --git a/Cargo.toml b/Cargo.toml index 776d2cf12..5aba6161c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,8 +28,10 @@ clap = { version = "4.4.4", features = ["derive", "cargo"] } color-eyre = "0.6.2" confy = "0.5.1" hex = "0.4.3" +rand = "0.8.4" libp2p = { version = "0.54", features = ["kad", "identify", "ping", "mdns", "autonat", "relay", "dcutr", "upnp", "noise", "yamux", "dns", "metrics", "tokio", "macros", "tcp", "quic", "serde", "websocket"] } libp2p-allow-block-list = "0.4" +libp2p-webrtc = { version = "=0.8.0-alpha", features = ["tokio"] } multihash = { version = "0.14.0", default-features = false, features = ["blake3", "sha3"] } semver = "1.0.23" serde = { version = "1.0", features = ["derive"] } diff --git a/bootstrap/CHANGELOG.md b/bootstrap/CHANGELOG.md index 011230fe0..a7131c618 100644 --- a/bootstrap/CHANGELOG.md +++ b/bootstrap/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [0.4.0] + +- Add webrtc support to bootstrap + ## [0.3.0](https://github.com/availproject/avail-light/releases/tag/avail-light-bootstrap-v0.3.0) - 2024-09-27 - Bump `otel` version to `0.24.0` diff --git a/bootstrap/Cargo.toml b/bootstrap/Cargo.toml index 673cd42a3..0492875f7 100644 --- a/bootstrap/Cargo.toml +++ b/bootstrap/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "avail-light-bootstrap" -version = "0.3.0" +version = "0.4.0" build = "../build.rs" edition = "2021" description = "Avail network Bootstrap Node for p2p Light Client" @@ -14,7 +14,9 @@ confy = { workspace = true } hex = { workspace = true } libp2p = { workspace = true } libp2p-allow-block-list = { workspace = true } +libp2p-webrtc = { workspace = true } multihash = { workspace = true } +rand = { workspace = true } semver = { workspace = true } serde = { workspace = true } tokio = { workspace = true } diff --git a/bootstrap/src/main.rs b/bootstrap/src/main.rs index 1c2b35c7d..c38d50a86 100644 --- a/bootstrap/src/main.rs +++ b/bootstrap/src/main.rs @@ -126,12 +126,21 @@ async fn run() -> Result<()> { } }); - // Listen on all interfaces with TCP + // Listen on all interfaces + network_client .start_listening(construct_multiaddress(cfg.ws_transport_enable, cfg.port)) .await - .context("Unable to create P2P listener.")?; - info!("Started listening for TCP traffic on port: {:?}.", cfg.port); + .context("Unable to create TCP P2P listener.")?; + + info!("TCP listener started on port {}.", cfg.port); + + network_client + .start_listening(webrtc_multiaddress(cfg.webrtc_port)) + .await + .context("Unable to create WebRTC P2P listener.")?; + + info!("WebRTC listening on port {}.", cfg.webrtc_port); info!("Bootstrap node starting ..."); // add bootstrap nodes, if provided @@ -168,3 +177,9 @@ fn construct_multiaddress(is_websocket: bool, port: u16) -> Multiaddr { tcp_multiaddress } + +fn webrtc_multiaddress(webrtc_port: u16) -> Multiaddr { + Multiaddr::from(Ipv4Addr::UNSPECIFIED) + .with(Protocol::Udp(webrtc_port)) + .with(Protocol::WebRTCDirect) +} diff --git a/bootstrap/src/p2p.rs b/bootstrap/src/p2p.rs index d98a16ec4..78bca0de4 100644 --- a/bootstrap/src/p2p.rs +++ b/bootstrap/src/p2p.rs @@ -8,7 +8,9 @@ use libp2p::{ swarm::NetworkBehaviour, tcp, yamux, PeerId, SwarmBuilder, }; +use libp2p_webrtc as webrtc; use multihash::Hasher; +use rand::thread_rng; use tokio::sync::mpsc; mod client; @@ -91,6 +93,11 @@ pub async fn init( noise::Config::new, yamux::Config::default, )? + .with_other_transport(|keypair| { + use webrtc::tokio::{Certificate, Transport}; + let certificate = Certificate::generate(&mut thread_rng()); + Ok(Transport::new(keypair.clone(), certificate?)) + })? .with_dns()? .with_behaviour(behaviour)? .build() diff --git a/bootstrap/src/p2p/event_loop.rs b/bootstrap/src/p2p/event_loop.rs index 2ac5746d0..e341b3880 100644 --- a/bootstrap/src/p2p/event_loop.rs +++ b/bootstrap/src/p2p/event_loop.rs @@ -17,7 +17,7 @@ use tokio::{ sync::{mpsc, oneshot}, time::{interval_at, Instant, Interval}, }; -use tracing::{debug, trace}; +use tracing::{debug, info, trace}; use crate::types::AgentVersion; @@ -257,7 +257,7 @@ impl EventLoop { }, SwarmEvent::NewListenAddr { address, .. } => { let local_peer_id = *self.swarm.local_peer_id(); - debug!( + info!( "Local node is listening on: {:?}", address.with(Protocol::P2p(local_peer_id)) ) diff --git a/bootstrap/src/types.rs b/bootstrap/src/types.rs index 951e09f63..6b87c0bff 100644 --- a/bootstrap/src/types.rs +++ b/bootstrap/src/types.rs @@ -73,6 +73,8 @@ pub struct RuntimeConfig { pub log_format_json: bool, /// Sets the listening P2P network service port. (default: 39000) pub port: u16, + /// P2P WebRTC listener port (default: 39001). + pub webrtc_port: u16, /// Enable WebSocket transport over TCP pub ws_transport_enable: bool, /// Sets the amount of time to keep connections alive when they're idle. (default: 30s). @@ -182,6 +184,7 @@ impl Default for RuntimeConfig { seed: "1".to_string(), }), port: 39000, + webrtc_port: 39001, ws_transport_enable: false, autonat_throttle_clients_global_max: 120, autonat_throttle_clients_peer_max: 4,