Skip to content

Commit e016d2e

Browse files
committed
lndclient: add forwarding history call
1 parent c1a9889 commit e016d2e

File tree

3 files changed

+108
-2
lines changed

3 files changed

+108
-2
lines changed

lndclient/lightning_client.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ type LightningClient interface {
5353
// ClosedChannels returns all closed channels of the backing lnd node.
5454
ClosedChannels(ctx context.Context) ([]ClosedChannel, error)
5555

56+
// ForwardingHistory makes a paginated call to our forwarding history
57+
// endpoint.
58+
ForwardingHistory(ctx context.Context,
59+
req ForwardingHistoryRequest) (*ForwardingHistoryResponse, error)
60+
5661
// ChannelBackup retrieves the backup for a particular channel. The
5762
// backup is returned as an encrypted chanbackup.Single payload.
5863
ChannelBackup(context.Context, wire.OutPoint) ([]byte, error)
@@ -842,6 +847,95 @@ func getInitiator(initiator lnrpc.Initiator) (Initiator, error) {
842847
}
843848
}
844849

850+
// ForwardingHistoryRequest contains the request parameters for a paginated
851+
// forwarding history call.
852+
type ForwardingHistoryRequest struct {
853+
// StartTime is the beginning of the query period.
854+
StartTime time.Time
855+
856+
// EndTime is the end of the query period.
857+
EndTime time.Time
858+
859+
// MaxEvents is the maximum number of events to return.
860+
MaxEvents uint32
861+
862+
// Offset is the index from which to start querying.
863+
Offset uint32
864+
}
865+
866+
// ForwardingHistoryResponse contains the response to a forwarding history
867+
// query, including last index offset required for paginated queries.
868+
type ForwardingHistoryResponse struct {
869+
// LastIndexOffset is the index offset of the last item in our set.
870+
LastIndexOffset uint32
871+
872+
// Events is the set of events that were found in the interval queried.
873+
Events []ForwardingEvent
874+
}
875+
876+
// ForwardingEvent represents a htlc that was forwarded through our node.
877+
type ForwardingEvent struct {
878+
// Timestamp is the time that we processed the forwarding event.
879+
Timestamp time.Time
880+
881+
// ChannelIn is the id of the channel the htlc arrived at our node on.
882+
ChannelIn uint64
883+
884+
// ChannelOut is the id of the channel the htlc left our node on.
885+
ChannelOut uint64
886+
887+
// AmountMsatIn is the amount that was forwarded into our node in
888+
// millisatoshis.
889+
AmountMsatIn lnwire.MilliSatoshi
890+
891+
// AmountMsatOut is the amount that was forwarded out of our node in
892+
// millisatoshis.
893+
AmountMsatOut lnwire.MilliSatoshi
894+
895+
// FeeMsat is the amount of fees earned in millisatoshis,
896+
FeeMsat lnwire.MilliSatoshi
897+
}
898+
899+
// ForwardingHistory returns a set of forwarding events for the period queried.
900+
// Note that this call is paginated, and the information required to make
901+
// subsequent calls is provided in the response.
902+
func (s *lightningClient) ForwardingHistory(ctx context.Context,
903+
req ForwardingHistoryRequest) (*ForwardingHistoryResponse, error) {
904+
905+
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
906+
defer cancel()
907+
908+
response, err := s.client.ForwardingHistory(
909+
s.adminMac.WithMacaroonAuth(rpcCtx),
910+
&lnrpc.ForwardingHistoryRequest{
911+
StartTime: uint64(req.StartTime.Unix()),
912+
EndTime: uint64(req.EndTime.Unix()),
913+
IndexOffset: req.Offset,
914+
NumMaxEvents: req.MaxEvents,
915+
},
916+
)
917+
if err != nil {
918+
return nil, err
919+
}
920+
921+
events := make([]ForwardingEvent, len(response.ForwardingEvents))
922+
for i, event := range response.ForwardingEvents {
923+
events[i] = ForwardingEvent{
924+
Timestamp: time.Unix(int64(event.Timestamp), 0),
925+
ChannelIn: event.ChanIdIn,
926+
ChannelOut: event.ChanIdOut,
927+
AmountMsatIn: lnwire.MilliSatoshi(event.AmtIn),
928+
AmountMsatOut: lnwire.MilliSatoshi(event.AmtOut),
929+
FeeMsat: lnwire.MilliSatoshi(event.FeeMsat),
930+
}
931+
}
932+
933+
return &ForwardingHistoryResponse{
934+
LastIndexOffset: response.LastOffsetIndex,
935+
Events: events,
936+
}, nil
937+
}
938+
845939
// ChannelBackup retrieves the backup for a particular channel. The backup is
846940
// returned as an encrypted chanbackup.Single payload.
847941
func (s *lightningClient) ChannelBackup(ctx context.Context,

test/lightning_client_mock.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ func (h *mockLightningClient) ClosedChannels(_ context.Context) ([]lndclient.Clo
182182
return h.lnd.ClosedChannels, nil
183183
}
184184

185+
// ForwardingHistory returns the mock's set of forwarding events.
186+
func (h *mockLightningClient) ForwardingHistory(_ context.Context,
187+
_ lndclient.ForwardingHistoryRequest) (*lndclient.ForwardingHistoryResponse,
188+
error) {
189+
190+
return &lndclient.ForwardingHistoryResponse{
191+
LastIndexOffset: 0,
192+
Events: h.lnd.ForwardingEvents,
193+
}, nil
194+
}
195+
185196
// ChannelBackup retrieves the backup for a particular channel. The
186197
// backup is returned as an encrypted chanbackup.Single payload.
187198
func (h *mockLightningClient) ChannelBackup(context.Context, wire.OutPoint) ([]byte, error) {

test/lnd_services_mock.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,9 @@ type LndMockServices struct {
163163
// keyed by hash string.
164164
Invoices map[lntypes.Hash]*lndclient.Invoice
165165

166-
Channels []lndclient.ChannelInfo
167-
ClosedChannels []lndclient.ClosedChannel
166+
Channels []lndclient.ChannelInfo
167+
ClosedChannels []lndclient.ClosedChannel
168+
ForwardingEvents []lndclient.ForwardingEvent
168169

169170
WaitForFinished func()
170171

0 commit comments

Comments
 (0)