2019-12-09 17:50:11 +01:00
|
|
|
package invoices
|
|
|
|
|
|
|
|
import (
|
2020-04-09 09:58:22 +02:00
|
|
|
"sync"
|
2019-12-09 17:50:11 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/clock"
|
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2022-05-05 22:11:50 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-12-09 17:50:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// invoiceExpiryWatcherTest holds a test fixture and implements checks
|
|
|
|
// for InvoiceExpiryWatcher tests.
|
|
|
|
type invoiceExpiryWatcherTest struct {
|
|
|
|
t *testing.T
|
2020-04-09 09:58:22 +02:00
|
|
|
wg sync.WaitGroup
|
2019-12-09 17:50:11 +01:00
|
|
|
watcher *InvoiceExpiryWatcher
|
|
|
|
testData invoiceExpiryTestData
|
|
|
|
canceledInvoices []lntypes.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// newInvoiceExpiryWatcherTest creates a new InvoiceExpiryWatcher test fixture
|
|
|
|
// and sets up the test environment.
|
|
|
|
func newInvoiceExpiryWatcherTest(t *testing.T, now time.Time,
|
|
|
|
numExpiredInvoices, numPendingInvoices int) *invoiceExpiryWatcherTest {
|
|
|
|
|
2021-05-11 08:45:29 +02:00
|
|
|
mockNotifier := newMockNotifier()
|
2019-12-09 17:50:11 +01:00
|
|
|
test := &invoiceExpiryWatcherTest{
|
2021-05-11 08:45:29 +02:00
|
|
|
watcher: NewInvoiceExpiryWatcher(
|
|
|
|
clock.NewTestClock(testTime), 0,
|
|
|
|
uint32(testCurrentHeight), nil, mockNotifier,
|
|
|
|
),
|
2019-12-09 17:50:11 +01:00
|
|
|
testData: generateInvoiceExpiryTestData(
|
|
|
|
t, now, 0, numExpiredInvoices, numPendingInvoices,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2020-04-09 09:58:22 +02:00
|
|
|
test.wg.Add(numExpiredInvoices)
|
|
|
|
|
2020-04-09 10:42:10 +02:00
|
|
|
err := test.watcher.Start(func(paymentHash lntypes.Hash,
|
|
|
|
force bool) error {
|
|
|
|
|
2020-07-17 14:24:54 +02:00
|
|
|
test.canceledInvoices = append(
|
|
|
|
test.canceledInvoices, paymentHash,
|
|
|
|
)
|
2020-04-09 09:58:22 +02:00
|
|
|
test.wg.Done()
|
2019-12-09 17:50:11 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-05-05 22:11:50 +02:00
|
|
|
require.NoError(t, err, "cannot start InvoiceExpiryWatcher")
|
2019-12-09 17:50:11 +01:00
|
|
|
|
|
|
|
return test
|
|
|
|
}
|
|
|
|
|
2020-04-09 09:58:22 +02:00
|
|
|
func (t *invoiceExpiryWatcherTest) waitForFinish(timeout time.Duration) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
// Wait for all cancels.
|
|
|
|
go func() {
|
|
|
|
t.wg.Wait()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(timeout):
|
|
|
|
t.t.Fatalf("test timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 17:50:11 +01:00
|
|
|
func (t *invoiceExpiryWatcherTest) checkExpectations() {
|
|
|
|
// Check that invoices that got canceled during the test are the ones
|
|
|
|
// that expired.
|
|
|
|
if len(t.canceledInvoices) != len(t.testData.expiredInvoices) {
|
|
|
|
t.t.Fatalf("expected %v cancellations, got %v",
|
2020-07-17 14:24:54 +02:00
|
|
|
len(t.testData.expiredInvoices),
|
|
|
|
len(t.canceledInvoices))
|
2019-12-09 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range t.canceledInvoices {
|
|
|
|
if _, ok := t.testData.expiredInvoices[t.canceledInvoices[i]]; !ok {
|
|
|
|
t.t.Fatalf("wrong invoice canceled")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that InvoiceExpiryWatcher can be started and stopped.
|
|
|
|
func TestInvoiceExpiryWatcherStartStop(t *testing.T) {
|
2021-05-11 08:45:29 +02:00
|
|
|
watcher := NewInvoiceExpiryWatcher(
|
|
|
|
clock.NewTestClock(testTime), 0, uint32(testCurrentHeight), nil,
|
|
|
|
newMockNotifier(),
|
|
|
|
)
|
2020-04-09 10:42:10 +02:00
|
|
|
cancel := func(lntypes.Hash, bool) error {
|
2019-12-09 17:50:11 +01:00
|
|
|
t.Fatalf("unexpected call")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := watcher.Start(cancel); err != nil {
|
|
|
|
t.Fatalf("unexpected error upon start: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := watcher.Start(cancel); err == nil {
|
|
|
|
t.Fatalf("expected error upon second start")
|
|
|
|
}
|
|
|
|
|
|
|
|
watcher.Stop()
|
|
|
|
|
|
|
|
if err := watcher.Start(cancel); err != nil {
|
|
|
|
t.Fatalf("unexpected error upon start: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that no invoices will expire from an empty InvoiceExpiryWatcher.
|
|
|
|
func TestInvoiceExpiryWithNoInvoices(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-04-09 09:58:22 +02:00
|
|
|
|
2019-12-09 17:50:11 +01:00
|
|
|
test := newInvoiceExpiryWatcherTest(t, testTime, 0, 0)
|
|
|
|
|
2020-04-09 09:58:22 +02:00
|
|
|
test.waitForFinish(testTimeout)
|
2019-12-09 17:50:11 +01:00
|
|
|
test.watcher.Stop()
|
|
|
|
test.checkExpectations()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that if all add invoices are expired, then all invoices
|
|
|
|
// will be canceled.
|
|
|
|
func TestInvoiceExpiryWithOnlyExpiredInvoices(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
test := newInvoiceExpiryWatcherTest(t, testTime, 0, 5)
|
|
|
|
|
|
|
|
for paymentHash, invoice := range test.testData.pendingInvoices {
|
2020-10-21 13:21:35 +02:00
|
|
|
test.watcher.AddInvoices(makeInvoiceExpiry(paymentHash, invoice))
|
2019-12-09 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 09:58:22 +02:00
|
|
|
test.waitForFinish(testTimeout)
|
2019-12-09 17:50:11 +01:00
|
|
|
test.watcher.Stop()
|
|
|
|
test.checkExpectations()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that if some invoices are expired, then those invoices
|
|
|
|
// will be canceled.
|
|
|
|
func TestInvoiceExpiryWithPendingAndExpiredInvoices(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-04-09 09:58:22 +02:00
|
|
|
|
2019-12-09 17:50:11 +01:00
|
|
|
test := newInvoiceExpiryWatcherTest(t, testTime, 5, 5)
|
|
|
|
|
|
|
|
for paymentHash, invoice := range test.testData.expiredInvoices {
|
2020-10-21 13:21:35 +02:00
|
|
|
test.watcher.AddInvoices(makeInvoiceExpiry(paymentHash, invoice))
|
2019-12-09 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for paymentHash, invoice := range test.testData.pendingInvoices {
|
2020-10-21 13:21:35 +02:00
|
|
|
test.watcher.AddInvoices(makeInvoiceExpiry(paymentHash, invoice))
|
2019-12-09 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 09:58:22 +02:00
|
|
|
test.waitForFinish(testTimeout)
|
2019-12-09 17:50:11 +01:00
|
|
|
test.watcher.Stop()
|
|
|
|
test.checkExpectations()
|
|
|
|
}
|
2020-01-23 00:21:12 +01:00
|
|
|
|
|
|
|
// Tests adding multiple invoices at once.
|
|
|
|
func TestInvoiceExpiryWhenAddingMultipleInvoices(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-04-09 09:58:22 +02:00
|
|
|
|
2020-01-23 00:21:12 +01:00
|
|
|
test := newInvoiceExpiryWatcherTest(t, testTime, 5, 5)
|
2021-04-23 08:19:54 +02:00
|
|
|
var invoices []invoiceExpiry
|
2020-04-09 09:58:22 +02:00
|
|
|
|
2020-01-23 00:21:12 +01:00
|
|
|
for hash, invoice := range test.testData.expiredInvoices {
|
2020-10-21 13:21:35 +02:00
|
|
|
invoices = append(invoices, makeInvoiceExpiry(hash, invoice))
|
2020-01-23 00:21:12 +01:00
|
|
|
}
|
2020-04-09 09:58:22 +02:00
|
|
|
|
2020-01-23 00:21:12 +01:00
|
|
|
for hash, invoice := range test.testData.pendingInvoices {
|
2020-10-21 13:21:35 +02:00
|
|
|
invoices = append(invoices, makeInvoiceExpiry(hash, invoice))
|
2020-01-23 00:21:12 +01:00
|
|
|
}
|
|
|
|
|
2020-10-21 13:21:35 +02:00
|
|
|
test.watcher.AddInvoices(invoices...)
|
2020-04-09 09:58:22 +02:00
|
|
|
test.waitForFinish(testTimeout)
|
2020-01-23 00:21:12 +01:00
|
|
|
test.watcher.Stop()
|
|
|
|
test.checkExpectations()
|
|
|
|
}
|
2021-05-11 08:45:30 +02:00
|
|
|
|
|
|
|
// TestExpiredHodlInv tests expiration of an already-expired hodl invoice
|
|
|
|
// which has no htlcs.
|
|
|
|
func TestExpiredHodlInv(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
creationDate := testTime.Add(time.Hour * -24)
|
|
|
|
expiry := time.Hour
|
|
|
|
|
|
|
|
test := setupHodlExpiry(
|
2022-11-30 12:00:37 +01:00
|
|
|
t, creationDate, expiry, 0, ContractOpen, nil,
|
2021-05-11 08:45:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
test.assertCanceled(t, test.hash)
|
|
|
|
test.watcher.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAcceptedHodlNotExpired tests that hodl invoices which are in an accepted
|
|
|
|
// state are not expired once their time-based expiry elapses, using a regular
|
|
|
|
// invoice that expires at the same time as a control to ensure that invoices
|
|
|
|
// with that timestamp would otherwise be expired.
|
|
|
|
func TestAcceptedHodlNotExpired(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
creationDate := testTime
|
|
|
|
expiry := time.Hour
|
|
|
|
|
|
|
|
test := setupHodlExpiry(
|
2022-11-30 12:00:37 +01:00
|
|
|
t, creationDate, expiry, 0, ContractAccepted, nil,
|
2021-05-11 08:45:30 +02:00
|
|
|
)
|
|
|
|
defer test.watcher.Stop()
|
|
|
|
|
|
|
|
// Add another invoice that will expire at our expiry time as a control
|
|
|
|
// value.
|
|
|
|
tsExpires := &invoiceExpiryTs{
|
|
|
|
PaymentHash: lntypes.Hash{1, 2, 3},
|
|
|
|
Expiry: creationDate.Add(expiry),
|
|
|
|
Keysend: true,
|
|
|
|
}
|
|
|
|
test.watcher.AddInvoices(tsExpires)
|
|
|
|
|
|
|
|
test.mockClock.SetTime(creationDate.Add(expiry + 1))
|
|
|
|
|
|
|
|
// Assert that only the ts expiry invoice is expired.
|
|
|
|
test.assertCanceled(t, tsExpires.PaymentHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHeightAlreadyExpired tests the case where we add an invoice with htlcs
|
|
|
|
// that have already expired to the expiry watcher.
|
|
|
|
func TestHeightAlreadyExpired(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-11-30 12:00:37 +01:00
|
|
|
expiredHtlc := []*InvoiceHTLC{
|
2021-05-11 08:45:30 +02:00
|
|
|
{
|
2022-11-30 12:00:37 +01:00
|
|
|
State: HtlcStateAccepted,
|
2021-05-11 08:45:30 +02:00
|
|
|
Expiry: uint32(testCurrentHeight),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
test := setupHodlExpiry(
|
2022-11-30 12:00:37 +01:00
|
|
|
t, testTime, time.Hour, 0, ContractAccepted,
|
2021-05-11 08:45:30 +02:00
|
|
|
expiredHtlc,
|
|
|
|
)
|
|
|
|
defer test.watcher.Stop()
|
|
|
|
|
|
|
|
test.assertCanceled(t, test.hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestExpiryHeightArrives tests the case where we add a hodl invoice to the
|
|
|
|
// expiry watcher when it has no htlcs, htlcs are added and then they finally
|
|
|
|
// expire. We use a non-zero delta for this test to check that we expire with
|
|
|
|
// sufficient buffer.
|
|
|
|
func TestExpiryHeightArrives(t *testing.T) {
|
|
|
|
var (
|
|
|
|
creationDate = testTime
|
|
|
|
expiry = time.Hour * 2
|
|
|
|
delta uint32 = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
// Start out with a hodl invoice that is open, and has no htlcs.
|
|
|
|
test := setupHodlExpiry(
|
2022-11-30 12:00:37 +01:00
|
|
|
t, creationDate, expiry, delta, ContractOpen, nil,
|
2021-05-11 08:45:30 +02:00
|
|
|
)
|
|
|
|
defer test.watcher.Stop()
|
|
|
|
|
|
|
|
htlc1 := uint32(testCurrentHeight + 10)
|
|
|
|
expiry1 := makeHeightExpiry(test.hash, htlc1)
|
|
|
|
|
|
|
|
// Add htlcs to our invoice and progress its state to accepted.
|
|
|
|
test.watcher.AddInvoices(expiry1)
|
2022-11-30 12:00:37 +01:00
|
|
|
test.setState(ContractAccepted)
|
2021-05-11 08:45:30 +02:00
|
|
|
|
|
|
|
// Progress time so that our expiry has elapsed. We no longer expect
|
|
|
|
// this invoice to be canceled because it has been accepted.
|
|
|
|
test.mockClock.SetTime(creationDate.Add(expiry))
|
|
|
|
|
|
|
|
// Tick our mock block subscription with the next block, we don't
|
|
|
|
// expect anything to happen.
|
|
|
|
currentHeight := uint32(testCurrentHeight + 1)
|
|
|
|
test.announceBlock(t, currentHeight)
|
|
|
|
|
|
|
|
// Now, we add another htlc to the invoice. This one has a lower expiry
|
|
|
|
// height than our current ones.
|
|
|
|
htlc2 := currentHeight + 5
|
|
|
|
expiry2 := makeHeightExpiry(test.hash, htlc2)
|
|
|
|
test.watcher.AddInvoices(expiry2)
|
|
|
|
|
|
|
|
// Announce our lowest htlc expiry block minus our delta, the invoice
|
|
|
|
// should be expired now.
|
|
|
|
test.announceBlock(t, htlc2-delta)
|
|
|
|
test.assertCanceled(t, test.hash)
|
|
|
|
}
|