Skip to content

Commit bec6432

Browse files
committed
Add balance and is_live fields to ChannelDetails
1 parent 02feaeb commit bec6432

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/ln/channel.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,15 @@ impl Channel {
15811581
(htlc_outbound_count as u32, htlc_outbound_value_msat)
15821582
}
15831583

1584+
/// Get the available (ie not including pending HTLCs) inbound and outbound balance in msat.
1585+
/// Doesn't bother handling the
1586+
/// if-we-removed-it-already-but-haven't-fully-resolved-they-can-still-send-an-inbound-HTLC
1587+
/// corner case properly.
1588+
pub fn get_inbound_outbound_available_balance_msat(&self) -> (u64, u64) {
1589+
(self.channel_value_satoshis * 1000 - self.value_to_self_msat - self.get_inbound_pending_htlc_sats().1,
1590+
self.value_to_self_msat - self.get_outbound_pending_htlc_stats().1)
1591+
}
1592+
15841593
pub fn update_add_htlc(&mut self, msg: &msgs::UpdateAddHTLC, pending_forward_state: PendingHTLCStatus) -> Result<(), ChannelError> {
15851594
if (self.channel_state & (ChannelState::ChannelFunded as u32 | ChannelState::RemoteShutdownSent as u32)) != (ChannelState::ChannelFunded as u32) {
15861595
return Err(ChannelError::Close("Got add HTLC message when channel was not in an operational state"));

src/ln/channelmanager.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,20 @@ pub struct ChannelDetails {
391391
pub channel_value_satoshis: u64,
392392
/// The user_id passed in to create_channel, or 0 if the channel was inbound.
393393
pub user_id: u64,
394+
/// The available outbound capacity for sending HTLCs to the remote peer. This does not include
395+
/// any pending HTLCs which are not yet fully resolved (and, thus, who's balance is not
396+
/// available for inclusion in new outbound HTLCs). This further does not include any pending
397+
/// outgoing HTLCs which are awaiting some other resolution to be sent.
398+
pub outbound_capacity_msat: u64,
399+
/// The available inbound capacity for the remote peer to send HTLCs to us. This does not
400+
/// include any pending HTLCs which are not yet fully resolved (and, thus, who's balance is not
401+
/// available for inclusion in new inbound HTLCs).
402+
/// Note that there are some corner cases not fully handled here, so the actual available
403+
/// inbound capacity may be slightly higher than this.
404+
pub inbound_capacity_msat: u64,
405+
/// True if the channel is (a) confirmed and funding_locked messages have been exchanged, (b)
406+
/// the peer is connected, and (c) no monitor update failure is pending resolution.
407+
pub is_live: bool,
394408
}
395409

396410
macro_rules! handle_error {
@@ -613,19 +627,26 @@ impl ChannelManager {
613627
let channel_state = self.channel_state.lock().unwrap();
614628
let mut res = Vec::with_capacity(channel_state.by_id.len());
615629
for (channel_id, channel) in channel_state.by_id.iter() {
630+
let (inbound_capacity_msat, outbound_capacity_msat) = channel.get_inbound_outbound_available_balance_msat();
616631
res.push(ChannelDetails {
617632
channel_id: (*channel_id).clone(),
618633
short_channel_id: channel.get_short_channel_id(),
619634
remote_network_id: channel.get_their_node_id(),
620635
channel_value_satoshis: channel.get_value_satoshis(),
636+
inbound_capacity_msat,
637+
outbound_capacity_msat,
621638
user_id: channel.get_user_id(),
639+
is_live: channel.is_live(),
622640
});
623641
}
624642
res
625643
}
626644

627645
/// Gets the list of usable channels, in random order. Useful as an argument to
628646
/// Router::get_route to ensure non-announced channels are used.
647+
///
648+
/// These are guaranteed to have their is_live value set to true, see the documentation for
649+
/// ChannelDetails::is_live for more info on exactly what the criteria are.
629650
pub fn list_usable_channels(&self) -> Vec<ChannelDetails> {
630651
let channel_state = self.channel_state.lock().unwrap();
631652
let mut res = Vec::with_capacity(channel_state.by_id.len());
@@ -634,12 +655,16 @@ impl ChannelManager {
634655
// internal/external nomenclature, but that's ok cause that's probably what the user
635656
// really wanted anyway.
636657
if channel.is_live() {
658+
let (inbound_capacity_msat, outbound_capacity_msat) = channel.get_inbound_outbound_available_balance_msat();
637659
res.push(ChannelDetails {
638660
channel_id: (*channel_id).clone(),
639661
short_channel_id: channel.get_short_channel_id(),
640662
remote_network_id: channel.get_their_node_id(),
641663
channel_value_satoshis: channel.get_value_satoshis(),
664+
inbound_capacity_msat,
665+
outbound_capacity_msat,
642666
user_id: channel.get_user_id(),
667+
is_live: true,
643668
});
644669
}
645670
}

0 commit comments

Comments
 (0)