- 
                Notifications
    You must be signed in to change notification settings 
- Fork 421
Fix matching of second-stage HTLC claim in get_htlc_balance #2610
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
Conversation
7f9eac1    to
    678f7a0      
    Compare
  
    678f7a0    to
    eec686b      
    Compare
  
    eec686b    to
    9694687      
    Compare
  
    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.
9694687    to
    fd66a29      
    Compare
  
    | Codecov ReportAttention:  
 ❗ 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     
 ☔ View full report in Codecov by Sentry. | 
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.
| // 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. | 
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.
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).
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_balancesto 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
0in the commitment transaction, we extend the existingget_claimable_balancestest to cover anchor outputs channels instead. This automatically results in HTLCs having a different output index than0because the two anchor outputs in commitment transactions usually come first due to the output sorting.