Skip to content

Commit

Permalink
Add peer ID to web-client PlainPeerInfo objects
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou authored and jsdanielh committed Jun 13, 2024
1 parent 52cc912 commit e4c44c2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
27 changes: 15 additions & 12 deletions network-libp2p/src/discovery/peer_contacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,19 +439,22 @@ impl PeerContactBook {
/// Additional addresses aside from the first are omitted.
///
/// The returned Vec may be empty.
pub fn known_peers(&self) -> Vec<PeerInfo> {
pub fn known_peers(&self) -> Vec<(PeerId, PeerInfo)> {
self.peer_contacts
.values()
.map(|contact| {
PeerInfo::new(
contact
.contact
.inner
.addresses
.first()
.expect("every peer should have at least one address")
.clone(),
contact.contact.inner.services,
.iter()
.map(|(peer_id, contact)| {
(
*peer_id,
PeerInfo::new(
contact
.contact
.inner
.addresses
.first()
.expect("every peer should have at least one address")
.clone(),
contact.contact.inner.services,
),
)
})
.collect()
Expand Down
2 changes: 1 addition & 1 deletion network-libp2p/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Network {

/// Retrieves a single PeerInfo peer existing in the PeerAddressBook.
/// If that peer has multiple associated addresses all but the first are omitted.
pub fn get_address_book(&self) -> Vec<PeerInfo> {
pub fn get_address_book(&self) -> Vec<(PeerId, PeerInfo)> {
self.contacts.read().known_peers()
}

Expand Down
2 changes: 1 addition & 1 deletion web-client/example/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ init().then(async () => {

document.querySelector('#address-book').addEventListener("click", async () => {
let contacts = await client.getAddressBook();
console.log(contacts);
console.table(contacts);
});

/**
Expand Down
9 changes: 6 additions & 3 deletions web-client/src/client/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ impl Client {
.network()
.get_address_book()
.into_iter()
.map(PlainPeerInfo::from)
.map(|(peer_id, peer_info)| PlainPeerInfo::from(peer_id.to_string(), peer_info))
.collect();

serde_wasm_bindgen::to_value(&contacts).unwrap().into()
Expand Down Expand Up @@ -966,8 +966,11 @@ impl Client {
peer_id.to_string(),
"joined",
Some(
serde_wasm_bindgen::to_value(&PlainPeerInfo::from(peer_info))
.unwrap(),
serde_wasm_bindgen::to_value(&PlainPeerInfo::from(
peer_id.to_string(),
peer_info,
))
.unwrap(),
),
))
}
Expand Down
6 changes: 4 additions & 2 deletions web-client/src/client/peer_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use wasm_bindgen::prelude::*;
#[derive(serde::Serialize, Tsify)]
#[serde(rename_all = "camelCase")]
pub struct PlainPeerInfo {
pub peer_id: String,
/// Address of the peer in `Multiaddr` format
address: String,
/// Node type of the peer
Expand All @@ -14,8 +15,8 @@ pub struct PlainPeerInfo {
node_type: String,
}

impl From<nimiq_network_interface::peer_info::PeerInfo> for PlainPeerInfo {
fn from(peer_info: nimiq_network_interface::peer_info::PeerInfo) -> Self {
impl PlainPeerInfo {
pub fn from(peer_id: String, peer_info: nimiq_network_interface::peer_info::PeerInfo) -> Self {
let node_type = if peer_info
.get_services()
.contains(Services::provided(NodeType::History))
Expand All @@ -31,6 +32,7 @@ impl From<nimiq_network_interface::peer_info::PeerInfo> for PlainPeerInfo {
};

Self {
peer_id,
address: peer_info.get_address().to_string(),
node_type: node_type.to_string(),
}
Expand Down

0 comments on commit e4c44c2

Please sign in to comment.