From 82eeadd819ad997e7bfcb0cf46a7af1fca2e42a3 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Fri, 27 Dec 2024 19:58:28 +0200 Subject: [PATCH] accounts: pass Store impl to NewService We want to be able to pass different DB implementations to NewService. In preparation for this, we make it implementation agnostic by letting it take a `Store` instead of constructing one itself. This this change, we also let LiT handle the closing of the accounts Store instead of the accounts service --- accounts/checkers_test.go | 12 +++++++++--- accounts/service.go | 13 ++++--------- accounts/service_test.go | 6 ++++-- terminal.go | 18 +++++++++++++++++- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/accounts/checkers_test.go b/accounts/checkers_test.go index 1398f4d1..d5c77246 100644 --- a/accounts/checkers_test.go +++ b/accounts/checkers_test.go @@ -523,7 +523,9 @@ func testSendPayment(t *testing.T, uri string) { errFunc := func(err error) { lndMock.mainErrChan <- err } - service, err := NewService(t.TempDir(), errFunc) + store, err := NewBoltStore(t.TempDir(), DBFilename) + require.NoError(t, err) + service, err := NewService(store, errFunc) require.NoError(t, err) err = service.Start(ctx, lndMock, routerMock, chainParams) @@ -719,7 +721,9 @@ func TestSendPaymentV2(t *testing.T) { errFunc := func(err error) { lndMock.mainErrChan <- err } - service, err := NewService(t.TempDir(), errFunc) + store, err := NewBoltStore(t.TempDir(), DBFilename) + require.NoError(t, err) + service, err := NewService(store, errFunc) require.NoError(t, err) err = service.Start(ctx, lndMock, routerMock, chainParams) @@ -906,7 +910,9 @@ func TestSendToRouteV2(t *testing.T) { errFunc := func(err error) { lndMock.mainErrChan <- err } - service, err := NewService(t.TempDir(), errFunc) + store, err := NewBoltStore(t.TempDir(), DBFilename) + require.NoError(t, err) + service, err := NewService(store, errFunc) require.NoError(t, err) err = service.Start(ctx, lndMock, routerMock, chainParams) diff --git a/accounts/service.go b/accounts/service.go index 42f4e08b..820dad23 100644 --- a/accounts/service.go +++ b/accounts/service.go @@ -78,16 +78,11 @@ type InterceptorService struct { // NewService returns a service backed by the macaroon Bolt DB stored in the // passed-in directory. -func NewService(dir string, - errCallback func(error)) (*InterceptorService, error) { - - accountStore, err := NewBoltStore(dir, DBFilename) - if err != nil { - return nil, err - } +func NewService(store Store, errCallback func(error)) (*InterceptorService, + error) { return &InterceptorService{ - store: accountStore, + store: store, invoiceToAccount: make(map[lntypes.Hash]AccountID), pendingPayments: make(map[lntypes.Hash]*trackedPayment), requestValuesStore: newRequestValuesStore(), @@ -242,7 +237,7 @@ func (s *InterceptorService) Stop() error { close(s.quit) s.wg.Wait() - return s.store.Close() + return nil } // IsRunning checks if the account service is running, and returns a boolean diff --git a/accounts/service_test.go b/accounts/service_test.go index 946a8f29..8834a3a0 100644 --- a/accounts/service_test.go +++ b/accounts/service_test.go @@ -243,7 +243,7 @@ func TestAccountService(t *testing.T) { // Start by closing the store. This should cause an // error once we make an invoice update, as the service // will fail when persisting the invoice update. - s.store.Close() + require.NoError(t, s.store.Close()) // Ensure that the service was started successfully and // still running though, despite the closing of the @@ -833,7 +833,9 @@ func TestAccountService(t *testing.T) { errFunc := func(err error) { lndMock.mainErrChan <- err } - service, err := NewService(t.TempDir(), errFunc) + store, err := NewBoltStore(t.TempDir(), DBFilename) + require.NoError(tt, err) + service, err := NewService(store, errFunc) require.NoError(t, err) // Is a setup call required to initialize initial diff --git a/terminal.go b/terminal.go index c92827ad..84a7f42a 100644 --- a/terminal.go +++ b/terminal.go @@ -214,6 +214,7 @@ type LightningTerminal struct { middleware *mid.Manager middlewareStarted bool + accountsStore *accounts.BoltStore accountService *accounts.InterceptorService accountServiceStarted bool @@ -412,8 +413,15 @@ func (g *LightningTerminal) start(ctx context.Context) error { ) } + g.accountsStore, err = accounts.NewBoltStore( + filepath.Dir(g.cfg.MacaroonPath), accounts.DBFilename, + ) + if err != nil { + return fmt.Errorf("error creating accounts store: %w", err) + } + g.accountService, err = accounts.NewService( - filepath.Dir(g.cfg.MacaroonPath), accountServiceErrCallback, + g.accountsStore, accountServiceErrCallback, ) if err != nil { return fmt.Errorf("error creating account service: %v", err) @@ -1421,6 +1429,14 @@ func (g *LightningTerminal) shutdownSubServers() error { } } + if g.accountsStore != nil { + err = g.accountsStore.Close() + if err != nil { + log.Errorf("Error closing accounts store: %v", err) + returnErr = err + } + } + if g.middlewareStarted { g.middleware.Stop() }