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

[bug]: reverse the order inside invoice settlement flow #7463

Open
yyforyongyu opened this issue Feb 27, 2023 · 1 comment
Open

[bug]: reverse the order inside invoice settlement flow #7463

yyforyongyu opened this issue Feb 27, 2023 · 1 comment
Assignees
Labels
bug Unintended code behaviour htlcswitch intermediate Issues suitable for developers moderately familiar with the codebase and LN invoices itests Issues related to integration tests. P2 should be fixed if one has time

Comments

@yyforyongyu
Copy link
Member

Background

When updating an invoice state, we'd mark it as settled before the HTLC is locked in first, resulting in a possible edge case that although an invoice is marked as settled, the HTLC is timed out.

lnd/htlcswitch/link.go

Lines 3285 to 3307 in d321182

event, err := l.cfg.Registry.NotifyExitHopHtlc(
invoiceHash, pd.Amount, pd.Timeout, int32(heightNow),
circuitKey, l.hodlQueue.ChanIn(), payload,
)
if err != nil {
return err
}
// Create a hodlHtlc struct and decide either resolved now or later.
htlc := hodlHtlc{
pd: pd,
obfuscator: obfuscator,
}
// If the event is nil, the invoice is being held, so we save payment
// descriptor for future reference.
if event == nil {
l.hodlMap[circuitKey] = htlc
return nil
}
// Process the received resolution.
return l.processHtlcResolution(event, htlc)

  • the invoice is marked as settled in NotifyExitHopHtlc, which calls notifyExitHopHtlcLocked.
  • the HTLC is settled in return l.processHtlcResolution(event, htlc).

With the final settle signal introduced here this issue is mitigated with extra data, but this still needs a proper fix.

Steps to reproduce

Remove the following sleep in testExternalFundingChanPoint and run the itest,

// Before Dave closes the channel, he needs to check the invoice is
// settled to avoid an error saying cannot close channel due to active
// HTLCs.
ht.AssertInvoiceSettled(dave, resp.PaymentAddr)
// TODO(yy): remove the sleep once the following bug is fixed.
// When the invoice is reported settled, the commitment dance is not
// yet finished, which can cause an error when closing the channel,
// saying there's active HTLCs. We need to investigate this issue and
// reverse the order to, first finish the commitment dance, then report
// the invoice as settled.
time.Sleep(2 * time.Second)

Expected behavior

When the invoice is reported as settled, the commitment dance should be finished.

Possible Solutions

  1. Reverse the steps so the HTLC is locked in before the invoice is marked as settled, or,
  2. Add a new state PendingSettle to the invoice and replace it with the current Settled state. Then we only mark it as settled AFTER the HTLC lock-in.
@yyforyongyu yyforyongyu added bug Unintended code behaviour intermediate Issues suitable for developers moderately familiar with the codebase and LN htlcswitch itests Issues related to integration tests. invoices labels Feb 27, 2023
@joostjager
Copy link
Contributor

An alternative name for a new PendingSettle state could be SettleRequested.

@yyforyongyu yyforyongyu changed the title [bug]: Invoices are settled while the commitment dance is not finished [bug]: reverse the order inside invoice settlement flow Feb 28, 2023
@saubyk saubyk added this to the v0.16.1 milestone Mar 2, 2023
@saubyk saubyk modified the milestones: v0.16.1, v0.17.0 Mar 13, 2023
@saubyk saubyk added the P2 should be fixed if one has time label Aug 8, 2023
@saubyk saubyk removed this from the Low Priority milestone Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unintended code behaviour htlcswitch intermediate Issues suitable for developers moderately familiar with the codebase and LN invoices itests Issues related to integration tests. P2 should be fixed if one has time
Projects
None yet
Development

No branches or pull requests

3 participants