Skip to content

Commit

Permalink
feat(swarm): add #[non_exhaustive] to key enums
Browse files Browse the repository at this point in the history
Add `#[non_exhaustive]` to the following enums so that future additions don't cause breaking changes in downstream client code: `FromSwarm`, `ToSwarm`, `SwarmEvent`, `ConnectionHandlerEvent`, `ConnectionEvent`.

Related: #4543.

Pull-Request: #4581.
  • Loading branch information
dhuseby authored Nov 2, 2023
1 parent e2e9179 commit a428ffd
Show file tree
Hide file tree
Showing 39 changed files with 93 additions and 350 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
echo "Package version: $PACKAGE_VERSION";
echo "Specified version: $SPECIFIED_VERSION";
test "$PACKAGE_VERSION" = "$SPECIFIED_VERSION"
test "$PACKAGE_VERSION" = "$SPECIFIED_VERSION" || test "=$PACKAGE_VERSION" = "$SPECIFIED_VERSION"
- name: Ensure manifest and CHANGELOG are properly updated
if: >
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ libp2p-rendezvous = { version = "0.14.0", path = "protocols/rendezvous" }
libp2p-request-response = { version = "0.26.0", path = "protocols/request-response" }
libp2p-server = { version = "0.12.3", path = "misc/server" }
libp2p-swarm = { version = "0.44.0", path = "swarm" }
libp2p-swarm-derive = { version = "0.34.0", path = "swarm-derive" }
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-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" }
Expand Down
18 changes: 1 addition & 17 deletions misc/allow-block-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,23 +231,7 @@ where
Ok(dummy::ConnectionHandler)
}

fn on_swarm_event(&mut self, event: FromSwarm) {
match event {
FromSwarm::ConnectionClosed(_) => {}
FromSwarm::ConnectionEstablished(_) => {}
FromSwarm::AddressChange(_) => {}
FromSwarm::DialFailure(_) => {}
FromSwarm::ListenFailure(_) => {}
FromSwarm::NewListener(_) => {}
FromSwarm::NewListenAddr(_) => {}
FromSwarm::ExpiredListenAddr(_) => {}
FromSwarm::ListenerError(_) => {}
FromSwarm::ListenerClosed(_) => {}
FromSwarm::NewExternalAddrCandidate(_) => {}
FromSwarm::ExternalAddrExpired(_) => {}
FromSwarm::ExternalAddrConfirmed(_) => {}
}
}
fn on_swarm_event(&mut self, _event: FromSwarm) {}

fn on_connection_handler_event(
&mut self,
Expand Down
10 changes: 1 addition & 9 deletions misc/connection-limits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,18 +340,10 @@ impl NetworkBehaviour for Behaviour {
FromSwarm::DialFailure(DialFailure { connection_id, .. }) => {
self.pending_outbound_connections.remove(&connection_id);
}
FromSwarm::AddressChange(_) => {}
FromSwarm::ListenFailure(ListenFailure { connection_id, .. }) => {
self.pending_inbound_connections.remove(&connection_id);
}
FromSwarm::NewListener(_) => {}
FromSwarm::NewListenAddr(_) => {}
FromSwarm::ExpiredListenAddr(_) => {}
FromSwarm::ListenerError(_) => {}
FromSwarm::ListenerClosed(_) => {}
FromSwarm::NewExternalAddrCandidate(_) => {}
FromSwarm::ExternalAddrExpired(_) => {}
FromSwarm::ExternalAddrConfirmed(_) => {}
_ => {}
}
}

Expand Down
51 changes: 22 additions & 29 deletions misc/metrics/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::sync::{Arc, Mutex};

use crate::protocol_stack;
use instant::Instant;
use libp2p_swarm::{ConnectionId, SwarmEvent};
use libp2p_swarm::{ConnectionId, DialError, SwarmEvent};
use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue};
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
Expand Down Expand Up @@ -185,11 +185,11 @@ impl Metrics {
}
}

impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleErr>> for Metrics {
fn record(&self, event: &libp2p_swarm::SwarmEvent<TBvEv, THandleErr>) {
impl<TBvEv, THandleErr> super::Recorder<SwarmEvent<TBvEv, THandleErr>> for Metrics {
fn record(&self, event: &SwarmEvent<TBvEv, THandleErr>) {
match event {
libp2p_swarm::SwarmEvent::Behaviour(_) => {}
libp2p_swarm::SwarmEvent::ConnectionEstablished {
SwarmEvent::Behaviour(_) => {}
SwarmEvent::ConnectionEstablished {
endpoint,
established_in: time_taken,
connection_id,
Expand All @@ -208,7 +208,7 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
.expect("lock not to be poisoned")
.insert(*connection_id, Instant::now());
}
libp2p_swarm::SwarmEvent::ConnectionClosed {
SwarmEvent::ConnectionClosed {
endpoint,
connection_id,
cause,
Expand All @@ -231,14 +231,14 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
.as_secs_f64(),
);
}
libp2p_swarm::SwarmEvent::IncomingConnection { send_back_addr, .. } => {
SwarmEvent::IncomingConnection { send_back_addr, .. } => {
self.connections_incoming
.get_or_create(&AddressLabels {
protocols: protocol_stack::as_string(send_back_addr),
})
.inc();
}
libp2p_swarm::SwarmEvent::IncomingConnectionError {
SwarmEvent::IncomingConnectionError {
error,
send_back_addr,
..
Expand All @@ -250,7 +250,7 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
})
.inc();
}
libp2p_swarm::SwarmEvent::OutgoingConnectionError { error, peer_id, .. } => {
SwarmEvent::OutgoingConnectionError { error, peer_id, .. } => {
let peer = match peer_id {
Some(_) => PeerStatus::Known,
None => PeerStatus::Unknown,
Expand All @@ -263,7 +263,7 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
};

match error {
libp2p_swarm::DialError::Transport(errors) => {
DialError::Transport(errors) => {
for (_multiaddr, error) in errors {
match error {
libp2p_core::transport::TransportError::MultiaddrNotSupported(
Expand All @@ -277,39 +277,31 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
};
}
}
libp2p_swarm::DialError::LocalPeerId { .. } => {
record(OutgoingConnectionError::LocalPeerId)
}
libp2p_swarm::DialError::NoAddresses => {
record(OutgoingConnectionError::NoAddresses)
}
libp2p_swarm::DialError::DialPeerConditionFalse(_) => {
DialError::LocalPeerId { .. } => record(OutgoingConnectionError::LocalPeerId),
DialError::NoAddresses => record(OutgoingConnectionError::NoAddresses),
DialError::DialPeerConditionFalse(_) => {
record(OutgoingConnectionError::DialPeerConditionFalse)
}
libp2p_swarm::DialError::Aborted => record(OutgoingConnectionError::Aborted),
libp2p_swarm::DialError::WrongPeerId { .. } => {
record(OutgoingConnectionError::WrongPeerId)
}
libp2p_swarm::DialError::Denied { .. } => {
record(OutgoingConnectionError::Denied)
}
DialError::Aborted => record(OutgoingConnectionError::Aborted),
DialError::WrongPeerId { .. } => record(OutgoingConnectionError::WrongPeerId),
DialError::Denied { .. } => record(OutgoingConnectionError::Denied),
};
}
libp2p_swarm::SwarmEvent::NewListenAddr { address, .. } => {
SwarmEvent::NewListenAddr { address, .. } => {
self.new_listen_addr
.get_or_create(&AddressLabels {
protocols: protocol_stack::as_string(address),
})
.inc();
}
libp2p_swarm::SwarmEvent::ExpiredListenAddr { address, .. } => {
SwarmEvent::ExpiredListenAddr { address, .. } => {
self.expired_listen_addr
.get_or_create(&AddressLabels {
protocols: protocol_stack::as_string(address),
})
.inc();
}
libp2p_swarm::SwarmEvent::ListenerClosed { addresses, .. } => {
SwarmEvent::ListenerClosed { addresses, .. } => {
for address in addresses {
self.listener_closed
.get_or_create(&AddressLabels {
Expand All @@ -318,10 +310,10 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
.inc();
}
}
libp2p_swarm::SwarmEvent::ListenerError { .. } => {
SwarmEvent::ListenerError { .. } => {
self.listener_error.inc();
}
libp2p_swarm::SwarmEvent::Dialing { .. } => {
SwarmEvent::Dialing { .. } => {
self.dial_attempt.inc();
}
SwarmEvent::NewExternalAddrCandidate { address } => {
Expand All @@ -345,6 +337,7 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
})
.inc();
}
_ => {}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ impl NetworkBehaviour for Behaviour {
self.inner.on_swarm_event(listener_closed)
}
confirmed @ FromSwarm::ExternalAddrConfirmed(_) => self.inner.on_swarm_event(confirmed),
_ => {}
}
}

Expand Down
11 changes: 1 addition & 10 deletions protocols/dcutr/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,7 @@ impl NetworkBehaviour for Behaviour {
FromSwarm::NewExternalAddrCandidate(NewExternalAddrCandidate { addr }) => {
self.address_candidates.add(addr.clone());
}
FromSwarm::AddressChange(_)
| FromSwarm::ConnectionEstablished(_)
| FromSwarm::ListenFailure(_)
| FromSwarm::NewListener(_)
| FromSwarm::NewListenAddr(_)
| FromSwarm::ExpiredListenAddr(_)
| FromSwarm::ListenerError(_)
| FromSwarm::ListenerClosed(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_) => {}
_ => {}
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions protocols/dcutr/src/handler/relayed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,7 @@ impl ConnectionHandler for Handler {
ConnectionEvent::DialUpgradeError(dial_upgrade_error) => {
self.on_dial_upgrade_error(dial_upgrade_error)
}
ConnectionEvent::AddressChange(_)
| ConnectionEvent::LocalProtocolsChange(_)
| ConnectionEvent::RemoteProtocolsChange(_) => {}
_ => {}
}
}
}
12 changes: 1 addition & 11 deletions protocols/floodsub/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,17 +490,7 @@ impl NetworkBehaviour for Floodsub {
FromSwarm::ConnectionClosed(connection_closed) => {
self.on_connection_closed(connection_closed)
}
FromSwarm::AddressChange(_)
| FromSwarm::DialFailure(_)
| FromSwarm::ListenFailure(_)
| FromSwarm::NewListener(_)
| FromSwarm::NewListenAddr(_)
| FromSwarm::ExpiredListenAddr(_)
| FromSwarm::ListenerError(_)
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_) => {}
_ => {}
}
}
}
Expand Down
11 changes: 1 addition & 10 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3458,16 +3458,7 @@ where
self.on_connection_closed(connection_closed)
}
FromSwarm::AddressChange(address_change) => self.on_address_change(address_change),
FromSwarm::DialFailure(_)
| FromSwarm::ListenFailure(_)
| FromSwarm::NewListener(_)
| FromSwarm::NewListenAddr(_)
| FromSwarm::ExpiredListenAddr(_)
| FromSwarm::ListenerError(_)
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_) => {}
_ => {}
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions protocols/gossipsub/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,7 @@ impl ConnectionHandler for Handler {
}) => {
tracing::debug!("Protocol negotiation failed: {e}")
}
ConnectionEvent::AddressChange(_)
| ConnectionEvent::ListenUpgradeError(_)
| ConnectionEvent::LocalProtocolsChange(_)
| ConnectionEvent::RemoteProtocolsChange(_) => {}
_ => {}
}
}
Handler::Disabled(_) => {}
Expand Down
11 changes: 1 addition & 10 deletions protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,16 +395,7 @@ impl NetworkBehaviour for Behaviour {
}
}
}
FromSwarm::NewListenAddr(_)
| FromSwarm::ExpiredListenAddr(_)
| FromSwarm::AddressChange(_)
| FromSwarm::ListenFailure(_)
| FromSwarm::NewListener(_)
| FromSwarm::ListenerError(_)
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_) => {}
FromSwarm::ExternalAddrConfirmed(_) => {}
_ => {}
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions protocols/identify/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,6 @@ impl ConnectionHandler for Handler {
));
self.trigger_next_identify.reset(self.interval);
}
ConnectionEvent::AddressChange(_)
| ConnectionEvent::ListenUpgradeError(_)
| ConnectionEvent::RemoteProtocolsChange(_) => {}
ConnectionEvent::LocalProtocolsChange(change) => {
let before = tracing::enabled!(Level::DEBUG)
.then(|| self.local_protocols_to_string())
Expand All @@ -436,6 +433,7 @@ impl ConnectionHandler for Handler {
});
}
}
_ => {}
}
}
}
Expand Down
10 changes: 1 addition & 9 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2554,15 +2554,7 @@ where
}
FromSwarm::DialFailure(dial_failure) => self.on_dial_failure(dial_failure),
FromSwarm::AddressChange(address_change) => self.on_address_change(address_change),
FromSwarm::ExpiredListenAddr(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::NewListenAddr(_)
| FromSwarm::ListenFailure(_)
| FromSwarm::NewListener(_)
| FromSwarm::ListenerClosed(_)
| FromSwarm::ListenerError(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_) => {}
_ => {}
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions protocols/kad/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,9 +777,6 @@ impl ConnectionHandler for Handler {
ConnectionEvent::DialUpgradeError(dial_upgrade_error) => {
self.on_dial_upgrade_error(dial_upgrade_error)
}
ConnectionEvent::AddressChange(_)
| ConnectionEvent::ListenUpgradeError(_)
| ConnectionEvent::LocalProtocolsChange(_) => {}
ConnectionEvent::RemoteProtocolsChange(change) => {
let dirty = self.remote_supported_protocols.on_protocols_change(change);

Expand All @@ -795,6 +792,7 @@ impl ConnectionHandler for Handler {
))
}
}
_ => {}
}
}
}
Expand Down
12 changes: 1 addition & 11 deletions protocols/perf/src/client/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,7 @@ impl NetworkBehaviour for Behaviour {
assert!(self.connected.remove(&peer_id));
}
}
FromSwarm::AddressChange(_)
| FromSwarm::DialFailure(_)
| FromSwarm::ListenFailure(_)
| FromSwarm::NewListener(_)
| FromSwarm::NewListenAddr(_)
| FromSwarm::ExpiredListenAddr(_)
| FromSwarm::ListenerError(_)
| FromSwarm::ListenerClosed(_)
| FromSwarm::NewExternalAddrCandidate(_)
| FromSwarm::ExternalAddrExpired(_)
| FromSwarm::ExternalAddrConfirmed(_) => {}
_ => {}
}
}

Expand Down
1 change: 1 addition & 0 deletions protocols/perf/src/client/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ impl ConnectionHandler for Handler {
ConnectionEvent::ListenUpgradeError(ListenUpgradeError { info: (), error }) => {
void::unreachable(error)
}
_ => {}
}
}

Expand Down
Loading

0 comments on commit a428ffd

Please sign in to comment.