Skip to content

Commit a04f3b1

Browse files
committed
challenger: custom context for LightningClient methods
Whenever we use the LightningClient from an LNC connection we need to add the macaroon to the headers.
1 parent 3d53f20 commit a04f3b1

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

aperture.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func (a *Aperture) Start(errChan chan error) error {
306306
}
307307

308308
a.challenger, err = NewLndChallenger(
309-
client, genInvoiceReq, errChan,
309+
client, genInvoiceReq, context.Background, errChan,
310310
)
311311
if err != nil {
312312
return err

challenger.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type InvoiceClient interface {
4141
// payment challenges.
4242
type LndChallenger struct {
4343
client InvoiceClient
44+
clientCtx func() context.Context
4445
genInvoiceReq InvoiceRequestGenerator
4546

4647
invoiceStates map[lntypes.Hash]lnrpc.Invoice_InvoiceState
@@ -69,15 +70,23 @@ const (
6970
// an lnd backend to create payment challenges.
7071
func NewLndChallenger(client InvoiceClient,
7172
genInvoiceReq InvoiceRequestGenerator,
73+
ctxFunc func() context.Context,
7274
errChan chan<- error) (*LndChallenger, error) {
7375

76+
// Make sure we have a valid context function. This will be called to
77+
// create a new context for each call to the lnd client.
78+
if ctxFunc == nil {
79+
ctxFunc = context.Background
80+
}
81+
7482
if genInvoiceReq == nil {
7583
return nil, fmt.Errorf("genInvoiceReq cannot be nil")
7684
}
7785

7886
invoicesMtx := &sync.Mutex{}
7987
return &LndChallenger{
8088
client: client,
89+
clientCtx: ctxFunc,
8190
genInvoiceReq: genInvoiceReq,
8291
invoiceStates: make(map[lntypes.Hash]lnrpc.Invoice_InvoiceState),
8392
invoicesMtx: invoicesMtx,
@@ -103,8 +112,9 @@ func (l *LndChallenger) Start() error {
103112
// cache. We need to keep track of all invoices, even quite old ones to
104113
// make sure tokens are valid. But to save space we only keep track of
105114
// an invoice's state.
115+
ctx := l.clientCtx()
106116
invoiceResp, err := l.client.ListInvoices(
107-
context.Background(), &lnrpc.ListInvoiceRequest{
117+
ctx, &lnrpc.ListInvoiceRequest{
108118
NumMaxInvoices: math.MaxUint64,
109119
},
110120
)
@@ -137,7 +147,7 @@ func (l *LndChallenger) Start() error {
137147
l.invoicesMtx.Unlock()
138148

139149
// We need to be able to cancel any subscription we make.
140-
ctxc, cancel := context.WithCancel(context.Background())
150+
ctxc, cancel := context.WithCancel(l.clientCtx())
141151
l.invoicesCancel = cancel
142152

143153
subscriptionResp, err := l.client.SubscribeInvoices(
@@ -261,12 +271,14 @@ func (l *LndChallenger) NewChallenge(price int64) (string, lntypes.Hash, error)
261271
log.Errorf("Error generating invoice request: %v", err)
262272
return "", lntypes.ZeroHash, err
263273
}
264-
ctx := context.Background()
274+
275+
ctx := l.clientCtx()
265276
response, err := l.client.AddInvoice(ctx, invoice)
266277
if err != nil {
267278
log.Errorf("Error adding invoice: %v", err)
268279
return "", lntypes.ZeroHash, err
269280
}
281+
270282
paymentHash, err := lntypes.MakeHash(response.RHash)
271283
if err != nil {
272284
log.Errorf("Error parsing payment hash: %v", err)

challenger_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func newChallenger() (*LndChallenger, *mockInvoiceClient, chan error) {
102102
mainErrChan := make(chan error)
103103
return &LndChallenger{
104104
client: mockClient,
105+
clientCtx: context.Background,
105106
genInvoiceReq: genInvoiceReq,
106107
invoiceStates: make(map[lntypes.Hash]lnrpc.Invoice_InvoiceState),
107108
quit: make(chan struct{}),
@@ -130,7 +131,7 @@ func TestLndChallenger(t *testing.T) {
130131
// First of all, test that the NewLndChallenger doesn't allow a nil
131132
// invoice generator function.
132133
errChan := make(chan error)
133-
_, err := NewLndChallenger(nil, nil, errChan)
134+
_, err := NewLndChallenger(nil, nil, nil, errChan)
134135
require.Error(t, err)
135136

136137
// Now mock the lnd backend and create a challenger instance that we can

0 commit comments

Comments
 (0)