invoices: make test HTLC circuit keys unique

This commit is contained in:
Andras Banki-Horvath committed 2024-03-01 10:08:08 +01:00
1 parent 3d56d2f1ee
commit 39211d25ed
1 file changed
+46 -21
+46 -21
View File
@@ -77,11 +77,13 @@ func randInvoice(value lnwire.MilliSatoshi) (*invpkg.Invoice, error) {
} }
// settleTestInvoice settles a test invoice. // settleTestInvoice settles a test invoice.
func settleTestInvoice(invoice *invpkg.Invoice, settleIndex uint64) { func settleTestInvoice(invoice *invpkg.Invoice, htlcID uint64,
settleIndex uint64) {
invoice.SettleDate = testNow invoice.SettleDate = testNow
invoice.AmtPaid = invoice.Terms.Value invoice.AmtPaid = invoice.Terms.Value
invoice.State = invpkg.ContractSettled invoice.State = invpkg.ContractSettled
invoice.Htlcs[models.CircuitKey{}] = &invpkg.InvoiceHTLC{ invoice.Htlcs[models.CircuitKey{HtlcID: htlcID}] = &invpkg.InvoiceHTLC{
Amt: invoice.Terms.Value, Amt: invoice.Terms.Value,
AcceptTime: testNow, AcceptTime: testNow,
ResolveTime: testNow, ResolveTime: testNow,
@@ -333,7 +335,7 @@ func testInvoiceWorkflowImpl(t *testing.T, test invWorkflowTest,
// now have the settled bit toggle to true and a non-default // now have the settled bit toggle to true and a non-default
// SettledDate // SettledDate
payAmt := fakeInvoice.Terms.Value * 2 payAmt := fakeInvoice.Terms.Value * 2
_, err = db.UpdateInvoice(ctxb, ref, nil, getUpdateInvoice(payAmt)) _, err = db.UpdateInvoice(ctxb, ref, nil, getUpdateInvoice(0, payAmt))
require.NoError(t, err, "unable to settle invoice") require.NoError(t, err, "unable to settle invoice")
dbInvoice2, err := db.LookupInvoice(ctxb, ref) dbInvoice2, err := db.LookupInvoice(ctxb, ref)
require.NoError(t, err, "unable to fetch invoice") require.NoError(t, err, "unable to fetch invoice")
@@ -902,8 +904,11 @@ func testInvoiceAddTimeSeries(t *testing.T,
_, err = db.InvoicesSettledSince(ctxb, 0) _, err = db.InvoicesSettledSince(ctxb, 0)
require.NoError(t, err) require.NoError(t, err)
var settledInvoices []invpkg.Invoice var (
var settleIndex uint64 = 1 settledInvoices []invpkg.Invoice
settleIndex uint64 = 1
htlcID uint64 = 0
)
// We'll now only settle the latter half of each of those invoices. // We'll now only settle the latter half of each of those invoices.
for i := 10; i < len(invoices); i++ { for i := 10; i < len(invoices); i++ {
invoice := &invoices[i] invoice := &invoices[i]
@@ -912,15 +917,18 @@ func testInvoiceAddTimeSeries(t *testing.T,
ref := invpkg.InvoiceRefByHash(paymentHash) ref := invpkg.InvoiceRefByHash(paymentHash)
_, err := db.UpdateInvoice( _, err := db.UpdateInvoice(
ctxb, ref, nil, getUpdateInvoice(invoice.Terms.Value), ctxb, ref, nil, getUpdateInvoice(
htlcID, invoice.Terms.Value,
),
) )
if err != nil { if err != nil {
t.Fatalf("unable to settle invoice: %v", err) t.Fatalf("unable to settle invoice: %v", err)
} }
// Create the settled invoice for the expectation set. // Create the settled invoice for the expectation set.
settleTestInvoice(invoice, settleIndex) settleTestInvoice(invoice, htlcID, settleIndex)
settleIndex++ settleIndex++
htlcID++
settledInvoices = append(settledInvoices, *invoice) settledInvoices = append(settledInvoices, *invoice)
} }
@@ -1158,7 +1166,11 @@ func testFetchPendingInvoices(t *testing.T,
require.Empty(t, pending) require.Empty(t, pending)
const numInvoices = 20 const numInvoices = 20
var settleIndex uint64 = 1 var (
settleIndex uint64 = 1
htlcID uint64 = 0
)
pendingInvoices := make(map[lntypes.Hash]invpkg.Invoice) pendingInvoices := make(map[lntypes.Hash]invpkg.Invoice)
for i := 1; i <= numInvoices; i++ { for i := 1; i <= numInvoices; i++ {
@@ -1182,11 +1194,14 @@ func testFetchPendingInvoices(t *testing.T,
} }
ref := invpkg.InvoiceRefByHash(paymentHash) ref := invpkg.InvoiceRefByHash(paymentHash)
_, err = db.UpdateInvoice(ctxb, ref, nil, getUpdateInvoice(amt)) _, err = db.UpdateInvoice(
ctxb, ref, nil, getUpdateInvoice(htlcID, amt),
)
require.NoError(t, err) require.NoError(t, err)
settleTestInvoice(invoice, settleIndex) settleTestInvoice(invoice, htlcID, settleIndex)
settleIndex++ settleIndex++
htlcID++
} }
// Fetch all pending invoices. // Fetch all pending invoices.
@@ -1219,7 +1234,7 @@ func testDuplicateSettleInvoice(t *testing.T,
// With the invoice in the DB, we'll now attempt to settle the invoice. // With the invoice in the DB, we'll now attempt to settle the invoice.
ref := invpkg.InvoiceRefByHash(payHash) ref := invpkg.InvoiceRefByHash(payHash)
dbInvoice, err := db.UpdateInvoice( dbInvoice, err := db.UpdateInvoice(
ctxb, ref, nil, getUpdateInvoice(amt), ctxb, ref, nil, getUpdateInvoice(0, amt),
) )
require.NoError(t, err, "unable to settle invoice") require.NoError(t, err, "unable to settle invoice")
@@ -1244,7 +1259,9 @@ func testDuplicateSettleInvoice(t *testing.T,
// If we try to settle the invoice again, then we should get the very // If we try to settle the invoice again, then we should get the very
// same invoice back, but with an error this time. // same invoice back, but with an error this time.
dbInvoice, err = db.UpdateInvoice(ctxb, ref, nil, getUpdateInvoice(amt)) dbInvoice, err = db.UpdateInvoice(
ctxb, ref, nil, getUpdateInvoice(1, amt),
)
require.ErrorIs(t, err, invpkg.ErrInvoiceAlreadySettled) require.ErrorIs(t, err, invpkg.ErrInvoiceAlreadySettled)
if dbInvoice == nil { if dbInvoice == nil {
@@ -1269,9 +1286,12 @@ func testQueryInvoices(t *testing.T,
// assume that the index of the invoice within the database is the same // assume that the index of the invoice within the database is the same
// as the amount of the invoice itself. // as the amount of the invoice itself.
const numInvoices = 50 const numInvoices = 50
var settleIndex uint64 = 1 var (
var invoices []invpkg.Invoice settleIndex uint64 = 1
var pendingInvoices []invpkg.Invoice htlcID uint64 = 0
invoices []invpkg.Invoice
pendingInvoices []invpkg.Invoice
)
ctxb := context.Background() ctxb := context.Background()
for i := 1; i <= numInvoices; i++ { for i := 1; i <= numInvoices; i++ {
@@ -1296,15 +1316,16 @@ func testQueryInvoices(t *testing.T,
if i%2 == 0 { if i%2 == 0 {
ref := invpkg.InvoiceRefByHash(paymentHash) ref := invpkg.InvoiceRefByHash(paymentHash)
_, err := db.UpdateInvoice( _, err := db.UpdateInvoice(
ctxb, ref, nil, getUpdateInvoice(amt), ctxb, ref, nil, getUpdateInvoice(htlcID, amt),
) )
if err != nil { if err != nil {
t.Fatalf("unable to settle invoice: %v", err) t.Fatalf("unable to settle invoice: %v", err)
} }
// Create the settled invoice for the expectation set. // Create the settled invoice for the expectation set.
settleTestInvoice(invoice, settleIndex) settleTestInvoice(invoice, htlcID, settleIndex)
settleIndex++ settleIndex++
htlcID++
} else { } else {
pendingInvoices = append(pendingInvoices, *invoice) pendingInvoices = append(pendingInvoices, *invoice)
} }
@@ -1662,7 +1683,9 @@ func testQueryInvoices(t *testing.T,
// getUpdateInvoice returns an invoice update callback that, when called, // getUpdateInvoice returns an invoice update callback that, when called,
// settles the invoice with the given amount. // settles the invoice with the given amount.
func getUpdateInvoice(amt lnwire.MilliSatoshi) invpkg.InvoiceUpdateCallback { func getUpdateInvoice(htlcID uint64,
amt lnwire.MilliSatoshi) invpkg.InvoiceUpdateCallback {
return func(invoice *invpkg.Invoice) (*invpkg.InvoiceUpdateDesc, return func(invoice *invpkg.Invoice) (*invpkg.InvoiceUpdateDesc,
error) { error) {
@@ -1672,7 +1695,7 @@ func getUpdateInvoice(amt lnwire.MilliSatoshi) invpkg.InvoiceUpdateCallback {
noRecords := make(record.CustomSet) noRecords := make(record.CustomSet)
htlcs := map[models.CircuitKey]*invpkg.HtlcAcceptDesc{ htlcs := map[models.CircuitKey]*invpkg.HtlcAcceptDesc{
{}: { {HtlcID: htlcID}: {
Amt: amt, Amt: amt,
CustomRecords: noRecords, CustomRecords: noRecords,
}, },
@@ -2366,7 +2389,7 @@ func testUnexpectedInvoicePreimage(t *testing.T,
// payment hash against which to validate the preimage. // payment hash against which to validate the preimage.
_, err = db.UpdateInvoice( _, err = db.UpdateInvoice(
ctxb, invpkg.InvoiceRefByAddr(invoice.Terms.PaymentAddr), nil, ctxb, invpkg.InvoiceRefByAddr(invoice.Terms.PaymentAddr), nil,
getUpdateInvoice(invoice.Terms.Value), getUpdateInvoice(0, invoice.Terms.Value),
) )
// Assert that we get ErrUnexpectedInvoicePreimage. // Assert that we get ErrUnexpectedInvoicePreimage.
@@ -2501,7 +2524,9 @@ func testDeleteInvoices(t *testing.T,
if i == 1 { if i == 1 {
invoice, err = db.UpdateInvoice( invoice, err = db.UpdateInvoice(
ctxb, invpkg.InvoiceRefByHash(paymentHash), nil, ctxb, invpkg.InvoiceRefByHash(paymentHash), nil,
getUpdateInvoice(invoice.Terms.Value), getUpdateInvoice(
uint64(i), invoice.Terms.Value,
),
) )
require.NoError(t, err, "unable to settle invoice") require.NoError(t, err, "unable to settle invoice")
} }