2019-04-16 12:33:30 +03:00
|
|
|
package contractcourt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-08-26 15:06:57 +03:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2019-04-16 12:33:30 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-08-26 15:06:57 +03:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
2019-04-16 12:33:30 +03:00
|
|
|
"github.com/lightningnetwork/lnd/invoices"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testInitialBlockHeight = 100
|
|
|
|
testHtlcExpiry = 150
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-14 22:11:34 +03:00
|
|
|
testResPreimage = lntypes.Preimage{1, 2, 3}
|
|
|
|
testResHash = testResPreimage.Hash()
|
|
|
|
testResCircuitKey = channeldb.CircuitKey{}
|
2019-08-26 15:06:57 +03:00
|
|
|
testOnionBlob = []byte{4, 5, 6}
|
2019-04-16 12:33:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestHtlcIncomingResolverFwdPreimageKnown tests resolution of a forwarded htlc
|
|
|
|
// for which the preimage is already known initially.
|
|
|
|
func TestHtlcIncomingResolverFwdPreimageKnown(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
defer timeout(t)()
|
|
|
|
|
|
|
|
ctx := newIncomingResolverTestContext(t)
|
|
|
|
ctx.registry.notifyErr = channeldb.ErrInvoiceNotFound
|
|
|
|
ctx.witnessBeacon.lookupPreimage[testResHash] = testResPreimage
|
|
|
|
ctx.resolve()
|
|
|
|
ctx.waitForResult(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHtlcIncomingResolverFwdContestedSuccess tests resolution of a forwarded
|
|
|
|
// htlc for which the preimage becomes known after the resolver has been
|
|
|
|
// started.
|
|
|
|
func TestHtlcIncomingResolverFwdContestedSuccess(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
defer timeout(t)()
|
|
|
|
|
|
|
|
ctx := newIncomingResolverTestContext(t)
|
|
|
|
ctx.registry.notifyErr = channeldb.ErrInvoiceNotFound
|
|
|
|
ctx.resolve()
|
|
|
|
|
|
|
|
// Simulate a new block coming in. HTLC is not yet expired.
|
|
|
|
ctx.notifyEpoch(testInitialBlockHeight + 1)
|
|
|
|
|
|
|
|
ctx.witnessBeacon.preImageUpdates <- testResPreimage
|
|
|
|
ctx.waitForResult(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHtlcIncomingResolverFwdContestedTimeout tests resolution of a forwarded
|
|
|
|
// htlc that times out after the resolver has been started.
|
|
|
|
func TestHtlcIncomingResolverFwdContestedTimeout(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
defer timeout(t)()
|
|
|
|
|
|
|
|
ctx := newIncomingResolverTestContext(t)
|
|
|
|
ctx.registry.notifyErr = channeldb.ErrInvoiceNotFound
|
|
|
|
ctx.resolve()
|
|
|
|
|
|
|
|
// Simulate a new block coming in. HTLC expires.
|
|
|
|
ctx.notifyEpoch(testHtlcExpiry)
|
|
|
|
|
|
|
|
ctx.waitForResult(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHtlcIncomingResolverFwdTimeout tests resolution of a forwarded htlc that
|
|
|
|
// has already expired when the resolver starts.
|
|
|
|
func TestHtlcIncomingResolverFwdTimeout(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
defer timeout(t)()
|
|
|
|
|
|
|
|
ctx := newIncomingResolverTestContext(t)
|
|
|
|
|
|
|
|
ctx.registry.notifyErr = channeldb.ErrInvoiceNotFound
|
|
|
|
ctx.witnessBeacon.lookupPreimage[testResHash] = testResPreimage
|
|
|
|
ctx.resolver.htlcExpiry = 90
|
|
|
|
ctx.resolve()
|
|
|
|
ctx.waitForResult(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHtlcIncomingResolverExitSettle tests resolution of an exit hop htlc for
|
|
|
|
// which the invoice has already been settled when the resolver starts.
|
|
|
|
func TestHtlcIncomingResolverExitSettle(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
defer timeout(t)()
|
|
|
|
|
|
|
|
ctx := newIncomingResolverTestContext(t)
|
|
|
|
ctx.registry.notifyEvent = &invoices.HodlEvent{
|
2019-08-14 22:11:34 +03:00
|
|
|
CircuitKey: testResCircuitKey,
|
|
|
|
Preimage: &testResPreimage,
|
2019-04-16 12:33:30 +03:00
|
|
|
}
|
|
|
|
ctx.resolve()
|
|
|
|
|
|
|
|
data := <-ctx.registry.notifyChan
|
|
|
|
if data.expiry != testHtlcExpiry {
|
|
|
|
t.Fatal("incorrect expiry")
|
|
|
|
}
|
|
|
|
if data.currentHeight != testInitialBlockHeight {
|
|
|
|
t.Fatal("incorrect block height")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.waitForResult(true)
|
2019-08-26 15:06:57 +03:00
|
|
|
|
|
|
|
if !bytes.Equal(
|
|
|
|
ctx.onionProcessor.offeredOnionBlob, testOnionBlob,
|
|
|
|
) {
|
|
|
|
t.Fatal("unexpected onion blob")
|
|
|
|
}
|
2019-04-16 12:33:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestHtlcIncomingResolverExitCancel tests resolution of an exit hop htlc for
|
|
|
|
// an invoice that is already canceled when the resolver starts.
|
|
|
|
func TestHtlcIncomingResolverExitCancel(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
defer timeout(t)()
|
|
|
|
|
|
|
|
ctx := newIncomingResolverTestContext(t)
|
|
|
|
ctx.registry.notifyEvent = &invoices.HodlEvent{
|
2019-08-14 22:11:34 +03:00
|
|
|
CircuitKey: testResCircuitKey,
|
2019-04-16 12:33:30 +03:00
|
|
|
}
|
|
|
|
ctx.resolve()
|
|
|
|
ctx.waitForResult(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHtlcIncomingResolverExitSettleHodl tests resolution of an exit hop htlc
|
|
|
|
// for a hodl invoice that is settled after the resolver has started.
|
|
|
|
func TestHtlcIncomingResolverExitSettleHodl(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
defer timeout(t)()
|
|
|
|
|
|
|
|
ctx := newIncomingResolverTestContext(t)
|
|
|
|
ctx.resolve()
|
|
|
|
|
|
|
|
notifyData := <-ctx.registry.notifyChan
|
|
|
|
notifyData.hodlChan <- invoices.HodlEvent{
|
2019-08-14 22:11:34 +03:00
|
|
|
CircuitKey: testResCircuitKey,
|
|
|
|
Preimage: &testResPreimage,
|
2019-04-16 12:33:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.waitForResult(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHtlcIncomingResolverExitTimeoutHodl tests resolution of an exit hop htlc
|
|
|
|
// for a hodl invoice that times out.
|
|
|
|
func TestHtlcIncomingResolverExitTimeoutHodl(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
defer timeout(t)()
|
|
|
|
|
|
|
|
ctx := newIncomingResolverTestContext(t)
|
|
|
|
ctx.resolve()
|
|
|
|
ctx.notifyEpoch(testHtlcExpiry)
|
|
|
|
ctx.waitForResult(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHtlcIncomingResolverExitCancelHodl tests resolution of an exit hop htlc
|
|
|
|
// for a hodl invoice that is canceled after the resolver has started.
|
|
|
|
func TestHtlcIncomingResolverExitCancelHodl(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
defer timeout(t)()
|
|
|
|
|
|
|
|
ctx := newIncomingResolverTestContext(t)
|
|
|
|
ctx.resolve()
|
|
|
|
notifyData := <-ctx.registry.notifyChan
|
|
|
|
notifyData.hodlChan <- invoices.HodlEvent{
|
2019-08-14 22:11:34 +03:00
|
|
|
CircuitKey: testResCircuitKey,
|
2019-04-16 12:33:30 +03:00
|
|
|
}
|
|
|
|
ctx.waitForResult(false)
|
|
|
|
}
|
|
|
|
|
2019-08-26 15:06:57 +03:00
|
|
|
type mockHopIterator struct {
|
|
|
|
hop.Iterator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *mockHopIterator) HopPayload() (*hop.Payload, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockOnionProcessor struct {
|
|
|
|
offeredOnionBlob []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *mockOnionProcessor) ReconstructHopIterator(r io.Reader, rHash []byte) (
|
|
|
|
hop.Iterator, error) {
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
o.offeredOnionBlob = data
|
|
|
|
|
|
|
|
return &mockHopIterator{}, nil
|
|
|
|
}
|
|
|
|
|
2019-04-16 12:33:30 +03:00
|
|
|
type incomingResolverTestContext struct {
|
2019-08-26 15:06:57 +03:00
|
|
|
registry *mockRegistry
|
|
|
|
witnessBeacon *mockWitnessBeacon
|
|
|
|
resolver *htlcIncomingContestResolver
|
|
|
|
notifier *mockNotifier
|
|
|
|
onionProcessor *mockOnionProcessor
|
|
|
|
resolveErr chan error
|
|
|
|
nextResolver ContractResolver
|
|
|
|
t *testing.T
|
2019-04-16 12:33:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newIncomingResolverTestContext(t *testing.T) *incomingResolverTestContext {
|
|
|
|
notifier := &mockNotifier{
|
|
|
|
epochChan: make(chan *chainntnfs.BlockEpoch),
|
|
|
|
spendChan: make(chan *chainntnfs.SpendDetail),
|
|
|
|
confChan: make(chan *chainntnfs.TxConfirmation),
|
|
|
|
}
|
|
|
|
witnessBeacon := newMockWitnessBeacon()
|
|
|
|
registry := &mockRegistry{
|
|
|
|
notifyChan: make(chan notifyExitHopData, 1),
|
|
|
|
}
|
|
|
|
|
2019-08-26 15:06:57 +03:00
|
|
|
onionProcessor := &mockOnionProcessor{}
|
|
|
|
|
2019-04-16 12:33:30 +03:00
|
|
|
checkPointChan := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
chainCfg := ChannelArbitratorConfig{
|
|
|
|
ChainArbitratorConfig: ChainArbitratorConfig{
|
2019-08-26 15:06:57 +03:00
|
|
|
Notifier: notifier,
|
|
|
|
PreimageDB: witnessBeacon,
|
|
|
|
Registry: registry,
|
|
|
|
OnionProcessor: onionProcessor,
|
2019-04-16 12:33:30 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-06 15:16:50 +03:00
|
|
|
cfg := ResolverConfig{
|
|
|
|
ChannelArbitratorConfig: chainCfg,
|
|
|
|
Checkpoint: func(_ ContractResolver) error {
|
|
|
|
checkPointChan <- struct{}{}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2019-04-16 12:33:30 +03:00
|
|
|
resolver := &htlcIncomingContestResolver{
|
|
|
|
htlcSuccessResolver: htlcSuccessResolver{
|
2019-11-06 15:16:50 +03:00
|
|
|
contractResolverKit: *newContractResolverKit(cfg),
|
|
|
|
htlcResolution: lnwallet.IncomingHtlcResolution{},
|
2019-11-05 16:23:15 +03:00
|
|
|
htlc: channeldb.HTLC{
|
2019-08-26 15:06:57 +03:00
|
|
|
RHash: testResHash,
|
|
|
|
OnionBlob: testOnionBlob,
|
2019-11-05 16:23:15 +03:00
|
|
|
},
|
2019-04-16 12:33:30 +03:00
|
|
|
},
|
|
|
|
htlcExpiry: testHtlcExpiry,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &incomingResolverTestContext{
|
2019-08-26 15:06:57 +03:00
|
|
|
registry: registry,
|
|
|
|
witnessBeacon: witnessBeacon,
|
|
|
|
resolver: resolver,
|
|
|
|
notifier: notifier,
|
|
|
|
onionProcessor: onionProcessor,
|
|
|
|
t: t,
|
2019-04-16 12:33:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *incomingResolverTestContext) resolve() {
|
|
|
|
// Start resolver.
|
|
|
|
i.resolveErr = make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
var err error
|
|
|
|
i.nextResolver, err = i.resolver.Resolve()
|
|
|
|
i.resolveErr <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Notify initial block height.
|
|
|
|
i.notifyEpoch(testInitialBlockHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *incomingResolverTestContext) notifyEpoch(height int32) {
|
|
|
|
i.notifier.epochChan <- &chainntnfs.BlockEpoch{
|
|
|
|
Height: height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *incomingResolverTestContext) waitForResult(expectSuccessRes bool) {
|
|
|
|
i.t.Helper()
|
|
|
|
|
|
|
|
err := <-i.resolveErr
|
|
|
|
if err != nil {
|
|
|
|
i.t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !expectSuccessRes {
|
2019-10-31 14:59:15 +03:00
|
|
|
if i.nextResolver != nil {
|
2019-04-16 12:33:30 +03:00
|
|
|
i.t.Fatal("expected no next resolver")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
successResolver, ok := i.nextResolver.(*htlcSuccessResolver)
|
|
|
|
if !ok {
|
|
|
|
i.t.Fatal("expected htlcSuccessResolver")
|
|
|
|
}
|
|
|
|
|
|
|
|
if successResolver.htlcResolution.Preimage != testResPreimage {
|
|
|
|
i.t.Fatal("invalid preimage")
|
|
|
|
}
|
|
|
|
|
|
|
|
successTx := successResolver.htlcResolution.SignedSuccessTx
|
|
|
|
if successTx != nil &&
|
|
|
|
!bytes.Equal(successTx.TxIn[0].Witness[3], testResPreimage[:]) {
|
|
|
|
|
|
|
|
i.t.Fatal("invalid preimage")
|
|
|
|
}
|
|
|
|
}
|