From d7d385f78f749ce6eb18f6e56ce0af7579c80322 Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Tue, 10 Oct 2023 22:30:38 +0200 Subject: [PATCH] invoices: refactor initial invoice scan when the registry starts In this commit we change how we select invoices to follow or delete when starting the InvoiceRegistry to instead of using the deperecated scan func from channeldb, use specific functions to gather pending and delete canceled invoices. --- invoices/invoiceregistry.go | 68 ++++++++----------------------------- 1 file changed, 15 insertions(+), 53 deletions(-) diff --git a/invoices/invoiceregistry.go b/invoices/invoiceregistry.go index e58ad4cdb..806b7a5e6 100644 --- a/invoices/invoiceregistry.go +++ b/invoices/invoiceregistry.go @@ -181,68 +181,30 @@ func NewRegistry(idb InvoiceDB, expiryWatcher *InvoiceExpiryWatcher, // to the invoice expiry watcher while also attempting to delete all canceled // invoices. func (i *InvoiceRegistry) scanInvoicesOnStart(ctx context.Context) error { - var ( - pending []invoiceExpiry - removable []InvoiceDeleteRef - ) - - reset := func() { - // Zero out our results on start and if the scan is ever run - // more than once. This latter case can happen if the kvdb - // layer needs to retry the View transaction underneath (eg. - // using the etcd driver, where all transactions are allowed - // to retry for serializability). - pending = nil - removable = make([]InvoiceDeleteRef, 0) - } - - scanFunc := func(paymentHash lntypes.Hash, - invoice *Invoice) error { - - if invoice.IsPending() { - expiryRef := makeInvoiceExpiry(paymentHash, invoice) - if expiryRef != nil { - pending = append(pending, expiryRef) - } - } else if i.cfg.GcCanceledInvoicesOnStartup && - invoice.State == ContractCanceled { - - // Consider invoice for removal if it is already - // canceled. Invoices that are expired but not yet - // canceled, will be queued up for cancellation after - // startup and will be deleted afterwards. - ref := InvoiceDeleteRef{ - PayHash: paymentHash, - AddIndex: invoice.AddIndex, - SettleIndex: invoice.SettleIndex, - } - - if invoice.Terms.PaymentAddr != BlankPayAddr { - ref.PayAddr = &invoice.Terms.PaymentAddr - } - - removable = append(removable, ref) - } - return nil - } - - err := i.idb.ScanInvoices(ctx, scanFunc, reset) + pendingInvoices, err := i.idb.FetchPendingInvoices(ctx) if err != nil { return err } + var pending []invoiceExpiry + for paymentHash, invoice := range pendingInvoices { + invoice := invoice + expiryRef := makeInvoiceExpiry(paymentHash, &invoice) + if expiryRef != nil { + pending = append(pending, expiryRef) + } + } + log.Debugf("Adding %d pending invoices to the expiry watcher", len(pending)) i.expiryWatcher.AddInvoices(pending...) - if len(removable) > 0 { - log.Infof("Attempting to delete %v canceled invoices", - len(removable)) - if err := i.idb.DeleteInvoice(ctx, removable); err != nil { + if i.cfg.GcCanceledInvoicesOnStartup { + log.Infof("Deleting canceled invoices") + err = i.idb.DeleteCanceledInvoices(ctx) + if err != nil { log.Warnf("Deleting canceled invoices failed: %v", err) - } else { - log.Infof("Deleted %v canceled invoices", - len(removable)) + return err } }