Skip to content

Commit 64dd5d1

Browse files
dunxenwvanlint
andcommitted
Remove AvailableBalances::balance_msat
The ChannelMonitor::get_claimable_balances method provides a more straightforward approach to the balance of a channel, which satisfies most use cases. The computation of AvailableBalances::balance_msat is complex and originally had a different purpose that is not applicable anymore. Co-authored-by: Willem Van Lint <[email protected]>
1 parent 33e6995 commit 64dd5d1

File tree

6 files changed

+14
-35
lines changed

6 files changed

+14
-35
lines changed

fuzz/src/router.rs

-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
242242
is_channel_ready: true,
243243
is_usable: true,
244244
is_public: true,
245-
balance_msat: 0,
246245
outbound_capacity_msat: capacity.saturating_mul(1000),
247246
next_outbound_htlc_limit_msat: capacity.saturating_mul(1000),
248247
next_outbound_htlc_minimum_msat: 0,

lightning/src/chain/chainmonitor.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,7 @@ where C::Target: chain::Filter,
394394
/// claims which are awaiting confirmation.
395395
///
396396
/// Includes the balances from each [`ChannelMonitor`] *except* those included in
397-
/// `ignored_channels`, allowing you to filter out balances from channels which are still open
398-
/// (and whose balance should likely be pulled from the [`ChannelDetails`]).
397+
/// `ignored_channels`.
399398
///
400399
/// See [`ChannelMonitor::get_claimable_balances`] for more details on the exact criteria for
401400
/// inclusion in the return value.

lightning/src/ln/channel.rs

-11
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ pub struct ChannelValueStat {
7979
}
8080

8181
pub struct AvailableBalances {
82-
/// The amount that would go to us if we close the channel, ignoring any on-chain fees.
83-
pub balance_msat: u64,
8482
/// Total amount available for our counterparty to send to us.
8583
pub inbound_capacity_msat: u64,
8684
/// Total amount available for us to send to our counterparty.
@@ -3076,14 +3074,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
30763074
let dust_exposure_limiting_feerate = self.get_dust_exposure_limiting_feerate(&fee_estimator);
30773075
let htlc_stats = context.get_pending_htlc_stats(None, dust_exposure_limiting_feerate);
30783076

3079-
let mut balance_msat = context.value_to_self_msat;
3080-
for ref htlc in context.pending_inbound_htlcs.iter() {
3081-
if let InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_)) = htlc.state {
3082-
balance_msat += htlc.amount_msat;
3083-
}
3084-
}
3085-
balance_msat -= htlc_stats.pending_outbound_htlcs_value_msat;
3086-
30873077
let outbound_capacity_msat = context.value_to_self_msat
30883078
.saturating_sub(htlc_stats.pending_outbound_htlcs_value_msat)
30893079
.saturating_sub(
@@ -3224,7 +3214,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
32243214
outbound_capacity_msat,
32253215
next_outbound_htlc_limit_msat: available_capacity_msat,
32263216
next_outbound_htlc_minimum_msat,
3227-
balance_msat,
32283217
}
32293218
}
32303219

lightning/src/ln/channel_state.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,14 @@ impl_writeable_tlv_based!(ChannelCounterparty, {
273273

274274
/// Details of a channel, as returned by [`ChannelManager::list_channels`] and [`ChannelManager::list_usable_channels`]
275275
///
276+
/// Balances of a channel are available through [`ChainMonitor::get_claimable_balances`] and
277+
/// [`ChannelMonitor::get_claimable_balances`], calculated with respect to the corresponding on-chain
278+
/// transactions.
279+
///
276280
/// [`ChannelManager::list_channels`]: crate::ln::channelmanager::ChannelManager::list_channels
277281
/// [`ChannelManager::list_usable_channels`]: crate::ln::channelmanager::ChannelManager::list_usable_channels
282+
/// [`ChainMonitor::get_claimable_balances`]: crate::chain::chainmonitor::ChainMonitor::get_claimable_balances
283+
/// [`ChannelMonitor::get_claimable_balances`]: crate::chain::channelmonitor::ChannelMonitor::get_claimable_balances
278284
#[derive(Clone, Debug, PartialEq)]
279285
pub struct ChannelDetails {
280286
/// The channel's ID (prior to funding transaction generation, this is a random 32 bytes,
@@ -353,24 +359,11 @@ pub struct ChannelDetails {
353359
///
354360
/// This value will be `None` for objects serialized with LDK versions prior to 0.0.115.
355361
pub feerate_sat_per_1000_weight: Option<u32>,
356-
/// Our total balance. This is the amount we would get if we close the channel.
357-
/// This value is not exact. Due to various in-flight changes and feerate changes, exactly this
358-
/// amount is not likely to be recoverable on close.
359-
///
360-
/// This does not include any pending HTLCs which are not yet fully resolved (and, thus, whose
361-
/// balance is not available for inclusion in new outbound HTLCs). This further does not include
362-
/// any pending outgoing HTLCs which are awaiting some other resolution to be sent.
363-
/// This does not consider any on-chain fees.
364-
///
365-
/// See also [`ChannelDetails::outbound_capacity_msat`]
366-
pub balance_msat: u64,
367362
/// The available outbound capacity for sending HTLCs to the remote peer. This does not include
368363
/// any pending HTLCs which are not yet fully resolved (and, thus, whose balance is not
369364
/// available for inclusion in new outbound HTLCs). This further does not include any pending
370365
/// outgoing HTLCs which are awaiting some other resolution to be sent.
371366
///
372-
/// See also [`ChannelDetails::balance_msat`]
373-
///
374367
/// This value is not exact. Due to various in-flight changes, feerate changes, and our
375368
/// conflict-avoidance policy, exactly this amount is not likely to be spendable. However, we
376369
/// should be able to spend nearly this amount.
@@ -380,8 +373,8 @@ pub struct ChannelDetails {
380373
/// the current state and per-HTLC limit(s). This is intended for use when routing, allowing us
381374
/// to use a limit as close as possible to the HTLC limit we can currently send.
382375
///
383-
/// See also [`ChannelDetails::next_outbound_htlc_minimum_msat`],
384-
/// [`ChannelDetails::balance_msat`], and [`ChannelDetails::outbound_capacity_msat`].
376+
/// See also [`ChannelDetails::next_outbound_htlc_minimum_msat`] and
377+
/// [`ChannelDetails::outbound_capacity_msat`].
385378
pub next_outbound_htlc_limit_msat: u64,
386379
/// The minimum value for sending a single HTLC to the remote peer. This is the equivalent of
387380
/// [`ChannelDetails::next_outbound_htlc_limit_msat`] but represents a lower-bound, rather than
@@ -532,7 +525,6 @@ impl ChannelDetails {
532525
channel_value_satoshis: context.get_value_satoshis(),
533526
feerate_sat_per_1000_weight: Some(context.get_feerate_sat_per_1000_weight()),
534527
unspendable_punishment_reserve: to_self_reserve_satoshis,
535-
balance_msat: balance.balance_msat,
536528
inbound_capacity_msat: balance.inbound_capacity_msat,
537529
outbound_capacity_msat: balance.outbound_capacity_msat,
538530
next_outbound_htlc_limit_msat: balance.next_outbound_htlc_limit_msat,
@@ -574,7 +566,7 @@ impl Writeable for ChannelDetails {
574566
(10, self.channel_value_satoshis, required),
575567
(12, self.unspendable_punishment_reserve, option),
576568
(14, user_channel_id_low, required),
577-
(16, self.balance_msat, required),
569+
(16, self.next_outbound_htlc_limit_msat, required), // Forwards compatibility for removed balance_msat field.
578570
(18, self.outbound_capacity_msat, required),
579571
(19, self.next_outbound_htlc_limit_msat, required),
580572
(20, self.inbound_capacity_msat, required),
@@ -612,7 +604,7 @@ impl Readable for ChannelDetails {
612604
(10, channel_value_satoshis, required),
613605
(12, unspendable_punishment_reserve, option),
614606
(14, user_channel_id_low, required),
615-
(16, balance_msat, required),
607+
(16, _balance_msat, option), // Backwards compatibility for removed balance_msat field.
616608
(18, outbound_capacity_msat, required),
617609
// Note that by the time we get past the required read above, outbound_capacity_msat will be
618610
// filled in, so we can safely unwrap it here.
@@ -640,6 +632,8 @@ impl Readable for ChannelDetails {
640632
let user_channel_id = user_channel_id_low as u128
641633
+ ((user_channel_id_high_opt.unwrap_or(0 as u64) as u128) << 64);
642634

635+
let _balance_msat: Option<u64> = _balance_msat;
636+
643637
Ok(Self {
644638
inbound_scid_alias,
645639
channel_id: channel_id.0.unwrap(),
@@ -652,7 +646,6 @@ impl Readable for ChannelDetails {
652646
channel_value_satoshis: channel_value_satoshis.0.unwrap(),
653647
unspendable_punishment_reserve,
654648
user_channel_id,
655-
balance_msat: balance_msat.0.unwrap(),
656649
outbound_capacity_msat: outbound_capacity_msat.0.unwrap(),
657650
next_outbound_htlc_limit_msat: next_outbound_htlc_limit_msat.0.unwrap(),
658651
next_outbound_htlc_minimum_msat: next_outbound_htlc_minimum_msat.0.unwrap(),

lightning/src/routing/router.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3602,7 +3602,6 @@ mod tests {
36023602
inbound_scid_alias: None,
36033603
channel_value_satoshis: 0,
36043604
user_channel_id: 0,
3605-
balance_msat: 0,
36063605
outbound_capacity_msat,
36073606
next_outbound_htlc_limit_msat: outbound_capacity_msat,
36083607
next_outbound_htlc_minimum_msat: 0,
@@ -8877,7 +8876,6 @@ pub(crate) mod bench_utils {
88778876
outbound_scid_alias: None,
88788877
channel_value_satoshis: 10_000_000_000,
88798878
user_channel_id: 0,
8880-
balance_msat: 10_000_000_000,
88818879
outbound_capacity_msat: 10_000_000_000,
88828880
next_outbound_htlc_minimum_msat: 0,
88838881
next_outbound_htlc_limit_msat: 10_000_000_000,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* The `AvailableBalances::balance_msat` field has been removed in favor of `ChannelMonitor::get_claimable_balances`. `ChannelDetails` serialized with versions of LDK >= 0.0.124 will have their `balance_msat` field set to `next_outbound_htlc_limit_msat` when read by versions of LDK prior to 0.0.124 (#3243).

0 commit comments

Comments
 (0)