mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-12-11 05:11:22 +01:00
rpcserver+channeldb: delete only failed payments if requested
This commit is contained in:
@@ -388,6 +388,8 @@ func TestPaymentControlDeleteNonInFligt(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var numSuccess, numInflight int
|
||||||
|
|
||||||
for _, p := range payments {
|
for _, p := range payments {
|
||||||
info, attempt, preimg, err := genInfo()
|
info, attempt, preimg, err := genInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -454,11 +456,15 @@ func TestPaymentControlDeleteNonInFligt(t *testing.T) {
|
|||||||
assertPaymentInfo(
|
assertPaymentInfo(
|
||||||
t, pControl, info.PaymentHash, info, nil, htlc,
|
t, pControl, info.PaymentHash, info, nil, htlc,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
numSuccess++
|
||||||
} else {
|
} else {
|
||||||
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight)
|
||||||
assertPaymentInfo(
|
assertPaymentInfo(
|
||||||
t, pControl, info.PaymentHash, info, nil, htlc,
|
t, pControl, info.PaymentHash, info, nil, htlc,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
numInflight++
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the payment is intended to have a duplicate payment, we
|
// If the payment is intended to have a duplicate payment, we
|
||||||
@@ -469,27 +475,66 @@ func TestPaymentControlDeleteNonInFligt(t *testing.T) {
|
|||||||
uint64(duplicateSeqNr), preimg,
|
uint64(duplicateSeqNr), preimg,
|
||||||
)
|
)
|
||||||
duplicateSeqNr++
|
duplicateSeqNr++
|
||||||
|
numSuccess++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete payments.
|
// Delete all failed payments.
|
||||||
if err := db.DeletePayments(); err != nil {
|
if err := db.DeletePayments(true); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// This should leave the in-flight payment.
|
// This should leave the succeeded and in-flight payments.
|
||||||
dbPayments, err := db.FetchPayments()
|
dbPayments, err := db.FetchPayments()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(dbPayments) != 1 {
|
if len(dbPayments) != numSuccess+numInflight {
|
||||||
t.Fatalf("expected one payment, got %d", len(dbPayments))
|
t.Fatalf("expected %d payments, got %d",
|
||||||
|
numSuccess+numInflight, len(dbPayments))
|
||||||
}
|
}
|
||||||
|
|
||||||
status := dbPayments[0].Status
|
var s, i int
|
||||||
if status != StatusInFlight {
|
for _, p := range dbPayments {
|
||||||
t.Fatalf("expected in-fligth status, got %v", status)
|
fmt.Println("fetch payment has status", p.Status)
|
||||||
|
switch p.Status {
|
||||||
|
case StatusSucceeded:
|
||||||
|
s++
|
||||||
|
case StatusInFlight:
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if s != numSuccess {
|
||||||
|
t.Fatalf("expected %d succeeded payments , got %d",
|
||||||
|
numSuccess, s)
|
||||||
|
}
|
||||||
|
if i != numInflight {
|
||||||
|
t.Fatalf("expected %d in-flight payments, got %d",
|
||||||
|
numInflight, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now delete all payments except in-flight.
|
||||||
|
if err := db.DeletePayments(false); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// This should leave the in-flight payment.
|
||||||
|
dbPayments, err = db.FetchPayments()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(dbPayments) != numInflight {
|
||||||
|
t.Fatalf("expected %d payments, got %d", numInflight,
|
||||||
|
len(dbPayments))
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range dbPayments {
|
||||||
|
if p.Status != StatusInFlight {
|
||||||
|
t.Fatalf("expected in-fligth status, got %v", p.Status)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally, check that we only have a single index left in the payment
|
// Finally, check that we only have a single index left in the payment
|
||||||
|
|||||||
@@ -677,7 +677,7 @@ func fetchPaymentWithSequenceNumber(tx kvdb.RTx, paymentHash lntypes.Hash,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DeletePayments deletes all completed and failed payments from the DB.
|
// DeletePayments deletes all completed and failed payments from the DB.
|
||||||
func (db *DB) DeletePayments() error {
|
func (db *DB) DeletePayments(failedOnly bool) error {
|
||||||
return kvdb.Update(db, func(tx kvdb.RwTx) error {
|
return kvdb.Update(db, func(tx kvdb.RwTx) error {
|
||||||
payments := tx.ReadWriteBucket(paymentsRootBucket)
|
payments := tx.ReadWriteBucket(paymentsRootBucket)
|
||||||
if payments == nil {
|
if payments == nil {
|
||||||
@@ -715,6 +715,12 @@ func (db *DB) DeletePayments() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we requested to only delete failed payments, we
|
||||||
|
// can return if this one is not.
|
||||||
|
if failedOnly && paymentStatus != StatusFailed {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Add the bucket to the set of buckets we can delete.
|
// Add the bucket to the set of buckets we can delete.
|
||||||
deleteBuckets = append(deleteBuckets, k)
|
deleteBuckets = append(deleteBuckets, k)
|
||||||
|
|
||||||
|
|||||||
11
rpcserver.go
11
rpcserver.go
@@ -5710,11 +5710,16 @@ func (r *rpcServer) ListPayments(ctx context.Context,
|
|||||||
|
|
||||||
// DeleteAllPayments deletes all outgoing payments from DB.
|
// DeleteAllPayments deletes all outgoing payments from DB.
|
||||||
func (r *rpcServer) DeleteAllPayments(ctx context.Context,
|
func (r *rpcServer) DeleteAllPayments(ctx context.Context,
|
||||||
_ *lnrpc.DeleteAllPaymentsRequest) (*lnrpc.DeleteAllPaymentsResponse, error) {
|
req *lnrpc.DeleteAllPaymentsRequest) (
|
||||||
|
*lnrpc.DeleteAllPaymentsResponse, error) {
|
||||||
|
|
||||||
rpcsLog.Debugf("[DeleteAllPayments]")
|
rpcsLog.Debugf("[DeleteAllPayments] failed_payments_only=%v",
|
||||||
|
req.FailedPaymentsOnly)
|
||||||
|
|
||||||
if err := r.server.remoteChanDB.DeletePayments(); err != nil {
|
err := r.server.remoteChanDB.DeletePayments(
|
||||||
|
req.FailedPaymentsOnly,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user