Skip to content

Commit c929b42

Browse files
committed
Include non-permanently connected peers in list_peers()
1 parent 8ba0604 commit c929b42

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
@@ -37,7 +37,7 @@ interface Node {
3737
[Throws=NodeError]
3838
u64 total_onchain_balance_sats();
3939
[Throws=NodeError]
40-
void connect(PublicKey node_id, NetAddress address, boolean permanently);
40+
void connect(PublicKey node_id, NetAddress address, boolean persist);
4141
[Throws=NodeError]
4242
void disconnect(PublicKey node_id);
4343
[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
@@ -1063,9 +1063,9 @@ impl Node {
10631063

10641064
/// Connect to a node on the peer-to-peer network.
10651065
///
1066-
/// If `permanently` is set to `true`, we'll remember the peer and reconnect to it on restart.
1066+
/// If `persist` is set to `true`, we'll remember the peer and reconnect to it on restart.
10671067
pub fn connect(
1068-
&self, node_id: PublicKey, address: NetAddress, permanently: bool,
1068+
&self, node_id: PublicKey, address: NetAddress, persist: bool,
10691069
) -> Result<(), Error> {
10701070
let rt_lock = self.runtime.read().unwrap();
10711071
if rt_lock.is_none() {
@@ -1096,7 +1096,7 @@ impl Node {
10961096

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

1099-
if permanently {
1099+
if persist {
11001100
self.peer_store.add_peer(peer_info)?;
11011101
}
11021102

@@ -1594,17 +1594,43 @@ impl Node {
15941594

15951595
/// Retrieves a list of known peers.
15961596
pub fn list_peers(&self) -> Vec<PeerDetails> {
1597-
let active_connected_peers: Vec<PublicKey> =
1598-
self.peer_manager.get_peer_node_ids().iter().map(|p| p.0).collect();
1599-
self.peer_store
1600-
.list_peers()
1601-
.iter()
1602-
.map(|p| PeerDetails {
1597+
let mut peers = Vec::new();
1598+
1599+
// First add all connected peers, preferring to list the connected address if available.
1600+
let connected_peers = self.peer_manager.get_peer_node_ids();
1601+
for (node_id, con_addr_opt) in connected_peers {
1602+
let stored_peer = self.peer_store.get_peer(&node_id);
1603+
let stored_addr_opt = stored_peer.as_ref().map(|p| p.address.clone());
1604+
let address = match (con_addr_opt, stored_addr_opt) {
1605+
(Some(con_addr), Some(_stored_addr)) => NetAddress(con_addr),
1606+
(None, Some(stored_addr)) => stored_addr,
1607+
(Some(con_addr), None) => NetAddress(con_addr),
1608+
(None, None) => continue,
1609+
};
1610+
1611+
let is_persisted = stored_peer.is_some();
1612+
let is_connected = true;
1613+
let details = PeerDetails { node_id, address, is_persisted, is_connected };
1614+
peers.push(details);
1615+
}
1616+
1617+
// Now add all known-but-offline peers, too.
1618+
for p in self.peer_store.list_peers() {
1619+
if peers.iter().find(|d| d.node_id == p.node_id).is_some() {
1620+
continue;
1621+
}
1622+
1623+
let details = PeerDetails {
16031624
node_id: p.node_id,
1604-
address: p.address.clone(),
1605-
is_connected: active_connected_peers.contains(&p.node_id),
1606-
})
1607-
.collect()
1625+
address: p.address,
1626+
is_persisted: true,
1627+
is_connected: false,
1628+
};
1629+
1630+
peers.push(details);
1631+
}
1632+
1633+
peers
16081634
}
16091635

16101636
/// 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)