2019-11-01 14:01:34 +03:00
|
|
|
package contractcourt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2019-08-26 15:06:57 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2020-07-07 20:49:50 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
2019-11-01 14:01:34 +03:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2020-07-07 20:49:56 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-11-01 14:01:34 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
outgoingContestHtlcExpiry = 110
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestHtlcOutgoingResolverTimeout tests resolution of an offered htlc that
|
|
|
|
// timed out.
|
|
|
|
func TestHtlcOutgoingResolverTimeout(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
defer timeout(t)()
|
|
|
|
|
|
|
|
// Setup the resolver with our test resolution.
|
|
|
|
ctx := newOutgoingResolverTestContext(t)
|
|
|
|
|
|
|
|
// Start the resolution process in a goroutine.
|
|
|
|
ctx.resolve()
|
|
|
|
|
|
|
|
// Notify arrival of the block after which the timeout path of the htlc
|
|
|
|
// unlocks.
|
|
|
|
ctx.notifyEpoch(outgoingContestHtlcExpiry - 1)
|
|
|
|
|
|
|
|
// Assert that the resolver finishes without error and transforms in a
|
|
|
|
// timeout resolver.
|
|
|
|
ctx.waitForResult(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHtlcOutgoingResolverRemoteClaim tests resolution of an offered htlc that
|
|
|
|
// is claimed by the remote party.
|
|
|
|
func TestHtlcOutgoingResolverRemoteClaim(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
defer timeout(t)()
|
|
|
|
|
|
|
|
// Setup the resolver with our test resolution and start the resolution
|
|
|
|
// process.
|
|
|
|
ctx := newOutgoingResolverTestContext(t)
|
2020-07-07 20:49:56 +03:00
|
|
|
|
|
|
|
// Replace our mocked checkpoint function with one which will push
|
|
|
|
// reports into a channel for us to consume. We do so on the resolver
|
|
|
|
// level because our test context has already created the resolver.
|
|
|
|
reportChan := make(chan *channeldb.ResolverReport)
|
|
|
|
ctx.resolver.Checkpoint = func(_ ContractResolver,
|
|
|
|
reports ...*channeldb.ResolverReport) error {
|
|
|
|
|
|
|
|
// Send all of our reports into the channel.
|
|
|
|
for _, report := range reports {
|
|
|
|
reportChan <- report
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-01 14:01:34 +03:00
|
|
|
ctx.resolve()
|
|
|
|
|
|
|
|
// The remote party sweeps the htlc. Notify our resolver of this event.
|
|
|
|
preimage := lntypes.Preimage{}
|
2020-07-07 20:49:56 +03:00
|
|
|
spendTx := &wire.MsgTx{
|
|
|
|
TxIn: []*wire.TxIn{
|
|
|
|
{
|
|
|
|
Witness: [][]byte{
|
|
|
|
{0}, {1}, {2}, preimage[:],
|
2019-11-01 14:01:34 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-07-07 20:49:56 +03:00
|
|
|
spendHash := spendTx.TxHash()
|
|
|
|
|
|
|
|
ctx.notifier.spendChan <- &chainntnfs.SpendDetail{
|
|
|
|
SpendingTx: spendTx,
|
|
|
|
SpenderTxHash: &spendHash,
|
|
|
|
}
|
|
|
|
|
2019-11-01 14:01:34 +03:00
|
|
|
// We expect the extracted preimage to be added to the witness beacon.
|
|
|
|
<-ctx.preimageDB.newPreimages
|
|
|
|
|
|
|
|
// We also expect a resolution message to the incoming side of the
|
|
|
|
// circuit.
|
|
|
|
<-ctx.resolutionChan
|
|
|
|
|
2020-07-07 20:49:56 +03:00
|
|
|
// Finally, check that we have a report as expected.
|
|
|
|
expectedReport := &channeldb.ResolverReport{
|
|
|
|
OutPoint: wire.OutPoint{},
|
|
|
|
Amount: 0,
|
|
|
|
ResolverType: channeldb.ResolverTypeOutgoingHtlc,
|
|
|
|
ResolverOutcome: channeldb.ResolverOutcomeClaimed,
|
|
|
|
SpendTxID: &spendHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
assertResolverReport(t, reportChan, expectedReport)
|
|
|
|
|
2019-11-01 14:01:34 +03:00
|
|
|
// Assert that the resolver finishes without error.
|
|
|
|
ctx.waitForResult(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
type resolveResult struct {
|
|
|
|
err error
|
|
|
|
nextResolver ContractResolver
|
|
|
|
}
|
|
|
|
|
|
|
|
type outgoingResolverTestContext struct {
|
|
|
|
resolver *htlcOutgoingContestResolver
|
|
|
|
notifier *mockNotifier
|
|
|
|
preimageDB *mockWitnessBeacon
|
|
|
|
resolverResultChan chan resolveResult
|
|
|
|
resolutionChan chan ResolutionMsg
|
|
|
|
t *testing.T
|
|
|
|
}
|
|
|
|
|
|
|
|
func newOutgoingResolverTestContext(t *testing.T) *outgoingResolverTestContext {
|
|
|
|
notifier := &mockNotifier{
|
|
|
|
epochChan: make(chan *chainntnfs.BlockEpoch),
|
|
|
|
spendChan: make(chan *chainntnfs.SpendDetail),
|
|
|
|
confChan: make(chan *chainntnfs.TxConfirmation),
|
|
|
|
}
|
|
|
|
|
|
|
|
checkPointChan := make(chan struct{}, 1)
|
|
|
|
resolutionChan := make(chan ResolutionMsg, 1)
|
|
|
|
|
|
|
|
preimageDB := newMockWitnessBeacon()
|
|
|
|
|
2019-08-26 15:06:57 +03:00
|
|
|
onionProcessor := &mockOnionProcessor{}
|
|
|
|
|
2019-11-01 14:01:34 +03:00
|
|
|
chainCfg := ChannelArbitratorConfig{
|
|
|
|
ChainArbitratorConfig: ChainArbitratorConfig{
|
|
|
|
Notifier: notifier,
|
|
|
|
PreimageDB: preimageDB,
|
|
|
|
DeliverResolutionMsg: func(msgs ...ResolutionMsg) error {
|
|
|
|
if len(msgs) != 1 {
|
|
|
|
return fmt.Errorf("expected 1 "+
|
|
|
|
"resolution msg, instead got %v",
|
|
|
|
len(msgs))
|
|
|
|
}
|
|
|
|
|
|
|
|
resolutionChan <- msgs[0]
|
|
|
|
return nil
|
|
|
|
},
|
2019-08-26 15:06:57 +03:00
|
|
|
OnionProcessor: onionProcessor,
|
2019-11-01 14:01:34 +03:00
|
|
|
},
|
2020-07-07 20:49:50 +03:00
|
|
|
PutResolverReport: func(_ kvdb.RwTx,
|
|
|
|
_ *channeldb.ResolverReport) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2019-11-01 14:01:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
outgoingRes := lnwallet.OutgoingHtlcResolution{
|
|
|
|
Expiry: outgoingContestHtlcExpiry,
|
|
|
|
SweepSignDesc: input.SignDescriptor{
|
|
|
|
Output: &wire.TxOut{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-06 15:16:50 +03:00
|
|
|
cfg := ResolverConfig{
|
|
|
|
ChannelArbitratorConfig: chainCfg,
|
2020-07-07 20:49:51 +03:00
|
|
|
Checkpoint: func(_ ContractResolver,
|
|
|
|
_ ...*channeldb.ResolverReport) error {
|
|
|
|
|
2019-11-06 15:16:50 +03:00
|
|
|
checkPointChan <- struct{}{}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-01 14:01:34 +03:00
|
|
|
resolver := &htlcOutgoingContestResolver{
|
|
|
|
htlcTimeoutResolver: htlcTimeoutResolver{
|
2019-11-06 15:16:50 +03:00
|
|
|
contractResolverKit: *newContractResolverKit(cfg),
|
|
|
|
htlcResolution: outgoingRes,
|
2019-08-26 15:06:57 +03:00
|
|
|
htlc: channeldb.HTLC{
|
2020-07-07 20:49:56 +03:00
|
|
|
Amt: lnwire.MilliSatoshi(testHtlcAmount),
|
2019-08-26 15:06:57 +03:00
|
|
|
RHash: testResHash,
|
|
|
|
OnionBlob: testOnionBlob,
|
|
|
|
},
|
2019-11-01 14:01:34 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &outgoingResolverTestContext{
|
|
|
|
resolver: resolver,
|
|
|
|
notifier: notifier,
|
|
|
|
preimageDB: preimageDB,
|
|
|
|
resolutionChan: resolutionChan,
|
|
|
|
t: t,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *outgoingResolverTestContext) resolve() {
|
|
|
|
// Start resolver.
|
|
|
|
i.resolverResultChan = make(chan resolveResult, 1)
|
|
|
|
go func() {
|
|
|
|
nextResolver, err := i.resolver.Resolve()
|
|
|
|
i.resolverResultChan <- resolveResult{
|
|
|
|
nextResolver: nextResolver,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Notify initial block height.
|
|
|
|
i.notifyEpoch(testInitialBlockHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *outgoingResolverTestContext) notifyEpoch(height int32) {
|
|
|
|
i.notifier.epochChan <- &chainntnfs.BlockEpoch{
|
|
|
|
Height: height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *outgoingResolverTestContext) waitForResult(expectTimeoutRes bool) {
|
|
|
|
i.t.Helper()
|
|
|
|
|
|
|
|
result := <-i.resolverResultChan
|
|
|
|
if result.err != nil {
|
|
|
|
i.t.Fatal(result.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !expectTimeoutRes {
|
|
|
|
if result.nextResolver != nil {
|
|
|
|
i.t.Fatal("expected no next resolver")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := result.nextResolver.(*htlcTimeoutResolver)
|
|
|
|
if !ok {
|
|
|
|
i.t.Fatal("expected htlcTimeoutResolver")
|
|
|
|
}
|
|
|
|
}
|