Skip to content

Commit

Permalink
feat: delete old ldk payments (#1092)
Browse files Browse the repository at this point in the history
* feat: delete old ldk payments

* chore: use duration instead of manual second amount, log payment_id of deleted payments
  • Loading branch information
rolznz authored Feb 14, 2025
1 parent 697a660 commit 6ca11b2
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lnclient/ldk/ldk.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type LDKService struct {
}

const resetRouterKey = "ResetRouter"
const maxInvoiceExpiry = 24 * time.Hour

func NewLDKService(ctx context.Context, cfg config.Config, eventPublisher events.EventPublisher, mnemonic, workDir string, network string, vssToken string) (result lnclient.LNClient, err error) {
if mnemonic == "" || workDir == "" {
Expand Down Expand Up @@ -382,6 +383,9 @@ func NewLDKService(ctx context.Context, cfg config.Config, eventPublisher events
"status": node.Status(),
"duration": math.Ceil(time.Since(syncStartTime).Seconds()),
}).Info("LDK node synced successfully")

// delete old payments while node is not syncing
ls.deleteOldLDKPayments()
}
}
}()
Expand Down Expand Up @@ -649,6 +653,10 @@ func (ls *LDKService) getMaxSpendable() uint64 {

func (ls *LDKService) MakeInvoice(ctx context.Context, amount int64, description string, descriptionHash string, expiry int64) (transaction *lnclient.Transaction, err error) {

if time.Duration(expiry)*time.Second > maxInvoiceExpiry {
return nil, errors.New("Expiry is too long")
}

maxReceivable := ls.getMaxReceivable()

if amount > maxReceivable {
Expand Down Expand Up @@ -1631,6 +1639,25 @@ func (ls *LDKService) GetStorageDir() (string, error) {
return "ldk/storage", nil
}

func (ls *LDKService) deleteOldLDKPayments() {
payments := ls.node.ListPayments()

now := time.Now()
for _, payment := range payments {
paymentCreatedAt := time.Unix(int64(payment.CreatedAt), 0)
if paymentCreatedAt.Add(maxInvoiceExpiry).Before(now) {
logger.Logger.WithFields(logrus.Fields{
"created_at": paymentCreatedAt,
"payment_id": payment.Id,
}).Debug("Deleting old payment")
err := ls.node.RemovePayment(payment.Id)
if err != nil {
logger.Logger.WithError(err).WithField("id", payment.Id).Error("failed to delete old payment")
}
}
}
}

func deleteOldLDKLogs(ldkLogDir string) {
logger.Logger.WithField("ldkLogDir", ldkLogDir).Debug("Deleting old LDK logs")
files, err := os.ReadDir(ldkLogDir)
Expand Down

0 comments on commit 6ca11b2

Please sign in to comment.