From 43579b017fb23718d644920e6d278bc5f32d1f61 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Mon, 9 Dec 2019 16:16:41 -0800 Subject: [PATCH] lndclient: expose ListTransactions as part of LightningClient --- lndclient/lightning_client.go | 36 ++++++++++++++++++++++++++++++++++- test/lightning_client_mock.go | 11 +++++++++++ test/lnd_services_mock.go | 9 +++++++++ test/walletkit_mock.go | 2 ++ 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/lndclient/lightning_client.go b/lndclient/lightning_client.go index c7e6356..d4bb13c 100644 --- a/lndclient/lightning_client.go +++ b/lndclient/lightning_client.go @@ -1,6 +1,7 @@ package lndclient import ( + "bytes" "context" "encoding/hex" "errors" @@ -9,13 +10,13 @@ import ( "time" "github.com/btcsuite/btcd/chaincfg" + "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/zpay32" - "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -36,6 +37,10 @@ type LightningClient interface { AddInvoice(ctx context.Context, in *invoicesrpc.AddInvoiceData) ( lntypes.Hash, string, error) + + // ListTransactions returns all known transactions of the backing lnd + // node. + ListTransactions(ctx context.Context) ([]*wire.MsgTx, error) } // Info contains info about the connected lnd node. @@ -348,3 +353,32 @@ func (s *lightningClient) AddInvoice(ctx context.Context, return hash, resp.PaymentRequest, nil } + +// ListTransactions returns all known transactions of the backing lnd node. +func (s *lightningClient) ListTransactions(ctx context.Context) ([]*wire.MsgTx, error) { + rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout) + defer cancel() + + rpcCtx = s.adminMac.WithMacaroonAuth(rpcCtx) + rpcIn := &lnrpc.GetTransactionsRequest{} + resp, err := s.client.GetTransactions(rpcCtx, rpcIn) + if err != nil { + return nil, err + } + + txs := make([]*wire.MsgTx, 0, len(resp.Transactions)) + for _, respTx := range resp.Transactions { + rawTx, err := hex.DecodeString(respTx.RawTxHex) + if err != nil { + return nil, err + } + + var tx wire.MsgTx + if err := tx.Deserialize(bytes.NewReader(rawTx)); err != nil { + return nil, err + } + txs = append(txs, &tx) + } + + return txs, nil +} diff --git a/test/lightning_client_mock.go b/test/lightning_client_mock.go index 07bf19e..694359d 100644 --- a/test/lightning_client_mock.go +++ b/test/lightning_client_mock.go @@ -7,6 +7,7 @@ import ( "time" "github.com/btcsuite/btcd/btcec" + "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" "github.com/lightninglabs/loop/lndclient" "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" @@ -119,3 +120,13 @@ func (h *mockLightningClient) AddInvoice(ctx context.Context, return hash, payReqString, nil } + +// ListTransactions returns all known transactions of the backing lnd node. +func (h *mockLightningClient) ListTransactions( + ctx context.Context) ([]*wire.MsgTx, error) { + + h.lnd.lock.Lock() + txs := h.lnd.Transactions + h.lnd.lock.Unlock() + return txs, nil +} diff --git a/test/lnd_services_mock.go b/test/lnd_services_mock.go index bd6ff70..4e902f2 100644 --- a/test/lnd_services_mock.go +++ b/test/lnd_services_mock.go @@ -122,6 +122,8 @@ type LndMockServices struct { Height int32 + Transactions []*wire.MsgTx + WaitForFinished func() lock sync.Mutex @@ -139,6 +141,13 @@ func (s *LndMockServices) NotifyHeight(height int32) error { return nil } +// AddRelevantTx marks the given transaction as relevant. +func (s *LndMockServices) AddTx(tx *wire.MsgTx) { + s.lock.Lock() + s.Transactions = append(s.Transactions, tx.Copy()) + s.lock.Unlock() +} + // IsDone checks whether all channels have been fully emptied. If not this may // indicate unexpected behaviour of the code under test. func (s *LndMockServices) IsDone() error { diff --git a/test/walletkit_mock.go b/test/walletkit_mock.go index 6a557ad..5581fdf 100644 --- a/test/walletkit_mock.go +++ b/test/walletkit_mock.go @@ -60,6 +60,7 @@ func (m *mockWalletKit) NextAddr(ctx context.Context) (btcutil.Address, error) { } func (m *mockWalletKit) PublishTransaction(ctx context.Context, tx *wire.MsgTx) error { + m.lnd.AddTx(tx) m.lnd.TxPublishChannel <- tx return nil } @@ -84,6 +85,7 @@ func (m *mockWalletKit) SendOutputs(ctx context.Context, outputs []*wire.TxOut, }) } + m.lnd.AddTx(&tx) m.lnd.SendOutputsChannel <- tx return &tx, nil