Skip to content

Commit

Permalink
Fix: handle connection ids greater than 9 in Peer impl of Human t…
Browse files Browse the repository at this point in the history
…rait (#634)

* Handle connection ids greater than 9 in peer Human impl

* Clippy

* Update CHANGELOG
  • Loading branch information
sandreae committed Jun 22, 2024
1 parent 35303b1 commit 79a895c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion aquadoggo/src/network/peers/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}

0 comments on commit 79a895c

Please sign in to comment.