mutli: switchover ForwardingEvents calls to lndclient

This commit is contained in:
carla 2020-06-17 09:42:55 +02:00
parent d4549d3a32
commit 2aec39dbe1
No known key found for this signature in database
GPG key ID: 4CA7FE54A6213C91
10 changed files with 94 additions and 92 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/btcsuite/btcutil"
"github.com/lightninglabs/loop/lndclient"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwire"
)
// feeReference returns a special unique reference for the fee paid on a
@ -325,28 +326,28 @@ func paymentEntry(payment settledPayment, paidToSelf bool,
// ID paired with timestamp in an effort to make txid unique per htlc forwarded.
// This is not used as a reference because we could theoretically have duplicate
// timestamps.
func forwardTxid(forward *lnrpc.ForwardingEvent) string {
return fmt.Sprintf("%v:%v:%v", forward.Timestamp, forward.ChanIdIn,
forward.ChanIdOut)
func forwardTxid(forward lndclient.ForwardingEvent) string {
return fmt.Sprintf("%v:%v:%v", forward.Timestamp, forward.ChannelIn,
forward.ChannelOut)
}
// forwardNote creates a note that indicates the amuonts that were forwarded in
// and out of our node.
func forwardNote(amtIn, amtOut uint64) string {
func forwardNote(amtIn, amtOut lnwire.MilliSatoshi) string {
return fmt.Sprintf("incoming: %v msat outgoing: %v msat", amtIn, amtOut)
}
// forwardingEntry produces a forwarding entry with a zero amount which reflects
// shifting of funds in our channels, and fees entry which reflects the fees we
// earned form the forward.
func forwardingEntry(forward *lnrpc.ForwardingEvent,
func forwardingEntry(forward lndclient.ForwardingEvent,
convert msatToFiat) ([]*HarmonyEntry, error) {
txid := forwardTxid(forward)
note := forwardNote(forward.AmtInMsat, forward.AmtOutMsat)
note := forwardNote(forward.AmountMsatIn, forward.AmountMsatOut)
fwdEntry, err := newHarmonyEntry(
int64(forward.Timestamp), 0, EntryTypeForward, txid, "", note,
forward.Timestamp.Unix(), 0, EntryTypeForward, txid, "", note,
false, convert,
)
if err != nil {
@ -359,7 +360,7 @@ func forwardingEntry(forward *lnrpc.ForwardingEvent,
}
feeEntry, err := newHarmonyEntry(
int64(forward.Timestamp), int64(forward.FeeMsat),
forward.Timestamp.Unix(), int64(forward.FeeMsat),
EntryTypeForwardFee, txid, "", "", false, convert,
)
if err != nil {

View file

@ -150,22 +150,22 @@ var (
settleTime: time.Unix(int64(paymentTime), 0),
}
forwardTs uint64 = 1590578022
forwardTs = time.Unix(1590578022, 0)
forwardChanIn uint64 = 130841883770880
forwardChanOut uint64 = 124244814004224
fwdInMsat uint64 = 4000
fwdOutMsat uint64 = 3000
fwdFeeMsat uint64 = 1000
fwdInMsat = lnwire.MilliSatoshi(4000)
fwdOutMsat = lnwire.MilliSatoshi(3000)
fwdFeeMsat = lnwire.MilliSatoshi(1000)
fwdEntry = &lnrpc.ForwardingEvent{
Timestamp: forwardTs,
ChanIdIn: forwardChanIn,
ChanIdOut: forwardChanOut,
FeeMsat: fwdFeeMsat,
AmtInMsat: fwdInMsat,
AmtOutMsat: fwdOutMsat,
fwdEntry = lndclient.ForwardingEvent{
Timestamp: forwardTs,
ChannelIn: forwardChanIn,
ChannelOut: forwardChanOut,
FeeMsat: fwdFeeMsat,
AmountMsatIn: fwdInMsat,
AmountMsatOut: fwdOutMsat,
}
)
@ -612,13 +612,12 @@ func TestForwardingEntry(t *testing.T) {
entries, err := forwardingEntry(fwdEntry, mockConvert)
require.NoError(t, err)
ts := time.Unix(int64(forwardTs), 0)
txid := forwardTxid(fwdEntry)
note := forwardNote(fwdInMsat, fwdOutMsat)
fwdFiat, _ := mockConvert(int64(0), 0)
fwdEntry := &HarmonyEntry{
Timestamp: ts,
Timestamp: forwardTs,
Amount: 0,
FiatValue: fwdFiat,
TxID: txid,
@ -632,8 +631,8 @@ func TestForwardingEntry(t *testing.T) {
feeFiat, _ := mockConvert(int64(fwdFeeMsat), 0)
feeEntry := &HarmonyEntry{
Timestamp: ts,
Amount: lnwire.MilliSatoshi(fwdFeeMsat),
Timestamp: forwardTs,
Amount: fwdFeeMsat,
FiatValue: feeFiat,
TxID: txid,
Reference: "",

View file

@ -7,6 +7,7 @@ import (
"time"
"github.com/lightninglabs/faraday/fiat"
"github.com/lightninglabs/loop/lndclient"
"github.com/lightningnetwork/lnd/lnrpc"
)
@ -41,7 +42,7 @@ type OffChainConfig struct {
ListPayments func() ([]*lnrpc.Payment, error)
// ListForwards lists all our forwards over out relevant period.
ListForwards func() ([]*lnrpc.ForwardingEvent, error)
ListForwards func() ([]lndclient.ForwardingEvent, error)
// OwnPubKey is our node's public key. We use this value to identify
// payments that are made to our own node.
@ -120,7 +121,7 @@ func offChainReportWithPrices(cfg *OffChainConfig, getPrice msatToFiat) (Report,
// invoices they paid.
func offChainReport(invoices []*lnrpc.Invoice, payments []settledPayment,
circularPayments map[string]bool, forwards []*lnrpc.ForwardingEvent,
circularPayments map[string]bool, forwards []lndclient.ForwardingEvent,
convert msatToFiat) (Report, error) {
var reports Report

View file

@ -5,6 +5,7 @@ import (
"time"
"github.com/lightninglabs/faraday/fiat"
"github.com/lightninglabs/loop/lndclient"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/stretchr/testify/require"
)
@ -236,7 +237,7 @@ func TestOffChainReport(t *testing.T) {
ListPayments: func() ([]*lnrpc.Payment, error) {
return test.payments, nil
},
ListForwards: func() ([]*lnrpc.ForwardingEvent,
ListForwards: func() ([]lndclient.ForwardingEvent,
error) {
return nil, nil

View file

@ -15,7 +15,7 @@ func channelInsights(ctx context.Context,
// Get revenue from a zero start time to the present to cover
// revenue over the lifetime of all our channels.
revenueCfg := getRevenueConfig(
ctx, cfg, 0, uint64(time.Now().Unix()),
ctx, cfg, time.Unix(0, 0), time.Now(),
)
report, err := revenue.GetRevenueReport(revenueCfg)

View file

@ -51,7 +51,7 @@ func parseNodeReportRequest(ctx context.Context, cfg *Config,
ListPayments: func() ([]*lnrpc.Payment, error) {
return cfg.wrapListPayments(ctx)
},
ListForwards: func() ([]*lnrpc.ForwardingEvent, error) {
ListForwards: func() ([]lndclient.ForwardingEvent, error) {
return cfg.wrapListForwards(ctx, start, end)
},
OwnPubKey: hex.EncodeToString(info.IdentityPubkey[:]),

View file

@ -6,7 +6,6 @@ import (
"github.com/lightninglabs/faraday/revenue"
"github.com/lightninglabs/loop/lndclient"
"github.com/lightningnetwork/lnd/lnrpc"
)
// parseRevenueRequest parses a request for a revenue report and wraps
@ -19,32 +18,29 @@ func parseRevenueRequest(ctx context.Context, cfg *Config,
// We allow start time to be zero so that revenue can
// be calculated over the channel's full lifetime without
// knowing the time it was opened.
endTime := req.EndTime
if endTime == 0 {
endTime = uint64(time.Now().Unix())
endTime := time.Unix(int64(req.EndTime), 0)
if req.EndTime == 0 {
endTime = time.Now()
}
return getRevenueConfig(ctx, cfg, req.StartTime, endTime)
start := time.Unix(int64(req.StartTime), 0)
return getRevenueConfig(ctx, cfg, start, endTime)
}
func getRevenueConfig(ctx context.Context, cfg *Config,
start, end uint64) *revenue.Config {
start, end time.Time) *revenue.Config {
forwardingHistory := func(offset,
maxEvents uint32) ([]*lnrpc.ForwardingEvent, uint32, error) {
resp, err := cfg.LightningClient.ForwardingHistory(
ctx, &lnrpc.ForwardingHistoryRequest{
StartTime: start,
EndTime: end,
IndexOffset: offset,
NumMaxEvents: maxEvents,
forwardingHistory := func(offset, maxEvents uint32) (
*lndclient.ForwardingHistoryResponse, error) {
return cfg.Lnd.Client.ForwardingHistory(
ctx, lndclient.ForwardingHistoryRequest{
StartTime: start,
EndTime: end,
MaxEvents: maxEvents,
Offset: offset,
},
)
if err != nil {
return nil, 0, err
}
return resp.ForwardingEvents, resp.LastOffsetIndex, nil
}
return &revenue.Config{

View file

@ -218,27 +218,27 @@ func (c *Config) wrapListPayments(ctx context.Context) ([]*lnrpc.Payment, error)
// wrapListForwards makes paginated calls to our forwarding events api.
func (c *Config) wrapListForwards(ctx context.Context, startTime,
endTime time.Time) ([]*lnrpc.ForwardingEvent, error) {
endTime time.Time) ([]lndclient.ForwardingEvent, error) {
var forwards []*lnrpc.ForwardingEvent
var forwards []lndclient.ForwardingEvent
query := func(offset, maxEvents uint64) (uint64, uint64, error) {
resp, err := c.LightningClient.ForwardingHistory(
ctx, &lnrpc.ForwardingHistoryRequest{
StartTime: uint64(startTime.Unix()),
EndTime: uint64(endTime.Unix()),
IndexOffset: uint32(offset),
NumMaxEvents: uint32(maxEvents),
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.ForwardingEvents...)
forwards = append(forwards, resp.Events...)
return uint64(resp.LastOffsetIndex),
uint64(len(resp.ForwardingEvents)), nil
return uint64(resp.LastIndexOffset),
uint64(len(resp.Events)), nil
}
// Make paginated calls to the forwards API, starting at offset 0 and

View file

@ -5,7 +5,6 @@ import (
"github.com/lightninglabs/faraday/paginater"
"github.com/lightninglabs/loop/lndclient"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwire"
)
@ -25,7 +24,7 @@ type Config struct {
// The period that these results queried over determines the period
// that the report is generated for.
ForwardingHistory func(offset, maxEvents uint32) (
[]*lnrpc.ForwardingEvent, uint32, error)
*lndclient.ForwardingHistoryResponse, error)
}
// GetRevenueReport produces a revenue report over the period specified.
@ -69,9 +68,9 @@ func GetRevenueReport(cfg *Config) (*Report, error) {
// forwarding events for the period provided. It takes a map of shortChannelIDs
// to outpoints which is used to convert forwarding events short ids to
// outpoint strings.
func getEvents(channelIDs map[lnwire.ShortChannelID]string,
queryForwards func(offset, maxEvents uint32) ([]*lnrpc.ForwardingEvent,
uint32, error)) ([]revenueEvent, error) {
func getEvents(channelIDs map[lnwire.ShortChannelID]string, queryForwards func(
offset, maxEvents uint32) (*lndclient.ForwardingHistoryResponse,
error)) ([]revenueEvent, error) {
var events []revenueEvent
@ -80,7 +79,7 @@ func getEvents(channelIDs map[lnwire.ShortChannelID]string,
// We use this function with our generic paginater to build up a set of
// forwarding events from the paginated api.
query := func(offset, maxEvents uint64) (uint64, uint64, error) {
fwdEvents, newOffset, err := queryForwards(
resp, err := queryForwards(
uint32(offset), uint32(maxQueryEvents),
)
if err != nil {
@ -91,12 +90,12 @@ func getEvents(channelIDs map[lnwire.ShortChannelID]string,
// and create a revenue event. Return an error if the short
// channel id's outpoint cannot be found, because we expect all
// known short channel ids to be provided.
for _, fwd := range fwdEvents {
for _, fwd := range resp.Events {
shortChanIn := lnwire.NewShortChanIDFromInt(
fwd.ChanIdIn,
fwd.ChannelIn,
)
shortChanOut := lnwire.NewShortChanIDFromInt(
fwd.ChanIdOut,
fwd.ChannelOut,
)
incoming, ok := channelIDs[shortChanIn]
@ -104,8 +103,8 @@ func getEvents(channelIDs map[lnwire.ShortChannelID]string,
log.Errorf("cannot find channel "+
"incoming outpoint for forward: %v(%v "+
"msat) -> %v(%v msat)", shortChanIn,
fwd.AmtInMsat, shortChanOut,
fwd.AmtOutMsat)
fwd.AmountMsatIn, shortChanOut,
fwd.AmountMsatOut)
continue
}
@ -115,8 +114,8 @@ func getEvents(channelIDs map[lnwire.ShortChannelID]string,
log.Errorf("cannot find channel "+
"outgoing outpoint for forward: %v(%v "+
"msat) -> %v(%v msat)", shortChanIn,
fwd.AmtInMsat, shortChanOut,
fwd.AmtOutMsat)
fwd.AmountMsatIn, shortChanOut,
fwd.AmountMsatOut)
continue
}
@ -124,12 +123,12 @@ func getEvents(channelIDs map[lnwire.ShortChannelID]string,
events = append(events, revenueEvent{
incomingChannel: incoming,
outgoingChannel: outgoing,
incomingAmt: lnwire.MilliSatoshi(fwd.AmtInMsat),
outgoingAmt: lnwire.MilliSatoshi(fwd.AmtOutMsat),
incomingAmt: fwd.AmountMsatIn,
outgoingAmt: fwd.AmountMsatOut,
})
}
return uint64(newOffset), uint64(len(fwdEvents)), nil
return offset, uint64(len(resp.Events)), nil
}
// Pass our query which accumulates events to our generic paginated

View file

@ -6,7 +6,6 @@ import (
"testing"
"github.com/lightninglabs/loop/lndclient"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
)
@ -36,7 +35,7 @@ func TestGetRevenueReport(t *testing.T) {
forwardHistErr error
openChannels []lndclient.ChannelInfo
closedChannels []lndclient.ClosedChannel
fwdHistory []*lnrpc.ForwardingEvent
fwdHistory []lndclient.ForwardingEvent
expectedReport *Report
expectErr error
}{
@ -57,9 +56,9 @@ func TestGetRevenueReport(t *testing.T) {
},
{
name: "cannot find channel",
fwdHistory: []*lnrpc.ForwardingEvent{
fwdHistory: []lndclient.ForwardingEvent{
{
ChanIdIn: 123,
ChannelIn: 123,
},
},
expectErr: nil,
@ -74,12 +73,12 @@ func TestGetRevenueReport(t *testing.T) {
ChannelPoint: chan2.ChannelPoint,
ChannelID: chan2.ChannelID,
}},
fwdHistory: []*lnrpc.ForwardingEvent{
fwdHistory: []lndclient.ForwardingEvent{
{
ChanIdIn: chan1.ChannelID,
ChanIdOut: chan2.ChannelID,
AmtOutMsat: 100,
AmtInMsat: 150,
ChannelIn: chan1.ChannelID,
ChannelOut: chan2.ChannelID,
AmountMsatOut: 100,
AmountMsatIn: 150,
},
},
expectedReport: &Report{
@ -117,9 +116,12 @@ func TestGetRevenueReport(t *testing.T) {
return test.closedChannels, test.closedChanErr
},
ForwardingHistory: func(offset,
max uint32) ([]*lnrpc.ForwardingEvent, uint32, error) {
max uint32) (*lndclient.ForwardingHistoryResponse, error) {
return test.fwdHistory, offset, test.forwardHistErr
return &lndclient.ForwardingHistoryResponse{
LastIndexOffset: offset,
Events: test.fwdHistory,
}, test.forwardHistErr
},
}
@ -147,20 +149,23 @@ func TestGetEvents(t *testing.T) {
chanOutID := lnwire.NewShortChanIDFromInt(321)
// mockedEvents is the set of events our mock returns.
mockedEvents := []*lnrpc.ForwardingEvent{
mockedEvents := []lndclient.ForwardingEvent{
{
ChanIdIn: chanInID.ToUint64(),
ChanIdOut: chanOutID.ToUint64(),
AmtOutMsat: 2000,
AmtInMsat: 4000,
ChannelIn: chanInID.ToUint64(),
ChannelOut: chanOutID.ToUint64(),
AmountMsatOut: 2000,
AmountMsatIn: 4000,
},
}
// mockQuery returns our set of mocked events.
mockQuery := func(_, _ uint32) ([]*lnrpc.ForwardingEvent, uint32,
mockQuery := func(_, _ uint32) (*lndclient.ForwardingHistoryResponse,
error) {
return mockedEvents, 0, nil
return &lndclient.ForwardingHistoryResponse{
LastIndexOffset: 0,
Events: mockedEvents,
}, nil
}
// channelIDFound is a map that will successfully lookup an outpoint for