From deadccf70513e5a446a41366e695337b561f152d Mon Sep 17 00:00:00 2001 From: Leo Nash Date: Thu, 3 Jul 2025 18:49:15 +0000 Subject: [PATCH 1/3] Use more idiomatic `sum` method when possible --- lightning/src/chain/channelmonitor.rs | 3 +-- lightning/src/ln/channel.rs | 35 +++++++++++++++++---------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index 616de1f0e3f..cb28a8ebb5d 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -2863,8 +2863,7 @@ impl ChannelMonitor { // In addition to `commit_tx_fee_sat`, this can also include dust HTLCs, and the total msat amount rounded down from non-dust HTLCs transaction_fee_satoshis: if us.holder_pays_commitment_tx_fee.unwrap_or(true) { let transaction = &us.funding.current_holder_commitment_tx.trust().built_transaction().transaction; - // Unwrap here; commitment transactions always have at least one output - let output_value_sat = transaction.output.iter().map(|txout| txout.value).reduce(|sum, value| sum + value).unwrap().to_sat(); + let output_value_sat: u64 = transaction.output.iter().map(|txout| txout.value.to_sat()).sum(); us.funding.channel_parameters.channel_value_satoshis - output_value_sat } else { 0 }, outbound_payment_htlc_rounded_msat, diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index ab61cee7036..bed8c168d56 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -4140,12 +4140,17 @@ where // Channel state once they will not be present in the next received commitment // transaction). let (local_balance_before_fee_msat, remote_balance_before_fee_msat) = { - let mut removed_outbound_total_msat = 0; - for htlc in self.pending_outbound_htlcs.iter() { - if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) = htlc.state { - removed_outbound_total_msat += htlc.amount_msat; - } - } + let removed_outbound_total_msat: u64 = self.pending_outbound_htlcs + .iter() + .filter_map(|htlc| { + matches!( + htlc.state, + OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) + | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) + ) + .then_some(htlc.amount_msat) + }) + .sum(); let pending_value_to_self_msat = funding.value_to_self_msat + htlc_stats.pending_inbound_htlcs_value_msat - removed_outbound_total_msat; let pending_remote_value_msat = @@ -4372,13 +4377,17 @@ where } if !funding.is_outbound() { - let mut removed_outbound_total_msat = 0; - for htlc in self.pending_outbound_htlcs.iter() { - if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) = htlc.state { - removed_outbound_total_msat += htlc.amount_msat; - } - } - + let removed_outbound_total_msat: u64 = self.pending_outbound_htlcs + .iter() + .filter_map(|htlc| { + matches!( + htlc.state, + OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) + | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) + ) + .then_some(htlc.amount_msat) + }) + .sum(); let pending_value_to_self_msat = funding.value_to_self_msat + htlc_stats.pending_inbound_htlcs_value_msat - removed_outbound_total_msat; let pending_remote_value_msat = From a954fff749ff140422675be3868a6d2db4ff7757 Mon Sep 17 00:00:00 2001 From: Leo Nash Date: Mon, 7 Jul 2025 17:10:40 +0000 Subject: [PATCH 2/3] Update the docs for `ClaimableOnChannelClose.transaction_fee_satoshis` In 85cc09ef4, we updated this member to include the sum of dust HTLCs on the commitment transaction, any elided anchors, as well as the sum of the msat amounts rounded down from non-dust HTLCs. --- lightning/src/chain/channelmonitor.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index cb28a8ebb5d..9461b02ef40 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -774,6 +774,8 @@ pub enum Balance { amount_satoshis: u64, /// The transaction fee we pay for the closing commitment transaction. This amount is not /// included in the [`Balance::ClaimableOnChannelClose::amount_satoshis`] value. + /// This amount includes the sum of dust HTLCs on the commitment transaction, any elided anchors, + /// as well as the sum of msat amounts rounded down from non-dust HTLCs. /// /// Note that if this channel is inbound (and thus our counterparty pays the commitment /// transaction fee) this value will be zero. For [`ChannelMonitor`]s created prior to LDK @@ -2860,7 +2862,8 @@ impl ChannelMonitor { let to_self_value_sat = us.funding.current_holder_commitment_tx.to_broadcaster_value_sat(); res.push(Balance::ClaimableOnChannelClose { amount_satoshis: to_self_value_sat + claimable_inbound_htlc_value_sat, - // In addition to `commit_tx_fee_sat`, this can also include dust HTLCs, and the total msat amount rounded down from non-dust HTLCs + // In addition to `commit_tx_fee_sat`, this can also include dust HTLCs, any elided anchors, + // and the total msat amount rounded down from non-dust HTLCs transaction_fee_satoshis: if us.holder_pays_commitment_tx_fee.unwrap_or(true) { let transaction = &us.funding.current_holder_commitment_tx.trust().built_transaction().transaction; let output_value_sat: u64 = transaction.output.iter().map(|txout| txout.value.to_sat()).sum(); From 3294d4532145707f55d9a3a55ad9c9a19a046dbe Mon Sep 17 00:00:00 2001 From: Leo Nash Date: Mon, 7 Jul 2025 17:23:15 +0000 Subject: [PATCH 3/3] Add a debug message if we catch an inconsistency in a `TxBuilder` impl `TxBuilder` will eventually be a public trait, and this debug assertion could catch any inconsistencies in the implementations of this trait. --- lightning/src/ln/channel.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index bed8c168d56..967ad41bc1b 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -4624,7 +4624,7 @@ where broadcaster_dust_limit_sat, logger, ); - debug_assert_eq!(stats, self.build_commitment_stats(funding, local, generated_by_local, None, None)); + debug_assert_eq!(stats, self.build_commitment_stats(funding, local, generated_by_local, None, None), "Caught an inconsistency between `TxBuilder::build_commitment_transaction` and the rest of the `TxBuilder` methods"); // This populates the HTLC-source table with the indices from the HTLCs in the commitment // transaction.