From 2f5caf8f0c9bd1e8881b930a37596b5322267581 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sun, 29 Oct 2023 13:49:29 +1100 Subject: [PATCH 01/10] Remove generic parameter `THandlerErr` --- examples/file-sharing/src/network.rs | 7 +------ misc/metrics/src/identify.rs | 4 ++-- misc/metrics/src/lib.rs | 4 ++-- misc/metrics/src/swarm.rs | 10 ++++------ swarm-test/src/lib.rs | 30 ++++++++++------------------ swarm/src/connection.rs | 10 ++++------ swarm/src/connection/error.rs | 19 ++++-------------- swarm/src/connection/pool.rs | 2 +- swarm/src/connection/pool/task.rs | 2 +- swarm/src/lib.rs | 19 ++++++++---------- 10 files changed, 38 insertions(+), 69 deletions(-) diff --git a/examples/file-sharing/src/network.rs b/examples/file-sharing/src/network.rs index 2ea16ef180c..ad5418193a4 100644 --- a/examples/file-sharing/src/network.rs +++ b/examples/file-sharing/src/network.rs @@ -1,5 +1,3 @@ -use async_std::io; -use either::Either; use futures::channel::{mpsc, oneshot}; use futures::prelude::*; @@ -208,10 +206,7 @@ impl EventLoop { } } - async fn handle_event( - &mut self, - event: SwarmEvent>, - ) { + async fn handle_event(&mut self, event: SwarmEvent) { match event { SwarmEvent::Behaviour(BehaviourEvent::Kademlia( kad::Event::OutboundQueryProgressed { diff --git a/misc/metrics/src/identify.rs b/misc/metrics/src/identify.rs index 4dac6ea6774..b1d4e9f0c89 100644 --- a/misc/metrics/src/identify.rs +++ b/misc/metrics/src/identify.rs @@ -123,8 +123,8 @@ impl super::Recorder for Metrics { } } -impl super::Recorder> for Metrics { - fn record(&self, event: &libp2p_swarm::SwarmEvent) { +impl super::Recorder> for Metrics { + fn record(&self, event: &libp2p_swarm::SwarmEvent) { if let libp2p_swarm::SwarmEvent::ConnectionClosed { peer_id, num_established, diff --git a/misc/metrics/src/lib.rs b/misc/metrics/src/lib.rs index 2132dd5d7fb..97968253faa 100644 --- a/misc/metrics/src/lib.rs +++ b/misc/metrics/src/lib.rs @@ -138,8 +138,8 @@ impl Recorder for Metrics { } } -impl Recorder> for Metrics { - fn record(&self, event: &libp2p_swarm::SwarmEvent) { +impl Recorder> for Metrics { + fn record(&self, event: &libp2p_swarm::SwarmEvent) { self.swarm.record(event); #[cfg(feature = "identify")] diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index 8837457d36a..2d295627fa9 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -157,8 +157,8 @@ impl Metrics { } } -impl super::Recorder> for Metrics { - fn record(&self, event: &libp2p_swarm::SwarmEvent) { +impl super::Recorder> for Metrics { + fn record(&self, event: &libp2p_swarm::SwarmEvent) { match event { libp2p_swarm::SwarmEvent::Behaviour(_) => {} libp2p_swarm::SwarmEvent::ConnectionEstablished { @@ -317,15 +317,13 @@ struct ConnectionClosedLabels { enum ConnectionError { Io, KeepAliveTimeout, - Handler, } -impl From<&libp2p_swarm::ConnectionError> for ConnectionError { - fn from(value: &libp2p_swarm::ConnectionError) -> Self { +impl From<&libp2p_swarm::ConnectionError> for ConnectionError { + fn from(value: &libp2p_swarm::ConnectionError) -> Self { match value { libp2p_swarm::ConnectionError::IO(_) => ConnectionError::Io, libp2p_swarm::ConnectionError::KeepAliveTimeout => ConnectionError::KeepAliveTimeout, - libp2p_swarm::ConnectionError::Handler(_) => ConnectionError::Handler, } } } diff --git a/swarm-test/src/lib.rs b/swarm-test/src/lib.rs index 41a606b300c..c1a95fddf5e 100644 --- a/swarm-test/src/lib.rs +++ b/swarm-test/src/lib.rs @@ -27,9 +27,7 @@ use libp2p_core::{ use libp2p_identity::{Keypair, PeerId}; use libp2p_plaintext as plaintext; use libp2p_swarm::dial_opts::PeerCondition; -use libp2p_swarm::{ - self as swarm, dial_opts::DialOpts, NetworkBehaviour, Swarm, SwarmEvent, THandlerErr, -}; +use libp2p_swarm::{self as swarm, dial_opts::DialOpts, NetworkBehaviour, Swarm, SwarmEvent}; use libp2p_yamux as yamux; use std::fmt::Debug; use std::time::Duration; @@ -65,9 +63,7 @@ pub trait SwarmExt { /// Wait for specified condition to return `Some`. async fn wait(&mut self, predicate: P) -> E where - P: Fn( - SwarmEvent<::ToSwarm, THandlerErr>, - ) -> Option, + P: Fn(SwarmEvent<::ToSwarm>) -> Option, P: Send; /// Listens for incoming connections, polling the [`Swarm`] until the transport is ready to accept connections. @@ -78,9 +74,7 @@ pub trait SwarmExt { /// Returns the next [`SwarmEvent`] or times out after 10 seconds. /// /// If the 10s timeout does not fit your usecase, please fall back to `StreamExt::next`. - async fn next_swarm_event( - &mut self, - ) -> SwarmEvent<::ToSwarm, THandlerErr>; + async fn next_swarm_event(&mut self) -> SwarmEvent<::ToSwarm>; /// Returns the next behaviour event or times out after 10 seconds. /// @@ -137,8 +131,8 @@ where TBehaviour2::ToSwarm: Debug, TBehaviour1: NetworkBehaviour + Send, TBehaviour1::ToSwarm: Debug, - SwarmEvent>: TryIntoOutput, - SwarmEvent>: TryIntoOutput, + SwarmEvent: TryIntoOutput, + SwarmEvent: TryIntoOutput, Out1: Debug, Out2: Debug, { @@ -180,15 +174,15 @@ pub trait TryIntoOutput: Sized { fn try_into_output(self) -> Result; } -impl TryIntoOutput for SwarmEvent { +impl TryIntoOutput for SwarmEvent { fn try_into_output(self) -> Result { self.try_into_behaviour_event() } } -impl TryIntoOutput> - for SwarmEvent +impl TryIntoOutput> + for SwarmEvent { - fn try_into_output(self) -> Result, Self> { + fn try_into_output(self) -> Result, Self> { Ok(self) } } @@ -281,7 +275,7 @@ where async fn wait(&mut self, predicate: P) -> E where - P: Fn(SwarmEvent<::ToSwarm, THandlerErr>) -> Option, + P: Fn(SwarmEvent<::ToSwarm>) -> Option, P: Send, { loop { @@ -341,9 +335,7 @@ where (memory_multiaddr, tcp_multiaddr) } - async fn next_swarm_event( - &mut self, - ) -> SwarmEvent<::ToSwarm, THandlerErr> { + async fn next_swarm_event(&mut self) -> SwarmEvent<::ToSwarm> { match futures::future::select( futures_timer::Delay::new(Duration::from_secs(10)), self.select_next_some(), diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index ee2729e0c82..e694dc271d5 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -238,7 +238,7 @@ where pub(crate) fn poll( self: Pin<&mut Self>, cx: &mut Context<'_>, - ) -> Poll, ConnectionError>> { + ) -> Poll, ConnectionError>> { let Self { requested_substreams, muxing, @@ -282,8 +282,8 @@ where Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)) => { return Poll::Ready(Ok(Event::Handler(event))); } - Poll::Ready(ConnectionHandlerEvent::Close(err)) => { - return Poll::Ready(Err(ConnectionError::Handler(err))); + Poll::Ready(ConnectionHandlerEvent::Close(_)) => { + todo!() } Poll::Ready(ConnectionHandlerEvent::ReportRemoteProtocols( ProtocolSupport::Added(protocols), @@ -451,9 +451,7 @@ where } #[cfg(test)] - fn poll_noop_waker( - &mut self, - ) -> Poll, ConnectionError>> { + fn poll_noop_waker(&mut self) -> Poll, ConnectionError>> { Pin::new(self).poll(&mut Context::from_waker(futures::task::noop_waker_ref())) } } diff --git a/swarm/src/connection/error.rs b/swarm/src/connection/error.rs index 5d5dda57868..33aa81c19a9 100644 --- a/swarm/src/connection/error.rs +++ b/swarm/src/connection/error.rs @@ -25,47 +25,36 @@ use std::{fmt, io}; /// Errors that can occur in the context of an established `Connection`. #[derive(Debug)] -pub enum ConnectionError { +pub enum ConnectionError { /// An I/O error occurred on the connection. // TODO: Eventually this should also be a custom error? IO(io::Error), /// The connection keep-alive timeout expired. KeepAliveTimeout, - - /// The connection handler produced an error. - Handler(THandlerErr), } -impl fmt::Display for ConnectionError -where - THandlerErr: fmt::Display, -{ +impl fmt::Display for ConnectionError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ConnectionError::IO(err) => write!(f, "Connection error: I/O error: {err}"), ConnectionError::KeepAliveTimeout => { write!(f, "Connection closed due to expired keep-alive timeout.") } - ConnectionError::Handler(err) => write!(f, "Connection error: Handler error: {err}"), } } } -impl std::error::Error for ConnectionError -where - THandlerErr: std::error::Error + 'static, -{ +impl std::error::Error for ConnectionError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { ConnectionError::IO(err) => Some(err), ConnectionError::KeepAliveTimeout => None, - ConnectionError::Handler(err) => Some(err), } } } -impl From for ConnectionError { +impl From for ConnectionError { fn from(error: io::Error) -> Self { ConnectionError::IO(error) } diff --git a/swarm/src/connection/pool.rs b/swarm/src/connection/pool.rs index 8a2f1cb6b20..a5381797dc6 100644 --- a/swarm/src/connection/pool.rs +++ b/swarm/src/connection/pool.rs @@ -257,7 +257,7 @@ pub(crate) enum PoolEvent { connected: Connected, /// The error that occurred, if any. If `None`, the connection /// was closed by the local peer. - error: Option>, + error: Option, /// The remaining established connections to the same peer. remaining_established_connection_ids: Vec, }, diff --git a/swarm/src/connection/pool/task.rs b/swarm/src/connection/pool/task.rs index f2c6928cd27..96989292b16 100644 --- a/swarm/src/connection/pool/task.rs +++ b/swarm/src/connection/pool/task.rs @@ -86,7 +86,7 @@ pub(crate) enum EstablishedConnectionEvent { Closed { id: ConnectionId, peer_id: PeerId, - error: Option>, + error: Option, }, } diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 908936069e0..aba77674506 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -166,12 +166,9 @@ pub type THandlerInEvent = as ConnectionHandle /// Custom event that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`]. pub type THandlerOutEvent = as ConnectionHandler>::ToBehaviour; -/// Custom error that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`]. -pub type THandlerErr = as ConnectionHandler>::Error; - /// Event generated by the `Swarm`. #[derive(Debug)] -pub enum SwarmEvent { +pub enum SwarmEvent { /// Event generated by the `NetworkBehaviour`. Behaviour(TBehaviourOutEvent), /// A connection to the given peer has been opened. @@ -205,7 +202,7 @@ pub enum SwarmEvent { num_established: u32, /// Reason for the disconnection, if it was not a successful /// active close. - cause: Option>, + cause: Option, }, /// A new connection arrived on a listener and is in the process of protocol negotiation. /// @@ -296,7 +293,7 @@ pub enum SwarmEvent { }, } -impl SwarmEvent { +impl SwarmEvent { /// Extract the `TBehaviourOutEvent` from this [`SwarmEvent`] in case it is the `Behaviour` variant, otherwise fail. #[allow(clippy::result_large_err)] pub fn try_into_behaviour_event(self) -> Result { @@ -666,7 +663,7 @@ where fn handle_pool_event( &mut self, event: PoolEvent>, - ) -> Option>> { + ) -> Option> { match event { PoolEvent::ConnectionEstablished { peer_id, @@ -911,7 +908,7 @@ where as Transport>::ListenerUpgrade, io::Error, >, - ) -> Option>> { + ) -> Option> { match event { TransportEvent::Incoming { listener_id: _, @@ -1039,7 +1036,7 @@ where fn handle_behaviour_event( &mut self, event: ToSwarm>, - ) -> Option>> { + ) -> Option> { match event { ToSwarm::GenerateEvent(event) => return Some(SwarmEvent::Behaviour(event)), ToSwarm::Dial { opts } => { @@ -1140,7 +1137,7 @@ where fn poll_next_event( mut self: Pin<&mut Self>, cx: &mut Context<'_>, - ) -> Poll>> { + ) -> Poll> { // We use a `this` variable because the compiler can't mutably borrow multiple times // across a `Deref`. let this = &mut *self; @@ -1319,7 +1316,7 @@ impl futures::Stream for Swarm where TBehaviour: NetworkBehaviour, { - type Item = SwarmEvent, THandlerErr>; + type Item = SwarmEvent>; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { self.as_mut().poll_next_event(cx).map(Some) From 507f1b698d7c2600ed315c140326202c441900ab Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sun, 29 Oct 2023 13:53:11 +1100 Subject: [PATCH 02/10] Remove `ConnectionHandlerEvent::Close` This commit purposely does not touch any protocol crates to avoid merge conflicts. --- protocols/dcutr/src/handler/relayed.rs | 8 +--- protocols/gossipsub/src/handler.rs | 8 +--- protocols/identify/src/handler.rs | 5 +- protocols/kad/src/handler.rs | 11 ++--- protocols/perf/src/client/handler.rs | 7 +-- protocols/perf/src/server/handler.rs | 7 +-- protocols/ping/src/handler.rs | 10 +--- protocols/relay/src/behaviour/handler.rs | 8 +--- protocols/relay/src/priv_client/handler.rs | 8 +--- protocols/request-response/src/handler.rs | 3 +- swarm/src/behaviour/toggle.rs | 7 +-- swarm/src/connection.rs | 3 -- swarm/src/dummy.rs | 7 +-- swarm/src/handler.rs | 56 +++------------------- swarm/src/handler/either.rs | 9 +--- swarm/src/handler/map_in.rs | 7 +-- swarm/src/handler/map_out.rs | 8 +--- swarm/src/handler/multi.rs | 7 +-- swarm/src/handler/one_shot.rs | 7 +-- swarm/src/handler/pending.rs | 7 +-- swarm/src/handler/select.rs | 13 +---- swarm/tests/connection_close.rs | 7 +-- 22 files changed, 30 insertions(+), 183 deletions(-) diff --git a/protocols/dcutr/src/handler/relayed.rs b/protocols/dcutr/src/handler/relayed.rs index 23ab9f4ae5a..e69be6b02be 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -71,7 +71,6 @@ pub struct Handler { ::OutboundProtocol, ::OutboundOpenInfo, ::ToBehaviour, - ::Error, >, >, /// Inbound connect, accepted by the behaviour, pending completion. @@ -261,12 +260,7 @@ impl ConnectionHandler for Handler { &mut self, cx: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { // Check for a pending (fatal) error. if let Some(err) = self.pending_error.take() { diff --git a/protocols/gossipsub/src/handler.rs b/protocols/gossipsub/src/handler.rs index 44258bb5394..8b7b217d68f 100644 --- a/protocols/gossipsub/src/handler.rs +++ b/protocols/gossipsub/src/handler.rs @@ -220,7 +220,6 @@ impl EnabledHandler { ::OutboundProtocol, ::OutboundOpenInfo, ::ToBehaviour, - ::Error, >, > { if !self.peer_kind_sent { @@ -431,12 +430,7 @@ impl ConnectionHandler for Handler { &mut self, cx: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { match self { Handler::Enabled(handler) => handler.poll(cx), diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index 51501d79f9c..7289424a934 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -57,7 +57,6 @@ pub struct Handler { Either, ReadyUpgrade>, (), Event, - io::Error, >; 4], >, @@ -317,9 +316,7 @@ impl ConnectionHandler for Handler { fn poll( &mut self, cx: &mut Context<'_>, - ) -> Poll< - ConnectionHandlerEvent, - > { + ) -> Poll> { if let Some(event) = self.events.pop() { return Poll::Ready(event); } diff --git a/protocols/kad/src/handler.rs b/protocols/kad/src/handler.rs index fce77bc13e4..2ae43b6f31f 100644 --- a/protocols/kad/src/handler.rs +++ b/protocols/kad/src/handler.rs @@ -706,12 +706,7 @@ impl ConnectionHandler for Handler { &mut self, cx: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { match &mut self.protocol_status { Some(status) if !status.reported => { @@ -847,7 +842,7 @@ impl Handler { } impl futures::Stream for OutboundSubstreamState { - type Item = ConnectionHandlerEvent; + type Item = ConnectionHandlerEvent; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { let this = self.get_mut(); @@ -979,7 +974,7 @@ impl futures::Stream for OutboundSubstreamState { } impl futures::Stream for InboundSubstreamState { - type Item = ConnectionHandlerEvent; + type Item = ConnectionHandlerEvent; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { let this = self.get_mut(); diff --git a/protocols/perf/src/client/handler.rs b/protocols/perf/src/client/handler.rs index a9bb0c7d483..8e93ccec0bc 100644 --- a/protocols/perf/src/client/handler.rs +++ b/protocols/perf/src/client/handler.rs @@ -157,12 +157,7 @@ impl ConnectionHandler for Handler { &mut self, cx: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { if let Some(event) = self.queued_events.pop_front() { return Poll::Ready(event); diff --git a/protocols/perf/src/server/handler.rs b/protocols/perf/src/server/handler.rs index 4e739995b67..5eb44bc6a93 100644 --- a/protocols/perf/src/server/handler.rs +++ b/protocols/perf/src/server/handler.rs @@ -119,12 +119,7 @@ impl ConnectionHandler for Handler { &mut self, cx: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { loop { match self.inbound.poll_unpin(cx) { diff --git a/protocols/ping/src/handler.rs b/protocols/ping/src/handler.rs index 3a92ef4b249..8bfc53c6778 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -228,14 +228,8 @@ impl ConnectionHandler for Handler { fn poll( &mut self, cx: &mut Context<'_>, - ) -> Poll< - ConnectionHandlerEvent< - ReadyUpgrade, - (), - Result, - Self::Error, - >, - > { + ) -> Poll, (), Result>> + { match self.state { State::Inactive { reported: true } => { return Poll::Pending; // nothing to do on this connection diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index 13619cb45c6..cb0684dadc0 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -344,7 +344,6 @@ pub struct Handler { ::OutboundProtocol, ::OutboundOpenInfo, ::ToBehaviour, - ::Error, >, >, @@ -623,12 +622,7 @@ impl ConnectionHandler for Handler { &mut self, cx: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { // Check for a pending (fatal) error. if let Some(err) = self.pending_error.take() { diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index fb7428e3133..ba004278aef 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -128,7 +128,6 @@ pub struct Handler { ::OutboundProtocol, ::OutboundOpenInfo, ::ToBehaviour, - ::Error, >, >, @@ -327,12 +326,7 @@ impl ConnectionHandler for Handler { &mut self, cx: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { // Check for a pending (fatal) error. if let Some(err) = self.pending_error.take() { diff --git a/protocols/request-response/src/handler.rs b/protocols/request-response/src/handler.rs index f4f5bf96c6c..5c0b440142b 100644 --- a/protocols/request-response/src/handler.rs +++ b/protocols/request-response/src/handler.rs @@ -389,8 +389,7 @@ where fn poll( &mut self, cx: &mut Context<'_>, - ) -> Poll, (), Self::ToBehaviour, Self::Error>> - { + ) -> Poll, (), Self::ToBehaviour>> { match self.worker_streams.poll_unpin(cx) { Poll::Ready((_, Ok(Ok(event)))) => { return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)); diff --git a/swarm/src/behaviour/toggle.rs b/swarm/src/behaviour/toggle.rs index e1da71a0450..59511f47aae 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -299,12 +299,7 @@ where &mut self, cx: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { if let Some(inner) = self.inner.as_mut() { inner.poll(cx) diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index e694dc271d5..a4c00ef2d5e 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -282,9 +282,6 @@ where Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)) => { return Poll::Ready(Ok(Event::Handler(event))); } - Poll::Ready(ConnectionHandlerEvent::Close(_)) => { - todo!() - } Poll::Ready(ConnectionHandlerEvent::ReportRemoteProtocols( ProtocolSupport::Added(protocols), )) => { diff --git a/swarm/src/dummy.rs b/swarm/src/dummy.rs index 1005c4be035..efef67a65bc 100644 --- a/swarm/src/dummy.rs +++ b/swarm/src/dummy.rs @@ -98,12 +98,7 @@ impl crate::handler::ConnectionHandler for ConnectionHandler { &mut self, _: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { Poll::Pending } diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index ea42f64cca9..3ab731ff994 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -149,12 +149,7 @@ pub trait ConnectionHandler: Send + 'static { &mut self, cx: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, >; /// Gracefully close the [`ConnectionHandler`]. @@ -539,21 +534,12 @@ impl SubstreamProtocol { /// Event produced by a handler. #[derive(Debug, Clone, PartialEq, Eq)] -pub enum ConnectionHandlerEvent { +pub enum ConnectionHandlerEvent { /// Request a new outbound substream to be opened with the remote. OutboundSubstreamRequest { /// The protocol(s) to apply on the substream. protocol: SubstreamProtocol, }, - - /// Close the connection for the given reason. - /// - /// Note this will affect all [`ConnectionHandler`]s handling this - /// connection, in other words it will close the connection for all - /// [`ConnectionHandler`]s. To signal that one has no more need for the - /// connection, while allowing other [`ConnectionHandler`]s to continue using - /// the connection, return false in [`ConnectionHandler::connection_keep_alive`]. - Close(TErr), /// We learned something about the protocols supported by the remote. ReportRemoteProtocols(ProtocolSupport), @@ -570,15 +556,15 @@ pub enum ProtocolSupport { } /// Event produced by a handler. -impl - ConnectionHandlerEvent +impl + ConnectionHandlerEvent { /// If this is an `OutboundSubstreamRequest`, maps the `info` member from a /// `TOutboundOpenInfo` to something else. pub fn map_outbound_open_info( self, map: F, - ) -> ConnectionHandlerEvent + ) -> ConnectionHandlerEvent where F: FnOnce(TOutboundOpenInfo) -> I, { @@ -591,7 +577,6 @@ impl ConnectionHandlerEvent::NotifyBehaviour(val) => { ConnectionHandlerEvent::NotifyBehaviour(val) } - ConnectionHandlerEvent::Close(val) => ConnectionHandlerEvent::Close(val), ConnectionHandlerEvent::ReportRemoteProtocols(support) => { ConnectionHandlerEvent::ReportRemoteProtocols(support) } @@ -600,10 +585,7 @@ impl /// If this is an `OutboundSubstreamRequest`, maps the protocol (`TConnectionUpgrade`) /// to something else. - pub fn map_protocol( - self, - map: F, - ) -> ConnectionHandlerEvent + pub fn map_protocol(self, map: F) -> ConnectionHandlerEvent where F: FnOnce(TConnectionUpgrade) -> I, { @@ -616,7 +598,6 @@ impl ConnectionHandlerEvent::NotifyBehaviour(val) => { ConnectionHandlerEvent::NotifyBehaviour(val) } - ConnectionHandlerEvent::Close(val) => ConnectionHandlerEvent::Close(val), ConnectionHandlerEvent::ReportRemoteProtocols(support) => { ConnectionHandlerEvent::ReportRemoteProtocols(support) } @@ -627,7 +608,7 @@ impl pub fn map_custom( self, map: F, - ) -> ConnectionHandlerEvent + ) -> ConnectionHandlerEvent where F: FnOnce(TCustom) -> I, { @@ -638,29 +619,6 @@ impl ConnectionHandlerEvent::NotifyBehaviour(val) => { ConnectionHandlerEvent::NotifyBehaviour(map(val)) } - ConnectionHandlerEvent::Close(val) => ConnectionHandlerEvent::Close(val), - ConnectionHandlerEvent::ReportRemoteProtocols(support) => { - ConnectionHandlerEvent::ReportRemoteProtocols(support) - } - } - } - - /// If this is a `Close` event, maps the content to something else. - pub fn map_close( - self, - map: F, - ) -> ConnectionHandlerEvent - where - F: FnOnce(TErr) -> I, - { - match self { - ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } => { - ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } - } - ConnectionHandlerEvent::NotifyBehaviour(val) => { - ConnectionHandlerEvent::NotifyBehaviour(val) - } - ConnectionHandlerEvent::Close(val) => ConnectionHandlerEvent::Close(map(val)), ConnectionHandlerEvent::ReportRemoteProtocols(support) => { ConnectionHandlerEvent::ReportRemoteProtocols(support) } diff --git a/swarm/src/handler/either.rs b/swarm/src/handler/either.rs index 093900135b8..c48217edd0f 100644 --- a/swarm/src/handler/either.rs +++ b/swarm/src/handler/either.rs @@ -119,22 +119,15 @@ where &mut self, cx: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { let event = match self { Either::Left(handler) => futures::ready!(handler.poll(cx)) .map_custom(Either::Left) - .map_close(Either::Left) .map_protocol(|p| Either::Left(SendWrapper(p))) .map_outbound_open_info(Either::Left), Either::Right(handler) => futures::ready!(handler.poll(cx)) .map_custom(Either::Right) - .map_close(Either::Right) .map_protocol(|p| Either::Right(SendWrapper(p))) .map_outbound_open_info(Either::Right), }; diff --git a/swarm/src/handler/map_in.rs b/swarm/src/handler/map_in.rs index e3458eb5451..b778a01ed30 100644 --- a/swarm/src/handler/map_in.rs +++ b/swarm/src/handler/map_in.rs @@ -76,12 +76,7 @@ where &mut self, cx: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { self.inner.poll(cx) } diff --git a/swarm/src/handler/map_out.rs b/swarm/src/handler/map_out.rs index cc06a4c50c8..922228bfa49 100644 --- a/swarm/src/handler/map_out.rs +++ b/swarm/src/handler/map_out.rs @@ -69,18 +69,12 @@ where &mut self, cx: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { self.inner.poll(cx).map(|ev| match ev { ConnectionHandlerEvent::NotifyBehaviour(ev) => { ConnectionHandlerEvent::NotifyBehaviour((self.map)(ev)) } - ConnectionHandlerEvent::Close(err) => ConnectionHandlerEvent::Close(err), ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } => { ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } } diff --git a/swarm/src/handler/multi.rs b/swarm/src/handler/multi.rs index 41e0cf42df9..dce7086fb16 100644 --- a/swarm/src/handler/multi.rs +++ b/swarm/src/handler/multi.rs @@ -241,12 +241,7 @@ where &mut self, cx: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { // Calling `gen_range(0, 0)` (see below) would panic, so we have return early to avoid // that situation. diff --git a/swarm/src/handler/one_shot.rs b/swarm/src/handler/one_shot.rs index a611bc5073c..e2bdbab612b 100644 --- a/swarm/src/handler/one_shot.rs +++ b/swarm/src/handler/one_shot.rs @@ -133,12 +133,7 @@ where &mut self, _: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { if !self.events_out.is_empty() { return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( diff --git a/swarm/src/handler/pending.rs b/swarm/src/handler/pending.rs index 90e6522404e..170b6c54544 100644 --- a/swarm/src/handler/pending.rs +++ b/swarm/src/handler/pending.rs @@ -60,12 +60,7 @@ impl ConnectionHandler for PendingConnectionHandler { &mut self, _: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { Poll::Pending } diff --git a/swarm/src/handler/select.rs b/swarm/src/handler/select.rs index 957ba43fbe7..b5d1b4bab6d 100644 --- a/swarm/src/handler/select.rs +++ b/swarm/src/handler/select.rs @@ -219,20 +219,12 @@ where &mut self, cx: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { match self.proto1.poll(cx) { Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)) => { return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Either::Left(event))); } - Poll::Ready(ConnectionHandlerEvent::Close(event)) => { - return Poll::Ready(ConnectionHandlerEvent::Close(Either::Left(event))); - } Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { protocol }) => { return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { protocol: protocol @@ -252,9 +244,6 @@ where event, ))); } - Poll::Ready(ConnectionHandlerEvent::Close(event)) => { - return Poll::Ready(ConnectionHandlerEvent::Close(Either::Right(event))); - } Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { protocol }) => { return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { protocol: protocol diff --git a/swarm/tests/connection_close.rs b/swarm/tests/connection_close.rs index a44518fa4ad..0b99df80a64 100644 --- a/swarm/tests/connection_close.rs +++ b/swarm/tests/connection_close.rs @@ -114,12 +114,7 @@ impl ConnectionHandler for HandlerWithState { &mut self, _: &mut Context<'_>, ) -> Poll< - ConnectionHandlerEvent< - Self::OutboundProtocol, - Self::OutboundOpenInfo, - Self::ToBehaviour, - Self::Error, - >, + ConnectionHandlerEvent, > { Poll::Pending } From 3299d5201c7e9fc707e9f1ec2ff3c428cef26b1b Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sun, 29 Oct 2023 13:55:06 +1100 Subject: [PATCH 03/10] Remove `Error` associated type --- protocols/dcutr/src/handler/relayed.rs | 3 --- protocols/gossipsub/src/handler.rs | 2 -- protocols/identify/src/handler.rs | 3 +-- protocols/kad/src/handler.rs | 1 - protocols/ping/src/handler.rs | 1 - protocols/relay/src/behaviour/handler.rs | 3 --- protocols/relay/src/priv_client/handler.rs | 3 --- protocols/request-response/src/handler.rs | 1 - swarm/src/behaviour/toggle.rs | 1 - swarm/src/dummy.rs | 1 - swarm/src/handler.rs | 2 -- swarm/src/handler/either.rs | 1 - swarm/src/handler/map_in.rs | 1 - swarm/src/handler/map_out.rs | 1 - swarm/src/handler/multi.rs | 1 - swarm/src/handler/one_shot.rs | 1 - swarm/src/handler/pending.rs | 1 - swarm/src/handler/select.rs | 1 - 18 files changed, 1 insertion(+), 27 deletions(-) diff --git a/protocols/dcutr/src/handler/relayed.rs b/protocols/dcutr/src/handler/relayed.rs index e69be6b02be..54e7c0f0f3a 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -209,9 +209,6 @@ impl Handler { impl ConnectionHandler for Handler { type FromBehaviour = Command; type ToBehaviour = Event; - type Error = StreamUpgradeError< - Either, - >; type InboundProtocol = Either; type OutboundProtocol = protocol::outbound::Upgrade; type OutboundOpenInfo = (); diff --git a/protocols/gossipsub/src/handler.rs b/protocols/gossipsub/src/handler.rs index 8b7b217d68f..f5fc0d03921 100644 --- a/protocols/gossipsub/src/handler.rs +++ b/protocols/gossipsub/src/handler.rs @@ -38,7 +38,6 @@ use std::{ pin::Pin, task::{Context, Poll}, }; -use void::Void; /// The event emitted by the Handler. This informs the behaviour of various events created /// by the handler. @@ -388,7 +387,6 @@ impl EnabledHandler { impl ConnectionHandler for Handler { type FromBehaviour = HandlerIn; type ToBehaviour = HandlerEvent; - type Error = Void; type InboundOpenInfo = (); type InboundProtocol = either::Either; type OutboundOpenInfo = (); diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index 7289424a934..e4213b1e86b 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -39,7 +39,7 @@ use libp2p_swarm::{ use log::{warn, Level}; use smallvec::SmallVec; use std::collections::HashSet; -use std::{io, task::Context, task::Poll, time::Duration}; +use std::{task::Context, task::Poll, time::Duration}; const STREAM_TIMEOUT: Duration = Duration::from_secs(60); const MAX_CONCURRENT_STREAMS_PER_CONNECTION: usize = 10; @@ -279,7 +279,6 @@ impl Handler { impl ConnectionHandler for Handler { type FromBehaviour = InEvent; type ToBehaviour = Event; - type Error = io::Error; type InboundProtocol = SelectUpgrade, ReadyUpgrade>; type OutboundProtocol = Either, ReadyUpgrade>; diff --git a/protocols/kad/src/handler.rs b/protocols/kad/src/handler.rs index 2ae43b6f31f..8904a13f828 100644 --- a/protocols/kad/src/handler.rs +++ b/protocols/kad/src/handler.rs @@ -599,7 +599,6 @@ impl Handler { impl ConnectionHandler for Handler { type FromBehaviour = HandlerIn; type ToBehaviour = HandlerEvent; - type Error = io::Error; // TODO: better error type? type InboundProtocol = Either; type OutboundProtocol = ProtocolConfig; type OutboundOpenInfo = (); diff --git a/protocols/ping/src/handler.rs b/protocols/ping/src/handler.rs index 8bfc53c6778..e641dce2137 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -213,7 +213,6 @@ impl Handler { impl ConnectionHandler for Handler { type FromBehaviour = Void; type ToBehaviour = Result; - type Error = Void; type InboundProtocol = ReadyUpgrade; type OutboundProtocol = ReadyUpgrade; type OutboundOpenInfo = (); diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index cb0684dadc0..ec7d62c3378 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -507,9 +507,6 @@ type Futures = FuturesUnordered>; impl ConnectionHandler for Handler { type FromBehaviour = In; type ToBehaviour = Event; - type Error = StreamUpgradeError< - Either, - >; type InboundProtocol = ReadyUpgrade; type InboundOpenInfo = (); type OutboundProtocol = ReadyUpgrade; diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index ba004278aef..76ff7ebf680 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -279,9 +279,6 @@ impl Handler { impl ConnectionHandler for Handler { type FromBehaviour = In; type ToBehaviour = Event; - type Error = StreamUpgradeError< - Either, - >; type InboundProtocol = ReadyUpgrade; type InboundOpenInfo = (); type OutboundProtocol = ReadyUpgrade; diff --git a/protocols/request-response/src/handler.rs b/protocols/request-response/src/handler.rs index 5c0b440142b..bc7b6bb1b0a 100644 --- a/protocols/request-response/src/handler.rs +++ b/protocols/request-response/src/handler.rs @@ -367,7 +367,6 @@ where { type FromBehaviour = OutboundMessage; type ToBehaviour = Event; - type Error = void::Void; type InboundProtocol = Protocol; type OutboundProtocol = Protocol; type OutboundOpenInfo = (); diff --git a/swarm/src/behaviour/toggle.rs b/swarm/src/behaviour/toggle.rs index 59511f47aae..5c23ee099a3 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -264,7 +264,6 @@ where { type FromBehaviour = TInner::FromBehaviour; type ToBehaviour = TInner::ToBehaviour; - type Error = TInner::Error; type InboundProtocol = Either, SendWrapper>; type OutboundProtocol = TInner::OutboundProtocol; type OutboundOpenInfo = TInner::OutboundOpenInfo; diff --git a/swarm/src/dummy.rs b/swarm/src/dummy.rs index efef67a65bc..2937fe74fe1 100644 --- a/swarm/src/dummy.rs +++ b/swarm/src/dummy.rs @@ -80,7 +80,6 @@ pub struct ConnectionHandler; impl crate::handler::ConnectionHandler for ConnectionHandler { type FromBehaviour = Void; type ToBehaviour = Void; - type Error = Void; type InboundProtocol = DeniedUpgrade; type OutboundProtocol = DeniedUpgrade; type InboundOpenInfo = (); diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index 3ab731ff994..caaf37d2526 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -102,8 +102,6 @@ pub trait ConnectionHandler: Send + 'static { type FromBehaviour: fmt::Debug + Send + 'static; /// A type representing message(s) a [`ConnectionHandler`] can send to a [`NetworkBehaviour`](crate::behaviour::NetworkBehaviour) via [`ConnectionHandlerEvent::NotifyBehaviour`]. type ToBehaviour: fmt::Debug + Send + 'static; - /// The type of errors returned by [`ConnectionHandler::poll`]. - type Error: error::Error + fmt::Debug + Send + 'static; /// The inbound upgrade for the protocol(s) used by the handler. type InboundProtocol: InboundUpgradeSend; /// The outbound upgrade for the protocol(s) used by the handler. diff --git a/swarm/src/handler/either.rs b/swarm/src/handler/either.rs index c48217edd0f..b48b7cdcb15 100644 --- a/swarm/src/handler/either.rs +++ b/swarm/src/handler/either.rs @@ -80,7 +80,6 @@ where { type FromBehaviour = Either; type ToBehaviour = Either; - type Error = Either; type InboundProtocol = Either, SendWrapper>; type OutboundProtocol = Either, SendWrapper>; diff --git a/swarm/src/handler/map_in.rs b/swarm/src/handler/map_in.rs index b778a01ed30..bd45eee4d97 100644 --- a/swarm/src/handler/map_in.rs +++ b/swarm/src/handler/map_in.rs @@ -52,7 +52,6 @@ where { type FromBehaviour = TNewIn; type ToBehaviour = TConnectionHandler::ToBehaviour; - type Error = TConnectionHandler::Error; type InboundProtocol = TConnectionHandler::InboundProtocol; type OutboundProtocol = TConnectionHandler::OutboundProtocol; type InboundOpenInfo = TConnectionHandler::InboundOpenInfo; diff --git a/swarm/src/handler/map_out.rs b/swarm/src/handler/map_out.rs index 922228bfa49..8ef8bad61b3 100644 --- a/swarm/src/handler/map_out.rs +++ b/swarm/src/handler/map_out.rs @@ -47,7 +47,6 @@ where { type FromBehaviour = TConnectionHandler::FromBehaviour; type ToBehaviour = TNewOut; - type Error = TConnectionHandler::Error; type InboundProtocol = TConnectionHandler::InboundProtocol; type OutboundProtocol = TConnectionHandler::OutboundProtocol; type InboundOpenInfo = TConnectionHandler::InboundOpenInfo; diff --git a/swarm/src/handler/multi.rs b/swarm/src/handler/multi.rs index dce7086fb16..c6e80b4ed71 100644 --- a/swarm/src/handler/multi.rs +++ b/swarm/src/handler/multi.rs @@ -111,7 +111,6 @@ where { type FromBehaviour = (K, ::FromBehaviour); type ToBehaviour = (K, ::ToBehaviour); - type Error = ::Error; type InboundProtocol = Upgrade::InboundProtocol>; type OutboundProtocol = ::OutboundProtocol; type InboundOpenInfo = Info::InboundOpenInfo>; diff --git a/swarm/src/handler/one_shot.rs b/swarm/src/handler/one_shot.rs index e2bdbab612b..b1fc41e9098 100644 --- a/swarm/src/handler/one_shot.rs +++ b/swarm/src/handler/one_shot.rs @@ -115,7 +115,6 @@ where { type FromBehaviour = TOutbound; type ToBehaviour = Result>; - type Error = void::Void; type InboundProtocol = TInbound; type OutboundProtocol = TOutbound; type OutboundOpenInfo = (); diff --git a/swarm/src/handler/pending.rs b/swarm/src/handler/pending.rs index 170b6c54544..23b9adcfd90 100644 --- a/swarm/src/handler/pending.rs +++ b/swarm/src/handler/pending.rs @@ -42,7 +42,6 @@ impl PendingConnectionHandler { impl ConnectionHandler for PendingConnectionHandler { type FromBehaviour = Void; type ToBehaviour = Void; - type Error = Void; type InboundProtocol = PendingUpgrade; type OutboundProtocol = PendingUpgrade; type OutboundOpenInfo = Void; diff --git a/swarm/src/handler/select.rs b/swarm/src/handler/select.rs index b5d1b4bab6d..fc470ff803e 100644 --- a/swarm/src/handler/select.rs +++ b/swarm/src/handler/select.rs @@ -181,7 +181,6 @@ where { type FromBehaviour = Either; type ToBehaviour = Either; - type Error = Either; type InboundProtocol = SelectUpgrade< SendWrapper<::InboundProtocol>, SendWrapper<::InboundProtocol>, From e27ec141328c794b4d82c9d76891a033a54357f9 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sun, 29 Oct 2023 13:57:14 +1100 Subject: [PATCH 04/10] Make it compile --- protocols/dcutr/src/handler/relayed.rs | 12 ++---------- protocols/perf/src/client/handler.rs | 3 --- protocols/perf/src/server/handler.rs | 1 - protocols/relay/src/behaviour/handler.rs | 20 +++++--------------- protocols/relay/src/priv_client/handler.rs | 22 ++++++---------------- swarm/src/connection.rs | 6 +----- swarm/tests/connection_close.rs | 1 - 7 files changed, 14 insertions(+), 51 deletions(-) diff --git a/protocols/dcutr/src/handler/relayed.rs b/protocols/dcutr/src/handler/relayed.rs index 54e7c0f0f3a..2a3efc171d7 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -259,12 +259,6 @@ impl ConnectionHandler for Handler { ) -> Poll< ConnectionHandlerEvent, > { - // Check for a pending (fatal) error. - if let Some(err) = self.pending_error.take() { - // The handler will not be polled again by the `Swarm`. - return Poll::Ready(ConnectionHandlerEvent::Close(err)); - } - // Return queued events. if let Some(event) = self.queued_events.pop_front() { return Poll::Ready(event); @@ -278,10 +272,8 @@ impl ConnectionHandler for Handler { Event::InboundConnectNegotiated(addresses), )); } - Err(e) => { - return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Apply( - Either::Left(e), - ))) + Err(_) => { + todo!() } } } diff --git a/protocols/perf/src/client/handler.rs b/protocols/perf/src/client/handler.rs index 8e93ccec0bc..196a1c5966e 100644 --- a/protocols/perf/src/client/handler.rs +++ b/protocols/perf/src/client/handler.rs @@ -35,7 +35,6 @@ use libp2p_swarm::{ }, ConnectionHandler, ConnectionHandlerEvent, StreamProtocol, SubstreamProtocol, }; -use void::Void; use crate::client::{RunError, RunId}; use crate::{RunParams, RunUpdate}; @@ -59,7 +58,6 @@ pub struct Handler { ::OutboundProtocol, ::OutboundOpenInfo, ::ToBehaviour, - ::Error, >, >, @@ -87,7 +85,6 @@ impl Default for Handler { impl ConnectionHandler for Handler { type FromBehaviour = Command; type ToBehaviour = Event; - type Error = Void; type InboundProtocol = DeniedUpgrade; type OutboundProtocol = ReadyUpgrade; type OutboundOpenInfo = (); diff --git a/protocols/perf/src/server/handler.rs b/protocols/perf/src/server/handler.rs index 5eb44bc6a93..59ab907578b 100644 --- a/protocols/perf/src/server/handler.rs +++ b/protocols/perf/src/server/handler.rs @@ -63,7 +63,6 @@ impl Default for Handler { impl ConnectionHandler for Handler { type FromBehaviour = Void; type ToBehaviour = Event; - type Error = Void; type InboundProtocol = ReadyUpgrade; type OutboundProtocol = DeniedUpgrade; type OutboundOpenInfo = Void; diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index ec7d62c3378..4cc93885f83 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -621,12 +621,6 @@ impl ConnectionHandler for Handler { ) -> Poll< ConnectionHandlerEvent, > { - // Check for a pending (fatal) error. - if let Some(err) = self.pending_error.take() { - // The handler will not be polled again by the `Swarm`. - return Poll::Ready(ConnectionHandlerEvent::Close(err)); - } - // Return queued events. if let Some(event) = self.queued_events.pop_front() { return Poll::Ready(event); @@ -703,17 +697,13 @@ impl ConnectionHandler for Handler { )); } Poll::Ready(Err(futures_bounded::Timeout { .. })) => { - return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Timeout)); + todo!() } - Poll::Ready(Ok(Either::Left(Err(e)))) => { - return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Apply( - Either::Left(e), - ))); + Poll::Ready(Ok(Either::Left(Err(_)))) => { + todo!() } - Poll::Ready(Ok(Either::Right(Err(e)))) => { - return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Apply( - Either::Right(e), - ))); + Poll::Ready(Ok(Either::Right(Err(_)))) => { + todo!() } Poll::Pending => {} } diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index 76ff7ebf680..55f2c99b1c7 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -325,12 +325,6 @@ impl ConnectionHandler for Handler { ) -> Poll< ConnectionHandlerEvent, > { - // Check for a pending (fatal) error. - if let Some(err) = self.pending_error.take() { - // The handler will not be polled again by the `Swarm`. - return Poll::Ready(ConnectionHandlerEvent::Close(err)); - } - // Inbound circuits loop { match self.outbound_circuits.poll_unpin(cx) { @@ -372,13 +366,11 @@ impl ConnectionHandler for Handler { }, )); } - Poll::Ready(Ok(Err(e))) => { - return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Apply( - Either::Right(e), - ))) + Poll::Ready(Ok(Err(_))) => { + todo!() } Poll::Ready(Err(Timeout { .. })) => { - return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Timeout)); + todo!() } Poll::Pending => break, } @@ -393,7 +385,7 @@ impl ConnectionHandler for Handler { let res = match worker_res { Ok(r) => r, Err(Timeout { .. }) => { - return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Timeout)); + todo!() } }; @@ -424,10 +416,8 @@ impl ConnectionHandler for Handler { self.insert_to_deny_futs(circuit); } }, - Err(e) => { - return Poll::Ready(ConnectionHandlerEvent::Close(StreamUpgradeError::Apply( - Either::Left(e), - ))); + Err(_) => { + todo!() } } } diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index a4c00ef2d5e..2380653bd18 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -1099,7 +1099,7 @@ mod tests { #[derive(Default)] struct ConfigurableProtocolConnectionHandler { - events: Vec>, + events: Vec>, active_protocols: HashSet, local_added: Vec>, local_removed: Vec>, @@ -1134,7 +1134,6 @@ mod tests { impl ConnectionHandler for MockConnectionHandler { type FromBehaviour = Void; type ToBehaviour = Void; - type Error = Void; type InboundProtocol = DeniedUpgrade; type OutboundProtocol = DeniedUpgrade; type InboundOpenInfo = (); @@ -1190,7 +1189,6 @@ mod tests { Self::OutboundProtocol, Self::OutboundOpenInfo, Self::ToBehaviour, - Self::Error, >, > { if self.outbound_requested { @@ -1208,7 +1206,6 @@ mod tests { impl ConnectionHandler for ConfigurableProtocolConnectionHandler { type FromBehaviour = Void; type ToBehaviour = Void; - type Error = Void; type InboundProtocol = ManyProtocolsUpgrade; type OutboundProtocol = DeniedUpgrade; type InboundOpenInfo = (); @@ -1267,7 +1264,6 @@ mod tests { Self::OutboundProtocol, Self::OutboundOpenInfo, Self::ToBehaviour, - Self::Error, >, > { if let Some(event) = self.events.pop() { diff --git a/swarm/tests/connection_close.rs b/swarm/tests/connection_close.rs index 0b99df80a64..397afe85cb4 100644 --- a/swarm/tests/connection_close.rs +++ b/swarm/tests/connection_close.rs @@ -96,7 +96,6 @@ impl NetworkBehaviour for Behaviour { impl ConnectionHandler for HandlerWithState { type FromBehaviour = Void; type ToBehaviour = u64; - type Error = Void; type InboundProtocol = DeniedUpgrade; type OutboundProtocol = DeniedUpgrade; type InboundOpenInfo = (); From f9f268ae7832b0fdd738c38dd17e4292361ff803 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sun, 29 Oct 2023 13:59:11 +1100 Subject: [PATCH 05/10] Add changelog entry --- swarm/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index df85fbdcd49..67e971e5ed1 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -5,6 +5,9 @@ See [PR 4076](https://github.com/libp2p/rust-libp2p/pull/4076). - Remove deprecated `PollParameters` from `NetworkBehaviour::poll` function. See [PR 4490](https://github.com/libp2p/rust-libp2p/pull/4490). +- Remove deprecated `ConnectionHandlerEvent::Close` and `ConnectionHandler::Error`. + `ConnectionHandler`s should not close connections directly as the connection might still be in use by other handlers. + See [PR XXXX](https://github.com/libp2p/rust-libp2p/pull/XXXX). - Add `PeerCondition::DisconnectedAndNotDialing` variant, combining pre-existing conditions. This is the new default. A new dialing attempt is iniated _only if_ the peer is both considered disconnected and there is currently no ongoing dialing attempt. From 7fce69baff4b0d3a58f07345519ded6f6e8d7f78 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sun, 29 Oct 2023 14:07:12 +1100 Subject: [PATCH 06/10] Remove trait constraint from `PoolEvent` --- swarm/src/connection/pool.rs | 6 +++--- swarm/src/lib.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/swarm/src/connection/pool.rs b/swarm/src/connection/pool.rs index a5381797dc6..a09bd4e1507 100644 --- a/swarm/src/connection/pool.rs +++ b/swarm/src/connection/pool.rs @@ -225,7 +225,7 @@ impl fmt::Debug for Pool { /// Event that can happen on the `Pool`. #[derive(Debug)] -pub(crate) enum PoolEvent { +pub(crate) enum PoolEvent { /// A new connection has been established. ConnectionEstablished { id: ConnectionId, @@ -289,7 +289,7 @@ pub(crate) enum PoolEvent { id: ConnectionId, peer_id: PeerId, /// The produced event. - event: THandler::ToBehaviour, + event: ToBehaviour, }, /// The connection to a node has changed its address. @@ -534,7 +534,7 @@ where } /// Polls the connection pool for events. - pub(crate) fn poll(&mut self, cx: &mut Context<'_>) -> Poll> + pub(crate) fn poll(&mut self, cx: &mut Context<'_>) -> Poll> where THandler: ConnectionHandler + 'static, ::OutboundOpenInfo: Send, diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index aba77674506..52cb790342d 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -662,7 +662,7 @@ where fn handle_pool_event( &mut self, - event: PoolEvent>, + event: PoolEvent>, ) -> Option> { match event { PoolEvent::ConnectionEstablished { From 7c3cc381506a6add4129fd8414213e8e8631aa0f Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sun, 29 Oct 2023 14:08:24 +1100 Subject: [PATCH 07/10] Remove trait constraint from `EstablishedConnectionEvent` --- swarm/src/connection/pool.rs | 2 +- swarm/src/connection/pool/task.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/swarm/src/connection/pool.rs b/swarm/src/connection/pool.rs index a09bd4e1507..6da58dea607 100644 --- a/swarm/src/connection/pool.rs +++ b/swarm/src/connection/pool.rs @@ -131,7 +131,7 @@ where /// Receivers for events reported from established connections. established_connection_events: - SelectAll>>, + SelectAll>>, /// Receivers for [`NewConnection`] objects that are dropped. new_connection_dropped_listeners: FuturesUnordered>, diff --git a/swarm/src/connection/pool/task.rs b/swarm/src/connection/pool/task.rs index 96989292b16..08674fd2ee5 100644 --- a/swarm/src/connection/pool/task.rs +++ b/swarm/src/connection/pool/task.rs @@ -66,7 +66,7 @@ pub(crate) enum PendingConnectionEvent { } #[derive(Debug)] -pub(crate) enum EstablishedConnectionEvent { +pub(crate) enum EstablishedConnectionEvent { /// A node we are connected to has changed its address. AddressChange { id: ConnectionId, @@ -77,7 +77,7 @@ pub(crate) enum EstablishedConnectionEvent { Notify { id: ConnectionId, peer_id: PeerId, - event: THandler::ToBehaviour, + event: ToBehaviour, }, /// A connection closed, possibly due to an error. /// @@ -171,7 +171,7 @@ pub(crate) async fn new_for_established_connection( peer_id: PeerId, mut connection: crate::connection::Connection, mut command_receiver: mpsc::Receiver>, - mut events: mpsc::Sender>, + mut events: mpsc::Sender>, ) where THandler: ConnectionHandler, { From c773cd09a130d18f024649f72841d6b4783983df Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sun, 29 Oct 2023 14:13:48 +1100 Subject: [PATCH 08/10] Update docs --- protocols/relay/src/priv_client/handler.rs | 3 +-- swarm/src/behaviour.rs | 15 ++++++--------- swarm/src/lib.rs | 8 ++------ 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index 55f2c99b1c7..418fa283116 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -157,8 +157,7 @@ pub struct Handler { /// Futures that try to send errors to the transport. /// - /// We may drop errors if this handler ends up in a terminal state (by returning - /// [`ConnectionHandlerEvent::Close`]). + /// We may drop errors if this handler ends up in a terminal state. send_error_futs: FuturesUnordered>, } diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index c89796f8e25..6dc0c68965d 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -284,16 +284,13 @@ pub enum ToSwarm { /// This address will be shared with all [`NetworkBehaviour`]s via [`FromSwarm::ExternalAddrExpired`]. ExternalAddrExpired(Multiaddr), - /// Instructs the `Swarm` to initiate a graceful close of one or all connections - /// with the given peer. + /// Instructs the `Swarm` to initiate a graceful close of one or all connections with the given peer. /// - /// Note: Closing a connection via - /// [`ToSwarm::CloseConnection`] does not inform the - /// corresponding [`ConnectionHandler`]. - /// Closing a connection via a [`ConnectionHandler`] can be done - /// either in a collaborative manner across [`ConnectionHandler`]s - /// with [`ConnectionHandler::connection_keep_alive`] or directly with - /// [`ConnectionHandlerEvent::Close`](crate::ConnectionHandlerEvent::Close). + /// Closing a connection via [`ToSwarm::CloseConnection`] will poll [`ConnectionHandler::poll_close`] to completion. + /// In most cases, stopping to "use" a connection is enough to have it closed. + /// The keep-alive algorithm will close a connection automatically once all [`ConnectionHandler`]s are idle. + /// + /// Use this command if you want to close a connection _despite_ it still being in use by one or more handlers. CloseConnection { /// The peer to disconnect. peer_id: PeerId, diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 52cb790342d..105b7b9b164 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -604,12 +604,8 @@ where /// /// Returns `Ok(())` if there was one or more established connections to the peer. /// - /// Note: Closing a connection via [`Swarm::disconnect_peer_id`] does - /// not inform the corresponding [`ConnectionHandler`]. - /// Closing a connection via a [`ConnectionHandler`] can be done either in a - /// collaborative manner across [`ConnectionHandler`]s - /// with [`ConnectionHandler::connection_keep_alive`] or directly with - /// [`ConnectionHandlerEvent::Close`]. + /// Closing a connection via [`Swarm::disconnect_peer_id`] will poll [`ConnectionHandler::poll_close`] to completion. + /// Use this function if you want to close a connection _despite_ it still being in use by one or more handlers. #[allow(clippy::result_unit_err)] pub fn disconnect_peer_id(&mut self, peer_id: PeerId) -> Result<(), ()> { let was_connected = self.pool.is_connected(peer_id); From dce7a16a83522039449b9a09fca128f78af5c53b Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 1 Nov 2023 12:35:16 +1100 Subject: [PATCH 09/10] Fix compile errors --- protocols/dcutr/src/handler/relayed.rs | 5 ++--- protocols/relay/src/priv_client/handler.rs | 1 - swarm/src/lib.rs | 4 ++-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/protocols/dcutr/src/handler/relayed.rs b/protocols/dcutr/src/handler/relayed.rs index 9c56734c79b..e5aa7cb82e1 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -40,7 +40,6 @@ use std::collections::VecDeque; use std::io; use std::task::{Context, Poll}; use std::time::Duration; -use void::Void; #[derive(Debug)] pub enum Command { @@ -181,8 +180,8 @@ impl Handler { impl ConnectionHandler for Handler { type FromBehaviour = Command; type ToBehaviour = Event; - type InboundProtocol = Either; - type OutboundProtocol = protocol::outbound::Upgrade; + type InboundProtocol = Either, DeniedUpgrade>; + type OutboundProtocol = ReadyUpgrade; type OutboundOpenInfo = (); type InboundOpenInfo = (); diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index c38bff1bd58..65aa66f2af0 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -21,7 +21,6 @@ use crate::priv_client::transport; use crate::protocol::{self, inbound_stop, outbound_hop}; use crate::{priv_client, proto, HOP_PROTOCOL_NAME, STOP_PROTOCOL_NAME}; -use either::Either; use futures::channel::{mpsc, oneshot}; use futures::future::FutureExt; use futures_timer::Delay; diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index e0569c377a9..8f08387ecdd 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -344,7 +344,7 @@ where /// can be polled again. pending_handler_event: Option<(PeerId, PendingNotifyHandler, THandlerInEvent)>, - pending_swarm_events: VecDeque>>, + pending_swarm_events: VecDeque>, } impl Unpin for Swarm where TBehaviour: NetworkBehaviour {} @@ -665,7 +665,7 @@ where &mut self.behaviour } - fn handle_pool_event(&mut self, event: PoolEvent>) { + fn handle_pool_event(&mut self, event: PoolEvent>) { match event { PoolEvent::ConnectionEstablished { peer_id, From e5085f17b24d7ea6427c9d212d7cf36b937a0286 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 2 Nov 2023 06:32:14 +1100 Subject: [PATCH 10/10] Update swarm/CHANGELOG.md --- swarm/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index fef25cb4198..75b79aa548e 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -7,7 +7,7 @@ See [PR 4490](https://github.com/libp2p/rust-libp2p/pull/4490). - Remove deprecated `ConnectionHandlerEvent::Close` and `ConnectionHandler::Error`. `ConnectionHandler`s should not close connections directly as the connection might still be in use by other handlers. - See [PR XXXX](https://github.com/libp2p/rust-libp2p/pull/XXXX). + See [PR 4755](https://github.com/libp2p/rust-libp2p/pull/4755). - Add `PeerCondition::DisconnectedAndNotDialing` variant, combining pre-existing conditions. This is the new default. A new dialing attempt is iniated _only if_ the peer is both considered disconnected and there is currently no ongoing dialing attempt.