Skip to content

Commit 5a1fbe6

Browse files
committed
Expose list_{channels, peers} to bindings
# Conflicts: # bindings/ldk_node.udl # Conflicts: # src/types.rs
1 parent bc7a4ba commit 5a1fbe6

File tree

4 files changed

+200
-5
lines changed

4 files changed

+200
-5
lines changed

bindings/ldk_node.udl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ interface Node {
5858
PaymentDetails? payment([ByRef]PaymentHash payment_hash);
5959
[Throws=NodeError]
6060
boolean remove_payment([ByRef]PaymentHash payment_hash);
61+
sequence<PeerDetails> list_peers();
62+
sequence<ChannelDetails> list_channels();
6163
};
6264

6365
[Error]
@@ -123,6 +125,34 @@ dictionary OutPoint {
123125
u32 vout;
124126
};
125127

128+
dictionary ChannelDetails {
129+
ChannelId channel_id;
130+
PublicKey counterparty_node_id;
131+
OutPoint? funding_txo;
132+
u64? short_channel_id;
133+
u64? outbound_scid_alias;
134+
u64? inbound_scid_alias;
135+
u64 channel_value_satoshis;
136+
u64? unspendable_punishment_reserve;
137+
UserChannelId user_channel_id;
138+
u64 balance_msat;
139+
u64 outbound_capacity_msat;
140+
u64 inbound_capacity_msat;
141+
u32? confirmations_required;
142+
u32? confirmations;
143+
boolean is_outbound;
144+
boolean is_channel_ready;
145+
boolean is_usable;
146+
boolean is_public;
147+
u16? cltv_expiry_delta;
148+
};
149+
150+
dictionary PeerDetails {
151+
PublicKey node_id;
152+
SocketAddr address;
153+
boolean is_connected;
154+
};
155+
126156
[Custom]
127157
typedef string Txid;
128158

src/lib.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,15 @@ use types::{
105105
ChainMonitor, ChannelManager, GossipSync, KeysManager, NetworkGraph, OnionMessenger,
106106
PeerManager, Scorer,
107107
};
108-
pub use types::{ChannelId, UserChannelId};
108+
pub use types::{ChannelDetails, ChannelId, PeerDetails, UserChannelId};
109109
use wallet::Wallet;
110110

111111
use logger::{log_error, log_info, FilesystemLogger, Logger};
112112

113113
use lightning::chain::keysinterface::EntropySource;
114114
use lightning::chain::{chainmonitor, BestBlock, Confirm, Watch};
115115
use lightning::ln::channelmanager::{
116-
self, ChainParameters, ChannelDetails, ChannelManagerReadArgs, PaymentId, RecipientOnionFields,
117-
Retry,
116+
self, ChainParameters, ChannelManagerReadArgs, PaymentId, RecipientOnionFields, Retry,
118117
};
119118
use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler};
120119
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
@@ -903,7 +902,7 @@ impl Node {
903902

904903
/// Retrieve a list of known channels.
905904
pub fn list_channels(&self) -> Vec<ChannelDetails> {
906-
self.channel_manager.list_channels()
905+
self.channel_manager.list_channels().into_iter().map(|c| c.into()).collect()
907906
}
908907

909908
/// Connect to a node on the peer-to-peer network.
@@ -1438,6 +1437,21 @@ impl Node {
14381437
) -> Vec<PaymentDetails> {
14391438
self.payment_store.list_filter(f)
14401439
}
1440+
1441+
/// Retrieves a list of known peers.
1442+
pub fn list_peers(&self) -> Vec<PeerDetails> {
1443+
let active_connected_peers: Vec<PublicKey> =
1444+
self.peer_manager.get_peer_node_ids().iter().map(|p| p.0).collect();
1445+
self.peer_store
1446+
.list_peers()
1447+
.iter()
1448+
.map(|p| PeerDetails {
1449+
node_id: p.pubkey,
1450+
address: p.address,
1451+
is_connected: active_connected_peers.contains(&p.pubkey),
1452+
})
1453+
.collect()
1454+
}
14411455
}
14421456

14431457
async fn connect_peer_if_necessary(

src/test/functional_tests.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ fn channel_full_cycle() {
4646
)
4747
.unwrap();
4848

49+
assert_eq!(
50+
node_a.list_peers().iter().map(|p| p.node_id).collect::<Vec<_>>(),
51+
[node_b.node_id()]
52+
);
4953
expect_event!(node_a, ChannelPending);
5054

5155
let funding_txo = match node_b.next_event() {

src/types.rs

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::UniffiCustomTypeConverter;
77

88
use lightning::chain::chainmonitor;
99
use lightning::chain::keysinterface::InMemorySigner;
10+
use lightning::ln::channelmanager::ChannelDetails as LdkChannelDetails;
1011
use lightning::ln::peer_handler::IgnoringMessageHandler;
1112
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
1213
use lightning::routing::gossip;
@@ -22,7 +23,7 @@ use lightning_transaction_sync::EsploraSyncClient;
2223
use bitcoin::hashes::sha256::Hash as Sha256;
2324
use bitcoin::hashes::Hash;
2425
use bitcoin::secp256k1::PublicKey;
25-
use bitcoin::{Address, Network, Txid};
26+
use bitcoin::{Address, Network, OutPoint, Txid};
2627

2728
use std::convert::TryInto;
2829
use std::net::SocketAddr;
@@ -290,3 +291,149 @@ impl UniffiCustomTypeConverter for Txid {
290291
obj.to_string()
291292
}
292293
}
294+
295+
/// Details of a channel as returned by [`Node::list_channels`].
296+
///
297+
/// [`Node::list_channels`]: [`crate::Node::list_channels`]
298+
pub struct ChannelDetails {
299+
/// The channel's ID (prior to funding transaction generation, this is a random 32 bytes,
300+
/// thereafter this is the transaction ID of the funding transaction XOR the funding transaction
301+
/// output).
302+
///
303+
/// Note that this means this value is *not* persistent - it can change once during the
304+
/// lifetime of the channel.
305+
pub channel_id: ChannelId,
306+
/// The `node_id` of our channel's counterparty.
307+
pub counterparty_node_id: PublicKey,
308+
/// The channel's funding transaction output, if we've negotiated the funding transaction with
309+
/// our counterparty already.
310+
pub funding_txo: Option<OutPoint>,
311+
/// Position of the funding transaction on-chain. `None` unless the funding transaction has been
312+
/// confirmed and the channel is fully opened.
313+
///
314+
/// Note that if [`inbound_scid_alias`] is set, it must be used for invoices and inbound
315+
/// payments instead of this.
316+
///
317+
/// For channels with [`confirmations_required`] set to `Some(0)`, [`outbound_scid_alias`] may
318+
/// be used in place of this in outbound routes.
319+
///
320+
/// [`inbound_scid_alias`]: Self::inbound_scid_alias
321+
/// [`outbound_scid_alias`]: Self::outbound_scid_alias
322+
/// [`confirmations_required`]: Self::confirmations_required
323+
pub short_channel_id: Option<u64>,
324+
/// An optional [`short_channel_id`] alias for this channel, randomly generated by us and
325+
/// usable in place of [`short_channel_id`] to reference the channel in outbound routes when
326+
/// the channel has not yet been confirmed (as long as [`confirmations_required`] is
327+
/// `Some(0)`).
328+
///
329+
/// This will be `None` as long as the channel is not available for routing outbound payments.
330+
///
331+
/// [`short_channel_id`]: Self::short_channel_id
332+
/// [`confirmations_required`]: Self::confirmations_required
333+
pub outbound_scid_alias: Option<u64>,
334+
/// An optional [`short_channel_id`] alias for this channel, randomly generated by our
335+
/// counterparty and usable in place of [`short_channel_id`] in invoice route hints. Our
336+
/// counterparty will recognize the alias provided here in place of the [`short_channel_id`]
337+
/// when they see a payment to be routed to us.
338+
///
339+
/// Our counterparty may choose to rotate this value at any time, though will always recognize
340+
/// previous values for inbound payment forwarding.
341+
///
342+
/// [`short_channel_id`]: Self::short_channel_id
343+
pub inbound_scid_alias: Option<u64>,
344+
/// The value, in satoshis, of this channel as appears in the funding output.
345+
pub channel_value_satoshis: u64,
346+
/// The value, in satoshis, that must always be held in the channel for us. This value ensures
347+
/// that if we broadcast a revoked state, our counterparty can punish us by claiming at least
348+
/// this value on chain.
349+
///
350+
/// This value is not included in [`outbound_capacity_msat`] as it can never be spent.
351+
///
352+
/// This value will be `None` for outbound channels until the counterparty accepts the channel.
353+
///
354+
/// [`outbound_capacity_msat`]: Self::outbound_capacity_msat
355+
pub unspendable_punishment_reserve: Option<u64>,
356+
/// The local `user_channel_id` of this channel.
357+
pub user_channel_id: UserChannelId,
358+
/// Total balance of the channel. This is the amount that will be returned to the user if the
359+
/// channel is closed.
360+
///
361+
/// The value is not exact, due to potential in-flight and fee-rate changes. Therefore, exactly
362+
/// this amount is likely irrecoverable on close.
363+
pub balance_msat: u64,
364+
/// Available outbound capacity for sending HTLCs to the remote peer.
365+
///
366+
/// The amount does not include any pending HTLCs which are not yet resolved (and, thus, whose
367+
/// balance is not available for inclusion in new outbound HTLCs). This further does not include
368+
/// any pending outgoing HTLCs which are awaiting some other resolution to be sent.
369+
pub outbound_capacity_msat: u64,
370+
/// Available outbound capacity for sending HTLCs to the remote peer.
371+
///
372+
/// The amount does not include any pending HTLCs which are not yet resolved
373+
/// (and, thus, whose balance is not available for inclusion in new inbound HTLCs). This further
374+
/// does not include any pending outgoing HTLCs which are awaiting some other resolution to be
375+
/// sent.
376+
pub inbound_capacity_msat: u64,
377+
/// The number of required confirmations on the funding transactions before the funding is
378+
/// considered "locked". The amount is selected by the channel fundee.
379+
///
380+
/// The value will be `None` for outbound channels until the counterparty accepts the channel.
381+
pub confirmations_required: Option<u32>,
382+
/// The current number of confirmations on the funding transaction.
383+
pub confirmations: Option<u32>,
384+
/// Returns `true` if the channel was initiated (and therefore funded) by us.
385+
pub is_outbound: bool,
386+
/// Returns `true` if the channel is confirmed, both parties have exchanged `channel_ready`
387+
/// messages, and the channel is not currently being shut down. Both parties exchange
388+
/// `channel_ready` messages upon independently verifying that the required confirmations count
389+
/// provided by `confirmations_required` has been reached.
390+
pub is_channel_ready: bool,
391+
/// Returns `true` if the channel is (a) confirmed and `channel_ready` has been exchanged,
392+
/// (b) the peer is connected, and (c) the channel is not currently negotiating shutdown.
393+
///
394+
/// This is a strict superset of `is_channel_ready`.
395+
pub is_usable: bool,
396+
/// Returns `true` if this channel is (or will be) publicly-announced
397+
pub is_public: bool,
398+
/// The difference in the CLTV value between incoming HTLCs and an outbound HTLC forwarded over
399+
/// the channel.
400+
pub cltv_expiry_delta: Option<u16>,
401+
}
402+
403+
impl From<LdkChannelDetails> for ChannelDetails {
404+
fn from(value: LdkChannelDetails) -> Self {
405+
ChannelDetails {
406+
channel_id: ChannelId(value.channel_id),
407+
counterparty_node_id: value.counterparty.node_id,
408+
funding_txo: value.funding_txo.and_then(|o| Some(o.into_bitcoin_outpoint())),
409+
short_channel_id: value.short_channel_id,
410+
outbound_scid_alias: value.outbound_scid_alias,
411+
inbound_scid_alias: value.inbound_scid_alias,
412+
channel_value_satoshis: value.channel_value_satoshis,
413+
unspendable_punishment_reserve: value.unspendable_punishment_reserve,
414+
user_channel_id: UserChannelId(value.user_channel_id),
415+
balance_msat: value.balance_msat,
416+
outbound_capacity_msat: value.outbound_capacity_msat,
417+
inbound_capacity_msat: value.inbound_capacity_msat,
418+
confirmations_required: value.confirmations_required,
419+
confirmations: value.confirmations,
420+
is_outbound: value.is_outbound,
421+
is_channel_ready: value.is_channel_ready,
422+
is_usable: value.is_usable,
423+
is_public: value.is_public,
424+
cltv_expiry_delta: value.config.and_then(|c| Some(c.cltv_expiry_delta)),
425+
}
426+
}
427+
}
428+
429+
/// Details of a known lightning peer as returned by [`Node::list_peers`].
430+
///
431+
/// [`Node::list_peers`]: [`crate::Node::list_peers`]
432+
pub struct PeerDetails {
433+
/// Our peer's node ID.
434+
pub node_id: PublicKey,
435+
/// The IP address and TCP port of the peer.
436+
pub address: SocketAddr,
437+
/// Indicates whether or not the user is currently has an active connection with the peer.
438+
pub is_connected: bool,
439+
}

0 commit comments

Comments
 (0)