Merge pull request #5660 from LN-Zap/upstream/feat/delete-payment

add DeletePayment that allows to delete a specific payment or its failed HTLCs
This commit is contained in:
Oliver Gugger 2021-09-13 14:56:51 +02:00 committed by GitHub
commit 45343e4454
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 1227 additions and 705 deletions

View File

@ -558,10 +558,7 @@ func TestPaymentControlDeletePayments(t *testing.T) {
db, cleanup, err := MakeTestDB()
defer cleanup()
if err != nil {
t.Fatalf("unable to init db: %v", err)
}
require.NoError(t, err, "unable to init db")
pControl := NewPaymentControl(db)
@ -569,203 +566,130 @@ func TestPaymentControlDeletePayments(t *testing.T) {
// 1. A payment with two failed attempts.
// 2. A Payment with one failed and one settled attempt.
// 3. A payment with one failed and one in-flight attempt.
attemptID := uint64(0)
for i := 0; i < 3; i++ {
info, attempt, preimg, err := genInfo()
if err != nil {
t.Fatalf("unable to generate htlc message: %v", err)
}
attempt.AttemptID = attemptID
attemptID++
// Init the payment.
err = pControl.InitPayment(info.PaymentIdentifier, info)
if err != nil {
t.Fatalf("unable to send htlc message: %v", err)
}
// Register and fail the first attempt for all three payments.
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
if err != nil {
t.Fatalf("unable to send htlc message: %v", err)
}
htlcFailure := HTLCFailUnreadable
_, err = pControl.FailAttempt(
info.PaymentIdentifier, attempt.AttemptID,
&HTLCFailInfo{
Reason: htlcFailure,
},
)
if err != nil {
t.Fatalf("unable to fail htlc: %v", err)
}
// Depending on the test case, fail or succeed the next
// attempt.
attempt.AttemptID = attemptID
attemptID++
_, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
if err != nil {
t.Fatalf("unable to send htlc message: %v", err)
}
switch i {
// Fail the attempt and the payment overall.
case 0:
htlcFailure := HTLCFailUnreadable
_, err = pControl.FailAttempt(
info.PaymentIdentifier, attempt.AttemptID,
&HTLCFailInfo{
Reason: htlcFailure,
},
)
if err != nil {
t.Fatalf("unable to fail htlc: %v", err)
}
failReason := FailureReasonNoRoute
_, err = pControl.Fail(info.PaymentIdentifier, failReason)
if err != nil {
t.Fatalf("unable to fail payment hash: %v", err)
}
// Settle the attempt
case 1:
_, err := pControl.SettleAttempt(
info.PaymentIdentifier, attempt.AttemptID,
&HTLCSettleInfo{
Preimage: preimg,
},
)
if err != nil {
t.Fatalf("error shouldn't have been received, got: %v", err)
}
// We leave the attmpet in-flight by doing nothing.
case 2:
}
payments := []*payment{
{status: StatusFailed},
{status: StatusSucceeded},
{status: StatusInFlight},
}
type fetchedPayment struct {
status PaymentStatus
htlcs int
}
assertPayments := func(expPayments []fetchedPayment) {
t.Helper()
dbPayments, err := db.FetchPayments()
if err != nil {
t.Fatal(err)
}
if len(dbPayments) != len(expPayments) {
t.Fatalf("expected %d payments, got %d",
len(expPayments), len(dbPayments))
}
for i := range dbPayments {
if dbPayments[i].Status != expPayments[i].status {
t.Fatalf("unexpected payment status")
}
if len(dbPayments[i].HTLCs) != expPayments[i].htlcs {
t.Fatalf("unexpected number of htlcs")
}
}
}
// Use helper function to register the test payments in the data and
// populate the data to the payments slice.
createTestPayments(t, pControl, payments)
// Check that all payments are there as we added them.
assertPayments([]fetchedPayment{
{
status: StatusFailed,
htlcs: 2,
},
{
status: StatusSucceeded,
htlcs: 2,
},
{
status: StatusInFlight,
htlcs: 2,
},
})
assertPayments(t, db, payments)
// Delete HTLC attempts for failed payments only.
if err := db.DeletePayments(true, true); err != nil {
t.Fatal(err)
}
require.NoError(t, db.DeletePayments(true, true))
// The failed payment is the only altered one.
assertPayments([]fetchedPayment{
{
status: StatusFailed,
htlcs: 0,
},
{
status: StatusSucceeded,
htlcs: 2,
},
{
status: StatusInFlight,
htlcs: 2,
},
})
payments[0].htlcs = 0
assertPayments(t, db, payments)
// Delete failed attempts for all payments.
if err := db.DeletePayments(false, true); err != nil {
t.Fatal(err)
}
require.NoError(t, db.DeletePayments(false, true))
// The failed attempts should be deleted, except for the in-flight
// payment, that shouldn't be altered until it has completed.
assertPayments([]fetchedPayment{
{
status: StatusFailed,
htlcs: 0,
},
{
status: StatusSucceeded,
htlcs: 1,
},
{
status: StatusInFlight,
htlcs: 2,
},
})
payments[1].htlcs = 1
assertPayments(t, db, payments)
// Now delete all failed payments.
if err := db.DeletePayments(true, false); err != nil {
t.Fatal(err)
}
require.NoError(t, db.DeletePayments(true, false))
assertPayments([]fetchedPayment{
{
status: StatusSucceeded,
htlcs: 1,
},
{
status: StatusInFlight,
htlcs: 2,
},
})
assertPayments(t, db, payments[1:])
// Finally delete all completed payments.
if err := db.DeletePayments(false, false); err != nil {
t.Fatal(err)
require.NoError(t, db.DeletePayments(false, false))
assertPayments(t, db, payments[2:])
}
// TestPaymentControlDeleteSinglePayment tests that DeletePayment correcly
// deletes information about a completed payment from the database.
func TestPaymentControlDeleteSinglePayment(t *testing.T) {
t.Parallel()
db, cleanup, err := MakeTestDB()
defer cleanup()
require.NoError(t, err, "unable to init db")
pControl := NewPaymentControl(db)
// Register four payments:
// All payments will have one failed HTLC attempt and one HTLC attempt
// according to its final status.
// 1. A payment with two failed attempts.
// 2. Another payment with two failed attempts.
// 3. A Payment with one failed and one settled attempt.
// 4. A payment with one failed and one in-flight attempt.
// Initiate payments, which is a slice of payment that is used as
// template to create the corresponding test payments in the database.
//
// Note: The payment id and number of htlc attempts of each payment will
// be added to this slice when creating the payments below.
// This allows the slice to be used directly for testing purposes.
payments := []*payment{
{status: StatusFailed},
{status: StatusFailed},
{status: StatusSucceeded},
{status: StatusInFlight},
}
assertPayments([]fetchedPayment{
{
status: StatusInFlight,
htlcs: 2,
},
})
// Use helper function to register the test payments in the data and
// populate the data to the payments slice.
createTestPayments(t, pControl, payments)
// Check that all payments are there as we added them.
assertPayments(t, db, payments)
// Delete HTLC attempts for first payment only.
require.NoError(t, db.DeletePayment(payments[0].id, true))
// The first payment is the only altered one as its failed HTLC should
// have been removed but is still present as payment.
payments[0].htlcs = 0
assertPayments(t, db, payments)
// Delete the first payment completely.
require.NoError(t, db.DeletePayment(payments[0].id, false))
// The first payment should have been deleted.
assertPayments(t, db, payments[1:])
// Now delete the second payment completely.
require.NoError(t, db.DeletePayment(payments[1].id, false))
// The Second payment should have been deleted.
assertPayments(t, db, payments[2:])
// Delete failed HTLC attempts for the third payment.
require.NoError(t, db.DeletePayment(payments[2].id, true))
// Only the successful HTLC attempt should be left for the third payment.
payments[2].htlcs = 1
assertPayments(t, db, payments[2:])
// Now delete the third payment completely.
require.NoError(t, db.DeletePayment(payments[2].id, false))
// Only the last payment should be left.
assertPayments(t, db, payments[3:])
// Deleting HTLC attempts from InFlight payments should not work and an
// error returned.
require.Error(t, db.DeletePayment(payments[3].id, true))
// The payment is InFlight and therefore should not have been altered.
assertPayments(t, db, payments[3:])
// Finally deleting the InFlight payment should also not work and an
// error returned.
require.Error(t, db.DeletePayment(payments[3].id, false))
// The payment is InFlight and therefore should not have been altered.
assertPayments(t, db, payments[3:])
}
// TestPaymentControlMultiShard checks the ability of payment control to
@ -1275,3 +1199,123 @@ func assertNoIndex(t *testing.T, p *PaymentControl, seqNr uint64) {
_, err := fetchPaymentIndexEntry(t, p, seqNr)
require.Equal(t, errNoSequenceNrIndex, err)
}
// payment is a helper structure that holds basic information on a test payment,
// such as the payment id, the status and the total number of HTLCs attempted.
type payment struct {
id lntypes.Hash
status PaymentStatus
htlcs int
}
// createTestPayments registers payments depending on the provided statuses in
// the payments slice. Each payment will receive one failed HTLC and another
// HTLC depending on the final status of the payment provided.
func createTestPayments(t *testing.T, p *PaymentControl, payments []*payment) {
attemptID := uint64(0)
for i := 0; i < len(payments); i++ {
info, attempt, preimg, err := genInfo()
require.NoError(t, err, "unable to generate htlc message")
// Set the payment id accordingly in the payments slice.
payments[i].id = info.PaymentIdentifier
attempt.AttemptID = attemptID
attemptID++
// Init the payment.
err = p.InitPayment(info.PaymentIdentifier, info)
require.NoError(t, err, "unable to send htlc message")
// Register and fail the first attempt for all payments.
_, err = p.RegisterAttempt(info.PaymentIdentifier, attempt)
require.NoError(t, err, "unable to send htlc message")
htlcFailure := HTLCFailUnreadable
_, err = p.FailAttempt(
info.PaymentIdentifier, attempt.AttemptID,
&HTLCFailInfo{
Reason: htlcFailure,
},
)
require.NoError(t, err, "unable to fail htlc")
// Increase the HTLC counter in the payments slice for the
// failed attempt.
payments[i].htlcs++
// Depending on the test case, fail or succeed the next
// attempt.
attempt.AttemptID = attemptID
attemptID++
_, err = p.RegisterAttempt(info.PaymentIdentifier, attempt)
require.NoError(t, err, "unable to send htlc message")
switch payments[i].status {
// Fail the attempt and the payment overall.
case StatusFailed:
htlcFailure := HTLCFailUnreadable
_, err = p.FailAttempt(
info.PaymentIdentifier, attempt.AttemptID,
&HTLCFailInfo{
Reason: htlcFailure,
},
)
require.NoError(t, err, "unable to fail htlc")
failReason := FailureReasonNoRoute
_, err = p.Fail(info.PaymentIdentifier,
failReason)
require.NoError(t, err, "unable to fail payment hash")
// Settle the attempt
case StatusSucceeded:
_, err := p.SettleAttempt(
info.PaymentIdentifier, attempt.AttemptID,
&HTLCSettleInfo{
Preimage: preimg,
},
)
require.NoError(t, err, "no error should have been "+
"received from settling a htlc attempt")
// We leave the attempt in-flight by doing nothing.
case StatusInFlight:
}
// Increase the HTLC counter in the payments slice for any
// attempt above.
payments[i].htlcs++
}
}
// assertPayments is a helper function that given a slice of payment and
// indices for the slice asserts that exactly the same payments in the
// slice for the provided indices exist when fetching payments from the
// database.
func assertPayments(t *testing.T, db *DB, payments []*payment) {
t.Helper()
dbPayments, err := db.FetchPayments()
require.NoError(t, err, "could not fetch payments from db")
// Make sure that the number of fetched payments is the same
// as expected.
require.Len(t, dbPayments, len(payments), "unexpected number of payments")
// Convert fetched payments of type MPPayment to our helper structure.
p := make([]*payment, len(dbPayments))
for i, dbPayment := range dbPayments {
p[i] = &payment{
id: dbPayment.Info.PaymentIdentifier,
status: dbPayment.Status,
htlcs: len(dbPayment.HTLCs),
}
}
// Check that each payment we want to assert exists in the database.
require.Equal(t, payments, p)
}

View File

@ -477,6 +477,37 @@ func readHtlcFailInfo(b []byte) (*HTLCFailInfo, error) {
return deserializeHTLCFailInfo(r)
}
// fetchFailedHtlcKeys retrieves the bucket keys of all failed HTLCs of a
// payment bucket.
func fetchFailedHtlcKeys(bucket kvdb.RBucket) ([][]byte, error) {
htlcsBucket := bucket.NestedReadBucket(paymentHtlcsBucket)
var htlcs []HTLCAttempt
var err error
if htlcsBucket != nil {
htlcs, err = fetchHtlcAttempts(htlcsBucket)
if err != nil {
return nil, err
}
}
// Now iterate though them and save the bucket keys for the failed
// HTLCs.
var htlcKeys [][]byte
for _, h := range htlcs {
if h.Failure == nil {
continue
}
htlcKeyBytes := make([]byte, 8)
binary.BigEndian.PutUint64(htlcKeyBytes, h.AttemptID)
htlcKeys = append(htlcKeys, htlcKeyBytes)
}
return htlcKeys, nil
}
// PaymentsQuery represents a query to the payments database starting or ending
// at a certain offset index. The number of retrieved records can be limited.
type PaymentsQuery struct {
@ -698,6 +729,94 @@ func fetchPaymentWithSequenceNumber(tx kvdb.RTx, paymentHash lntypes.Hash,
return duplicatePayment, nil
}
// DeletePayment deletes a payment from the DB given its payment hash. If
// failedHtlcsOnly is set, only failed HTLC attempts of the payment will be
// deleted.
func (d *DB) DeletePayment(paymentHash lntypes.Hash, failedHtlcsOnly bool) error { // nolint:interfacer
return kvdb.Update(d, func(tx kvdb.RwTx) error {
payments := tx.ReadWriteBucket(paymentsRootBucket)
if payments == nil {
return nil
}
bucket := payments.NestedReadWriteBucket(paymentHash[:])
if bucket == nil {
return fmt.Errorf("non bucket element in payments " +
"bucket")
}
// If the status is InFlight, we cannot safely delete
// the payment information, so we return early.
paymentStatus, err := fetchPaymentStatus(bucket)
if err != nil {
return err
}
// If the status is InFlight, we cannot safely delete
// the payment information, so we return an error.
if paymentStatus == StatusInFlight {
return fmt.Errorf("payment '%v' has status InFlight "+
"and therefore cannot be deleted",
paymentHash.String())
}
// Delete the failed HTLC attempts we found.
if failedHtlcsOnly {
toDelete, err := fetchFailedHtlcKeys(bucket)
if err != nil {
return err
}
htlcsBucket := bucket.NestedReadWriteBucket(
paymentHtlcsBucket,
)
for _, htlcID := range toDelete {
err = htlcsBucket.Delete(
htlcBucketKey(htlcAttemptInfoKey, htlcID),
)
if err != nil {
return err
}
err = htlcsBucket.Delete(
htlcBucketKey(htlcFailInfoKey, htlcID),
)
if err != nil {
return err
}
err = htlcsBucket.Delete(
htlcBucketKey(htlcSettleInfoKey, htlcID),
)
if err != nil {
return err
}
}
return nil
}
seqNrs, err := fetchSequenceNumbers(bucket)
if err != nil {
return err
}
if err := payments.DeleteNestedBucket(paymentHash[:]); err != nil {
return err
}
indexBucket := tx.ReadWriteBucket(paymentsIndexBucket)
for _, k := range seqNrs {
if err := indexBucket.Delete(k); err != nil {
return err
}
}
return nil
}, func() {})
}
// DeletePayments deletes all completed and failed payments from the DB. If
// failedOnly is set, only failed payments will be considered for deletion. If
// failedHtlsOnly is set, the payment itself won't be deleted, only failed HTLC
@ -752,34 +871,9 @@ func (d *DB) DeletePayments(failedOnly, failedHtlcsOnly bool) error {
// If we are only deleting failed HTLCs, fetch them.
if failedHtlcsOnly {
htlcsBucket := bucket.NestedReadBucket(
paymentHtlcsBucket,
)
var htlcs []HTLCAttempt
if htlcsBucket != nil {
htlcs, err = fetchHtlcAttempts(
htlcsBucket,
)
if err != nil {
return err
}
}
// Now iterate though them and save the bucket
// keys for the failed HTLCs.
var toDelete [][]byte
for _, h := range htlcs {
if h.Failure == nil {
continue
}
htlcIDBytes := make([]byte, 8)
binary.BigEndian.PutUint64(
htlcIDBytes, h.AttemptID,
)
toDelete = append(toDelete, htlcIDBytes)
toDelete, err := fetchFailedHtlcKeys(bucket)
if err != nil {
return err
}
hash, err := lntypes.MakeHash(k)

View File

@ -68,6 +68,8 @@ proposed channel type is used.
avoid misleading error messages from dependent services if they use `After`
systemd option.
* [Delete a specific payment, or its failed HTLCs](https://github.com/lightningnetwork/lnd/pull/5660).
### Batched channel funding
[Multiple channels can now be opened in a single

File diff suppressed because it is too large Load Diff

View File

@ -1357,6 +1357,42 @@ func local_request_Lightning_ListPayments_0(ctx context.Context, marshaler runti
}
var (
filter_Lightning_DeletePayment_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)}
)
func request_Lightning_DeletePayment_0(ctx context.Context, marshaler runtime.Marshaler, client LightningClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq DeletePaymentRequest
var metadata runtime.ServerMetadata
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Lightning_DeletePayment_0); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
msg, err := client.DeletePayment(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD))
return msg, metadata, err
}
func local_request_Lightning_DeletePayment_0(ctx context.Context, marshaler runtime.Marshaler, server LightningServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq DeletePaymentRequest
var metadata runtime.ServerMetadata
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Lightning_DeletePayment_0); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
msg, err := server.DeletePayment(ctx, &protoReq)
return msg, metadata, err
}
var (
filter_Lightning_DeleteAllPayments_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)}
)
@ -2910,6 +2946,29 @@ func RegisterLightningHandlerServer(ctx context.Context, mux *runtime.ServeMux,
})
mux.Handle("DELETE", pattern_Lightning_DeletePayment_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
var stream runtime.ServerTransportStream
ctx = grpc.NewContextWithServerTransportStream(ctx, &stream)
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req, "/lnrpc.Lightning/DeletePayment", runtime.WithHTTPPathPattern("/v1/payment"))
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
resp, md, err := local_request_Lightning_DeletePayment_0(rctx, inboundMarshaler, server, req, pathParams)
md.HeaderMD, md.TrailerMD = metadata.Join(md.HeaderMD, stream.Header()), metadata.Join(md.TrailerMD, stream.Trailer())
ctx = runtime.NewServerMetadataContext(ctx, md)
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
forward_Lightning_DeletePayment_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
})
mux.Handle("DELETE", pattern_Lightning_DeleteAllPayments_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
@ -4165,6 +4224,26 @@ func RegisterLightningHandlerClient(ctx context.Context, mux *runtime.ServeMux,
})
mux.Handle("DELETE", pattern_Lightning_DeletePayment_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
rctx, err := runtime.AnnotateContext(ctx, mux, req, "/lnrpc.Lightning/DeletePayment", runtime.WithHTTPPathPattern("/v1/payment"))
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
resp, md, err := request_Lightning_DeletePayment_0(rctx, inboundMarshaler, client, req, pathParams)
ctx = runtime.NewServerMetadataContext(ctx, md)
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
forward_Lightning_DeletePayment_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
})
mux.Handle("DELETE", pattern_Lightning_DeleteAllPayments_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
@ -4683,6 +4762,8 @@ var (
pattern_Lightning_ListPayments_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1}, []string{"v1", "payments"}, ""))
pattern_Lightning_DeletePayment_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1}, []string{"v1", "payment"}, ""))
pattern_Lightning_DeleteAllPayments_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1}, []string{"v1", "payments"}, ""))
pattern_Lightning_DescribeGraph_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1}, []string{"v1", "graph"}, ""))
@ -4803,6 +4884,8 @@ var (
forward_Lightning_ListPayments_0 = runtime.ForwardResponseMessage
forward_Lightning_DeletePayment_0 = runtime.ForwardResponseMessage
forward_Lightning_DeleteAllPayments_0 = runtime.ForwardResponseMessage
forward_Lightning_DescribeGraph_0 = runtime.ForwardResponseMessage

View File

@ -1000,6 +1000,31 @@ func RegisterLightningJSONCallbacks(registry map[string]func(ctx context.Context
callback(string(respBytes), nil)
}
registry["lnrpc.Lightning.DeletePayment"] = func(ctx context.Context,
conn *grpc.ClientConn, reqJSON string, callback func(string, error)) {
req := &DeletePaymentRequest{}
err := marshaler.Unmarshal([]byte(reqJSON), req)
if err != nil {
callback("", err)
return
}
client := NewLightningClient(conn)
resp, err := client.DeletePayment(ctx, req)
if err != nil {
callback("", err)
return
}
respBytes, err := marshaler.Marshal(resp)
if err != nil {
callback("", err)
return
}
callback(string(respBytes), nil)
}
registry["lnrpc.Lightning.DeleteAllPayments"] = func(ctx context.Context,
conn *grpc.ClientConn, reqJSON string, callback func(string, error)) {

View File

@ -340,7 +340,14 @@ service Lightning {
rpc ListPayments (ListPaymentsRequest) returns (ListPaymentsResponse);
/*
DeleteAllPayments deletes all outgoing payments from DB.
DeletePayment deletes an outgoing payment from DB. Note that it will not
attempt to delete an In-Flight payment, since that would be unsafe.
*/
rpc DeletePayment (DeletePaymentRequest) returns (DeletePaymentResponse);
/*
DeleteAllPayments deletes all outgoing payments from DB. Note that it will
not attempt to delete In-Flight payments, since that would be unsafe.
*/
rpc DeleteAllPayments (DeleteAllPaymentsRequest)
returns (DeleteAllPaymentsResponse);
@ -3448,6 +3455,16 @@ message ListPaymentsResponse {
uint64 last_index_offset = 3;
}
message DeletePaymentRequest {
// Payment hash to delete.
bytes payment_hash = 1;
/*
Only delete failed HTLCs from the payment, not the payment itself.
*/
bool failed_htlcs_only = 2;
}
message DeleteAllPaymentsRequest {
// Only delete failed payments.
bool failed_payments_only = 1;
@ -3458,6 +3475,9 @@ message DeleteAllPaymentsRequest {
bool failed_htlcs_only = 2;
}
message DeletePaymentResponse {
}
message DeleteAllPaymentsResponse {
}

View File

@ -1688,6 +1688,46 @@
]
}
},
"/v1/payment": {
"delete": {
"summary": "DeletePayment deletes an outgoing payment from DB. Note that it will not\nattempt to delete an In-Flight payment, since that would be unsafe.",
"operationId": "Lightning_DeletePayment",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/lnrpcDeletePaymentResponse"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"parameters": [
{
"name": "payment_hash",
"description": "Payment hash to delete.",
"in": "query",
"required": false,
"type": "string",
"format": "byte"
},
{
"name": "failed_htlcs_only",
"description": "Only delete failed HTLCs from the payment, not the payment itself.",
"in": "query",
"required": false,
"type": "boolean"
}
],
"tags": [
"Lightning"
]
}
},
"/v1/payments": {
"get": {
"summary": "lncli: `listpayments`\nListPayments returns a list of all outgoing payments.",
@ -1743,7 +1783,7 @@
]
},
"delete": {
"summary": "DeleteAllPayments deletes all outgoing payments from DB.",
"summary": "DeleteAllPayments deletes all outgoing payments from DB. Note that it will\nnot attempt to delete In-Flight payments, since that would be unsafe.",
"operationId": "Lightning_DeleteAllPayments",
"responses": {
"200": {
@ -3688,6 +3728,9 @@
}
}
},
"lnrpcDeletePaymentResponse": {
"type": "object"
},
"lnrpcDisconnectPeerResponse": {
"type": "object"
},

View File

@ -95,6 +95,8 @@ http:
get: "/v1/invoices/subscribe"
- selector: lnrpc.Lightning.DecodePayReq
get: "/v1/payreq/{pay_req}"
- selector: lnrpc.Lightning.DeletePayment
delete: "/v1/payment"
- selector: lnrpc.Lightning.ListPayments
get: "/v1/payments"
- selector: lnrpc.Lightning.DeleteAllPayments

View File

@ -249,7 +249,12 @@ type LightningClient interface {
//ListPayments returns a list of all outgoing payments.
ListPayments(ctx context.Context, in *ListPaymentsRequest, opts ...grpc.CallOption) (*ListPaymentsResponse, error)
//
//DeleteAllPayments deletes all outgoing payments from DB.
//DeletePayment deletes an outgoing payment from DB. Note that it will not
//attempt to delete an In-Flight payment, since that would be unsafe.
DeletePayment(ctx context.Context, in *DeletePaymentRequest, opts ...grpc.CallOption) (*DeletePaymentResponse, error)
//
//DeleteAllPayments deletes all outgoing payments from DB. Note that it will
//not attempt to delete In-Flight payments, since that would be unsafe.
DeleteAllPayments(ctx context.Context, in *DeleteAllPaymentsRequest, opts ...grpc.CallOption) (*DeleteAllPaymentsResponse, error)
// lncli: `describegraph`
//DescribeGraph returns a description of the latest graph state from the
@ -937,6 +942,15 @@ func (c *lightningClient) ListPayments(ctx context.Context, in *ListPaymentsRequ
return out, nil
}
func (c *lightningClient) DeletePayment(ctx context.Context, in *DeletePaymentRequest, opts ...grpc.CallOption) (*DeletePaymentResponse, error) {
out := new(DeletePaymentResponse)
err := c.cc.Invoke(ctx, "/lnrpc.Lightning/DeletePayment", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *lightningClient) DeleteAllPayments(ctx context.Context, in *DeleteAllPaymentsRequest, opts ...grpc.CallOption) (*DeleteAllPaymentsResponse, error) {
out := new(DeleteAllPaymentsResponse)
err := c.cc.Invoke(ctx, "/lnrpc.Lightning/DeleteAllPayments", in, out, opts...)
@ -1416,7 +1430,12 @@ type LightningServer interface {
//ListPayments returns a list of all outgoing payments.
ListPayments(context.Context, *ListPaymentsRequest) (*ListPaymentsResponse, error)
//
//DeleteAllPayments deletes all outgoing payments from DB.
//DeletePayment deletes an outgoing payment from DB. Note that it will not
//attempt to delete an In-Flight payment, since that would be unsafe.
DeletePayment(context.Context, *DeletePaymentRequest) (*DeletePaymentResponse, error)
//
//DeleteAllPayments deletes all outgoing payments from DB. Note that it will
//not attempt to delete In-Flight payments, since that would be unsafe.
DeleteAllPayments(context.Context, *DeleteAllPaymentsRequest) (*DeleteAllPaymentsResponse, error)
// lncli: `describegraph`
//DescribeGraph returns a description of the latest graph state from the
@ -1667,6 +1686,9 @@ func (UnimplementedLightningServer) DecodePayReq(context.Context, *PayReqString)
func (UnimplementedLightningServer) ListPayments(context.Context, *ListPaymentsRequest) (*ListPaymentsResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method ListPayments not implemented")
}
func (UnimplementedLightningServer) DeletePayment(context.Context, *DeletePaymentRequest) (*DeletePaymentResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method DeletePayment not implemented")
}
func (UnimplementedLightningServer) DeleteAllPayments(context.Context, *DeleteAllPaymentsRequest) (*DeleteAllPaymentsResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method DeleteAllPayments not implemented")
}
@ -2472,6 +2494,24 @@ func _Lightning_ListPayments_Handler(srv interface{}, ctx context.Context, dec f
return interceptor(ctx, in, info, handler)
}
func _Lightning_DeletePayment_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(DeletePaymentRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(LightningServer).DeletePayment(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/lnrpc.Lightning/DeletePayment",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(LightningServer).DeletePayment(ctx, req.(*DeletePaymentRequest))
}
return interceptor(ctx, in, info, handler)
}
func _Lightning_DeleteAllPayments_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(DeleteAllPaymentsRequest)
if err := dec(in); err != nil {
@ -2997,6 +3037,10 @@ var Lightning_ServiceDesc = grpc.ServiceDesc{
MethodName: "ListPayments",
Handler: _Lightning_ListPayments_Handler,
},
{
MethodName: "DeletePayment",
Handler: _Lightning_DeletePayment_Handler,
},
{
MethodName: "DeleteAllPayments",
Handler: _Lightning_DeleteAllPayments_Handler,

View File

@ -442,6 +442,10 @@ func MainRPCServerPermissions() map[string][]bakery.Op {
Entity: "offchain",
Action: "read",
}},
"/lnrpc.Lightning/DeletePayment": {{
Entity: "offchain",
Action: "write",
}},
"/lnrpc.Lightning/DeleteAllPayments": {{
Entity: "offchain",
Action: "write",
@ -5934,6 +5938,31 @@ func (r *rpcServer) ListPayments(ctx context.Context,
return paymentsResp, nil
}
// DeletePayment deletes a payment from the DB given its payment hash. If
// failedHtlcsOnly is set, only failed HTLC attempts of the payment will be
// deleted.
func (r *rpcServer) DeletePayment(ctx context.Context,
req *lnrpc.DeletePaymentRequest) (
*lnrpc.DeletePaymentResponse, error) {
hash, err := lntypes.MakeHash(req.PaymentHash)
if err != nil {
return nil, err
}
rpcsLog.Infof("[DeletePayment] payment_identifier=%v, "+
"failed_htlcs_only=%v", hash, req.FailedHtlcsOnly)
err = r.server.chanStateDB.DeletePayment(
hash, req.FailedHtlcsOnly,
)
if err != nil {
return nil, err
}
return &lnrpc.DeletePaymentResponse{}, nil
}
// DeleteAllPayments deletes all outgoing payments from DB.
func (r *rpcServer) DeleteAllPayments(ctx context.Context,
req *lnrpc.DeleteAllPaymentsRequest) (