Skip to content

Commit 5aa8f6f

Browse files
committed
Include non-permanently connected peers in list_peers()
1 parent ffa2387 commit 5aa8f6f

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

bindings/ldk_node.udl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ interface LDKNode {
4949
[Throws=NodeError]
5050
u64 total_onchain_balance_sats();
5151
[Throws=NodeError]
52-
void connect(PublicKey node_id, NetAddress address, boolean permanently);
52+
void connect(PublicKey node_id, NetAddress address, boolean persist);
5353
[Throws=NodeError]
5454
void disconnect(PublicKey node_id);
5555
[Throws=NodeError]
@@ -176,6 +176,7 @@ dictionary ChannelDetails {
176176
dictionary PeerDetails {
177177
PublicKey node_id;
178178
NetAddress address;
179+
boolean is_persisted;
179180
boolean is_connected;
180181
};
181182

src/lib.rs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,9 +1284,9 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
12841284

12851285
/// Connect to a node on the peer-to-peer network.
12861286
///
1287-
/// If `permanently` is set to `true`, we'll remember the peer and reconnect to it on restart.
1287+
/// If `persist` is set to `true`, we'll remember the peer and reconnect to it on restart.
12881288
pub fn connect(
1289-
&self, node_id: PublicKey, address: NetAddress, permanently: bool,
1289+
&self, node_id: PublicKey, address: NetAddress, persist: bool,
12901290
) -> Result<(), Error> {
12911291
let rt_lock = self.runtime.read().unwrap();
12921292
if rt_lock.is_none() {
@@ -1311,7 +1311,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
13111311

13121312
log_info!(self.logger, "Connected to peer {}@{}. ", peer_info.node_id, peer_info.address);
13131313

1314-
if permanently {
1314+
if persist {
13151315
self.peer_store.add_peer(peer_info)?;
13161316
}
13171317

@@ -1833,17 +1833,43 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
18331833

18341834
/// Retrieves a list of known peers.
18351835
pub fn list_peers(&self) -> Vec<PeerDetails> {
1836-
let active_connected_peers: Vec<PublicKey> =
1837-
self.peer_manager.get_peer_node_ids().iter().map(|p| p.0).collect();
1838-
self.peer_store
1839-
.list_peers()
1840-
.iter()
1841-
.map(|p| PeerDetails {
1836+
let mut peers = Vec::new();
1837+
1838+
// First add all connected peers, preferring to list the connected address if available.
1839+
let connected_peers = self.peer_manager.get_peer_node_ids();
1840+
let connected_peers_len = connected_peers.len();
1841+
for (node_id, con_addr_opt) in connected_peers {
1842+
let stored_peer = self.peer_store.get_peer(&node_id);
1843+
let stored_addr_opt = stored_peer.as_ref().map(|p| p.address.clone());
1844+
let address = match (con_addr_opt, stored_addr_opt) {
1845+
(Some(con_addr), _) => NetAddress(con_addr),
1846+
(None, Some(stored_addr)) => stored_addr,
1847+
(None, None) => continue,
1848+
};
1849+
1850+
let is_persisted = stored_peer.is_some();
1851+
let is_connected = true;
1852+
let details = PeerDetails { node_id, address, is_persisted, is_connected };
1853+
peers.push(details);
1854+
}
1855+
1856+
// Now add all known-but-offline peers, too.
1857+
for p in self.peer_store.list_peers() {
1858+
if peers.iter().take(connected_peers_len).find(|d| d.node_id == p.node_id).is_some() {
1859+
continue;
1860+
}
1861+
1862+
let details = PeerDetails {
18421863
node_id: p.node_id,
1843-
address: p.address.clone(),
1844-
is_connected: active_connected_peers.contains(&p.node_id),
1845-
})
1846-
.collect()
1864+
address: p.address,
1865+
is_persisted: true,
1866+
is_connected: false,
1867+
};
1868+
1869+
peers.push(details);
1870+
}
1871+
1872+
peers
18471873
}
18481874

18491875
/// Creates a digital ECDSA signature of a message with the node's secret key.

src/types.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,13 @@ impl From<LdkChannelDetails> for ChannelDetails {
236236
/// [`Node::list_peers`]: [`crate::Node::list_peers`]
237237
#[derive(Debug, Clone, PartialEq, Eq)]
238238
pub struct PeerDetails {
239-
/// Our peer's node ID.
239+
/// The node ID of the peer.
240240
pub node_id: PublicKey,
241-
/// The IP address and TCP port of the peer.
241+
/// The network address of the peer.
242242
pub address: NetAddress,
243-
/// Indicates whether or not the user is currently has an active connection with the peer.
243+
/// Indicates whether we'll try to reconnect to this peer after restarts.
244+
pub is_persisted: bool,
245+
/// Indicates whether we currently have an active connection with the peer.
244246
pub is_connected: bool,
245247
}
246248

0 commit comments

Comments
 (0)