Skip to content

Commit

Permalink
web rtc support on bootstrap (#716)
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksandar Terentić <[email protected]>
  • Loading branch information
vbhattaccmu and aterentic-ethernal authored Oct 16, 2024
1 parent 9b4668c commit fa52619
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
4 changes: 4 additions & 0 deletions bootstrap/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
4 changes: 3 additions & 1 deletion bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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 }
Expand Down
21 changes: 18 additions & 3 deletions bootstrap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
7 changes: 7 additions & 0 deletions bootstrap/src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions bootstrap/src/p2p/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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))
)
Expand Down
3 changes: 3 additions & 0 deletions bootstrap/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit fa52619

Please sign in to comment.