Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion payments/db/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,13 @@ func (s *SQLStore) DeletePayment(paymentHash lntypes.Hash,

// If we are only deleting failed HTLCs, we delete them.
if failedHtlcsOnly {
return s.db.DeleteFailedAttempts(
return db.DeleteFailedAttempts(
ctx, fetchPayment.Payment.ID,
)
}

// Be careful to not use s.db here, because we are in a
// transaction, is there a way to make this more secure?
return db.DeletePayment(ctx, fetchPayment.Payment.ID)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these changes should be in the prior commit (was confusing to have it here since it seemed like it might be related to the change below but it is not)


}, func() {
Expand All @@ -569,3 +571,47 @@ func (s *SQLStore) DeletePayment(paymentHash lntypes.Hash,

return nil
}

// DeleteFailedAttempts removes all failed HTLCs from the db. It should
// be called for a given payment whenever all inflight htlcs are
// completed, and the payment has reached a final terminal state.
func (s *SQLStore) DeleteFailedAttempts(paymentHash lntypes.Hash) error {
// In case we are configured to keep failed payment attempts, we exit
// early.
if s.keepFailedPaymentAttempts {
return nil
}

ctx := context.TODO()

err := s.db.ExecTx(ctx, sqldb.WriteTxOpt(), func(db SQLQueries) error {
// We first fetch the payment to get the payment ID.
payment, err := db.FetchPayment(ctx, paymentHash[:])
if err != nil {
return fmt.Errorf("failed to fetch payment: %w", err)
}

completePayment, err := s.fetchPaymentWithCompleteData(
ctx, db, payment,
)
if err != nil {
return fmt.Errorf("failed to fetch payment with "+
"complete data: %w", err)
}

if err := completePayment.Status.removable(); err != nil {
return fmt.Errorf("payment %v cannot be deleted: %w",
paymentHash, err)
}

// Then we delete the failed attempts for this payment.
return db.DeleteFailedAttempts(ctx, payment.Payment.ID)
}, func() {
})
if err != nil {
return fmt.Errorf("failed to delete failed attempts for "+
"payment %v: %w", paymentHash, err)
}

return nil
}