aperture+challenger: add error channel to challenger

To make sure we can capture errors in the challenger's invoice
subscription, we hand the main error channel to the challenger so it can
report back errors on it.
This commit is contained in:
Oliver Gugger
2020-09-23 10:07:42 +02:00
parent 5237b07a6e
commit e1269a7f86
3 changed files with 23 additions and 8 deletions

View File

@@ -110,7 +110,10 @@ func run() error {
Value: price, Value: price,
}, nil }, nil
} }
challenger, err := NewLndChallenger(cfg.Authenticator, genInvoiceReq) errChan := make(chan error)
challenger, err := NewLndChallenger(
cfg.Authenticator, genInvoiceReq, errChan,
)
if err != nil { if err != nil {
return err return err
} }
@@ -164,7 +167,6 @@ func run() error {
) )
log.Infof("Starting the server, listening on %s.", cfg.ListenAddr) log.Infof("Starting the server, listening on %s.", cfg.ListenAddr)
errChan := make(chan error)
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()

View File

@@ -49,6 +49,8 @@ type LndChallenger struct {
invoicesCancel func() invoicesCancel func()
invoicesCond *sync.Cond invoicesCond *sync.Cond
errChan chan<- error
quit chan struct{} quit chan struct{}
wg sync.WaitGroup wg sync.WaitGroup
} }
@@ -66,8 +68,8 @@ const (
// NewLndChallenger creates a new challenger that uses the given connection // NewLndChallenger creates a new challenger that uses the given connection
// details to connect to an lnd backend to create payment challenges. // details to connect to an lnd backend to create payment challenges.
func NewLndChallenger(cfg *authConfig, genInvoiceReq InvoiceRequestGenerator) ( func NewLndChallenger(cfg *authConfig, genInvoiceReq InvoiceRequestGenerator,
*LndChallenger, error) { errChan chan<- error) (*LndChallenger, error) {
if genInvoiceReq == nil { if genInvoiceReq == nil {
return nil, fmt.Errorf("genInvoiceReq cannot be nil") return nil, fmt.Errorf("genInvoiceReq cannot be nil")
@@ -89,6 +91,7 @@ func NewLndChallenger(cfg *authConfig, genInvoiceReq InvoiceRequestGenerator) (
invoicesMtx: invoicesMtx, invoicesMtx: invoicesMtx,
invoicesCond: sync.NewCond(invoicesMtx), invoicesCond: sync.NewCond(invoicesMtx),
quit: make(chan struct{}), quit: make(chan struct{}),
errChan: errChan,
}, nil }, nil
} }

View File

@@ -20,6 +20,7 @@ type invoiceStreamMock struct {
lnrpc.Lightning_SubscribeInvoicesClient lnrpc.Lightning_SubscribeInvoicesClient
updateChan chan *lnrpc.Invoice updateChan chan *lnrpc.Invoice
errChan chan error
quit chan struct{} quit chan struct{}
} }
@@ -28,6 +29,9 @@ func (i *invoiceStreamMock) Recv() (*lnrpc.Invoice, error) {
case msg := <-i.updateChan: case msg := <-i.updateChan:
return msg, nil return msg, nil
case err := <-i.errChan:
return nil, err
case <-i.quit: case <-i.quit:
return nil, context.Canceled return nil, context.Canceled
} }
@@ -36,6 +40,7 @@ func (i *invoiceStreamMock) Recv() (*lnrpc.Invoice, error) {
type mockInvoiceClient struct { type mockInvoiceClient struct {
invoices []*lnrpc.Invoice invoices []*lnrpc.Invoice
updateChan chan *lnrpc.Invoice updateChan chan *lnrpc.Invoice
errChan chan error
quit chan struct{} quit chan struct{}
lastAddIndex uint64 lastAddIndex uint64
@@ -60,6 +65,7 @@ func (m *mockInvoiceClient) SubscribeInvoices(_ context.Context,
return &invoiceStreamMock{ return &invoiceStreamMock{
updateChan: m.updateChan, updateChan: m.updateChan,
errChan: m.errChan,
quit: m.quit, quit: m.quit,
}, nil }, nil
} }
@@ -81,9 +87,10 @@ func (m *mockInvoiceClient) stop() {
close(m.quit) close(m.quit)
} }
func newChallenger() (*LndChallenger, *mockInvoiceClient) { func newChallenger() (*LndChallenger, *mockInvoiceClient, chan error) {
mockClient := &mockInvoiceClient{ mockClient := &mockInvoiceClient{
updateChan: make(chan *lnrpc.Invoice), updateChan: make(chan *lnrpc.Invoice),
errChan: make(chan error, 1),
quit: make(chan struct{}), quit: make(chan struct{}),
} }
genInvoiceReq := func(price int64) (*lnrpc.Invoice, error) { genInvoiceReq := func(price int64) (*lnrpc.Invoice, error) {
@@ -91,6 +98,7 @@ func newChallenger() (*LndChallenger, *mockInvoiceClient) {
nil nil
} }
invoicesMtx := &sync.Mutex{} invoicesMtx := &sync.Mutex{}
mainErrChan := make(chan error)
return &LndChallenger{ return &LndChallenger{
client: mockClient, client: mockClient,
genInvoiceReq: genInvoiceReq, genInvoiceReq: genInvoiceReq,
@@ -98,7 +106,8 @@ func newChallenger() (*LndChallenger, *mockInvoiceClient) {
quit: make(chan struct{}), quit: make(chan struct{}),
invoicesMtx: invoicesMtx, invoicesMtx: invoicesMtx,
invoicesCond: sync.NewCond(invoicesMtx), invoicesCond: sync.NewCond(invoicesMtx),
}, mockClient errChan: mainErrChan,
}, mockClient, mainErrChan
} }
func newInvoice(hash lntypes.Hash, addIndex uint64, func newInvoice(hash lntypes.Hash, addIndex uint64,
@@ -119,12 +128,13 @@ func TestLndChallenger(t *testing.T) {
// First of all, test that the NewLndChallenger doesn't allow a nil // First of all, test that the NewLndChallenger doesn't allow a nil
// invoice generator function. // invoice generator function.
_, err := NewLndChallenger(nil, nil) errChan := make(chan error)
_, err := NewLndChallenger(nil, nil, errChan)
require.Error(t, err) require.Error(t, err)
// Now mock the lnd backend and create a challenger instance that we can // Now mock the lnd backend and create a challenger instance that we can
// test. // test.
c, invoiceMock := newChallenger() c, invoiceMock, _ := newChallenger()
// Creating a new challenge should add an invoice to the lnd backend. // Creating a new challenge should add an invoice to the lnd backend.
req, hash, err := c.NewChallenge(1337) req, hash, err := c.NewChallenge(1337)