Skip to content

Commit

Permalink
Consolidate Channel balance fetching into one fn returning struct
Browse files Browse the repository at this point in the history
Some simple code motion to clean up how channel balances get
fetched.
  • Loading branch information
TheBlueMatt committed Apr 27, 2022
1 parent 68ee7ef commit ed06722
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
62 changes: 32 additions & 30 deletions lightning/src/ln/channel.rs
Expand Up @@ -62,6 +62,17 @@ pub struct ChannelValueStat {
pub counterparty_dust_limit_msat: u64,
}

pub struct AvailableBalance {
/// The amount that would go to us if we close the channel, ignoring any on-chain fees.
pub balance_msat: u64,
/// Total amount available for our counterparty to send to us, ignoring HTLCs.
pub inbound_capacity_msat: u64,
/// Total amount available for us to send to our counterparty, ignoring HTLCs.
pub outbound_capacity_msat: u64,
/// The maximum value we can assign to the next outbound HTLC
pub next_outbound_htlc_limit_msat: u64,
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum FeeUpdateState {
// Inbound states mirroring InboundHTLCState
Expand Down Expand Up @@ -2330,47 +2341,38 @@ impl<Signer: Sign> Channel<Signer> {
stats
}

/// Get the available (ie not including pending HTLCs) inbound and outbound balance, plus the
/// amount available for a single HTLC send, all in msat.
/// Get the available balances, see [`AvailableBalance`]'s fields for more info.
/// Doesn't bother handling the
/// if-we-removed-it-already-but-haven't-fully-resolved-they-can-still-send-an-inbound-HTLC
/// corner case properly.
/// The channel reserve is subtracted from each balance.
/// See also [`Channel::get_balance_msat`]
pub fn get_inbound_outbound_available_balance_msat(&self) -> (u64, u64, u64) {
pub fn get_available_balance_msat(&self) -> AvailableBalance {
// Note that we have to handle overflow due to the above case.
let outbound_stats = self.get_outbound_pending_htlc_stats(None);

let mut balance_msat = self.value_to_self_msat - outbound_stats.pending_htlcs_value_msat;
for ref htlc in self.pending_inbound_htlcs.iter() {
if let InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_)) = htlc.state {
balance_msat += htlc.amount_msat;
}
}

let outbound_capacity_msat = cmp::max(self.value_to_self_msat as i64
- outbound_stats.pending_htlcs_value_msat as i64
- self.counterparty_selected_channel_reserve_satoshis.unwrap_or(0) as i64 * 1000,
0) as u64;
(
cmp::max(self.channel_value_satoshis as i64 * 1000
- self.value_to_self_msat as i64
- self.get_inbound_pending_htlc_stats(None).pending_htlcs_value_msat as i64
- self.holder_selected_channel_reserve_satoshis as i64 * 1000,
0) as u64,
AvailableBalance {
inbound_capacity_msat: cmp::max(self.channel_value_satoshis as i64 * 1000
- self.value_to_self_msat as i64
- self.get_inbound_pending_htlc_stats(None).pending_htlcs_value_msat as i64
- self.holder_selected_channel_reserve_satoshis as i64 * 1000,
0) as u64,
outbound_capacity_msat,
cmp::max(cmp::min(outbound_capacity_msat as i64,
self.counterparty_max_htlc_value_in_flight_msat as i64
- outbound_stats.pending_htlcs_value_msat as i64),
0) as u64
)
}

/// Get our total balance in msat.
/// This is the amount that would go to us if we close the channel, ignoring any on-chain fees.
/// See also [`Channel::get_inbound_outbound_available_balance_msat`]
pub fn get_balance_msat(&self) -> u64 {
// Include our local balance, plus any inbound HTLCs we know the preimage for, minus any
// HTLCs sent or which will be sent after commitment signed's are exchanged.
let mut balance_msat = self.value_to_self_msat;
for ref htlc in self.pending_inbound_htlcs.iter() {
if let InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_)) = htlc.state {
balance_msat += htlc.amount_msat;
}
next_outbound_htlc_limit_msat: cmp::max(cmp::min(outbound_capacity_msat as i64,
self.counterparty_max_htlc_value_in_flight_msat as i64
- outbound_stats.pending_htlcs_value_msat as i64),
0) as u64,
balance_msat,
}
balance_msat - self.get_outbound_pending_htlc_stats(None).pending_htlcs_value_msat
}

pub fn get_holder_counterparty_selected_channel_reserve_satoshis(&self) -> (u64, Option<u64>) {
Expand Down
12 changes: 5 additions & 7 deletions lightning/src/ln/channelmanager.rs
Expand Up @@ -1677,9 +1677,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
let channel_state = self.channel_state.lock().unwrap();
res.reserve(channel_state.by_id.len());
for (channel_id, channel) in channel_state.by_id.iter().filter(f) {
let (inbound_capacity_msat, outbound_capacity_msat, next_outbound_htlc_limit_msat) =
channel.get_inbound_outbound_available_balance_msat();
let balance_msat = channel.get_balance_msat();
let balance = channel.get_available_balance_msat();
let (to_remote_reserve_satoshis, to_self_reserve_satoshis) =
channel.get_holder_counterparty_selected_channel_reserve_satoshis();
res.push(ChannelDetails {
Expand All @@ -1706,10 +1704,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
inbound_scid_alias: channel.latest_inbound_scid_alias(),
channel_value_satoshis: channel.get_value_satoshis(),
unspendable_punishment_reserve: to_self_reserve_satoshis,
balance_msat,
inbound_capacity_msat,
outbound_capacity_msat,
next_outbound_htlc_limit_msat,
balance_msat: balance.balance_msat,
inbound_capacity_msat: balance.inbound_capacity_msat,
outbound_capacity_msat: balance.outbound_capacity_msat,
next_outbound_htlc_limit_msat: balance.next_outbound_htlc_limit_msat,
user_channel_id: channel.get_user_id(),
confirmations_required: channel.minimum_depth(),
force_close_spend_delay: channel.get_counterparty_selected_contest_delay(),
Expand Down

0 comments on commit ed06722

Please sign in to comment.