rpcserver: implement pagination support for listing invoices
Co-authored-by: Andrey Savitskiy <taketa@users.noreply.github.com> Co-authored-by: Valentine Wallace <valentine.m.wallace@gmail.com>
This commit is contained in:
parent
f315b5b0d1
commit
2906629ae5
35
rpcserver.go
35
rpcserver.go
@ -2873,25 +2873,38 @@ func (r *rpcServer) LookupInvoice(ctx context.Context,
|
|||||||
func (r *rpcServer) ListInvoices(ctx context.Context,
|
func (r *rpcServer) ListInvoices(ctx context.Context,
|
||||||
req *lnrpc.ListInvoiceRequest) (*lnrpc.ListInvoiceResponse, error) {
|
req *lnrpc.ListInvoiceRequest) (*lnrpc.ListInvoiceResponse, error) {
|
||||||
|
|
||||||
dbInvoices, err := r.server.chanDB.FetchAllInvoices(req.PendingOnly)
|
// If the number of invoices was not specified, then we'll default to
|
||||||
if err != nil {
|
// returning the latest 100 invoices.
|
||||||
return nil, err
|
if req.NumMaxInvoices == 0 {
|
||||||
|
req.NumMaxInvoices = 100
|
||||||
}
|
}
|
||||||
|
|
||||||
invoices := make([]*lnrpc.Invoice, len(dbInvoices))
|
// Next, we'll map the proto request into a format that is understood by
|
||||||
for i, dbInvoice := range dbInvoices {
|
// the database.
|
||||||
|
q := channeldb.InvoiceQuery{
|
||||||
|
IndexOffset: req.IndexOffset,
|
||||||
|
NumMaxInvoices: req.NumMaxInvoices,
|
||||||
|
PendingOnly: req.PendingOnly,
|
||||||
|
}
|
||||||
|
invoiceSlice, err := r.server.chanDB.QueryInvoices(q)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to query invoices: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
rpcInvoice, err := createRPCInvoice(&dbInvoice)
|
// Before returning the response, we'll need to convert each invoice
|
||||||
|
// into it's proto representation.
|
||||||
|
resp := &lnrpc.ListInvoiceResponse{
|
||||||
|
Invoices: make([]*lnrpc.Invoice, len(invoiceSlice.Invoices)),
|
||||||
|
LastIndexOffset: invoiceSlice.LastIndexOffset,
|
||||||
|
}
|
||||||
|
for i, invoice := range invoiceSlice.Invoices {
|
||||||
|
resp.Invoices[i], err = createRPCInvoice(invoice)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
invoices[i] = rpcInvoice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &lnrpc.ListInvoiceResponse{
|
return resp, nil
|
||||||
Invoices: invoices,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubscribeInvoices returns a uni-directional stream (server -> client) for
|
// SubscribeInvoices returns a uni-directional stream (server -> client) for
|
||||||
|
Loading…
Reference in New Issue
Block a user