Skip to content

Commit 43579b0

Browse files
committed
lndclient: expose ListTransactions as part of LightningClient
1 parent a9033ba commit 43579b0

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

lndclient/lightning_client.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package lndclient
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/hex"
67
"errors"
@@ -9,13 +10,13 @@ import (
910
"time"
1011

1112
"github.com/btcsuite/btcd/chaincfg"
13+
"github.com/btcsuite/btcd/wire"
1214
"github.com/btcsuite/btcutil"
1315
"github.com/lightningnetwork/lnd/channeldb"
1416
"github.com/lightningnetwork/lnd/lnrpc"
1517
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
1618
"github.com/lightningnetwork/lnd/lntypes"
1719
"github.com/lightningnetwork/lnd/zpay32"
18-
1920
"google.golang.org/grpc"
2021
"google.golang.org/grpc/codes"
2122
"google.golang.org/grpc/status"
@@ -36,6 +37,10 @@ type LightningClient interface {
3637

3738
AddInvoice(ctx context.Context, in *invoicesrpc.AddInvoiceData) (
3839
lntypes.Hash, string, error)
40+
41+
// ListTransactions returns all known transactions of the backing lnd
42+
// node.
43+
ListTransactions(ctx context.Context) ([]*wire.MsgTx, error)
3944
}
4045

4146
// Info contains info about the connected lnd node.
@@ -348,3 +353,32 @@ func (s *lightningClient) AddInvoice(ctx context.Context,
348353

349354
return hash, resp.PaymentRequest, nil
350355
}
356+
357+
// ListTransactions returns all known transactions of the backing lnd node.
358+
func (s *lightningClient) ListTransactions(ctx context.Context) ([]*wire.MsgTx, error) {
359+
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
360+
defer cancel()
361+
362+
rpcCtx = s.adminMac.WithMacaroonAuth(rpcCtx)
363+
rpcIn := &lnrpc.GetTransactionsRequest{}
364+
resp, err := s.client.GetTransactions(rpcCtx, rpcIn)
365+
if err != nil {
366+
return nil, err
367+
}
368+
369+
txs := make([]*wire.MsgTx, 0, len(resp.Transactions))
370+
for _, respTx := range resp.Transactions {
371+
rawTx, err := hex.DecodeString(respTx.RawTxHex)
372+
if err != nil {
373+
return nil, err
374+
}
375+
376+
var tx wire.MsgTx
377+
if err := tx.Deserialize(bytes.NewReader(rawTx)); err != nil {
378+
return nil, err
379+
}
380+
txs = append(txs, &tx)
381+
}
382+
383+
return txs, nil
384+
}

test/lightning_client_mock.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/btcsuite/btcd/btcec"
10+
"github.com/btcsuite/btcd/wire"
1011
"github.com/btcsuite/btcutil"
1112
"github.com/lightninglabs/loop/lndclient"
1213
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
@@ -119,3 +120,13 @@ func (h *mockLightningClient) AddInvoice(ctx context.Context,
119120

120121
return hash, payReqString, nil
121122
}
123+
124+
// ListTransactions returns all known transactions of the backing lnd node.
125+
func (h *mockLightningClient) ListTransactions(
126+
ctx context.Context) ([]*wire.MsgTx, error) {
127+
128+
h.lnd.lock.Lock()
129+
txs := h.lnd.Transactions
130+
h.lnd.lock.Unlock()
131+
return txs, nil
132+
}

test/lnd_services_mock.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ type LndMockServices struct {
122122

123123
Height int32
124124

125+
Transactions []*wire.MsgTx
126+
125127
WaitForFinished func()
126128

127129
lock sync.Mutex
@@ -139,6 +141,13 @@ func (s *LndMockServices) NotifyHeight(height int32) error {
139141
return nil
140142
}
141143

144+
// AddRelevantTx marks the given transaction as relevant.
145+
func (s *LndMockServices) AddTx(tx *wire.MsgTx) {
146+
s.lock.Lock()
147+
s.Transactions = append(s.Transactions, tx.Copy())
148+
s.lock.Unlock()
149+
}
150+
142151
// IsDone checks whether all channels have been fully emptied. If not this may
143152
// indicate unexpected behaviour of the code under test.
144153
func (s *LndMockServices) IsDone() error {

test/walletkit_mock.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func (m *mockWalletKit) NextAddr(ctx context.Context) (btcutil.Address, error) {
6060
}
6161

6262
func (m *mockWalletKit) PublishTransaction(ctx context.Context, tx *wire.MsgTx) error {
63+
m.lnd.AddTx(tx)
6364
m.lnd.TxPublishChannel <- tx
6465
return nil
6566
}
@@ -84,6 +85,7 @@ func (m *mockWalletKit) SendOutputs(ctx context.Context, outputs []*wire.TxOut,
8485
})
8586
}
8687

88+
m.lnd.AddTx(&tx)
8789
m.lnd.SendOutputsChannel <- tx
8890

8991
return &tx, nil

0 commit comments

Comments
 (0)