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 two issues in Balance generation #2593

Conversation

TheBlueMatt
Copy link
Collaborator

The second issue I hit in prod on my node - an anchor HTLC timeout transaction confirmed but the output on the HTLC transaction was still locked for another few hundred blocks. During this time, we don't generate a corresponding Balance at all, which is a potentially dangerous situation. While fixing it, I noticed the first issue - we shouldn't be deciding if an HTLC is confirming based on the output index in the HTLC transaction matching the output index in the commitment transaction.

Sadly I don't have time to write tests here (or real commit messages). The second should be trivial to test, the first shouldn't be too hard, I think if we swap around some of the amounts in the existing balances tests it'll swap the output order and the old code will break.

This will allow us to use the HTLC resolution tracking to detect
the HTLC spend via `OnchainEvent::MaturingOutput` in the next
commit.
@TheBlueMatt TheBlueMatt added the help wanted Extra attention is needed label Sep 24, 2023
@TheBlueMatt TheBlueMatt added this to the 0.0.117 milestone Sep 24, 2023
@codecov-commenter
Copy link

Codecov Report

Patch coverage: 90.62% and project coverage change: -0.04% ⚠️

Comparison is base (a37a16a) 88.86% compared to head (fda3894) 88.83%.

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

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2593      +/-   ##
==========================================
- Coverage   88.86%   88.83%   -0.04%     
==========================================
  Files         113      113              
  Lines       84419    84427       +8     
  Branches    84419    84427       +8     
==========================================
- Hits        75022    75000      -22     
- Misses       7197     7222      +25     
- Partials     2200     2205       +5     
Files Changed Coverage Δ
lightning/src/chain/channelmonitor.rs 84.83% <90.62%> (-0.07%) ⬇️

... and 6 files with indirect coverage changes

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

@wpaulino wpaulino self-assigned this Sep 24, 2023
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.

Situation is dangerous in the sense than get_claimable_balances might yield back erroneous balance, or even lack to see them. Not a time-sensitive claim direct error as HTLC claims are handled by other ChannelMonitor logics. This still can be an issue for ChainMonitor::get_claimable_balances() consumers.

Looking on Balance detection, I don’t know if we’re covering the claim of the two anchor outputs through the anyone can spend path, once the CSV encumbrance is expired. Not necessarily a must, though nice to clean the UTXO set if we can afford it.

I don’t have time to write test either, at least before catching up on my mempool reviews though available to review more fixes.

descriptor: SpendableOutputDescriptor::DelayedPaymentOutput(_) }
if event.transaction.as_ref().map(|tx| tx.input.iter()
.any(|inp| Some(inp.previous_output.txid) == confirmed_txid &&
inp.previous_output.vout == htlc_commitment_tx_output_idx))
Copy link

Choose a reason for hiding this comment

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

So before this commit 259815f we will match any DelayedPaymentOutput as soon as the htlc_commitment_tx_output_idx match the transaction output. IIUC, I think such case can arise when you have a second-stage HTLC transaction revocable output with an index higher than the balance outputs (according to BOLT3 outputs ordering). Post-anchor, HTLC-timeout transaction can be aggregated so the index can be higher than 1.

The fix adds an additional check than parent commitment transaction is confirmed.

},
OnchainEvent::MaturingOutput {
descriptor: SpendableOutputDescriptor::DelayedPaymentOutput(ref descriptor) }
if descriptor.outpoint.into_bitcoin_outpoint() == htlc_output_to_spend => {
Copy link

Choose a reason for hiding this comment

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

IIUC this path should check we’re detecting well a second-stage HTLC transaction (HTLC-timeout or HTLC-preimage) CSV encumbered delayed and return further a ClaimableAwaitingConfirmations balance event

@wpaulino
Copy link
Contributor

Closing in favor of #2610. @ariard feel free to continue review there.

@wpaulino wpaulino closed this Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants