From 9ad0595064fe302498368f310dc74463fca9cea6 Mon Sep 17 00:00:00 2001 From: Dmitry Markin Date: Tue, 2 Aug 2022 18:40:35 +0300 Subject: [PATCH] Use `Multiaddr` instead of `ConnectedPoint` in `DialError::WrongPeerId` --- swarm/CHANGELOG.md | 2 ++ swarm/src/lib.rs | 27 +++++++++++++-------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 02edf9beef4..1a0d54e80e0 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -4,6 +4,8 @@ - Update to `libp2p-core` `v0.35.0`. +- `DialError::WrongPeerId` now uses `Multiaddr` instead of ambiguous `ConnectedPoint` + [PR 2741]: https://github.com/libp2p/rust-libp2p/pull/2741/ # 0.37.0 diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index f32c2df56bf..15627df3a1d 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -1441,7 +1441,7 @@ pub enum DialError { /// The peer identity obtained on the connection did not match the one that was expected. WrongPeerId { obtained: PeerId, - endpoint: ConnectedPoint, + address: Multiaddr, }, /// An I/O error occurred on the connection. ConnectionIo(io::Error), @@ -1454,9 +1454,15 @@ impl From> for DialError { match error { PendingConnectionError::ConnectionLimit(limit) => DialError::ConnectionLimit(limit), PendingConnectionError::Aborted => DialError::Aborted, - PendingConnectionError::WrongPeerId { obtained, endpoint } => { - DialError::WrongPeerId { obtained, endpoint } - } + PendingConnectionError::WrongPeerId { obtained, endpoint } => match endpoint { + ConnectedPoint::Dialer { + address, + role_override: _, + } => DialError::WrongPeerId { obtained, address }, + ConnectedPoint::Listener { .. } => panic!( + "`Listener` endpoint found in `PendingOutboundConnectionError::WrongPeerId`" + ), + }, PendingConnectionError::IO(e) => DialError::ConnectionIo(e), PendingConnectionError::Transport(e) => DialError::Transport(e), } @@ -1482,7 +1488,7 @@ impl fmt::Display for DialError { "Dial error: Pending connection attempt has been aborted." ), DialError::InvalidPeerId(multihash) => write!(f, "Dial error: multihash {:?} is not a PeerId", multihash), - DialError::WrongPeerId { obtained, endpoint} => write!(f, "Dial error: Unexpected peer ID {} at {:?}.", obtained, endpoint), + DialError::WrongPeerId { obtained, address } => write!(f, "Dial error: Unexpected peer ID {} at {}.", obtained, address), DialError::ConnectionIo(e) => write!( f, "Dial error: An I/O error occurred on the connection: {:?}.", e @@ -1623,7 +1629,6 @@ mod tests { use libp2p::yamux; use libp2p_core::multiaddr::multiaddr; use libp2p_core::transport::TransportEvent; - use libp2p_core::Endpoint; use quickcheck::{quickcheck, Arbitrary, Gen, QuickCheck}; use rand::prelude::SliceRandom; use rand::Rng; @@ -2341,15 +2346,9 @@ mod tests { })); assert_eq!(peer_id.unwrap(), other_id); match error { - DialError::WrongPeerId { obtained, endpoint } => { + DialError::WrongPeerId { obtained, address } => { assert_eq!(obtained, *swarm1.local_peer_id()); - assert_eq!( - endpoint, - ConnectedPoint::Dialer { - address: other_addr, - role_override: Endpoint::Dialer, - } - ); + assert_eq!(address, other_addr); } x => panic!("wrong error {:?}", x), }