2019-10-30 13:07:26 +01:00
|
|
|
package contractcourt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2020-07-07 19:49:50 +02:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
2019-10-30 13:07:26 +01:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2020-03-11 08:52:20 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2019-10-30 13:07:26 +01:00
|
|
|
"github.com/lightningnetwork/lnd/sweep"
|
|
|
|
)
|
|
|
|
|
|
|
|
type commitSweepResolverTestContext struct {
|
|
|
|
resolver *commitSweepResolver
|
|
|
|
notifier *mockNotifier
|
|
|
|
sweeper *mockSweeper
|
|
|
|
resolverResultChan chan resolveResult
|
|
|
|
t *testing.T
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCommitSweepResolverTestContext(t *testing.T,
|
|
|
|
resolution *lnwallet.CommitOutputResolution) *commitSweepResolverTestContext {
|
|
|
|
|
|
|
|
notifier := &mockNotifier{
|
|
|
|
epochChan: make(chan *chainntnfs.BlockEpoch),
|
|
|
|
spendChan: make(chan *chainntnfs.SpendDetail),
|
|
|
|
confChan: make(chan *chainntnfs.TxConfirmation),
|
|
|
|
}
|
|
|
|
|
|
|
|
sweeper := newMockSweeper()
|
|
|
|
|
|
|
|
checkPointChan := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
chainCfg := ChannelArbitratorConfig{
|
|
|
|
ChainArbitratorConfig: ChainArbitratorConfig{
|
|
|
|
Notifier: notifier,
|
|
|
|
Sweeper: sweeper,
|
|
|
|
},
|
2020-07-07 19:49:50 +02:00
|
|
|
PutResolverReport: func(_ kvdb.RwTx,
|
|
|
|
_ *channeldb.ResolverReport) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2019-10-30 13:07:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg := ResolverConfig{
|
|
|
|
ChannelArbitratorConfig: chainCfg,
|
2020-07-07 19:49:51 +02:00
|
|
|
Checkpoint: func(_ ContractResolver,
|
|
|
|
_ ...*channeldb.ResolverReport) error {
|
|
|
|
|
2019-10-30 13:07:26 +01:00
|
|
|
checkPointChan <- struct{}{}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resolver := newCommitSweepResolver(
|
|
|
|
*resolution, 0, wire.OutPoint{}, cfg,
|
|
|
|
)
|
|
|
|
|
|
|
|
return &commitSweepResolverTestContext{
|
|
|
|
resolver: resolver,
|
|
|
|
notifier: notifier,
|
|
|
|
sweeper: sweeper,
|
|
|
|
t: t,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *commitSweepResolverTestContext) resolve() {
|
|
|
|
// Start resolver.
|
|
|
|
i.resolverResultChan = make(chan resolveResult, 1)
|
|
|
|
go func() {
|
|
|
|
nextResolver, err := i.resolver.Resolve()
|
|
|
|
i.resolverResultChan <- resolveResult{
|
|
|
|
nextResolver: nextResolver,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *commitSweepResolverTestContext) notifyEpoch(height int32) {
|
|
|
|
i.notifier.epochChan <- &chainntnfs.BlockEpoch{
|
|
|
|
Height: height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *commitSweepResolverTestContext) waitForResult() {
|
|
|
|
i.t.Helper()
|
|
|
|
|
|
|
|
result := <-i.resolverResultChan
|
|
|
|
if result.err != nil {
|
|
|
|
i.t.Fatal(result.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.nextResolver != nil {
|
|
|
|
i.t.Fatal("expected no next resolver")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockSweeper struct {
|
2019-11-25 13:17:44 +01:00
|
|
|
sweptInputs chan input.Input
|
|
|
|
updatedInputs chan wire.OutPoint
|
2020-01-08 13:08:47 +01:00
|
|
|
sweepErr error
|
2019-10-30 13:07:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newMockSweeper() *mockSweeper {
|
|
|
|
return &mockSweeper{
|
2019-11-25 13:17:44 +01:00
|
|
|
sweptInputs: make(chan input.Input),
|
|
|
|
updatedInputs: make(chan wire.OutPoint),
|
2019-10-30 13:07:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 11:42:58 +01:00
|
|
|
func (s *mockSweeper) SweepInput(input input.Input, params sweep.Params) (
|
|
|
|
chan sweep.Result, error) {
|
2019-10-30 13:07:26 +01:00
|
|
|
|
|
|
|
s.sweptInputs <- input
|
|
|
|
|
|
|
|
result := make(chan sweep.Result, 1)
|
|
|
|
result <- sweep.Result{
|
2020-01-08 13:08:47 +01:00
|
|
|
Tx: &wire.MsgTx{},
|
|
|
|
Err: s.sweepErr,
|
2019-10-30 13:07:26 +01:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mockSweeper) CreateSweepTx(inputs []input.Input, feePref sweep.FeePreference,
|
|
|
|
currentBlockHeight uint32) (*wire.MsgTx, error) {
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 08:52:20 +01:00
|
|
|
func (s *mockSweeper) RelayFeePerKW() chainfee.SatPerKWeight {
|
|
|
|
return 253
|
|
|
|
}
|
|
|
|
|
2019-11-25 13:17:44 +01:00
|
|
|
func (s *mockSweeper) UpdateParams(input wire.OutPoint,
|
|
|
|
params sweep.ParamsUpdate) (chan sweep.Result, error) {
|
|
|
|
|
|
|
|
s.updatedInputs <- input
|
|
|
|
|
|
|
|
result := make(chan sweep.Result, 1)
|
|
|
|
result <- sweep.Result{
|
|
|
|
Tx: &wire.MsgTx{},
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:07:26 +01:00
|
|
|
var _ UtxoSweeper = &mockSweeper{}
|
|
|
|
|
|
|
|
// TestCommitSweepResolverNoDelay tests resolution of a direct commitment output
|
|
|
|
// unencumbered by a time lock.
|
|
|
|
func TestCommitSweepResolverNoDelay(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
defer timeout(t)()
|
|
|
|
|
|
|
|
res := lnwallet.CommitOutputResolution{
|
|
|
|
SelfOutputSignDesc: input.SignDescriptor{
|
|
|
|
Output: &wire.TxOut{
|
|
|
|
Value: 100,
|
|
|
|
},
|
2020-03-06 16:11:46 +01:00
|
|
|
WitnessScript: []byte{0},
|
2019-10-30 13:07:26 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := newCommitSweepResolverTestContext(t, &res)
|
|
|
|
ctx.resolve()
|
|
|
|
|
|
|
|
ctx.notifier.confChan <- &chainntnfs.TxConfirmation{}
|
|
|
|
|
|
|
|
// No csv delay, so the input should be swept immediately.
|
|
|
|
<-ctx.sweeper.sweptInputs
|
|
|
|
|
|
|
|
ctx.waitForResult()
|
|
|
|
}
|
|
|
|
|
2020-05-07 13:14:38 +02:00
|
|
|
// testCommitSweepResolverDelay tests resolution of a direct commitment output
|
|
|
|
// that is encumbered by a time lock. sweepErr indicates whether the local node
|
|
|
|
// fails to sweep the output.
|
|
|
|
func testCommitSweepResolverDelay(t *testing.T, sweepErr error) {
|
2019-10-30 13:07:26 +01:00
|
|
|
defer timeout(t)()
|
|
|
|
|
2020-05-07 13:14:38 +02:00
|
|
|
const sweepProcessInterval = 100 * time.Millisecond
|
2019-10-30 13:07:26 +01:00
|
|
|
amt := int64(100)
|
|
|
|
outpoint := wire.OutPoint{
|
|
|
|
Index: 5,
|
|
|
|
}
|
|
|
|
res := lnwallet.CommitOutputResolution{
|
|
|
|
SelfOutputSignDesc: input.SignDescriptor{
|
|
|
|
Output: &wire.TxOut{
|
|
|
|
Value: amt,
|
|
|
|
},
|
2020-03-06 16:11:46 +01:00
|
|
|
WitnessScript: []byte{0},
|
2019-10-30 13:07:26 +01:00
|
|
|
},
|
|
|
|
MaturityDelay: 3,
|
|
|
|
SelfOutPoint: outpoint,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := newCommitSweepResolverTestContext(t, &res)
|
|
|
|
|
2020-05-07 13:14:38 +02:00
|
|
|
// Setup whether we expect the sweeper to receive a sweep error in this
|
|
|
|
// test case.
|
|
|
|
ctx.sweeper.sweepErr = sweepErr
|
|
|
|
|
2019-10-30 13:07:26 +01:00
|
|
|
report := ctx.resolver.report()
|
2020-05-07 13:14:38 +02:00
|
|
|
expectedReport := ContractReport{
|
2019-10-30 13:07:26 +01:00
|
|
|
Outpoint: outpoint,
|
|
|
|
Type: ReportOutputUnencumbered,
|
|
|
|
Amount: btcutil.Amount(amt),
|
|
|
|
LimboBalance: btcutil.Amount(amt),
|
2020-05-07 13:14:38 +02:00
|
|
|
}
|
|
|
|
if *report != expectedReport {
|
|
|
|
t.Fatalf("unexpected resolver report. want=%v got=%v",
|
|
|
|
expectedReport, report)
|
2019-10-30 13:07:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.resolve()
|
|
|
|
|
|
|
|
ctx.notifier.confChan <- &chainntnfs.TxConfirmation{
|
|
|
|
BlockHeight: testInitialBlockHeight - 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow resolver to process confirmation.
|
2020-05-07 13:14:38 +02:00
|
|
|
time.Sleep(sweepProcessInterval)
|
2019-10-30 13:07:26 +01:00
|
|
|
|
|
|
|
// Expect report to be updated.
|
|
|
|
report = ctx.resolver.report()
|
|
|
|
if report.MaturityHeight != testInitialBlockHeight+2 {
|
|
|
|
t.Fatal("report maturity height incorrect")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify initial block height. The csv lock is still in effect, so we
|
|
|
|
// don't expect any sweep to happen yet.
|
|
|
|
ctx.notifyEpoch(testInitialBlockHeight)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.sweeper.sweptInputs:
|
|
|
|
t.Fatal("no sweep expected")
|
2020-05-07 13:14:38 +02:00
|
|
|
case <-time.After(sweepProcessInterval):
|
2019-10-30 13:07:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// A new block arrives. The commit tx confirmed at height -1 and the csv
|
|
|
|
// is 3, so a spend will be valid in the first block after height +1.
|
|
|
|
ctx.notifyEpoch(testInitialBlockHeight + 1)
|
|
|
|
|
|
|
|
<-ctx.sweeper.sweptInputs
|
|
|
|
|
|
|
|
ctx.waitForResult()
|
|
|
|
|
2020-05-07 13:14:38 +02:00
|
|
|
// If this test case generates a sweep error, we don't expect to be
|
|
|
|
// able to recover anything. This might happen if the local commitment
|
|
|
|
// output was swept by a justice transaction by the remote party.
|
|
|
|
expectedRecoveredBalance := btcutil.Amount(amt)
|
|
|
|
if sweepErr != nil {
|
|
|
|
expectedRecoveredBalance = 0
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:07:26 +01:00
|
|
|
report = ctx.resolver.report()
|
2020-05-07 13:14:38 +02:00
|
|
|
expectedReport = ContractReport{
|
2019-10-30 13:07:26 +01:00
|
|
|
Outpoint: outpoint,
|
|
|
|
Type: ReportOutputUnencumbered,
|
|
|
|
Amount: btcutil.Amount(amt),
|
|
|
|
MaturityHeight: testInitialBlockHeight + 2,
|
2020-05-07 13:14:38 +02:00
|
|
|
RecoveredBalance: expectedRecoveredBalance,
|
2020-01-08 13:08:47 +01:00
|
|
|
}
|
|
|
|
if *report != expectedReport {
|
|
|
|
t.Fatalf("unexpected resolver report. want=%v got=%v",
|
|
|
|
expectedReport, report)
|
|
|
|
}
|
|
|
|
|
2020-05-07 13:14:38 +02:00
|
|
|
}
|
2020-01-08 13:08:47 +01:00
|
|
|
|
2020-05-07 13:14:38 +02:00
|
|
|
// TestCommitSweepResolverDelay tests resolution of a direct commitment output
|
|
|
|
// that is encumbered by a time lock.
|
|
|
|
func TestCommitSweepResolverDelay(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-01-08 13:08:47 +01:00
|
|
|
|
2020-05-07 13:14:38 +02:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
sweepErr error
|
|
|
|
}{{
|
|
|
|
name: "success",
|
|
|
|
sweepErr: nil,
|
|
|
|
}, {
|
|
|
|
name: "remote spend",
|
|
|
|
sweepErr: sweep.ErrRemoteSpend,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
ok := t.Run(tc.name, func(t *testing.T) {
|
|
|
|
testCommitSweepResolverDelay(t, tc.sweepErr)
|
|
|
|
})
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2020-01-08 13:08:47 +01:00
|
|
|
}
|
|
|
|
}
|