lndclient: expose ListTransactions as part of LightningClient

pull/125/head
Wilmer Paulino 4 years ago
parent a9033bab2a
commit 43579b017f
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F

@ -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
}

@ -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
}

@ -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 {

@ -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

Loading…
Cancel
Save