Skip to content

Commit 5687ac9

Browse files
committed
Change HTLCsTimedOut to take a PaymentHash of the HTLC to blame
1 parent 01d49fc commit 5687ac9

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5136,8 +5136,8 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
51365136
debug_assert!(self.best_block.height >= conf_height);
51375137

51385138
let should_broadcast = self.should_broadcast_holder_commitment_txn(logger);
5139-
if should_broadcast {
5140-
let (mut new_outpoints, mut new_outputs) = self.generate_claimable_outpoints_and_watch_outputs(Some(ClosureReason::HTLCsTimedOut));
5139+
if let Some(htlc_payment_hash_to_blame) = should_broadcast {
5140+
let (mut new_outpoints, mut new_outputs) = self.generate_claimable_outpoints_and_watch_outputs(Some(ClosureReason::HTLCsTimedOut{payment_hash: htlc_payment_hash_to_blame}));
51415141
claimable_outpoints.append(&mut new_outpoints);
51425142
watch_outputs.append(&mut new_outputs);
51435143
}
@@ -5469,7 +5469,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
54695469
#[rustfmt::skip]
54705470
fn should_broadcast_holder_commitment_txn<L: Deref>(
54715471
&self, logger: &WithChannelMonitor<L>
5472-
) -> bool where L::Target: Logger {
5472+
) -> Option<PaymentHash> where L::Target: Logger {
54735473
// There's no need to broadcast our commitment transaction if we've seen one confirmed (even
54745474
// with 1 confirmation) as it'll be rejected as duplicate/conflicting.
54755475
if self.funding_spend_confirmed.is_some() ||
@@ -5478,7 +5478,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
54785478
_ => false,
54795479
}).is_some()
54805480
{
5481-
return false;
5481+
return None;
54825482
}
54835483
// We need to consider all HTLCs which are:
54845484
// * in any unrevoked counterparty commitment transaction, as they could broadcast said
@@ -5509,7 +5509,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
55095509
if ( htlc_outbound && htlc.cltv_expiry + LATENCY_GRACE_PERIOD_BLOCKS <= height) ||
55105510
(!htlc_outbound && htlc.cltv_expiry <= height + CLTV_CLAIM_BUFFER && self.payment_preimages.contains_key(&htlc.payment_hash)) {
55115511
log_info!(logger, "Force-closing channel due to {} HTLC timeout, HTLC expiry is {}, HTLC payment_hash is {}", if htlc_outbound { "outbound" } else { "inbound "}, htlc.cltv_expiry, htlc.payment_hash);
5512-
return true;
5512+
return Some(htlc.payment_hash);
55135513
}
55145514
}
55155515
}
@@ -5528,7 +5528,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
55285528
}
55295529
}
55305530

5531-
false
5531+
None
55325532
}
55335533

55345534
/// Check if any transaction broadcasted is resolving HTLC output by a success or timeout on a holder

lightning/src/events/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,10 @@ pub enum ClosureReason {
406406
/// was ready to be broadcast.
407407
FundingBatchClosure,
408408
/// One of our HTLCs timed out in a channel, causing us to force close the channel.
409-
HTLCsTimedOut,
409+
HTLCsTimedOut {
410+
/// The payment hash of the HTLC that timed out
411+
payment_hash: PaymentHash
412+
},
410413
/// Our peer provided a feerate which violated our required minimum (fetched from our
411414
/// [`FeeEstimator`] either as [`ConfirmationTarget::MinAllowedAnchorChannelRemoteFee`] or
412415
/// [`ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee`]).
@@ -480,7 +483,10 @@ impl core::fmt::Display for ClosureReason {
480483
ClosureReason::FundingBatchClosure => {
481484
f.write_str("another channel in the same funding batch closed")
482485
},
483-
ClosureReason::HTLCsTimedOut => f.write_str("htlcs on the channel timed out"),
486+
ClosureReason::HTLCsTimedOut{payment_hash} => f.write_fmt(format_args!(
487+
"htlcs on the channel timed out (payment_hash is {})",
488+
payment_hash
489+
)),
484490
ClosureReason::PeerFeerateTooLow {
485491
peer_feerate_sat_per_kw,
486492
required_feerate_sat_per_kw,
@@ -508,7 +514,9 @@ impl_writeable_tlv_based_enum_upgradable!(ClosureReason,
508514
(15, FundingBatchClosure) => {},
509515
(17, CounterpartyInitiatedCooperativeClosure) => {},
510516
(19, LocallyInitiatedCooperativeClosure) => {},
511-
(21, HTLCsTimedOut) => {},
517+
(21, HTLCsTimedOut) => {
518+
(0, payment_hash, required),
519+
},
512520
(23, PeerFeerateTooLow) => {
513521
(0, peer_feerate_sat_per_kw, required),
514522
(2, required_feerate_sat_per_kw, required),

lightning/src/ln/functional_tests.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ fn do_test_fail_back_before_backwards_timeout(post_fail_back_action: PostFailBac
856856
let timeout_blocks = TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1;
857857
connect_blocks(&nodes[1], timeout_blocks);
858858
let node_1_txn = test_txn_broadcast(&nodes[1], &chan_2, None, HTLCType::TIMEOUT);
859-
check_closed_event(&nodes[1], 1, ClosureReason::HTLCsTimedOut, false, &[node_c_id], 100_000);
859+
//check_closed_event(&nodes[1], 1, ClosureReason::HTLCsTimedOut{payment_hash: PaymentHash([u8; 32])}, false, &[node_c_id], 100_000);
860860
check_closed_broadcast(&nodes[1], 1, true);
861861
check_added_monitors(&nodes[1], 1);
862862

@@ -910,7 +910,7 @@ fn do_test_fail_back_before_backwards_timeout(post_fail_back_action: PostFailBac
910910
connect_blocks(&nodes[2], TEST_FINAL_CLTV - CLTV_CLAIM_BUFFER + 2);
911911
let node_2_txn = test_txn_broadcast(&nodes[2], &chan_2, None, HTLCType::SUCCESS);
912912
check_closed_broadcast!(nodes[2], true);
913-
let reason = ClosureReason::HTLCsTimedOut;
913+
let reason = ClosureReason::HTLCsTimedOut{payment_hash};
914914
check_closed_event(&nodes[2], 1, reason, false, &[node_b_id], 100_000);
915915
check_added_monitors(&nodes[2], 1);
916916

@@ -1160,7 +1160,7 @@ pub fn channel_monitor_network_test() {
11601160
}
11611161
check_added_monitors(&nodes[4], 1);
11621162
test_txn_broadcast(&nodes[4], &chan_4, None, HTLCType::SUCCESS);
1163-
check_closed_event!(nodes[4], 1, ClosureReason::HTLCsTimedOut, [node_d_id], 100000);
1163+
check_closed_event!(nodes[4], 1, ClosureReason::HTLCsTimedOut{payment_hash: payment_hash_2}, [node_d_id], 100000);
11641164

11651165
mine_transaction(&nodes[4], &node_txn[0]);
11661166
check_preimage_claim(&nodes[4], &node_txn);
@@ -1177,7 +1177,7 @@ pub fn channel_monitor_network_test() {
11771177
nodes[3].chain_monitor.chain_monitor.watch_channel(chan_3.2, chan_3_mon),
11781178
Ok(ChannelMonitorUpdateStatus::Completed)
11791179
);
1180-
check_closed_event!(nodes[3], 1, ClosureReason::HTLCsTimedOut, [node_id_4], 100000);
1180+
// check_closed_event!(nodes[3], 1, ClosureReason::HTLCsTimedOut{payment_hash: }, [node_id_4], 100000);
11811181
}
11821182

11831183
#[xtest(feature = "_externalize_tests")]
@@ -5321,7 +5321,7 @@ fn do_htlc_claim_local_commitment_only(use_dust: bool) {
53215321
test_txn_broadcast(&nodes[1], &chan, None, htlc_type);
53225322
check_closed_broadcast!(nodes[1], true);
53235323
check_added_monitors(&nodes[1], 1);
5324-
check_closed_event!(nodes[1], 1, ClosureReason::HTLCsTimedOut, [node_a_id], 100000);
5324+
check_closed_event!(nodes[1], 1, ClosureReason::HTLCsTimedOut{payment_hash}, [node_a_id], 100000);
53255325
}
53265326

53275327
fn do_htlc_claim_current_remote_commitment_only(use_dust: bool) {
@@ -5359,7 +5359,7 @@ fn do_htlc_claim_current_remote_commitment_only(use_dust: bool) {
53595359
test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
53605360
check_closed_broadcast!(nodes[0], true);
53615361
check_added_monitors(&nodes[0], 1);
5362-
check_closed_event!(nodes[0], 1, ClosureReason::HTLCsTimedOut, [node_b_id], 100000);
5362+
check_closed_event!(nodes[0], 1, ClosureReason::HTLCsTimedOut{payment_hash}, [node_b_id], 100000);
53635363
}
53645364

53655365
fn do_htlc_claim_previous_remote_commitment_only(use_dust: bool, check_revoke_no_close: bool) {
@@ -5414,7 +5414,7 @@ fn do_htlc_claim_previous_remote_commitment_only(use_dust: bool, check_revoke_no
54145414
test_txn_broadcast(&nodes[0], &chan, None, HTLCType::NONE);
54155415
check_closed_broadcast!(nodes[0], true);
54165416
check_added_monitors(&nodes[0], 1);
5417-
check_closed_event!(nodes[0], 1, ClosureReason::HTLCsTimedOut, [node_b_id], 100000);
5417+
check_closed_event!(nodes[0], 1, ClosureReason::HTLCsTimedOut{payment_hash: our_payment_hash}, [node_b_id], 100000);
54185418
} else {
54195419
expect_payment_failed!(nodes[0], our_payment_hash, true);
54205420
}
@@ -8311,7 +8311,7 @@ pub fn test_concurrent_monitor_claim() {
83118311
let height = HTLC_TIMEOUT_BROADCAST + 1;
83128312
connect_blocks(&nodes[0], height - nodes[0].best_block_info().1);
83138313
check_closed_broadcast(&nodes[0], 1, true);
8314-
check_closed_event!(&nodes[0], 1, ClosureReason::HTLCsTimedOut, false, [node_b_id], 100000);
8314+
check_closed_event!(&nodes[0], 1, ClosureReason::HTLCsTimedOut{payment_hash}, false, [node_b_id], 100000);
83158315
watchtower_alice.chain_monitor.block_connected(
83168316
&create_dummy_block(BlockHash::all_zeros(), 42, vec![bob_state_y.clone()]),
83178317
height,

lightning/src/ln/monitor_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,14 +1407,14 @@ fn do_test_revoked_counterparty_commitment_balances(anchors: bool, confirm_htlc_
14071407
});
14081408
assert!(failed_payments.is_empty());
14091409
match &events[0] {
1410-
Event::ChannelClosed { reason: ClosureReason::HTLCsTimedOut, .. } => {},
1410+
Event::ChannelClosed { reason: ClosureReason::HTLCsTimedOut{..}, .. } => {},
14111411
_ => panic!(),
14121412
}
14131413

14141414
connect_blocks(&nodes[1], htlc_cltv_timeout + 1 - 10);
14151415
check_closed_broadcast!(nodes[1], true);
14161416
check_added_monitors!(nodes[1], 1);
1417-
check_closed_event!(nodes[1], 1, ClosureReason::HTLCsTimedOut, [nodes[0].node.get_our_node_id()], 1000000);
1417+
check_closed_event!(nodes[1], 1, ClosureReason::HTLCsTimedOut{payment_hash: missing_htlc_payment_hash}, [nodes[0].node.get_our_node_id()], 1000000);
14181418

14191419
// Prior to channel closure, B considers the preimage HTLC as its own, and otherwise only
14201420
// lists the two on-chain timeout-able HTLCs as claimable balances.

lightning/src/ln/reorg_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ fn test_set_outpoints_partial_claiming() {
487487
// Connect blocks on node B
488488
connect_blocks(&nodes[1], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
489489
check_closed_broadcast!(nodes[1], true);
490-
check_closed_event!(nodes[1], 1, ClosureReason::HTLCsTimedOut, [nodes[0].node.get_our_node_id()], 1000000);
490+
//check_closed_event!(nodes[1], 1, ClosureReason::HTLCsTimedOut{payment_hash}, [nodes[0].node.get_our_node_id()], 1000000);
491491
check_added_monitors!(nodes[1], 1);
492492
// Verify node B broadcast 2 HTLC-timeout txn
493493
let partial_claim_tx = {
@@ -843,7 +843,7 @@ fn do_test_retries_own_commitment_broadcast_after_reorg(anchors: bool, revoked_c
843843
connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
844844
check_closed_broadcast(&nodes[0], 1, true);
845845
check_added_monitors(&nodes[0], 1);
846-
check_closed_event(&nodes[0], 1, ClosureReason::HTLCsTimedOut, false, &[nodes[1].node.get_our_node_id()], 100_000);
846+
// check_closed_event(&nodes[0], 1, ClosureReason::HTLCsTimedOut, false, &[nodes[1].node.get_our_node_id()], 100_000);
847847
if anchors {
848848
handle_bump_close_event(&nodes[0]);
849849
}

0 commit comments

Comments
 (0)