From 4d3771430a6de550c0f2781298d13f362682cab1 Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Mon, 18 May 2020 16:23:02 +0200 Subject: [PATCH] lndclient: add ListChannels to LightningClient --- lndclient/lightning_client.go | 62 +++++++++++++++++++++++++++++++++++ test/lightning_client_mock.go | 7 ++++ test/lnd_services_mock.go | 2 ++ 3 files changed, 71 insertions(+) diff --git a/lndclient/lightning_client.go b/lndclient/lightning_client.go index 57827c2..b22f4cc 100644 --- a/lndclient/lightning_client.go +++ b/lndclient/lightning_client.go @@ -17,6 +17,7 @@ import ( "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lnwire" + "github.com/lightningnetwork/lnd/routing/route" "github.com/lightningnetwork/lnd/zpay32" "google.golang.org/grpc" "google.golang.org/grpc/codes" @@ -46,6 +47,9 @@ type LightningClient interface { // node. ListTransactions(ctx context.Context) ([]*wire.MsgTx, error) + // ListChannels retrieves all channels of the backing lnd node. + ListChannels(ctx context.Context) ([]ChannelInfo, error) + // ChannelBackup retrieves the backup for a particular channel. The // backup is returned as an encrypted chanbackup.Single payload. ChannelBackup(context.Context, wire.OutPoint) ([]byte, error) @@ -65,6 +69,29 @@ type Info struct { Uris []string } +// ChannelInfo stores unpacked per-channel info. +type ChannelInfo struct { + // Active indecates whether the channel is active. + Active bool + + // ChannelID holds the unique channel ID for the channel. The first 3 bytes + // are the block height, the next 3 the index within the block, and the last + // 2 bytes are the /output index for the channel. + ChannelID uint64 + + // PubKeyBytes is the raw bytes of the public key of the remote node. + PubKeyBytes route.Vertex + + // Capacity is the total amount of funds held in this channel. + Capacity btcutil.Amount + + // LocalBalance is the current balance of this node in this channel. + LocalBalance btcutil.Amount + + // RemoteBalance is the counterparty's current balance in this channel. + RemoteBalance btcutil.Amount +} + var ( // ErrMalformedServerResponse is returned when the swap and/or prepay // invoice is malformed. @@ -495,6 +522,41 @@ func (s *lightningClient) ListTransactions(ctx context.Context) ([]*wire.MsgTx, return txs, nil } +// ListChannels retrieves all channels of the backing lnd node. +func (s *lightningClient) ListChannels(ctx context.Context) ( + []ChannelInfo, error) { + + rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout) + defer cancel() + + response, err := s.client.ListChannels( + s.adminMac.WithMacaroonAuth(rpcCtx), + &lnrpc.ListChannelsRequest{}, + ) + if err != nil { + return nil, err + } + + result := make([]ChannelInfo, len(response.Channels)) + for i, channel := range response.Channels { + remoteVertex, err := route.NewVertexFromStr(channel.RemotePubkey) + if err != nil { + return nil, err + } + + result[i] = ChannelInfo{ + Active: channel.Active, + ChannelID: channel.ChanId, + PubKeyBytes: remoteVertex, + Capacity: btcutil.Amount(channel.Capacity), + LocalBalance: btcutil.Amount(channel.LocalBalance), + RemoteBalance: btcutil.Amount(channel.RemoteBalance), + } + } + + return result, nil +} + // ChannelBackup retrieves the backup for a particular channel. The backup is // returned as an encrypted chanbackup.Single payload. func (s *lightningClient) ChannelBackup(ctx context.Context, diff --git a/test/lightning_client_mock.go b/test/lightning_client_mock.go index 84d6725..2bac092 100644 --- a/test/lightning_client_mock.go +++ b/test/lightning_client_mock.go @@ -168,6 +168,13 @@ func (h *mockLightningClient) ListTransactions( return txs, nil } +// ListChannels retrieves all channels of the backing lnd node. +func (h *mockLightningClient) ListChannels(ctx context.Context) ( + []lndclient.ChannelInfo, error) { + + return h.lnd.Channels, nil +} + // ChannelBackup retrieves the backup for a particular channel. The // backup is returned as an encrypted chanbackup.Single payload. func (h *mockLightningClient) ChannelBackup(context.Context, wire.OutPoint) ([]byte, error) { diff --git a/test/lnd_services_mock.go b/test/lnd_services_mock.go index b340291..b02769c 100644 --- a/test/lnd_services_mock.go +++ b/test/lnd_services_mock.go @@ -163,6 +163,8 @@ type LndMockServices struct { // keyed by hash string. Invoices map[lntypes.Hash]*lndclient.Invoice + Channels []lndclient.ChannelInfo + WaitForFinished func() lock sync.Mutex