|
| 1 | +package challenger |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/lightningnetwork/lnd/lnrpc" |
| 9 | + "github.com/lightningnetwork/lnd/lntypes" |
| 10 | + "google.golang.org/grpc" |
| 11 | +) |
| 12 | + |
| 13 | +type invoiceStreamMock struct { |
| 14 | + lnrpc.Lightning_SubscribeInvoicesClient |
| 15 | + |
| 16 | + updateChan chan *lnrpc.Invoice |
| 17 | + errChan chan error |
| 18 | + quit chan struct{} |
| 19 | +} |
| 20 | + |
| 21 | +func (i *invoiceStreamMock) Recv() (*lnrpc.Invoice, error) { |
| 22 | + select { |
| 23 | + case msg := <-i.updateChan: |
| 24 | + return msg, nil |
| 25 | + |
| 26 | + case err := <-i.errChan: |
| 27 | + return nil, err |
| 28 | + |
| 29 | + case <-i.quit: |
| 30 | + return nil, context.Canceled |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +type MockInvoiceClient struct { |
| 35 | + invoices []*lnrpc.Invoice |
| 36 | + updateChan chan *lnrpc.Invoice |
| 37 | + errChan chan error |
| 38 | + quit chan struct{} |
| 39 | + |
| 40 | + lastAddIndex uint64 |
| 41 | +} |
| 42 | + |
| 43 | +// ListInvoices returns a paginated list of all invoices known to lnd. |
| 44 | +func (m *MockInvoiceClient) ListInvoices(_ context.Context, |
| 45 | + _ *lnrpc.ListInvoiceRequest, |
| 46 | + _ ...grpc.CallOption) (*lnrpc.ListInvoiceResponse, error) { |
| 47 | + |
| 48 | + return &lnrpc.ListInvoiceResponse{ |
| 49 | + Invoices: m.invoices, |
| 50 | + }, nil |
| 51 | +} |
| 52 | + |
| 53 | +// SubscribeInvoices subscribes to updates on invoices. |
| 54 | +func (m *MockInvoiceClient) SubscribeInvoices(_ context.Context, |
| 55 | + in *lnrpc.InvoiceSubscription, _ ...grpc.CallOption) ( |
| 56 | + lnrpc.Lightning_SubscribeInvoicesClient, error) { |
| 57 | + |
| 58 | + m.lastAddIndex = in.AddIndex |
| 59 | + |
| 60 | + return &invoiceStreamMock{ |
| 61 | + updateChan: m.updateChan, |
| 62 | + errChan: m.errChan, |
| 63 | + quit: m.quit, |
| 64 | + }, nil |
| 65 | +} |
| 66 | + |
| 67 | +// AddInvoice adds a new invoice to lnd. |
| 68 | +func (m *MockInvoiceClient) AddInvoice(_ context.Context, in *lnrpc.Invoice, |
| 69 | + _ ...grpc.CallOption) (*lnrpc.AddInvoiceResponse, error) { |
| 70 | + |
| 71 | + m.invoices = append(m.invoices, in) |
| 72 | + |
| 73 | + return &lnrpc.AddInvoiceResponse{ |
| 74 | + RHash: in.RHash, |
| 75 | + PaymentRequest: in.PaymentRequest, |
| 76 | + AddIndex: uint64(len(m.invoices) - 1), |
| 77 | + }, nil |
| 78 | +} |
| 79 | + |
| 80 | +func (m *MockInvoiceClient) stop() { |
| 81 | + close(m.quit) |
| 82 | +} |
| 83 | + |
| 84 | +func NewChallenger() (*LndChallenger, *MockInvoiceClient, chan error) { |
| 85 | + mockClient := &MockInvoiceClient{ |
| 86 | + updateChan: make(chan *lnrpc.Invoice), |
| 87 | + errChan: make(chan error, 1), |
| 88 | + quit: make(chan struct{}), |
| 89 | + } |
| 90 | + genInvoiceReq := func(price int64) (*lnrpc.Invoice, error) { |
| 91 | + return newInvoice(lntypes.ZeroHash, 99, lnrpc.Invoice_OPEN), |
| 92 | + nil |
| 93 | + } |
| 94 | + invoicesMtx := &sync.Mutex{} |
| 95 | + mainErrChan := make(chan error) |
| 96 | + return &LndChallenger{ |
| 97 | + client: mockClient, |
| 98 | + genInvoiceReq: genInvoiceReq, |
| 99 | + invoiceStates: make(map[lntypes.Hash]lnrpc.Invoice_InvoiceState), |
| 100 | + quit: make(chan struct{}), |
| 101 | + invoicesMtx: invoicesMtx, |
| 102 | + invoicesCond: sync.NewCond(invoicesMtx), |
| 103 | + errChan: mainErrChan, |
| 104 | + }, mockClient, mainErrChan |
| 105 | +} |
| 106 | + |
| 107 | +func newInvoice(hash lntypes.Hash, addIndex uint64, |
| 108 | + state lnrpc.Invoice_InvoiceState) *lnrpc.Invoice { |
| 109 | + |
| 110 | + return &lnrpc.Invoice{ |
| 111 | + PaymentRequest: "foo", |
| 112 | + RHash: hash[:], |
| 113 | + AddIndex: addIndex, |
| 114 | + State: state, |
| 115 | + CreationDate: time.Now().Unix(), |
| 116 | + Expiry: 10, |
| 117 | + } |
| 118 | +} |
0 commit comments