Skip to content

Commit a375f64

Browse files
committed
Consolidate Channel balance fetching into one fn returning struct
Some simple code motion to clean up how channel balances get fetched.
1 parent 68ee7ef commit a375f64

File tree

2 files changed

+38
-37
lines changed

2 files changed

+38
-37
lines changed

lightning/src/ln/channel.rs

+33-30
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ pub struct ChannelValueStat {
6262
pub counterparty_dust_limit_msat: u64,
6363
}
6464

65+
pub struct AvailableBalances {
66+
/// The amount that would go to us if we close the channel, ignoring any on-chain fees.
67+
pub balance_msat: u64,
68+
/// Total amount available for our counterparty to send to us, ignoring HTLCs.
69+
pub inbound_capacity_msat: u64,
70+
/// Total amount available for us to send to our counterparty, ignoring HTLCs.
71+
pub outbound_capacity_msat: u64,
72+
/// The maximum value we can assign to the next outbound HTLC
73+
pub next_outbound_htlc_limit_msat: u64,
74+
}
75+
6576
#[derive(Debug, Clone, Copy, PartialEq)]
6677
enum FeeUpdateState {
6778
// Inbound states mirroring InboundHTLCState
@@ -2330,47 +2341,39 @@ impl<Signer: Sign> Channel<Signer> {
23302341
stats
23312342
}
23322343

2333-
/// Get the available (ie not including pending HTLCs) inbound and outbound balance, plus the
2334-
/// amount available for a single HTLC send, all in msat.
2344+
/// Get the available balances, see [`AvailableBalances`]'s fields for more info.
23352345
/// Doesn't bother handling the
23362346
/// if-we-removed-it-already-but-haven't-fully-resolved-they-can-still-send-an-inbound-HTLC
23372347
/// corner case properly.
2338-
/// The channel reserve is subtracted from each balance.
2339-
/// See also [`Channel::get_balance_msat`]
2340-
pub fn get_inbound_outbound_available_balance_msat(&self) -> (u64, u64, u64) {
2348+
pub fn get_available_balances(&self) -> AvailableBalances {
23412349
// Note that we have to handle overflow due to the above case.
23422350
let outbound_stats = self.get_outbound_pending_htlc_stats(None);
2343-
let outbound_capacity_msat = cmp::max(self.value_to_self_msat as i64
2344-
- outbound_stats.pending_htlcs_value_msat as i64
2345-
- self.counterparty_selected_channel_reserve_satoshis.unwrap_or(0) as i64 * 1000,
2346-
0) as u64;
2347-
(
2348-
cmp::max(self.channel_value_satoshis as i64 * 1000
2349-
- self.value_to_self_msat as i64
2350-
- self.get_inbound_pending_htlc_stats(None).pending_htlcs_value_msat as i64
2351-
- self.holder_selected_channel_reserve_satoshis as i64 * 1000,
2352-
0) as u64,
2353-
outbound_capacity_msat,
2354-
cmp::max(cmp::min(outbound_capacity_msat as i64,
2355-
self.counterparty_max_htlc_value_in_flight_msat as i64
2356-
- outbound_stats.pending_htlcs_value_msat as i64),
2357-
0) as u64
2358-
)
2359-
}
2360-
2361-
/// Get our total balance in msat.
2362-
/// This is the amount that would go to us if we close the channel, ignoring any on-chain fees.
2363-
/// See also [`Channel::get_inbound_outbound_available_balance_msat`]
2364-
pub fn get_balance_msat(&self) -> u64 {
2365-
// Include our local balance, plus any inbound HTLCs we know the preimage for, minus any
2366-
// HTLCs sent or which will be sent after commitment signed's are exchanged.
2351+
23672352
let mut balance_msat = self.value_to_self_msat;
23682353
for ref htlc in self.pending_inbound_htlcs.iter() {
23692354
if let InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_)) = htlc.state {
23702355
balance_msat += htlc.amount_msat;
23712356
}
23722357
}
2373-
balance_msat - self.get_outbound_pending_htlc_stats(None).pending_htlcs_value_msat
2358+
balance_msat -= outbound_stats.pending_htlcs_value_msat;
2359+
2360+
let outbound_capacity_msat = cmp::max(self.value_to_self_msat as i64
2361+
- outbound_stats.pending_htlcs_value_msat as i64
2362+
- self.counterparty_selected_channel_reserve_satoshis.unwrap_or(0) as i64 * 1000,
2363+
0) as u64;
2364+
AvailableBalances {
2365+
inbound_capacity_msat: cmp::max(self.channel_value_satoshis as i64 * 1000
2366+
- self.value_to_self_msat as i64
2367+
- self.get_inbound_pending_htlc_stats(None).pending_htlcs_value_msat as i64
2368+
- self.holder_selected_channel_reserve_satoshis as i64 * 1000,
2369+
0) as u64,
2370+
outbound_capacity_msat,
2371+
next_outbound_htlc_limit_msat: cmp::max(cmp::min(outbound_capacity_msat as i64,
2372+
self.counterparty_max_htlc_value_in_flight_msat as i64
2373+
- outbound_stats.pending_htlcs_value_msat as i64),
2374+
0) as u64,
2375+
balance_msat,
2376+
}
23742377
}
23752378

23762379
pub fn get_holder_counterparty_selected_channel_reserve_satoshis(&self) -> (u64, Option<u64>) {

lightning/src/ln/channelmanager.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1677,9 +1677,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
16771677
let channel_state = self.channel_state.lock().unwrap();
16781678
res.reserve(channel_state.by_id.len());
16791679
for (channel_id, channel) in channel_state.by_id.iter().filter(f) {
1680-
let (inbound_capacity_msat, outbound_capacity_msat, next_outbound_htlc_limit_msat) =
1681-
channel.get_inbound_outbound_available_balance_msat();
1682-
let balance_msat = channel.get_balance_msat();
1680+
let balance = channel.get_available_balances();
16831681
let (to_remote_reserve_satoshis, to_self_reserve_satoshis) =
16841682
channel.get_holder_counterparty_selected_channel_reserve_satoshis();
16851683
res.push(ChannelDetails {
@@ -1706,10 +1704,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
17061704
inbound_scid_alias: channel.latest_inbound_scid_alias(),
17071705
channel_value_satoshis: channel.get_value_satoshis(),
17081706
unspendable_punishment_reserve: to_self_reserve_satoshis,
1709-
balance_msat,
1710-
inbound_capacity_msat,
1711-
outbound_capacity_msat,
1712-
next_outbound_htlc_limit_msat,
1707+
balance_msat: balance.balance_msat,
1708+
inbound_capacity_msat: balance.inbound_capacity_msat,
1709+
outbound_capacity_msat: balance.outbound_capacity_msat,
1710+
next_outbound_htlc_limit_msat: balance.next_outbound_htlc_limit_msat,
17131711
user_channel_id: channel.get_user_id(),
17141712
confirmations_required: channel.minimum_depth(),
17151713
force_close_spend_delay: channel.get_counterparty_selected_contest_delay(),

0 commit comments

Comments
 (0)