diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index 616de1f0e3f..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,11 +2862,11 @@ 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; - // 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..967ad41bc1b 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 = @@ -4615,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.