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:
Wilmer Paulino 2018-08-10 20:24:43 -07:00 committed by Valentine Wallace
parent f315b5b0d1
commit 2906629ae5
No known key found for this signature in database
GPG Key ID: B0E55E8D1776A58D

@ -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
// returning the latest 100 invoices.
if req.NumMaxInvoices == 0 {
req.NumMaxInvoices = 100
}
// Next, we'll map the proto request into a format that is understood by
// 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)
}
// 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 := make([]*lnrpc.Invoice, len(dbInvoices))
for i, dbInvoice := range dbInvoices {
rpcInvoice, err := createRPCInvoice(&dbInvoice)
if err != nil {
return nil, err
} }
invoices[i] = rpcInvoice return resp, nil
}
return &lnrpc.ListInvoiceResponse{
Invoices: invoices,
}, nil
} }
// SubscribeInvoices returns a uni-directional stream (server -> client) for // SubscribeInvoices returns a uni-directional stream (server -> client) for