channeldb: add method removable to decide removing payments
This commit adds a new method, `removable`, to help decide whether deleting a payment is allowed.
This commit is contained in:
1 parent
de33c3e009
commit
a6f4f0dfc9
2 files changed
+37
-8
No files matched your search
@@ -75,3 +75,31 @@ func (ps PaymentStatus) initializable() error {
|
|||||||
return fmt.Errorf("%w: %v", ErrUnknownPaymentStatus, ps)
|
return fmt.Errorf("%w: %v", ErrUnknownPaymentStatus, ps)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// removable returns an error to specify whether deleting the payment with its
|
||||||
|
// current status is allowed. A payment cannot be safely deleted if it has
|
||||||
|
// inflight HTLCs.
|
||||||
|
func (ps PaymentStatus) removable() error {
|
||||||
|
switch ps {
|
||||||
|
// The payment has been created but has no HTLCs and can be removed.
|
||||||
|
case StatusInitiated:
|
||||||
|
return nil
|
||||||
|
|
||||||
|
// There are still inflight HTLCs and the payment needs to wait for the
|
||||||
|
// final outcomes.
|
||||||
|
case StatusInFlight:
|
||||||
|
return ErrPaymentInFlight
|
||||||
|
|
||||||
|
// The payment has been attempted and is succeeded and is allowed to be
|
||||||
|
// removed.
|
||||||
|
case StatusSucceeded:
|
||||||
|
return nil
|
||||||
|
|
||||||
|
// Failed payments are allowed to be removed.
|
||||||
|
case StatusFailed:
|
||||||
|
return nil
|
||||||
|
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("%w: %v", ErrUnknownPaymentStatus, ps)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -783,12 +783,12 @@ func (d *DB) DeletePayment(paymentHash lntypes.Hash,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the status is InFlight, we cannot safely delete
|
// If the payment has inflight HTLCs, we cannot safely delete
|
||||||
// the payment information, so we return an error.
|
// the payment information, so we return an error.
|
||||||
if paymentStatus == StatusInFlight {
|
if err := paymentStatus.removable(); err != nil {
|
||||||
return fmt.Errorf("payment '%v' has status InFlight "+
|
return fmt.Errorf("payment '%v' has inflight HTLCs"+
|
||||||
"and therefore cannot be deleted",
|
"and therefore cannot be deleted: %w",
|
||||||
paymentHash.String())
|
paymentHash.String(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete the failed HTLC attempts we found.
|
// Delete the failed HTLC attempts we found.
|
||||||
@@ -888,9 +888,10 @@ func (d *DB) DeletePayments(failedOnly, failedHtlcsOnly bool) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the status is InFlight, we cannot safely delete
|
// If the payment has inflight HTLCs, we cannot safely
|
||||||
// the payment information, so we return early.
|
// delete the payment information, so we return an nil
|
||||||
if paymentStatus == StatusInFlight {
|
// to skip it.
|
||||||
|
if err := paymentStatus.removable(); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user