Skip to content

Commit b87a8e0

Browse files
authored
Merge pull request #3945 from tankyleo/txbuilder-fixups
#3775 follow-ups
2 parents eac01d0 + 3294d45 commit b87a8e0

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,8 @@ pub enum Balance {
774774
amount_satoshis: u64,
775775
/// The transaction fee we pay for the closing commitment transaction. This amount is not
776776
/// included in the [`Balance::ClaimableOnChannelClose::amount_satoshis`] value.
777+
/// This amount includes the sum of dust HTLCs on the commitment transaction, any elided anchors,
778+
/// as well as the sum of msat amounts rounded down from non-dust HTLCs.
777779
///
778780
/// Note that if this channel is inbound (and thus our counterparty pays the commitment
779781
/// transaction fee) this value will be zero. For [`ChannelMonitor`]s created prior to LDK
@@ -2860,11 +2862,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
28602862
let to_self_value_sat = us.funding.current_holder_commitment_tx.to_broadcaster_value_sat();
28612863
res.push(Balance::ClaimableOnChannelClose {
28622864
amount_satoshis: to_self_value_sat + claimable_inbound_htlc_value_sat,
2863-
// In addition to `commit_tx_fee_sat`, this can also include dust HTLCs, and the total msat amount rounded down from non-dust HTLCs
2865+
// In addition to `commit_tx_fee_sat`, this can also include dust HTLCs, any elided anchors,
2866+
// and the total msat amount rounded down from non-dust HTLCs
28642867
transaction_fee_satoshis: if us.holder_pays_commitment_tx_fee.unwrap_or(true) {
28652868
let transaction = &us.funding.current_holder_commitment_tx.trust().built_transaction().transaction;
2866-
// Unwrap here; commitment transactions always have at least one output
2867-
let output_value_sat = transaction.output.iter().map(|txout| txout.value).reduce(|sum, value| sum + value).unwrap().to_sat();
2869+
let output_value_sat: u64 = transaction.output.iter().map(|txout| txout.value.to_sat()).sum();
28682870
us.funding.channel_parameters.channel_value_satoshis - output_value_sat
28692871
} else { 0 },
28702872
outbound_payment_htlc_rounded_msat,

lightning/src/ln/channel.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4140,12 +4140,17 @@ where
41404140
// Channel state once they will not be present in the next received commitment
41414141
// transaction).
41424142
let (local_balance_before_fee_msat, remote_balance_before_fee_msat) = {
4143-
let mut removed_outbound_total_msat = 0;
4144-
for htlc in self.pending_outbound_htlcs.iter() {
4145-
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) = htlc.state {
4146-
removed_outbound_total_msat += htlc.amount_msat;
4147-
}
4148-
}
4143+
let removed_outbound_total_msat: u64 = self.pending_outbound_htlcs
4144+
.iter()
4145+
.filter_map(|htlc| {
4146+
matches!(
4147+
htlc.state,
4148+
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _))
4149+
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _))
4150+
)
4151+
.then_some(htlc.amount_msat)
4152+
})
4153+
.sum();
41494154
let pending_value_to_self_msat =
41504155
funding.value_to_self_msat + htlc_stats.pending_inbound_htlcs_value_msat - removed_outbound_total_msat;
41514156
let pending_remote_value_msat =
@@ -4372,13 +4377,17 @@ where
43724377
}
43734378

43744379
if !funding.is_outbound() {
4375-
let mut removed_outbound_total_msat = 0;
4376-
for htlc in self.pending_outbound_htlcs.iter() {
4377-
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) = htlc.state {
4378-
removed_outbound_total_msat += htlc.amount_msat;
4379-
}
4380-
}
4381-
4380+
let removed_outbound_total_msat: u64 = self.pending_outbound_htlcs
4381+
.iter()
4382+
.filter_map(|htlc| {
4383+
matches!(
4384+
htlc.state,
4385+
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _))
4386+
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _))
4387+
)
4388+
.then_some(htlc.amount_msat)
4389+
})
4390+
.sum();
43824391
let pending_value_to_self_msat =
43834392
funding.value_to_self_msat + htlc_stats.pending_inbound_htlcs_value_msat - removed_outbound_total_msat;
43844393
let pending_remote_value_msat =
@@ -4615,7 +4624,7 @@ where
46154624
broadcaster_dust_limit_sat,
46164625
logger,
46174626
);
4618-
debug_assert_eq!(stats, self.build_commitment_stats(funding, local, generated_by_local, None, None));
4627+
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");
46194628

46204629
// This populates the HTLC-source table with the indices from the HTLCs in the commitment
46214630
// transaction.

0 commit comments

Comments
 (0)