Skip to content

Commit 3754730

Browse files
committed
loop: paginate fetching payments when running the cost migration
1 parent 3d1d3eb commit 3754730

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

cost_migration.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ import (
1414
)
1515

1616
const (
17+
// costMigrationID is the identifier for the cost migration.
1718
costMigrationID = "cost_migration"
19+
20+
// paymentBatchSize is the maximum number of payments we'll fetch in
21+
// one go.
22+
paymentBatchSize = 1000
1823
)
1924

2025
// CalculateLoopOutCost calculates the total cost of a loop out swap. It will
@@ -132,18 +137,30 @@ func MigrateLoopOutCosts(ctx context.Context, lnd lndclient.LndServices,
132137
return err
133138
}
134139

135-
// Next we fetch all payments from LND.
136-
payments, err := lnd.Client.ListPayments(
137-
ctx, lndclient.ListPaymentsRequest{},
138-
)
139-
if err != nil {
140-
return err
141-
}
142-
143140
// Gather payment fees to a map for easier lookup.
144141
paymentFees := make(map[lntypes.Hash]lnwire.MilliSatoshi)
145-
for _, payment := range payments.Payments {
146-
paymentFees[payment.Hash] = payment.Fee
142+
offset := uint64(0)
143+
144+
for {
145+
payments, err := lnd.Client.ListPayments(
146+
ctx, lndclient.ListPaymentsRequest{
147+
Offset: offset,
148+
MaxPayments: paymentBatchSize,
149+
},
150+
)
151+
if err != nil {
152+
return err
153+
}
154+
155+
if len(payments.Payments) == 0 {
156+
break
157+
}
158+
159+
for _, payment := range payments.Payments {
160+
paymentFees[payment.Hash] = payment.Fee
161+
}
162+
163+
offset = payments.LastIndexOffset + 1
147164
}
148165

149166
// Now we'll calculate the cost for each swap and finally update the

loopd/daemon.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
412412
// Run the costs migration.
413413
err = loop.MigrateLoopOutCosts(d.mainCtx, d.lnd.LndServices, swapDb)
414414
if err != nil {
415+
log.Errorf("Cost migration failed: %v", err)
416+
415417
return err
416418
}
417419

test/lightning_client_mock.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,24 @@ func (h *mockLightningClient) ListInvoices(_ context.Context,
278278

279279
// ListPayments makes a paginated call to our list payments endpoint.
280280
func (h *mockLightningClient) ListPayments(_ context.Context,
281-
_ lndclient.ListPaymentsRequest) (*lndclient.ListPaymentsResponse,
281+
req lndclient.ListPaymentsRequest) (*lndclient.ListPaymentsResponse,
282282
error) {
283283

284+
if req.Offset >= uint64(len(h.lnd.Payments)) {
285+
return &lndclient.ListPaymentsResponse{}, nil
286+
}
287+
288+
lastIndexOffset := req.Offset + req.MaxPayments
289+
if lastIndexOffset > uint64(len(h.lnd.Payments)) {
290+
lastIndexOffset = uint64(len(h.lnd.Payments))
291+
}
292+
293+
result := h.lnd.Payments[req.Offset:lastIndexOffset]
294+
284295
return &lndclient.ListPaymentsResponse{
285-
Payments: h.lnd.Payments,
296+
Payments: result,
297+
FirstIndexOffset: req.Offset,
298+
LastIndexOffset: lastIndexOffset - 1,
286299
}, nil
287300
}
288301

0 commit comments

Comments
 (0)