From 5a25520e5114bd76f1eda14d1565771568248f7f Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Thu, 1 Aug 2024 14:02:03 +0000 Subject: [PATCH 01/21] Introduce features --- Cargo.toml | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ade6d469..4a0e4f3f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ network-interface = "1.1.1" parking_lot = "0.12.3" pin-project = "1.1.0" prost = "0.12.6" -quinn = { version = "0.9.3", default-features = false, features = ["tls-rustls", "runtime-tokio"] } +quinn = { version = "0.9.3", default-features = false, features = ["tls-rustls", "runtime-tokio"], optional = true } rand = { version = "0.8.0", features = ["getrandom"] } rcgen = "0.10.0" ring = "0.16.20" @@ -36,10 +36,10 @@ simple-dns = "0.7.0" smallvec = "1.13.2" snow = { version = "0.9.3", features = ["ring-resolver"], default-features = false } socket2 = { version = "0.5.7", features = ["all"] } -str0m = "0.5.1" +str0m = { version = "0.5.1", optional = true } thiserror = "1.0.61" tokio-stream = "0.1.12" -tokio-tungstenite = { version = "0.20.0", features = ["rustls-tls-native-roots"] } +tokio-tungstenite = { version = "0.20.0", features = ["rustls-tls-native-roots"], optional = true } tokio-util = { version = "0.7.11", features = ["compat", "io", "codec"] } tokio = { version = "1.26.0", features = ["rt", "net", "io-util", "time", "macros", "sync", "parking_lot"] } tracing = { version = "0.1.40", features = ["log"] } @@ -47,7 +47,7 @@ trust-dns-resolver = "0.23.2" uint = "0.9.5" unsigned-varint = { version = "0.8.0", features = ["codec"] } url = "2.4.0" -webpki = "0.22.4" +webpki = { version = "0.22.4", optional = true } x25519-dalek = "2.0.0" x509-parser = "0.16.0" yasna = "0.5.0" @@ -87,6 +87,10 @@ futures_ringbuf = "0.4.0" [features] custom_sc_network = [] +tls = ["dep:webpki"] +quick = ["tls", "dep:quinn"] +webrtc = ["dep:str0m"] +websocket = ["dep:tokio-tungstenite"] [profile.release] debug = true From b5299aaabaff81e5b8ec7123f26c76bb39db05b5 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Thu, 1 Aug 2024 14:03:41 +0000 Subject: [PATCH 02/21] Feature `tls` --- src/crypto/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index 50436e98..74ae5863 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -25,6 +25,7 @@ use crate::{error::*, peer_id::*}; pub mod ed25519; pub(crate) mod noise; +#[cfg(feature = "tls")] pub(crate) mod tls; pub(crate) mod keys_proto { include!(concat!(env!("OUT_DIR"), "/keys_proto.rs")); From 02e8cddca2af07b160ed656d6154a09c30d6d178 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Thu, 1 Aug 2024 14:06:27 +0000 Subject: [PATCH 03/21] Feature-aware `Error` --- src/error.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/error.rs b/src/error.rs index 05dbbd82..d0d9b079 100644 --- a/src/error.rs +++ b/src/error.rs @@ -83,18 +83,21 @@ pub enum Error { DnsAddressResolutionFailed, #[error("Transport error: `{0}`")] TransportError(String), + #[cfg(feature = "tls")] #[error("Failed to generate certificate: `{0}`")] CertificateGeneration(#[from] crate::crypto::tls::certificate::GenError), #[error("Invalid data")] InvalidData, #[error("Input rejected")] InputRejected, + #[cfg(feature = "websocket")] #[error("WebSocket error: `{0}`")] WebSocket(#[from] tokio_tungstenite::tungstenite::error::Error), #[error("Insufficient peers")] InsufficientPeers, #[error("Substream doens't exist")] SubstreamDoesntExist, + #[cfg(feature = "webrtc")] #[error("`str0m` error: `{0}`")] WebRtc(#[from] str0m::RtcError), #[error("Remote peer disconnected")] @@ -109,6 +112,7 @@ pub enum Error { NoAddressAvailable(PeerId), #[error("Connection closed")] ConnectionClosed, + #[cfg(feature = "quick")] #[error("Quinn error: `{0}`")] Quinn(quinn::ConnectionError), #[error("Invalid certificate")] @@ -237,6 +241,7 @@ impl From for Error { } } +#[cfg(feature = "quick")] impl From for Error { fn from(error: quinn::ConnectionError) -> Self { match error { From d4cd8c3142c5abd710c7f9fbcde887bd2273b41c Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Thu, 1 Aug 2024 14:20:27 +0000 Subject: [PATCH 04/21] Make `LiteP2p` & `LiteP2pConfig` feature-aware --- src/config.rs | 27 ++++++++++++++++++++++++--- src/lib.rs | 16 +++++++++++++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 75620a59..b17dbd37 100644 --- a/src/config.rs +++ b/src/config.rs @@ -29,14 +29,20 @@ use crate::{ notification, request_response, UserProtocol, }, transport::{ - manager::limits::ConnectionLimitsConfig, quic::config::Config as QuicConfig, - tcp::config::Config as TcpConfig, webrtc::config::Config as WebRtcConfig, - websocket::config::Config as WebSocketConfig, MAX_PARALLEL_DIALS, + manager::limits::ConnectionLimitsConfig, tcp::config::Config as TcpConfig, + MAX_PARALLEL_DIALS, }, types::protocol::ProtocolName, PeerId, }; +#[cfg(feature = "quick")] +use crate::transport::quick::config::Config as QuicConfig; +#[cfg(feature = "webrtc")] +use crate::transport::webrtc::config::Config as WebRtcConfig; +#[cfg(feature = "websocket")] +use crate::transport::websocket::config::Config as WebSocketConfig; + use multiaddr::Multiaddr; use std::{collections::HashMap, sync::Arc}; @@ -66,12 +72,15 @@ pub struct ConfigBuilder { tcp: Option, /// QUIC transport config. + #[cfg(feature = "quick")] quic: Option, /// WebRTC transport config. + #[cfg(feature = "webrtc")] webrtc: Option, /// WebSocket transport config. + #[cfg(feature = "websocket")] websocket: Option, /// Keypair. @@ -125,8 +134,11 @@ impl ConfigBuilder { pub fn new() -> Self { Self { tcp: None, + #[cfg(feature = "quick")] quic: None, + #[cfg(feature = "webrtc")] webrtc: None, + #[cfg(feature = "websocket")] websocket: None, keypair: None, ping: None, @@ -151,18 +163,21 @@ impl ConfigBuilder { } /// Add QUIC transport configuration, enabling the transport. + #[cfg(feature = "quick")] pub fn with_quic(mut self, config: QuicConfig) -> Self { self.quic = Some(config); self } /// Add WebRTC transport configuration, enabling the transport. + #[cfg(feature = "webrtc")] pub fn with_webrtc(mut self, config: WebRtcConfig) -> Self { self.webrtc = Some(config); self } /// Add WebSocket transport configuration, enabling the transport. + #[cfg(feature = "websocket")] pub fn with_websocket(mut self, config: WebSocketConfig) -> Self { self.websocket = Some(config); self @@ -264,8 +279,11 @@ impl ConfigBuilder { keypair, tcp: self.tcp.take(), mdns: self.mdns.take(), + #[cfg(feature = "quick")] quic: self.quic.take(), + #[cfg(feature = "webrtc")] webrtc: self.webrtc.take(), + #[cfg(feature = "websocket")] websocket: self.websocket.take(), ping: self.ping.take(), identify: self.identify.take(), @@ -288,12 +306,15 @@ pub struct Litep2pConfig { pub(crate) tcp: Option, /// QUIC transport config. + #[cfg(feature = "quick")] pub(crate) quic: Option, /// WebRTC transport config. + #[cfg(feature = "webrtc")] pub(crate) webrtc: Option, /// WebSocket transport config. + #[cfg(feature = "websocket")] pub(crate) websocket: Option, /// Keypair. diff --git a/src/lib.rs b/src/lib.rs index ef75a3b7..852ae70e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,14 +38,18 @@ use crate::{ }, transport::{ manager::{SupportedTransport, TransportManager}, - quic::QuicTransport, tcp::TcpTransport, - webrtc::WebRtcTransport, - websocket::WebSocketTransport, TransportBuilder, TransportEvent, }, }; +#[cfg(feature = "quick")] +use crate::transport::quick::QuicTransport; +#[cfg(feature = "webrtc")] +use crate::transport::webrtc::WebRtcTransport; +#[cfg(feature = "websocket")] +use crate::transport::websocket::WebSocketTransport; + use multiaddr::{Multiaddr, Protocol}; use multihash::Multihash; use transport::Endpoint; @@ -297,6 +301,7 @@ impl Litep2p { } // enable quic transport if the config exists + #[cfg(feature = "quick")] if let Some(config) = litep2p_config.quic.take() { let handle = transport_manager.transport_handle(Arc::clone(&litep2p_config.executor)); let (transport, transport_listen_addresses) = @@ -313,6 +318,7 @@ impl Litep2p { } // enable webrtc transport if the config exists + #[cfg(feature = "webrtc")] if let Some(config) = litep2p_config.webrtc.take() { let handle = transport_manager.transport_handle(Arc::clone(&litep2p_config.executor)); let (transport, transport_listen_addresses) = @@ -329,6 +335,7 @@ impl Litep2p { } // enable websocket transport if the config exists + #[cfg(feature = "websocket")] if let Some(config) = litep2p_config.websocket.take() { let handle = transport_manager.transport_handle(Arc::clone(&litep2p_config.executor)); let (transport, transport_listen_addresses) = @@ -396,14 +403,17 @@ impl Litep2p { .tcp .is_some() .then(|| supported_transports.insert(SupportedTransport::Tcp)); + #[cfg(feature = "quick")] config .quic .is_some() .then(|| supported_transports.insert(SupportedTransport::Quic)); + #[cfg(feature = "websocket")] config .websocket .is_some() .then(|| supported_transports.insert(SupportedTransport::WebSocket)); + #[cfg(feature = "webrtc")] config .webrtc .is_some() From 31b2b9ecfb341e04c5ea014bcccde33c4c9a980a Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Thu, 1 Aug 2024 14:27:45 +0000 Subject: [PATCH 05/21] CI: test all features --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bdae2e5a..56189d91 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -147,4 +147,4 @@ jobs: cache-all-crates: true - name: Run tests - run: cargo test + run: cargo test --all-features From 925c7464a6fd2e1b90fec8cdab616d8bda478bee Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Thu, 1 Aug 2024 15:09:49 +0000 Subject: [PATCH 06/21] Make transport manager feature-aware --- Cargo.toml | 2 +- src/config.rs | 14 ++++----- src/error.rs | 4 +-- src/lib.rs | 8 ++--- src/transport/manager/handle.rs | 35 ++++++++++++++------- src/transport/manager/mod.rs | 56 ++++++++++++++++++++------------- src/transport/manager/types.rs | 3 ++ src/transport/mod.rs | 3 ++ 8 files changed, 77 insertions(+), 48 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4a0e4f3f..8d3badc1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -88,7 +88,7 @@ futures_ringbuf = "0.4.0" [features] custom_sc_network = [] tls = ["dep:webpki"] -quick = ["tls", "dep:quinn"] +quic = ["tls", "dep:quinn"] webrtc = ["dep:str0m"] websocket = ["dep:tokio-tungstenite"] diff --git a/src/config.rs b/src/config.rs index b17dbd37..34d67ca0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -36,8 +36,8 @@ use crate::{ PeerId, }; -#[cfg(feature = "quick")] -use crate::transport::quick::config::Config as QuicConfig; +#[cfg(feature = "quic")] +use crate::transport::quic::config::Config as QuicConfig; #[cfg(feature = "webrtc")] use crate::transport::webrtc::config::Config as WebRtcConfig; #[cfg(feature = "websocket")] @@ -72,7 +72,7 @@ pub struct ConfigBuilder { tcp: Option, /// QUIC transport config. - #[cfg(feature = "quick")] + #[cfg(feature = "quic")] quic: Option, /// WebRTC transport config. @@ -134,7 +134,7 @@ impl ConfigBuilder { pub fn new() -> Self { Self { tcp: None, - #[cfg(feature = "quick")] + #[cfg(feature = "quic")] quic: None, #[cfg(feature = "webrtc")] webrtc: None, @@ -163,7 +163,7 @@ impl ConfigBuilder { } /// Add QUIC transport configuration, enabling the transport. - #[cfg(feature = "quick")] + #[cfg(feature = "quic")] pub fn with_quic(mut self, config: QuicConfig) -> Self { self.quic = Some(config); self @@ -279,7 +279,7 @@ impl ConfigBuilder { keypair, tcp: self.tcp.take(), mdns: self.mdns.take(), - #[cfg(feature = "quick")] + #[cfg(feature = "quic")] quic: self.quic.take(), #[cfg(feature = "webrtc")] webrtc: self.webrtc.take(), @@ -306,7 +306,7 @@ pub struct Litep2pConfig { pub(crate) tcp: Option, /// QUIC transport config. - #[cfg(feature = "quick")] + #[cfg(feature = "quic")] pub(crate) quic: Option, /// WebRTC transport config. diff --git a/src/error.rs b/src/error.rs index d0d9b079..0de5fefb 100644 --- a/src/error.rs +++ b/src/error.rs @@ -112,7 +112,7 @@ pub enum Error { NoAddressAvailable(PeerId), #[error("Connection closed")] ConnectionClosed, - #[cfg(feature = "quick")] + #[cfg(feature = "quic")] #[error("Quinn error: `{0}`")] Quinn(quinn::ConnectionError), #[error("Invalid certificate")] @@ -241,7 +241,7 @@ impl From for Error { } } -#[cfg(feature = "quick")] +#[cfg(feature = "quic")] impl From for Error { fn from(error: quinn::ConnectionError) -> Self { match error { diff --git a/src/lib.rs b/src/lib.rs index 852ae70e..4c0608dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,7 +43,7 @@ use crate::{ }, }; -#[cfg(feature = "quick")] +#[cfg(feature = "quic")] use crate::transport::quick::QuicTransport; #[cfg(feature = "webrtc")] use crate::transport::webrtc::WebRtcTransport; @@ -301,7 +301,7 @@ impl Litep2p { } // enable quic transport if the config exists - #[cfg(feature = "quick")] + #[cfg(feature = "quic")] if let Some(config) = litep2p_config.quic.take() { let handle = transport_manager.transport_handle(Arc::clone(&litep2p_config.executor)); let (transport, transport_listen_addresses) = @@ -403,7 +403,7 @@ impl Litep2p { .tcp .is_some() .then(|| supported_transports.insert(SupportedTransport::Tcp)); - #[cfg(feature = "quick")] + #[cfg(feature = "quic")] config .quic .is_some() @@ -525,7 +525,6 @@ mod tests { let config = ConfigBuilder::new() .with_tcp(Default::default()) - .with_quic(Default::default()) .with_notification_protocol(config1) .with_notification_protocol(config2) .with_libp2p_ping(ping_config) @@ -601,7 +600,6 @@ mod tests { let config = ConfigBuilder::new() .with_tcp(Default::default()) - .with_quic(Default::default()) .with_notification_protocol(config1) .with_notification_protocol(config2) .with_libp2p_ping(ping_config) diff --git a/src/transport/manager/handle.rs b/src/transport/manager/handle.rs index 833308ea..29fd8ab2 100644 --- a/src/transport/manager/handle.rs +++ b/src/transport/manager/handle.rs @@ -121,16 +121,18 @@ impl TransportManagerHandle { match iter.next() { None => false, - Some(Protocol::Tcp(_)) => match ( - iter.next(), - self.supported_transport.contains(&SupportedTransport::WebSocket), - ) { - (Some(Protocol::Ws(_)), true) => true, - (Some(Protocol::Wss(_)), true) => true, - (Some(Protocol::P2p(_)), _) => + Some(Protocol::Tcp(_)) => match iter.next() { + Some(Protocol::P2p(_)) => self.supported_transport.contains(&SupportedTransport::Tcp), + #[cfg(feature = "websocket")] + Some(Protocol::Ws(_)) => + self.supported_transport.contains(&SupportedTransport::WebSocket), + #[cfg(feature = "websocket")] + Some(Protocol::Wss(_)) => + self.supported_transport.contains(&SupportedTransport::WebSocket), _ => false, }, + #[cfg(feature = "quic")] Some(Protocol::Udp(_)) => match ( iter.next(), self.supported_transport.contains(&SupportedTransport::Quic), @@ -333,9 +335,21 @@ mod tests { } #[tokio::test] - async fn tcp_and_websocket_supported() { + async fn tcp_supported() { let (mut handle, _rx) = make_transport_manager_handle(); handle.supported_transport.insert(SupportedTransport::Tcp); + + let address = + "/dns4/google.com/tcp/24928/p2p/12D3KooWKrUnV42yDR7G6DewmgHtFaVCJWLjQRi2G9t5eJD3BvTy" + .parse() + .unwrap(); + assert!(handle.supported_transport(&address)); + } + + #[cfg(feature = "websocket")] + #[tokio::test] + async fn websocket_supported() { + let (mut handle, _rx) = make_transport_manager_handle(); handle.supported_transport.insert(SupportedTransport::WebSocket); let address = @@ -378,7 +392,7 @@ mod tests { #[test] fn zero_addresses_added() { let (mut handle, _rx) = make_transport_manager_handle(); - handle.supported_transport.insert(SupportedTransport::Quic); + handle.supported_transport.insert(SupportedTransport::Tcp); assert!( handle.add_known_address( @@ -388,9 +402,6 @@ mod tests { .with(Protocol::Ip4(std::net::Ipv4Addr::new(127, 0, 0, 1))) .with(Protocol::Udp(8888)) .with(Protocol::Utp), - Multiaddr::empty() - .with(Protocol::Ip4(std::net::Ipv4Addr::new(127, 0, 0, 1))) - .with(Protocol::Tcp(8888)), Multiaddr::empty() .with(Protocol::Ip4(std::net::Ipv4Addr::new(127, 0, 0, 1))) .with(Protocol::Tcp(8888)) diff --git a/src/transport/manager/mod.rs b/src/transport/manager/mod.rs index 0129e241..d55934c7 100644 --- a/src/transport/manager/mod.rs +++ b/src/transport/manager/mod.rs @@ -499,33 +499,35 @@ impl TransportManager { ); let mut transports = HashSet::new(); + #[cfg(feature = "websocket")] let mut websocket = Vec::new(); + #[cfg(feature = "quic")] let mut quic = Vec::new(); let mut tcp = Vec::new(); for (address, record) in &mut records { record.set_connection_id(connection_id); - let mut iter = address.iter(); - match iter.find(|protocol| std::matches!(protocol, Protocol::QuicV1)) { - Some(_) => { - quic.push(address.clone()); - transports.insert(SupportedTransport::Quic); - } - _ => match address - .iter() - .find(|protocol| std::matches!(protocol, Protocol::Ws(_) | Protocol::Wss(_))) - { - Some(_) => { - websocket.push(address.clone()); - transports.insert(SupportedTransport::WebSocket); - } - None => { - tcp.push(address.clone()); - transports.insert(SupportedTransport::Tcp); - } - }, + #[cfg(feature = "quic")] + if address.iter().find(|p| std::matches!(p, Protocol::QuicV1)).is_some() { + quic.push(address.clone()); + transports.insert(SupportedTransport::Quic); + continue; } + + #[cfg(feature = "websocket")] + if address + .iter() + .find(|p| std::matches!(p, Protocol::Ws(_) | Protocol::Wss(_))) + .is_some() + { + websocket.push(address.clone()); + transports.insert(SupportedTransport::WebSocket); + continue; + } + + tcp.push(address.clone()); + transports.insert(SupportedTransport::Tcp); } peers.insert( @@ -548,6 +550,7 @@ impl TransportManager { .open(connection_id, tcp)?; } + #[cfg(feature = "quic")] if !quic.is_empty() { self.transports .get_mut(&SupportedTransport::Quic) @@ -555,6 +558,7 @@ impl TransportManager { .open(connection_id, quic)?; } + #[cfg(feature = "websocket")] if !websocket.is_empty() { self.transports .get_mut(&SupportedTransport::WebSocket) @@ -604,10 +608,12 @@ impl TransportManager { .ok_or_else(|| Error::TransportNotSupported(record.address().clone()))? { Protocol::Tcp(_) => match protocol_stack.next() { + #[cfg(feature = "websocket")] Some(Protocol::Ws(_)) | Some(Protocol::Wss(_)) => SupportedTransport::WebSocket, Some(Protocol::P2p(_)) => SupportedTransport::Tcp, _ => return Err(Error::TransportNotSupported(record.address().clone())), }, + #[cfg(feature = "quic")] Protocol::Udp(_) => match protocol_stack .next() .ok_or_else(|| Error::TransportNotSupported(record.address().clone()))? @@ -622,7 +628,7 @@ impl TransportManager { tracing::error!( target: LOG_TARGET, ?protocol, - "invalid protocol, expected `tcp`" + "invalid protocol" ); return Err(Error::TransportNotSupported(record.address().clone())); @@ -2020,9 +2026,14 @@ mod tests { .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) .try_init(); + let mut transports = HashSet::new(); + transports.insert(SupportedTransport::Tcp); + #[cfg(feature = "quic")] + transports.insert(SupportedTransport::Quic); + let (_manager, handle) = TransportManager::new( Keypair::generate(), - HashSet::from_iter([SupportedTransport::Tcp, SupportedTransport::Quic]), + transports, BandwidthSink::new(), 8usize, ConnectionLimitsConfig::default(), @@ -2054,7 +2065,10 @@ mod tests { .with(Protocol::P2p( Multihash::from_bytes(&PeerId::random().to_bytes()).unwrap(), )); + #[cfg(feature = "quic")] assert!(handle.supported_transport(&address)); + #[cfg(not(feature = "quic"))] + assert!(!handle.supported_transport(&address)); // websocket let address = Multiaddr::empty() diff --git a/src/transport/manager/types.rs b/src/transport/manager/types.rs index befabf0f..b367a086 100644 --- a/src/transport/manager/types.rs +++ b/src/transport/manager/types.rs @@ -34,12 +34,15 @@ pub enum SupportedTransport { Tcp, /// QUIC. + #[cfg(feature = "quic")] Quic, /// WebRTC + #[cfg(feature = "webrtc")] WebRtc, /// WebSocket + #[cfg(feature = "websocket")] WebSocket, } diff --git a/src/transport/mod.rs b/src/transport/mod.rs index 1885a641..bad76080 100644 --- a/src/transport/mod.rs +++ b/src/transport/mod.rs @@ -28,9 +28,12 @@ use multiaddr::Multiaddr; use std::{fmt::Debug, time::Duration}; pub(crate) mod common; +#[cfg(feature = "quic")] pub mod quic; pub mod tcp; +#[cfg(feature = "webrtc")] pub mod webrtc; +#[cfg(feature = "websocket")] pub mod websocket; pub(crate) mod dummy; From bf35e8d7b6a4f6d23d958e879475062436767f98 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Thu, 1 Aug 2024 16:03:57 +0000 Subject: [PATCH 07/21] Make substream feature-aware --- src/substream/mod.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/substream/mod.rs b/src/substream/mod.rs index 7cc41fa3..5fc1ec3c 100644 --- a/src/substream/mod.rs +++ b/src/substream/mod.rs @@ -24,11 +24,18 @@ use crate::{ codec::ProtocolCodec, error::{Error, SubstreamError}, - transport::{quic, tcp, webrtc, websocket}, + transport::tcp, types::SubstreamId, PeerId, }; +#[cfg(feature = "quic")] +use crate::transport::quic; +#[cfg(feature = "webrtc")] +use crate::transport::webrtc; +#[cfg(feature = "websocket")] +use crate::transport::websocket; + use bytes::{Buf, Bytes, BytesMut}; use futures::{Sink, Stream}; use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt, ReadBuf}; @@ -50,8 +57,11 @@ macro_rules! poll_flush { ($substream:expr, $cx:ident) => {{ match $substream { SubstreamType::Tcp(substream) => Pin::new(substream).poll_flush($cx), + #[cfg(feature = "websocket")] SubstreamType::WebSocket(substream) => Pin::new(substream).poll_flush($cx), + #[cfg(feature = "quic")] SubstreamType::Quic(substream) => Pin::new(substream).poll_flush($cx), + #[cfg(feature = "webrtc")] SubstreamType::WebRtc(substream) => Pin::new(substream).poll_flush($cx), #[cfg(test)] SubstreamType::Mock(_) => unreachable!(), @@ -63,8 +73,11 @@ macro_rules! poll_write { ($substream:expr, $cx:ident, $frame:expr) => {{ match $substream { SubstreamType::Tcp(substream) => Pin::new(substream).poll_write($cx, $frame), + #[cfg(feature = "websocket")] SubstreamType::WebSocket(substream) => Pin::new(substream).poll_write($cx, $frame), + #[cfg(feature = "quic")] SubstreamType::Quic(substream) => Pin::new(substream).poll_write($cx, $frame), + #[cfg(feature = "webrtc")] SubstreamType::WebRtc(substream) => Pin::new(substream).poll_write($cx, $frame), #[cfg(test)] SubstreamType::Mock(_) => unreachable!(), @@ -76,8 +89,11 @@ macro_rules! poll_read { ($substream:expr, $cx:ident, $buffer:expr) => {{ match $substream { SubstreamType::Tcp(substream) => Pin::new(substream).poll_read($cx, $buffer), + #[cfg(feature = "websocket")] SubstreamType::WebSocket(substream) => Pin::new(substream).poll_read($cx, $buffer), + #[cfg(feature = "quic")] SubstreamType::Quic(substream) => Pin::new(substream).poll_read($cx, $buffer), + #[cfg(feature = "webrtc")] SubstreamType::WebRtc(substream) => Pin::new(substream).poll_read($cx, $buffer), #[cfg(test)] SubstreamType::Mock(_) => unreachable!(), @@ -89,8 +105,11 @@ macro_rules! poll_shutdown { ($substream:expr, $cx:ident) => {{ match $substream { SubstreamType::Tcp(substream) => Pin::new(substream).poll_shutdown($cx), + #[cfg(feature = "websocket")] SubstreamType::WebSocket(substream) => Pin::new(substream).poll_shutdown($cx), + #[cfg(feature = "quic")] SubstreamType::Quic(substream) => Pin::new(substream).poll_shutdown($cx), + #[cfg(feature = "webrtc")] SubstreamType::WebRtc(substream) => Pin::new(substream).poll_shutdown($cx), #[cfg(test)] SubstreamType::Mock(substream) => { @@ -150,8 +169,11 @@ macro_rules! check_size { /// Substream type. enum SubstreamType { Tcp(tcp::Substream), + #[cfg(feature = "websocket")] WebSocket(websocket::Substream), + #[cfg(feature = "quic")] Quic(quic::Substream), + #[cfg(feature = "webrtc")] WebRtc(webrtc::Substream), #[cfg(test)] Mock(Box), @@ -161,8 +183,11 @@ impl fmt::Debug for SubstreamType { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Tcp(_) => write!(f, "Tcp"), + #[cfg(feature = "websocket")] Self::WebSocket(_) => write!(f, "WebSocket"), + #[cfg(feature = "quic")] Self::Quic(_) => write!(f, "Quic"), + #[cfg(feature = "webrtc")] Self::WebRtc(_) => write!(f, "WebRtc"), #[cfg(test)] Self::Mock(_) => write!(f, "Mock"), @@ -254,6 +279,7 @@ impl Substream { } /// Create new [`Substream`] for WebSocket. + #[cfg(feature = "websocket")] pub(crate) fn new_websocket( peer: PeerId, substream_id: SubstreamId, @@ -271,6 +297,7 @@ impl Substream { } /// Create new [`Substream`] for QUIC. + #[cfg(feature = "quic")] pub(crate) fn new_quic( peer: PeerId, substream_id: SubstreamId, @@ -283,6 +310,7 @@ impl Substream { } /// Create new [`Substream`] for WebRTC. + #[cfg(feature = "webrtc")] pub(crate) fn new_webrtc( peer: PeerId, substream_id: SubstreamId, @@ -315,8 +343,11 @@ impl Substream { pub async fn close(self) { let _ = match self.substream { SubstreamType::Tcp(mut substream) => substream.shutdown().await, + #[cfg(feature = "websocket")] SubstreamType::WebSocket(mut substream) => substream.shutdown().await, + #[cfg(feature = "quic")] SubstreamType::Quic(mut substream) => substream.shutdown().await, + #[cfg(feature = "webrtc")] SubstreamType::WebRtc(mut substream) => substream.shutdown().await, #[cfg(test)] SubstreamType::Mock(mut substream) => { @@ -391,6 +422,7 @@ impl Substream { substream.flush().await.map_err(From::from) } }, + #[cfg(feature = "websocket")] SubstreamType::WebSocket(ref mut substream) => match self.codec { ProtocolCodec::Unspecified => panic!("codec is unspecified"), ProtocolCodec::Identity(payload_size) => @@ -414,6 +446,7 @@ impl Substream { substream.flush().await.map_err(From::from) } }, + #[cfg(feature = "quic")] SubstreamType::Quic(ref mut substream) => match self.codec { ProtocolCodec::Unspecified => panic!("codec is unspecified"), ProtocolCodec::Identity(payload_size) => @@ -428,6 +461,7 @@ impl Substream { substream.write_all_chunks(&mut [len.freeze(), bytes]).await } }, + #[cfg(feature = "webrtc")] SubstreamType::WebRtc(ref mut substream) => match self.codec { ProtocolCodec::Unspecified => panic!("codec is unspecified"), ProtocolCodec::Identity(payload_size) => From b43226059e22b87a560edca846cc37fbea19645c Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Thu, 1 Aug 2024 16:08:36 +0000 Subject: [PATCH 08/21] Remove misleading `tls` feature --- Cargo.toml | 11 +++++++++-- src/crypto/mod.rs | 2 +- src/error.rs | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8d3badc1..eaf8c535 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -87,10 +87,17 @@ futures_ringbuf = "0.4.0" [features] custom_sc_network = [] -tls = ["dep:webpki"] -quic = ["tls", "dep:quinn"] +quic = ["dep:webpki", "dep:quinn"] webrtc = ["dep:str0m"] websocket = ["dep:tokio-tungstenite"] [profile.release] debug = true + +[[example]] +name = "echo_notification" +required-features = ["quic"] + +[[example]] +name = "syncing" +required-features = ["quic"] diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index 74ae5863..4ddaeada 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -25,7 +25,7 @@ use crate::{error::*, peer_id::*}; pub mod ed25519; pub(crate) mod noise; -#[cfg(feature = "tls")] +#[cfg(feature = "quic")] pub(crate) mod tls; pub(crate) mod keys_proto { include!(concat!(env!("OUT_DIR"), "/keys_proto.rs")); diff --git a/src/error.rs b/src/error.rs index 0de5fefb..947f2239 100644 --- a/src/error.rs +++ b/src/error.rs @@ -83,7 +83,7 @@ pub enum Error { DnsAddressResolutionFailed, #[error("Transport error: `{0}`")] TransportError(String), - #[cfg(feature = "tls")] + #[cfg(feature = "quic")] #[error("Failed to generate certificate: `{0}`")] CertificateGeneration(#[from] crate::crypto::tls::certificate::GenError), #[error("Invalid data")] From 06960621027bd0799f90c85252cdea20894e0dbb Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Thu, 1 Aug 2024 16:18:10 +0000 Subject: [PATCH 09/21] Fix substream & webrtc tests --- tests/substream.rs | 36 ++++++++++++++++++++++++++++++++---- tests/webrtc.rs | 2 ++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/tests/substream.rs b/tests/substream.rs index c4b50f6c..61472ea1 100644 --- a/tests/substream.rs +++ b/tests/substream.rs @@ -23,14 +23,16 @@ use litep2p::{ config::ConfigBuilder, protocol::{Direction, TransportEvent, TransportService, UserProtocol}, substream::{Substream, SubstreamSet}, - transport::{ - quic::config::Config as QuicConfig, tcp::config::Config as TcpConfig, - websocket::config::Config as WebSocketConfig, - }, + transport::tcp::config::Config as TcpConfig, types::{protocol::ProtocolName, SubstreamId}, Error, Litep2p, Litep2pEvent, PeerId, }; +#[cfg(feature = "quic")] +use litep2p::transport::quic::config::Config as QuicConfig; +#[cfg(feature = "websocket")] +use litep2p::transport::websocket::config::Config as WebSocketConfig; + use bytes::Bytes; use futures::{Sink, SinkExt, StreamExt}; use tokio::{ @@ -50,7 +52,9 @@ use std::{ enum Transport { Tcp(TcpConfig), + #[cfg(feature = "quic")] Quic(QuicConfig), + #[cfg(feature = "websocket")] WebSocket(WebSocketConfig), } @@ -243,6 +247,7 @@ async fn too_big_identity_payload_framed_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn too_big_identity_payload_framed_quic() { too_big_identity_payload_framed( @@ -252,6 +257,7 @@ async fn too_big_identity_payload_framed_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn too_big_identity_payload_framed_websocket() { too_big_identity_payload_framed( @@ -270,7 +276,9 @@ async fn too_big_identity_payload_framed(transport1: Transport, transport2: Tran let (custom_protocol1, tx1) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config1 = match transport1 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol1)) @@ -279,7 +287,9 @@ async fn too_big_identity_payload_framed(transport1: Transport, transport2: Tran let (custom_protocol2, _tx2) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config2 = match transport2 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "wesocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol2)) @@ -328,6 +338,7 @@ async fn too_big_identity_payload_sink_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn too_big_identity_payload_sink_quic() { too_big_identity_payload_sink( @@ -337,6 +348,7 @@ async fn too_big_identity_payload_sink_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn too_big_identity_payload_sink_websocket() { too_big_identity_payload_sink( @@ -355,7 +367,9 @@ async fn too_big_identity_payload_sink(transport1: Transport, transport2: Transp let (custom_protocol1, tx1) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config1 = match transport1 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol1)) @@ -364,7 +378,9 @@ async fn too_big_identity_payload_sink(transport1: Transport, transport2: Transp let (custom_protocol2, _tx2) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config2 = match transport2 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "webscocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol2)) @@ -415,6 +431,7 @@ async fn correct_payload_size_sink_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn correct_payload_size_sink_quic() { correct_payload_size_sink( @@ -424,6 +441,7 @@ async fn correct_payload_size_sink_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn correct_payload_size_sink_websocket() { correct_payload_size_sink( @@ -442,7 +460,9 @@ async fn correct_payload_size_sink(transport1: Transport, transport2: Transport) let (custom_protocol1, tx1) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config1 = match transport1 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol1)) @@ -451,7 +471,9 @@ async fn correct_payload_size_sink(transport1: Transport, transport2: Transport) let (custom_protocol2, _tx2) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config2 = match transport2 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol2)) @@ -499,6 +521,7 @@ async fn correct_payload_size_async_write_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn correct_payload_size_async_write_quic() { correct_payload_size_async_write( @@ -508,6 +531,7 @@ async fn correct_payload_size_async_write_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn correct_payload_size_async_write_websocket() { correct_payload_size_async_write( @@ -526,7 +550,9 @@ async fn correct_payload_size_async_write(transport1: Transport, transport2: Tra let (custom_protocol1, tx1) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config1 = match transport1 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol1)) @@ -535,7 +561,9 @@ async fn correct_payload_size_async_write(transport1: Transport, transport2: Tra let (custom_protocol2, _tx2) = CustomProtocol::new(ProtocolCodec::Identity(10usize)); let config2 = match transport2 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol2)) diff --git a/tests/webrtc.rs b/tests/webrtc.rs index d80c6cb2..fd0651f8 100644 --- a/tests/webrtc.rs +++ b/tests/webrtc.rs @@ -18,6 +18,8 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +#![cfg(feature = "webrtc")] + use futures::StreamExt; use litep2p::{ config::ConfigBuilder as Litep2pConfigBuilder, From afae0d5d2303a98c321cbf2c138b0174a19b7844 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Fri, 2 Aug 2024 13:01:11 +0000 Subject: [PATCH 10/21] Make connection tests feature-aware --- tests/conformance/rust/mod.rs | 2 +- tests/connection/mod.rs | 92 ++++++++++++++++++++++++++++++++--- 2 files changed, 86 insertions(+), 8 deletions(-) diff --git a/tests/conformance/rust/mod.rs b/tests/conformance/rust/mod.rs index 67ef0d45..ed8e3cd7 100644 --- a/tests/conformance/rust/mod.rs +++ b/tests/conformance/rust/mod.rs @@ -24,5 +24,5 @@ mod identify; mod kademlia; #[cfg(test)] mod ping; -#[cfg(test)] +#[cfg(all(test, feature = "quic"))] mod quic_ping; diff --git a/tests/connection/mod.rs b/tests/connection/mod.rs index 33524cae..e95506bb 100644 --- a/tests/connection/mod.rs +++ b/tests/connection/mod.rs @@ -21,27 +21,33 @@ use litep2p::{ config::ConfigBuilder, crypto::ed25519::Keypair, - error::{AddressError, Error}, + error::Error, protocol::libp2p::ping::{Config as PingConfig, PingEvent}, - transport::{ - quic::config::Config as QuicConfig, tcp::config::Config as TcpConfig, - websocket::config::Config as WebSocketConfig, - }, + transport::tcp::config::Config as TcpConfig, Litep2p, Litep2pEvent, PeerId, }; +#[cfg(feature = "websocket")] +use litep2p::transport::websocket::config::Config as WebSocketConfig; +#[cfg(feature = "quic")] +use litep2p::{error::AddressError, transport::quic::config::Config as QuicConfig}; + use futures::{Stream, StreamExt}; use multiaddr::{Multiaddr, Protocol}; use multihash::Multihash; use network_interface::{NetworkInterface, NetworkInterfaceConfig}; -use tokio::net::{TcpListener, UdpSocket}; +use tokio::net::TcpListener; +#[cfg(fetaure = "quic")] +use tokio::net::UdpSocket; #[cfg(test)] mod protocol_dial_invalid_address; enum Transport { Tcp(TcpConfig), + #[cfg(feature = "quic")] Quic(QuicConfig), + #[cfg(feature = "websocket")] WebSocket(WebSocketConfig), } @@ -60,6 +66,7 @@ async fn two_litep2ps_work_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn two_litep2ps_work_quic() { two_litep2ps_work( @@ -69,6 +76,7 @@ async fn two_litep2ps_work_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn two_litep2ps_work_websocket() { two_litep2ps_work( @@ -96,7 +104,9 @@ async fn two_litep2ps_work(transport1: Transport, transport2: Transport) { let config1 = match transport1 { Transport::Tcp(config) => config1.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => config1.with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => config1.with_websocket(config), } .build(); @@ -108,7 +118,9 @@ async fn two_litep2ps_work(transport1: Transport, transport2: Transport) { let config2 = match transport2 { Transport::Tcp(config) => config2.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => config2.with_quic(config), + #[cfg(feature = "webscoekt")] Transport::WebSocket(config) => config2.with_websocket(config), } .build(); @@ -151,6 +163,7 @@ async fn dial_failure_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_failure_quic() { dial_failure( @@ -166,6 +179,7 @@ async fn dial_failure_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dial_failure_websocket() { dial_failure( @@ -199,7 +213,9 @@ async fn dial_failure(transport1: Transport, transport2: Transport, dial_address let config1 = match transport1 { Transport::Tcp(config) => config1.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => config1.with_quic(config), + #[cfg(feature = "webscocket")] Transport::WebSocket(config) => config1.with_websocket(config), } .build(); @@ -211,7 +227,9 @@ async fn dial_failure(transport1: Transport, transport2: Transport, dial_address let config2 = match transport2 { Transport::Tcp(config) => config2.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => config2.with_quic(config), + #[cfg(feature = "webscocket")] Transport::WebSocket(config) => config2.with_websocket(config), } .build(); @@ -316,6 +334,7 @@ async fn connection_timeout_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn connection_timeout_quic() { // create udp socket but don't respond to any inbound datagrams @@ -332,6 +351,7 @@ async fn connection_timeout_quic() { connection_timeout(Transport::Quic(Default::default()), address).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn connection_timeout_websocket() { // create tcp listener but don't accept any inbound connections @@ -367,7 +387,9 @@ async fn connection_timeout(transport: Transport, address: Multiaddr) { let litep2p_config = match transport { Transport::Tcp(config) => litep2p_config.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => litep2p_config.with_quic(config), + #[cfg(feature = "webscocket")] Transport::WebSocket(config) => litep2p_config.with_websocket(config), } .build(); @@ -389,6 +411,7 @@ async fn connection_timeout(transport: Transport, address: Multiaddr) { assert!(std::matches!(error, Error::Timeout)); } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_quic_peer_id_missing() { let _ = tracing_subscriber::fmt() @@ -427,11 +450,13 @@ async fn dial_self_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_self_quic() { dial_self(Transport::Quic(Default::default())).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dial_self_websocket() { dial_self(Transport::WebSocket(WebSocketConfig { @@ -453,7 +478,9 @@ async fn dial_self(transport: Transport) { let litep2p_config = match transport { Transport::Tcp(config) => litep2p_config.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => litep2p_config.with_quic(config), + #[cfg(feature = "webscocket")] Transport::WebSocket(config) => litep2p_config.with_websocket(config), } .build(); @@ -469,7 +496,36 @@ async fn dial_self(transport: Transport) { } #[tokio::test] -async fn attempt_to_dial_using_unsupported_transport() { +async fn attempt_to_dial_using_unsupported_transport_tcp() { + let _ = tracing_subscriber::fmt() + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) + .try_init(); + + let (ping_config, _ping_event_stream) = PingConfig::default(); + let config = ConfigBuilder::new() + .with_keypair(Keypair::generate()) + .with_tcp(Default::default()) + .with_libp2p_ping(ping_config) + .build(); + + let mut litep2p = Litep2p::new(config).unwrap(); + let address = Multiaddr::empty() + .with(Protocol::from(std::net::Ipv4Addr::new(127, 0, 0, 1))) + .with(Protocol::Tcp(8888)) + .with(Protocol::Ws(std::borrow::Cow::Borrowed("/"))) + .with(Protocol::P2p( + Multihash::from_bytes(&PeerId::random().to_bytes()).unwrap(), + )); + + assert!(std::matches!( + litep2p.dial_address(address.clone()).await, + Err(Error::TransportNotSupported(_)) + )); +} + +#[cfg(feature = "quic")] +#[tokio::test] +async fn attempt_to_dial_using_unsupported_transport_quic() { let _ = tracing_subscriber::fmt() .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) .try_init(); @@ -510,6 +566,7 @@ async fn keep_alive_timeout_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn keep_alive_timeout_quic() { keep_alive_timeout( @@ -519,6 +576,7 @@ async fn keep_alive_timeout_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn keep_alive_timeout_websocket() { keep_alive_timeout( @@ -546,7 +604,9 @@ async fn keep_alive_timeout(transport1: Transport, transport2: Transport) { let config1 = match transport1 { Transport::Tcp(config) => config1.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => config1.with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => config1.with_websocket(config), } .build(); @@ -559,7 +619,9 @@ async fn keep_alive_timeout(transport1: Transport, transport2: Transport) { let config2 = match transport2 { Transport::Tcp(config) => config2.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => config2.with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => config2.with_websocket(config), } .build(); @@ -654,6 +716,7 @@ async fn simultaneous_dial_tcp() { } } +#[cfg(feature = "quic")] #[tokio::test] async fn simultaneous_dial_quic() { let _ = tracing_subscriber::fmt() @@ -706,6 +769,7 @@ async fn simultaneous_dial_quic() { } } +#[cfg(feature = "quic")] #[tokio::test] async fn simultaneous_dial_ipv6_quic() { let _ = tracing_subscriber::fmt() @@ -758,6 +822,7 @@ async fn simultaneous_dial_ipv6_quic() { } } +#[cfg(feature = "webscocket")] #[tokio::test] async fn websocket_over_ipv6() { let _ = tracing_subscriber::fmt() @@ -871,6 +936,7 @@ async fn tcp_dns_resolution() { } } +#[cfg(feature = "websocket")] #[tokio::test] async fn websocket_dns_resolution() { let _ = tracing_subscriber::fmt() @@ -955,6 +1021,7 @@ async fn multiple_listen_addresses_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn multiple_listen_addresses_quic() { multiple_listen_addresses( @@ -977,6 +1044,7 @@ async fn multiple_listen_addresses_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn multiple_listen_addresses_websocket() { multiple_listen_addresses( @@ -1009,7 +1077,9 @@ async fn make_dummy_litep2p( let litep2p_config = match transport { Transport::Tcp(config) => litep2p_config.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => litep2p_config.with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => litep2p_config.with_websocket(config), } .build(); @@ -1081,6 +1151,7 @@ async fn port_in_use_tcp() { .unwrap(); } +#[cfg(feature = "websocket")] #[tokio::test] async fn port_in_use_websocket() { let _ = tracing_subscriber::fmt() @@ -1247,6 +1318,7 @@ async fn unspecified_listen_address_tcp() { } } +#[cfg(feature = "websocket")] #[tokio::test] async fn unspecified_listen_address_websocket() { let _ = tracing_subscriber::fmt() @@ -1368,6 +1440,7 @@ async fn simultaneous_dial_then_redial_tcp() { .await } +#[cfg(feature = "websocket")] #[tokio::test] async fn simultaneous_dial_then_redial_websocket() { simultaneous_dial_then_redial( @@ -1383,6 +1456,7 @@ async fn simultaneous_dial_then_redial_websocket() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn simultaneous_dial_then_redial_quic() { simultaneous_dial_then_redial( @@ -1404,7 +1478,9 @@ async fn simultaneous_dial_then_redial(transport1: Transport, transport2: Transp let config1 = match transport1 { Transport::Tcp(config) => config1.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => config1.with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => config1.with_websocket(config), } .build(); @@ -1416,7 +1492,9 @@ async fn simultaneous_dial_then_redial(transport1: Transport, transport2: Transp let config2 = match transport2 { Transport::Tcp(config) => config2.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => config2.with_quic(config), + #[cfg(feature = "webscocket")] Transport::WebSocket(config) => config2.with_websocket(config), } .build(); From 4b73f243342f0e912ee0e708c86984cd248222fc Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Fri, 2 Aug 2024 13:06:09 +0000 Subject: [PATCH 11/21] Make ping tests feature-aware --- tests/protocol/ping.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/protocol/ping.rs b/tests/protocol/ping.rs index 32beb41d..91835c3c 100644 --- a/tests/protocol/ping.rs +++ b/tests/protocol/ping.rs @@ -20,18 +20,20 @@ use futures::StreamExt; use litep2p::{ - config::ConfigBuilder, - protocol::libp2p::ping::ConfigBuilder as PingConfigBuilder, - transport::{ - quic::config::Config as QuicConfig, tcp::config::Config as TcpConfig, - websocket::config::Config as WebSocketConfig, - }, - Litep2p, + config::ConfigBuilder, protocol::libp2p::ping::ConfigBuilder as PingConfigBuilder, + transport::tcp::config::Config as TcpConfig, Litep2p, }; +#[cfg(feature = "quic")] +use litep2p::transport::quic::config::Config as QuicConfig; +#[cfg(feature = "websocket")] +use litep2p::transport::websocket::config::Config as WebSocketConfig; + enum Transport { Tcp(TcpConfig), + #[cfg(feature = "quic")] Quic(QuicConfig), + #[cfg(feature = "webscocket")] WebSocket(WebSocketConfig), } @@ -44,6 +46,7 @@ async fn ping_supported_tcp() { .await; } +#[cfg(feature = "webscocket")] #[tokio::test] async fn ping_supported_websocket() { ping_supported( @@ -53,6 +56,7 @@ async fn ping_supported_websocket() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn ping_supported_quic() { ping_supported( @@ -71,7 +75,9 @@ async fn ping_supported(transport1: Transport, transport2: Transport) { PingConfigBuilder::new().with_max_failure(3usize).build(); let config1 = match transport1 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "webscocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_libp2p_ping(ping_config1) @@ -80,7 +86,9 @@ async fn ping_supported(transport1: Transport, transport2: Transport) { let (ping_config2, mut ping_event_stream2) = PingConfigBuilder::new().build(); let config2 = match transport2 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "webscocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_libp2p_ping(ping_config2) From 0bdca0122d133610bc31eb6eda8186ac9d780322 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Fri, 2 Aug 2024 13:09:32 +0000 Subject: [PATCH 12/21] Make identify tests feature-aware --- tests/protocol/identify.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/tests/protocol/identify.rs b/tests/protocol/identify.rs index 07f29d15..9b64f056 100644 --- a/tests/protocol/identify.rs +++ b/tests/protocol/identify.rs @@ -26,16 +26,20 @@ use litep2p::{ identify::{Config, IdentifyEvent}, ping::Config as PingConfig, }, - transport::{ - quic::config::Config as QuicConfig, tcp::config::Config as TcpConfig, - websocket::config::Config as WebSocketConfig, - }, + transport::tcp::config::Config as TcpConfig, Litep2p, Litep2pEvent, }; +#[cfg(feature = "quic")] +use litep2p::transport::quic::config::Config as QuicConfig; +#[cfg(feature = "websocket")] +use litep2p::transport::websocket::config::Config as WebSocketConfig; + enum Transport { - Quic(QuicConfig), Tcp(TcpConfig), + #[cfg(feature = "quic")] + Quic(QuicConfig), + #[cfg(feature = "websocket")] WebSocket(WebSocketConfig), } @@ -48,6 +52,7 @@ async fn identify_supported_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn identify_supported_quic() { identify_supported( @@ -57,6 +62,7 @@ async fn identify_supported_quic() { .await } +#[cfg(feature = "websocket")] #[tokio::test] async fn identify_supported_websocket() { identify_supported( @@ -82,7 +88,9 @@ async fn identify_supported(transport1: Transport, transport2: Transport) { let config1 = match transport1 { Transport::Tcp(config) => config_builder.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => config_builder.with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => config_builder.with_websocket(config), } .build(); @@ -98,7 +106,9 @@ async fn identify_supported(transport1: Transport, transport2: Transport) { let config2 = match transport2 { Transport::Tcp(config) => config_builder.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => config_builder.with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => config_builder.with_websocket(config), } .build(); @@ -180,6 +190,7 @@ async fn identify_not_supported_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn identify_not_supported_quic() { identify_not_supported( @@ -189,6 +200,7 @@ async fn identify_not_supported_quic() { .await } +#[cfg(feature = "websocket")] #[tokio::test] async fn identify_not_supported_websocket() { identify_not_supported( @@ -206,7 +218,9 @@ async fn identify_not_supported(transport1: Transport, transport2: Transport) { let (ping_config, _event_stream) = PingConfig::default(); let config1 = match transport1 { Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_keypair(Keypair::generate()) @@ -221,7 +235,9 @@ async fn identify_not_supported(transport1: Transport, transport2: Transport) { let config2 = match transport2 { Transport::Tcp(config) => config_builder.with_tcp(config), + #[cfg(feature = "quic")] Transport::Quic(config) => config_builder.with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => config_builder.with_websocket(config), } .build(); From 5d27d73f07665c1dd8683eea49eddbbf4b2a5713 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Fri, 2 Aug 2024 13:45:53 +0000 Subject: [PATCH 13/21] Make notifications tests feature-aware --- tests/protocol/notification.rs | 416 ++++++++++----------------------- 1 file changed, 129 insertions(+), 287 deletions(-) diff --git a/tests/protocol/notification.rs b/tests/protocol/notification.rs index f40fc3f6..c5c203c3 100644 --- a/tests/protocol/notification.rs +++ b/tests/protocol/notification.rs @@ -26,31 +26,43 @@ use litep2p::{ Config as NotificationConfig, ConfigBuilder, Direction, NotificationError, NotificationEvent, NotificationHandle, ValidationResult, }, - transport::{ - quic::config::Config as QuicConfig, tcp::config::Config as TcpConfig, - websocket::config::Config as WebSocketConfig, - }, + transport::tcp::config::Config as TcpConfig, types::protocol::ProtocolName, Litep2p, Litep2pEvent, PeerId, }; +#[cfg(feature = "quic")] +use litep2p::transport::quic::config::Config as QuicConfig; +#[cfg(feature = "websocket")] +use litep2p::transport::websocket::config::Config as WebSocketConfig; + use bytes::BytesMut; use futures::StreamExt; use multiaddr::{Multiaddr, Protocol}; use multihash::Multihash; -use std::{ - net::{Ipv4Addr, Ipv6Addr}, - task::Poll, - time::Duration, -}; +#[cfg(any(feature = "quic", feature = "websocket"))] +use std::net::Ipv6Addr; +use std::{net::Ipv4Addr, task::Poll, time::Duration}; enum Transport { Tcp(TcpConfig), + #[cfg(feature = "quic")] Quic(QuicConfig), + #[cfg(feature = "websocket")] WebSocket(WebSocketConfig), } +fn add_transport(config: Litep2pConfigBuilder, transport: Transport) -> Litep2pConfigBuilder { + match transport { + Transport::Tcp(transport) => config.with_tcp(transport), + #[cfg(feature = "quic")] + Transport::Quic(transport) => config.with_quic(transport), + #[cfg(feature = "websocket")] + Transport::WebSocket(transport) => config.with_websocket(transport), + } +} + async fn connect_peers(litep2p1: &mut Litep2p, litep2p2: &mut Litep2p) { let address = litep2p2.listen_addresses().next().unwrap().clone(); litep2p1.dial_address(address).await.unwrap(); @@ -97,12 +109,7 @@ async fn make_default_litep2p(transport: Transport) -> (Litep2p, NotificationHan .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config); - let config = match transport { - Transport::Tcp(transport_config) => config.with_tcp(transport_config), - Transport::Quic(transport_config) => config.with_quic(transport_config), - Transport::WebSocket(transport_config) => config.with_websocket(transport_config), - } - .build(); + let config = add_transport(config, transport).build(); (Litep2p::new(config).unwrap(), handle) } @@ -122,6 +129,7 @@ async fn open_substreams_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn open_substreams_quic() { open_substreams( @@ -131,6 +139,7 @@ async fn open_substreams_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn open_substreams_websocket() { open_substreams( @@ -165,12 +174,7 @@ async fn open_substreams(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -186,12 +190,7 @@ async fn open_substreams(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -289,6 +288,7 @@ async fn reject_substream_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn reject_substream_quic() { reject_substream( @@ -298,6 +298,7 @@ async fn reject_substream_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn reject_substream_websocket() { reject_substream( @@ -332,12 +333,7 @@ async fn reject_substream(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -353,12 +349,7 @@ async fn reject_substream(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -414,6 +405,7 @@ async fn notification_stream_closed_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn notification_stream_closed_quic() { notification_stream_closed( @@ -423,6 +415,7 @@ async fn notification_stream_closed_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn notification_stream_closed_websocket() { notification_stream_closed( @@ -457,12 +450,7 @@ async fn notification_stream_closed(transport1: Transport, transport2: Transport .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -478,12 +466,7 @@ async fn notification_stream_closed(transport1: Transport, transport2: Transport .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -588,6 +571,7 @@ async fn reconnect_after_disconnect_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn reconnect_after_disconnect_quic() { reconnect_after_disconnect( @@ -597,6 +581,7 @@ async fn reconnect_after_disconnect_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn reconnect_after_disconnect_websocket() { reconnect_after_disconnect( @@ -631,12 +616,7 @@ async fn reconnect_after_disconnect(transport1: Transport, transport2: Transport .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -652,12 +632,7 @@ async fn reconnect_after_disconnect(transport1: Transport, transport2: Transport .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -819,6 +794,7 @@ async fn set_new_handshake_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn set_new_handshake_quic() { set_new_handshake( @@ -828,6 +804,7 @@ async fn set_new_handshake_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn set_new_handshake_websocket() { set_new_handshake( @@ -864,7 +841,9 @@ async fn set_new_handshake(transport1: Transport, transport2: Transport) { let config1 = match transport1 { Transport::Tcp(config) => config1.with_tcp(config), + #[cfg(feature = "websocket")] Transport::Quic(config) => config1.with_quic(config), + #[cfg(feature = "websocket")] Transport::WebSocket(config) => config1.with_websocket(config), } .build(); @@ -883,12 +862,7 @@ async fn set_new_handshake(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1035,6 +1009,7 @@ async fn both_nodes_open_substreams_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn both_nodes_open_substreams_quic() { both_nodes_open_substreams( @@ -1044,6 +1019,7 @@ async fn both_nodes_open_substreams_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn both_nodes_open_substreams_websocket() { both_nodes_open_substreams( @@ -1078,12 +1054,7 @@ async fn both_nodes_open_substreams(transport1: Transport, transport2: Transport .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -1099,12 +1070,7 @@ async fn both_nodes_open_substreams(transport1: Transport, transport2: Transport .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1207,6 +1173,7 @@ async fn both_nodes_open_substream_one_rejects_substreams_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] #[cfg(debug_assertions)] async fn both_nodes_open_substream_one_rejects_substreams_quic() { @@ -1217,6 +1184,7 @@ async fn both_nodes_open_substream_one_rejects_substreams_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] #[cfg(debug_assertions)] async fn both_nodes_open_substream_one_rejects_substreams_websocket() { @@ -1255,12 +1223,7 @@ async fn both_nodes_open_substream_one_rejects_substreams( .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -1276,12 +1239,7 @@ async fn both_nodes_open_substream_one_rejects_substreams( .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1348,11 +1306,13 @@ async fn send_sync_notification_to_non_existent_peer_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn send_sync_notification_to_non_existent_peer_quic() { send_sync_notification_to_non_existent_peer(Transport::Quic(Default::default())).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn send_sync_notification_to_non_existent_peer_websocket() { send_sync_notification_to_non_existent_peer(Transport::WebSocket(WebSocketConfig { @@ -1381,12 +1341,7 @@ async fn send_sync_notification_to_non_existent_peer(transport1: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); @@ -1410,11 +1365,13 @@ async fn send_async_notification_to_non_existent_peer_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn send_async_notification_to_non_existent_peer_quic() { send_async_notification_to_non_existent_peer(Transport::Quic(Default::default())).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn send_async_notification_to_non_existent_peer_websocket() { send_async_notification_to_non_existent_peer(Transport::WebSocket(WebSocketConfig { @@ -1443,12 +1400,7 @@ async fn send_async_notification_to_non_existent_peer(transport1: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); @@ -1475,11 +1427,13 @@ async fn try_to_connect_to_non_existent_peer_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn try_to_connect_to_non_existent_peer_quic() { try_to_connect_to_non_existent_peer(Transport::Quic(Default::default())).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn try_to_connect_to_non_existent_peer_websocket() { try_to_connect_to_non_existent_peer(Transport::WebSocket(WebSocketConfig { @@ -1508,12 +1462,7 @@ async fn try_to_connect_to_non_existent_peer(transport1: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); @@ -1545,11 +1494,13 @@ async fn try_to_disconnect_non_existent_peer_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn try_to_disconnect_non_existent_peer_quic() { try_to_disconnect_non_existent_peer(Transport::Quic(Default::default())).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn try_to_disconnect_non_existent_peer_websocket() { try_to_disconnect_non_existent_peer(Transport::WebSocket(WebSocketConfig { @@ -1578,12 +1529,7 @@ async fn try_to_disconnect_non_existent_peer(transport1: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); @@ -1613,6 +1559,7 @@ async fn try_to_reopen_substream_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn try_to_reopen_substream_quic() { try_to_reopen_substream( @@ -1622,6 +1569,7 @@ async fn try_to_reopen_substream_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn try_to_reopen_substream_websocket() { try_to_reopen_substream( @@ -1656,12 +1604,7 @@ async fn try_to_reopen_substream(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -1677,12 +1620,7 @@ async fn try_to_reopen_substream(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1769,6 +1707,7 @@ async fn substream_validation_timeout_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn substream_validation_timeout_quic() { substream_validation_timeout( @@ -1778,6 +1717,7 @@ async fn substream_validation_timeout_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn substream_validation_timeout_websocket() { substream_validation_timeout( @@ -1812,12 +1752,7 @@ async fn substream_validation_timeout(transport1: Transport, transport2: Transpo .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -1833,12 +1768,7 @@ async fn substream_validation_timeout(transport1: Transport, transport2: Transpo .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1894,6 +1824,7 @@ async fn unsupported_protocol_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn unsupported_protocol_quic() { unsupported_protocol( @@ -1903,6 +1834,7 @@ async fn unsupported_protocol_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn unsupported_protocol_websocket() { unsupported_protocol( @@ -1932,12 +1864,7 @@ async fn unsupported_protocol(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, _handle2) = ConfigBuilder::new(ProtocolName::from("/notif/2")) .with_max_size(1024usize) @@ -1947,12 +1874,7 @@ async fn unsupported_protocol(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1996,6 +1918,7 @@ async fn dialer_fallback_protocol_works_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn dialer_fallback_protocol_works_quic() { dialer_fallback_protocol_works( @@ -2005,6 +1928,7 @@ async fn dialer_fallback_protocol_works_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dialer_fallback_protocol_works_websocket() { dialer_fallback_protocol_works( @@ -2035,12 +1959,7 @@ async fn dialer_fallback_protocol_works(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/notif/1")) .with_max_size(1024usize) @@ -2050,12 +1969,7 @@ async fn dialer_fallback_protocol_works(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2134,6 +2048,7 @@ async fn listener_fallback_protocol_works_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn listener_fallback_protocol_works_quic() { listener_fallback_protocol_works( @@ -2143,6 +2058,7 @@ async fn listener_fallback_protocol_works_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn listener_fallback_protocol_works_websocket() { listener_fallback_protocol_works( @@ -2172,12 +2088,7 @@ async fn listener_fallback_protocol_works(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/notif/2")) .with_max_size(1024usize) @@ -2188,12 +2099,7 @@ async fn listener_fallback_protocol_works(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2272,6 +2178,7 @@ async fn enable_auto_accept_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn enable_auto_accept_quic() { enable_auto_accept( @@ -2281,6 +2188,7 @@ async fn enable_auto_accept_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn enable_auto_accept_websocket() { enable_auto_accept( @@ -2315,12 +2223,7 @@ async fn enable_auto_accept(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -2336,12 +2239,7 @@ async fn enable_auto_accept(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2428,6 +2326,7 @@ async fn send_using_notification_sink_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn send_using_notification_sink_quic() { send_using_notification_sink( @@ -2437,6 +2336,7 @@ async fn send_using_notification_sink_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn send_using_notification_sink_websocket() { send_using_notification_sink( @@ -2471,12 +2371,7 @@ async fn send_using_notification_sink(transport1: Transport, transport2: Transpo .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -2492,12 +2387,7 @@ async fn send_using_notification_sink(transport1: Transport, transport2: Transpo .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2609,6 +2499,7 @@ async fn dial_peer_when_opening_substream_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_peer_when_opening_substream_quic() { dial_peer_when_opening_substream( @@ -2618,6 +2509,7 @@ async fn dial_peer_when_opening_substream_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dial_peer_when_opening_substream_websocket() { dial_peer_when_opening_substream( @@ -2652,12 +2544,7 @@ async fn dial_peer_when_opening_substream(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config2, mut handle2) = NotificationConfig::new( ProtocolName::from("/notif/1"), @@ -2673,12 +2560,7 @@ async fn dial_peer_when_opening_substream(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2796,6 +2678,7 @@ async fn open_and_close_batched_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn open_and_close_batched_quic() { open_and_close_batched( @@ -2806,6 +2689,7 @@ async fn open_and_close_batched_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn open_and_close_batched_websocket() { open_and_close_batched( @@ -2999,6 +2883,7 @@ async fn open_and_close_batched_duplicate_peer_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn open_and_close_batched_duplicate_peer_quic() { open_and_close_batched_duplicate_peer( @@ -3009,6 +2894,7 @@ async fn open_and_close_batched_duplicate_peer_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn open_and_close_batched_duplicate_peer_websocket() { open_and_close_batched_duplicate_peer( @@ -3245,6 +3131,7 @@ async fn no_listener_address_for_one_peer_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn no_listener_address_for_one_peer_quic() { no_listener_address_for_one_peer( @@ -3254,6 +3141,7 @@ async fn no_listener_address_for_one_peer_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn no_listener_address_for_one_peer_websocket() { no_listener_address_for_one_peer( @@ -3365,6 +3253,7 @@ async fn auto_accept_inbound_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn auto_accept_inbound_quic() { auto_accept_inbound( @@ -3374,6 +3263,7 @@ async fn auto_accept_inbound_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn auto_accept_inbound_websocket() { auto_accept_inbound( @@ -3400,12 +3290,7 @@ async fn auto_accept_inbound(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (mut notif_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/notif/1")) .with_max_size(1024usize) @@ -3422,12 +3307,7 @@ async fn auto_accept_inbound(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -3507,6 +3387,7 @@ async fn dial_failure_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_failure_quic() { dial_failure( @@ -3516,6 +3397,7 @@ async fn dial_failure_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dial_failure_websocket() { dial_failure( @@ -3547,12 +3429,7 @@ async fn dial_failure(transport1: Transport, transport2: Transport) { .with_notification_protocol(notif_config1) .with_notification_protocol(notif_config2); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config3, _handle3) = ConfigBuilder::new(ProtocolName::from("/notif/1")) .with_max_size(1024usize) @@ -3568,24 +3445,21 @@ async fn dial_failure(transport1: Transport, transport2: Transport) { let known_address = match &transport2 { Transport::Tcp(_) => Multiaddr::empty() - .with(Protocol::Ip6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))) + .with(Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1))) .with(Protocol::Tcp(5)), + #[cfg(feature = "quic")] Transport::Quic(_) => Multiaddr::empty() - .with(Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1))) + .with(Protocol::Ip6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))) .with(Protocol::Udp(5)) .with(Protocol::QuicV1), + #[cfg(feature = "websocket")] Transport::WebSocket(_) => Multiaddr::empty() .with(Protocol::Ip6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))) .with(Protocol::Tcp(5)) .with(Protocol::Ws(std::borrow::Cow::Owned("/".to_string()))), }; - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -3630,6 +3504,7 @@ async fn dialing_disabled_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn dialing_disabled_quic() { dialing_disabled( @@ -3639,6 +3514,7 @@ async fn dialing_disabled_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dialing_disabled_websocket() { dialing_disabled( @@ -3672,12 +3548,7 @@ async fn dialing_disabled(transport1: Transport, transport2: Transport) { .with_notification_protocol(notif_config1) .with_notification_protocol(notif_config2); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config3, _handle3) = ConfigBuilder::new(ProtocolName::from("/notif/1")) .with_max_size(1024usize) @@ -3691,12 +3562,7 @@ async fn dialing_disabled(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config3); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -3741,6 +3607,7 @@ async fn validation_takes_too_long_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn validation_takes_too_long_quic() { validation_takes_too_long( @@ -3750,6 +3617,7 @@ async fn validation_takes_too_long_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn validation_takes_too_long_websocket() { validation_takes_too_long( @@ -3773,12 +3641,7 @@ async fn validation_takes_too_long(transport1: Transport, transport2: Transport) .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config3, mut handle2) = ConfigBuilder::new(ProtocolName::from("/notif/1")) .with_max_size(1024usize) @@ -3789,12 +3652,7 @@ async fn validation_takes_too_long(transport1: Transport, transport2: Transport) .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config3); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -3856,6 +3714,7 @@ async fn ignored_validation_open_substream_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn ignored_validation_open_substream_quic() { ignored_validation_open_substream( @@ -3865,6 +3724,7 @@ async fn ignored_validation_open_substream_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn ignored_validation_open_substream_websocket() { ignored_validation_open_substream( @@ -3888,12 +3748,7 @@ async fn ignored_validation_open_substream(transport1: Transport, transport2: Tr .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config3, mut handle2) = ConfigBuilder::new(ProtocolName::from("/notif/1")) .with_max_size(1024usize) @@ -3904,12 +3759,7 @@ async fn ignored_validation_open_substream(transport1: Transport, transport2: Tr .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config3); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -4007,6 +3857,7 @@ async fn clogged_channel_disconnects_peer_tcp() { .await } +#[cfg(feature = "quic")] #[tokio::test] async fn clogged_channel_disconnects_peer_quic() { clogged_channel_disconnects_peer( @@ -4016,6 +3867,7 @@ async fn clogged_channel_disconnects_peer_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn clogged_channel_disconnects_peer_websocket() { clogged_channel_disconnects_peer( @@ -4040,12 +3892,7 @@ async fn clogged_channel_disconnects_peer(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (notif_config3, mut handle2) = ConfigBuilder::new(ProtocolName::from("/notif/1")) .with_max_size(100 * 1024) @@ -4057,12 +3904,7 @@ async fn clogged_channel_disconnects_peer(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_notification_protocol(notif_config3); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); From 9102757f82b38c40dfacf74012f0e781173eb3de Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Fri, 2 Aug 2024 14:01:55 +0000 Subject: [PATCH 14/21] Make request-response tests feature-aware --- tests/protocol/notification.rs | 10 +- tests/protocol/request_response.rs | 430 +++++++++-------------------- 2 files changed, 130 insertions(+), 310 deletions(-) diff --git a/tests/protocol/notification.rs b/tests/protocol/notification.rs index c5c203c3..00cb6a0d 100644 --- a/tests/protocol/notification.rs +++ b/tests/protocol/notification.rs @@ -41,9 +41,9 @@ use futures::StreamExt; use multiaddr::{Multiaddr, Protocol}; use multihash::Multihash; -#[cfg(any(feature = "quic", feature = "websocket"))] -use std::net::Ipv6Addr; -use std::{net::Ipv4Addr, task::Poll, time::Duration}; +#[cfg(feature = "quic")] +use std::net::Ipv4Addr; +use std::{net::Ipv6Addr, task::Poll, time::Duration}; enum Transport { Tcp(TcpConfig), @@ -3445,11 +3445,11 @@ async fn dial_failure(transport1: Transport, transport2: Transport) { let known_address = match &transport2 { Transport::Tcp(_) => Multiaddr::empty() - .with(Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1))) + .with(Protocol::Ip6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))) .with(Protocol::Tcp(5)), #[cfg(feature = "quic")] Transport::Quic(_) => Multiaddr::empty() - .with(Protocol::Ip6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))) + .with(Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1))) .with(Protocol::Udp(5)) .with(Protocol::QuicV1), #[cfg(feature = "websocket")] diff --git a/tests/protocol/request_response.rs b/tests/protocol/request_response.rs index 4d15e42e..4d12bdbb 100644 --- a/tests/protocol/request_response.rs +++ b/tests/protocol/request_response.rs @@ -25,14 +25,16 @@ use litep2p::{ Config as RequestResponseConfig, ConfigBuilder, DialOptions, RequestResponseError, RequestResponseEvent, }, - transport::{ - quic::config::Config as QuicConfig, tcp::config::Config as TcpConfig, - websocket::config::Config as WebSocketConfig, - }, + transport::tcp::config::Config as TcpConfig, types::{protocol::ProtocolName, RequestId}, Litep2p, Litep2pEvent, PeerId, }; +#[cfg(feature = "quic")] +use litep2p::transport::quic::config::Config as QuicConfig; +#[cfg(feature = "websocket")] +use litep2p::transport::websocket::config::Config as WebSocketConfig; + use futures::{channel, StreamExt}; use multiaddr::{Multiaddr, Protocol}; use multihash::Multihash; @@ -40,19 +42,33 @@ use rand::{Rng, SeedableRng}; use rand_xorshift::XorShiftRng; use tokio::time::sleep; +#[cfg(feature = "quic")] +use std::net::Ipv4Addr; use std::{ collections::{HashMap, HashSet}, - net::{Ipv4Addr, Ipv6Addr}, + net::Ipv6Addr, task::Poll, time::Duration, }; enum Transport { Tcp(TcpConfig), + #[cfg(feature = "quic")] Quic(QuicConfig), + #[cfg(feature = "websocket")] WebSocket(WebSocketConfig), } +fn add_transport(config: Litep2pConfigBuilder, transport: Transport) -> Litep2pConfigBuilder { + match transport { + Transport::Tcp(transport) => config.with_tcp(transport), + #[cfg(feature = "quic")] + Transport::Quic(transport) => config.with_quic(transport), + #[cfg(feature = "websocket")] + Transport::WebSocket(transport) => config.with_websocket(transport), + } +} + async fn connect_peers(litep2p1: &mut Litep2p, litep2p2: &mut Litep2p) { let address = litep2p2.listen_addresses().next().unwrap().clone(); tracing::info!("address: {address}"); @@ -100,6 +116,7 @@ async fn send_request_receive_response_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn send_request_receive_response_quic() { send_request_receive_response( @@ -109,6 +126,7 @@ async fn send_request_receive_response_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn send_request_receive_response_websocket() { send_request_receive_response( @@ -140,12 +158,7 @@ async fn send_request_receive_response(transport1: Transport, transport2: Transp .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -158,12 +171,7 @@ async fn send_request_receive_response(transport1: Transport, transport2: Transp .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -225,6 +233,7 @@ async fn reject_request_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn reject_request_quic() { reject_request( @@ -234,6 +243,7 @@ async fn reject_request_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn reject_request_websocket() { reject_request( @@ -266,12 +276,7 @@ async fn reject_request(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -284,12 +289,7 @@ async fn reject_request(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -352,6 +352,7 @@ async fn multiple_simultaneous_requests_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn multiple_simultaneous_requests_quic() { multiple_simultaneous_requests( @@ -361,6 +362,7 @@ async fn multiple_simultaneous_requests_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn multiple_simultaneous_requests_websocket() { multiple_simultaneous_requests( @@ -393,12 +395,7 @@ async fn multiple_simultaneous_requests(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -411,12 +408,7 @@ async fn multiple_simultaneous_requests(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -516,6 +508,7 @@ async fn request_timeout_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn request_timeout_quic() { request_timeout( @@ -525,6 +518,7 @@ async fn request_timeout_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn request_timeout_websocket() { request_timeout( @@ -557,12 +551,7 @@ async fn request_timeout(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, _handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -575,12 +564,7 @@ async fn request_timeout(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -632,6 +616,7 @@ async fn protocol_not_supported_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn protocol_not_supported_quic() { protocol_not_supported( @@ -641,6 +626,7 @@ async fn protocol_not_supported_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn protocol_not_supported_websocket() { protocol_not_supported( @@ -673,12 +659,7 @@ async fn protocol_not_supported(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, _handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/2"), @@ -691,12 +672,7 @@ async fn protocol_not_supported(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -746,6 +722,7 @@ async fn connection_close_while_request_is_pending_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn connection_close_while_request_is_pending_quic() { connection_close_while_request_is_pending( @@ -755,6 +732,7 @@ async fn connection_close_while_request_is_pending_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn connection_close_while_request_is_pending_websocket() { connection_close_while_request_is_pending( @@ -786,12 +764,7 @@ async fn connection_close_while_request_is_pending(transport1: Transport, transp .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -804,12 +777,7 @@ async fn connection_close_while_request_is_pending(transport1: Transport, transp .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -859,6 +827,7 @@ async fn request_too_big_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn request_too_big_quic() { request_too_big( @@ -868,6 +837,7 @@ async fn request_too_big_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn request_too_big_websocket() { request_too_big( @@ -899,12 +869,7 @@ async fn request_too_big(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, _handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -917,12 +882,7 @@ async fn request_too_big(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -968,6 +928,7 @@ async fn response_too_big_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn response_too_big_quic() { response_too_big( @@ -977,6 +938,7 @@ async fn response_too_big_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn response_too_big_websocket() { response_too_big( @@ -1008,12 +970,7 @@ async fn response_too_big(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -1026,12 +983,7 @@ async fn response_too_big(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1206,6 +1158,7 @@ async fn dialer_fallback_protocol_works_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn dialer_fallback_protocol_works_quic() { dialer_fallback_protocol_works( @@ -1215,6 +1168,7 @@ async fn dialer_fallback_protocol_works_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dialer_fallback_protocol_works_websocket() { dialer_fallback_protocol_works( @@ -1245,12 +1199,7 @@ async fn dialer_fallback_protocol_works(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -1263,12 +1212,7 @@ async fn dialer_fallback_protocol_works(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1330,6 +1274,7 @@ async fn listener_fallback_protocol_works_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn listener_fallback_protocol_works_quic() { listener_fallback_protocol_works( @@ -1339,6 +1284,7 @@ async fn listener_fallback_protocol_works_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn listener_fallback_protocol_works_websocket() { listener_fallback_protocol_works( @@ -1370,12 +1316,7 @@ async fn listener_fallback_protocol_works(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1/improved"), @@ -1388,12 +1329,7 @@ async fn listener_fallback_protocol_works(transport1: Transport, transport2: Tra .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1455,6 +1391,7 @@ async fn dial_peer_when_sending_request_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_peer_when_sending_request_quic() { dial_peer_when_sending_request( @@ -1464,6 +1401,7 @@ async fn dial_peer_when_sending_request_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dial_peer_when_sending_request_websocket() { dial_peer_when_sending_request( @@ -1495,12 +1433,7 @@ async fn dial_peer_when_sending_request(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1/improved"), @@ -1513,12 +1446,7 @@ async fn dial_peer_when_sending_request(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1580,6 +1508,7 @@ async fn dial_peer_but_no_known_address_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_peer_but_no_known_address_quic() { dial_peer_but_no_known_address( @@ -1589,6 +1518,7 @@ async fn dial_peer_but_no_known_address_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dial_peer_but_no_known_address_websocket() { dial_peer_but_no_known_address( @@ -1620,12 +1550,7 @@ async fn dial_peer_but_no_known_address(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, _handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1/improved"), @@ -1638,12 +1563,7 @@ async fn dial_peer_but_no_known_address(transport1: Transport, transport2: Trans .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1687,6 +1607,7 @@ async fn cancel_request_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn cancel_request_quic() { cancel_request( @@ -1696,6 +1617,7 @@ async fn cancel_request_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn cancel_request_websocket() { cancel_request( @@ -1727,12 +1649,7 @@ async fn cancel_request(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/1"), @@ -1745,12 +1662,7 @@ async fn cancel_request(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1812,6 +1724,7 @@ async fn substream_open_failure_reported_once_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn substream_open_failure_reported_once_quic() { substream_open_failure_reported_once( @@ -1821,6 +1734,7 @@ async fn substream_open_failure_reported_once_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn substream_open_failure_reported_once_websocket() { substream_open_failure_reported_once( @@ -1852,12 +1766,7 @@ async fn substream_open_failure_reported_once(transport1: Transport, transport2: .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, _handle2) = RequestResponseConfig::new( ProtocolName::from("/protocol/2"), @@ -1870,12 +1779,7 @@ async fn substream_open_failure_reported_once(transport1: Transport, transport2: .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -1937,6 +1841,7 @@ async fn excess_inbound_request_rejected_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn excess_inbound_request_rejected_quic() { excess_inbound_request_rejected( @@ -1946,6 +1851,7 @@ async fn excess_inbound_request_rejected_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn excess_inbound_request_rejected_websocket() { excess_inbound_request_rejected( @@ -1974,12 +1880,7 @@ async fn excess_inbound_request_rejected(transport1: Transport, transport2: Tran .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, _handle2) = ConfigBuilder::new(ProtocolName::from("/protocol/1")) .with_max_size(1024) @@ -1990,12 +1891,7 @@ async fn excess_inbound_request_rejected(transport1: Transport, transport2: Tran .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2058,6 +1954,7 @@ async fn feedback_received_for_succesful_response_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn feedback_received_for_succesful_response_quic() { feedback_received_for_succesful_response( @@ -2067,6 +1964,7 @@ async fn feedback_received_for_succesful_response_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn feedback_received_for_succesful_response_websocket() { feedback_received_for_succesful_response( @@ -2095,12 +1993,7 @@ async fn feedback_received_for_succesful_response(transport1: Transport, transpo .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/protocol/1")) .with_max_size(1024) @@ -2110,12 +2003,7 @@ async fn feedback_received_for_succesful_response(transport1: Transport, transpo .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2179,6 +2067,7 @@ async fn feedback_received_for_succesful_response(transport1: Transport, transpo // .await; // } +#[cfg(feature = "quic")] #[tokio::test] async fn feedback_not_received_for_failed_response_quic() { feedback_not_received_for_failed_response( @@ -2203,6 +2092,7 @@ async fn feedback_not_received_for_failed_response_quic() { // .await; // } +#[cfg(feature = "quic")] async fn feedback_not_received_for_failed_response(transport1: Transport, transport2: Transport) { let _ = tracing_subscriber::fmt() .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) @@ -2216,12 +2106,7 @@ async fn feedback_not_received_for_failed_response(transport1: Transport, transp .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/protocol/1")) .with_max_size(1024) @@ -2231,12 +2116,7 @@ async fn feedback_not_received_for_failed_response(transport1: Transport, transp .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2289,6 +2169,7 @@ async fn custom_timeout_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn custom_timeout_quic() { custom_timeout( @@ -2298,6 +2179,7 @@ async fn custom_timeout_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn custom_timeout_websocket() { custom_timeout( @@ -2321,12 +2203,7 @@ async fn custom_timeout(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, _handle2) = ConfigBuilder::new(ProtocolName::from("/protocol/1")) .with_max_size(1024) @@ -2336,12 +2213,7 @@ async fn custom_timeout(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2383,11 +2255,13 @@ async fn outbound_request_for_unconnected_peer_tcp() { outbound_request_for_unconnected_peer(Transport::Tcp(Default::default())).await; } +#[cfg(feature = "quic")] #[tokio::test] async fn outbound_request_for_unconnected_peer_quic() { outbound_request_for_unconnected_peer(Transport::Quic(Default::default())).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn outbound_request_for_unconnected_peer_websocket() { outbound_request_for_unconnected_peer(Transport::WebSocket(Default::default())).await; @@ -2406,12 +2280,7 @@ async fn outbound_request_for_unconnected_peer(transport1: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); tokio::spawn(async move { let mut litep2p1 = Litep2p::new(config1).unwrap(); @@ -2440,11 +2309,13 @@ async fn dial_failure_tcp() { dial_failure(Transport::Tcp(Default::default())).await; } +#[cfg(feature = "quic")] #[tokio::test] async fn dial_failure_quic() { dial_failure(Transport::Quic(Default::default())).await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn dial_failure_websocket() { dial_failure(Transport::WebSocket(Default::default())).await; @@ -2469,11 +2340,13 @@ async fn dial_failure(transport: Transport) { .with(Protocol::Ip6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))) .with(Protocol::Tcp(5)) .with(Protocol::P2p(Multihash::from(peer))), + #[cfg(feature = "quic")] Transport::Quic(_) => Multiaddr::empty() .with(Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1))) .with(Protocol::Udp(5)) .with(Protocol::QuicV1) .with(Protocol::P2p(Multihash::from(peer))), + #[cfg(feature = "websocket")] Transport::WebSocket(_) => Multiaddr::empty() .with(Protocol::Ip6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))) .with(Protocol::Tcp(5)) @@ -2481,12 +2354,7 @@ async fn dial_failure(transport: Transport) { .with(Protocol::P2p(Multihash::from(peer))), }; - let config = match transport { - Transport::Tcp(config) => litep2p_config.with_tcp(config), - Transport::Quic(config) => litep2p_config.with_quic(config), - Transport::WebSocket(config) => litep2p_config.with_websocket(config), - } - .build(); + let config = add_transport(litep2p_config, transport).build(); let mut litep2p = Litep2p::new(config).unwrap(); litep2p.add_known_address(peer, vec![known_address].into_iter()); @@ -2514,6 +2382,7 @@ async fn large_response_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn large_response_quic() { large_response( @@ -2523,6 +2392,7 @@ async fn large_response_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn large_response_websocket() { large_response( @@ -2546,12 +2416,7 @@ async fn large_response(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/protocol/1")) .with_max_size(16 * 1024 * 1024) @@ -2561,12 +2426,7 @@ async fn large_response(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2624,6 +2484,7 @@ async fn binary_incompatible_fallback_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn binary_incompatible_fallback_quic() { binary_incompatible_fallback( @@ -2633,6 +2494,7 @@ async fn binary_incompatible_fallback_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn binary_incompatible_fallback_websocket() { binary_incompatible_fallback( @@ -2657,12 +2519,7 @@ async fn binary_incompatible_fallback(transport1: Transport, transport2: Transpo .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/protocol/1")) .with_max_size(16 * 1024 * 1024) @@ -2672,12 +2529,7 @@ async fn binary_incompatible_fallback(transport1: Transport, transport2: Transpo .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2737,6 +2589,7 @@ async fn binary_incompatible_fallback_inbound_request_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn binary_incompatible_fallback_inbound_request_quic() { binary_incompatible_fallback_inbound_request( @@ -2746,6 +2599,7 @@ async fn binary_incompatible_fallback_inbound_request_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn binary_incompatible_fallback_inbound_request_websocket() { binary_incompatible_fallback_inbound_request( @@ -2773,12 +2627,7 @@ async fn binary_incompatible_fallback_inbound_request( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/protocol/1")) .with_max_size(16 * 1024 * 1024) @@ -2788,12 +2637,7 @@ async fn binary_incompatible_fallback_inbound_request( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2848,6 +2692,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn binary_incompatible_fallback_two_fallback_protocols_quic() { binary_incompatible_fallback_two_fallback_protocols( @@ -2857,6 +2702,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn binary_incompatible_fallback_two_fallback_protocols_websocket() { binary_incompatible_fallback_two_fallback_protocols( @@ -2888,12 +2734,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/genesis/protocol/1")) @@ -2905,12 +2746,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -2970,6 +2806,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols_inbound_request_tcp .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn binary_incompatible_fallback_two_fallback_protocols_inbound_request_quic() { binary_incompatible_fallback_two_fallback_protocols_inbound_request( @@ -2979,6 +2816,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols_inbound_request_qui .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn binary_incompatible_fallback_two_fallback_protocols_inbound_request_websocket() { binary_incompatible_fallback_two_fallback_protocols_inbound_request( @@ -3010,12 +2848,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols_inbound_request( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/genesis/protocol/1")) @@ -3027,12 +2860,7 @@ async fn binary_incompatible_fallback_two_fallback_protocols_inbound_request( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -3087,6 +2915,7 @@ async fn binary_incompatible_fallback_compatible_nodes_tcp() { .await; } +#[cfg(feature = "quic")] #[tokio::test] async fn binary_incompatible_fallback_compatible_nodes_quic() { binary_incompatible_fallback_compatible_nodes( @@ -3096,6 +2925,7 @@ async fn binary_incompatible_fallback_compatible_nodes_quic() { .await; } +#[cfg(feature = "websocket")] #[tokio::test] async fn binary_incompatible_fallback_compatible_nodes_websocket() { binary_incompatible_fallback_compatible_nodes( @@ -3127,12 +2957,7 @@ async fn binary_incompatible_fallback_compatible_nodes( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - Transport::Quic(config) => config1.with_quic(config), - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (req_resp_config2, mut handle2) = ConfigBuilder::new(ProtocolName::from("/genesis/protocol/2")) @@ -3148,12 +2973,7 @@ async fn binary_incompatible_fallback_compatible_nodes( .with_keypair(Keypair::generate()) .with_request_response_protocol(req_resp_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - Transport::Quic(config) => config2.with_quic(config), - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); From 44859715c59688bf0b642756e498a51bf3bd5ce0 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Fri, 2 Aug 2024 14:21:36 +0000 Subject: [PATCH 15/21] Factor out transport building --- tests/connection/mod.rs | 102 ++++++++----------------------------- tests/protocol/identify.rs | 60 ++++++++-------------- tests/protocol/ping.rs | 34 ++++++------- 3 files changed, 55 insertions(+), 141 deletions(-) diff --git a/tests/connection/mod.rs b/tests/connection/mod.rs index e95506bb..d6b044be 100644 --- a/tests/connection/mod.rs +++ b/tests/connection/mod.rs @@ -51,6 +51,16 @@ enum Transport { WebSocket(WebSocketConfig), } +fn add_transport(config: ConfigBuilder, transport: Transport) -> ConfigBuilder { + match transport { + Transport::Tcp(transport) => config.with_tcp(transport), + #[cfg(feature = "quic")] + Transport::Quic(transport) => config.with_quic(transport), + #[cfg(feature = "websocket")] + Transport::WebSocket(transport) => config.with_websocket(transport), + } +} + #[tokio::test] async fn two_litep2ps_work_tcp() { two_litep2ps_work( @@ -102,28 +112,14 @@ async fn two_litep2ps_work(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => config1.with_quic(config), - #[cfg(feature = "websocket")] - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (ping_config2, _ping_event_stream2) = PingConfig::default(); let config2 = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => config2.with_quic(config), - #[cfg(feature = "webscoekt")] - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -211,28 +207,14 @@ async fn dial_failure(transport1: Transport, transport2: Transport, dial_address .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => config1.with_quic(config), - #[cfg(feature = "webscocket")] - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (ping_config2, _ping_event_stream2) = PingConfig::default(); let config2 = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => config2.with_quic(config), - #[cfg(feature = "webscocket")] - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -384,15 +366,7 @@ async fn connection_timeout(transport: Transport, address: Multiaddr) { let litep2p_config = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config); - - let litep2p_config = match transport { - Transport::Tcp(config) => litep2p_config.with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => litep2p_config.with_quic(config), - #[cfg(feature = "webscocket")] - Transport::WebSocket(config) => litep2p_config.with_websocket(config), - } - .build(); + let litep2p_config = add_transport(litep2p_config, transport).build(); let mut litep2p = Litep2p::new(litep2p_config).unwrap(); @@ -475,15 +449,7 @@ async fn dial_self(transport: Transport) { let litep2p_config = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config); - - let litep2p_config = match transport { - Transport::Tcp(config) => litep2p_config.with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => litep2p_config.with_quic(config), - #[cfg(feature = "webscocket")] - Transport::WebSocket(config) => litep2p_config.with_websocket(config), - } - .build(); + let litep2p_config = add_transport(litep2p_config, transport).build(); let mut litep2p = Litep2p::new(litep2p_config).unwrap(); let address = litep2p.listen_addresses().next().unwrap().clone(); @@ -602,14 +568,7 @@ async fn keep_alive_timeout(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => config1.with_quic(config), - #[cfg(feature = "websocket")] - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let (ping_config2, mut ping_event_stream2) = PingConfig::default(); @@ -617,14 +576,7 @@ async fn keep_alive_timeout(transport1: Transport, transport2: Transport) { .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => config2.with_quic(config), - #[cfg(feature = "websocket")] - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p2 = Litep2p::new(config2).unwrap(); let address1 = litep2p1.listen_addresses().next().unwrap().clone(); @@ -1476,28 +1428,14 @@ async fn simultaneous_dial_then_redial(transport1: Transport, transport2: Transp .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config1); - let config1 = match transport1 { - Transport::Tcp(config) => config1.with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => config1.with_quic(config), - #[cfg(feature = "websocket")] - Transport::WebSocket(config) => config1.with_websocket(config), - } - .build(); + let config1 = add_transport(config1, transport1).build(); let (ping_config2, _ping_event_stream2) = PingConfig::default(); let config2 = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_ping(ping_config2); - let config2 = match transport2 { - Transport::Tcp(config) => config2.with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => config2.with_quic(config), - #[cfg(feature = "webscocket")] - Transport::WebSocket(config) => config2.with_websocket(config), - } - .build(); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); diff --git a/tests/protocol/identify.rs b/tests/protocol/identify.rs index 9b64f056..e8a2f1d7 100644 --- a/tests/protocol/identify.rs +++ b/tests/protocol/identify.rs @@ -43,6 +43,16 @@ enum Transport { WebSocket(WebSocketConfig), } +fn add_transport(config: ConfigBuilder, transport: Transport) -> ConfigBuilder { + match transport { + Transport::Tcp(transport) => config.with_tcp(transport), + #[cfg(feature = "quic")] + Transport::Quic(transport) => config.with_quic(transport), + #[cfg(feature = "websocket")] + Transport::WebSocket(transport) => config.with_websocket(transport), + } +} + #[tokio::test] async fn identify_supported_tcp() { identify_supported( @@ -82,36 +92,20 @@ async fn identify_supported(transport1: Transport, transport2: Transport) { Some("agent v1".to_string()), Vec::new(), ); - let config_builder = ConfigBuilder::new() + let config_builder1 = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_identify(identify_config1); - - let config1 = match transport1 { - Transport::Tcp(config) => config_builder.with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => config_builder.with_quic(config), - #[cfg(feature = "websocket")] - Transport::WebSocket(config) => config_builder.with_websocket(config), - } - .build(); + let config1 = add_transport(config_builder1, transport1).build(); let (identify_config2, mut identify_event_stream2) = Config::new( "/proto/2".to_string(), Some("agent v2".to_string()), Vec::new(), ); - let config_builder = ConfigBuilder::new() + let config_builder2 = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_identify(identify_config2); - - let config2 = match transport2 { - Transport::Tcp(config) => config_builder.with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => config_builder.with_quic(config), - #[cfg(feature = "websocket")] - Transport::WebSocket(config) => config_builder.with_websocket(config), - } - .build(); + let config2 = add_transport(config_builder2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); @@ -216,31 +210,17 @@ async fn identify_not_supported(transport1: Transport, transport2: Transport) { .try_init(); let (ping_config, _event_stream) = PingConfig::default(); - let config1 = match transport1 { - Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => ConfigBuilder::new().with_quic(config), - #[cfg(feature = "websocket")] - Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), - } - .with_keypair(Keypair::generate()) - .with_libp2p_ping(ping_config) - .build(); + let config_builder1 = ConfigBuilder::new() + .with_keypair(Keypair::generate()) + .with_libp2p_ping(ping_config); + let config1 = add_transport(config_builder1, transport1).build(); let (identify_config2, mut identify_event_stream2) = Config::new("litep2p".to_string(), None, Vec::new()); - let config_builder = ConfigBuilder::new() + let config_builder2 = ConfigBuilder::new() .with_keypair(Keypair::generate()) .with_libp2p_identify(identify_config2); - - let config2 = match transport2 { - Transport::Tcp(config) => config_builder.with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => config_builder.with_quic(config), - #[cfg(feature = "websocket")] - Transport::WebSocket(config) => config_builder.with_websocket(config), - } - .build(); + let config2 = add_transport(config_builder2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); diff --git a/tests/protocol/ping.rs b/tests/protocol/ping.rs index 91835c3c..c481ed6d 100644 --- a/tests/protocol/ping.rs +++ b/tests/protocol/ping.rs @@ -33,10 +33,20 @@ enum Transport { Tcp(TcpConfig), #[cfg(feature = "quic")] Quic(QuicConfig), - #[cfg(feature = "webscocket")] + #[cfg(feature = "websocket")] WebSocket(WebSocketConfig), } +fn add_transport(config: ConfigBuilder, transport: Transport) -> ConfigBuilder { + match transport { + Transport::Tcp(transport) => config.with_tcp(transport), + #[cfg(feature = "quic")] + Transport::Quic(transport) => config.with_quic(transport), + #[cfg(feature = "websocket")] + Transport::WebSocket(transport) => config.with_websocket(transport), + } +} + #[tokio::test] async fn ping_supported_tcp() { ping_supported( @@ -73,26 +83,12 @@ async fn ping_supported(transport1: Transport, transport2: Transport) { let (ping_config1, mut ping_event_stream1) = PingConfigBuilder::new().with_max_failure(3usize).build(); - let config1 = match transport1 { - Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => ConfigBuilder::new().with_quic(config), - #[cfg(feature = "webscocket")] - Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), - } - .with_libp2p_ping(ping_config1) - .build(); + let config1 = ConfigBuilder::new().with_libp2p_ping(ping_config1); + let config1 = add_transport(config1, transport1).build(); let (ping_config2, mut ping_event_stream2) = PingConfigBuilder::new().build(); - let config2 = match transport2 { - Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), - #[cfg(feature = "quic")] - Transport::Quic(config) => ConfigBuilder::new().with_quic(config), - #[cfg(feature = "webscocket")] - Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), - } - .with_libp2p_ping(ping_config2) - .build(); + let config2 = ConfigBuilder::new().with_libp2p_ping(ping_config2); + let config2 = add_transport(config2, transport2).build(); let mut litep2p1 = Litep2p::new(config1).unwrap(); let mut litep2p2 = Litep2p::new(config2).unwrap(); From 5cb9c4fb2bf143cf324953c2d24a905daec8ef94 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Fri, 2 Aug 2024 14:49:24 +0000 Subject: [PATCH 16/21] Cleanup compilation warnings --- src/crypto/mod.rs | 4 ++-- src/crypto/noise/mod.rs | 2 ++ src/crypto/noise/protocol.rs | 12 +----------- src/lib.rs | 6 ++++-- src/protocol/protocol_set.rs | 9 +++++---- src/transport/manager/handle.rs | 2 +- src/transport/manager/mod.rs | 2 +- src/transport/tcp/mod.rs | 8 ++++---- 8 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index 4ddaeada..b565639a 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -95,8 +95,8 @@ impl TryFrom for PublicKey { type Error = Error; fn try_from(pubkey: keys_proto::PublicKey) -> Result { - let key_type = keys_proto::KeyType::from_i32(pubkey.r#type) - .ok_or_else(|| Error::Other(format!("Unknown key type: {}", pubkey.r#type)))?; + let key_type = keys_proto::KeyType::try_from(pubkey.r#type) + .map_err(|_| Error::Other(format!("Unknown key type: {}", pubkey.r#type)))?; match key_type { keys_proto::KeyType::Ed25519 => diff --git a/src/crypto/noise/mod.rs b/src/crypto/noise/mod.rs index 1a877da7..f130042c 100644 --- a/src/crypto/noise/mod.rs +++ b/src/crypto/noise/mod.rs @@ -143,6 +143,7 @@ impl NoiseContext { } /// Create new [`NoiseContext`] with prologue. + #[cfg(feature = "webrtc")] pub fn with_prologue(id_keys: &Keypair, prologue: Vec) -> crate::Result { let noise: Builder<'_> = Builder::with_resolver( NOISE_PARAMETERS.parse().expect("qed; Valid noise pattern"), @@ -160,6 +161,7 @@ impl NoiseContext { } /// Get remote public key from the received Noise payload. + #[cfg(feature = "webrtc")] pub fn get_remote_public_key(&mut self, reply: &[u8]) -> crate::Result { let (len_slice, reply) = reply.split_at(2); let len = u16::from_be_bytes(len_slice.try_into().map_err(|_| error::Error::InvalidData)?) diff --git a/src/crypto/noise/protocol.rs b/src/crypto/noise/protocol.rs index 38924880..ce745f35 100644 --- a/src/crypto/noise/protocol.rs +++ b/src/crypto/noise/protocol.rs @@ -18,7 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::crypto::{self, noise::x25519_spec}; +use crate::crypto::noise::x25519_spec; use rand::SeedableRng; use zeroize::Zeroize; @@ -30,16 +30,6 @@ pub struct Keypair { pub public: PublicKey, } -/// The associated public identity of a DH keypair. -#[derive(Clone)] -pub struct KeypairIdentity { - /// The public identity key. - pub public: crypto::PublicKey, - - /// The signature over the public DH key. - pub signature: Option>, -} - /// DH secret key. #[derive(Clone)] pub struct SecretKey(pub T); diff --git a/src/lib.rs b/src/lib.rs index 4c0608dc..c63e465e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,7 @@ use crate::{ }; #[cfg(feature = "quic")] -use crate::transport::quick::QuicTransport; +use crate::transport::quic::QuicTransport; #[cfg(feature = "webrtc")] use crate::transport::webrtc::WebRtcTransport; #[cfg(feature = "websocket")] @@ -76,9 +76,11 @@ pub mod types; pub mod yamux; mod bandwidth; -mod mock; mod multistream_select; +#[cfg(test)] +mod mock; + /// Public result type used by the crate. pub type Result = std::result::Result; diff --git a/src/protocol/protocol_set.rs b/src/protocol/protocol_set.rs index f2328ebe..a0458146 100644 --- a/src/protocol/protocol_set.rs +++ b/src/protocol/protocol_set.rs @@ -38,14 +38,13 @@ use futures::{stream::FuturesUnordered, Stream, StreamExt}; use multiaddr::Multiaddr; use tokio::sync::mpsc::{channel, Receiver, Sender}; +#[cfg(any(feature = "quic", feature = "webrtc", feature = "websocket"))] +use std::sync::atomic::Ordering; use std::{ collections::HashMap, fmt::Debug, pin::Pin, - sync::{ - atomic::{AtomicUsize, Ordering}, - Arc, - }, + sync::{atomic::AtomicUsize, Arc}, task::{Context, Poll}, }; @@ -213,6 +212,7 @@ pub struct ProtocolSet { mgr_tx: Sender, connection: ConnectionHandle, rx: Receiver, + #[allow(unused)] next_substream_id: Arc, fallback_names: HashMap, } @@ -253,6 +253,7 @@ impl ProtocolSet { } /// Get next substream ID. + #[cfg(any(feature = "quic", feature = "webrtc", feature = "websocket"))] pub fn next_substream_id(&self) -> SubstreamId { SubstreamId::from(self.next_substream_id.fetch_add(1usize, Ordering::Relaxed)) } diff --git a/src/transport/manager/handle.rs b/src/transport/manager/handle.rs index 29fd8ab2..31df6c36 100644 --- a/src/transport/manager/handle.rs +++ b/src/transport/manager/handle.rs @@ -287,7 +287,7 @@ pub struct TransportHandle { pub protocols: HashMap, pub next_connection_id: Arc, pub next_substream_id: Arc, - pub protocol_names: Vec, + pub _protocol_names: Vec, pub bandwidth_sink: BandwidthSink, pub executor: Arc, } diff --git a/src/transport/manager/mod.rs b/src/transport/manager/mod.rs index d55934c7..d3ebddc8 100644 --- a/src/transport/manager/mod.rs +++ b/src/transport/manager/mod.rs @@ -357,7 +357,7 @@ impl TransportManager { keypair: self.keypair.clone(), protocols: self.protocols.clone(), bandwidth_sink: self.bandwidth_sink.clone(), - protocol_names: self.protocol_names.iter().cloned().collect(), + _protocol_names: self.protocol_names.iter().cloned().collect(), next_substream_id: self.next_substream_id.clone(), next_connection_id: self.next_connection_id.clone(), } diff --git a/src/transport/tcp/mod.rs b/src/transport/tcp/mod.rs index 62cc00d9..066e3f36 100644 --- a/src/transport/tcp/mod.rs +++ b/src/transport/tcp/mod.rs @@ -514,7 +514,7 @@ mod tests { let handle1 = crate::transport::manager::TransportHandle { executor: Arc::new(DefaultExecutor {}), - protocol_names: Vec::new(), + _protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair1.clone(), @@ -545,7 +545,7 @@ mod tests { let handle2 = crate::transport::manager::TransportHandle { executor: Arc::new(DefaultExecutor {}), - protocol_names: Vec::new(), + _protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair2.clone(), @@ -594,7 +594,7 @@ mod tests { let handle1 = crate::transport::manager::TransportHandle { executor: Arc::new(DefaultExecutor {}), - protocol_names: Vec::new(), + _protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair1.clone(), @@ -630,7 +630,7 @@ mod tests { let handle2 = crate::transport::manager::TransportHandle { executor: Arc::new(DefaultExecutor {}), - protocol_names: Vec::new(), + _protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair2.clone(), From ecc9ab69beb18d847aabfa0ba369b710aed0e346 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Fri, 2 Aug 2024 15:07:25 +0000 Subject: [PATCH 17/21] Fix tests compilation --- src/transport/quic/mod.rs | 4 ++-- tests/connection/mod.rs | 2 +- tests/protocol/notification.rs | 2 +- tests/protocol/ping.rs | 2 +- tests/substream.rs | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/transport/quic/mod.rs b/src/transport/quic/mod.rs index 5972993f..eab11396 100644 --- a/src/transport/quic/mod.rs +++ b/src/transport/quic/mod.rs @@ -495,7 +495,7 @@ mod tests { let handle1 = TransportHandle { executor: Arc::new(DefaultExecutor {}), - protocol_names: Vec::new(), + _protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair1.clone(), @@ -522,7 +522,7 @@ mod tests { let handle2 = TransportHandle { executor: Arc::new(DefaultExecutor {}), - protocol_names: Vec::new(), + _protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair2.clone(), diff --git a/tests/connection/mod.rs b/tests/connection/mod.rs index d6b044be..89be67e4 100644 --- a/tests/connection/mod.rs +++ b/tests/connection/mod.rs @@ -37,7 +37,7 @@ use multiaddr::{Multiaddr, Protocol}; use multihash::Multihash; use network_interface::{NetworkInterface, NetworkInterfaceConfig}; use tokio::net::TcpListener; -#[cfg(fetaure = "quic")] +#[cfg(feature = "quic")] use tokio::net::UdpSocket; #[cfg(test)] diff --git a/tests/protocol/notification.rs b/tests/protocol/notification.rs index 00cb6a0d..8227cbe9 100644 --- a/tests/protocol/notification.rs +++ b/tests/protocol/notification.rs @@ -841,7 +841,7 @@ async fn set_new_handshake(transport1: Transport, transport2: Transport) { let config1 = match transport1 { Transport::Tcp(config) => config1.with_tcp(config), - #[cfg(feature = "websocket")] + #[cfg(feature = "quic")] Transport::Quic(config) => config1.with_quic(config), #[cfg(feature = "websocket")] Transport::WebSocket(config) => config1.with_websocket(config), diff --git a/tests/protocol/ping.rs b/tests/protocol/ping.rs index c481ed6d..cc862a58 100644 --- a/tests/protocol/ping.rs +++ b/tests/protocol/ping.rs @@ -56,7 +56,7 @@ async fn ping_supported_tcp() { .await; } -#[cfg(feature = "webscocket")] +#[cfg(feature = "websocket")] #[tokio::test] async fn ping_supported_websocket() { ping_supported( diff --git a/tests/substream.rs b/tests/substream.rs index 61472ea1..efcf5011 100644 --- a/tests/substream.rs +++ b/tests/substream.rs @@ -289,7 +289,7 @@ async fn too_big_identity_payload_framed(transport1: Transport, transport2: Tran Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), - #[cfg(feature = "wesocket")] + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol2)) @@ -380,7 +380,7 @@ async fn too_big_identity_payload_sink(transport1: Transport, transport2: Transp Transport::Tcp(config) => ConfigBuilder::new().with_tcp(config), #[cfg(feature = "quic")] Transport::Quic(config) => ConfigBuilder::new().with_quic(config), - #[cfg(feature = "webscocket")] + #[cfg(feature = "websocket")] Transport::WebSocket(config) => ConfigBuilder::new().with_websocket(config), } .with_user_protocol(Box::new(custom_protocol2)) From 09c7ae2c45dc356aac2dd3f315caff4e1a8fe495 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Fri, 2 Aug 2024 15:23:51 +0000 Subject: [PATCH 18/21] Fix some clippy warnings --- src/multistream_select/length_delimited.rs | 1 - src/protocol/notification/mod.rs | 4 ++-- src/transport/common/listener.rs | 2 +- src/yamux/connection/stream.rs | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/multistream_select/length_delimited.rs b/src/multistream_select/length_delimited.rs index 9d5d3ce3..7052d629 100644 --- a/src/multistream_select/length_delimited.rs +++ b/src/multistream_select/length_delimited.rs @@ -25,7 +25,6 @@ use std::{ io, pin::Pin, task::{Context, Poll}, - u16, }; const MAX_LEN_BYTES: u16 = 2; diff --git a/src/protocol/notification/mod.rs b/src/protocol/notification/mod.rs index 867e75a6..eefe5904 100644 --- a/src/protocol/notification/mod.rs +++ b/src/protocol/notification/mod.rs @@ -531,7 +531,7 @@ impl NotificationProtocol { let Some(context) = self.peers.get_mut(&peer) else { tracing::error!(target: LOG_TARGET, ?peer, "peer doesn't exist for outbound substream"); debug_assert!(false); - return Err(Error::PeerDoesntExist(peer.clone())); + return Err(Error::PeerDoesntExist(peer)); }; let pending_peer = self.pending_outbound.remove(&substream_id); @@ -661,7 +661,7 @@ impl NotificationProtocol { let Some(context) = self.peers.get_mut(&peer) else { tracing::error!(target: LOG_TARGET, ?peer, "peer doesn't exist for inbound substream"); debug_assert!(false); - return Err(Error::PeerDoesntExist(peer.clone())); + return Err(Error::PeerDoesntExist(peer)); }; tracing::debug!( diff --git a/src/transport/common/listener.rs b/src/transport/common/listener.rs index 275d7beb..d9afe729 100644 --- a/src/transport/common/listener.rs +++ b/src/transport/common/listener.rs @@ -370,7 +370,7 @@ fn multiaddr_to_socket_address( ?protocol, "invalid transport protocol, expected `Tcp`", ); - return Err(Error::AddressError(AddressError::InvalidProtocol)); + Err(Error::AddressError(AddressError::InvalidProtocol)) } }; diff --git a/src/yamux/connection/stream.rs b/src/yamux/connection/stream.rs index 03e3fa39..d6762bb2 100644 --- a/src/yamux/connection/stream.rs +++ b/src/yamux/connection/stream.rs @@ -494,7 +494,7 @@ impl Shared { WindowUpdateMode::OnRead => { debug_assert!(self.config.receive_window >= self.window); let bytes_received = self.config.receive_window.saturating_sub(self.window); - let buffer_len: u32 = self.buffer.len().try_into().unwrap_or(std::u32::MAX); + let buffer_len: u32 = self.buffer.len().try_into().unwrap_or(u32::MAX); bytes_received.saturating_sub(buffer_len) } From 99f2c3906b5241c49fdfe10e7ba893e29ef4ef3f Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Fri, 2 Aug 2024 15:25:16 +0000 Subject: [PATCH 19/21] Run CI tests with `--all-features` --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 56189d91..97deb0fb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -89,7 +89,7 @@ jobs: cache-all-crates: true - name: Cargo check - run: cargo check + run: cargo check --all-features doc: name: Check documentation @@ -126,7 +126,7 @@ jobs: cache-all-crates: true - name: Run clippy - run: cargo clippy + run: cargo clippy --all-features test: name: Test From eb46aa6a8d50f6a2123be8fd79271594858a34d6 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Tue, 6 Aug 2024 14:35:10 +0000 Subject: [PATCH 20/21] Remove unused `_protocol_names` from `TransportManagerHandle` --- src/transport/manager/handle.rs | 1 - src/transport/manager/mod.rs | 1 - src/transport/quic/mod.rs | 2 -- src/transport/tcp/mod.rs | 4 ---- 4 files changed, 8 deletions(-) diff --git a/src/transport/manager/handle.rs b/src/transport/manager/handle.rs index 31df6c36..ece77f02 100644 --- a/src/transport/manager/handle.rs +++ b/src/transport/manager/handle.rs @@ -287,7 +287,6 @@ pub struct TransportHandle { pub protocols: HashMap, pub next_connection_id: Arc, pub next_substream_id: Arc, - pub _protocol_names: Vec, pub bandwidth_sink: BandwidthSink, pub executor: Arc, } diff --git a/src/transport/manager/mod.rs b/src/transport/manager/mod.rs index d3ebddc8..de7130de 100644 --- a/src/transport/manager/mod.rs +++ b/src/transport/manager/mod.rs @@ -357,7 +357,6 @@ impl TransportManager { keypair: self.keypair.clone(), protocols: self.protocols.clone(), bandwidth_sink: self.bandwidth_sink.clone(), - _protocol_names: self.protocol_names.iter().cloned().collect(), next_substream_id: self.next_substream_id.clone(), next_connection_id: self.next_connection_id.clone(), } diff --git a/src/transport/quic/mod.rs b/src/transport/quic/mod.rs index eab11396..d0ac2375 100644 --- a/src/transport/quic/mod.rs +++ b/src/transport/quic/mod.rs @@ -495,7 +495,6 @@ mod tests { let handle1 = TransportHandle { executor: Arc::new(DefaultExecutor {}), - _protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair1.clone(), @@ -522,7 +521,6 @@ mod tests { let handle2 = TransportHandle { executor: Arc::new(DefaultExecutor {}), - _protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair2.clone(), diff --git a/src/transport/tcp/mod.rs b/src/transport/tcp/mod.rs index 066e3f36..9eb24d40 100644 --- a/src/transport/tcp/mod.rs +++ b/src/transport/tcp/mod.rs @@ -514,7 +514,6 @@ mod tests { let handle1 = crate::transport::manager::TransportHandle { executor: Arc::new(DefaultExecutor {}), - _protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair1.clone(), @@ -545,7 +544,6 @@ mod tests { let handle2 = crate::transport::manager::TransportHandle { executor: Arc::new(DefaultExecutor {}), - _protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair2.clone(), @@ -594,7 +592,6 @@ mod tests { let handle1 = crate::transport::manager::TransportHandle { executor: Arc::new(DefaultExecutor {}), - _protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair1.clone(), @@ -630,7 +627,6 @@ mod tests { let handle2 = crate::transport::manager::TransportHandle { executor: Arc::new(DefaultExecutor {}), - _protocol_names: Vec::new(), next_substream_id: Default::default(), next_connection_id: Default::default(), keypair: keypair2.clone(), From 16014167ef546e1f4cbcb73c26a9cbf21babdfd9 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Tue, 6 Aug 2024 15:24:17 +0000 Subject: [PATCH 21/21] tests: extract `Transport` helper into `common` module --- tests/common/mod.rs | 44 ++++++++++++++++++++++++++++++ tests/connection/mod.rs | 20 ++------------ tests/mod.rs | 1 + tests/protocol/identify.rs | 24 +--------------- tests/protocol/notification.rs | 20 +------------- tests/protocol/ping.rs | 26 ++---------------- tests/protocol/request_response.rs | 20 +------------- 7 files changed, 52 insertions(+), 103 deletions(-) create mode 100644 tests/common/mod.rs diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 00000000..50026109 --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,44 @@ +// Copyright 2024 litep2p developers +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +use litep2p::{config::ConfigBuilder, transport::tcp::config::Config as TcpConfig}; + +#[cfg(feature = "quic")] +use litep2p::transport::quic::config::Config as QuicConfig; +#[cfg(feature = "websocket")] +use litep2p::transport::websocket::config::Config as WebSocketConfig; + +pub(crate) enum Transport { + Tcp(TcpConfig), + #[cfg(feature = "quic")] + Quic(QuicConfig), + #[cfg(feature = "websocket")] + WebSocket(WebSocketConfig), +} + +pub(crate) fn add_transport(config: ConfigBuilder, transport: Transport) -> ConfigBuilder { + match transport { + Transport::Tcp(transport) => config.with_tcp(transport), + #[cfg(feature = "quic")] + Transport::Quic(transport) => config.with_quic(transport), + #[cfg(feature = "websocket")] + Transport::WebSocket(transport) => config.with_websocket(transport), + } +} diff --git a/tests/connection/mod.rs b/tests/connection/mod.rs index 89be67e4..769a161f 100644 --- a/tests/connection/mod.rs +++ b/tests/connection/mod.rs @@ -40,27 +40,11 @@ use tokio::net::TcpListener; #[cfg(feature = "quic")] use tokio::net::UdpSocket; +use crate::common::{add_transport, Transport}; + #[cfg(test)] mod protocol_dial_invalid_address; -enum Transport { - Tcp(TcpConfig), - #[cfg(feature = "quic")] - Quic(QuicConfig), - #[cfg(feature = "websocket")] - WebSocket(WebSocketConfig), -} - -fn add_transport(config: ConfigBuilder, transport: Transport) -> ConfigBuilder { - match transport { - Transport::Tcp(transport) => config.with_tcp(transport), - #[cfg(feature = "quic")] - Transport::Quic(transport) => config.with_quic(transport), - #[cfg(feature = "websocket")] - Transport::WebSocket(transport) => config.with_websocket(transport), - } -} - #[tokio::test] async fn two_litep2ps_work_tcp() { two_litep2ps_work( diff --git a/tests/mod.rs b/tests/mod.rs index bdd53f26..a4bede25 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -18,6 +18,7 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +mod common; mod conformance; mod connection; mod protocol; diff --git a/tests/protocol/identify.rs b/tests/protocol/identify.rs index e8a2f1d7..efb3da28 100644 --- a/tests/protocol/identify.rs +++ b/tests/protocol/identify.rs @@ -26,32 +26,10 @@ use litep2p::{ identify::{Config, IdentifyEvent}, ping::Config as PingConfig, }, - transport::tcp::config::Config as TcpConfig, Litep2p, Litep2pEvent, }; -#[cfg(feature = "quic")] -use litep2p::transport::quic::config::Config as QuicConfig; -#[cfg(feature = "websocket")] -use litep2p::transport::websocket::config::Config as WebSocketConfig; - -enum Transport { - Tcp(TcpConfig), - #[cfg(feature = "quic")] - Quic(QuicConfig), - #[cfg(feature = "websocket")] - WebSocket(WebSocketConfig), -} - -fn add_transport(config: ConfigBuilder, transport: Transport) -> ConfigBuilder { - match transport { - Transport::Tcp(transport) => config.with_tcp(transport), - #[cfg(feature = "quic")] - Transport::Quic(transport) => config.with_quic(transport), - #[cfg(feature = "websocket")] - Transport::WebSocket(transport) => config.with_websocket(transport), - } -} +use crate::common::{add_transport, Transport}; #[tokio::test] async fn identify_supported_tcp() { diff --git a/tests/protocol/notification.rs b/tests/protocol/notification.rs index 8227cbe9..ae034d49 100644 --- a/tests/protocol/notification.rs +++ b/tests/protocol/notification.rs @@ -31,8 +31,6 @@ use litep2p::{ Litep2p, Litep2pEvent, PeerId, }; -#[cfg(feature = "quic")] -use litep2p::transport::quic::config::Config as QuicConfig; #[cfg(feature = "websocket")] use litep2p::transport::websocket::config::Config as WebSocketConfig; @@ -45,23 +43,7 @@ use multihash::Multihash; use std::net::Ipv4Addr; use std::{net::Ipv6Addr, task::Poll, time::Duration}; -enum Transport { - Tcp(TcpConfig), - #[cfg(feature = "quic")] - Quic(QuicConfig), - #[cfg(feature = "websocket")] - WebSocket(WebSocketConfig), -} - -fn add_transport(config: Litep2pConfigBuilder, transport: Transport) -> Litep2pConfigBuilder { - match transport { - Transport::Tcp(transport) => config.with_tcp(transport), - #[cfg(feature = "quic")] - Transport::Quic(transport) => config.with_quic(transport), - #[cfg(feature = "websocket")] - Transport::WebSocket(transport) => config.with_websocket(transport), - } -} +use crate::common::{add_transport, Transport}; async fn connect_peers(litep2p1: &mut Litep2p, litep2p2: &mut Litep2p) { let address = litep2p2.listen_addresses().next().unwrap().clone(); diff --git a/tests/protocol/ping.rs b/tests/protocol/ping.rs index cc862a58..5a6fecbc 100644 --- a/tests/protocol/ping.rs +++ b/tests/protocol/ping.rs @@ -20,32 +20,10 @@ use futures::StreamExt; use litep2p::{ - config::ConfigBuilder, protocol::libp2p::ping::ConfigBuilder as PingConfigBuilder, - transport::tcp::config::Config as TcpConfig, Litep2p, + config::ConfigBuilder, protocol::libp2p::ping::ConfigBuilder as PingConfigBuilder, Litep2p, }; -#[cfg(feature = "quic")] -use litep2p::transport::quic::config::Config as QuicConfig; -#[cfg(feature = "websocket")] -use litep2p::transport::websocket::config::Config as WebSocketConfig; - -enum Transport { - Tcp(TcpConfig), - #[cfg(feature = "quic")] - Quic(QuicConfig), - #[cfg(feature = "websocket")] - WebSocket(WebSocketConfig), -} - -fn add_transport(config: ConfigBuilder, transport: Transport) -> ConfigBuilder { - match transport { - Transport::Tcp(transport) => config.with_tcp(transport), - #[cfg(feature = "quic")] - Transport::Quic(transport) => config.with_quic(transport), - #[cfg(feature = "websocket")] - Transport::WebSocket(transport) => config.with_websocket(transport), - } -} +use crate::common::{add_transport, Transport}; #[tokio::test] async fn ping_supported_tcp() { diff --git a/tests/protocol/request_response.rs b/tests/protocol/request_response.rs index 4d12bdbb..39d7e046 100644 --- a/tests/protocol/request_response.rs +++ b/tests/protocol/request_response.rs @@ -30,8 +30,6 @@ use litep2p::{ Litep2p, Litep2pEvent, PeerId, }; -#[cfg(feature = "quic")] -use litep2p::transport::quic::config::Config as QuicConfig; #[cfg(feature = "websocket")] use litep2p::transport::websocket::config::Config as WebSocketConfig; @@ -51,23 +49,7 @@ use std::{ time::Duration, }; -enum Transport { - Tcp(TcpConfig), - #[cfg(feature = "quic")] - Quic(QuicConfig), - #[cfg(feature = "websocket")] - WebSocket(WebSocketConfig), -} - -fn add_transport(config: Litep2pConfigBuilder, transport: Transport) -> Litep2pConfigBuilder { - match transport { - Transport::Tcp(transport) => config.with_tcp(transport), - #[cfg(feature = "quic")] - Transport::Quic(transport) => config.with_quic(transport), - #[cfg(feature = "websocket")] - Transport::WebSocket(transport) => config.with_websocket(transport), - } -} +use crate::common::{add_transport, Transport}; async fn connect_peers(litep2p1: &mut Litep2p, litep2p2: &mut Litep2p) { let address = litep2p2.listen_addresses().next().unwrap().clone();