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

Return Peer's announcement info if available #278

Closed
Closed
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
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,11 +1487,13 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {

/// Retrieves a list of known peers.
pub fn list_peers(&self) -> Vec<PeerDetails> {
let mut peers = Vec::new();
let network_graph = self.network_graph.read_only();

// First add all connected peers, preferring to list the connected address if available.
let connected_peers = self.peer_manager.get_peer_node_ids();
let connected_peers_len = connected_peers.len();

let mut peers = Vec::with_capacity(connected_peers_len);
for (node_id, con_addr_opt) in connected_peers {
let stored_peer = self.peer_store.get_peer(&node_id);
let stored_addr_opt = stored_peer.as_ref().map(|p| p.address.clone());
Expand All @@ -1500,10 +1502,11 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
(None, Some(stored_addr)) => stored_addr,
(None, None) => continue,
};
let announcement_info = network_graph.node(&node_id.into()).and_then(|n| n.announcement_info.clone());

let is_persisted = stored_peer.is_some();
let is_connected = true;
let details = PeerDetails { node_id, address, is_persisted, is_connected };
let details = PeerDetails { node_id, address, is_persisted, is_connected, announcement_info };
peers.push(details);
}

Expand All @@ -1512,12 +1515,14 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
if peers.iter().take(connected_peers_len).find(|d| d.node_id == p.node_id).is_some() {
continue;
}
let announcement_info = network_graph.node(&p.node_id.into()).and_then(|n| n.announcement_info.clone());

let details = PeerDetails {
node_id: p.node_id,
address: p.address,
is_persisted: true,
is_connected: false,
announcement_info,
};

peers.push(details);
Expand Down
4 changes: 4 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::fmt::Display;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
use std::str::FromStr;
use std::sync::{Arc, Mutex, RwLock};
use lightning::routing::gossip::NodeAnnouncementInfo;

pub(crate) type ChainMonitor<K> = chainmonitor::ChainMonitor<
InMemorySigner,
Expand Down Expand Up @@ -265,6 +266,9 @@ pub struct PeerDetails {
pub is_persisted: bool,
/// Indicates whether we currently have an active connection with the peer.
pub is_connected: bool,
/// Peer's announcement info if we have it.
/// Not available for peers synced over RGS.
pub announcement_info: Option<NodeAnnouncementInfo>
}

/// The network address of a Lightning node.
Expand Down
Loading