From 66aef12a0109ea4281c1932d2884aac80e764ed5 Mon Sep 17 00:00:00 2001 From: carla Date: Thu, 25 Jun 2020 17:09:01 +0200 Subject: [PATCH] multi: move lnd wrapping into package Move wrapping/pagintion of lnd queries into a single package so that it can be reused outside of our rpc server. --- frdrpc/channel_insights.go | 5 +- frdrpc/node_report.go | 20 ++++- frdrpc/revenue_report.go | 3 +- frdrpc/rpcserver.go | 135 --------------------------------- lndwrap/lndwrap.go | 148 +++++++++++++++++++++++++++++++++++++ 5 files changed, 170 insertions(+), 141 deletions(-) create mode 100644 lndwrap/lndwrap.go diff --git a/frdrpc/channel_insights.go b/frdrpc/channel_insights.go index 1fec00a..b0d426b 100644 --- a/frdrpc/channel_insights.go +++ b/frdrpc/channel_insights.go @@ -5,6 +5,7 @@ import ( "time" "github.com/lightninglabs/faraday/insights" + "github.com/lightninglabs/faraday/lndwrap" "github.com/lightninglabs/faraday/revenue" ) @@ -24,7 +25,9 @@ func channelInsights(ctx context.Context, } return insights.GetChannels(&insights.Config{ - OpenChannels: cfg.wrapListChannels(ctx, false), + OpenChannels: lndwrap.ListChannels( + ctx, cfg.Lnd.Client, false, + ), CurrentHeight: func() (u uint32, err error) { info, err := cfg.Lnd.Client.GetInfo(ctx) if err != nil { diff --git a/frdrpc/node_report.go b/frdrpc/node_report.go index 21d4493..63e182c 100644 --- a/frdrpc/node_report.go +++ b/frdrpc/node_report.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/lightninglabs/faraday/accounting" + "github.com/lightninglabs/faraday/lndwrap" "github.com/lightninglabs/lndclient" ) @@ -21,7 +22,9 @@ func parseNodeReportRequest(ctx context.Context, cfg *Config, } onChain := &accounting.OnChainConfig{ - OpenChannels: cfg.wrapListChannels(ctx, false), + OpenChannels: lndwrap.ListChannels( + ctx, cfg.Lnd.Client, false, + ), ClosedChannels: func() ([]lndclient.ClosedChannel, error) { return cfg.Lnd.Client.ClosedChannels(ctx) }, @@ -44,13 +47,22 @@ func parseNodeReportRequest(ctx context.Context, cfg *Config, offChain := &accounting.OffChainConfig{ ListInvoices: func() ([]lndclient.Invoice, error) { - return cfg.wrapListInvoices(ctx) + return lndwrap.ListInvoices( + ctx, 0, uint64(maxInvoiceQueries), + cfg.Lnd.Client, + ) }, ListPayments: func() ([]lndclient.Payment, error) { - return cfg.wrapListPayments(ctx) + return lndwrap.ListPayments( + ctx, 0, uint64(maxPaymentQueries), + cfg.Lnd.Client, + ) }, ListForwards: func() ([]lndclient.ForwardingEvent, error) { - return cfg.wrapListForwards(ctx, start, end) + return lndwrap.ListForwards( + ctx, uint64(maxForwardQueries), start, end, + cfg.Lnd.Client, + ) }, OwnPubKey: hex.EncodeToString(info.IdentityPubkey[:]), StartTime: start, diff --git a/frdrpc/revenue_report.go b/frdrpc/revenue_report.go index 8eaaa46..cb7e59e 100644 --- a/frdrpc/revenue_report.go +++ b/frdrpc/revenue_report.go @@ -4,6 +4,7 @@ import ( "context" "time" + "github.com/lightninglabs/faraday/lndwrap" "github.com/lightninglabs/faraday/revenue" "github.com/lightninglabs/lndclient" ) @@ -44,7 +45,7 @@ func getRevenueConfig(ctx context.Context, cfg *Config, } return &revenue.Config{ - ListChannels: cfg.wrapListChannels(ctx, false), + ListChannels: lndwrap.ListChannels(ctx, cfg.Lnd.Client, false), ClosedChannels: func() ([]lndclient.ClosedChannel, error) { return cfg.Lnd.Client.ClosedChannels(ctx) }, diff --git a/frdrpc/rpcserver.go b/frdrpc/rpcserver.go index e700ecc..a6f7485 100644 --- a/frdrpc/rpcserver.go +++ b/frdrpc/rpcserver.go @@ -16,12 +16,10 @@ import ( "net/http" "sync" "sync/atomic" - "time" proxy "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/lightninglabs/faraday/accounting" "github.com/lightninglabs/faraday/fiat" - "github.com/lightninglabs/faraday/paginater" "github.com/lightninglabs/faraday/recommend" "github.com/lightninglabs/faraday/revenue" "github.com/lightninglabs/lndclient" @@ -106,139 +104,6 @@ type Config struct { CORSOrigin string } -// wrapListChannels wraps the listchannels call to lnd, with a publicOnly bool -// that can be used to toggle whether private channels are included. -func (c *Config) wrapListChannels(ctx context.Context, - publicOnly bool) func() ([]lndclient.ChannelInfo, error) { - - return func() ([]lndclient.ChannelInfo, error) { - resp, err := c.Lnd.Client.ListChannels(ctx) - if err != nil { - return nil, err - } - - // If we want all channels, we can just return now. - if !publicOnly { - return resp, err - } - - // If we only want public channels, we skip over all private - // channels and return a list of public only. - var publicChannels []lndclient.ChannelInfo - for _, channel := range resp { - if channel.Private { - continue - } - - publicChannels = append(publicChannels, channel) - } - - return publicChannels, nil - } -} - -// wrapListInvoices makes paginated calls to lnd to get our full set of -// invoices. -func (c *Config) wrapListInvoices(ctx context.Context) ([]lndclient.Invoice, error) { - var invoices []lndclient.Invoice - - query := func(offset, maxInvoices uint64) (uint64, uint64, error) { - resp, err := c.Lnd.Client.ListInvoices( - ctx, lndclient.ListInvoicesRequest{ - Offset: offset, - MaxInvoices: maxInvoices, - }, - ) - if err != nil { - return 0, 0, err - } - - invoices = append(invoices, resp.Invoices...) - - return resp.LastIndexOffset, uint64(len(resp.Invoices)), nil - } - - // Make paginated calls to the invoices API, starting at offset 0 and - // querying our max number of invoices each time. - if err := paginater.QueryPaginated( - ctx, query, 0, uint64(maxInvoiceQueries), - ); err != nil { - return nil, err - } - - return invoices, nil -} - -// wrapListPayments makes a set of paginated calls to lnd to get our full set -// of payments. -func (c *Config) wrapListPayments(ctx context.Context) ([]lndclient.Payment, - error) { - - var payments []lndclient.Payment - - query := func(offset, maxEvents uint64) (uint64, uint64, error) { - resp, err := c.Lnd.Client.ListPayments( - ctx, lndclient.ListPaymentsRequest{ - Offset: offset, - MaxPayments: maxEvents, - }, - ) - if err != nil { - return 0, 0, err - } - - payments = append(payments, resp.Payments...) - - return resp.LastIndexOffset, uint64(len(resp.Payments)), nil - } - - // Make paginated calls to the payments API, starting at offset 0 and - // querying our max number of payments each time. - if err := paginater.QueryPaginated( - ctx, query, 0, uint64(maxPaymentQueries), - ); err != nil { - return nil, err - } - - return payments, nil -} - -// wrapListForwards makes paginated calls to our forwarding events api. -func (c *Config) wrapListForwards(ctx context.Context, startTime, - endTime time.Time) ([]lndclient.ForwardingEvent, error) { - - var forwards []lndclient.ForwardingEvent - - query := func(offset, maxEvents uint64) (uint64, uint64, error) { - resp, err := c.Lnd.Client.ForwardingHistory( - ctx, lndclient.ForwardingHistoryRequest{ - StartTime: startTime, - EndTime: endTime, - Offset: uint32(offset), - MaxEvents: uint32(maxEvents), - }, - ) - if err != nil { - return 0, 0, err - } - - forwards = append(forwards, resp.Events...) - - return uint64(resp.LastIndexOffset), - uint64(len(resp.Events)), nil - } - - // Make paginated calls to the forwards API, starting at offset 0 and - // querying our max number of payments each time. - if err := paginater.QueryPaginated( - ctx, query, 0, uint64(maxForwardQueries), - ); err != nil { - return nil, err - } - - return forwards, nil -} - // NewRPCServer returns a server which will listen for rpc requests on the // rpc listen address provided. Note that the server returned is not running, // and should be started using Start(). diff --git a/lndwrap/lndwrap.go b/lndwrap/lndwrap.go new file mode 100644 index 0000000..1e917c1 --- /dev/null +++ b/lndwrap/lndwrap.go @@ -0,0 +1,148 @@ +// Package lndwrap wraps various calls to lndclient for convenience. It offers +// wrapping for paginated queries that will obtain all entries from a desired +// index onwards. +package lndwrap + +import ( + "context" + "time" + + "github.com/lightninglabs/faraday/paginater" + "github.com/lightninglabs/lndclient" +) + +// ListInvoices makes paginated calls to lnd to get our full set of +// invoices. +func ListInvoices(ctx context.Context, startOffset, maxInvoices uint64, + lnd lndclient.LightningClient) ([]lndclient.Invoice, error) { + + var invoices []lndclient.Invoice + + query := func(offset, maxInvoices uint64) (uint64, uint64, error) { + resp, err := lnd.ListInvoices( + ctx, lndclient.ListInvoicesRequest{ + Offset: offset, + MaxInvoices: maxInvoices, + }, + ) + if err != nil { + return 0, 0, err + } + + invoices = append(invoices, resp.Invoices...) + + return resp.LastIndexOffset, uint64(len(resp.Invoices)), nil + } + + // Make paginated calls to the invoices API, starting at offset 0 and + // querying our max number of invoices each time. + if err := paginater.QueryPaginated( + ctx, query, startOffset, maxInvoices, + ); err != nil { + return nil, err + } + + return invoices, nil +} + +// ListPayments makes a set of paginated calls to lnd to get our full set +// of payments. +func ListPayments(ctx context.Context, startOffset, maxPayments uint64, + lnd lndclient.LightningClient) ([]lndclient.Payment, error) { + + var payments []lndclient.Payment + + query := func(offset, maxEvents uint64) (uint64, uint64, error) { + resp, err := lnd.ListPayments( + ctx, lndclient.ListPaymentsRequest{ + Offset: offset, + MaxPayments: maxEvents, + }, + ) + if err != nil { + return 0, 0, err + } + + payments = append(payments, resp.Payments...) + + return resp.LastIndexOffset, uint64(len(resp.Payments)), nil + } + + // Make paginated calls to the payments API, starting at offset 0 and + // querying our max number of payments each time. + if err := paginater.QueryPaginated( + ctx, query, startOffset, maxPayments, + ); err != nil { + return nil, err + } + + return payments, nil +} + +// ListForwards makes paginated calls to our forwarding events api. +func ListForwards(ctx context.Context, maxForwards uint64, startTime, + endTime time.Time, lnd lndclient.LightningClient) ( + []lndclient.ForwardingEvent, error) { + + var forwards []lndclient.ForwardingEvent + + query := func(offset, maxEvents uint64) (uint64, uint64, error) { + resp, err := lnd.ForwardingHistory( + ctx, lndclient.ForwardingHistoryRequest{ + StartTime: startTime, + EndTime: endTime, + Offset: uint32(offset), + MaxEvents: uint32(maxEvents), + }, + ) + if err != nil { + return 0, 0, err + } + + forwards = append(forwards, resp.Events...) + + return uint64(resp.LastIndexOffset), + uint64(len(resp.Events)), nil + } + + // Make paginated calls to the forwards API, starting at offset 0 and + // querying our max number of payments each time. + if err := paginater.QueryPaginated( + ctx, query, 0, maxForwards, + ); err != nil { + return nil, err + } + + return forwards, nil +} + +// ListChannels wraps the listchannels call to lnd, with a publicOnly bool +// that can be used to toggle whether private channels are included. +func ListChannels(ctx context.Context, lnd lndclient.LightningClient, + publicOnly bool) func() ([]lndclient.ChannelInfo, error) { + + return func() ([]lndclient.ChannelInfo, error) { + resp, err := lnd.ListChannels(ctx) + if err != nil { + return nil, err + } + + // If we want all channels, we can just return now. + if !publicOnly { + return resp, err + } + + // If we only want public channels, we skip over all private + // channels and return a list of public only. + var publicChannels []lndclient.ChannelInfo + for _, channel := range resp { + if channel.Private { + continue + } + + publicChannels = append(publicChannels, channel) + } + + return publicChannels, nil + } +}