From 499f2b16cf36bd591e893927c5f28e702f614613 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Wed, 4 Dec 2019 18:47:53 +0100 Subject: [PATCH] invoices: add RegistryConfig struct --- htlcswitch/mock.go | 9 ++++++--- invoices/invoiceregistry.go | 26 ++++++++++++++++---------- invoices/invoiceregistry_test.go | 15 ++++++++++++--- server.go | 8 +++++--- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index c63aa171..d3d61ca9 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -790,9 +790,12 @@ func newMockRegistry(minDelta uint32) *mockInvoiceRegistry { panic(err) } - finalCltvRejectDelta := int32(5) - - registry := invoices.NewRegistry(cdb, finalCltvRejectDelta) + registry := invoices.NewRegistry( + cdb, + &invoices.RegistryConfig{ + FinalCltvRejectDelta: 5, + }, + ) registry.Start() return &mockInvoiceRegistry{ diff --git a/invoices/invoiceregistry.go b/invoices/invoiceregistry.go index e67ecd80..e54f0952 100644 --- a/invoices/invoiceregistry.go +++ b/invoices/invoiceregistry.go @@ -41,6 +41,16 @@ type HodlEvent struct { AcceptHeight int32 } +// RegistryConfig contains the configuration parameters for invoice registry. +type RegistryConfig struct { + // FinalCltvRejectDelta defines the number of blocks before the expiry + // of the htlc where we no longer settle it as an exit hop and instead + // cancel it back. Normally this value should be lower than the cltv + // expiry of any invoice we create and the code effectuating this should + // not be hit. + FinalCltvRejectDelta int32 +} + // InvoiceRegistry is a central registry of all the outstanding invoices // created by the daemon. The registry is a thin wrapper around a map in order // to ensure that all updates/reads are thread safe. @@ -49,6 +59,9 @@ type InvoiceRegistry struct { cdb *channeldb.DB + // cfg contains the registry's configuration parameters. + cfg *RegistryConfig + clientMtx sync.Mutex nextClientID uint32 notificationClients map[uint32]*InvoiceSubscription @@ -69,13 +82,6 @@ type InvoiceRegistry struct { // subscriber. This is used to unsubscribe from all hashes efficiently. hodlReverseSubscriptions map[chan<- interface{}]map[channeldb.CircuitKey]struct{} - // finalCltvRejectDelta defines the number of blocks before the expiry - // of the htlc where we no longer settle it as an exit hop and instead - // cancel it back. Normally this value should be lower than the cltv - // expiry of any invoice we create and the code effectuating this should - // not be hit. - finalCltvRejectDelta int32 - wg sync.WaitGroup quit chan struct{} } @@ -84,7 +90,7 @@ type InvoiceRegistry struct { // wraps the persistent on-disk invoice storage with an additional in-memory // layer. The in-memory layer is in place such that debug invoices can be added // which are volatile yet available system wide within the daemon. -func NewRegistry(cdb *channeldb.DB, finalCltvRejectDelta int32) *InvoiceRegistry { +func NewRegistry(cdb *channeldb.DB, cfg *RegistryConfig) *InvoiceRegistry { return &InvoiceRegistry{ cdb: cdb, @@ -95,7 +101,7 @@ func NewRegistry(cdb *channeldb.DB, finalCltvRejectDelta int32) *InvoiceRegistry invoiceEvents: make(chan interface{}, 100), hodlSubscriptions: make(map[channeldb.CircuitKey]map[chan<- interface{}]struct{}), hodlReverseSubscriptions: make(map[chan<- interface{}]map[channeldb.CircuitKey]struct{}), - finalCltvRejectDelta: finalCltvRejectDelta, + cfg: cfg, quit: make(chan struct{}), } } @@ -442,7 +448,7 @@ func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash, amtPaid: amtPaid, expiry: expiry, currentHeight: currentHeight, - finalCltvRejectDelta: i.finalCltvRejectDelta, + finalCltvRejectDelta: i.cfg.FinalCltvRejectDelta, customRecords: payload.CustomRecords(), } diff --git a/invoices/invoiceregistry_test.go b/invoices/invoiceregistry_test.go index b5cf30ca..1ae46f63 100644 --- a/invoices/invoiceregistry_test.go +++ b/invoices/invoiceregistry_test.go @@ -71,7 +71,10 @@ func newTestContext(t *testing.T) *testContext { } // Instantiate and start the invoice ctx.registry. - registry := NewRegistry(cdb, testFinalCltvRejectDelta) + cfg := RegistryConfig{ + FinalCltvRejectDelta: testFinalCltvRejectDelta, + } + registry := NewRegistry(cdb, &cfg) err = registry.Start() if err != nil { @@ -390,7 +393,10 @@ func TestSettleHoldInvoice(t *testing.T) { defer cleanup() // Instantiate and start the invoice ctx.registry. - registry := NewRegistry(cdb, testFinalCltvRejectDelta) + cfg := RegistryConfig{ + FinalCltvRejectDelta: testFinalCltvRejectDelta, + } + registry := NewRegistry(cdb, &cfg) err = registry.Start() if err != nil { @@ -558,7 +564,10 @@ func TestCancelHoldInvoice(t *testing.T) { defer cleanup() // Instantiate and start the invoice ctx.registry. - registry := NewRegistry(cdb, testFinalCltvRejectDelta) + cfg := RegistryConfig{ + FinalCltvRejectDelta: testFinalCltvRejectDelta, + } + registry := NewRegistry(cdb, &cfg) err = registry.Start() if err != nil { diff --git a/server.go b/server.go index bb9493f8..1a767910 100644 --- a/server.go +++ b/server.go @@ -378,6 +378,10 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, return nil, err } + registryConfig := invoices.RegistryConfig{ + FinalCltvRejectDelta: defaultFinalCltvRejectDelta, + } + s := &server{ chanDB: chanDB, cc: cc, @@ -386,9 +390,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, readPool: readPool, chansToRestore: chansToRestore, - invoices: invoices.NewRegistry( - chanDB, defaultFinalCltvRejectDelta, - ), + invoices: invoices.NewRegistry(chanDB, ®istryConfig), channelNotifier: channelnotifier.New(chanDB),