Skip to content

Commit ed64389

Browse files
committed
Include non-permanently connected peers in list_peers()
1 parent 618eead commit ed64389

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
@@ -36,7 +36,7 @@ interface Node {
3636
[Throws=NodeError]
3737
u64 total_onchain_balance_sats();
3838
[Throws=NodeError]
39-
void connect(PublicKey node_id, NetAddress address, boolean permanently);
39+
void connect(PublicKey node_id, NetAddress address, boolean persist);
4040
[Throws=NodeError]
4141
void disconnect(PublicKey node_id);
4242
[Throws=NodeError]
@@ -153,6 +153,7 @@ dictionary ChannelDetails {
153153
dictionary PeerDetails {
154154
PublicKey node_id;
155155
NetAddress address;
156+
boolean is_persisted;
156157
boolean is_connected;
157158
};
158159

src/lib.rs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,9 +1019,9 @@ impl Node {
10191019

10201020
/// Connect to a node on the peer-to-peer network.
10211021
///
1022-
/// If `permanently` is set to `true`, we'll remember the peer and reconnect to it on restart.
1022+
/// If `persist` is set to `true`, we'll remember the peer and reconnect to it on restart.
10231023
pub fn connect(
1024-
&self, node_id: PublicKey, address: NetAddress, permanently: bool,
1024+
&self, node_id: PublicKey, address: NetAddress, persist: bool,
10251025
) -> Result<(), Error> {
10261026
let rt_lock = self.runtime.read().unwrap();
10271027
if rt_lock.is_none() {
@@ -1052,7 +1052,7 @@ impl Node {
10521052

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

1055-
if permanently {
1055+
if persist {
10561056
self.peer_store.add_peer(peer_info)?;
10571057
}
10581058

@@ -1550,17 +1550,43 @@ impl Node {
15501550

15511551
/// Retrieves a list of known peers.
15521552
pub fn list_peers(&self) -> Vec<PeerDetails> {
1553-
let active_connected_peers: Vec<PublicKey> =
1554-
self.peer_manager.get_peer_node_ids().iter().map(|p| p.0).collect();
1555-
self.peer_store
1556-
.list_peers()
1557-
.iter()
1558-
.map(|p| PeerDetails {
1553+
let mut peers = Vec::new();
1554+
1555+
// First add all connected peers, preferring to list the connected address if available.
1556+
let connected_peers = self.peer_manager.get_peer_node_ids();
1557+
for (node_id, con_addr_opt) in connected_peers {
1558+
let stored_peer = self.peer_store.get_peer(&node_id);
1559+
let stored_addr_opt = stored_peer.as_ref().map(|p| p.address.clone());
1560+
let address = match (con_addr_opt, stored_addr_opt) {
1561+
(Some(con_addr), Some(_stored_addr)) => NetAddress(con_addr),
1562+
(None, Some(stored_addr)) => stored_addr,
1563+
(Some(con_addr), None) => NetAddress(con_addr),
1564+
(None, None) => continue,
1565+
};
1566+
1567+
let is_persisted = stored_peer.is_some();
1568+
let is_connected = true;
1569+
let details = PeerDetails { node_id, address, is_persisted, is_connected };
1570+
peers.push(details);
1571+
}
1572+
1573+
// Now add all known-but-offline peers, too.
1574+
for p in self.peer_store.list_peers() {
1575+
if peers.iter().find(|d| d.node_id == p.node_id).is_some() {
1576+
continue;
1577+
}
1578+
1579+
let details = PeerDetails {
15591580
node_id: p.node_id,
1560-
address: p.address.clone(),
1561-
is_connected: active_connected_peers.contains(&p.node_id),
1562-
})
1563-
.collect()
1581+
address: p.address,
1582+
is_persisted: true,
1583+
is_connected: false,
1584+
};
1585+
1586+
peers.push(details);
1587+
}
1588+
1589+
peers
15641590
}
15651591

15661592
/// 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
@@ -397,11 +397,13 @@ impl From<LdkChannelDetails> for ChannelDetails {
397397
/// [`Node::list_peers`]: [`crate::Node::list_peers`]
398398
#[derive(Debug, Clone, PartialEq, Eq)]
399399
pub struct PeerDetails {
400-
/// Our peer's node ID.
400+
/// The node ID of the peer.
401401
pub node_id: PublicKey,
402-
/// The IP address and TCP port of the peer.
402+
/// The network address of the peer.
403403
pub address: NetAddress,
404-
/// Indicates whether or not the user is currently has an active connection with the peer.
404+
/// Indicates whether we'll try to reconnect to this peer after restarts.
405+
pub is_persisted: bool,
406+
/// Indicates whether we currently have an active connection with the peer.
405407
pub is_connected: bool,
406408
}
407409

0 commit comments

Comments
 (0)