Skip to content

Commit 56fe49c

Browse files
authored
Merge pull request #95 from tnull/2023-05-list-all-peers
Include non-permanently connected peers in `list_peers()`
2 parents e93f564 + f562395 commit 56fe49c

File tree

3 files changed

+70
-41
lines changed

3 files changed

+70
-41
lines changed

bindings/ldk_node.udl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ interface LDKNode {
5050
[Throws=NodeError]
5151
u64 total_onchain_balance_sats();
5252
[Throws=NodeError]
53-
void connect(PublicKey node_id, NetAddress address, boolean permanently);
53+
void connect(PublicKey node_id, NetAddress address, boolean persist);
5454
[Throws=NodeError]
5555
void disconnect(PublicKey node_id);
5656
[Throws=NodeError]
@@ -158,7 +158,7 @@ dictionary ChannelDetails {
158158
ChannelId channel_id;
159159
PublicKey counterparty_node_id;
160160
OutPoint? funding_txo;
161-
u64 channel_value_satoshis;
161+
u64 channel_value_sats;
162162
u64? unspendable_punishment_reserve;
163163
UserChannelId user_channel_id;
164164
u32 feerate_sat_per_1000_weight;
@@ -177,6 +177,7 @@ dictionary ChannelDetails {
177177
dictionary PeerDetails {
178178
PublicKey node_id;
179179
NetAddress address;
180+
boolean is_persisted;
180181
boolean is_connected;
181182
};
182183

src/lib.rs

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,9 +1301,9 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
13011301

13021302
/// Connect to a node on the peer-to-peer network.
13031303
///
1304-
/// If `permanently` is set to `true`, we'll remember the peer and reconnect to it on restart.
1304+
/// If `persist` is set to `true`, we'll remember the peer and reconnect to it on restart.
13051305
pub fn connect(
1306-
&self, node_id: PublicKey, address: NetAddress, permanently: bool,
1306+
&self, node_id: PublicKey, address: NetAddress, persist: bool,
13071307
) -> Result<(), Error> {
13081308
let rt_lock = self.runtime.read().unwrap();
13091309
if rt_lock.is_none() {
@@ -1328,7 +1328,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
13281328

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

1331-
if permanently {
1331+
if persist {
13321332
self.peer_store.add_peer(peer_info)?;
13331333
}
13341334

@@ -1850,25 +1850,51 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
18501850

18511851
/// Retrieves a list of known peers.
18521852
pub fn list_peers(&self) -> Vec<PeerDetails> {
1853-
let active_connected_peers: Vec<PublicKey> =
1854-
self.peer_manager.get_peer_node_ids().iter().map(|p| p.0).collect();
1855-
self.peer_store
1856-
.list_peers()
1857-
.iter()
1858-
.map(|p| PeerDetails {
1853+
let mut peers = Vec::new();
1854+
1855+
// First add all connected peers, preferring to list the connected address if available.
1856+
let connected_peers = self.peer_manager.get_peer_node_ids();
1857+
let connected_peers_len = connected_peers.len();
1858+
for (node_id, con_addr_opt) in connected_peers {
1859+
let stored_peer = self.peer_store.get_peer(&node_id);
1860+
let stored_addr_opt = stored_peer.as_ref().map(|p| p.address.clone());
1861+
let address = match (con_addr_opt, stored_addr_opt) {
1862+
(Some(con_addr), _) => NetAddress(con_addr),
1863+
(None, Some(stored_addr)) => stored_addr,
1864+
(None, None) => continue,
1865+
};
1866+
1867+
let is_persisted = stored_peer.is_some();
1868+
let is_connected = true;
1869+
let details = PeerDetails { node_id, address, is_persisted, is_connected };
1870+
peers.push(details);
1871+
}
1872+
1873+
// Now add all known-but-offline peers, too.
1874+
for p in self.peer_store.list_peers() {
1875+
if peers.iter().take(connected_peers_len).find(|d| d.node_id == p.node_id).is_some() {
1876+
continue;
1877+
}
1878+
1879+
let details = PeerDetails {
18591880
node_id: p.node_id,
1860-
address: p.address.clone(),
1861-
is_connected: active_connected_peers.contains(&p.node_id),
1862-
})
1863-
.collect()
1881+
address: p.address,
1882+
is_persisted: true,
1883+
is_connected: false,
1884+
};
1885+
1886+
peers.push(details);
1887+
}
1888+
1889+
peers
18641890
}
18651891

18661892
/// Creates a digital ECDSA signature of a message with the node's secret key.
18671893
///
18681894
/// A receiver knowing the corresponding `PublicKey` (e.g. the node’s id) and the message
18691895
/// can be sure that the signature was generated by the caller.
18701896
/// Signatures are EC recoverable, meaning that given the message and the
1871-
/// signature the PublicKey of the signer can be extracted.
1897+
/// signature the `PublicKey` of the signer can be extracted.
18721898
pub fn sign_message(&self, msg: &[u8]) -> Result<String, Error> {
18731899
self.keys_manager.sign_message(msg)
18741900
}

src/types.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,26 @@ impl Readable for UserChannelId {
130130

131131
/// Details of a channel as returned by [`Node::list_channels`].
132132
///
133-
/// [`Node::list_channels`]: [`crate::Node::list_channels`]
133+
/// [`Node::list_channels`]: crate::Node::list_channels
134134
#[derive(Debug, Clone, PartialEq, Eq)]
135135
pub struct ChannelDetails {
136-
/// The channel's ID (prior to funding transaction generation, this is a random 32 bytes,
137-
/// thereafter this is the transaction ID of the funding transaction XOR the funding transaction
138-
/// output).
136+
/// The channel ID (prior to funding transaction generation, this is a random 32-byte
137+
/// identifier, afterwards this is the transaction ID of the funding transaction XOR the
138+
/// funding transaction output).
139139
///
140140
/// Note that this means this value is *not* persistent - it can change once during the
141141
/// lifetime of the channel.
142142
pub channel_id: ChannelId,
143-
/// The `node_id` of our channel's counterparty.
143+
/// The node ID of our the channel's counterparty.
144144
pub counterparty_node_id: PublicKey,
145145
/// The channel's funding transaction output, if we've negotiated the funding transaction with
146146
/// our counterparty already.
147147
pub funding_txo: Option<OutPoint>,
148-
/// The value, in satoshis, of this channel as appears in the funding output.
149-
pub channel_value_satoshis: u64,
150-
/// The value, in satoshis, that must always be held in the channel for us. This value ensures
151-
/// that if we broadcast a revoked state, our counterparty can punish us by claiming at least
152-
/// this value on chain.
148+
/// The value, in satoshis, of this channel as it appears in the funding output.
149+
pub channel_value_sats: u64,
150+
/// The value, in satoshis, that must always be held as a reserve in the channel for us. This
151+
/// value ensures that if we broadcast a revoked state, our counterparty can punish us by
152+
/// claiming at least this value on chain.
153153
///
154154
/// This value is not included in [`outbound_capacity_msat`] as it can never be spent.
155155
///
@@ -162,19 +162,19 @@ pub struct ChannelDetails {
162162
/// The currently negotiated fee rate denominated in satoshi per 1000 weight units,
163163
/// which is applied to commitment and HTLC transactions.
164164
pub feerate_sat_per_1000_weight: u32,
165-
/// Total balance of the channel. This is the amount that will be returned to the user if the
166-
/// channel is closed.
165+
/// The total balance of the channel. This is the amount that will be returned to
166+
/// the user if the channel is closed.
167167
///
168168
/// The value is not exact, due to potential in-flight and fee-rate changes. Therefore, exactly
169169
/// this amount is likely irrecoverable on close.
170170
pub balance_msat: u64,
171-
/// Available outbound capacity for sending HTLCs to the remote peer.
171+
/// The available outbound capacity for sending HTLCs to the remote peer.
172172
///
173173
/// The amount does not include any pending HTLCs which are not yet resolved (and, thus, whose
174174
/// balance is not available for inclusion in new outbound HTLCs). This further does not include
175175
/// any pending outgoing HTLCs which are awaiting some other resolution to be sent.
176176
pub outbound_capacity_msat: u64,
177-
/// Available outbound capacity for sending HTLCs to the remote peer.
177+
/// The available outbound capacity for sending HTLCs to the remote peer.
178178
///
179179
/// The amount does not include any pending HTLCs which are not yet resolved
180180
/// (and, thus, whose balance is not available for inclusion in new inbound HTLCs). This further
@@ -190,13 +190,13 @@ pub struct ChannelDetails {
190190
pub confirmations: Option<u32>,
191191
/// Returns `true` if the channel was initiated (and therefore funded) by us.
192192
pub is_outbound: bool,
193-
/// Returns `true` if the channel is confirmed, both parties have exchanged `channel_ready`
194-
/// messages, and the channel is not currently being shut down. Both parties exchange
195-
/// `channel_ready` messages upon independently verifying that the required confirmations count
196-
/// provided by `confirmations_required` has been reached.
193+
/// Returns `true` if both parties have exchanged `channel_ready` messages, and the channel is
194+
/// not currently being shut down. Both parties exchange `channel_ready` messages upon
195+
/// independently verifying that the required confirmations count provided by
196+
/// `confirmations_required` has been reached.
197197
pub is_channel_ready: bool,
198-
/// Returns `true` if the channel is (a) confirmed and `channel_ready` has been exchanged,
199-
/// (b) the peer is connected, and (c) the channel is not currently negotiating shutdown.
198+
/// Returns `true` if the channel (a) `channel_ready` messages have been exchanged, (b) the
199+
/// peer is connected, and (c) the channel is not currently negotiating shutdown.
200200
///
201201
/// This is a strict superset of `is_channel_ready`.
202202
pub is_usable: bool,
@@ -213,7 +213,7 @@ impl From<LdkChannelDetails> for ChannelDetails {
213213
channel_id: ChannelId(value.channel_id),
214214
counterparty_node_id: value.counterparty.node_id,
215215
funding_txo: value.funding_txo.and_then(|o| Some(o.into_bitcoin_outpoint())),
216-
channel_value_satoshis: value.channel_value_satoshis,
216+
channel_value_sats: value.channel_value_satoshis,
217217
unspendable_punishment_reserve: value.unspendable_punishment_reserve,
218218
user_channel_id: UserChannelId(value.user_channel_id),
219219
balance_msat: value.balance_msat,
@@ -233,14 +233,16 @@ impl From<LdkChannelDetails> for ChannelDetails {
233233

234234
/// Details of a known Lightning peer as returned by [`Node::list_peers`].
235235
///
236-
/// [`Node::list_peers`]: [`crate::Node::list_peers`]
236+
/// [`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)