From 17f529bfada6ba4cbda92e451481996f11cc0ea6 Mon Sep 17 00:00:00 2001 From: Carla Kirk-Cohen Date: Wed, 12 Jul 2023 16:08:20 -0400 Subject: [PATCH] invoices/test: rename newInvoice to reflect return value better Test utils currently have two different test invoices - a minimalist one that is used in a variety of test cases, and a customizable one that is used specifically for tests concerning invoice expiry. The latter is renamed and moved closer to the calling code to more clearly indicate its use. --- invoices/test_utils_test.go | 88 +++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/invoices/test_utils_test.go b/invoices/test_utils_test.go index e16b3ecc3..8bbcdc3ad 100644 --- a/invoices/test_utils_test.go +++ b/invoices/test_utils_test.go @@ -275,46 +275,6 @@ func getCircuitKey(htlcID uint64) invpkg.CircuitKey { } } -func newTestInvoice(t *testing.T, preimage lntypes.Preimage, - timestamp time.Time, expiry time.Duration) *invpkg.Invoice { - - if expiry == 0 { - expiry = time.Hour - } - - var payAddr [32]byte - if _, err := rand.Read(payAddr[:]); err != nil { - t.Fatalf("unable to generate payment addr: %v", err) - } - - rawInvoice, err := zpay32.NewInvoice( - testNetParams, - preimage.Hash(), - timestamp, - zpay32.Amount(testInvoiceAmount), - zpay32.Description(testInvoiceDescription), - zpay32.Expiry(expiry), - zpay32.PaymentAddr(payAddr), - ) - require.NoError(t, err, "Error while creating new invoice") - - paymentRequest, err := rawInvoice.Encode(testMessageSigner) - - require.NoError(t, err, "Error while encoding payment request") - - return &invpkg.Invoice{ - Terms: invpkg.ContractTerm{ - PaymentPreimage: &preimage, - PaymentAddr: payAddr, - Value: testInvoiceAmount, - Expiry: expiry, - Features: testFeatures, - }, - PaymentRequest: []byte(paymentRequest), - CreationDate: timestamp, - } -} - // timeout implements a test level timeout. func timeout() func() { done := make(chan struct{}) @@ -360,7 +320,7 @@ func generateInvoiceExpiryTestData( var preimage lntypes.Preimage binary.BigEndian.PutUint32(preimage[:4], uint32(offset+i)) expiry := time.Duration((i+offset)%24) * time.Hour - invoice := newTestInvoice( + invoice := newInvoiceExpiryTestInvoice( t, preimage, expiredCreationDate, expiry, ) testData.expiredInvoices[preimage.Hash()] = invoice @@ -370,13 +330,57 @@ func generateInvoiceExpiryTestData( var preimage lntypes.Preimage binary.BigEndian.PutUint32(preimage[4:], uint32(offset+i)) expiry := time.Duration((i+offset)%24) * time.Hour - invoice := newTestInvoice(t, preimage, now, expiry) + invoice := newInvoiceExpiryTestInvoice(t, preimage, now, expiry) testData.pendingInvoices[preimage.Hash()] = invoice } return testData } +// newInvoiceExpiryTestInvoice creates a test invoice with a randomly generated +// payment address and custom preimage and expiry details. It should be used in +// the case where tests require custom invoice expiry and unique payment +// hashes. +func newInvoiceExpiryTestInvoice(t *testing.T, preimage lntypes.Preimage, + timestamp time.Time, expiry time.Duration) *invpkg.Invoice { + + if expiry == 0 { + expiry = time.Hour + } + + var payAddr [32]byte + if _, err := rand.Read(payAddr[:]); err != nil { + t.Fatalf("unable to generate payment addr: %v", err) + } + + rawInvoice, err := zpay32.NewInvoice( + testNetParams, + preimage.Hash(), + timestamp, + zpay32.Amount(testInvoiceAmount), + zpay32.Description(testInvoiceDescription), + zpay32.Expiry(expiry), + zpay32.PaymentAddr(payAddr), + ) + require.NoError(t, err, "Error while creating new invoice") + + paymentRequest, err := rawInvoice.Encode(testMessageSigner) + + require.NoError(t, err, "Error while encoding payment request") + + return &invpkg.Invoice{ + Terms: invpkg.ContractTerm{ + PaymentPreimage: &preimage, + PaymentAddr: payAddr, + Value: testInvoiceAmount, + Expiry: expiry, + Features: testFeatures, + }, + PaymentRequest: []byte(paymentRequest), + CreationDate: timestamp, + } +} + // checkSettleResolution asserts the resolution is a settle with the correct // preimage. If successful, the HtlcSettleResolution is returned in case further // checks are desired.