Skip to content

Commit f5e899e

Browse files
committed
sqldb: fetch AMP subinvoices also with a limit.
We also make sure AMP subinvoices are also fetched with the pagination limit.
1 parent e600957 commit f5e899e

File tree

3 files changed

+75
-56
lines changed

3 files changed

+75
-56
lines changed

invoices/sql_store.go

Lines changed: 68 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -878,67 +878,81 @@ func (i *SQLStore) InvoicesSettledSince(ctx context.Context, idx uint64) (
878878

879879
// Now fetch all the AMP sub invoices that were settled since
880880
// the provided index.
881-
//
882-
// TODO(ziggie): Add limit query for AMP sub invoices.
883-
ampInvoices, err := i.db.FetchSettledAMPSubInvoices(
884-
ctx, sqlc.FetchSettledAMPSubInvoicesParams{
885-
SettleIndexGet: sqldb.SQLInt64(idx + 1),
886-
},
887-
)
888-
if err != nil {
889-
return err
890-
}
881+
err = queryWithLimit(int64(idx), false, i.opts.paginationLimit,
882+
//nolint:ll
883+
func(offset int64) (int, int64, error) {
884+
ampInvoices, err := i.db.FetchSettledAMPSubInvoices(
885+
ctx, sqlc.FetchSettledAMPSubInvoicesParams{
886+
SettleIndexGet: sqldb.SQLInt64(
887+
offset,
888+
),
889+
NumLimit: int32(i.opts.paginationLimit),
890+
},
891+
)
892+
if err != nil {
893+
return 0, 0, err
894+
}
891895

892-
for _, ampInvoice := range ampInvoices {
893-
// Convert the row to a sqlc.Invoice so we can use the
894-
// existing fetchInvoiceData function.
895-
sqlInvoice := sqlc.Invoice{
896-
ID: ampInvoice.ID,
897-
Hash: ampInvoice.Hash,
898-
Preimage: ampInvoice.Preimage,
899-
SettleIndex: ampInvoice.AmpSettleIndex,
900-
SettledAt: ampInvoice.AmpSettledAt,
901-
Memo: ampInvoice.Memo,
902-
AmountMsat: ampInvoice.AmountMsat,
903-
CltvDelta: ampInvoice.CltvDelta,
904-
Expiry: ampInvoice.Expiry,
905-
PaymentAddr: ampInvoice.PaymentAddr,
906-
PaymentRequest: ampInvoice.PaymentRequest,
907-
State: ampInvoice.State,
908-
AmountPaidMsat: ampInvoice.AmountPaidMsat,
909-
IsAmp: ampInvoice.IsAmp,
910-
IsHodl: ampInvoice.IsHodl,
911-
IsKeysend: ampInvoice.IsKeysend,
912-
CreatedAt: ampInvoice.CreatedAt.UTC(),
913-
}
896+
if len(ampInvoices) == 0 {
897+
return 0, 0, nil
898+
}
914899

915-
// Fetch the state and HTLCs for this AMP sub invoice.
916-
_, invoice, err := fetchInvoiceData(
917-
ctx, db, sqlInvoice,
918-
(*[32]byte)(ampInvoice.SetID), true,
919-
)
920-
if err != nil {
921-
return fmt.Errorf("unable to fetch "+
922-
"AMP invoice(id=%d) from db: %w",
923-
ampInvoice.ID, err)
924-
}
900+
for _, ampInvoice := range ampInvoices {
901+
// Convert the row to a sqlc.Invoice so we can use the
902+
// existing fetchInvoiceData function.
903+
sqlInvoice := sqlc.Invoice{
904+
ID: ampInvoice.ID,
905+
Hash: ampInvoice.Hash,
906+
Preimage: ampInvoice.Preimage,
907+
SettleIndex: ampInvoice.AmpSettleIndex,
908+
SettledAt: ampInvoice.AmpSettledAt,
909+
Memo: ampInvoice.Memo,
910+
AmountMsat: ampInvoice.AmountMsat,
911+
CltvDelta: ampInvoice.CltvDelta,
912+
Expiry: ampInvoice.Expiry,
913+
PaymentAddr: ampInvoice.PaymentAddr,
914+
PaymentRequest: ampInvoice.PaymentRequest,
915+
State: ampInvoice.State,
916+
AmountPaidMsat: ampInvoice.AmountPaidMsat,
917+
IsAmp: ampInvoice.IsAmp,
918+
IsHodl: ampInvoice.IsHodl,
919+
IsKeysend: ampInvoice.IsKeysend,
920+
CreatedAt: ampInvoice.CreatedAt.UTC(),
921+
}
922+
923+
// Fetch the state and HTLCs for this AMP sub invoice.
924+
_, invoice, err := fetchInvoiceData(
925+
ctx, db, sqlInvoice,
926+
(*[32]byte)(ampInvoice.SetID), true,
927+
)
928+
if err != nil {
929+
return 0, 0, fmt.Errorf("unable to fetch "+
930+
"AMP invoice(id=%d) from db: %w",
931+
ampInvoice.ID, err)
932+
}
925933

926-
invoices = append(invoices, *invoice)
934+
invoices = append(invoices, *invoice)
927935

928-
processedCount++
929-
if time.Since(lastLogTime) >=
930-
invoiceProgressLogInterval {
936+
processedCount++
937+
if time.Since(lastLogTime) >=
938+
invoiceProgressLogInterval {
931939

932-
log.Debugf("Processed %d settled invoices "+
933-
"including AMP sub invoices which "+
934-
"have a settle index greater than %v",
935-
processedCount, idx)
940+
log.Debugf("Processed %d settled invoices "+
941+
"including AMP sub invoices which "+
942+
"have a settle index greater than %v",
943+
processedCount, idx)
936944

937-
lastLogTime = time.Now()
938-
}
939-
}
945+
lastLogTime = time.Now()
946+
}
947+
}
940948

941-
return nil
949+
lastInv := ampInvoices[len(ampInvoices)-1]
950+
LastIndexOffset := lastInv.SettleIndex.Int64
951+
952+
return len(ampInvoices), LastIndexOffset, nil
953+
})
954+
955+
return err
942956
}, func() {
943957
invoices = nil
944958
})

sqldb/sqlc/amp_invoices.sql.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlc/queries/amp_invoices.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ WHERE (
5757
) AND (
5858
a.settle_index <= sqlc.narg('settle_index_let') OR
5959
sqlc.narg('settle_index_let') IS NULL
60-
);
60+
)
61+
ORDER BY a.settle_index ASC
62+
LIMIT @num_limit;
6163

6264
-- name: UpdateAMPSubInvoiceHTLCPreimage :execresult
6365
UPDATE amp_sub_invoice_htlcs AS a

0 commit comments

Comments
 (0)