diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a5b81db5..14e36305c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Handle connection ids greater than 9 in `Peer` impl of `Human` trait [#634](https://github.com/p2panda/aquadoggo/pull/634) + ## [0.7.4] ### Added diff --git a/aquadoggo/src/network/peers/peer.rs b/aquadoggo/src/network/peers/peer.rs index 08696e221..9604440a8 100644 --- a/aquadoggo/src/network/peers/peer.rs +++ b/aquadoggo/src/network/peers/peer.rs @@ -59,7 +59,38 @@ impl PartialOrd for Peer { impl Human for Peer { fn display(&self) -> String { // Trick to nicely display `ConnectionId` struct - let connection_id = &format!("{:?}", self.1)[13..][..1]; + let connection_id = format!("{:?}", self.1); + let connection_id = connection_id[13..] + .strip_suffix(')') + .expect("ConnectionId format is as expected"); format!("{} ({})", self.0, connection_id) } } + +#[cfg(test)] +mod tests { + use libp2p::identity::Keypair; + use libp2p::swarm::ConnectionId; + use p2panda_rs::Human; + + use super::Peer; + + #[test] + fn peers_display() { + let peer_id = Keypair::generate_ed25519().public().to_peer_id(); + let peer_connection_2 = Peer::new(peer_id, ConnectionId::new_unchecked(2)); + assert_eq!(peer_connection_2.display(), format!("{} ({})", peer_id, 2)); + + let peer_connection_23 = Peer::new(peer_id, ConnectionId::new_unchecked(23)); + assert_eq!( + peer_connection_23.display(), + format!("{} ({})", peer_id, 23) + ); + + let peer_connection_999 = Peer::new(peer_id, ConnectionId::new_unchecked(999)); + assert_eq!( + peer_connection_999.display(), + format!("{} ({})", peer_id, 999) + ); + } +}