Skip to content

[feature]: Garbage Collect Failed Payments Similar to canceled invoices #9707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ziggie1984 opened this issue Apr 13, 2025 · 2 comments
Open
Labels
enhancement Improvements to existing features / behaviour

Comments

@ziggie1984
Copy link
Collaborator

Currently failed payments can only be deleted when actively triggered by the user via the deletepayments rpc. We should provide a config setting which let's the user automatically garbage-collect the payments similar to how its done with invoices:

; gc-canceled-invoices-on-startup=false
; gc-canceled-invoices-on-the-fly=false
@ziggie1984 ziggie1984 added the enhancement Improvements to existing features / behaviour label Apr 13, 2025
@AbhinavAnand241201
Copy link

AbhinavAnand241201 commented Apr 19, 2025

hello community .....i'm a beginner in this repo yet i'm trying to think abt the approaches or the things we could do here ...

Two main cleanup scenarios could be

  1. Startup Cleanup:

    • run when LND starts
    • bulk cleanup of accumulated failed payments
    • this is less time-critical
  2. On-the-fly Cleanup:

    • run immediately when payments fail
    • real-time cleanup
    • must be efficient enough ..
      the main thing is to optimise the database size and not let it grow unnecessarily.......so we are doing the GC of failed payments .....
      also note that we need to be careful with the in-flight payments ....that is ... in a switch case
      case StatusInFlight: return ErrPaymentInFlight ...we cant cleanup those ....
      ..one key differnce with respect to invoices is ....in case of payments we need to do better state handeling....invoice garbage collection is a little differeent than payment one

the way invoice GC is implemented in the repo is

Configuration Options:
Startup cleanup (GcCanceledInvoicesOnStartup)
On-the-fly cleanup (GcCanceledInvoicesOnTheFly)

Cleanup Triggers:
On startup if configured,
After invoice cancellation if on-the-fly GC is enabled,

only deletes canceled invoices and maintains or preserves non-canceled invoices....

Deletes from multiple indexes (invoice index, payment address index, add index)
i'm thinking, working on this issue locally ...and getting more familiar with the codebase.

@AbhinavAnand241201
Copy link

hey..@ziggie1984 ...i've worked out a simple approach for this now ..
the codebase currently has two options
1.Manual deletion via RPCs like DeletePayment and DeleteAllPayments.
and
2. // keepFailedPaymentAttempts determines whether failed htlc attempts // are kept on disk or removed to save space. keepFailedPaymentAttempts bool
my solution implements automatic garbage collection for failed payments, mirroring the existing approach used for canceled invoices. add two primary garbage collection mechanisms:

"Startup Garbage" Collection: Cleans up all failed payments when LND starts
"On-the-fly Garbage" Collection: Removes failed payments immediately when they fail.....

Database Scanning: i'm using a two-phase approach when scanning the database to avoid cursor invalidation issues:

First, it will collect all payment hashes that meet the deletion criteria
Then, delete each payment in a separate operation
Status Verification: Before deleting any payment, it verify its status to ensure only failed payments are removed:

if bytes.Equal(paymentStatus, []byte(StatusFailed)) { paymentHashes = append(paymentHashes, hash) }

configuration Integration: i will be integrating the new options with LND's existing configuration system, making them available via:

Command-line flags
Configuration file settings
Programmatic API options

the idea is implementing two core garbage collection functions in the channeldb package:

GarbageCollectFailedPayments(): Scans and removes all failed payments from the database
DeleteFailedPayment(): Removes a specific failed payment, using for for on-the-fly collection

We will also need to modifiy the payment lifecycle to support on-the-fly garbage collection:
When a payment reaches a terminal failed state, we check if on-the-fly GC is enabled
If enabled, we delete the entire payment record
If disabled but keepFailedPaymentAttempts=false, we only delete the failed attempt.

one safetyguard is

Only payments with a definitive "StatusFailed" are eligible for garbage collection, no in-flights..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvements to existing features / behaviour
Projects
None yet
Development

No branches or pull requests

2 participants