2022-11-30 12:00:37 +01:00
|
|
|
package invoices_test
|
2019-11-20 18:06:51 +01:00
|
|
|
|
|
|
|
import (
|
2020-05-22 00:37:39 +02:00
|
|
|
"crypto/rand"
|
2019-12-09 17:50:11 +01:00
|
|
|
"encoding/binary"
|
2019-11-20 18:06:51 +01:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime/pprof"
|
2021-05-11 08:45:30 +02:00
|
|
|
"sync"
|
2019-11-20 18:06:51 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
|
2019-11-20 18:06:51 +01:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2021-09-23 16:54:27 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2021-05-11 08:45:30 +02:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2019-11-25 11:46:29 +01:00
|
|
|
"github.com/lightningnetwork/lnd/clock"
|
2022-11-30 12:00:37 +01:00
|
|
|
invpkg "github.com/lightningnetwork/lnd/invoices"
|
2019-11-20 18:06:51 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/lightningnetwork/lnd/record"
|
|
|
|
"github.com/lightningnetwork/lnd/zpay32"
|
2021-03-25 03:48:59 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-11-20 18:06:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockPayload struct {
|
2019-12-05 12:55:17 +01:00
|
|
|
mpp *record.MPP
|
2021-03-03 18:54:11 +01:00
|
|
|
amp *record.AMP
|
2019-12-05 12:55:17 +01:00
|
|
|
customRecords record.CustomSet
|
2022-01-11 14:15:23 +01:00
|
|
|
metadata []byte
|
2019-11-20 18:06:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *mockPayload) MultiPath() *record.MPP {
|
|
|
|
return p.mpp
|
|
|
|
}
|
|
|
|
|
2021-03-03 18:54:11 +01:00
|
|
|
func (p *mockPayload) AMPRecord() *record.AMP {
|
|
|
|
return p.amp
|
|
|
|
}
|
|
|
|
|
2019-11-20 18:06:51 +01:00
|
|
|
func (p *mockPayload) CustomRecords() record.CustomSet {
|
2019-12-05 12:55:17 +01:00
|
|
|
// This function should always return a map instance, but for mock
|
|
|
|
// configuration we do accept nil.
|
|
|
|
if p.customRecords == nil {
|
|
|
|
return make(record.CustomSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.customRecords
|
2019-11-20 18:06:51 +01:00
|
|
|
}
|
|
|
|
|
2022-01-11 14:15:23 +01:00
|
|
|
func (p *mockPayload) Metadata() []byte {
|
|
|
|
return p.metadata
|
|
|
|
}
|
|
|
|
|
2022-11-30 12:00:37 +01:00
|
|
|
type mockChainNotifier struct {
|
|
|
|
chainntnfs.ChainNotifier
|
|
|
|
|
|
|
|
blockChan chan *chainntnfs.BlockEpoch
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockNotifier() *mockChainNotifier {
|
|
|
|
return &mockChainNotifier{
|
|
|
|
blockChan: make(chan *chainntnfs.BlockEpoch),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterBlockEpochNtfn mocks a block epoch notification, using the mock's
|
|
|
|
// block channel to deliver blocks to the client.
|
|
|
|
func (m *mockChainNotifier) RegisterBlockEpochNtfn(*chainntnfs.BlockEpoch) (
|
|
|
|
*chainntnfs.BlockEpochEvent, error) {
|
|
|
|
|
|
|
|
return &chainntnfs.BlockEpochEvent{
|
|
|
|
Epochs: m.blockChan,
|
|
|
|
Cancel: func() {},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-22 20:37:23 +01:00
|
|
|
const (
|
|
|
|
testHtlcExpiry = uint32(5)
|
|
|
|
|
|
|
|
testInvoiceCltvDelta = uint32(4)
|
|
|
|
|
|
|
|
testFinalCltvRejectDelta = int32(4)
|
|
|
|
|
|
|
|
testCurrentHeight = int32(1)
|
|
|
|
)
|
|
|
|
|
2019-11-20 18:06:51 +01:00
|
|
|
var (
|
|
|
|
testTimeout = 5 * time.Second
|
|
|
|
|
|
|
|
testTime = time.Date(2018, time.February, 2, 14, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
testInvoicePreimage = lntypes.Preimage{1}
|
|
|
|
|
|
|
|
testInvoicePaymentHash = testInvoicePreimage.Hash()
|
|
|
|
|
|
|
|
testPrivKeyBytes, _ = hex.DecodeString(
|
2022-02-23 14:48:00 +01:00
|
|
|
"e126f68f7eafcc8b74f54d269fe206be715000f94dac067d1c04a8ca3b2d" +
|
|
|
|
"b734",
|
|
|
|
)
|
2019-11-20 18:06:51 +01:00
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
testPrivKey, _ = btcec.PrivKeyFromBytes(testPrivKeyBytes)
|
2019-11-20 18:06:51 +01:00
|
|
|
|
|
|
|
testInvoiceDescription = "coffee"
|
|
|
|
|
|
|
|
testInvoiceAmount = lnwire.MilliSatoshi(100000)
|
|
|
|
|
|
|
|
testNetParams = &chaincfg.MainNetParams
|
|
|
|
|
|
|
|
testMessageSigner = zpay32.MessageSigner{
|
2021-09-23 16:54:27 +02:00
|
|
|
SignCompact: func(msg []byte) ([]byte, error) {
|
|
|
|
hash := chainhash.HashB(msg)
|
2022-02-23 14:48:00 +01:00
|
|
|
sig, err := ecdsa.SignCompact(testPrivKey, hash, true)
|
2019-11-20 18:06:51 +01:00
|
|
|
if err != nil {
|
2022-11-30 12:00:37 +01:00
|
|
|
return nil, fmt.Errorf("can't sign the "+
|
|
|
|
"message: %v", err)
|
2019-11-20 18:06:51 +01:00
|
|
|
}
|
|
|
|
return sig, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testFeatures = lnwire.NewFeatureVector(
|
|
|
|
nil, lnwire.Features,
|
|
|
|
)
|
|
|
|
|
|
|
|
testPayload = &mockPayload{}
|
2019-12-09 17:50:11 +01:00
|
|
|
|
|
|
|
testInvoiceCreationDate = testTime
|
2019-11-20 18:06:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type testContext struct {
|
2023-10-26 11:35:31 +02:00
|
|
|
idb invpkg.InvoiceDB
|
2022-11-30 12:00:37 +01:00
|
|
|
registry *invpkg.InvoiceRegistry
|
2021-05-11 08:45:33 +02:00
|
|
|
notifier *mockChainNotifier
|
2019-11-25 11:46:29 +01:00
|
|
|
clock *clock.TestClock
|
2019-11-20 18:06:51 +01:00
|
|
|
|
2022-08-27 09:06:03 +02:00
|
|
|
t *testing.T
|
2019-11-20 18:06:51 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 12:00:37 +01:00
|
|
|
func defaultRegistryConfig() invpkg.RegistryConfig {
|
|
|
|
return invpkg.RegistryConfig{
|
|
|
|
FinalCltvRejectDelta: testFinalCltvRejectDelta,
|
|
|
|
HtlcHoldDuration: 30 * time.Second,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestContext(t *testing.T,
|
2023-10-26 11:35:31 +02:00
|
|
|
registryCfg *invpkg.RegistryConfig,
|
|
|
|
makeDB func(t *testing.T) (invpkg.InvoiceDB,
|
|
|
|
*clock.TestClock)) *testContext {
|
2022-11-30 12:00:37 +01:00
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
2023-10-26 11:35:31 +02:00
|
|
|
idb, clock := makeDB(t)
|
2021-05-11 08:45:33 +02:00
|
|
|
notifier := newMockNotifier()
|
|
|
|
|
2022-11-30 12:00:37 +01:00
|
|
|
expiryWatcher := invpkg.NewInvoiceExpiryWatcher(
|
2021-05-11 08:45:33 +02:00
|
|
|
clock, 0, uint32(testCurrentHeight), nil, notifier,
|
2021-05-11 08:45:29 +02:00
|
|
|
)
|
2019-12-09 17:50:11 +01:00
|
|
|
|
2022-11-30 12:00:37 +01:00
|
|
|
cfg := defaultRegistryConfig()
|
|
|
|
if registryCfg != nil {
|
|
|
|
cfg = *registryCfg
|
2019-11-20 18:06:51 +01:00
|
|
|
}
|
2022-11-30 12:00:37 +01:00
|
|
|
cfg.Clock = clock
|
|
|
|
|
|
|
|
// Instantiate and start the invoice ctx.registry.
|
|
|
|
registry := invpkg.NewRegistry(idb, expiryWatcher, &cfg)
|
2019-11-20 18:06:51 +01:00
|
|
|
|
2023-10-26 11:35:31 +02:00
|
|
|
require.NoError(t, registry.Start())
|
2022-08-27 09:06:03 +02:00
|
|
|
t.Cleanup(func() {
|
|
|
|
require.NoError(t, registry.Stop())
|
|
|
|
})
|
2019-11-20 18:06:51 +01:00
|
|
|
|
|
|
|
ctx := testContext{
|
2022-11-30 12:00:37 +01:00
|
|
|
idb: idb,
|
2019-11-20 18:06:51 +01:00
|
|
|
registry: registry,
|
2021-05-11 08:45:33 +02:00
|
|
|
notifier: notifier,
|
2019-11-20 18:06:51 +01:00
|
|
|
clock: clock,
|
|
|
|
t: t,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ctx
|
|
|
|
}
|
|
|
|
|
2022-11-30 12:00:37 +01:00
|
|
|
func getCircuitKey(htlcID uint64) invpkg.CircuitKey {
|
|
|
|
return invpkg.CircuitKey{
|
2019-11-20 18:06:51 +01:00
|
|
|
ChanID: lnwire.ShortChannelID{
|
|
|
|
BlockHeight: 1, TxIndex: 2, TxPosition: 3,
|
|
|
|
},
|
|
|
|
HtlcID: htlcID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-12 22:35:51 +02:00
|
|
|
// newInvoice returns an invoice that can be used for testing, using the
|
|
|
|
// constant values defined above (deep copied if necessary).
|
|
|
|
//
|
|
|
|
// Note that this invoice *does not* have a payment address set. It will
|
|
|
|
// create a regular invoice with a preimage is hodl is false, and a hodl
|
|
|
|
// invoice with no preimage otherwise.
|
|
|
|
func newInvoice(t *testing.T, hodl bool) *invpkg.Invoice {
|
|
|
|
invoice := &invpkg.Invoice{
|
|
|
|
Terms: invpkg.ContractTerm{
|
2023-07-12 23:08:07 +02:00
|
|
|
Value: testInvoiceAmount,
|
2023-07-12 22:35:51 +02:00
|
|
|
Expiry: time.Hour,
|
|
|
|
Features: testFeatures.Clone(),
|
|
|
|
},
|
|
|
|
CreationDate: testInvoiceCreationDate,
|
|
|
|
}
|
|
|
|
|
|
|
|
// If creating a hodl invoice, we don't include a preimage.
|
|
|
|
if hodl {
|
|
|
|
invoice.HodlInvoice = true
|
|
|
|
return invoice
|
|
|
|
}
|
|
|
|
|
|
|
|
preimage, err := lntypes.MakePreimage(
|
|
|
|
testInvoicePreimage[:],
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
invoice.Terms.PaymentPreimage = &preimage
|
|
|
|
|
|
|
|
return invoice
|
|
|
|
}
|
|
|
|
|
2019-11-20 18:06:51 +01:00
|
|
|
// timeout implements a test level timeout.
|
|
|
|
func timeout() func() {
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
2024-01-22 17:54:23 +01:00
|
|
|
case <-time.After(10 * time.Second):
|
2019-11-20 18:06:51 +01:00
|
|
|
err := pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
|
|
|
if err != nil {
|
2022-11-30 12:00:37 +01:00
|
|
|
panic(fmt.Sprintf("error writing to std out "+
|
|
|
|
"after timeout: %v", err))
|
2019-11-20 18:06:51 +01:00
|
|
|
}
|
|
|
|
panic("timeout")
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
}
|
2019-12-09 17:50:11 +01:00
|
|
|
|
|
|
|
// invoiceExpiryTestData simply holds generated expired and pending invoices.
|
|
|
|
type invoiceExpiryTestData struct {
|
2022-11-30 12:00:37 +01:00
|
|
|
expiredInvoices map[lntypes.Hash]*invpkg.Invoice
|
|
|
|
pendingInvoices map[lntypes.Hash]*invpkg.Invoice
|
2019-12-09 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// generateInvoiceExpiryTestData generates the specified number of fake expired
|
|
|
|
// and pending invoices anchored to the passed now timestamp.
|
|
|
|
func generateInvoiceExpiryTestData(
|
|
|
|
t *testing.T, now time.Time,
|
|
|
|
offset, numExpired, numPending int) invoiceExpiryTestData {
|
|
|
|
|
|
|
|
var testData invoiceExpiryTestData
|
|
|
|
|
2022-11-30 12:00:37 +01:00
|
|
|
testData.expiredInvoices = make(map[lntypes.Hash]*invpkg.Invoice)
|
|
|
|
testData.pendingInvoices = make(map[lntypes.Hash]*invpkg.Invoice)
|
2019-12-09 17:50:11 +01:00
|
|
|
|
|
|
|
expiredCreationDate := now.Add(-24 * time.Hour)
|
|
|
|
|
|
|
|
for i := 1; i <= numExpired; i++ {
|
|
|
|
var preimage lntypes.Preimage
|
|
|
|
binary.BigEndian.PutUint32(preimage[:4], uint32(offset+i))
|
|
|
|
expiry := time.Duration((i+offset)%24) * time.Hour
|
2023-07-12 22:08:20 +02:00
|
|
|
invoice := newInvoiceExpiryTestInvoice(
|
2022-11-30 12:00:37 +01:00
|
|
|
t, preimage, expiredCreationDate, expiry,
|
|
|
|
)
|
2019-12-09 17:50:11 +01:00
|
|
|
testData.expiredInvoices[preimage.Hash()] = invoice
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= numPending; i++ {
|
|
|
|
var preimage lntypes.Preimage
|
|
|
|
binary.BigEndian.PutUint32(preimage[4:], uint32(offset+i))
|
|
|
|
expiry := time.Duration((i+offset)%24) * time.Hour
|
2023-07-12 22:08:20 +02:00
|
|
|
invoice := newInvoiceExpiryTestInvoice(t, preimage, now, expiry)
|
2019-12-09 17:50:11 +01:00
|
|
|
testData.pendingInvoices[preimage.Hash()] = invoice
|
|
|
|
}
|
|
|
|
|
|
|
|
return testData
|
|
|
|
}
|
2021-03-25 03:48:59 +01:00
|
|
|
|
2023-07-12 22:08:20 +02:00
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 03:48:59 +01:00
|
|
|
// checkSettleResolution asserts the resolution is a settle with the correct
|
|
|
|
// preimage. If successful, the HtlcSettleResolution is returned in case further
|
|
|
|
// checks are desired.
|
2022-11-30 12:00:37 +01:00
|
|
|
func checkSettleResolution(t *testing.T, res invpkg.HtlcResolution,
|
|
|
|
expPreimage lntypes.Preimage) *invpkg.HtlcSettleResolution {
|
2021-03-25 03:48:59 +01:00
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
2022-11-30 12:00:37 +01:00
|
|
|
settleResolution, ok := res.(*invpkg.HtlcSettleResolution)
|
2021-03-25 03:48:59 +01:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, expPreimage, settleResolution.Preimage)
|
|
|
|
|
|
|
|
return settleResolution
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkFailResolution asserts the resolution is a fail with the correct reason.
|
2022-06-22 10:15:41 +02:00
|
|
|
// If successful, the HtlcFailResolution is returned in case further checks are
|
2021-03-25 03:48:59 +01:00
|
|
|
// desired.
|
2022-11-30 12:00:37 +01:00
|
|
|
func checkFailResolution(t *testing.T, res invpkg.HtlcResolution,
|
|
|
|
expOutcome invpkg.FailResolutionResult) *invpkg.HtlcFailResolution {
|
2021-03-25 03:48:59 +01:00
|
|
|
|
|
|
|
t.Helper()
|
2022-11-30 12:00:37 +01:00
|
|
|
failResolution, ok := res.(*invpkg.HtlcFailResolution)
|
2021-03-25 03:48:59 +01:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, expOutcome, failResolution.Outcome)
|
|
|
|
|
|
|
|
return failResolution
|
|
|
|
}
|
2021-05-11 08:45:30 +02:00
|
|
|
|
|
|
|
type hodlExpiryTest struct {
|
|
|
|
hash lntypes.Hash
|
2022-11-30 12:00:37 +01:00
|
|
|
state invpkg.ContractState
|
2021-05-11 08:45:30 +02:00
|
|
|
stateLock sync.Mutex
|
|
|
|
mockNotifier *mockChainNotifier
|
|
|
|
mockClock *clock.TestClock
|
|
|
|
cancelChan chan lntypes.Hash
|
2022-11-30 12:00:37 +01:00
|
|
|
watcher *invpkg.InvoiceExpiryWatcher
|
2021-05-11 08:45:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hodlExpiryTest) announceBlock(t *testing.T, height uint32) {
|
2022-11-30 12:00:37 +01:00
|
|
|
t.Helper()
|
|
|
|
|
2021-05-11 08:45:30 +02:00
|
|
|
select {
|
|
|
|
case h.mockNotifier.blockChan <- &chainntnfs.BlockEpoch{
|
|
|
|
Height: int32(height),
|
|
|
|
}:
|
|
|
|
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatalf("block %v not consumed", height)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hodlExpiryTest) assertCanceled(t *testing.T, expected lntypes.Hash) {
|
|
|
|
select {
|
|
|
|
case actual := <-h.cancelChan:
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatalf("invoice: %v not canceled", h.hash)
|
|
|
|
}
|
|
|
|
}
|