Merge pull request #5224 from bhandras/invoice-gc-errors

invoices: do not fail DeleteInvoice if payment addr isn't indexed
This commit is contained in:
Olaoluwa Osuntokun 2021-04-29 15:39:35 -07:00 committed by GitHub
commit 41c089fbf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 20 deletions

@ -2425,15 +2425,6 @@ func TestDeleteInvoices(t *testing.T) {
// Restore the hash. // Restore the hash.
invoicesToDelete[0].PayHash[2] ^= 3 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 // XOR the second invoice's payment settle index as it is settled, and
// attempt to delete. // attempt to delete.
invoicesToDelete[1].SettleIndex ^= 11 invoicesToDelete[1].SettleIndex ^= 11

@ -2305,15 +2305,19 @@ func (d *DB) DeleteInvoice(invoicesToDelete []InvoiceDeleteRef) error {
// fetched invoice key matches the one in the // fetched invoice key matches the one in the
// payment address index. // payment address index.
key := payAddrIndex.Get(ref.PayAddr[:]) key := payAddrIndex.Get(ref.PayAddr[:])
if !bytes.Equal(key, invoiceKey) { if bytes.Equal(key, invoiceKey) {
return fmt.Errorf("unknown invoice " + // Delete from the payment address index.
"in pay addr index") // Note that since the payment address
} // index has been introduced with an
// empty migration it may be possible
// Delete from the payment address index. // that the index doesn't have an entry
err := payAddrIndex.Delete(ref.PayAddr[:]) // for this invoice.
if err != nil { // ref: https://github.com/lightningnetwork/lnd/pull/4285/commits/cbf71b5452fa1d3036a43309e490787c5f7f08dc#r426368127
return err if err := payAddrIndex.Delete(
ref.PayAddr[:],
); err != nil {
return err
}
} }
} }

@ -213,8 +213,15 @@ func (i *InvoiceRegistry) scanInvoicesOnStart() error {
len(pending)) len(pending))
i.expiryWatcher.AddInvoices(pending...) i.expiryWatcher.AddInvoices(pending...)
if err := i.cdb.DeleteInvoice(removable); err != nil { if len(removable) > 0 {
log.Warnf("Deleting old invoices failed: %v", err) 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 return nil