Skip to content

Commit

Permalink
feat(swarm): remove handler field from ConnectionClosed event
Browse files Browse the repository at this point in the history
Instead of transferring the `ConnectionHandler` back in a event to the `NetworkBehaviour`, we introduce `ConnectionHandler::poll_close`. This function will be polled like a `Stream` and can emit events that will be delivered to the `NetworkBehaviour`.

Resolves: #3046.

Pull-Request: #4076.
  • Loading branch information
thomaseizinger authored Oct 26, 2023
1 parent 7517934 commit ddf26b9
Show file tree
Hide file tree
Showing 39 changed files with 271 additions and 217 deletions.
2 changes: 1 addition & 1 deletion misc/allow-block-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ where
Ok(dummy::ConnectionHandler)
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionClosed(_) => {}
FromSwarm::ConnectionEstablished(_) => {}
Expand Down
4 changes: 2 additions & 2 deletions misc/connection-limits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl NetworkBehaviour for Behaviour {
Ok(dummy::ConnectionHandler)
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionClosed(ConnectionClosed {
peer_id,
Expand Down Expand Up @@ -583,7 +583,7 @@ mod tests {
)))
}

fn on_swarm_event(&mut self, _event: FromSwarm<Self::ConnectionHandler>) {}
fn on_swarm_event(&mut self, _event: FromSwarm) {}

fn on_connection_handler_event(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion misc/memory-connection-limits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl NetworkBehaviour for Behaviour {
Ok(dummy::ConnectionHandler)
}

fn on_swarm_event(&mut self, _: FromSwarm<Self::ConnectionHandler>) {}
fn on_swarm_event(&mut self, _: FromSwarm) {}

fn on_connection_handler_event(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion misc/memory-connection-limits/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<const MEM_PENDING: usize, const MEM_ESTABLISHED: usize> NetworkBehaviour
Ok(dummy::ConnectionHandler)
}

fn on_swarm_event(&mut self, _: FromSwarm<Self::ConnectionHandler>) {}
fn on_swarm_event(&mut self, _: FromSwarm) {}

fn on_connection_handler_event(
&mut self,
Expand Down
6 changes: 2 additions & 4 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,14 @@ impl Behaviour {
peer_id,
connection_id,
endpoint,
handler,
remaining_established,
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
self.inner
.on_swarm_event(FromSwarm::ConnectionClosed(ConnectionClosed {
peer_id,
connection_id,
endpoint,
handler,
remaining_established,
}));

Expand Down Expand Up @@ -539,7 +537,7 @@ impl NetworkBehaviour for Behaviour {
.handle_established_outbound_connection(connection_id, peer, addr, role_override)
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
self.listen_addresses.on_swarm_event(&event);

match event {
Expand Down
4 changes: 2 additions & 2 deletions protocols/dcutr/src/behaviour_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Behaviour {
connection_id,
endpoint: connected_point,
..
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
if !connected_point.is_relayed() {
let connections = self
Expand Down Expand Up @@ -358,7 +358,7 @@ impl NetworkBehaviour for Behaviour {
Poll::Pending
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
self.external_addresses.on_swarm_event(&event);

match event {
Expand Down
4 changes: 2 additions & 2 deletions protocols/floodsub/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl Floodsub {
peer_id,
remaining_established,
..
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
if remaining_established > 0 {
// we only care about peer disconnections
Expand Down Expand Up @@ -482,7 +482,7 @@ impl NetworkBehaviour for Floodsub {
Poll::Pending
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(connection_established) => {
self.on_connection_established(connection_established)
Expand Down
4 changes: 2 additions & 2 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3091,7 +3091,7 @@ where
endpoint,
remaining_established,
..
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
// Remove IP from peer scoring system
if let Some((peer_score, ..)) = &mut self.peer_score {
Expand Down Expand Up @@ -3433,7 +3433,7 @@ where
Poll::Pending
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(connection_established) => {
self.on_connection_established(connection_established)
Expand Down
4 changes: 0 additions & 4 deletions protocols/gossipsub/src/behaviour/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// Collection of tests for the gossipsub network behaviour

use super::*;
use crate::protocol::ProtocolConfig;
use crate::subscription_filter::WhitelistSubscriptionFilter;
use crate::transform::{DataTransform, IdentityTransform};
use crate::ValidationError;
Expand Down Expand Up @@ -267,13 +266,10 @@ where
for connection_id in peer_connections.connections.clone() {
active_connections = active_connections.checked_sub(1).unwrap();

let dummy_handler = Handler::new(ProtocolConfig::default());

gs.on_swarm_event(FromSwarm::ConnectionClosed(ConnectionClosed {
peer_id: *peer_id,
connection_id,
endpoint: &fake_endpoint,
handler: dummy_handler,
remaining_established: active_connections,
}));
}
Expand Down
2 changes: 1 addition & 1 deletion protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl NetworkBehaviour for Behaviour {
Ok(self.discovered_peers.get(&peer))
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
let listen_addr_changed = self.listen_addresses.on_swarm_event(&event);
let external_addr_changed = self.external_addresses.on_swarm_event(&event);

Expand Down
4 changes: 2 additions & 2 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2009,7 +2009,7 @@ where
remaining_established,
connection_id,
..
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
self.connections.remove(&connection_id);

Expand Down Expand Up @@ -2526,7 +2526,7 @@ where
}
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
self.listen_addresses.on_swarm_event(&event);
let external_addresses_changed = self.external_addresses.on_swarm_event(&event);

Expand Down
2 changes: 1 addition & 1 deletion protocols/mdns/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ where
void::unreachable(ev)
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
self.listen_addresses
.write()
.unwrap_or_else(|e| e.into_inner())
Expand Down
3 changes: 1 addition & 2 deletions protocols/perf/src/client/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl NetworkBehaviour for Behaviour {
Ok(Handler::default())
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(ConnectionEstablished { peer_id, .. }) => {
self.connected.insert(peer_id);
Expand All @@ -115,7 +115,6 @@ impl NetworkBehaviour for Behaviour {
peer_id,
connection_id: _,
endpoint: _,
handler: _,
remaining_established,
}) => {
if remaining_established == 0 {
Expand Down
2 changes: 1 addition & 1 deletion protocols/perf/src/server/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl NetworkBehaviour for Behaviour {
Ok(Handler::default())
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(_) => {}
FromSwarm::ConnectionClosed(_) => {}
Expand Down
2 changes: 1 addition & 1 deletion protocols/ping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl NetworkBehaviour for Behaviour {
}
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(_)
| FromSwarm::ConnectionClosed(_)
Expand Down
4 changes: 2 additions & 2 deletions protocols/relay/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl Behaviour {
peer_id,
connection_id,
..
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
if let hash_map::Entry::Occupied(mut peer) = self.reservations.entry(peer_id) {
peer.get_mut().remove(&connection_id);
Expand Down Expand Up @@ -332,7 +332,7 @@ impl NetworkBehaviour for Behaviour {
)))
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
self.external_addresses.on_swarm_event(&event);

match event {
Expand Down
4 changes: 2 additions & 2 deletions protocols/relay/src/priv_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Behaviour {
connection_id,
endpoint,
..
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
if !endpoint.is_relayed() {
match self.directly_connected_peers.entry(peer_id) {
Expand Down Expand Up @@ -192,7 +192,7 @@ impl NetworkBehaviour for Behaviour {
Ok(Either::Left(handler))
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(ConnectionEstablished {
peer_id,
Expand Down
2 changes: 1 addition & 1 deletion protocols/rendezvous/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl NetworkBehaviour for Behaviour {
.on_connection_handler_event(peer_id, connection_id, event);
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
let changed = self.external_addresses.on_swarm_event(&event);

self.inner.on_swarm_event(event);
Expand Down
2 changes: 1 addition & 1 deletion protocols/rendezvous/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl NetworkBehaviour for Behaviour {
}
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
self.inner.on_swarm_event(event);
}
}
Expand Down
4 changes: 2 additions & 2 deletions protocols/request-response/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ where
connection_id,
remaining_established,
..
}: ConnectionClosed<<Self as NetworkBehaviour>::ConnectionHandler>,
}: ConnectionClosed,
) {
let connections = self
.connected
Expand Down Expand Up @@ -764,7 +764,7 @@ where
Ok(handler)
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionEstablished(_) => {}
FromSwarm::ConnectionClosed(connection_closed) => {
Expand Down
2 changes: 1 addition & 1 deletion protocols/upnp/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl NetworkBehaviour for Behaviour {
Ok(dummy::ConnectionHandler)
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::NewListenAddr(NewListenAddr {
listener_id,
Expand Down
2 changes: 2 additions & 0 deletions swarm-derive/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 0.34.0 - unreleased

- Adapt to interface changes in `libp2p-swarm`.
See [PR 4706](https://github.com/libp2p/rust-libp2p/pull/4076).

## 0.33.0

Expand Down
19 changes: 3 additions & 16 deletions swarm-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,26 +285,15 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result<Toke
data_struct
.fields
.iter()
.enumerate()
// The outmost handler belongs to the last behaviour.
.rev()
.enumerate()
.map(|(enum_n, (field_n, field))| {
let handler = if field_n == 0 {
// Given that the iterator is reversed, this is the innermost handler only.
quote! { let handler = handlers }
} else {
quote! {
let (handlers, handler) = handlers.into_inner()
}
};
.map(|(enum_n, field)| {
let inject = match field.ident {
Some(ref i) => quote! {
self.#i.on_swarm_event(#from_swarm::ConnectionClosed(#connection_closed {
peer_id,
connection_id,
endpoint,
handler,
remaining_established,
}));
},
Expand All @@ -313,14 +302,12 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result<Toke
peer_id,
connection_id,
endpoint,
handler,
remaining_established,
}));
},
};

quote! {
#handler;
#inject;
}
})
Expand Down Expand Up @@ -841,7 +828,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result<Toke
std::task::Poll::Pending
}

fn on_swarm_event(&mut self, event: #from_swarm<Self::ConnectionHandler>) {
fn on_swarm_event(&mut self, event: #from_swarm) {
match event {
#from_swarm::ConnectionEstablished(
#connection_established { peer_id, connection_id, endpoint, failed_addresses, other_established })
Expand All @@ -850,7 +837,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result<Toke
#address_change { peer_id, connection_id, old, new })
=> { #(#on_address_change_stmts)* }
#from_swarm::ConnectionClosed(
#connection_closed { peer_id, connection_id, endpoint, handler: handlers, remaining_established })
#connection_closed { peer_id, connection_id, endpoint, remaining_established })
=> { #(#on_connection_closed_stmts)* }
#from_swarm::DialFailure(
#dial_failure { peer_id, connection_id, error })
Expand Down
3 changes: 3 additions & 0 deletions swarm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 0.44.0 - unreleased

- Remove `handler` field from `ConnectionClosed`.
If you need to transfer state from a `ConnectionHandler` to its `NetworkBehaviour` when a connection closes, use `ConnectionHandler::poll_close`.
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).
- Add `PeerCondition::DisconnectedAndNotDialing` variant, combining pre-existing conditions.
Expand Down
Loading

0 comments on commit ddf26b9

Please sign in to comment.