Skip to content

Commit 1a30bdb

Browse files
committed
accounts: thread context through to accounts
1 parent 83a3209 commit 1a30bdb

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

accounts/checkers_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,8 @@ func TestSendPaymentCalls(t *testing.T) {
499499

500500
func testSendPayment(t *testing.T, uri string) {
501501
var (
502-
parentCtx = context.Background()
503-
zeroFee = &lnrpc.FeeLimit{Limit: &lnrpc.FeeLimit_Fixed{
502+
ctx = context.Background()
503+
zeroFee = &lnrpc.FeeLimit{Limit: &lnrpc.FeeLimit_Fixed{
504504
Fixed: 0,
505505
}}
506506
requestID uint64
@@ -520,7 +520,7 @@ func testSendPayment(t *testing.T, uri string) {
520520
service, err := NewService(t.TempDir(), errFunc)
521521
require.NoError(t, err)
522522

523-
err = service.Start(lndMock, routerMock, chainParams)
523+
err = service.Start(ctx, lndMock, routerMock, chainParams)
524524
require.NoError(t, err)
525525

526526
assertBalance := func(id AccountID, expectedBalance int64) {
@@ -533,7 +533,7 @@ func testSendPayment(t *testing.T, uri string) {
533533

534534
// This should error because there is no account in the context.
535535
err = service.checkers.checkIncomingRequest(
536-
parentCtx, uri, &lnrpc.SendRequest{},
536+
ctx, uri, &lnrpc.SendRequest{},
537537
)
538538
require.ErrorContains(t, err, "no account found in context")
539539

@@ -543,7 +543,7 @@ func testSendPayment(t *testing.T, uri string) {
543543
)
544544
require.NoError(t, err)
545545

546-
ctxWithAcct := AddAccountToContext(parentCtx, acct)
546+
ctxWithAcct := AddAccountToContext(ctx, acct)
547547

548548
// This should error because there is no request ID in the context.
549549
err = service.checkers.checkIncomingRequest(
@@ -552,7 +552,7 @@ func testSendPayment(t *testing.T, uri string) {
552552
require.ErrorContains(t, err, "no request ID found in context")
553553

554554
reqID1 := nextRequestID()
555-
ctx := AddRequestIDToContext(ctxWithAcct, reqID1)
555+
ctx = AddRequestIDToContext(ctxWithAcct, reqID1)
556556

557557
// This should error because no payment hash is provided.
558558
err = service.checkers.checkIncomingRequest(
@@ -698,7 +698,7 @@ func testSendPayment(t *testing.T, uri string) {
698698
func TestSendPaymentV2(t *testing.T) {
699699
var (
700700
uri = "/routerrpc.Router/SendPaymentV2"
701-
parentCtx = context.Background()
701+
ctx = context.Background()
702702
requestID uint64
703703
)
704704

@@ -716,7 +716,7 @@ func TestSendPaymentV2(t *testing.T) {
716716
service, err := NewService(t.TempDir(), errFunc)
717717
require.NoError(t, err)
718718

719-
err = service.Start(lndMock, routerMock, chainParams)
719+
err = service.Start(ctx, lndMock, routerMock, chainParams)
720720
require.NoError(t, err)
721721

722722
assertBalance := func(id AccountID, expectedBalance int64) {
@@ -729,7 +729,7 @@ func TestSendPaymentV2(t *testing.T) {
729729

730730
// This should error because there is no account in the context.
731731
err = service.checkers.checkIncomingRequest(
732-
parentCtx, uri, &routerrpc.SendPaymentRequest{},
732+
ctx, uri, &routerrpc.SendPaymentRequest{},
733733
)
734734
require.ErrorContains(t, err, "no account found in context")
735735

@@ -739,7 +739,7 @@ func TestSendPaymentV2(t *testing.T) {
739739
)
740740
require.NoError(t, err)
741741

742-
ctxWithAcct := AddAccountToContext(parentCtx, acct)
742+
ctxWithAcct := AddAccountToContext(ctx, acct)
743743

744744
// This should error because there is no request ID in the context.
745745
err = service.checkers.checkIncomingRequest(
@@ -748,7 +748,7 @@ func TestSendPaymentV2(t *testing.T) {
748748
require.ErrorContains(t, err, "no request ID found in context")
749749

750750
reqID1 := nextRequestID()
751-
ctx := AddRequestIDToContext(ctxWithAcct, reqID1)
751+
ctx = AddRequestIDToContext(ctxWithAcct, reqID1)
752752

753753
// This should error because no payment hash is provided.
754754
err = service.checkers.checkIncomingRequest(
@@ -885,7 +885,7 @@ func TestSendPaymentV2(t *testing.T) {
885885
func TestSendToRouteV2(t *testing.T) {
886886
var (
887887
uri = "/routerrpc.Router/SendToRouteV2"
888-
parentCtx = context.Background()
888+
ctx = context.Background()
889889
requestID uint64
890890
)
891891

@@ -903,7 +903,7 @@ func TestSendToRouteV2(t *testing.T) {
903903
service, err := NewService(t.TempDir(), errFunc)
904904
require.NoError(t, err)
905905

906-
err = service.Start(lndMock, routerMock, chainParams)
906+
err = service.Start(ctx, lndMock, routerMock, chainParams)
907907
require.NoError(t, err)
908908

909909
assertBalance := func(id AccountID, expectedBalance int64) {
@@ -916,7 +916,7 @@ func TestSendToRouteV2(t *testing.T) {
916916

917917
// This should error because there is no account in the context.
918918
err = service.checkers.checkIncomingRequest(
919-
parentCtx, uri, &routerrpc.SendToRouteRequest{},
919+
ctx, uri, &routerrpc.SendToRouteRequest{},
920920
)
921921
require.ErrorContains(t, err, "no account found in context")
922922

@@ -926,7 +926,7 @@ func TestSendToRouteV2(t *testing.T) {
926926
)
927927
require.NoError(t, err)
928928

929-
ctxWithAcct := AddAccountToContext(parentCtx, acct)
929+
ctxWithAcct := AddAccountToContext(ctx, acct)
930930

931931
// This should error because there is no request ID in the context.
932932
err = service.checkers.checkIncomingRequest(
@@ -935,7 +935,7 @@ func TestSendToRouteV2(t *testing.T) {
935935
require.ErrorContains(t, err, "no request ID found in context")
936936

937937
reqID1 := nextRequestID()
938-
ctx := AddRequestIDToContext(ctxWithAcct, reqID1)
938+
ctx = AddRequestIDToContext(ctxWithAcct, reqID1)
939939

940940
// This should error because no payment hash is provided.
941941
err = service.checkers.checkIncomingRequest(

accounts/service.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/btcsuite/btcd/chaincfg"
1111
"github.com/lightninglabs/lndclient"
12+
"github.com/lightninglabs/taproot-assets/fn"
1213
"github.com/lightningnetwork/lnd/channeldb"
1314
invpkg "github.com/lightningnetwork/lnd/invoices"
1415
"github.com/lightningnetwork/lnd/lnrpc"
@@ -55,7 +56,7 @@ type InterceptorService struct {
5556
routerClient lndclient.RouterClient
5657

5758
mainCtx context.Context
58-
contextCancel context.CancelFunc
59+
contextCancel fn.Option[context.CancelFunc]
5960

6061
requestMtx sync.Mutex
6162
checkers *AccountChecker
@@ -85,12 +86,8 @@ func NewService(dir string,
8586
return nil, err
8687
}
8788

88-
mainCtx, contextCancel := context.WithCancel(context.Background())
89-
9089
return &InterceptorService{
9190
store: accountStore,
92-
mainCtx: mainCtx,
93-
contextCancel: contextCancel,
9491
invoiceToAccount: make(map[lntypes.Hash]AccountID),
9592
pendingPayments: make(map[lntypes.Hash]*trackedPayment),
9693
requestValuesStore: newRequestValuesStore(),
@@ -101,9 +98,14 @@ func NewService(dir string,
10198
}
10299

103100
// Start starts the account service and its interceptor capability.
104-
func (s *InterceptorService) Start(lightningClient lndclient.LightningClient,
101+
func (s *InterceptorService) Start(ctx context.Context,
102+
lightningClient lndclient.LightningClient,
105103
routerClient lndclient.RouterClient, params *chaincfg.Params) error {
106104

105+
mainCtx, contextCancel := context.WithCancel(ctx)
106+
s.mainCtx = mainCtx
107+
s.contextCancel = fn.Some(contextCancel)
108+
107109
s.routerClient = routerClient
108110
s.checkers = NewAccountChecker(s, params)
109111

@@ -180,7 +182,7 @@ func (s *InterceptorService) Start(lightningClient lndclient.LightningClient,
180182
s.wg.Add(1)
181183
go func() {
182184
defer s.wg.Done()
183-
defer s.contextCancel()
185+
defer contextCancel()
184186

185187
for {
186188
select {
@@ -235,9 +237,8 @@ func (s *InterceptorService) Stop() error {
235237
s.requestMtx.Lock()
236238
defer s.requestMtx.Unlock()
237239

238-
s.contextCancel()
240+
s.contextCancel.WhenSome(func(fn context.CancelFunc) { fn() })
239241
close(s.quit)
240-
241242
s.wg.Wait()
242243

243244
return s.store.Close()

accounts/service_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,10 @@ func TestAccountService(t *testing.T) {
838838
}
839839

840840
// Any errors during startup expected?
841-
err = service.Start(lndMock, routerMock, chainParams)
841+
err = service.Start(
842+
context.Background(), lndMock, routerMock,
843+
chainParams,
844+
)
842845
if tc.startupErr != "" {
843846
require.ErrorContains(tt, err, tc.startupErr)
844847

terminal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ func (g *LightningTerminal) startInternalSubServers(ctx context.Context,
10421042
log.Infof("Starting LiT account service")
10431043
if !g.cfg.Accounts.Disable {
10441044
err = g.accountService.Start(
1045-
g.lndClient.Client, g.lndClient.Router,
1045+
ctx, g.lndClient.Client, g.lndClient.Router,
10461046
g.lndClient.ChainParams,
10471047
)
10481048
if err != nil {

0 commit comments

Comments
 (0)