2019-10-30 15:07:26 +03:00
|
|
|
package contractcourt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2020-07-07 20:49:50 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-10-30 15:07:26 +03:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2021-04-26 20:08:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/kvdb"
|
2020-08-27 22:34:55 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntest/mock"
|
2019-10-30 15:07:26 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2020-03-11 10:52:20 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2019-10-30 15:07:26 +03:00
|
|
|
"github.com/lightningnetwork/lnd/sweep"
|
|
|
|
)
|
|
|
|
|
|
|
|
type commitSweepResolverTestContext struct {
|
|
|
|
resolver *commitSweepResolver
|
2020-08-27 22:34:55 +03:00
|
|
|
notifier *mock.ChainNotifier
|
2019-10-30 15:07:26 +03:00
|
|
|
sweeper *mockSweeper
|
|
|
|
resolverResultChan chan resolveResult
|
|
|
|
t *testing.T
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCommitSweepResolverTestContext(t *testing.T,
|
|
|
|
resolution *lnwallet.CommitOutputResolution) *commitSweepResolverTestContext {
|
|
|
|
|
2020-08-27 22:34:55 +03:00
|
|
|
notifier := &mock.ChainNotifier{
|
|
|
|
EpochChan: make(chan *chainntnfs.BlockEpoch),
|
|
|
|
SpendChan: make(chan *chainntnfs.SpendDetail),
|
|
|
|
ConfChan: make(chan *chainntnfs.TxConfirmation),
|
2019-10-30 15:07:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sweeper := newMockSweeper()
|
|
|
|
|
|
|
|
checkPointChan := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
chainCfg := ChannelArbitratorConfig{
|
|
|
|
ChainArbitratorConfig: ChainArbitratorConfig{
|
|
|
|
Notifier: notifier,
|
|
|
|
Sweeper: sweeper,
|
|
|
|
},
|
2020-07-07 20:49:50 +03:00
|
|
|
PutResolverReport: func(_ kvdb.RwTx,
|
|
|
|
_ *channeldb.ResolverReport) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2019-10-30 15:07:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg := ResolverConfig{
|
|
|
|
ChannelArbitratorConfig: chainCfg,
|
2020-07-07 20:49:51 +03:00
|
|
|
Checkpoint: func(_ ContractResolver,
|
|
|
|
_ ...*channeldb.ResolverReport) error {
|
|
|
|
|
2019-10-30 15:07:26 +03: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) {
|
2020-08-27 22:34:55 +03:00
|
|
|
i.notifier.EpochChan <- &chainntnfs.BlockEpoch{
|
2019-10-30 15:07:26 +03:00
|
|
|
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 {
|
2020-12-09 14:24:03 +03:00
|
|
|
sweptInputs chan input.Input
|
|
|
|
updatedInputs chan wire.OutPoint
|
|
|
|
sweepTx *wire.MsgTx
|
|
|
|
sweepErr error
|
|
|
|
createSweepTxChan chan *wire.MsgTx
|
2021-05-11 15:57:31 +03:00
|
|
|
|
2021-05-11 16:00:00 +03:00
|
|
|
deadlines []int
|
2019-10-30 15:07:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newMockSweeper() *mockSweeper {
|
|
|
|
return &mockSweeper{
|
2021-05-11 15:57:31 +03:00
|
|
|
sweptInputs: make(chan input.Input, 3),
|
2020-12-09 14:24:03 +03:00
|
|
|
updatedInputs: make(chan wire.OutPoint),
|
|
|
|
sweepTx: &wire.MsgTx{},
|
|
|
|
createSweepTxChan: make(chan *wire.MsgTx),
|
2021-05-11 16:00:00 +03:00
|
|
|
deadlines: []int{},
|
2019-10-30 15:07:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 13:42:58 +03:00
|
|
|
func (s *mockSweeper) SweepInput(input input.Input, params sweep.Params) (
|
|
|
|
chan sweep.Result, error) {
|
2019-10-30 15:07:26 +03:00
|
|
|
|
|
|
|
s.sweptInputs <- input
|
|
|
|
|
2021-05-11 15:57:31 +03:00
|
|
|
// Update the deadlines used if it's set.
|
|
|
|
if params.Fee.ConfTarget != 0 {
|
2021-05-11 16:00:00 +03:00
|
|
|
s.deadlines = append(s.deadlines, int(params.Fee.ConfTarget))
|
2021-05-11 15:57:31 +03:00
|
|
|
}
|
|
|
|
|
2019-10-30 15:07:26 +03:00
|
|
|
result := make(chan sweep.Result, 1)
|
|
|
|
result <- sweep.Result{
|
2020-07-07 20:49:53 +03:00
|
|
|
Tx: s.sweepTx,
|
2020-01-08 15:08:47 +03:00
|
|
|
Err: s.sweepErr,
|
2019-10-30 15:07:26 +03:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mockSweeper) CreateSweepTx(inputs []input.Input, feePref sweep.FeePreference,
|
|
|
|
currentBlockHeight uint32) (*wire.MsgTx, error) {
|
|
|
|
|
2020-12-09 14:24:03 +03:00
|
|
|
// We will wait for the test to supply the sweep tx to return.
|
|
|
|
sweepTx := <-s.createSweepTxChan
|
|
|
|
return sweepTx, nil
|
2019-10-30 15:07:26 +03:00
|
|
|
}
|
|
|
|
|
2020-03-11 10:52:20 +03:00
|
|
|
func (s *mockSweeper) RelayFeePerKW() chainfee.SatPerKWeight {
|
|
|
|
return 253
|
|
|
|
}
|
|
|
|
|
2019-11-25 15:17:44 +03: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{
|
2020-07-07 20:49:53 +03:00
|
|
|
Tx: s.sweepTx,
|
2019-11-25 15:17:44 +03:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-10-30 15:07:26 +03: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 18:11:46 +03:00
|
|
|
WitnessScript: []byte{0},
|
2019-10-30 15:07:26 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := newCommitSweepResolverTestContext(t, &res)
|
2020-07-07 20:49:58 +03:00
|
|
|
|
|
|
|
// Replace our checkpoint with one which will push reports into a
|
|
|
|
// channel for us to consume. We replace this function on the resolver
|
|
|
|
// itself because it is created by the test context.
|
|
|
|
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-10-30 15:07:26 +03:00
|
|
|
ctx.resolve()
|
|
|
|
|
2020-07-07 20:49:58 +03:00
|
|
|
spendTx := &wire.MsgTx{}
|
|
|
|
spendHash := spendTx.TxHash()
|
2020-08-27 22:34:55 +03:00
|
|
|
ctx.notifier.ConfChan <- &chainntnfs.TxConfirmation{
|
2020-07-07 20:49:58 +03:00
|
|
|
Tx: spendTx,
|
|
|
|
}
|
2019-10-30 15:07:26 +03:00
|
|
|
|
|
|
|
// No csv delay, so the input should be swept immediately.
|
|
|
|
<-ctx.sweeper.sweptInputs
|
|
|
|
|
2020-07-07 20:49:58 +03:00
|
|
|
amt := btcutil.Amount(res.SelfOutputSignDesc.Output.Value)
|
|
|
|
expectedReport := &channeldb.ResolverReport{
|
|
|
|
OutPoint: wire.OutPoint{},
|
|
|
|
Amount: amt,
|
|
|
|
ResolverType: channeldb.ResolverTypeCommit,
|
|
|
|
ResolverOutcome: channeldb.ResolverOutcomeClaimed,
|
|
|
|
SpendTxID: &spendHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
assertResolverReport(t, reportChan, expectedReport)
|
|
|
|
|
2019-10-30 15:07:26 +03:00
|
|
|
ctx.waitForResult()
|
|
|
|
}
|
|
|
|
|
2020-05-07 14:14:38 +03: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 15:07:26 +03:00
|
|
|
defer timeout(t)()
|
|
|
|
|
2020-05-07 14:14:38 +03:00
|
|
|
const sweepProcessInterval = 100 * time.Millisecond
|
2019-10-30 15:07:26 +03:00
|
|
|
amt := int64(100)
|
|
|
|
outpoint := wire.OutPoint{
|
|
|
|
Index: 5,
|
|
|
|
}
|
|
|
|
res := lnwallet.CommitOutputResolution{
|
|
|
|
SelfOutputSignDesc: input.SignDescriptor{
|
|
|
|
Output: &wire.TxOut{
|
|
|
|
Value: amt,
|
|
|
|
},
|
2020-03-06 18:11:46 +03:00
|
|
|
WitnessScript: []byte{0},
|
2019-10-30 15:07:26 +03:00
|
|
|
},
|
|
|
|
MaturityDelay: 3,
|
|
|
|
SelfOutPoint: outpoint,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := newCommitSweepResolverTestContext(t, &res)
|
|
|
|
|
2020-07-07 20:49:58 +03:00
|
|
|
// Replace our checkpoint with one which will push reports into a
|
|
|
|
// channel for us to consume. We replace this function on the resolver
|
|
|
|
// itself because it is created by the test context.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-07 14:14:38 +03:00
|
|
|
// Setup whether we expect the sweeper to receive a sweep error in this
|
|
|
|
// test case.
|
|
|
|
ctx.sweeper.sweepErr = sweepErr
|
|
|
|
|
2019-10-30 15:07:26 +03:00
|
|
|
report := ctx.resolver.report()
|
2020-05-07 14:14:38 +03:00
|
|
|
expectedReport := ContractReport{
|
2019-10-30 15:07:26 +03:00
|
|
|
Outpoint: outpoint,
|
|
|
|
Type: ReportOutputUnencumbered,
|
|
|
|
Amount: btcutil.Amount(amt),
|
|
|
|
LimboBalance: btcutil.Amount(amt),
|
2020-05-07 14:14:38 +03:00
|
|
|
}
|
|
|
|
if *report != expectedReport {
|
|
|
|
t.Fatalf("unexpected resolver report. want=%v got=%v",
|
|
|
|
expectedReport, report)
|
2019-10-30 15:07:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.resolve()
|
|
|
|
|
2020-08-27 22:34:55 +03:00
|
|
|
ctx.notifier.ConfChan <- &chainntnfs.TxConfirmation{
|
2019-10-30 15:07:26 +03:00
|
|
|
BlockHeight: testInitialBlockHeight - 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow resolver to process confirmation.
|
2020-05-07 14:14:38 +03:00
|
|
|
time.Sleep(sweepProcessInterval)
|
2019-10-30 15:07:26 +03: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 14:14:38 +03:00
|
|
|
case <-time.After(sweepProcessInterval):
|
2019-10-30 15:07:26 +03: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
|
|
|
|
|
2020-07-07 20:49:58 +03:00
|
|
|
// Set the resolution report outcome based on whether our sweep
|
|
|
|
// succeeded.
|
|
|
|
outcome := channeldb.ResolverOutcomeClaimed
|
|
|
|
if sweepErr != nil {
|
|
|
|
outcome = channeldb.ResolverOutcomeUnclaimed
|
|
|
|
}
|
|
|
|
sweepTx := ctx.sweeper.sweepTx.TxHash()
|
|
|
|
|
|
|
|
assertResolverReport(t, reportChan, &channeldb.ResolverReport{
|
|
|
|
OutPoint: outpoint,
|
|
|
|
ResolverType: channeldb.ResolverTypeCommit,
|
|
|
|
ResolverOutcome: outcome,
|
|
|
|
Amount: btcutil.Amount(amt),
|
|
|
|
SpendTxID: &sweepTx,
|
|
|
|
})
|
|
|
|
|
2019-10-30 15:07:26 +03:00
|
|
|
ctx.waitForResult()
|
|
|
|
|
2020-05-07 14:14:38 +03: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 15:07:26 +03:00
|
|
|
report = ctx.resolver.report()
|
2020-05-07 14:14:38 +03:00
|
|
|
expectedReport = ContractReport{
|
2019-10-30 15:07:26 +03:00
|
|
|
Outpoint: outpoint,
|
|
|
|
Type: ReportOutputUnencumbered,
|
|
|
|
Amount: btcutil.Amount(amt),
|
|
|
|
MaturityHeight: testInitialBlockHeight + 2,
|
2020-05-07 14:14:38 +03:00
|
|
|
RecoveredBalance: expectedRecoveredBalance,
|
2020-01-08 15:08:47 +03:00
|
|
|
}
|
|
|
|
if *report != expectedReport {
|
|
|
|
t.Fatalf("unexpected resolver report. want=%v got=%v",
|
|
|
|
expectedReport, report)
|
|
|
|
}
|
|
|
|
|
2020-05-07 14:14:38 +03:00
|
|
|
}
|
2020-01-08 15:08:47 +03:00
|
|
|
|
2020-05-07 14:14:38 +03: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 15:08:47 +03:00
|
|
|
|
2020-05-07 14:14:38 +03: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 15:08:47 +03:00
|
|
|
}
|
|
|
|
}
|