Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement Debug trait #4426

Merged
merged 9 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ libp2p = { version = "0.52.3", path = "libp2p" }
libp2p-allow-block-list = { version = "0.2.0", path = "misc/allow-block-list" }
libp2p-autonat = { version = "0.11.0", path = "protocols/autonat" }
libp2p-connection-limits = { version = "0.2.1", path = "misc/connection-limits" }
libp2p-core = { version = "0.40.0", path = "core" }
libp2p-core = { version = "0.40.1", path = "core" }
libp2p-dcutr = { version = "0.10.0", path = "protocols/dcutr" }
libp2p-deflate = { version = "0.40.0", path = "transports/deflate" }
libp2p-dns = { version = "0.40.0", path = "transports/dns" }
Expand Down
9 changes: 8 additions & 1 deletion core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
## 0.40.0
## 0.40.1 - unreleased

- Implement `Debug` for `StreamMuxerEvent`.
See [PR 4426].

[PR 4426]: https://github.com/libp2p/rust-libp2p/pull/4426

## 0.40.0

- Allow `ListenerId` to be user-controlled, i.e. to be provided on `Transport::listen_on`.
See [PR 3567].
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-core"
edition = "2021"
rust-version = { workspace = true }
description = "Core traits and structs of libp2p"
version = "0.40.0"
version = "0.40.1"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
1 change: 1 addition & 0 deletions core/src/muxing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub trait StreamMuxer {
}

/// An event produced by a [`StreamMuxer`].
#[derive(Debug)]
pub enum StreamMuxerEvent {
/// The address of the remote has changed.
AddressChange(Multiaddr),
Expand Down
4 changes: 4 additions & 0 deletions swarm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
## 0.43.4 - unreleased

- Implement `Debug` for event structs.
See [PR 4426].

- Improve error message when `DialPeerCondition` prevents a dial.
See [PR 4409].

[PR 4426]: https://github.com/libp2p/rust-libp2p/pull/4426
[PR 4409]: https://github.com/libp2p/rust-libp2p/pull/4409

## 0.43.3
Expand Down
26 changes: 14 additions & 12 deletions swarm/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ pub enum CloseConnection {

/// Enumeration with the list of the possible events
/// to pass to [`on_swarm_event`](NetworkBehaviour::on_swarm_event).
#[derive(Debug)]
pub enum FromSwarm<'a, Handler> {
/// Informs the behaviour about a newly established connection to a peer.
ConnectionEstablished(ConnectionEstablished<'a>),
Expand Down Expand Up @@ -450,7 +451,7 @@ pub enum FromSwarm<'a, Handler> {
}

/// [`FromSwarm`] variant that informs the behaviour about a newly established connection to a peer.
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct ConnectionEstablished<'a> {
pub peer_id: PeerId,
pub connection_id: ConnectionId,
Expand All @@ -464,6 +465,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)]
pub struct ConnectionClosed<'a, Handler> {
pub peer_id: PeerId,
pub connection_id: ConnectionId,
Expand All @@ -474,7 +476,7 @@ pub struct ConnectionClosed<'a, Handler> {

/// [`FromSwarm`] variant that informs the behaviour that the [`ConnectedPoint`] of an existing
/// connection has changed.
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct AddressChange<'a> {
pub peer_id: PeerId,
pub connection_id: ConnectionId,
Expand All @@ -484,7 +486,7 @@ pub struct AddressChange<'a> {

/// [`FromSwarm`] variant that informs the behaviour that the dial to a known
/// or unknown node failed.
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct DialFailure<'a> {
pub peer_id: Option<PeerId>,
pub error: &'a DialError,
Expand All @@ -496,7 +498,7 @@ pub struct DialFailure<'a> {
///
/// This can include, for example, an error during the handshake of the encryption layer, or the
/// connection unexpectedly closed.
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct ListenFailure<'a> {
pub local_addr: &'a Multiaddr,
pub send_back_addr: &'a Multiaddr,
Expand All @@ -505,14 +507,14 @@ pub struct ListenFailure<'a> {
}

/// [`FromSwarm`] variant that informs the behaviour that a new listener was created.
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct NewListener {
pub listener_id: ListenerId,
}

/// [`FromSwarm`] variant that informs the behaviour
/// that we have started listening on a new multiaddr.
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct NewListenAddr<'a> {
pub listener_id: ListenerId,
pub addr: &'a Multiaddr,
Expand All @@ -521,40 +523,40 @@ pub struct NewListenAddr<'a> {
/// [`FromSwarm`] variant that informs the behaviour that a multiaddr
/// we were listening on has expired,
/// which means that we are no longer listening on it.
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct ExpiredListenAddr<'a> {
pub listener_id: ListenerId,
pub addr: &'a Multiaddr,
}

/// [`FromSwarm`] variant that informs the behaviour that a listener experienced an error.
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct ListenerError<'a> {
pub listener_id: ListenerId,
pub err: &'a (dyn std::error::Error + 'static),
}

/// [`FromSwarm`] variant that informs the behaviour that a listener closed.
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct ListenerClosed<'a> {
pub listener_id: ListenerId,
pub reason: Result<(), &'a std::io::Error>,
}

/// [`FromSwarm`] variant that informs the behaviour about a new candidate for an external address for us.
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct NewExternalAddrCandidate<'a> {
pub addr: &'a Multiaddr,
}

/// [`FromSwarm`] variant that informs the behaviour that an external address was confirmed.
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct ExternalAddrConfirmed<'a> {
pub addr: &'a Multiaddr,
}

/// [`FromSwarm`] variant that informs the behaviour that an external address was removed.
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct ExternalAddrExpired<'a> {
pub addr: &'a Multiaddr,
}
Expand Down
47 changes: 44 additions & 3 deletions swarm/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,42 @@ pub enum ConnectionEvent<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IO
RemoteProtocolsChange(ProtocolsChange<'a>),
}

impl<'a, IP, OP, IOI, OOI> fmt::Debug for ConnectionEvent<'a, IP, OP, IOI, OOI>
where
IP: InboundUpgradeSend + fmt::Debug,
IP::Output: fmt::Debug,
IP::Error: fmt::Debug,
OP: OutboundUpgradeSend + fmt::Debug,
OP::Output: fmt::Debug,
OP::Error: fmt::Debug,
IOI: fmt::Debug,
OOI: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ConnectionEvent::FullyNegotiatedInbound(v) => {
f.debug_tuple("FullyNegotiatedInbound").field(v).finish()
}
ConnectionEvent::FullyNegotiatedOutbound(v) => {
f.debug_tuple("FullyNegotiatedOutbound").field(v).finish()
}
ConnectionEvent::AddressChange(v) => f.debug_tuple("AddressChange").field(v).finish(),
ConnectionEvent::DialUpgradeError(v) => {
f.debug_tuple("DialUpgradeError").field(v).finish()
}
ConnectionEvent::ListenUpgradeError(v) => {
f.debug_tuple("ListenUpgradeError").field(v).finish()
}
ConnectionEvent::LocalProtocolsChange(v) => {
f.debug_tuple("LocalProtocolsChange").field(v).finish()
}
ConnectionEvent::RemoteProtocolsChange(v) => {
f.debug_tuple("RemoteProtocolsChange").field(v).finish()
}
}
}
}
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved

impl<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IOI, OOI>
ConnectionEvent<'a, IP, OP, IOI, OOI>
{
Expand Down Expand Up @@ -262,6 +298,7 @@ impl<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IOI, OOI>
/// of simultaneously open negotiated inbound substreams. In other words it is up to the
/// [`ConnectionHandler`] implementation to stop a malicious remote node to open and keep alive
/// an excessive amount of inbound substreams.
#[derive(Debug)]
pub struct FullyNegotiatedInbound<IP: InboundUpgradeSend, IOI> {
pub protocol: IP::Output,
pub info: IOI,
Expand All @@ -271,18 +308,20 @@ pub struct FullyNegotiatedInbound<IP: InboundUpgradeSend, IOI> {
///
/// The `protocol` field is the information that was previously passed to
/// [`ConnectionHandlerEvent::OutboundSubstreamRequest`].
#[derive(Debug)]
pub struct FullyNegotiatedOutbound<OP: OutboundUpgradeSend, OOI> {
pub protocol: OP::Output,
pub info: OOI,
}

/// [`ConnectionEvent`] variant that informs the handler about a change in the address of the remote.
#[derive(Debug)]
pub struct AddressChange<'a> {
pub new_address: &'a Multiaddr,
}

/// [`ConnectionEvent`] variant that informs the handler about a change in the protocols supported on the connection.
#[derive(Clone)]
#[derive(Debug, Clone)]
pub enum ProtocolsChange<'a> {
Added(ProtocolsAdded<'a>),
Removed(ProtocolsRemoved<'a>),
Expand Down Expand Up @@ -352,7 +391,7 @@ impl<'a> ProtocolsChange<'a> {
}

/// An [`Iterator`] over all protocols that have been added.
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct ProtocolsAdded<'a> {
protocols: Peekable<Difference<'a, StreamProtocol, RandomState>>,
}
Expand All @@ -366,7 +405,7 @@ impl<'a> ProtocolsAdded<'a> {
}

/// An [`Iterator`] over all protocols that have been removed.
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct ProtocolsRemoved<'a> {
protocols: Either<
Peekable<Difference<'a, StreamProtocol, RandomState>>,
Expand Down Expand Up @@ -399,13 +438,15 @@ impl<'a> Iterator for ProtocolsRemoved<'a> {

/// [`ConnectionEvent`] variant that informs the handler
/// that upgrading an outbound substream to the given protocol has failed.
#[derive(Debug)]
pub struct DialUpgradeError<OOI, OP: OutboundUpgradeSend> {
pub info: OOI,
pub error: StreamUpgradeError<OP::Error>,
}

/// [`ConnectionEvent`] variant that informs the handler
/// that upgrading an inbound substream to the given protocol has failed.
#[derive(Debug)]
pub struct ListenUpgradeError<IOI, IP: InboundUpgradeSend> {
pub info: IOI,
pub error: IP::Error,
Expand Down
1 change: 1 addition & 0 deletions swarm/src/handler/map_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::handler::{
use std::{fmt::Debug, marker::PhantomData, task::Context, task::Poll};

/// Wrapper around a protocol handler that turns the input event into something else.
#[derive(Debug)]
pub struct MapInEvent<TConnectionHandler, TNewIn, TMap> {
inner: TConnectionHandler,
map: TMap,
Expand Down
1 change: 1 addition & 0 deletions swarm/src/handler/map_out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::fmt::Debug;
use std::task::{Context, Poll};

/// Wrapper around a protocol handler that turns the output event into something else.
#[derive(Debug)]
pub struct MapOutEvent<TConnectionHandler, TMap> {
inner: TConnectionHandler,
map: TMap,
Expand Down