From a0d7877d9a327dbd74ac9426551f2ab30ae41753 Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Tue, 28 Jul 2020 23:57:51 +0200 Subject: [PATCH] multi: make canceled invoice garbage collection configurable This commit extends the application config with a flag to control canceled invoice garbage collection upon startup. --- config.go | 2 ++ invoices/invoiceregistry.go | 8 +++++++- invoices/invoiceregistry_test.go | 5 +++-- server.go | 11 ++++++----- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/config.go b/config.go index 3a52ca96..89f3fa89 100644 --- a/config.go +++ b/config.go @@ -242,6 +242,8 @@ type Config struct { KeysendHoldTime time.Duration `long:"keysend-hold-time" description:"If non-zero, keysend payments are accepted but not immediately settled. If the payment isn't settled manually after the specified time, it is canceled automatically. [experimental]"` + GcCanceledInvoicesOnStartup bool `long:"gc-canceled-invoices-on-startup" description:"If true, we'll attempt to garbage collect canceled invoices upon start."` + Routing *routing.Conf `group:"routing" namespace:"routing"` Workers *lncfg.Workers `group:"workers" namespace:"workers"` diff --git a/invoices/invoiceregistry.go b/invoices/invoiceregistry.go index e53d7da8..bc40168a 100644 --- a/invoices/invoiceregistry.go +++ b/invoices/invoiceregistry.go @@ -57,6 +57,10 @@ type RegistryConfig struct { // send payments. AcceptKeySend bool + // GcCanceledInvoicesOnStartup if set, we'll attempt to garbage collect + // all canceled invoices upon start. + GcCanceledInvoicesOnStartup bool + // KeysendHoldTime indicates for how long we want to accept and hold // spontaneous keysend payments. KeysendHoldTime time.Duration @@ -171,7 +175,9 @@ func (i *InvoiceRegistry) scanInvoicesOnStart() error { if invoice.IsPending() { pending[paymentHash] = invoice - } else if invoice.State == channeldb.ContractCanceled { + } else if i.cfg.GcCanceledInvoicesOnStartup && + invoice.State == channeldb.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 diff --git a/invoices/invoiceregistry_test.go b/invoices/invoiceregistry_test.go index 0da260a2..6e5ca221 100644 --- a/invoices/invoiceregistry_test.go +++ b/invoices/invoiceregistry_test.go @@ -1091,8 +1091,9 @@ func TestOldInvoiceRemovalOnStart(t *testing.T) { require.NoError(t, err) cfg := RegistryConfig{ - FinalCltvRejectDelta: testFinalCltvRejectDelta, - Clock: testClock, + FinalCltvRejectDelta: testFinalCltvRejectDelta, + Clock: testClock, + GcCanceledInvoicesOnStartup: true, } expiryWatcher := NewInvoiceExpiryWatcher(cfg.Clock) diff --git a/server.go b/server.go index fe711cec..d3de88ef 100644 --- a/server.go +++ b/server.go @@ -396,11 +396,12 @@ func newServer(cfg *Config, listenAddrs []net.Addr, chanDB *channeldb.DB, } registryConfig := invoices.RegistryConfig{ - FinalCltvRejectDelta: lncfg.DefaultFinalCltvRejectDelta, - HtlcHoldDuration: invoices.DefaultHtlcHoldDuration, - Clock: clock.NewDefaultClock(), - AcceptKeySend: cfg.AcceptKeySend, - KeysendHoldTime: cfg.KeysendHoldTime, + FinalCltvRejectDelta: lncfg.DefaultFinalCltvRejectDelta, + HtlcHoldDuration: invoices.DefaultHtlcHoldDuration, + Clock: clock.NewDefaultClock(), + AcceptKeySend: cfg.AcceptKeySend, + GcCanceledInvoicesOnStartup: cfg.GcCanceledInvoicesOnStartup, + KeysendHoldTime: cfg.KeysendHoldTime, } s := &server{