From d36a3a801ec65ddb35990be0c11457b9456fd81b Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 10 May 2022 23:35:27 -0500 Subject: [PATCH 1/7] swarm/: Rename references of protocol handler to connection handler --- swarm/src/behaviour/toggle.rs | 4 ++-- swarm/src/connection.rs | 2 +- swarm/src/connection/handler_wrapper.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/swarm/src/behaviour/toggle.rs b/swarm/src/behaviour/toggle.rs index 70eec9665b2..00d7587478b 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -435,8 +435,8 @@ mod tests { /// 2. When combining [`ConnectionHandler`] implementations a single /// [`ConnectionHandler`] might be notified of an inbound upgrade error /// unrelated to its own upgrade logic. For example when nesting a - /// [`ToggleProtoHandler`] in a - /// [`ConnectionHandlerSelect`](crate::protocols_handler::ConnectionHandlerSelect) + /// [`ToggleConnectionHandler`] in a + /// [`ConnectionHandlerSelect`](crate::connection_handler::ConnectionHandlerSelect) /// the former might receive an inbound upgrade error even when disabled. /// /// [`ToggleProtoHandler`] should ignore the error in both of these cases. diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index c058833d099..9215b999671 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -113,7 +113,7 @@ where /// Begins an orderly shutdown of the connection, returning the connection /// handler and a `Future` that resolves when connection shutdown is complete. pub fn close(self) -> (THandler, Close) { - (self.handler.into_protocols_handler(), self.muxing.close().0) + (self.handler.into_connection_handler(), self.muxing.close().0) } /// Polls the handler and the substream, forwarding events from the former to the latter and diff --git a/swarm/src/connection/handler_wrapper.rs b/swarm/src/connection/handler_wrapper.rs index ad487913729..f0b4946197f 100644 --- a/swarm/src/connection/handler_wrapper.rs +++ b/swarm/src/connection/handler_wrapper.rs @@ -110,7 +110,7 @@ impl HandlerWrapper { } } - pub(crate) fn into_protocols_handler(self) -> TProtoHandler { + pub(crate) fn into_connection_handler(self) -> TConnectionHandler { self.handler } } From d717299d3d7632a1b812c759767c7a18e84eafe2 Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 10 May 2022 23:35:48 -0500 Subject: [PATCH 2/7] swarm-derive/: Rename references of protocol handler to connection handler --- swarm-derive/src/lib.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index 2ee311057ec..e81bd8ae4d1 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -50,8 +50,8 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { let net_behv_event_proc = quote! {::libp2p::swarm::NetworkBehaviourEventProcess}; let either_ident = quote! {::libp2p::core::either::EitherOutput}; let network_behaviour_action = quote! {::libp2p::swarm::NetworkBehaviourAction}; - let into_protocols_handler = quote! {::libp2p::swarm::IntoConnectionHandler}; - let protocols_handler = quote! {::libp2p::swarm::ConnectionHandler}; + let into_connection_handler = quote! {::libp2p::swarm::IntoConnectionHandler}; + let connection_handler = quote! {::libp2p::swarm::ConnectionHandler}; let into_proto_select_ident = quote! {::libp2p::swarm::IntoConnectionHandlerSelect}; let peer_id = quote! {::libp2p::core::PeerId}; let connection_id = quote! {::libp2p::core::connection::ConnectionId}; @@ -371,7 +371,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { }); // The [`ConnectionHandler`] associated type. - let protocols_handler_ty = { + let connection_handler_ty = { let mut ph_ty = None; for field in data_struct_fields.iter() { let ty = &field.ty; @@ -402,7 +402,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { match out_handler { Some(h) => { - out_handler = Some(quote! { #into_protocols_handler::select(#h, #builder) }) + out_handler = Some(quote! { #into_connection_handler::select(#h, #builder) }) } ref mut h @ None => *h = Some(builder), } @@ -476,7 +476,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { match out_handler { Some(h) => { - out_handler = Some(quote! { #into_protocols_handler::select(#h, #builder) }) + out_handler = Some(quote! { #into_connection_handler::select(#h, #builder) }) } ref mut h @ None => *h = Some(builder), } @@ -530,11 +530,11 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { impl #impl_generics #trait_to_impl for #name #ty_generics #where_clause { - type ConnectionHandler = #protocols_handler_ty; + type ConnectionHandler = #connection_handler_ty; type OutEvent = #out_event; fn new_handler(&mut self) -> Self::ConnectionHandler { - use #into_protocols_handler; + use #into_connection_handler; #new_handler } @@ -552,7 +552,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { #(#inject_address_change_stmts);* } - fn inject_connection_closed(&mut self, peer_id: &#peer_id, connection_id: &#connection_id, endpoint: &#connected_point, handlers: ::Handler, remaining_established: usize) { + fn inject_connection_closed(&mut self, peer_id: &#peer_id, connection_id: &#connection_id, endpoint: &#connected_point, handlers: ::Handler, remaining_established: usize) { #(#inject_connection_closed_stmts);* } @@ -596,7 +596,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { &mut self, peer_id: #peer_id, connection_id: #connection_id, - event: <::Handler as #protocols_handler>::OutEvent + event: <::Handler as #connection_handler>::OutEvent ) { match event { #(#inject_node_event_stmts),* From bb80f3f387ecb54bd09167b47efdbf18de2423ac Mon Sep 17 00:00:00 2001 From: chad Date: Wed, 11 May 2022 12:40:55 -0500 Subject: [PATCH 3/7] swarm/: Rename references of TProtoHandler, ToggleProtoHandler and ToggleIntoProtoHandler --- swarm/src/behaviour/toggle.rs | 28 +++++++++--------- swarm/src/connection.rs | 5 +++- swarm/src/connection/handler_wrapper.rs | 38 ++++++++++++------------- swarm/src/handler/map_in.rs | 27 +++++++++--------- swarm/src/handler/map_out.rs | 26 ++++++++--------- 5 files changed, 64 insertions(+), 60 deletions(-) diff --git a/swarm/src/behaviour/toggle.rs b/swarm/src/behaviour/toggle.rs index 00d7587478b..25183b932c7 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -70,11 +70,11 @@ impl NetworkBehaviour for Toggle where TBehaviour: NetworkBehaviour, { - type ConnectionHandler = ToggleIntoProtoHandler; + type ConnectionHandler = ToggleIntoConnectionHandler; type OutEvent = TBehaviour::OutEvent; fn new_handler(&mut self) -> Self::ConnectionHandler { - ToggleIntoProtoHandler { + ToggleIntoConnectionHandler { inner: self.inner.as_mut().map(|i| i.new_handler()), } } @@ -223,9 +223,9 @@ where params: &mut impl PollParameters, ) -> Poll> { if let Some(inner) = self.inner.as_mut() { - inner - .poll(cx, params) - .map(|action| action.map_handler(|h| ToggleIntoProtoHandler { inner: Some(h) })) + inner.poll(cx, params).map(|action| { + action.map_handler(|h| ToggleIntoConnectionHandler { inner: Some(h) }) + }) } else { Poll::Pending } @@ -244,22 +244,22 @@ where } /// Implementation of `IntoConnectionHandler` that can be in the disabled state. -pub struct ToggleIntoProtoHandler { +pub struct ToggleIntoConnectionHandler { inner: Option, } -impl IntoConnectionHandler for ToggleIntoProtoHandler +impl IntoConnectionHandler for ToggleIntoConnectionHandler where TInner: IntoConnectionHandler, { - type Handler = ToggleProtoHandler; + type Handler = ToggleConnectionHandler; fn into_handler( self, remote_peer_id: &PeerId, connected_point: &ConnectedPoint, ) -> Self::Handler { - ToggleProtoHandler { + ToggleConnectionHandler { inner: self .inner .map(|h| h.into_handler(remote_peer_id, connected_point)), @@ -276,11 +276,11 @@ where } /// Implementation of [`ConnectionHandler`] that can be in the disabled state. -pub struct ToggleProtoHandler { +pub struct ToggleConnectionHandler { inner: Option, } -impl ConnectionHandler for ToggleProtoHandler +impl ConnectionHandler for ToggleConnectionHandler where TInner: ConnectionHandler, { @@ -426,7 +426,7 @@ mod tests { use super::*; use crate::handler::DummyConnectionHandler; - /// A disabled [`ToggleProtoHandler`] can receive listen upgrade errors in + /// A disabled [`ToggleConnectionHandler`] can receive listen upgrade errors in /// the following two cases: /// /// 1. Protocol negotiation on an incoming stream failed with no protocol @@ -439,10 +439,10 @@ mod tests { /// [`ConnectionHandlerSelect`](crate::connection_handler::ConnectionHandlerSelect) /// the former might receive an inbound upgrade error even when disabled. /// - /// [`ToggleProtoHandler`] should ignore the error in both of these cases. + /// [`ToggleConnectionHandler`] should ignore the error in both of these cases. #[test] fn ignore_listen_upgrade_error_when_disabled() { - let mut handler = ToggleProtoHandler:: { inner: None }; + let mut handler = ToggleConnectionHandler:: { inner: None }; handler.inject_listen_upgrade_error(Either::Right(()), ConnectionHandlerUpgrErr::Timeout); } diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index 9215b999671..42c29239cdc 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -113,7 +113,10 @@ where /// Begins an orderly shutdown of the connection, returning the connection /// handler and a `Future` that resolves when connection shutdown is complete. pub fn close(self) -> (THandler, Close) { - (self.handler.into_connection_handler(), self.muxing.close().0) + ( + self.handler.into_connection_handler(), + self.muxing.close().0, + ) } /// Polls the handler and the substream, forwarding events from the former to the latter and diff --git a/swarm/src/connection/handler_wrapper.rs b/swarm/src/connection/handler_wrapper.rs index f0b4946197f..13bd22c1715 100644 --- a/swarm/src/connection/handler_wrapper.rs +++ b/swarm/src/connection/handler_wrapper.rs @@ -42,35 +42,35 @@ use std::{error, fmt, pin::Pin, task::Context, task::Poll, time::Duration}; /// - Driving substream upgrades /// - Handling connection timeout // TODO: add a caching system for protocols that are supported or not -pub struct HandlerWrapper +pub struct HandlerWrapper where - TProtoHandler: ConnectionHandler, + TConnectionHandler: ConnectionHandler, { /// The underlying handler. - handler: TProtoHandler, + handler: TConnectionHandler, /// Futures that upgrade incoming substreams. negotiating_in: FuturesUnordered< SubstreamUpgrade< - TProtoHandler::InboundOpenInfo, + TConnectionHandler::InboundOpenInfo, InboundUpgradeApply< Substream, - SendWrapper, + SendWrapper, >, >, >, /// Futures that upgrade outgoing substreams. negotiating_out: FuturesUnordered< SubstreamUpgrade< - TProtoHandler::OutboundOpenInfo, + TConnectionHandler::OutboundOpenInfo, OutboundUpgradeApply< Substream, - SendWrapper, + SendWrapper, >, >, >, /// For each outbound substream request, how to upgrade it. The first element of the tuple /// is the unique identifier (see `unique_dial_upgrade_id`). - queued_dial_upgrades: Vec<(u64, SendWrapper)>, + queued_dial_upgrades: Vec<(u64, SendWrapper)>, /// Unique identifier assigned to each queued dial upgrade. unique_dial_upgrade_id: u64, /// The currently planned connection & handler shutdown. @@ -79,7 +79,7 @@ where substream_upgrade_protocol_override: Option, } -impl std::fmt::Debug for HandlerWrapper { +impl std::fmt::Debug for HandlerWrapper { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("HandlerWrapper") .field("negotiating_in", &self.negotiating_in) @@ -94,9 +94,9 @@ impl std::fmt::Debug for HandlerWrapper HandlerWrapper { +impl HandlerWrapper { pub(crate) fn new( - handler: TProtoHandler, + handler: TConnectionHandler, substream_upgrade_protocol_override: Option, ) -> Self { Self { @@ -224,22 +224,22 @@ where } } -pub type OutboundOpenInfo = ( +pub type OutboundOpenInfo = ( u64, - ::OutboundOpenInfo, + ::OutboundOpenInfo, Duration, ); -impl HandlerWrapper +impl HandlerWrapper where - TProtoHandler: ConnectionHandler, + TConnectionHandler: ConnectionHandler, { pub fn inject_substream( &mut self, substream: Substream, // The first element of the tuple is the unique upgrade identifier // (see `unique_dial_upgrade_id`). - endpoint: SubstreamEndpoint>, + endpoint: SubstreamEndpoint>, ) { match endpoint { SubstreamEndpoint::Listener => { @@ -290,7 +290,7 @@ where } } - pub fn inject_event(&mut self, event: TProtoHandler::InEvent) { + pub fn inject_event(&mut self, event: TConnectionHandler::InEvent) { self.handler.inject_event(event); } @@ -303,8 +303,8 @@ where cx: &mut Context<'_>, ) -> Poll< Result< - Event, TProtoHandler::OutEvent>, - Error, + Event, TConnectionHandler::OutEvent>, + Error, >, > { while let Poll::Ready(Some((user_data, res))) = self.negotiating_in.poll_next_unpin(cx) { diff --git a/swarm/src/handler/map_in.rs b/swarm/src/handler/map_in.rs index 96575bbcb58..a209225045e 100644 --- a/swarm/src/handler/map_in.rs +++ b/swarm/src/handler/map_in.rs @@ -27,15 +27,15 @@ use libp2p_core::Multiaddr; use std::{fmt::Debug, marker::PhantomData, task::Context, task::Poll}; /// Wrapper around a protocol handler that turns the input event into something else. -pub struct MapInEvent { - inner: TProtoHandler, +pub struct MapInEvent { + inner: TConnectionHandler, map: TMap, marker: PhantomData, } -impl MapInEvent { +impl MapInEvent { /// Creates a `MapInEvent`. - pub(crate) fn new(inner: TProtoHandler, map: TMap) -> Self { + pub(crate) fn new(inner: TConnectionHandler, map: TMap) -> Self { MapInEvent { inner, map, @@ -44,20 +44,21 @@ impl MapInEvent { } } -impl ConnectionHandler for MapInEvent +impl ConnectionHandler + for MapInEvent where - TProtoHandler: ConnectionHandler, - TMap: Fn(TNewIn) -> Option, + TConnectionHandler: ConnectionHandler, + TMap: Fn(TNewIn) -> Option, TNewIn: Debug + Send + 'static, TMap: Send + 'static, { type InEvent = TNewIn; - type OutEvent = TProtoHandler::OutEvent; - type Error = TProtoHandler::Error; - type InboundProtocol = TProtoHandler::InboundProtocol; - type OutboundProtocol = TProtoHandler::OutboundProtocol; - type InboundOpenInfo = TProtoHandler::InboundOpenInfo; - type OutboundOpenInfo = TProtoHandler::OutboundOpenInfo; + type OutEvent = TConnectionHandler::OutEvent; + type Error = TConnectionHandler::Error; + type InboundProtocol = TConnectionHandler::InboundProtocol; + type OutboundProtocol = TConnectionHandler::OutboundProtocol; + type InboundOpenInfo = TConnectionHandler::InboundOpenInfo; + type OutboundOpenInfo = TConnectionHandler::OutboundOpenInfo; fn listen_protocol(&self) -> SubstreamProtocol { self.inner.listen_protocol() diff --git a/swarm/src/handler/map_out.rs b/swarm/src/handler/map_out.rs index f92958e30b4..2eb0c2f9bdc 100644 --- a/swarm/src/handler/map_out.rs +++ b/swarm/src/handler/map_out.rs @@ -28,32 +28,32 @@ use std::fmt::Debug; use std::task::{Context, Poll}; /// Wrapper around a protocol handler that turns the output event into something else. -pub struct MapOutEvent { - inner: TProtoHandler, +pub struct MapOutEvent { + inner: TConnectionHandler, map: TMap, } -impl MapOutEvent { +impl MapOutEvent { /// Creates a `MapOutEvent`. - pub(crate) fn new(inner: TProtoHandler, map: TMap) -> Self { + pub(crate) fn new(inner: TConnectionHandler, map: TMap) -> Self { MapOutEvent { inner, map } } } -impl ConnectionHandler for MapOutEvent +impl ConnectionHandler for MapOutEvent where - TProtoHandler: ConnectionHandler, - TMap: FnMut(TProtoHandler::OutEvent) -> TNewOut, + TConnectionHandler: ConnectionHandler, + TMap: FnMut(TConnectionHandler::OutEvent) -> TNewOut, TNewOut: Debug + Send + 'static, TMap: Send + 'static, { - type InEvent = TProtoHandler::InEvent; + type InEvent = TConnectionHandler::InEvent; type OutEvent = TNewOut; - type Error = TProtoHandler::Error; - type InboundProtocol = TProtoHandler::InboundProtocol; - type OutboundProtocol = TProtoHandler::OutboundProtocol; - type InboundOpenInfo = TProtoHandler::InboundOpenInfo; - type OutboundOpenInfo = TProtoHandler::OutboundOpenInfo; + type Error = TConnectionHandler::Error; + type InboundProtocol = TConnectionHandler::InboundProtocol; + type OutboundProtocol = TConnectionHandler::OutboundProtocol; + type InboundOpenInfo = TConnectionHandler::InboundOpenInfo; + type OutboundOpenInfo = TConnectionHandler::OutboundOpenInfo; fn listen_protocol(&self) -> SubstreamProtocol { self.inner.listen_protocol() From 7c96844febfb10309f7eaf71bec0bd778eb088a2 Mon Sep 17 00:00:00 2001 From: chad Date: Mon, 16 May 2022 18:00:45 -0500 Subject: [PATCH 4/7] minor version bump to 0.27.2 (#2637) --- swarm-derive/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm-derive/Cargo.toml b/swarm-derive/Cargo.toml index 4fc0ac26b27..5f87974193c 100644 --- a/swarm-derive/Cargo.toml +++ b/swarm-derive/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-swarm-derive" edition = "2021" rust-version = "1.56.1" description = "Procedural macros of libp2p-core" -version = "0.27.1" +version = "0.27.2" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" From 98134299e4dfb92372b04f1c2a479d6cce72f0d6 Mon Sep 17 00:00:00 2001 From: chad Date: Mon, 16 May 2022 18:02:37 -0500 Subject: [PATCH 5/7] swarm-derive:/ Update change log --- swarm-derive/CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/swarm-derive/CHANGELOG.md b/swarm-derive/CHANGELOG.md index 91bf8da9625..febfa748277 100644 --- a/swarm-derive/CHANGELOG.md +++ b/swarm-derive/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.27.2 + +- Replace references of Protocol Handler with Connection Handler . See [PR 2640]. + +[PR 2640]: https://github.com/libp2p/rust-libp2p/pull/2640 + # 0.27.1 - Allow mixing of ignored fields. See [PR 2570]. From 0b50a5437502d10d633494b2adf79f57ce84100e Mon Sep 17 00:00:00 2001 From: chad Date: Mon, 16 May 2022 18:06:06 -0500 Subject: [PATCH 6/7] swarm:/ update changelog --- swarm/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 079c0a3f351..8f4b3940efa 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -8,9 +8,12 @@ - Rename `IncomingInfo::to_connected_point` to `IncomingInfo::create_connected_point`. See [PR 2620]. +- Rename `TProtoHandler` to `TConnectionHandler`, `ToggleProtoHandler` to `ToggleConnectionHandler`, `ToggleIntoProtoHandler` to `ToggleIntoConnectionHandler`. See [PR 2640]. + [PR 2529]: https://github.com/libp2p/rust-libp2p/pull/2529 [PR 2610]: https://github.com/libp2p/rust-libp2p/pull/2610 [PR 2620]: https://github.com/libp2p/rust-libp2p/pull/2620 +[PR 2640]: https://github.com/libp2p/rust-libp2p/pull/2640 # 0.35.0 From f31120c5d780718dfdfec6330a6963bbb28fe339 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Tue, 17 May 2022 22:28:40 +0200 Subject: [PATCH 7/7] Apply suggestions from code review --- swarm-derive/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swarm-derive/CHANGELOG.md b/swarm-derive/CHANGELOG.md index febfa748277..cbea63c477d 100644 --- a/swarm-derive/CHANGELOG.md +++ b/swarm-derive/CHANGELOG.md @@ -1,6 +1,6 @@ -# 0.27.2 +# 0.27.2 [unreleased] -- Replace references of Protocol Handler with Connection Handler . See [PR 2640]. +- Replace references of Protocol Handler with Connection Handler. See [PR 2640]. [PR 2640]: https://github.com/libp2p/rust-libp2p/pull/2640