Skip to content

Commit 025b6d2

Browse files
committed
Expose list_{channels, peers} to bindings
1 parent 49f7f59 commit 025b6d2

File tree

4 files changed

+201
-4
lines changed

4 files changed

+201
-4
lines changed

bindings/ldk_node.udl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ interface Node {
5151
[Throws=NodeError]
5252
Invoice receive_variable_amount_payment([ByRef]string description, u32 expiry_secs);
5353
PaymentDetails? payment([ByRef]PaymentHash payment_hash);
54+
sequence<PeerDetails> list_peers();
55+
sequence<ChannelDetails> list_channels();
5456
};
5557

5658
[Error]
@@ -67,6 +69,7 @@ enum NodeError {
6769
"NonUniquePaymentHash",
6870
"InvalidAmount",
6971
"InvalidInvoice",
72+
"InvalidTxid",
7073
"InvoiceCreationFailed",
7174
"InsufficientFunds",
7275
"PaymentFailed",
@@ -116,6 +119,34 @@ dictionary OutPoint {
116119
u32 vout;
117120
};
118121

122+
dictionary ChannelDetails {
123+
ChannelId channel_id;
124+
PublicKey counterparty_node_id;
125+
OutPoint? funding_txo;
126+
u64? short_channel_id;
127+
u64? outbound_scid_alias;
128+
u64? inbound_scid_alias;
129+
u64 channel_value_satoshis;
130+
u64? unspendable_punishment_reserve;
131+
UserChannelId user_channel_id;
132+
u64 balance_msat;
133+
u64 outbound_capacity_msat;
134+
u64 inbound_capacity_msat;
135+
u32? confirmations_required;
136+
u32? confirmations;
137+
boolean is_outbound;
138+
boolean is_channel_ready;
139+
boolean is_usable;
140+
boolean is_public;
141+
u16? cltv_expiry_delta;
142+
};
143+
144+
dictionary PeerDetails {
145+
PublicKey node_id;
146+
SocketAddr address;
147+
boolean is_connected;
148+
};
149+
119150
[Custom]
120151
typedef string Txid;
121152

src/lib.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ use types::{
104104
ChainMonitor, ChannelManager, GossipSync, KeysManager, Network, NetworkGraph, OnionMessenger,
105105
PeerManager, Scorer,
106106
};
107-
pub use types::{ChannelId, UserChannelId};
107+
pub use types::{ChannelDetails, ChannelId, PeerDetails, UserChannelId};
108108
use wallet::Wallet;
109109

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

112112
use lightning::chain::keysinterface::EntropySource;
113113
use lightning::chain::{chainmonitor, BestBlock, Confirm, Watch};
114114
use lightning::ln::channelmanager::{
115-
self, ChainParameters, ChannelDetails, ChannelManagerReadArgs, PaymentId, RecipientOnionFields,
115+
self, ChainParameters, ChannelManagerReadArgs, PaymentId, RecipientOnionFields,
116116
Retry,
117117
};
118118
use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler};
@@ -852,7 +852,7 @@ impl Node {
852852

853853
/// Retrieve a list of known channels.
854854
pub fn list_channels(&self) -> Vec<ChannelDetails> {
855-
self.channel_manager.list_channels()
855+
self.channel_manager.list_channels().into_iter().map(|c| c.into()).collect()
856856
}
857857

858858
/// Connect to a node on the peer-to-peer network.
@@ -1383,6 +1383,21 @@ impl Node {
13831383
) -> Vec<PaymentDetails> {
13841384
self.payment_store.list_filter(f)
13851385
}
1386+
1387+
/// Retrieves a list of known peers.
1388+
pub fn list_peers(&self) -> Vec<PeerDetails> {
1389+
let active_connected_peers: Vec<PublicKey> =
1390+
self.peer_manager.get_peer_node_ids().iter().map(|p| p.0).collect();
1391+
self.peer_store
1392+
.list_peers()
1393+
.iter()
1394+
.map(|p| PeerDetails {
1395+
node_id: p.pubkey,
1396+
address: p.address,
1397+
is_connected: active_connected_peers.contains(&p.pubkey),
1398+
})
1399+
.collect()
1400+
}
13861401
}
13871402

13881403
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
@@ -36,6 +36,10 @@ fn channel_full_cycle() {
3636
.connect_open_channel(node_b.node_id(), *node_b.listening_address().unwrap(), 50000, true)
3737
.unwrap();
3838

39+
assert_eq!(
40+
node_a.list_peers().iter().map(|p| p.node_id).collect::<Vec<_>>(),
41+
[node_b.node_id()]
42+
);
3943
expect_event!(node_a, ChannelPending);
4044

4145
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, Txid};
26+
use bitcoin::{Address, OutPoint, Txid};
2627

2728
use std::convert::TryInto;
2829
use std::default::Default;
@@ -350,3 +351,149 @@ impl UniffiCustomTypeConverter for Txid {
350351
obj.to_string()
351352
}
352353
}
354+
355+
/// Details of a channel as returned by [`Node::list_channels`].
356+
///
357+
/// [`Node::list_channels`]: [`crate::Node::list_channels`]
358+
pub struct ChannelDetails {
359+
/// The channel's ID (prior to funding transaction generation, this is a random 32 bytes,
360+
/// thereafter this is the transaction ID of the funding transaction XOR the funding transaction
361+
/// output).
362+
///
363+
/// Note that this means this value is *not* persistent - it can change once during the
364+
/// lifetime of the channel.
365+
pub channel_id: ChannelId,
366+
/// The `node_id` of our channel's counterparty.
367+
pub counterparty_node_id: PublicKey,
368+
/// The channel's funding transaction output, if we've negotiated the funding transaction with
369+
/// our counterparty already.
370+
pub funding_txo: Option<OutPoint>,
371+
/// Position of the funding transaction on-chain. `None` unless the funding transaction has been
372+
/// confirmed and the channel is fully opened.
373+
///
374+
/// Note that if [`inbound_scid_alias`] is set, it must be used for invoices and inbound
375+
/// payments instead of this.
376+
///
377+
/// For channels with [`confirmations_required`] set to `Some(0)`, [`outbound_scid_alias`] may
378+
/// be used in place of this in outbound routes.
379+
///
380+
/// [`inbound_scid_alias`]: Self::inbound_scid_alias
381+
/// [`outbound_scid_alias`]: Self::outbound_scid_alias
382+
/// [`confirmations_required`]: Self::confirmations_required
383+
pub short_channel_id: Option<u64>,
384+
/// An optional [`short_channel_id`] alias for this channel, randomly generated by us and
385+
/// usable in place of [`short_channel_id`] to reference the channel in outbound routes when
386+
/// the channel has not yet been confirmed (as long as [`confirmations_required`] is
387+
/// `Some(0)`).
388+
///
389+
/// This will be `None` as long as the channel is not available for routing outbound payments.
390+
///
391+
/// [`short_channel_id`]: Self::short_channel_id
392+
/// [`confirmations_required`]: Self::confirmations_required
393+
pub outbound_scid_alias: Option<u64>,
394+
/// An optional [`short_channel_id`] alias for this channel, randomly generated by our
395+
/// counterparty and usable in place of [`short_channel_id`] in invoice route hints. Our
396+
/// counterparty will recognize the alias provided here in place of the [`short_channel_id`]
397+
/// when they see a payment to be routed to us.
398+
///
399+
/// Our counterparty may choose to rotate this value at any time, though will always recognize
400+
/// previous values for inbound payment forwarding.
401+
///
402+
/// [`short_channel_id`]: Self::short_channel_id
403+
pub inbound_scid_alias: Option<u64>,
404+
/// The value, in satoshis, of this channel as appears in the funding output.
405+
pub channel_value_satoshis: u64,
406+
/// The value, in satoshis, that must always be held in the channel for us. This value ensures
407+
/// that if we broadcast a revoked state, our counterparty can punish us by claiming at least
408+
/// this value on chain.
409+
///
410+
/// This value is not included in [`outbound_capacity_msat`] as it can never be spent.
411+
///
412+
/// This value will be `None` for outbound channels until the counterparty accepts the channel.
413+
///
414+
/// [`outbound_capacity_msat`]: Self::outbound_capacity_msat
415+
pub unspendable_punishment_reserve: Option<u64>,
416+
/// The local `user_channel_id` of this channel.
417+
pub user_channel_id: UserChannelId,
418+
/// Total balance of the channel. This is the amount that will be returned to the user if the
419+
/// channel is closed.
420+
///
421+
/// The value is not exact, due to potential in-flight and fee-rate changes. Therefore, exactly
422+
/// this amount is likely irrecoverable on close.
423+
pub balance_msat: u64,
424+
/// Available outbound capacity for sending HTLCs to the remote peer.
425+
///
426+
/// The amount does not include any pending HTLCs which are not yet resolved (and, thus, whose
427+
/// balance is not available for inclusion in new outbound HTLCs). This further does not include
428+
/// any pending outgoing HTLCs which are awaiting some other resolution to be sent.
429+
pub outbound_capacity_msat: u64,
430+
/// Available outbound capacity for sending HTLCs to the remote peer.
431+
///
432+
/// The amount does not include any pending HTLCs which are not yet resolved
433+
/// (and, thus, whose balance is not available for inclusion in new inbound HTLCs). This further
434+
/// does not include any pending outgoing HTLCs which are awaiting some other resolution to be
435+
/// sent.
436+
pub inbound_capacity_msat: u64,
437+
/// The number of required confirmations on the funding transactions before the funding is
438+
/// considered "locked". The amount is selected by the channel fundee.
439+
///
440+
/// The value will be `None` for outbound channels until the counterparty accepts the channel.
441+
pub confirmations_required: Option<u32>,
442+
/// The current number of confirmations on the funding transaction.
443+
pub confirmations: Option<u32>,
444+
/// Returns `true` if the channel was initiated (and therefore funded) by us.
445+
pub is_outbound: bool,
446+
/// Returns `true` if the channel is confirmed, both parties have exchanged `channel_ready`
447+
/// messages, and the channel is not currently being shut down. Both parties exchange
448+
/// `channel_ready` messages upon independently verifying that the required confirmations count
449+
/// provided by `confirmations_required` has been reached.
450+
pub is_channel_ready: bool,
451+
/// Returns `true` if the channel is (a) confirmed and `channel_ready` has been exchanged,
452+
/// (b) the peer is connected, and (c) the channel is not currently negotiating shutdown.
453+
///
454+
/// This is a strict superset of `is_channel_ready`.
455+
pub is_usable: bool,
456+
/// Returns `true` if this channel is (or will be) publicly-announced
457+
pub is_public: bool,
458+
/// The difference in the CLTV value between incoming HTLCs and an outbound HTLC forwarded over
459+
/// the channel.
460+
pub cltv_expiry_delta: Option<u16>,
461+
}
462+
463+
impl From<LdkChannelDetails> for ChannelDetails {
464+
fn from(value: LdkChannelDetails) -> Self {
465+
ChannelDetails {
466+
channel_id: ChannelId(value.channel_id),
467+
counterparty_node_id: value.counterparty.node_id,
468+
funding_txo: value.funding_txo.and_then(|o| Some(o.into_bitcoin_outpoint())),
469+
short_channel_id: value.short_channel_id,
470+
outbound_scid_alias: value.outbound_scid_alias,
471+
inbound_scid_alias: value.inbound_scid_alias,
472+
channel_value_satoshis: value.channel_value_satoshis,
473+
unspendable_punishment_reserve: value.unspendable_punishment_reserve,
474+
user_channel_id: UserChannelId(value.user_channel_id),
475+
balance_msat: value.balance_msat,
476+
outbound_capacity_msat: value.outbound_capacity_msat,
477+
inbound_capacity_msat: value.inbound_capacity_msat,
478+
confirmations_required: value.confirmations_required,
479+
confirmations: value.confirmations,
480+
is_outbound: value.is_outbound,
481+
is_channel_ready: value.is_channel_ready,
482+
is_usable: value.is_usable,
483+
is_public: value.is_public,
484+
cltv_expiry_delta: value.config.and_then(|c| Some(c.cltv_expiry_delta)),
485+
}
486+
}
487+
}
488+
489+
/// Details of a known lightning peer as returned by [`Node::list_peers`].
490+
///
491+
/// [`Node::list_peers`]: [`crate::Node::list_peers`]
492+
pub struct PeerDetails {
493+
/// Our peer's node ID.
494+
pub node_id: PublicKey,
495+
/// The IP address and TCP port of the peer.
496+
pub address: SocketAddr,
497+
/// Indicates whether or not the user is currently has an active connection with the peer.
498+
pub is_connected: bool,
499+
}

0 commit comments

Comments
 (0)