Skip to content

Commit

Permalink
Merge branch 'main' into testnet-alpha-1
Browse files Browse the repository at this point in the history
  • Loading branch information
ozwaldorf committed Oct 20, 2023
2 parents 7b01af4 + bfb270a commit ef51f26
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 22 deletions.
116 changes: 109 additions & 7 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ dependencies = [
"percent-encoding",
"pin-project-lite",
"rand 0.8.5",
"sha1",
"sha1 0.10.6",
"smallvec",
"tokio",
"tokio-util 0.7.9",
Expand Down Expand Up @@ -1342,6 +1342,16 @@ version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3ac9f8b63eca6fd385229b3675f6cc0dc5c8a5c8a54a59d4f52ffd670d87b0c"

[[package]]
name = "bytecodec"
version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adf4c9d0bbf32eea58d7c0f812058138ee8edaf0f2802b6d03561b504729a325"
dependencies = [
"byteorder",
"trackable 0.2.24",
]

[[package]]
name = "bytecount"
version = "0.6.4"
Expand Down Expand Up @@ -1928,15 +1938,30 @@ dependencies = [
"libc",
]

[[package]]
name = "crc"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49fc9a695bca7f35f5f4c15cddc84415f66a74ea78eef08e90c5024f2b540e23"
dependencies = [
"crc-catalog 1.1.1",
]

[[package]]
name = "crc"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86ec7a15cbe22e59248fc7eadb1907dab5ba09372595da4d73dd805ed4417dfe"
dependencies = [
"crc-catalog",
"crc-catalog 2.2.0",
]

[[package]]
name = "crc-catalog"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccaeedb56da03b09f598226e25e80088cb4cd25f316e6e4df7d695f0feeb1403"

[[package]]
name = "crc-catalog"
version = "2.2.0"
Expand Down Expand Up @@ -3725,7 +3750,7 @@ dependencies = [
"http",
"httpdate",
"mime",
"sha1",
"sha1 0.10.6",
]

[[package]]
Expand Down Expand Up @@ -3795,6 +3820,15 @@ dependencies = [
"digest 0.10.7",
]

[[package]]
name = "hmac-sha1"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1333fad8d94b82cab989da428b0b36a3435db3870d85e971a1d6dc0a8576722"
dependencies = [
"sha1 0.2.0",
]

[[package]]
name = "home"
version = "0.5.5"
Expand Down Expand Up @@ -4700,6 +4734,7 @@ dependencies = [
"serde_json",
"smallvec",
"str0m",
"stunclient",
"time",
"tokio",
"tokio-util 0.7.9",
Expand Down Expand Up @@ -5289,6 +5324,12 @@ dependencies = [
"digest 0.10.7",
]

[[package]]
name = "md5"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771"

[[package]]
name = "memchr"
version = "2.6.4"
Expand Down Expand Up @@ -8095,7 +8136,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "514eb06a3f6b1f119b6a00a9a87afac072894817d3283b0d36adc8f8a135886a"
dependencies = [
"bytes",
"crc",
"crc 3.0.1",
"fxhash",
"log",
"rand 0.8.5",
Expand Down Expand Up @@ -8400,6 +8441,12 @@ dependencies = [
"sha1-asm",
]

[[package]]
name = "sha1"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc30b1e1e8c40c121ca33b86c23308a090d19974ef001b4bf6e61fd1a0fb095c"

[[package]]
name = "sha1"
version = "0.10.6"
Expand Down Expand Up @@ -8764,7 +8811,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "959ba881f3e4a040ab65886ca6bcaaead8f06ce18ab4b1599b799c501d5335cc"
dependencies = [
"combine",
"crc",
"crc 3.0.1",
"hmac",
"once_cell",
"openssl",
Expand Down Expand Up @@ -8846,6 +8893,32 @@ dependencies = [
"syn 2.0.38",
]

[[package]]
name = "stun_codec"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f1df6c4e592afc1ffed8f4bc50ab9f4d70cfccb42829662b7d276247cbef3b1"
dependencies = [
"bytecodec",
"byteorder",
"crc 2.1.0",
"hmac-sha1",
"md5",
"trackable 1.3.0",
]

[[package]]
name = "stunclient"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92ff24a850e0e53c373cbb348f3ba8393207409fe7f2f55efc9b5a46e58652ad"
dependencies = [
"bytecodec",
"rand 0.8.5",
"stun_codec",
"tokio",
]

[[package]]
name = "subtle"
version = "2.5.0"
Expand Down Expand Up @@ -9650,6 +9723,35 @@ dependencies = [
"tracing-serde",
]

[[package]]
name = "trackable"
version = "0.2.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b98abb9e7300b9ac902cc04920945a874c1973e08c310627cc4458c04b70dd32"
dependencies = [
"trackable 1.3.0",
"trackable_derive",
]

[[package]]
name = "trackable"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15bd114abb99ef8cee977e517c8f37aee63f184f2d08e3e6ceca092373369ae"
dependencies = [
"trackable_derive",
]

[[package]]
name = "trackable_derive"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebeb235c5847e2f82cfe0f07eb971d1e5f6804b18dac2ae16349cc604380f82f"
dependencies = [
"quote 1.0.33",
"syn 1.0.109",
]

[[package]]
name = "triomphe"
version = "0.1.9"
Expand Down Expand Up @@ -9686,7 +9788,7 @@ dependencies = [
"log",
"rand 0.8.5",
"rustls 0.21.7",
"sha1",
"sha1 0.10.6",
"thiserror",
"url",
"utf-8",
Expand Down Expand Up @@ -10428,7 +10530,7 @@ dependencies = [
"flate2",
"hmac",
"pbkdf2 0.11.0",
"sha1",
"sha1 0.10.6",
"time",
"zstd 0.11.2+zstd.1.5.2",
]
Expand Down
2 changes: 1 addition & 1 deletion core/e2e/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ fn build_config(
config.inject::<Handshake<FinalTypes>>(HandshakeConfig {
// TODO: figure out how to have e2e testing for the different transports (browser oriented)
transports: vec![TransportConfig::WebRTC(WebRtcConfig {
udp_address: ([127, 0, 0, 1], ports.handshake.webrtc).into(),
udp_address: ([0, 0, 0, 0], ports.handshake.webrtc).into(),
})],
http_address: ([127, 0, 0, 1], ports.handshake.http).into(),
});
Expand Down
1 change: 1 addition & 0 deletions core/handshake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ tower = "0.4"
tower-http = { version = "0.4.4", features = ["cors"] }
hyper = "0.14"
async-channel = "1.9.0"
stunclient = "0.4"

[dev-dependencies]
criterion = { version = "0.5.0", features = ["html_reports", "async_tokio"] }
Expand Down
5 changes: 4 additions & 1 deletion core/handshake/src/transports/webrtc/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ impl WebRtcDriver {
Instant::now(),
Receive {
source,
destination: self.socket.local_addr().unwrap(),
destination: (
[127,0,0,1],
self.socket.local_addr().unwrap().port()
).into(),
contents: buf[..n].try_into().unwrap(),
},
)) {
Expand Down
25 changes: 19 additions & 6 deletions core/handshake/src/transports/webrtc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
mod driver;
mod signal;

use std::net::{IpAddr, SocketAddr};
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};

use anyhow::Result;
use async_trait::async_trait;
use axum::Router;
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use stunclient::StunClient;
use tokio::sync::mpsc::{channel, Receiver};
use tokio::sync::Notify;
use tracing::{info, warn};
use tracing::{debug, info, warn};
use triomphe::Arc;

use self::driver::{ConnectionMap, WebRtcDriver};
Expand All @@ -30,7 +31,7 @@ pub struct WebRtcConfig {
impl Default for WebRtcConfig {
fn default() -> Self {
Self {
udp_address: ([127, 0, 0, 1], 4320).into(),
udp_address: ([0, 0, 0, 0], 4320).into(),
}
}
}
Expand Down Expand Up @@ -60,8 +61,20 @@ impl Transport for WebRtcTransport {
// Bind the driver to the udp socket
let driver = WebRtcDriver::bind(config.udp_address, conns.clone()).await?;

// TODO: Get public address from config, and/or stun
let local_addr = driver.socket.local_addr()?;
let stun = StunClient::new(
"stun.l.google.com:19302"
.to_socket_addrs()
.unwrap()
.find(|x| x.is_ipv4())
.expect("failed to get stun server"),
);
let public_addr = stun.query_external_address_async(&driver.socket).await?;
debug!("Got public address via STUN: {public_addr}");

let mut local_addr = driver.socket.local_addr()?;
if local_addr.ip() == IpAddr::from([0, 0, 0, 0]) {
local_addr.set_ip(IpAddr::from([127, 0, 0, 1]));
}

// Spawn the IO loop
let notify = Arc::new(Notify::new());
Expand All @@ -70,7 +83,7 @@ impl Transport for WebRtcTransport {
// A bounded channel is used to provide some back pressure for incoming client handshakes.
let (conn_tx, conn_rx) = channel(1024);
// Construct our http router for negotiating via SDP.
let router = router(local_addr, conns.clone(), conn_tx)?;
let router = router(vec![public_addr, local_addr], conns.clone(), conn_tx)?;

Ok((
Self {
Expand Down
13 changes: 9 additions & 4 deletions core/handshake/src/transports/webrtc/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ struct SignalState {
// Sender for incoming connections (given to each connection)
conn_tx: Sender<(HandshakeRequestFrame, IpAddr, Receiver<RequestFrame>)>,
// Our address candidate used for the sdp response
host: Candidate,
host: Vec<Candidate>,
}

pub fn router(
udp_addr: SocketAddr,
udp_addrs: Vec<SocketAddr>,
client_map: ConnectionMap,
conn_tx: Sender<(HandshakeRequestFrame, IpAddr, Receiver<RequestFrame>)>,
) -> Result<Router> {
Expand All @@ -33,7 +33,10 @@ pub fn router(
.with_state(Arc::new(SignalState {
client_map,
conn_tx,
host: Candidate::host(udp_addr)?,
host: udp_addrs
.into_iter()
.map(|s| Candidate::host(s).expect("failed to parse candidate from socket address"))
.collect(),
})))
}

Expand All @@ -43,7 +46,9 @@ async fn handler(
Json(offer): Json<SdpOffer>,
) -> Result<Json<SdpAnswer>, String> {
let mut rtc = Rtc::new();
rtc.add_local_candidate(state.host.clone());
for host in &state.host {
rtc.add_local_candidate(host.clone());
}
let answer = rtc
.sdp_api()
.accept_offer(offer)
Expand Down
6 changes: 3 additions & 3 deletions core/rpc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use fleek_crypto::{
EthAddress,
NodePublicKey,
NodeSecretKey,
PublicKey,
SecretKey,
};
use hp_fixed::unsigned::HpUfixed;
Expand Down Expand Up @@ -332,8 +333,7 @@ async fn test_rpc_get_reputation() -> Result<()> {
genesis_node.reputation = Some(46);
genesis.node_info.push(genesis_node);

let (mut rpc, query_runner) = init_rpc_without_consensus(Some(genesis)).await.unwrap();
let node_index = query_runner.pubkey_to_index(node_public_key).unwrap();
let (mut rpc, _query_runner) = init_rpc_without_consensus(Some(genesis)).await.unwrap();
let port = 30002;
rpc.config.port = port;

Expand All @@ -345,7 +345,7 @@ async fn test_rpc_get_reputation() -> Result<()> {
let req = json!({
"jsonrpc": "2.0",
"method":"flk_get_reputation",
"params": node_index,
"params": {"public_key": node_public_key.to_base58()},
"id":1,
});

Expand Down

0 comments on commit ef51f26

Please sign in to comment.