diff --git a/Cargo.lock b/Cargo.lock index b69a700f7ca..f61f94c09a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3106,7 +3106,7 @@ dependencies = [ [[package]] name = "libp2p-swarm" -version = "0.44.0" +version = "0.44.1" dependencies = [ "async-std", "either", @@ -3139,7 +3139,7 @@ dependencies = [ [[package]] name = "libp2p-swarm-derive" -version = "0.34.0" +version = "0.34.1" dependencies = [ "heck", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 8fca61c365e..00aa66625cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -99,8 +99,8 @@ libp2p-relay = { version = "0.17.1", path = "protocols/relay" } libp2p-rendezvous = { version = "0.14.0", path = "protocols/rendezvous" } libp2p-request-response = { version = "0.26.1", path = "protocols/request-response" } libp2p-server = { version = "0.12.5", path = "misc/server" } -libp2p-swarm = { version = "0.44.0", path = "swarm" } -libp2p-swarm-derive = { version = "=0.34.0", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required. +libp2p-swarm = { version = "0.44.1", path = "swarm" } +libp2p-swarm-derive = { version = "=0.34.1", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required. libp2p-swarm-test = { version = "0.3.0", path = "swarm-test" } libp2p-tcp = { version = "0.41.0", path = "transports/tcp" } libp2p-tls = { version = "0.3.0", path = "transports/tls" } diff --git a/protocols/autonat/src/behaviour.rs b/protocols/autonat/src/behaviour.rs index 0b80a079c0f..e95163ab23f 100644 --- a/protocols/autonat/src/behaviour.rs +++ b/protocols/autonat/src/behaviour.rs @@ -35,12 +35,9 @@ use libp2p_request_response::{ self as request_response, InboundRequestId, OutboundRequestId, ProtocolSupport, ResponseChannel, }; use libp2p_swarm::{ - behaviour::{ - AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredListenAddr, - ExternalAddrExpired, FromSwarm, - }, - ConnectionDenied, ConnectionId, ListenAddresses, NetworkBehaviour, NewExternalAddrCandidate, - THandler, THandlerInEvent, THandlerOutEvent, ToSwarm, + behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}, + ConnectionDenied, ConnectionId, ListenAddresses, NetworkBehaviour, THandler, THandlerInEvent, + THandlerOutEvent, ToSwarm, }; use std::{ collections::{HashMap, HashSet, VecDeque}, @@ -363,18 +360,10 @@ impl Behaviour { ConnectionClosed { peer_id, connection_id, - endpoint, remaining_established, + .. }: ConnectionClosed, ) { - self.inner - .on_swarm_event(FromSwarm::ConnectionClosed(ConnectionClosed { - peer_id, - connection_id, - endpoint, - remaining_established, - })); - if remaining_established == 0 { self.connected.remove(&peer_id); } else { @@ -386,20 +375,7 @@ impl Behaviour { } } - fn on_dial_failure( - &mut self, - DialFailure { - peer_id, - connection_id, - error, - }: DialFailure, - ) { - self.inner - .on_swarm_event(FromSwarm::DialFailure(DialFailure { - peer_id, - connection_id, - error, - })); + fn on_dial_failure(&mut self, DialFailure { peer_id, error, .. }: DialFailure) { if let Some(event) = self.as_server().on_outbound_dial_error(peer_id, error) { self.pending_actions .push_back(ToSwarm::GenerateEvent(Event::InboundProbe(event))); @@ -542,57 +518,25 @@ impl NetworkBehaviour for Behaviour { fn on_swarm_event(&mut self, event: FromSwarm) { self.listen_addresses.on_swarm_event(&event); + self.inner.on_swarm_event(event); match event { - FromSwarm::ConnectionEstablished(connection_established) => { - self.inner - .on_swarm_event(FromSwarm::ConnectionEstablished(connection_established)); - self.on_connection_established(connection_established) - } - FromSwarm::ConnectionClosed(connection_closed) => { - self.on_connection_closed(connection_closed) - } - FromSwarm::DialFailure(dial_failure) => self.on_dial_failure(dial_failure), - FromSwarm::AddressChange(address_change) => { - self.inner - .on_swarm_event(FromSwarm::AddressChange(address_change)); - self.on_address_change(address_change) - } - listen_addr @ FromSwarm::NewListenAddr(_) => { - self.inner.on_swarm_event(listen_addr); + FromSwarm::ConnectionEstablished(e) => self.on_connection_established(e), + FromSwarm::ConnectionClosed(e) => self.on_connection_closed(e), + FromSwarm::DialFailure(e) => self.on_dial_failure(e), + FromSwarm::AddressChange(e) => self.on_address_change(e), + FromSwarm::NewListenAddr(_) => { self.as_client().on_new_address(); } - FromSwarm::ExpiredListenAddr(ExpiredListenAddr { listener_id, addr }) => { - self.inner - .on_swarm_event(FromSwarm::ExpiredListenAddr(ExpiredListenAddr { - listener_id, - addr, - })); - self.as_client().on_expired_address(addr); - } - FromSwarm::ExternalAddrExpired(ExternalAddrExpired { addr }) => { - self.inner - .on_swarm_event(FromSwarm::ExternalAddrExpired(ExternalAddrExpired { addr })); - self.as_client().on_expired_address(addr); - } - FromSwarm::NewExternalAddrCandidate(NewExternalAddrCandidate { addr }) => { - self.inner - .on_swarm_event(FromSwarm::NewExternalAddrCandidate( - NewExternalAddrCandidate { addr }, - )); - self.probe_address(addr.to_owned()); - } - listen_failure @ FromSwarm::ListenFailure(_) => { - self.inner.on_swarm_event(listen_failure) + FromSwarm::ExpiredListenAddr(e) => { + self.as_client().on_expired_address(e.addr); } - new_listener @ FromSwarm::NewListener(_) => self.inner.on_swarm_event(new_listener), - listener_error @ FromSwarm::ListenerError(_) => { - self.inner.on_swarm_event(listener_error) + FromSwarm::ExternalAddrExpired(e) => { + self.as_client().on_expired_address(e.addr); } - listener_closed @ FromSwarm::ListenerClosed(_) => { - self.inner.on_swarm_event(listener_closed) + FromSwarm::NewExternalAddrCandidate(e) => { + self.probe_address(e.addr.to_owned()); } - confirmed @ FromSwarm::ExternalAddrConfirmed(_) => self.inner.on_swarm_event(confirmed), _ => {} } } diff --git a/protocols/rendezvous/src/server.rs b/protocols/rendezvous/src/server.rs index 78aa42043cd..667c71e20e3 100644 --- a/protocols/rendezvous/src/server.rs +++ b/protocols/rendezvous/src/server.rs @@ -215,20 +215,12 @@ impl NetworkBehaviour for Behaviour { }) => { continue; } - ToSwarm::Dial { .. } - | ToSwarm::ListenOn { .. } - | ToSwarm::RemoveListener { .. } - | ToSwarm::NotifyHandler { .. } - | ToSwarm::NewExternalAddrCandidate(_) - | ToSwarm::ExternalAddrConfirmed(_) - | ToSwarm::ExternalAddrExpired(_) - | ToSwarm::CloseConnection { .. } => { - let new_to_swarm = to_swarm + other => { + let new_to_swarm = other .map_out(|_| unreachable!("we manually map `GenerateEvent` variants")); return Poll::Ready(new_to_swarm); } - _ => {} }; } diff --git a/swarm-derive/CHANGELOG.md b/swarm-derive/CHANGELOG.md index 08adba00cdb..85ea1a46048 100644 --- a/swarm-derive/CHANGELOG.md +++ b/swarm-derive/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.34.1 + +- Always forward all variants of `FromSwarm`. + See [PR 4825](https://github.com/libp2p/rust-libp2p/pull/4825). + ## 0.34.0 - Adapt to interface changes in `libp2p-swarm`. diff --git a/swarm-derive/Cargo.toml b/swarm-derive/Cargo.toml index 11cc4b6a12a..78ffd337101 100644 --- a/swarm-derive/Cargo.toml +++ b/swarm-derive/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-swarm-derive" edition = "2021" rust-version = { workspace = true } description = "Procedural macros of libp2p-swarm" -version = "0.34.0" +version = "0.34.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index 514975390b0..5c8ce93966d 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -71,19 +71,6 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result syn::Result quote! { - self.#i.on_swarm_event(#from_swarm::ConnectionEstablished(#connection_established { - peer_id, - connection_id, - endpoint, - failed_addresses, - other_established, - })); + self.#i.on_swarm_event(event); }, None => quote! { - self.#field_n.on_swarm_event(#from_swarm::ConnectionEstablished(#connection_established { - peer_id, - connection_id, - endpoint, - failed_addresses, - other_established, - })); - }, - }) - }; - - // Build the list of statements to put in the body of `on_swarm_event()` - // for the `FromSwarm::AddressChange variant`. - let on_address_change_stmts = { - data_struct - .fields - .iter() - .enumerate() - .map(|(field_n, field)| match field.ident { - Some(ref i) => quote! { - self.#i.on_swarm_event(#from_swarm::AddressChange(#address_change { - peer_id, - connection_id, - old, - new, - })); - }, - None => quote! { - self.#field_n.on_swarm_event(#from_swarm::AddressChange(#address_change { - peer_id, - connection_id, - old, - new, - })); - }, - }) - }; - - // Build the list of statements to put in the body of `on_swarm_event()` - // for the `FromSwarm::ConnectionClosed` variant. - let on_connection_closed_stmts = { - data_struct - .fields - .iter() - .rev() - .enumerate() - .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, - remaining_established, - })); - }, - None => quote! { - self.#enum_n.on_swarm_event(#from_swarm::ConnectionClosed(#connection_closed { - peer_id, - connection_id, - endpoint, - remaining_established, - })); - }, - }; - - quote! { - #inject; - } - }) - }; - - // Build the list of statements to put in the body of `on_swarm_event()` - // for the `FromSwarm::DialFailure` variant. - let on_dial_failure_stmts = data_struct - .fields - .iter() - .enumerate() - .map(|(enum_n, field)| match field.ident { - Some(ref i) => quote! { - self.#i.on_swarm_event(#from_swarm::DialFailure(#dial_failure { - peer_id, - connection_id, - error, - })); - }, - None => quote! { - self.#enum_n.on_swarm_event(#from_swarm::DialFailure(#dial_failure { - peer_id, - connection_id, - error, - })); - }, - }); - - // Build the list of statements to put in the body of `on_swarm_event()` - // for the `FromSwarm::ListenFailure` variant. - let on_listen_failure_stmts = data_struct - .fields - .iter() - .enumerate() - .map(|(enum_n, field)| match field.ident { - Some(ref i) => quote! { - self.#i.on_swarm_event(#from_swarm::ListenFailure(#listen_failure { - local_addr, - send_back_addr, - connection_id, - error - })); - }, - None => quote! { - self.#enum_n.on_swarm_event(#from_swarm::ListenFailure(#listen_failure { - local_addr, - send_back_addr, - connection_id, - error - })); - }, - }); - - // Build the list of statements to put in the body of `on_swarm_event()` - // for the `FromSwarm::NewListener` variant. - let on_new_listener_stmts = { - data_struct - .fields - .iter() - .enumerate() - .map(|(field_n, field)| match field.ident { - Some(ref i) => quote! { - self.#i.on_swarm_event(#from_swarm::NewListener(#new_listener { - listener_id, - })); - }, - None => quote! { - self.#field_n.on_swarm_event(#from_swarm::NewListener(#new_listener { - listener_id, - })); - }, - }) - }; - - // Build the list of statements to put in the body of `on_swarm_event()` - // for the `FromSwarm::NewListenAddr` variant. - let on_new_listen_addr_stmts = { - data_struct - .fields - .iter() - .enumerate() - .map(|(field_n, field)| match field.ident { - Some(ref i) => quote! { - self.#i.on_swarm_event(#from_swarm::NewListenAddr(#new_listen_addr { - listener_id, - addr, - })); - }, - None => quote! { - self.#field_n.on_swarm_event(#from_swarm::NewListenAddr(#new_listen_addr { - listener_id, - addr, - })); - }, - }) - }; - - // Build the list of statements to put in the body of `on_swarm_event()` - // for the `FromSwarm::ExpiredListenAddr` variant. - let on_expired_listen_addr_stmts = { - data_struct - .fields - .iter() - .enumerate() - .map(|(field_n, field)| match field.ident { - Some(ref i) => quote! { - self.#i.on_swarm_event(#from_swarm::ExpiredListenAddr(#expired_listen_addr { - listener_id, - addr, - })); - }, - None => quote! { - self.#field_n.on_swarm_event(#from_swarm::ExpiredListenAddr(#expired_listen_addr { - listener_id, - addr, - })); - }, - }) - }; - - // Build the list of statements to put in the body of `on_swarm_event()` - // for the `FromSwarm::NewExternalAddr` variant. - let on_new_external_addr_candidate_stmts = { - data_struct - .fields - .iter() - .enumerate() - .map(|(field_n, field)| match field.ident { - Some(ref i) => quote! { - self.#i.on_swarm_event(#from_swarm::NewExternalAddrCandidate(#new_external_addr_candidate { - addr, - })); - }, - None => quote! { - self.#field_n.on_swarm_event(#from_swarm::NewExternalAddrCandidate(#new_external_addr_candidate { - addr, - })); - }, - }) - }; - - // Build the list of statements to put in the body of `on_swarm_event()` - // for the `FromSwarm::ExternalAddrExpired` variant. - let on_external_addr_expired_stmts = { - data_struct - .fields - .iter() - .enumerate() - .map(|(field_n, field)| match field.ident { - Some(ref i) => quote! { - self.#i.on_swarm_event(#from_swarm::ExternalAddrExpired(#external_addr_expired { - addr, - })); - }, - None => quote! { - self.#field_n.on_swarm_event(#from_swarm::ExternalAddrExpired(#external_addr_expired { - addr, - })); - }, - }) - }; - - // Build the list of statements to put in the body of `on_swarm_event()` - // for the `FromSwarm::ExternalAddrConfirmed` variant. - let on_external_addr_confirmed_stmts = { - data_struct - .fields - .iter() - .enumerate() - .map(|(field_n, field)| match field.ident { - Some(ref i) => quote! { - self.#i.on_swarm_event(#from_swarm::ExternalAddrConfirmed(#external_addr_confirmed { - addr, - })); - }, - None => quote! { - self.#field_n.on_swarm_event(#from_swarm::ExternalAddrConfirmed(#external_addr_confirmed { - addr, - })); - }, - }) - }; - - // Build the list of statements to put in the body of `on_swarm_event()` - // for the `FromSwarm::ListenerError` variant. - let on_listener_error_stmts = { - data_struct - .fields - .iter() - .enumerate() - .map(|(field_n, field)| match field.ident { - Some(ref i) => quote! { - self.#i.on_swarm_event(#from_swarm::ListenerError(#listener_error { - listener_id, - err, - })); - }, - None => quote! { - self.#field_n.on_swarm_event(#from_swarm::ListenerError(#listener_error { - listener_id, - err, - })); - }, - }) - }; - - // Build the list of statements to put in the body of `on_swarm_event()` - // for the `FromSwarm::ListenerClosed` variant. - let on_listener_closed_stmts = { - data_struct - .fields - .iter() - .enumerate() - .map(|(field_n, field)| match field.ident { - Some(ref i) => quote! { - self.#i.on_swarm_event(#from_swarm::ListenerClosed(#listener_closed { - listener_id, - reason, - })); - }, - None => quote! { - self.#field_n.on_swarm_event(#from_swarm::ListenerClosed(#listener_closed { - listener_id, - reason, - })); + self.#field_n.on_swarm_event(event); }, }) }; @@ -792,48 +481,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result { #(#on_connection_established_stmts)* } - #from_swarm::AddressChange( - #address_change { peer_id, connection_id, old, new }) - => { #(#on_address_change_stmts)* } - #from_swarm::ConnectionClosed( - #connection_closed { peer_id, connection_id, endpoint, remaining_established }) - => { #(#on_connection_closed_stmts)* } - #from_swarm::DialFailure( - #dial_failure { peer_id, connection_id, error }) - => { #(#on_dial_failure_stmts)* } - #from_swarm::ListenFailure( - #listen_failure { local_addr, send_back_addr, connection_id, error }) - => { #(#on_listen_failure_stmts)* } - #from_swarm::NewListener( - #new_listener { listener_id }) - => { #(#on_new_listener_stmts)* } - #from_swarm::NewListenAddr( - #new_listen_addr { listener_id, addr }) - => { #(#on_new_listen_addr_stmts)* } - #from_swarm::ExpiredListenAddr( - #expired_listen_addr { listener_id, addr }) - => { #(#on_expired_listen_addr_stmts)* } - #from_swarm::NewExternalAddrCandidate( - #new_external_addr_candidate { addr }) - => { #(#on_new_external_addr_candidate_stmts)* } - #from_swarm::ExternalAddrExpired( - #external_addr_expired { addr }) - => { #(#on_external_addr_expired_stmts)* } - #from_swarm::ExternalAddrConfirmed( - #external_addr_confirmed { addr }) - => { #(#on_external_addr_confirmed_stmts)* } - #from_swarm::ListenerError( - #listener_error { listener_id, err }) - => { #(#on_listener_error_stmts)* } - #from_swarm::ListenerClosed( - #listener_closed { listener_id, reason }) - => { #(#on_listener_closed_stmts)* } - _ => {} - } + #(#on_swarm_event_stmts)* } } }; diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 48cafee6ced..65dce4b002a 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.44.1 + +- Implement `Clone` & `Copy` for `FromSwarm. + This makes it easier to forward these events when wrapping other behaviours. + See [PR 4825](https://github.com/libp2p/rust-libp2p/pull/4825). + ## 0.44.0 - Add `#[non_exhaustive]` to `FromSwarm`, `ToSwarm`, `SwarmEvent`, `ConnectionHandlerEvent`, `ConnectionEvent`. diff --git a/swarm/Cargo.toml b/swarm/Cargo.toml index 3b706df6d2b..817e4b4855e 100644 --- a/swarm/Cargo.toml +++ b/swarm/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-swarm" edition = "2021" rust-version = { workspace = true } description = "The libp2p swarm" -version = "0.44.0" +version = "0.44.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index c25b14e75e3..4be129a4eea 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -391,7 +391,7 @@ pub enum CloseConnection { /// Enumeration with the list of the possible events /// to pass to [`on_swarm_event`](NetworkBehaviour::on_swarm_event). -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] #[non_exhaustive] pub enum FromSwarm<'a> { /// Informs the behaviour about a newly established connection to a peer. @@ -449,7 +449,7 @@ pub struct ConnectionEstablished<'a> { /// This event is always paired with an earlier /// [`FromSwarm::ConnectionEstablished`] with the same peer ID, connection ID /// and endpoint. -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub struct ConnectionClosed<'a> { pub peer_id: PeerId, pub connection_id: ConnectionId, diff --git a/swarm/src/test.rs b/swarm/src/test.rs index 4f6adfc37b0..547277550bb 100644 --- a/swarm/src/test.rs +++ b/swarm/src/test.rs @@ -437,6 +437,8 @@ where } fn on_swarm_event(&mut self, event: FromSwarm) { + self.inner.on_swarm_event(event); + match event { FromSwarm::ConnectionEstablished(connection_established) => { self.on_connection_established(connection_established) @@ -444,68 +446,33 @@ where FromSwarm::ConnectionClosed(connection_closed) => { self.on_connection_closed(connection_closed) } - FromSwarm::DialFailure(DialFailure { - peer_id, - connection_id, - error, - }) => { + FromSwarm::DialFailure(DialFailure { peer_id, .. }) => { self.on_dial_failure.push(peer_id); - self.inner - .on_swarm_event(FromSwarm::DialFailure(DialFailure { - peer_id, - connection_id, - error, - })); } FromSwarm::NewListener(NewListener { listener_id }) => { self.on_new_listener.push(listener_id); - self.inner - .on_swarm_event(FromSwarm::NewListener(NewListener { listener_id })); } FromSwarm::NewListenAddr(NewListenAddr { listener_id, addr }) => { self.on_new_listen_addr.push((listener_id, addr.clone())); - self.inner - .on_swarm_event(FromSwarm::NewListenAddr(NewListenAddr { - listener_id, - addr, - })); } FromSwarm::ExpiredListenAddr(ExpiredListenAddr { listener_id, addr }) => { self.on_expired_listen_addr .push((listener_id, addr.clone())); - self.inner - .on_swarm_event(FromSwarm::ExpiredListenAddr(ExpiredListenAddr { - listener_id, - addr, - })); } FromSwarm::NewExternalAddrCandidate(NewExternalAddrCandidate { addr }) => { self.on_new_external_addr.push(addr.clone()); - self.inner - .on_swarm_event(FromSwarm::NewExternalAddrCandidate( - NewExternalAddrCandidate { addr }, - )); } FromSwarm::ExternalAddrExpired(ExternalAddrExpired { addr }) => { self.on_expired_external_addr.push(addr.clone()); - self.inner - .on_swarm_event(FromSwarm::ExternalAddrExpired(ExternalAddrExpired { addr })); } - FromSwarm::ListenerError(ListenerError { listener_id, err }) => { + FromSwarm::ListenerError(ListenerError { listener_id, .. }) => { self.on_listener_error.push(listener_id); - self.inner - .on_swarm_event(FromSwarm::ListenerError(ListenerError { listener_id, err })); } FromSwarm::ListenerClosed(ListenerClosed { listener_id, reason, }) => { self.on_listener_closed.push((listener_id, reason.is_ok())); - self.inner - .on_swarm_event(FromSwarm::ListenerClosed(ListenerClosed { - listener_id, - reason, - })); } _ => {} }