Skip to content

Commit efbaa19

Browse files
authored
Merge pull request #2626 from TheBlueMatt/2023-09-revert-2476
Revert "Remove AvailableBalances::balance_msat"
2 parents 955e810 + c7c4226 commit efbaa19

File tree

6 files changed

+35
-14
lines changed

6 files changed

+35
-14
lines changed

fuzz/src/router.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
233233
force_close_spend_delay: None,
234234
is_outbound: true, is_channel_ready: true,
235235
is_usable: true, is_public: true,
236+
balance_msat: 0,
236237
outbound_capacity_msat: capacity.saturating_mul(1000),
237238
next_outbound_htlc_limit_msat: capacity.saturating_mul(1000),
238239
next_outbound_htlc_minimum_msat: 0,

lightning/src/chain/chainmonitor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,8 @@ where C::Target: chain::Filter,
423423
/// claims which are awaiting confirmation.
424424
///
425425
/// Includes the balances from each [`ChannelMonitor`] *except* those included in
426-
/// `ignored_channels`.
426+
/// `ignored_channels`, allowing you to filter out balances from channels which are still open
427+
/// (and whose balance should likely be pulled from the [`ChannelDetails`]).
427428
///
428429
/// See [`ChannelMonitor::get_claimable_balances`] for more details on the exact criteria for
429430
/// inclusion in the return value.

lightning/src/ln/channel.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ pub struct ChannelValueStat {
6666
}
6767

6868
pub struct AvailableBalances {
69+
/// The amount that would go to us if we close the channel, ignoring any on-chain fees.
70+
pub balance_msat: u64,
6971
/// Total amount available for our counterparty to send to us.
7072
pub inbound_capacity_msat: u64,
7173
/// Total amount available for us to send to our counterparty.
@@ -1655,6 +1657,14 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
16551657
let inbound_stats = context.get_inbound_pending_htlc_stats(None);
16561658
let outbound_stats = context.get_outbound_pending_htlc_stats(None);
16571659

1660+
let mut balance_msat = context.value_to_self_msat;
1661+
for ref htlc in context.pending_inbound_htlcs.iter() {
1662+
if let InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_)) = htlc.state {
1663+
balance_msat += htlc.amount_msat;
1664+
}
1665+
}
1666+
balance_msat -= outbound_stats.pending_htlcs_value_msat;
1667+
16581668
let outbound_capacity_msat = context.value_to_self_msat
16591669
.saturating_sub(outbound_stats.pending_htlcs_value_msat)
16601670
.saturating_sub(
@@ -1771,6 +1781,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
17711781
outbound_capacity_msat,
17721782
next_outbound_htlc_limit_msat: available_capacity_msat,
17731783
next_outbound_htlc_minimum_msat,
1784+
balance_msat,
17741785
}
17751786
}
17761787

lightning/src/ln/channelmanager.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,12 +1448,6 @@ pub struct ChannelCounterparty {
14481448
}
14491449

14501450
/// Details of a channel, as returned by [`ChannelManager::list_channels`] and [`ChannelManager::list_usable_channels`]
1451-
///
1452-
/// Balances of a channel are available through [`ChainMonitor::get_claimable_balances`] and
1453-
/// [`ChannelMonitor::get_claimable_balances`], calculated with respect to the corresponding on-chain
1454-
/// transactions.
1455-
///
1456-
/// [`ChainMonitor::get_claimable_balances`]: crate::chain::chainmonitor::ChainMonitor::get_claimable_balances
14571451
#[derive(Clone, Debug, PartialEq)]
14581452
pub struct ChannelDetails {
14591453
/// The channel's ID (prior to funding transaction generation, this is a random 32 bytes,
@@ -1535,11 +1529,24 @@ pub struct ChannelDetails {
15351529
///
15361530
/// This value will be `None` for objects serialized with LDK versions prior to 0.0.115.
15371531
pub feerate_sat_per_1000_weight: Option<u32>,
1532+
/// Our total balance. This is the amount we would get if we close the channel.
1533+
/// This value is not exact. Due to various in-flight changes and feerate changes, exactly this
1534+
/// amount is not likely to be recoverable on close.
1535+
///
1536+
/// This does not include any pending HTLCs which are not yet fully resolved (and, thus, whose
1537+
/// balance is not available for inclusion in new outbound HTLCs). This further does not include
1538+
/// any pending outgoing HTLCs which are awaiting some other resolution to be sent.
1539+
/// This does not consider any on-chain fees.
1540+
///
1541+
/// See also [`ChannelDetails::outbound_capacity_msat`]
1542+
pub balance_msat: u64,
15381543
/// The available outbound capacity for sending HTLCs to the remote peer. This does not include
15391544
/// any pending HTLCs which are not yet fully resolved (and, thus, whose balance is not
15401545
/// available for inclusion in new outbound HTLCs). This further does not include any pending
15411546
/// outgoing HTLCs which are awaiting some other resolution to be sent.
15421547
///
1548+
/// See also [`ChannelDetails::balance_msat`]
1549+
///
15431550
/// This value is not exact. Due to various in-flight changes, feerate changes, and our
15441551
/// conflict-avoidance policy, exactly this amount is not likely to be spendable. However, we
15451552
/// should be able to spend nearly this amount.
@@ -1549,8 +1556,8 @@ pub struct ChannelDetails {
15491556
/// the current state and per-HTLC limit(s). This is intended for use when routing, allowing us
15501557
/// to use a limit as close as possible to the HTLC limit we can currently send.
15511558
///
1552-
/// See also [`ChannelDetails::next_outbound_htlc_minimum_msat`] and
1553-
/// [`ChannelDetails::outbound_capacity_msat`].
1559+
/// See also [`ChannelDetails::next_outbound_htlc_minimum_msat`],
1560+
/// [`ChannelDetails::balance_msat`], and [`ChannelDetails::outbound_capacity_msat`].
15541561
pub next_outbound_htlc_limit_msat: u64,
15551562
/// The minimum value for sending a single HTLC to the remote peer. This is the equivalent of
15561563
/// [`ChannelDetails::next_outbound_htlc_limit_msat`] but represents a lower-bound, rather than
@@ -1680,6 +1687,7 @@ impl ChannelDetails {
16801687
channel_value_satoshis: context.get_value_satoshis(),
16811688
feerate_sat_per_1000_weight: Some(context.get_feerate_sat_per_1000_weight()),
16821689
unspendable_punishment_reserve: to_self_reserve_satoshis,
1690+
balance_msat: balance.balance_msat,
16831691
inbound_capacity_msat: balance.inbound_capacity_msat,
16841692
outbound_capacity_msat: balance.outbound_capacity_msat,
16851693
next_outbound_htlc_limit_msat: balance.next_outbound_htlc_limit_msat,
@@ -8419,7 +8427,7 @@ impl Writeable for ChannelDetails {
84198427
(10, self.channel_value_satoshis, required),
84208428
(12, self.unspendable_punishment_reserve, option),
84218429
(14, user_channel_id_low, required),
8422-
(16, self.next_outbound_htlc_limit_msat, required), // Forwards compatibility for removed balance_msat field.
8430+
(16, self.balance_msat, required),
84238431
(18, self.outbound_capacity_msat, required),
84248432
(19, self.next_outbound_htlc_limit_msat, required),
84258433
(20, self.inbound_capacity_msat, required),
@@ -8455,7 +8463,7 @@ impl Readable for ChannelDetails {
84558463
(10, channel_value_satoshis, required),
84568464
(12, unspendable_punishment_reserve, option),
84578465
(14, user_channel_id_low, required),
8458-
(16, _balance_msat, option), // Backwards compatibility for removed balance_msat field.
8466+
(16, balance_msat, required),
84598467
(18, outbound_capacity_msat, required),
84608468
// Note that by the time we get past the required read above, outbound_capacity_msat will be
84618469
// filled in, so we can safely unwrap it here.
@@ -8481,8 +8489,6 @@ impl Readable for ChannelDetails {
84818489
let user_channel_id = user_channel_id_low as u128 +
84828490
((user_channel_id_high_opt.unwrap_or(0 as u64) as u128) << 64);
84838491

8484-
let _balance_msat: Option<u64> = _balance_msat;
8485-
84868492
Ok(Self {
84878493
inbound_scid_alias,
84888494
channel_id: channel_id.0.unwrap(),
@@ -8495,6 +8501,7 @@ impl Readable for ChannelDetails {
84958501
channel_value_satoshis: channel_value_satoshis.0.unwrap(),
84968502
unspendable_punishment_reserve,
84978503
user_channel_id,
8504+
balance_msat: balance_msat.0.unwrap(),
84988505
outbound_capacity_msat: outbound_capacity_msat.0.unwrap(),
84998506
next_outbound_htlc_limit_msat: next_outbound_htlc_limit_msat.0.unwrap(),
85008507
next_outbound_htlc_minimum_msat: next_outbound_htlc_minimum_msat.0.unwrap(),

lightning/src/routing/router.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2870,6 +2870,7 @@ mod tests {
28702870
inbound_scid_alias: None,
28712871
channel_value_satoshis: 0,
28722872
user_channel_id: 0,
2873+
balance_msat: 0,
28732874
outbound_capacity_msat,
28742875
next_outbound_htlc_limit_msat: outbound_capacity_msat,
28752876
next_outbound_htlc_minimum_msat: 0,
@@ -7780,6 +7781,7 @@ pub(crate) mod bench_utils {
77807781
outbound_scid_alias: None,
77817782
channel_value_satoshis: 10_000_000_000,
77827783
user_channel_id: 0,
7784+
balance_msat: 10_000_000_000,
77837785
outbound_capacity_msat: 10_000_000_000,
77847786
next_outbound_htlc_minimum_msat: 0,
77857787
next_outbound_htlc_limit_msat: 10_000_000_000,

pending_changelog/remove_balance_msat.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)