Skip to content
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

Fix matching of second-stage HTLC claim in get_htlc_balance #2610

Merged
merged 9 commits into from
Sep 29, 2023

Conversation

wpaulino
Copy link
Contributor

We incorrectly assumed that the descriptor's output index from second-stage HTLC transaction would always match the HTLC's output index in the commitment transaction. This doesn't make any sense though, we need to make sure we map the descriptor to it's corresponding HTLC in the commitment. Instead, we check that the transaction from which the descriptor originated from spends the HTLC in question.

Note that pre-anchors, second-stage HTLC transactions are always 1 input-1 output, so previously we would only match if the HTLC was the first output in the commitment transaction. Post-anchors, they are malleable, so we can aggregate multiple HTLC claims into a single transaction making this even more likely to happen. Unfortunately, we lacked proper coverage in this area so the bug went unnoticed. To address this, we aim to extend our existing coverage of get_claimable_balances to anchor outputs channels in the following commits.


Depends on #2605 and #2606.

This replaces #2593. It turned out that only the second commit was necessary, as the third commit is just another way of doing the second commit, and the first commit is only a pre-requisite for the third commit. Rather than changing HTLC amounts to ensure proper coverage when HTLCs have a different output index than 0 in the commitment transaction, we extend the existing get_claimable_balances test to cover anchor outputs channels instead. This automatically results in HTLCs having a different output index than 0 because the two anchor outputs in commitment transactions usually come first due to the output sorting.

lightning/src/ln/monitor_tests.rs Outdated Show resolved Hide resolved
lightning/src/ln/monitor_tests.rs Show resolved Hide resolved
lightning/src/ln/monitor_tests.rs Outdated Show resolved Hide resolved
lightning/src/chain/channelmonitor.rs Outdated Show resolved Hide resolved
lightning/src/chain/channelmonitor.rs Outdated Show resolved Hide resolved
TheBlueMatt and others added 9 commits September 29, 2023 16:24
We incorrectly assumed that the descriptor's output index from
second-stage HTLC transaction would always match the HTLC's output index
in the commitment transaction. This doesn't make any sense though, we
need to make sure we map the descriptor to it's corresponding HTLC in
the commitment. Instead, we check that the transaction from which the
descriptor originated from spends the HTLC in question.

Note that pre-anchors, second-stage HTLC transactions are always 1
input-1 output, so previously we would only match if the HTLC was the
first output in the commitment transaction. Post-anchors, they are
malleable, so  we can aggregate multiple HTLC claims into a single
transaction making this even more likely to happen. Unfortunately, we
lacked proper coverage in this area so the bug went unnoticed. To
address this, we aim to extend our existing coverage of
`get_claimable_balances` to anchor outputs channels in the following
commits.
@codecov-commenter
Copy link

Codecov Report

Attention: 10 lines in your changes are missing coverage. Please review.

Comparison is base (620244d) 89.05% compared to head (fd66a29) 89.00%.
Report is 9 commits behind head on main.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2610      +/-   ##
==========================================
- Coverage   89.05%   89.00%   -0.06%     
==========================================
  Files         112      112              
  Lines       86705    86930     +225     
  Branches    86705    86930     +225     
==========================================
+ Hits        77217    77369     +152     
- Misses       7259     7330      +71     
- Partials     2229     2231       +2     
Files Coverage Δ
lightning/src/chain/channelmonitor.rs 84.72% <100.00%> (-0.14%) ⬇️
lightning/src/ln/functional_test_utils.rs 91.38% <81.81%> (-0.11%) ⬇️
lightning/src/ln/monitor_tests.rs 98.06% <98.17%> (-0.44%) ⬇️

... and 12 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@TheBlueMatt TheBlueMatt merged commit cff2061 into lightningdevkit:main Sep 29, 2023
4 of 15 checks passed
@wpaulino wpaulino deleted the missing-htlc-claim-balance branch September 29, 2023 23:55
Copy link

@ariard ariard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post-merge Code Review ACK fd66a29

Didn’t look into the tests deeply, though this code approach is far better than #2593 as now we have one code branch to handle balance detection of maturing output of second-stage HTLC transactions.

// index as the HTLC input. This is true pre-anchors, as there's
// only 1 input and 1 output. This is also true post-anchors,
// because we have a SIGHASH_SINGLE|ANYONECANPAY signature from our
// channel counterparty.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be canonical this is a property of the transaction signature bip143 digest algorithm where the sha_single_output is computed from the index of the spent input. This is also true for bip341 digest algorithm, however if transaction digest algorithm computation of sighash flags change in the future, we would have to update this code in consequence (and indeed more complicated sighash flags have been proposed in the past).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants