invoices/test: add test for hodl invoices
This commit is contained in:
2 files changed
+223
No files matched your search
@@ -8,11 +8,13 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"runtime/pprof"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/lightningnetwork/lnd/chainntnfs"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/clock"
|
||||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
@@ -367,3 +369,111 @@ func checkFailResolution(t *testing.T, res HtlcResolution,
|
||||
|
||||
return failResolution
|
||||
}
|
||||
|
||||
type hodlExpiryTest struct {
|
||||
hash lntypes.Hash
|
||||
state channeldb.ContractState
|
||||
stateLock sync.Mutex
|
||||
mockNotifier *mockChainNotifier
|
||||
mockClock *clock.TestClock
|
||||
cancelChan chan lntypes.Hash
|
||||
watcher *InvoiceExpiryWatcher
|
||||
}
|
||||
|
||||
func (h *hodlExpiryTest) setState(state channeldb.ContractState) {
|
||||
h.stateLock.Lock()
|
||||
defer h.stateLock.Unlock()
|
||||
|
||||
h.state = state
|
||||
}
|
||||
|
||||
func (h *hodlExpiryTest) announceBlock(t *testing.T, height uint32) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// setupHodlExpiry creates a hodl invoice in our expiry watcher and runs an
|
||||
// arbitrary update function which advances the invoices's state.
|
||||
func setupHodlExpiry(t *testing.T, creationDate time.Time,
|
||||
expiry time.Duration, heightDelta uint32,
|
||||
startState channeldb.ContractState,
|
||||
startHtlcs []*channeldb.InvoiceHTLC) *hodlExpiryTest {
|
||||
|
||||
mockNotifier := newMockNotifier()
|
||||
mockClock := clock.NewTestClock(testTime)
|
||||
|
||||
test := &hodlExpiryTest{
|
||||
state: startState,
|
||||
watcher: NewInvoiceExpiryWatcher(
|
||||
mockClock, heightDelta, uint32(testCurrentHeight), nil,
|
||||
mockNotifier,
|
||||
),
|
||||
cancelChan: make(chan lntypes.Hash),
|
||||
mockNotifier: mockNotifier,
|
||||
mockClock: mockClock,
|
||||
}
|
||||
|
||||
// Use an unbuffered channel to block on cancel calls so that the test
|
||||
// does not exit before we've processed all the invoices we expect.
|
||||
cancelImpl := func(paymentHash lntypes.Hash, force bool) error {
|
||||
test.stateLock.Lock()
|
||||
currentState := test.state
|
||||
test.stateLock.Unlock()
|
||||
|
||||
if currentState != channeldb.ContractOpen && !force {
|
||||
return nil
|
||||
}
|
||||
|
||||
select {
|
||||
case test.cancelChan <- paymentHash:
|
||||
case <-time.After(testTimeout):
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
require.NoError(t, test.watcher.Start(cancelImpl))
|
||||
|
||||
// We set preimage and hash so that we can use our existing test
|
||||
// helpers. In practice we would only have the hash, but this does not
|
||||
// affect what we're testing at all.
|
||||
preimage := lntypes.Preimage{1}
|
||||
test.hash = preimage.Hash()
|
||||
|
||||
invoice := newTestInvoice(t, preimage, creationDate, expiry)
|
||||
invoice.State = startState
|
||||
invoice.HodlInvoice = true
|
||||
invoice.Htlcs = make(map[channeldb.CircuitKey]*channeldb.InvoiceHTLC)
|
||||
|
||||
// If we have any htlcs, add them with unique circult keys.
|
||||
for i, htlc := range startHtlcs {
|
||||
key := channeldb.CircuitKey{
|
||||
HtlcID: uint64(i),
|
||||
}
|
||||
|
||||
invoice.Htlcs[key] = htlc
|
||||
}
|
||||
|
||||
// Create an expiry entry for our invoice in its starting state. This
|
||||
// mimics adding invoices to the watcher on start.
|
||||
entry := makeInvoiceExpiry(test.hash, invoice)
|
||||
test.watcher.AddInvoices(entry)
|
||||
|
||||
return test
|
||||
}
|
||||
Reference in new issue
Block a user