Skip to content

Commit a9892a9

Browse files
committed
Include HTLC amts in ClaimableOnChannelClose.transaction_fee_satoshis
In addition to the amount set by `commit_tx_fee_sat`, the transaction fee of a commitment transaction may also increase due to HTLC amounts that get burned to fees, and any elided anchors. We now include these amounts in the `transaction_fee_satoshis` field of `Balance::ClaimableOnChannelClose` too. This commit also makes the `transaction_fee_satoshis` field correct for custom transactions not encompassed in `chan_utils::commit_tx_fee_sat`.
1 parent 660b86e commit a9892a9

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2736,15 +2736,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
27362736
}
27372737
} else {
27382738
let mut claimable_inbound_htlc_value_sat = 0;
2739-
let mut nondust_htlc_count = 0;
27402739
let mut outbound_payment_htlc_rounded_msat = 0;
27412740
let mut outbound_forwarded_htlc_rounded_msat = 0;
27422741
let mut inbound_claiming_htlc_rounded_msat = 0;
27432742
let mut inbound_htlc_rounded_msat = 0;
27442743
for (htlc, source) in holder_commitment_htlcs!(us, CURRENT_WITH_SOURCES) {
2745-
if htlc.transaction_output_index.is_some() {
2746-
nondust_htlc_count += 1;
2747-
}
27482744
let rounded_value_msat = if htlc.transaction_output_index.is_none() {
27492745
htlc.amount_msat
27502746
} else { htlc.amount_msat % 1000 };
@@ -2788,11 +2784,12 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
27882784
let to_self_value_sat = us.funding.current_holder_commitment_tx.to_broadcaster_value_sat();
27892785
res.push(Balance::ClaimableOnChannelClose {
27902786
amount_satoshis: to_self_value_sat + claimable_inbound_htlc_value_sat,
2787+
// In addition to `commit_tx_fee_sat`, this can also include dust HTLCs, and the total msat amount rounded down from non-dust HTLCs
27912788
transaction_fee_satoshis: if us.holder_pays_commitment_tx_fee.unwrap_or(true) {
2792-
chan_utils::commit_tx_fee_sat(
2793-
us.funding.current_holder_commitment_tx.feerate_per_kw(), nondust_htlc_count,
2794-
us.channel_type_features(),
2795-
)
2789+
let transaction = &us.funding.current_holder_commitment_tx.trust().built_transaction().transaction;
2790+
// Unwrap here; commitment transactions always have at least one output
2791+
let output_value_sat = transaction.output.iter().map(|txout| txout.value).reduce(|sum, value| sum + value).unwrap().to_sat();
2792+
us.funding.channel_parameters.channel_value_satoshis - output_value_sat
27962793
} else { 0 },
27972794
outbound_payment_htlc_rounded_msat,
27982795
outbound_forwarded_htlc_rounded_msat,

lightning/src/ln/monitor_tests.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,11 @@ fn do_test_claim_value_force_close(anchors: bool, prev_commitment_tx: bool) {
544544
let commitment_tx_fee = chan_feerate as u64 *
545545
(chan_utils::commitment_tx_base_weight(&channel_type_features) + 2 * chan_utils::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
546546
let anchor_outputs_value = if anchors { 2 * channel::ANCHOR_OUTPUT_VALUE_SATOSHI } else { 0 };
547+
let amount_satoshis = 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - commitment_tx_fee - anchor_outputs_value - 1; /* msat amount that is burned to fees */
547548
assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
548-
amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - commitment_tx_fee - anchor_outputs_value - 1 /* msat amount that is burned to fees */,
549-
transaction_fee_satoshis: commitment_tx_fee,
549+
amount_satoshis,
550+
// In addition to `commitment_tx_fee`, this also includes the dust HTLC, and the total msat amount rounded down from non-dust HTLCs
551+
transaction_fee_satoshis: 1_000_000 - 4_000 - 3_000 - 1_000 - amount_satoshis - anchor_outputs_value,
550552
outbound_payment_htlc_rounded_msat: 3300,
551553
outbound_forwarded_htlc_rounded_msat: 0,
552554
inbound_claiming_htlc_rounded_msat: 0,
@@ -598,16 +600,18 @@ fn do_test_claim_value_force_close(anchors: bool, prev_commitment_tx: bool) {
598600
let commitment_tx_fee = chan_feerate as u64 *
599601
(chan_utils::commitment_tx_base_weight(&channel_type_features) +
600602
if prev_commitment_tx { 1 } else { 2 } * chan_utils::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
601-
let mut a_expected_balances = vec![Balance::ClaimableOnChannelClose {
602-
amount_satoshis: 1_000_000 - // Channel funding value in satoshis
603+
let amount_satoshis = 1_000_000 - // Channel funding value in satoshis
603604
4_000 - // The to-be-failed HTLC value in satoshis
604605
3_000 - // The claimed HTLC value in satoshis
605606
1_000 - // The push_msat value in satoshis
606607
3 - // The dust HTLC value in satoshis
607608
commitment_tx_fee - // The commitment transaction fee with two HTLC outputs
608609
anchor_outputs_value - // The anchor outputs value in satoshis
609-
1, // The rounded up msat part of the one HTLC
610-
transaction_fee_satoshis: commitment_tx_fee,
610+
1; // The rounded up msat part of the one HTLC
611+
let mut a_expected_balances = vec![Balance::ClaimableOnChannelClose {
612+
amount_satoshis, // Channel funding value in satoshis
613+
// In addition to `commitment_tx_fee`, this also includes the dust HTLC, and the total msat amount rounded down from non-dust HTLCs
614+
transaction_fee_satoshis: 1_000_000 - 4_000 - 3_000 - 1_000 - amount_satoshis - anchor_outputs_value,
611615
outbound_payment_htlc_rounded_msat: 3000 + if prev_commitment_tx {
612616
200 /* 1 to-be-failed HTLC */ } else { 300 /* 2 HTLCs */ },
613617
outbound_forwarded_htlc_rounded_msat: 0,

0 commit comments

Comments
 (0)