Skip to content

Commit 82eeadd

Browse files
committed
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
1 parent 86fc2cc commit 82eeadd

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

accounts/checkers_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,9 @@ func testSendPayment(t *testing.T, uri string) {
523523
errFunc := func(err error) {
524524
lndMock.mainErrChan <- err
525525
}
526-
service, err := NewService(t.TempDir(), errFunc)
526+
store, err := NewBoltStore(t.TempDir(), DBFilename)
527+
require.NoError(t, err)
528+
service, err := NewService(store, errFunc)
527529
require.NoError(t, err)
528530

529531
err = service.Start(ctx, lndMock, routerMock, chainParams)
@@ -719,7 +721,9 @@ func TestSendPaymentV2(t *testing.T) {
719721
errFunc := func(err error) {
720722
lndMock.mainErrChan <- err
721723
}
722-
service, err := NewService(t.TempDir(), errFunc)
724+
store, err := NewBoltStore(t.TempDir(), DBFilename)
725+
require.NoError(t, err)
726+
service, err := NewService(store, errFunc)
723727
require.NoError(t, err)
724728

725729
err = service.Start(ctx, lndMock, routerMock, chainParams)
@@ -906,7 +910,9 @@ func TestSendToRouteV2(t *testing.T) {
906910
errFunc := func(err error) {
907911
lndMock.mainErrChan <- err
908912
}
909-
service, err := NewService(t.TempDir(), errFunc)
913+
store, err := NewBoltStore(t.TempDir(), DBFilename)
914+
require.NoError(t, err)
915+
service, err := NewService(store, errFunc)
910916
require.NoError(t, err)
911917

912918
err = service.Start(ctx, lndMock, routerMock, chainParams)

accounts/service.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,11 @@ type InterceptorService struct {
7878

7979
// NewService returns a service backed by the macaroon Bolt DB stored in the
8080
// passed-in directory.
81-
func NewService(dir string,
82-
errCallback func(error)) (*InterceptorService, error) {
83-
84-
accountStore, err := NewBoltStore(dir, DBFilename)
85-
if err != nil {
86-
return nil, err
87-
}
81+
func NewService(store Store, errCallback func(error)) (*InterceptorService,
82+
error) {
8883

8984
return &InterceptorService{
90-
store: accountStore,
85+
store: store,
9186
invoiceToAccount: make(map[lntypes.Hash]AccountID),
9287
pendingPayments: make(map[lntypes.Hash]*trackedPayment),
9388
requestValuesStore: newRequestValuesStore(),
@@ -242,7 +237,7 @@ func (s *InterceptorService) Stop() error {
242237
close(s.quit)
243238
s.wg.Wait()
244239

245-
return s.store.Close()
240+
return nil
246241
}
247242

248243
// IsRunning checks if the account service is running, and returns a boolean

accounts/service_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func TestAccountService(t *testing.T) {
243243
// Start by closing the store. This should cause an
244244
// error once we make an invoice update, as the service
245245
// will fail when persisting the invoice update.
246-
s.store.Close()
246+
require.NoError(t, s.store.Close())
247247

248248
// Ensure that the service was started successfully and
249249
// still running though, despite the closing of the
@@ -833,7 +833,9 @@ func TestAccountService(t *testing.T) {
833833
errFunc := func(err error) {
834834
lndMock.mainErrChan <- err
835835
}
836-
service, err := NewService(t.TempDir(), errFunc)
836+
store, err := NewBoltStore(t.TempDir(), DBFilename)
837+
require.NoError(tt, err)
838+
service, err := NewService(store, errFunc)
837839
require.NoError(t, err)
838840

839841
// Is a setup call required to initialize initial

terminal.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ type LightningTerminal struct {
214214
middleware *mid.Manager
215215
middlewareStarted bool
216216

217+
accountsStore *accounts.BoltStore
217218
accountService *accounts.InterceptorService
218219
accountServiceStarted bool
219220

@@ -412,8 +413,15 @@ func (g *LightningTerminal) start(ctx context.Context) error {
412413
)
413414
}
414415

416+
g.accountsStore, err = accounts.NewBoltStore(
417+
filepath.Dir(g.cfg.MacaroonPath), accounts.DBFilename,
418+
)
419+
if err != nil {
420+
return fmt.Errorf("error creating accounts store: %w", err)
421+
}
422+
415423
g.accountService, err = accounts.NewService(
416-
filepath.Dir(g.cfg.MacaroonPath), accountServiceErrCallback,
424+
g.accountsStore, accountServiceErrCallback,
417425
)
418426
if err != nil {
419427
return fmt.Errorf("error creating account service: %v", err)
@@ -1421,6 +1429,14 @@ func (g *LightningTerminal) shutdownSubServers() error {
14211429
}
14221430
}
14231431

1432+
if g.accountsStore != nil {
1433+
err = g.accountsStore.Close()
1434+
if err != nil {
1435+
log.Errorf("Error closing accounts store: %v", err)
1436+
returnErr = err
1437+
}
1438+
}
1439+
14241440
if g.middlewareStarted {
14251441
g.middleware.Stop()
14261442
}

0 commit comments

Comments
 (0)