Skip to content

Commit 9298133

Browse files
bhandrasRoasbeef
authored andcommitted
sqldb+invoices: synchronize SQL invoice updater behavior with KV version
Previously SQL invoice updater ignored the set ID hint when updating an AMP invoice resulting in update subscriptions returning all of the AMP state as well as all AMP HTLCs. This commit synchornizes behavior with the KV implementation such that we now only return relevant AMP state and HTLCs when updating an AMP invoice.
1 parent b3dc3ed commit 9298133

File tree

4 files changed

+106
-18
lines changed

4 files changed

+106
-18
lines changed

invoices/sql_store.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strconv"
1111
"time"
1212

13+
"github.com/davecgh/go-spew/spew"
1314
"github.com/lightningnetwork/lnd/channeldb/models"
1415
"github.com/lightningnetwork/lnd/clock"
1516
"github.com/lightningnetwork/lnd/lntypes"
@@ -46,6 +47,9 @@ type SQLInvoiceQueries interface { //nolint:interfacebloat
4647
GetInvoice(ctx context.Context,
4748
arg sqlc.GetInvoiceParams) ([]sqlc.Invoice, error)
4849

50+
GetInvoiceBySetID(ctx context.Context, setID []byte) ([]sqlc.Invoice,
51+
error)
52+
4953
GetInvoiceFeatures(ctx context.Context,
5054
invoiceID int64) ([]sqlc.InvoiceFeature, error)
5155

@@ -343,16 +347,31 @@ func (i *SQLStore) fetchInvoice(ctx context.Context,
343347
params.SetID = ref.SetID()[:]
344348
}
345349

346-
rows, err := db.GetInvoice(ctx, params)
350+
var (
351+
rows []sqlc.Invoice
352+
err error
353+
)
354+
355+
// We need to split the query based on how we intend to look up the
356+
// invoice. If only the set ID is given then we want to have an exact
357+
// match on the set ID. If other fields are given, we want to match on
358+
// those fields and the set ID but with a less strict join condition.
359+
if params.Hash == nil && params.PaymentAddr == nil &&
360+
params.SetID != nil {
361+
362+
rows, err = db.GetInvoiceBySetID(ctx, params.SetID)
363+
} else {
364+
rows, err = db.GetInvoice(ctx, params)
365+
}
347366
switch {
348367
case len(rows) == 0:
349368
return nil, ErrInvoiceNotFound
350369

351370
case len(rows) > 1:
352371
// In case the reference is ambiguous, meaning it matches more
353372
// than one invoice, we'll return an error.
354-
return nil, fmt.Errorf("ambiguous invoice ref: %s",
355-
ref.String())
373+
return nil, fmt.Errorf("ambiguous invoice ref: %s: %s",
374+
ref.String(), spew.Sdump(rows))
356375

357376
case err != nil:
358377
return nil, fmt.Errorf("unable to fetch invoice: %w", err)
@@ -1308,13 +1327,24 @@ func (s *sqlInvoiceUpdater) Finalize(_ UpdateType) error {
13081327
// invoice and is therefore atomic. The fields to update are controlled by the
13091328
// supplied callback.
13101329
func (i *SQLStore) UpdateInvoice(ctx context.Context, ref InvoiceRef,
1311-
_ *SetID, callback InvoiceUpdateCallback) (
1330+
setID *SetID, callback InvoiceUpdateCallback) (
13121331
*Invoice, error) {
13131332

13141333
var updatedInvoice *Invoice
13151334

13161335
txOpt := SQLInvoiceQueriesTxOptions{readOnly: false}
13171336
txErr := i.db.ExecTx(ctx, &txOpt, func(db SQLInvoiceQueries) error {
1337+
if setID != nil {
1338+
// Make sure to use the set ID if this is an AMP update.
1339+
var setIDBytes [32]byte
1340+
copy(setIDBytes[:], setID[:])
1341+
ref.setID = &setIDBytes
1342+
1343+
// If we're updating an AMP invoice, we'll also only
1344+
// need to fetch the HTLCs for the given set ID.
1345+
ref.refModifier = HtlcSetOnlyModifier
1346+
}
1347+
13181348
invoice, err := i.fetchInvoice(ctx, db, ref)
13191349
if err != nil {
13201350
return err

sqldb/sqlc/invoices.sql.go

Lines changed: 60 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlc/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlc/queries/invoices.sql

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ WHERE invoice_id = $1;
2626
-- name: GetInvoice :many
2727
SELECT i.*
2828
FROM invoices i
29-
LEFT JOIN amp_sub_invoices a on i.id = a.invoice_id
29+
LEFT JOIN amp_sub_invoices a
30+
ON i.id = a.invoice_id
31+
AND (
32+
a.set_id = sqlc.narg('set_id') OR sqlc.narg('set_id') IS NULL
33+
)
3034
WHERE (
3135
i.id = sqlc.narg('add_index') OR
3236
sqlc.narg('add_index') IS NULL
@@ -39,13 +43,16 @@ WHERE (
3943
) AND (
4044
i.payment_addr = sqlc.narg('payment_addr') OR
4145
sqlc.narg('payment_addr') IS NULL
42-
) AND (
43-
a.set_id = sqlc.narg('set_id') OR
44-
sqlc.narg('set_id') IS NULL
4546
)
4647
GROUP BY i.id
4748
LIMIT 2;
4849

50+
-- name: GetInvoiceBySetID :many
51+
SELECT i.*
52+
FROM invoices i
53+
INNER JOIN amp_sub_invoices a
54+
ON i.id = a.invoice_id AND a.set_id = $1;
55+
4956
-- name: FilterInvoices :many
5057
SELECT
5158
invoices.*

0 commit comments

Comments
 (0)