-
Notifications
You must be signed in to change notification settings - Fork 422
Only claim HTLCs with matching payment hash upon preimage monitor update #4154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TheBlueMatt
merged 2 commits into
lightningdevkit:main
from
wpaulino:ladder-preimage-claim-fix
Oct 10, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3836,3 +3836,85 @@ fn test_lost_timeout_monitor_events() { | |
do_test_lost_timeout_monitor_events(CommitmentType::LocalWithLastHTLC, false, true); | ||
do_test_lost_timeout_monitor_events(CommitmentType::LocalWithLastHTLC, true, true); | ||
} | ||
|
||
#[test] | ||
fn test_ladder_preimage_htlc_claims() { | ||
// Tests that when we learn of a preimage via a monitor update we only claim HTLCs with the | ||
// corresponding payment hash. This test is a reproduction of a scenario that happened in | ||
// production where the second HTLC claim also included the first HTLC (even though it was | ||
// already claimed) resulting in an invalid claim transaction. | ||
let chanmon_cfgs = create_chanmon_cfgs(2); | ||
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); | ||
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); | ||
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); | ||
|
||
let node_id_0 = nodes[0].node.get_our_node_id(); | ||
let node_id_1 = nodes[1].node.get_our_node_id(); | ||
|
||
let (_, _, channel_id, _) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0); | ||
|
||
let (payment_preimage1, payment_hash1, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000); | ||
let (payment_preimage2, payment_hash2, _, _) = route_payment(&nodes[0], &[&nodes[1]], 1_000_000); | ||
|
||
nodes[0].node.force_close_broadcasting_latest_txn(&channel_id, &node_id_1, "test".to_string()).unwrap(); | ||
check_added_monitors(&nodes[0], 1); | ||
check_closed_broadcast(&nodes[0], 1, true); | ||
let reason = ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(true), message: "test".to_string() }; | ||
check_closed_event(&nodes[0], 1, reason, false, &[node_id_1], 1_000_000); | ||
|
||
let commitment_tx = { | ||
let mut txn = nodes[0].tx_broadcaster.txn_broadcast(); | ||
assert_eq!(txn.len(), 1); | ||
txn.remove(0) | ||
}; | ||
mine_transaction(&nodes[0], &commitment_tx); | ||
mine_transaction(&nodes[1], &commitment_tx); | ||
|
||
check_closed_broadcast(&nodes[1], 1, true); | ||
check_added_monitors(&nodes[1], 1); | ||
check_closed_event(&nodes[1], 1, ClosureReason::CommitmentTxConfirmed, false, &[node_id_0], 1_000_000); | ||
|
||
nodes[1].node.claim_funds(payment_preimage1); | ||
expect_payment_claimed!(&nodes[1], payment_hash1, 1_000_000); | ||
check_added_monitors(&nodes[1], 1); | ||
|
||
let (htlc1, htlc_claim_tx1) = { | ||
let mut txn = nodes[1].tx_broadcaster.txn_broadcast(); | ||
assert_eq!(txn.len(), 1); | ||
let htlc_claim_tx = txn.remove(0); | ||
assert_eq!(htlc_claim_tx.input.len(), 1); | ||
check_spends!(htlc_claim_tx, commitment_tx); | ||
(htlc_claim_tx.input[0].previous_output, htlc_claim_tx) | ||
}; | ||
mine_transaction(&nodes[0], &htlc_claim_tx1); | ||
mine_transaction(&nodes[1], &htlc_claim_tx1); | ||
|
||
connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1); | ||
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1); | ||
|
||
expect_payment_sent(&nodes[0], payment_preimage1, None, true, false); | ||
check_added_monitors(&nodes[0], 1); | ||
|
||
nodes[1].node.claim_funds(payment_preimage2); | ||
expect_payment_claimed!(&nodes[1], payment_hash2, 1_000_000); | ||
check_added_monitors(&nodes[1], 1); | ||
|
||
let (htlc2, htlc_claim_tx2) = { | ||
let mut txn = nodes[1].tx_broadcaster.txn_broadcast(); | ||
assert_eq!(txn.len(), 1, "{:?}", txn.iter().map(|tx| tx.compute_txid()).collect::<Vec<_>>()); | ||
let htlc_claim_tx = txn.remove(0); | ||
assert_eq!(htlc_claim_tx.input.len(), 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add coverage for the case where at this point we should claim multiple outputs on the commit tx ? I'm thinking of the MPP case. The actual code does this correctly. |
||
check_spends!(htlc_claim_tx, commitment_tx); | ||
(htlc_claim_tx.input[0].previous_output, htlc_claim_tx) | ||
}; | ||
assert_ne!(htlc1, htlc2); | ||
|
||
mine_transaction(&nodes[0], &htlc_claim_tx2); | ||
mine_transaction(&nodes[1], &htlc_claim_tx2); | ||
|
||
connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1); | ||
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1); | ||
|
||
expect_payment_sent(&nodes[0], payment_preimage2, None, true, false); | ||
check_added_monitors(&nodes[0], 1); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if we can be more aggressive and remove the
Option
here - we have this data at all places where this is called. If not, we lose funds, and we should panic ? Also applies toget_point_for_commitment_number
.