From adbcc3f7a34926d7db336fab32b6db7919a7c492 Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Mon, 19 Apr 2021 16:30:53 +0200 Subject: [PATCH 1/2] invoices: do not fail DeleteInvoice if payment addr isn't indexed This commit relaxes DeleteInvoice failure cases by removing the requirement that the invoice needs to be indexed by the payment address. Since payment address index was introduced with an empty migration it is possible that users have old invoices which were never added to this index causing invoice garbage collection to get stuck. --- channeldb/invoice_test.go | 9 --------- channeldb/invoices.go | 22 +++++++++++++--------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/channeldb/invoice_test.go b/channeldb/invoice_test.go index 8f0f2848..ec54353d 100644 --- a/channeldb/invoice_test.go +++ b/channeldb/invoice_test.go @@ -2425,15 +2425,6 @@ func TestDeleteInvoices(t *testing.T) { // Restore the hash. invoicesToDelete[0].PayHash[2] ^= 3 - // XOR one byte of one of the references' payment address and attempt - // to delete. - invoicesToDelete[1].PayAddr[5] ^= 7 - require.Error(t, db.DeleteInvoice(invoicesToDelete)) - assertInvoiceCount(3) - - // Restore the payment address. - invoicesToDelete[1].PayAddr[5] ^= 7 - // XOR the second invoice's payment settle index as it is settled, and // attempt to delete. invoicesToDelete[1].SettleIndex ^= 11 diff --git a/channeldb/invoices.go b/channeldb/invoices.go index 9e3e7326..146e8d88 100644 --- a/channeldb/invoices.go +++ b/channeldb/invoices.go @@ -2305,15 +2305,19 @@ func (d *DB) DeleteInvoice(invoicesToDelete []InvoiceDeleteRef) error { // fetched invoice key matches the one in the // payment address index. key := payAddrIndex.Get(ref.PayAddr[:]) - if !bytes.Equal(key, invoiceKey) { - return fmt.Errorf("unknown invoice " + - "in pay addr index") - } - - // Delete from the payment address index. - err := payAddrIndex.Delete(ref.PayAddr[:]) - if err != nil { - return err + if bytes.Equal(key, invoiceKey) { + // Delete from the payment address index. + // Note that since the payment address + // index has been introduced with an + // empty migration it may be possible + // that the index doesn't have an entry + // for this invoice. + // ref: https://github.com/lightningnetwork/lnd/pull/4285/commits/cbf71b5452fa1d3036a43309e490787c5f7f08dc#r426368127 + if err := payAddrIndex.Delete( + ref.PayAddr[:], + ); err != nil { + return err + } } } From ec50f2cccedd6582d1b15a6be224159821e57a98 Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Thu, 22 Apr 2021 15:31:17 +0200 Subject: [PATCH 2/2] invoices: more verbose logging for startup invoice GC --- invoices/invoiceregistry.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/invoices/invoiceregistry.go b/invoices/invoiceregistry.go index b73de5e5..5016c96c 100644 --- a/invoices/invoiceregistry.go +++ b/invoices/invoiceregistry.go @@ -213,8 +213,15 @@ func (i *InvoiceRegistry) scanInvoicesOnStart() error { len(pending)) i.expiryWatcher.AddInvoices(pending...) - if err := i.cdb.DeleteInvoice(removable); err != nil { - log.Warnf("Deleting old invoices failed: %v", err) + if len(removable) > 0 { + log.Infof("Attempting to delete %v canceled invoices", + len(removable)) + if err := i.cdb.DeleteInvoice(removable); err != nil { + log.Warnf("Deleting canceled invoices failed: %v", err) + } else { + log.Infof("Deleted %v canceled invoices", + len(removable)) + } } return nil