lnrpc: use queried payments to list payments in the rpc

Changes the grpc proto file, generates the protobuf, and
enables a queried way to retrieve payments in the rpc, where
backward compatibility is enforced by returning all payments
in the database by default. Adds a payment index field to
the returned payments of the rpc call.
This commit is contained in:
bitromortac 2020-01-17 07:35:03 +01:00
parent d5dd48fa71
commit 39c58d9d14
4 changed files with 930 additions and 766 deletions

File diff suppressed because it is too large Load Diff

View File

@ -3102,6 +3102,13 @@ message Payment {
/// The HTLCs made in attempt to settle the payment [EXPERIMENTAL].
repeated HTLCAttempt htlcs = 14;
/**
The creation index of this payment. Each payment can be uniquely identified
by this index, which may not strictly increment by 1 for payments made in
older versions of lnd.
*/
uint64 payment_index = 15;
}
message HTLCAttempt {
@ -3134,14 +3141,46 @@ message ListPaymentsRequest {
/**
If true, then return payments that have not yet fully completed. This means
that pending payments, as well as failed payments will show up if this
field is set to True.
field is set to true. This flag doesn't change the meaning of the indices,
which are tied to individual payments.
*/
bool include_incomplete = 1;
/**
The index of a payment that will be used as either the start or end of a
query to determine which payments should be returned in the response. The
index_offset is exclusive. In the case of a zero index_offset, the query
will start with the oldest payment when paginating forwards, or will end
with the most recent payment when paginating backwards.
*/
uint64 index_offset = 2;
/// The maximal number of payments returned in the response to this query.
uint64 max_payments = 3;
/**
If set, the payments returned will result from seeking backwards from the
specified index offset. This can be used to paginate backwards. The order
of the returned payments is always oldest first (ascending index order).
*/
bool reversed = 4;
}
message ListPaymentsResponse {
/// The list of payments
repeated Payment payments = 1;
/**
The index of the first item in the set of returned payments. This can be
used as the index_offset to continue seeking backwards in the next request.
*/
uint64 first_index_offset = 2;
/**
The index of the last item in the set of returned payments. This can be used
as the index_offset to continue seeking forwards in the next request.
*/
uint64 last_index_offset = 3;
}
message DeleteAllPaymentsRequest {

View File

@ -1155,7 +1155,31 @@
"parameters": [
{
"name": "include_incomplete",
"description": "*\nIf true, then return payments that have not yet fully completed. This means\nthat pending payments, as well as failed payments will show up if this\nfield is set to True.",
"description": "*\nIf true, then return payments that have not yet fully completed. This means\nthat pending payments, as well as failed payments will show up if this\nfield is set to true. This flag doesn't change the meaning of the indices,\nwhich are tied to individual payments.",
"in": "query",
"required": false,
"type": "boolean",
"format": "boolean"
},
{
"name": "index_offset",
"description": "*\nThe index of a payment that will be used as either the start or end of a\nquery to determine which payments should be returned in the response. The\nindex_offset is exclusive. In the case of a zero index_offset, the query\nwill start with the oldest payment when paginating forwards, or will end\nwith the most recent payment when paginating backwards.",
"in": "query",
"required": false,
"type": "string",
"format": "uint64"
},
{
"name": "max_payments",
"description": "/ The maximal number of payments returned in the response to this query.",
"in": "query",
"required": false,
"type": "string",
"format": "uint64"
},
{
"name": "reversed",
"description": "*\nIf set, the payments returned will result from seeking backwards from the\nspecified index offset. This can be used to paginate backwards. The order\nof the returned payments is always oldest first (ascending index order).",
"in": "query",
"required": false,
"type": "boolean",
@ -3494,6 +3518,16 @@
"$ref": "#/definitions/lnrpcPayment"
},
"title": "/ The list of payments"
},
"first_index_offset": {
"type": "string",
"format": "uint64",
"description": "*\nThe index of the first item in the set of returned payments. This can be\nused as the index_offset to continue seeking backwards in the next request."
},
"last_index_offset": {
"type": "string",
"format": "uint64",
"description": "*\nThe index of the last item in the set of returned payments. This can be used\nas the index_offset to continue seeking forwards in the next request."
}
}
},
@ -3957,6 +3991,11 @@
"$ref": "#/definitions/lnrpcHTLCAttempt"
},
"description": "/ The HTLCs made in attempt to settle the payment [EXPERIMENTAL]."
},
"payment_index": {
"type": "string",
"format": "uint64",
"description": "*\nThe creation index of this payment. Each payment can be uniquely identified\nby this index, which may not strictly increment by 1 for payments made in\nolder versions of lnd."
}
}
},

View File

@ -5153,26 +5153,37 @@ func marshallTopologyChange(topChange *routing.TopologyChange) *lnrpc.GraphTopol
}
}
// ListPayments returns a list of all outgoing payments.
// ListPayments returns a list of outgoing payments determined by a paginated
// database query.
func (r *rpcServer) ListPayments(ctx context.Context,
req *lnrpc.ListPaymentsRequest) (*lnrpc.ListPaymentsResponse, error) {
rpcsLog.Debugf("[ListPayments]")
payments, err := r.server.chanDB.FetchPayments()
query := channeldb.PaymentsQuery{
IndexOffset: req.IndexOffset,
MaxPayments: req.MaxPayments,
Reversed: req.Reversed,
IncludeIncomplete: req.IncludeIncomplete,
}
// If the maximum number of payments wasn't specified, then we'll
// default to return the maximal number of payments representable.
if req.MaxPayments == 0 {
query.MaxPayments = math.MaxUint64
}
paymentsQuerySlice, err := r.server.chanDB.QueryPayments(query)
if err != nil {
return nil, err
}
paymentsResp := &lnrpc.ListPaymentsResponse{}
for _, payment := range payments {
// To keep compatibility with the old API, we only return
// non-suceeded payments if requested.
if payment.Status != channeldb.StatusSucceeded &&
!req.IncludeIncomplete {
continue
}
paymentsResp := &lnrpc.ListPaymentsResponse{
LastIndexOffset: paymentsQuerySlice.LastIndexOffset,
FirstIndexOffset: paymentsQuerySlice.FirstIndexOffset,
}
for _, payment := range paymentsQuerySlice.Payments {
// Fetch the payment's route and preimage. If no HTLC was
// successful, an empty route and preimage will be used.
var (
@ -5231,6 +5242,7 @@ func (r *rpcServer) ListPayments(ctx context.Context,
PaymentRequest: string(payment.Info.PaymentRequest),
Status: status,
Htlcs: htlcs,
PaymentIndex: payment.SequenceNum,
})
}