From 28910f2bd9630174f994a02e93d567a3768a70c0 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Raynaud Date: Thu, 28 Mar 2024 17:55:01 +0100 Subject: [PATCH] feat: add more transports to peer swarm in relay --- Cargo.lock | 92 ++++++++++++++++++++++++++++++++++- mithril-relay/Cargo.toml | 4 ++ mithril-relay/src/p2p/peer.rs | 28 ++++++----- 3 files changed, 110 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5bfbb95cfb2..f47d2045ee9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -700,6 +700,15 @@ dependencies = [ "digest 0.10.7", ] +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -1433,7 +1442,7 @@ version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ - "block-buffer", + "block-buffer 0.10.4", "crypto-common", "subtle", ] @@ -2847,7 +2856,10 @@ dependencies = [ "libp2p-quic", "libp2p-swarm", "libp2p-tcp", + "libp2p-tls", "libp2p-upnp", + "libp2p-websocket", + "libp2p-websocket-websys", "libp2p-yamux", "multiaddr", "pin-project", @@ -3233,6 +3245,44 @@ dependencies = [ "void", ] +[[package]] +name = "libp2p-websocket" +version = "0.43.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4846d51afd08180e164291c3754ba30dd4fbac6fac65571be56403c16431a5e" +dependencies = [ + "either", + "futures", + "futures-rustls", + "libp2p-core", + "libp2p-identity", + "parking_lot", + "pin-project-lite", + "rw-stream-sink", + "soketto", + "tracing", + "url", + "webpki-roots", +] + +[[package]] +name = "libp2p-websocket-websys" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "550e578dcc9cd572be9dd564831d1f5efe8e6661953768b1d56c1d462855bf6f" +dependencies = [ + "bytes", + "futures", + "js-sys", + "libp2p-core", + "parking_lot", + "send_wrapper", + "thiserror", + "tracing", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "libp2p-yamux" version = "0.45.1" @@ -5345,6 +5395,12 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca" +[[package]] +name = "send_wrapper" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd0b0ec5f1c1ca621c432a25813d8d60c88abe6d3e08a3eb9cf37d97a0fe3d73" + [[package]] name = "serde" version = "1.0.197" @@ -5508,6 +5564,19 @@ dependencies = [ "unsafe-libyaml", ] +[[package]] +name = "sha-1" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" +dependencies = [ + "block-buffer 0.9.0", + "cfg-if", + "cpufeatures", + "digest 0.9.0", + "opaque-debug", +] + [[package]] name = "sha1" version = "0.10.6" @@ -5730,6 +5799,21 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "soketto" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d1c5305e39e09653383c2c7244f2f78b3bcae37cf50c64cb4789c9f5096ec2" +dependencies = [ + "base64 0.13.1", + "bytes", + "futures", + "httparse", + "log", + "rand", + "sha-1", +] + [[package]] name = "spin" version = "0.5.2" @@ -6713,6 +6797,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "webpki-roots" +version = "0.25.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" + [[package]] name = "widestring" version = "1.0.2" diff --git a/mithril-relay/Cargo.toml b/mithril-relay/Cargo.toml index a00366a09da..17bf60d6e64 100644 --- a/mithril-relay/Cargo.toml +++ b/mithril-relay/Cargo.toml @@ -24,7 +24,11 @@ libp2p = { version = "0.53.2", features = [ "noise", "ping", "pnet", + "quic", "tcp", + "tls", + "websocket-websys", + "websocket", "yamux", ] } mithril-common = { path = "../mithril-common", features = ["full"] } diff --git a/mithril-relay/src/p2p/peer.rs b/mithril-relay/src/p2p/peer.rs index 6e629d04005..2aa5fb56905 100644 --- a/mithril-relay/src/p2p/peer.rs +++ b/mithril-relay/src/p2p/peer.rs @@ -1,12 +1,12 @@ #![allow(missing_docs)] use anyhow::{anyhow, Context}; use libp2p::{ - core::upgrade::Version, + core::{muxing::StreamMuxerBox, transport::dummy::DummyTransport}, futures::StreamExt, gossipsub::{self, ValidationMode}, noise, ping, swarm::{self, DialError, NetworkBehaviour}, - tcp, yamux, Multiaddr, PeerId, Swarm, Transport, + tls, yamux, Multiaddr, PeerId, Swarm, SwarmBuilder, }; use mithril_common::{ messages::{RegisterSignatureMessage, RegisterSignerMessage}, @@ -104,19 +104,21 @@ impl Peer { /// Start the peer pub async fn start(mut self) -> StdResult { debug!("Peer: starting..."); - let mut swarm = libp2p::SwarmBuilder::with_new_identity() + let mut swarm = SwarmBuilder::with_new_identity() .with_tokio() - .with_other_transport(|key| { - let noise_config = noise::Config::new(key).unwrap(); - let yamux_config = yamux::Config::default(); - let base_transport = - tcp::tokio::Transport::new(tcp::Config::default().nodelay(true)); - base_transport - .upgrade(Version::V1Lazy) - .authenticate(noise_config) - .multiplex(yamux_config) - })? + .with_tcp( + Default::default(), + (tls::Config::new, noise::Config::new), + yamux::Config::default, + )? + .with_quic() + .with_other_transport(|_key| DummyTransport::<(PeerId, StreamMuxerBox)>::new())? .with_dns()? + .with_websocket( + (tls::Config::new, noise::Config::new), + yamux::Config::default, + ) + .await? .with_behaviour(|key| { let gossipsub_config = gossipsub::ConfigBuilder::default() .max_transmit_size(262144)