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

Fix: handle connection ids greater than 9 in Peer impl of Human trait #634

Merged
merged 3 commits into from
Jun 22, 2024
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
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)
);
}
}
Loading