Skip to content

Commit 8b1cdd4

Browse files
committed
test/test: add lookup invoice to mock lightning client
Track the invoices we create with AddInvoice so that we can realistically lookup and settle with the mock.
1 parent 87a0a0c commit 8b1cdd4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

test/lightning_client_mock.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/btcsuite/btcd/wire"
1212
"github.com/btcsuite/btcutil"
1313
"github.com/lightninglabs/loop/lndclient"
14+
"github.com/lightningnetwork/lnd/channeldb"
1415
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
1516
"github.com/lightningnetwork/lnd/lntypes"
1617
"github.com/lightningnetwork/lnd/zpay32"
@@ -125,9 +126,38 @@ func (h *mockLightningClient) AddInvoice(ctx context.Context,
125126
return lntypes.Hash{}, "", err
126127
}
127128

129+
// Add the invoice we have created to our mock's set of invoices.
130+
h.lnd.Invoices[hash] = &lndclient.Invoice{
131+
Preimage: nil,
132+
Hash: hash,
133+
PaymentRequest: payReqString,
134+
Amount: in.Value,
135+
CreationDate: creationDate,
136+
State: channeldb.ContractOpen,
137+
IsKeysend: false,
138+
}
139+
128140
return hash, payReqString, nil
129141
}
130142

143+
// LookupInvoice looks up an invoice in the mock's set of stored invoices.
144+
// If it is not found, this call will fail. Note that these invoices should
145+
// be settled using settleInvoice to have a preimage, settled state and settled
146+
// date set.
147+
func (h *mockLightningClient) LookupInvoice(_ context.Context,
148+
hash lntypes.Hash) (*lndclient.Invoice, error) {
149+
150+
h.lnd.lock.Lock()
151+
defer h.lnd.lock.Unlock()
152+
153+
inv, ok := h.lnd.Invoices[hash]
154+
if !ok {
155+
return nil, fmt.Errorf("invoice: %x not found", hash)
156+
}
157+
158+
return inv, nil
159+
}
160+
131161
// ListTransactions returns all known transactions of the backing lnd node.
132162
func (h *mockLightningClient) ListTransactions(
133163
ctx context.Context) ([]*wire.MsgTx, error) {

test/lnd_services_mock.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func NewMockLnd() *LndMockServices {
6969
NodePubkey: testNodePubkey,
7070
Signature: testSignature,
7171
SignatureMsg: testSignatureMsg,
72+
Invoices: make(map[lntypes.Hash]*lndclient.Invoice),
7273
}
7374

7475
lightningClient.lnd = &lnd
@@ -158,6 +159,10 @@ type LndMockServices struct {
158159

159160
Transactions []*wire.MsgTx
160161

162+
// Invoices is a set of invoices that have been created by the mock,
163+
// keyed by hash string.
164+
Invoices map[lntypes.Hash]*lndclient.Invoice
165+
161166
WaitForFinished func()
162167

163168
lock sync.Mutex

0 commit comments

Comments
 (0)