mirror of
https://github.com/lightninglabs/faraday.git
synced 2026-08-16 13:01:10 +02:00
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.
This commit is contained in:
parent
61a850e9f5
commit
66aef12a01
5 changed files with 170 additions and 141 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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().
|
||||
|
|
|
|||
148
lndwrap/lndwrap.go
Normal file
148
lndwrap/lndwrap.go
Normal file
|
|
@ -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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue