diff --git a/libp2p/src/builder.rs b/libp2p/src/builder.rs index 83541378667..0dbeaa7e2ee 100644 --- a/libp2p/src/builder.rs +++ b/libp2p/src/builder.rs @@ -42,15 +42,15 @@ mod select_security; /// .with_quic() /// .with_other_transport(|_key| DummyTransport::<(PeerId, StreamMuxerBox)>::new())? /// .with_dns()? -/// .with_relay_client( -/// (libp2p_tls::Config::new, libp2p_noise::Config::new), -/// libp2p_yamux::Config::default, -/// )? /// .with_websocket( /// (libp2p_tls::Config::new, libp2p_noise::Config::new), /// libp2p_yamux::Config::default, /// ) /// .await? +/// .with_relay_client( +/// (libp2p_tls::Config::new, libp2p_noise::Config::new), +/// libp2p_yamux::Config::default, +/// )? /// .with_behaviour(|_key, relay| MyBehaviour { relay })? /// .build(); /// # @@ -307,11 +307,11 @@ mod tests { .with_quic() .with_dns() .unwrap() - .with_relay_client(libp2p_tls::Config::new, libp2p_yamux::Config::default) - .unwrap() .with_websocket(libp2p_tls::Config::new, libp2p_yamux::Config::default) .await .unwrap() + .with_relay_client(libp2p_tls::Config::new, libp2p_yamux::Config::default) + .unwrap() .with_bandwidth_logging(); let _: Swarm = builder .with_behaviour(|_key, relay| MyBehaviour { relay }) diff --git a/libp2p/src/builder/phase/dns.rs b/libp2p/src/builder/phase/dns.rs index 1d20cacd26d..ebcb3af54a2 100644 --- a/libp2p/src/builder/phase/dns.rs +++ b/libp2p/src/builder/phase/dns.rs @@ -11,13 +11,16 @@ impl SwarmBuilder Result< - SwarmBuilder>, + SwarmBuilder< + super::provider::AsyncStd, + WebsocketPhase, + >, std::io::Error, > { Ok(SwarmBuilder { keypair: self.keypair, phantom: PhantomData, - phase: RelayPhase { + phase: WebsocketPhase { transport: libp2p_dns::async_std::Transport::system(self.phase.transport).await?, }, }) @@ -29,13 +32,16 @@ impl SwarmBuilder Result< - SwarmBuilder>, + SwarmBuilder< + super::provider::Tokio, + WebsocketPhase, + >, std::io::Error, > { Ok(SwarmBuilder { keypair: self.keypair, phantom: PhantomData, - phase: RelayPhase { + phase: WebsocketPhase { transport: libp2p_dns::tokio::Transport::system(self.phase.transport)?, }, }) @@ -43,11 +49,11 @@ impl SwarmBuilder SwarmBuilder> { - pub(crate) fn without_dns(self) -> SwarmBuilder> { + pub(crate) fn without_dns(self) -> SwarmBuilder> { SwarmBuilder { keypair: self.keypair, phantom: PhantomData, - phase: RelayPhase { + phase: WebsocketPhase { transport: self.phase.transport, }, } @@ -61,8 +67,8 @@ impl SwarmBuilder R, ) -> Result>, R::Error> { self.without_dns() - .without_relay() .without_websocket() + .without_relay() .with_behaviour(constructor) } } diff --git a/libp2p/src/builder/phase/other_transport.rs b/libp2p/src/builder/phase/other_transport.rs index 6a050f4f2de..1453d2d097b 100644 --- a/libp2p/src/builder/phase/other_transport.rs +++ b/libp2p/src/builder/phase/other_transport.rs @@ -74,7 +74,10 @@ impl pub async fn with_dns( self, ) -> Result< - SwarmBuilder>, + SwarmBuilder< + super::provider::AsyncStd, + WebsocketPhase, + >, std::io::Error, > { self.without_any_other_transports().with_dns().await @@ -87,7 +90,10 @@ impl pub fn with_dns( self, ) -> Result< - SwarmBuilder>, + SwarmBuilder< + super::provider::Tokio, + WebsocketPhase, + >, std::io::Error, > { self.without_any_other_transports().with_dns() @@ -105,7 +111,7 @@ impl ) -> Result< SwarmBuilder< Provider, - WebsocketPhase, + BandwidthLoggingPhase, >, SecUpgrade::Error, > where @@ -132,6 +138,7 @@ impl { self.without_any_other_transports() .without_dns() + .without_websocket() .with_relay_client(security_upgrade, multiplexer_upgrade) } } @@ -149,8 +156,8 @@ impl ) { self.without_any_other_transports() .without_dns() - .without_relay() .without_websocket() + .without_relay() .with_bandwidth_logging() } } @@ -163,8 +170,8 @@ impl ) -> Result>, R::Error> { self.without_any_other_transports() .without_dns() - .without_relay() .without_websocket() + .without_relay() .without_bandwidth_logging() .with_behaviour(constructor) } diff --git a/libp2p/src/builder/phase/quic.rs b/libp2p/src/builder/phase/quic.rs index e49b3b07135..47eec66b8d2 100644 --- a/libp2p/src/builder/phase/quic.rs +++ b/libp2p/src/builder/phase/quic.rs @@ -82,7 +82,7 @@ impl SwarmBuilder Result< SwarmBuilder< Provider, - super::websocket::WebsocketPhase, + BandwidthLoggingPhase, >, SecUpgrade::Error, > where @@ -108,6 +108,9 @@ impl SwarmBuilder>::Upgrade as UpgradeInfo>::Info: Send, { self.without_quic() + .without_any_other_transports() + .without_dns() + .without_websocket() .with_relay_client(security_upgrade, multiplexer_upgrade) } @@ -139,8 +142,8 @@ impl SwarmBuilder SwarmBuilder Result< - SwarmBuilder>, + SwarmBuilder< + super::provider::AsyncStd, + WebsocketPhase, + >, std::io::Error, > { self.without_quic() @@ -163,7 +169,10 @@ impl SwarmBuilder Result< - SwarmBuilder>, + SwarmBuilder< + super::provider::Tokio, + WebsocketPhase, + >, std::io::Error, > { self.without_quic() @@ -190,7 +199,7 @@ macro_rules! impl_quic_phase_with_websocket { ) -> Result< SwarmBuilder< $providerPascalCase, - BandwidthLoggingPhase, + RelayPhase, >, super::websocket::WebsocketError, > @@ -218,7 +227,6 @@ macro_rules! impl_quic_phase_with_websocket { self.without_quic() .without_any_other_transports() .without_dns() - .without_relay() .with_websocket(security_upgrade, multiplexer_upgrade) .await } @@ -250,8 +258,8 @@ impl SwarmBuilder SwarmBuilder Result< SwarmBuilder< Provider, - WebsocketPhase, + BandwidthLoggingPhase, >, SecUpgrade::Error, > where @@ -78,20 +78,17 @@ impl SwarmBuilder SwarmBuilder> { pub(crate) fn without_relay( self, - ) -> SwarmBuilder> { + ) -> SwarmBuilder> { SwarmBuilder { keypair: self.keypair, phantom: PhantomData, - phase: WebsocketPhase { + phase: BandwidthLoggingPhase { transport: self.phase.transport, relay_behaviour: NoRelayBehaviour, }, @@ -123,70 +120,6 @@ impl SwarmBuilder R, ) -> Result>, R::Error> { - self.without_relay() - .without_websocket() - .with_behaviour(constructor) + self.without_relay().with_behaviour(constructor) } } -macro_rules! impl_relay_phase_with_websocket { - ($providerKebabCase:literal, $providerPascalCase:ty, $websocketStream:ty) => { - #[cfg(all(feature = $providerKebabCase, not(target_arch = "wasm32"), feature = "websocket"))] - impl SwarmBuilder<$providerPascalCase, RelayPhase> { - pub async fn with_websocket < - SecUpgrade, - SecStream, - SecError, - MuxUpgrade, - MuxStream, - MuxError, - > ( - self, - security_upgrade: SecUpgrade, - multiplexer_upgrade: MuxUpgrade, - ) -> Result< - SwarmBuilder< - $providerPascalCase, - BandwidthLoggingPhase, - >, - super::websocket::WebsocketError, - > - where - SecStream: futures::AsyncRead + futures::AsyncWrite + Unpin + Send + 'static, - SecError: std::error::Error + Send + Sync + 'static, - SecUpgrade: IntoSecurityUpgrade<$websocketStream>, - SecUpgrade::Upgrade: InboundUpgrade, Output = (libp2p_identity::PeerId, SecStream), Error = SecError> + OutboundUpgrade, Output = (libp2p_identity::PeerId, SecStream), Error = SecError> + Clone + Send + 'static, - >>::Future: Send, - >>::Future: Send, - <<>::Upgrade as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send, - <>::Upgrade as UpgradeInfo>::Info: Send, - - MuxStream: StreamMuxer + Send + 'static, - MuxStream::Substream: Send + 'static, - MuxStream::Error: Send + Sync + 'static, - MuxUpgrade: IntoMultiplexerUpgrade, - MuxUpgrade::Upgrade: InboundUpgrade, Output = MuxStream, Error = MuxError> + OutboundUpgrade, Output = MuxStream, Error = MuxError> + Clone + Send + 'static, - >>::Future: Send, - >>::Future: Send, - MuxError: std::error::Error + Send + Sync + 'static, - <<>::Upgrade as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send, - <>::Upgrade as UpgradeInfo>::Info: Send, - { - self.without_relay() - .with_websocket(security_upgrade, multiplexer_upgrade) - .await - } - } - } -} -impl_relay_phase_with_websocket!( - "async-std", - super::provider::AsyncStd, - rw_stream_sink::RwStreamSink< - libp2p_websocket::BytesConnection, - > -); -impl_relay_phase_with_websocket!( - "tokio", - super::provider::Tokio, - rw_stream_sink::RwStreamSink> -); diff --git a/libp2p/src/builder/phase/tcp.rs b/libp2p/src/builder/phase/tcp.rs index d2b69798252..aee786c869d 100644 --- a/libp2p/src/builder/phase/tcp.rs +++ b/libp2p/src/builder/phase/tcp.rs @@ -176,7 +176,7 @@ macro_rules! impl_tcp_phase_with_websocket { ) -> Result< SwarmBuilder< $providerPascalCase, - BandwidthLoggingPhase, + RelayPhase, >, WebsocketError, > @@ -205,7 +205,6 @@ macro_rules! impl_tcp_phase_with_websocket { .without_quic() .without_any_other_transports() .without_dns() - .without_relay() .with_websocket(security_upgrade, multiplexer_upgrade) .await } diff --git a/libp2p/src/builder/phase/websocket.rs b/libp2p/src/builder/phase/websocket.rs index bc536b4a7d1..0415b3a3b4d 100644 --- a/libp2p/src/builder/phase/websocket.rs +++ b/libp2p/src/builder/phase/websocket.rs @@ -4,15 +4,20 @@ use crate::SwarmBuilder; use libp2p_core::muxing::{StreamMuxer, StreamMuxerBox}; #[cfg(all(not(target_arch = "wasm32"), feature = "websocket"))] use libp2p_core::Transport; -#[cfg(all(not(target_arch = "wasm32"), feature = "websocket"))] +#[cfg(any( + all(not(target_arch = "wasm32"), feature = "websocket"), + feature = "relay" +))] use libp2p_core::{InboundUpgrade, Negotiated, OutboundUpgrade, UpgradeInfo}; -#[cfg(all(not(target_arch = "wasm32"), feature = "websocket"))] +#[cfg(any( + all(not(target_arch = "wasm32"), feature = "websocket"), + feature = "relay" +))] use libp2p_identity::PeerId; use std::marker::PhantomData; -pub struct WebsocketPhase { +pub struct WebsocketPhase { pub(crate) transport: T, - pub(crate) relay_behaviour: R, } macro_rules! impl_websocket_builder { @@ -39,7 +44,7 @@ macro_rules! impl_websocket_builder { /// # } /// ``` #[cfg(all(not(target_arch = "wasm32"), feature = $providerKebabCase, feature = "websocket"))] - impl SwarmBuilder<$providerPascalCase, WebsocketPhase> { + impl SwarmBuilder<$providerPascalCase, WebsocketPhase> { pub async fn with_websocket< SecUpgrade, SecStream, @@ -54,7 +59,7 @@ macro_rules! impl_websocket_builder { ) -> Result< SwarmBuilder< $providerPascalCase, - BandwidthLoggingPhase, + RelayPhase, >, WebsocketError, > @@ -96,11 +101,10 @@ macro_rules! impl_websocket_builder { Ok(SwarmBuilder { keypair: self.keypair, phantom: PhantomData, - phase: BandwidthLoggingPhase { + phase: RelayPhase { transport: websocket_transport .or_transport(self.phase.transport) .map(|either, _| either.into_inner()), - relay_behaviour: self.phase.relay_behaviour, }, }) } @@ -129,15 +133,12 @@ impl_websocket_builder!( rw_stream_sink::RwStreamSink> ); -impl - SwarmBuilder> -{ - pub(crate) fn without_websocket(self) -> SwarmBuilder> { +impl SwarmBuilder> { + pub(crate) fn without_websocket(self) -> SwarmBuilder> { SwarmBuilder { keypair: self.keypair, phantom: PhantomData, - phase: BandwidthLoggingPhase { - relay_behaviour: self.phase.relay_behaviour, + phase: RelayPhase { transport: self.phase.transport, }, } @@ -146,27 +147,51 @@ impl // Shortcuts #[cfg(feature = "relay")] -impl - SwarmBuilder> -{ - pub fn with_behaviour>( +impl SwarmBuilder> { + /// See [`SwarmBuilder::with_relay_client`]. + pub fn with_relay_client( self, - constructor: impl FnOnce(&libp2p_identity::Keypair, libp2p_relay::client::Behaviour) -> R, - ) -> Result>, R::Error> { + security_upgrade: SecUpgrade, + multiplexer_upgrade: MuxUpgrade, + ) -> Result< + SwarmBuilder< + Provider, + BandwidthLoggingPhase, + >, + SecUpgrade::Error, + > where + + SecStream: futures::AsyncRead + futures::AsyncWrite + Unpin + Send + 'static, + SecError: std::error::Error + Send + Sync + 'static, + SecUpgrade: IntoSecurityUpgrade, + SecUpgrade::Upgrade: InboundUpgrade, Output = (PeerId, SecStream), Error = SecError> + OutboundUpgrade, Output = (PeerId, SecStream), Error = SecError> + Clone + Send + 'static, + >>::Future: Send, + >>::Future: Send, + <<>::Upgrade as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send, + <>::Upgrade as UpgradeInfo>::Info: Send, + + MuxStream: libp2p_core::muxing::StreamMuxer + Send + 'static, + MuxStream::Substream: Send + 'static, + MuxStream::Error: Send + Sync + 'static, + MuxUpgrade: IntoMultiplexerUpgrade, + MuxUpgrade::Upgrade: InboundUpgrade, Output = MuxStream, Error = MuxError> + OutboundUpgrade, Output = MuxStream, Error = MuxError> + Clone + Send + 'static, + >>::Future: Send, + >>::Future: Send, + MuxError: std::error::Error + Send + Sync + 'static, + <<>::Upgrade as UpgradeInfo>::InfoIter as IntoIterator>::IntoIter: Send, + <>::Upgrade as UpgradeInfo>::Info: Send, + { self.without_websocket() - .without_bandwidth_logging() - .with_behaviour(constructor) + .with_relay_client(security_upgrade, multiplexer_upgrade) } } - -impl - SwarmBuilder> -{ +impl SwarmBuilder> { pub fn with_behaviour>( self, constructor: impl FnOnce(&libp2p_identity::Keypair) -> R, ) -> Result>, R::Error> { self.without_websocket() + .without_relay() .without_bandwidth_logging() .with_behaviour(constructor) }