Skip to content

Commit c726ef9

Browse files
committed
Expose list_{channels, peers} to bindings
# Conflicts: # bindings/ldk_node.udl
1 parent 13ab9b6 commit c726ef9

File tree

4 files changed

+200
-5
lines changed

4 files changed

+200
-5
lines changed

bindings/ldk_node.udl

+30
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

+18-4
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,15 @@ use types::{
105105
ChainMonitor, ChannelManager, GossipSync, KeysManager, Network, 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};
@@ -902,7 +901,7 @@ impl Node {
902901

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

908907
/// Connect to a node on the peer-to-peer network.
@@ -1442,6 +1441,21 @@ impl Node {
14421441
) -> Vec<PaymentDetails> {
14431442
self.payment_store.list_filter(f)
14441443
}
1444+
1445+
/// Retrieves a list of known peers.
1446+
pub fn list_peers(&self) -> Vec<PeerDetails> {
1447+
let active_connected_peers: Vec<PublicKey> =
1448+
self.peer_manager.get_peer_node_ids().iter().map(|p| p.0).collect();
1449+
self.peer_store
1450+
.list_peers()
1451+
.iter()
1452+
.map(|p| PeerDetails {
1453+
node_id: p.pubkey,
1454+
address: p.address,
1455+
is_connected: active_connected_peers.contains(&p.pubkey),
1456+
})
1457+
.collect()
1458+
}
14451459
}
14461460

14471461
async fn connect_peer_if_necessary(

src/test/functional_tests.rs

+4
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

+148-1
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)