2019-03-07 02:22:46 +00:00
|
|
|
package loop
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2021-05-24 06:46:03 +00:00
|
|
|
"math"
|
2019-03-06 20:13:50 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-10-01 15:21:19 +00:00
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
2022-06-24 11:57:42 +00:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
2022-03-14 12:36:02 +00:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2019-10-01 15:21:19 +00:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2020-06-17 20:25:57 +00:00
|
|
|
"github.com/lightninglabs/lndclient"
|
2019-03-07 04:32:24 +00:00
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
2019-03-06 23:29:44 +00:00
|
|
|
"github.com/lightninglabs/loop/sweep"
|
2024-01-18 11:26:14 +00:00
|
|
|
"github.com/lightninglabs/loop/sweepbatcher"
|
2019-03-06 23:29:44 +00:00
|
|
|
"github.com/lightninglabs/loop/test"
|
2020-06-02 07:31:39 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2021-05-24 06:46:03 +00:00
|
|
|
"github.com/lightningnetwork/lnd/zpay32"
|
2020-06-02 07:31:39 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
2020-05-20 09:38:27 +00:00
|
|
|
// TestLoopOutPaymentParameters tests the first part of the loop out process up
|
|
|
|
// to the point where the off-chain payments are made.
|
|
|
|
func TestLoopOutPaymentParameters(t *testing.T) {
|
2022-04-24 20:59:59 +00:00
|
|
|
t.Run("stable protocol", func(t *testing.T) {
|
|
|
|
testLoopOutPaymentParameters(t)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("experimental protocol", func(t *testing.T) {
|
|
|
|
loopdb.EnableExperimentalProtocol()
|
|
|
|
defer loopdb.ResetCurrentProtocolVersion()
|
|
|
|
|
|
|
|
testLoopOutPaymentParameters(t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestLoopOutPaymentParameters tests the first part of the loop out process up
|
|
|
|
// to the point where the off-chain payments are made.
|
|
|
|
func testLoopOutPaymentParameters(t *testing.T) {
|
2020-05-20 09:38:27 +00:00
|
|
|
defer test.Guard(t)()
|
|
|
|
|
|
|
|
// Set up test context objects.
|
|
|
|
lnd := test.NewMockLnd()
|
|
|
|
ctx := test.NewContext(t, lnd)
|
2020-05-29 09:27:47 +00:00
|
|
|
server := newServerMock(lnd)
|
2024-01-18 11:26:07 +00:00
|
|
|
store := loopdb.NewStoreMock(t)
|
2020-05-20 09:38:27 +00:00
|
|
|
|
|
|
|
expiryChan := make(chan time.Time)
|
|
|
|
timerFactory := func(_ time.Duration) <-chan time.Time {
|
|
|
|
return expiryChan
|
|
|
|
}
|
|
|
|
|
|
|
|
height := int32(600)
|
|
|
|
|
|
|
|
cfg := &swapConfig{
|
|
|
|
lnd: &lnd.LndServices,
|
|
|
|
store: store,
|
|
|
|
server: server,
|
|
|
|
}
|
|
|
|
|
|
|
|
sweeper := &sweep.Sweeper{Lnd: &lnd.LndServices}
|
|
|
|
|
|
|
|
blockEpochChan := make(chan interface{})
|
|
|
|
statusChan := make(chan SwapInfo)
|
|
|
|
|
2022-11-29 20:36:14 +00:00
|
|
|
const maxParts = uint32(5)
|
2020-05-20 09:38:27 +00:00
|
|
|
|
2022-05-27 09:14:16 +00:00
|
|
|
chanSet := loopdb.ChannelSet{2, 3}
|
|
|
|
|
2020-05-20 09:38:27 +00:00
|
|
|
// Initiate the swap.
|
2020-05-19 07:54:27 +00:00
|
|
|
req := *testRequest
|
2022-05-27 09:14:16 +00:00
|
|
|
req.OutgoingChanSet = chanSet
|
2020-05-19 07:54:27 +00:00
|
|
|
|
2020-06-30 12:06:36 +00:00
|
|
|
initResult, err := newLoopOutSwap(
|
2020-05-19 07:54:27 +00:00
|
|
|
context.Background(), cfg, height, &req,
|
2020-05-20 09:38:27 +00:00
|
|
|
)
|
2022-11-29 20:36:14 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-30 12:06:36 +00:00
|
|
|
swap := initResult.swap
|
2020-05-20 09:38:27 +00:00
|
|
|
|
|
|
|
// Execute the swap in its own goroutine.
|
|
|
|
errChan := make(chan error)
|
|
|
|
swapCtx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := swap.execute(swapCtx, &executeConfig{
|
2022-06-24 11:57:42 +00:00
|
|
|
statusChan: statusChan,
|
|
|
|
sweeper: sweeper,
|
|
|
|
blockEpochChan: blockEpochChan,
|
|
|
|
timerFactory: timerFactory,
|
|
|
|
loopOutMaxParts: maxParts,
|
|
|
|
cancelSwap: server.CancelLoopOutSwap,
|
|
|
|
verifySchnorrSig: mockVerifySchnorrSigFail,
|
2020-05-20 09:38:27 +00:00
|
|
|
}, height)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
errChan <- err
|
|
|
|
}()
|
|
|
|
|
2024-01-18 11:26:07 +00:00
|
|
|
store.AssertLoopOutStored()
|
2020-05-20 09:38:27 +00:00
|
|
|
|
|
|
|
state := <-statusChan
|
2022-11-29 20:36:14 +00:00
|
|
|
require.Equal(t, loopdb.StateInitiated, state.State)
|
2020-05-20 09:38:27 +00:00
|
|
|
|
2022-05-27 09:14:16 +00:00
|
|
|
// Check that the SwapInfo contains the outgoing chan set
|
|
|
|
require.Equal(t, chanSet, state.OutgoingChanSet)
|
|
|
|
|
|
|
|
// Check that the SwapInfo does not contain a last hop
|
|
|
|
require.Nil(t, state.LastHop)
|
|
|
|
|
2020-05-20 09:38:27 +00:00
|
|
|
// Intercept the swap and prepay payments. Order is undefined.
|
|
|
|
payments := []test.RouterPaymentChannelMessage{
|
|
|
|
<-ctx.Lnd.RouterSendPaymentChannel,
|
|
|
|
<-ctx.Lnd.RouterSendPaymentChannel,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the swap payment.
|
|
|
|
var swapPayment test.RouterPaymentChannelMessage
|
|
|
|
for _, p := range payments {
|
|
|
|
if p.Invoice == swap.SwapInvoice {
|
|
|
|
swapPayment = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that it is sent as a multi-part payment.
|
2022-11-29 20:36:14 +00:00
|
|
|
require.Equal(t, maxParts, swapPayment.MaxParts)
|
2020-05-20 09:38:27 +00:00
|
|
|
|
2020-05-19 07:54:27 +00:00
|
|
|
// Verify the outgoing channel set restriction.
|
2022-11-29 20:36:14 +00:00
|
|
|
require.Equal(
|
|
|
|
t, []uint64(req.OutgoingChanSet), swapPayment.OutgoingChanIds,
|
|
|
|
)
|
2020-05-19 07:54:27 +00:00
|
|
|
|
2020-05-20 09:38:27 +00:00
|
|
|
// Swap is expected to register for confirmation of the htlc. Assert
|
|
|
|
// this to prevent a blocked channel in the mock.
|
2020-08-04 18:28:06 +00:00
|
|
|
ctx.AssertRegisterConf(false, defaultConfirmations)
|
2020-05-20 09:38:27 +00:00
|
|
|
|
|
|
|
// Cancel the swap. There is nothing else we need to assert. The payment
|
|
|
|
// parameters don't play a role in the remainder of the swap process.
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
// Expect the swap to signal that it was cancelled.
|
2022-11-29 20:36:14 +00:00
|
|
|
require.Equal(t, context.Canceled, <-errChan)
|
2020-05-20 09:38:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
// TestLateHtlcPublish tests that the client is not revealing the preimage if
|
|
|
|
// there are not enough blocks left.
|
|
|
|
func TestLateHtlcPublish(t *testing.T) {
|
2022-04-24 20:59:59 +00:00
|
|
|
t.Run("stable protocol", func(t *testing.T) {
|
|
|
|
testLateHtlcPublish(t)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("experimental protocol", func(t *testing.T) {
|
|
|
|
loopdb.EnableExperimentalProtocol()
|
|
|
|
defer loopdb.ResetCurrentProtocolVersion()
|
|
|
|
|
|
|
|
testLateHtlcPublish(t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLateHtlcPublish(t *testing.T) {
|
2019-03-06 20:13:50 +00:00
|
|
|
defer test.Guard(t)()
|
|
|
|
|
|
|
|
lnd := test.NewMockLnd()
|
|
|
|
|
|
|
|
ctx := test.NewContext(t, lnd)
|
|
|
|
|
2020-05-29 09:27:47 +00:00
|
|
|
server := newServerMock(lnd)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2024-01-18 11:26:07 +00:00
|
|
|
store := loopdb.NewStoreMock(t)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
expiryChan := make(chan time.Time)
|
|
|
|
timerFactory := func(expiry time.Duration) <-chan time.Time {
|
|
|
|
return expiryChan
|
|
|
|
}
|
|
|
|
|
|
|
|
height := int32(600)
|
|
|
|
|
2020-06-08 10:53:07 +00:00
|
|
|
cfg := newSwapConfig(&lnd.LndServices, store, server)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-07-14 12:54:06 +00:00
|
|
|
testRequest.Expiry = height + testLoopOutMinOnChainCltvDelta
|
|
|
|
|
2020-06-30 12:06:36 +00:00
|
|
|
initResult, err := newLoopOutSwap(
|
2019-03-06 20:13:50 +00:00
|
|
|
context.Background(), cfg, height, testRequest,
|
|
|
|
)
|
2022-11-29 20:36:14 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-30 12:06:36 +00:00
|
|
|
swap := initResult.swap
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
sweeper := &sweep.Sweeper{Lnd: &lnd.LndServices}
|
|
|
|
|
|
|
|
blockEpochChan := make(chan interface{})
|
|
|
|
statusChan := make(chan SwapInfo)
|
|
|
|
|
|
|
|
errChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
err := swap.execute(context.Background(), &executeConfig{
|
2022-06-24 11:57:42 +00:00
|
|
|
statusChan: statusChan,
|
|
|
|
sweeper: sweeper,
|
|
|
|
blockEpochChan: blockEpochChan,
|
|
|
|
timerFactory: timerFactory,
|
|
|
|
cancelSwap: server.CancelLoopOutSwap,
|
|
|
|
verifySchnorrSig: mockVerifySchnorrSigFail,
|
2019-03-06 20:13:50 +00:00
|
|
|
}, height)
|
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Error(err)
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
errChan <- err
|
|
|
|
}()
|
|
|
|
|
2024-01-18 11:26:07 +00:00
|
|
|
store.AssertLoopOutStored()
|
2022-11-29 20:36:14 +00:00
|
|
|
status := <-statusChan
|
|
|
|
require.Equal(t, loopdb.StateInitiated, status.State)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
signalSwapPaymentResult := ctx.AssertPaid(swapInvoiceDesc)
|
|
|
|
signalPrepaymentResult := ctx.AssertPaid(prepayInvoiceDesc)
|
|
|
|
|
|
|
|
// Expect client to register for conf
|
2020-08-04 18:28:06 +00:00
|
|
|
ctx.AssertRegisterConf(false, defaultConfirmations)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
// // Wait too long before publishing htlc.
|
2022-12-15 15:43:38 +00:00
|
|
|
blockEpochChan <- swap.CltvExpiry - 10
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
signalSwapPaymentResult(
|
|
|
|
errors.New(lndclient.PaymentResultUnknownPaymentHash),
|
|
|
|
)
|
|
|
|
signalPrepaymentResult(
|
|
|
|
errors.New(lndclient.PaymentResultUnknownPaymentHash),
|
|
|
|
)
|
|
|
|
|
2024-01-18 11:26:07 +00:00
|
|
|
store.AssertStoreFinished(loopdb.StateFailTimeout)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2022-11-29 20:36:14 +00:00
|
|
|
status = <-statusChan
|
|
|
|
require.Equal(t, loopdb.StateFailTimeout, status.State)
|
|
|
|
require.NoError(t, <-errChan)
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
2019-10-01 15:21:19 +00:00
|
|
|
|
|
|
|
// TestCustomSweepConfTarget ensures we are able to sweep a Loop Out HTLC with a
|
|
|
|
// custom confirmation target.
|
|
|
|
func TestCustomSweepConfTarget(t *testing.T) {
|
2022-04-24 20:59:59 +00:00
|
|
|
t.Run("stable protocol", func(t *testing.T) {
|
|
|
|
testCustomSweepConfTarget(t)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("experimental protocol", func(t *testing.T) {
|
|
|
|
loopdb.EnableExperimentalProtocol()
|
|
|
|
defer loopdb.ResetCurrentProtocolVersion()
|
|
|
|
|
|
|
|
testCustomSweepConfTarget(t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCustomSweepConfTarget(t *testing.T) {
|
2019-10-01 15:21:19 +00:00
|
|
|
defer test.Guard(t)()
|
|
|
|
|
|
|
|
lnd := test.NewMockLnd()
|
|
|
|
ctx := test.NewContext(t, lnd)
|
2020-05-29 09:27:47 +00:00
|
|
|
server := newServerMock(lnd)
|
2019-10-01 15:21:19 +00:00
|
|
|
|
|
|
|
// Use the highest sweep confirmation target before we attempt to use
|
|
|
|
// the default.
|
2020-07-15 14:44:48 +00:00
|
|
|
testReq := *testRequest
|
|
|
|
|
2020-07-14 12:54:06 +00:00
|
|
|
testReq.SweepConfTarget = testLoopOutMinOnChainCltvDelta -
|
2019-10-01 15:21:19 +00:00
|
|
|
DefaultSweepConfTargetDelta - 1
|
|
|
|
|
2021-04-09 07:14:54 +00:00
|
|
|
// Set on-chain HTLC CLTV.
|
|
|
|
testReq.Expiry = ctx.Lnd.Height + testLoopOutMinOnChainCltvDelta
|
|
|
|
|
2019-10-01 15:21:19 +00:00
|
|
|
// Set up custom fee estimates such that the lower confirmation target
|
|
|
|
// yields a much higher fee rate.
|
2020-07-15 14:44:48 +00:00
|
|
|
ctx.Lnd.SetFeeEstimate(testReq.SweepConfTarget, 250)
|
2019-10-01 15:21:19 +00:00
|
|
|
ctx.Lnd.SetFeeEstimate(DefaultSweepConfTarget, 10000)
|
|
|
|
|
2020-06-08 10:53:07 +00:00
|
|
|
cfg := newSwapConfig(
|
2024-01-18 11:26:07 +00:00
|
|
|
&lnd.LndServices, loopdb.NewStoreMock(t), server,
|
2020-06-08 10:53:07 +00:00
|
|
|
)
|
|
|
|
|
2020-06-30 12:06:36 +00:00
|
|
|
initResult, err := newLoopOutSwap(
|
2020-07-15 14:44:48 +00:00
|
|
|
context.Background(), cfg, ctx.Lnd.Height, &testReq,
|
2019-10-01 15:21:19 +00:00
|
|
|
)
|
2022-11-29 20:36:14 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-30 12:06:36 +00:00
|
|
|
swap := initResult.swap
|
2019-10-01 15:21:19 +00:00
|
|
|
|
|
|
|
// Set up the required dependencies to execute the swap.
|
|
|
|
//
|
|
|
|
// TODO: create test context similar to loopInTestContext.
|
|
|
|
sweeper := &sweep.Sweeper{Lnd: &lnd.LndServices}
|
|
|
|
blockEpochChan := make(chan interface{})
|
|
|
|
statusChan := make(chan SwapInfo)
|
|
|
|
expiryChan := make(chan time.Time)
|
|
|
|
timerFactory := func(expiry time.Duration) <-chan time.Time {
|
|
|
|
return expiryChan
|
|
|
|
}
|
|
|
|
|
2024-01-18 11:26:14 +00:00
|
|
|
errChan := make(chan error, 2)
|
|
|
|
|
|
|
|
batcherStore := sweepbatcher.NewStoreMock()
|
|
|
|
|
|
|
|
batcher := sweepbatcher.NewBatcher(
|
|
|
|
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
|
|
|
|
mockMuSig2SignSweep, mockVerifySchnorrSigSuccess,
|
|
|
|
lnd.ChainParams, batcherStore, cfg.store,
|
|
|
|
)
|
|
|
|
|
|
|
|
tctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2019-10-01 15:21:19 +00:00
|
|
|
go func() {
|
2024-01-18 11:26:14 +00:00
|
|
|
err := batcher.Run(tctx)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := swap.execute(tctx, &executeConfig{
|
2022-06-24 11:57:42 +00:00
|
|
|
statusChan: statusChan,
|
|
|
|
blockEpochChan: blockEpochChan,
|
|
|
|
timerFactory: timerFactory,
|
|
|
|
sweeper: sweeper,
|
2024-01-18 11:26:14 +00:00
|
|
|
batcher: batcher,
|
2022-06-24 11:57:42 +00:00
|
|
|
cancelSwap: server.CancelLoopOutSwap,
|
|
|
|
verifySchnorrSig: mockVerifySchnorrSigFail,
|
2019-10-01 15:21:19 +00:00
|
|
|
}, ctx.Lnd.Height)
|
|
|
|
if err != nil {
|
2019-10-28 16:06:07 +00:00
|
|
|
log.Error(err)
|
2019-10-01 15:21:19 +00:00
|
|
|
}
|
|
|
|
errChan <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
// The swap should be found in its initial state.
|
2024-01-18 11:26:07 +00:00
|
|
|
cfg.store.(*loopdb.StoreMock).AssertLoopOutStored()
|
2019-10-01 15:21:19 +00:00
|
|
|
state := <-statusChan
|
2022-11-29 20:36:14 +00:00
|
|
|
require.Equal(t, loopdb.StateInitiated, state.State)
|
2019-10-01 15:21:19 +00:00
|
|
|
|
|
|
|
// We'll then pay both the swap and prepay invoice, which should trigger
|
|
|
|
// the server to publish the on-chain HTLC.
|
|
|
|
signalSwapPaymentResult := ctx.AssertPaid(swapInvoiceDesc)
|
|
|
|
signalPrepaymentResult := ctx.AssertPaid(prepayInvoiceDesc)
|
|
|
|
|
|
|
|
signalSwapPaymentResult(nil)
|
|
|
|
signalPrepaymentResult(nil)
|
|
|
|
|
|
|
|
// Notify the confirmation notification for the HTLC.
|
2020-08-04 18:28:06 +00:00
|
|
|
ctx.AssertRegisterConf(false, defaultConfirmations)
|
2019-10-01 15:21:19 +00:00
|
|
|
|
2019-10-07 15:29:24 +00:00
|
|
|
blockEpochChan <- ctx.Lnd.Height + 1
|
2019-10-01 15:21:19 +00:00
|
|
|
|
|
|
|
htlcTx := wire.NewMsgTx(2)
|
|
|
|
htlcTx.AddTxOut(&wire.TxOut{
|
|
|
|
Value: int64(swap.AmountRequested),
|
|
|
|
PkScript: swap.htlc.PkScript,
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.NotifyConf(htlcTx)
|
|
|
|
|
2020-06-02 07:31:39 +00:00
|
|
|
// Assert that we made a query to track our payment, as required for
|
|
|
|
// preimage push tracking.
|
|
|
|
trackPayment := ctx.AssertTrackPayment()
|
|
|
|
|
2019-10-01 15:21:19 +00:00
|
|
|
expiryChan <- time.Now()
|
|
|
|
|
2024-01-18 11:26:14 +00:00
|
|
|
// The client should then register for a spend of the HTLC and attempt
|
|
|
|
// to sweep it using the custom confirmation target.
|
|
|
|
ctx.AssertRegisterSpendNtfn(swap.htlc.PkScript)
|
|
|
|
|
|
|
|
ctx.AssertEpochListeners(1)
|
|
|
|
|
|
|
|
err = ctx.Lnd.NotifyHeight(ctx.Lnd.Height + 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-15 08:55:22 +00:00
|
|
|
// Expect a signing request for the HTLC success transaction.
|
2022-05-05 09:59:31 +00:00
|
|
|
if !IsTaprootSwap(&swap.SwapContract) {
|
|
|
|
<-ctx.Lnd.SignOutputRawChannel
|
|
|
|
}
|
2020-01-15 08:55:22 +00:00
|
|
|
|
2024-01-18 11:26:07 +00:00
|
|
|
cfg.store.(*loopdb.StoreMock).AssertLoopOutState(loopdb.StatePreimageRevealed)
|
2019-10-01 15:21:19 +00:00
|
|
|
status := <-statusChan
|
2022-11-29 20:36:14 +00:00
|
|
|
require.Equal(t, loopdb.StatePreimageRevealed, status.State)
|
2019-10-01 15:21:19 +00:00
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
// When using taproot htlcs the flow is different as we do reveal the
|
|
|
|
// preimage before sweeping in order for the server to trust us with
|
|
|
|
// our MuSig2 signing attempts.
|
|
|
|
if IsTaprootSwap(&swap.SwapContract) {
|
|
|
|
preimage := <-server.preimagePush
|
|
|
|
require.Equal(t, swap.Preimage, preimage)
|
|
|
|
|
|
|
|
// Try MuSig2 signing first and fail it so that we go for a
|
|
|
|
// normal sweep.
|
|
|
|
for i := 0; i < maxMusigSweepRetries; i++ {
|
|
|
|
expiryChan <- time.Now()
|
|
|
|
preimage := <-server.preimagePush
|
|
|
|
require.Equal(t, swap.Preimage, preimage)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-ctx.Lnd.SignOutputRawChannel
|
|
|
|
}
|
|
|
|
|
2019-10-01 15:21:19 +00:00
|
|
|
// assertSweepTx performs some sanity checks on a sweep transaction to
|
|
|
|
// ensure it was constructed correctly.
|
|
|
|
assertSweepTx := func(expConfTarget int32) *wire.MsgTx {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
sweepTx := ctx.ReceiveTx()
|
2022-11-29 20:36:14 +00:00
|
|
|
require.Equal(
|
|
|
|
t, htlcTx.TxHash(),
|
|
|
|
sweepTx.TxIn[0].PreviousOutPoint.Hash,
|
|
|
|
)
|
2019-10-01 15:21:19 +00:00
|
|
|
|
|
|
|
// The fee used for the sweep transaction is an estimate based
|
|
|
|
// on the maximum witness size, so we should expect to see a
|
|
|
|
// lower fee when using the actual witness size of the
|
|
|
|
// transaction.
|
|
|
|
fee := btcutil.Amount(
|
|
|
|
htlcTx.TxOut[0].Value - sweepTx.TxOut[0].Value,
|
|
|
|
)
|
|
|
|
|
|
|
|
weight := blockchain.GetTransactionWeight(btcutil.NewTx(sweepTx))
|
2022-06-02 16:26:17 +00:00
|
|
|
feeRate, err := ctx.Lnd.WalletKit.EstimateFeeRate(
|
2019-10-01 15:21:19 +00:00
|
|
|
context.Background(), expConfTarget,
|
|
|
|
)
|
2022-11-29 20:36:14 +00:00
|
|
|
require.NoError(t, err, "unable to retrieve fee estimate")
|
|
|
|
|
2019-10-01 15:21:19 +00:00
|
|
|
minFee := feeRate.FeeForWeight(weight)
|
2022-11-29 20:36:14 +00:00
|
|
|
// Just an estimate that works to sanity check fee upper bound.
|
|
|
|
maxFee := btcutil.Amount(float64(minFee) * 1.5)
|
2019-10-01 15:21:19 +00:00
|
|
|
|
2022-11-29 20:36:14 +00:00
|
|
|
require.GreaterOrEqual(t, fee, minFee)
|
|
|
|
require.LessOrEqual(t, fee, maxFee)
|
2019-10-01 15:21:19 +00:00
|
|
|
|
|
|
|
return sweepTx
|
|
|
|
}
|
|
|
|
|
|
|
|
// The sweep should have a fee that corresponds to the custom
|
|
|
|
// confirmation target.
|
2024-01-18 11:26:14 +00:00
|
|
|
sweepTx := assertSweepTx(testReq.SweepConfTarget)
|
2019-10-01 15:21:19 +00:00
|
|
|
|
2020-06-02 07:31:39 +00:00
|
|
|
// Once we have published an on chain sweep, we expect a preimage to
|
|
|
|
// have been pushed to our server.
|
2022-05-05 09:59:31 +00:00
|
|
|
if !IsTaprootSwap(&swap.SwapContract) {
|
|
|
|
preimage := <-server.preimagePush
|
|
|
|
require.Equal(t, swap.Preimage, preimage)
|
|
|
|
}
|
2020-06-02 07:31:39 +00:00
|
|
|
|
|
|
|
// Now that we have pushed our preimage to the sever, we send an update
|
|
|
|
// indicating that our off chain htlc is settled. We do this so that
|
|
|
|
// we don't have to keep consuming preimage pushes from our server mock
|
|
|
|
// for every sweep attempt.
|
|
|
|
trackPayment.Updates <- lndclient.PaymentStatus{
|
|
|
|
State: lnrpc.Payment_SUCCEEDED,
|
|
|
|
}
|
|
|
|
|
2024-01-18 11:26:14 +00:00
|
|
|
// Notify the batch for the spend.
|
2019-10-01 15:21:19 +00:00
|
|
|
ctx.NotifySpend(sweepTx, 0)
|
|
|
|
|
2024-01-18 11:26:14 +00:00
|
|
|
// After receiving the notification the batch will start monitoring the
|
|
|
|
// confirmations.
|
|
|
|
ctx.AssertRegisterConf(true, 3)
|
|
|
|
|
2024-01-18 11:26:07 +00:00
|
|
|
cfg.store.(*loopdb.StoreMock).AssertLoopOutState(loopdb.StateSuccess)
|
2019-10-01 15:21:19 +00:00
|
|
|
status = <-statusChan
|
2022-11-29 20:36:14 +00:00
|
|
|
require.Equal(t, loopdb.StateSuccess, status.State)
|
|
|
|
require.NoError(t, <-errChan)
|
2019-10-01 15:21:19 +00:00
|
|
|
}
|
2020-06-02 07:31:39 +00:00
|
|
|
|
|
|
|
// TestPreimagePush tests or logic that decides whether to push our preimage to
|
|
|
|
// the server. First, we test the case where we have not yet disclosed our
|
|
|
|
// preimage with a sweep, so we do not want to push our preimage yet. Next, we
|
|
|
|
// broadcast a sweep attempt and push our preimage to the server. In this stage
|
|
|
|
// we mock a server failure by not sending a settle update for our payment.
|
|
|
|
// Finally, we make a last sweep attempt, push the preimage (because we have
|
|
|
|
// not detected our settle) and settle the off chain htlc, indicating that the
|
|
|
|
// server successfully settled using the preimage push. In this test, we need
|
|
|
|
// to start with a fee rate that will be too high, then progress to an
|
2021-04-22 07:28:32 +00:00
|
|
|
// acceptable one.
|
2020-06-02 07:31:39 +00:00
|
|
|
func TestPreimagePush(t *testing.T) {
|
2022-04-24 20:59:59 +00:00
|
|
|
t.Run("stable protocol", func(t *testing.T) {
|
|
|
|
testPreimagePush(t)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("experimental protocol", func(t *testing.T) {
|
|
|
|
loopdb.EnableExperimentalProtocol()
|
|
|
|
defer loopdb.ResetCurrentProtocolVersion()
|
|
|
|
|
|
|
|
testPreimagePush(t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPreimagePush(t *testing.T) {
|
2020-06-02 07:31:39 +00:00
|
|
|
defer test.Guard(t)()
|
|
|
|
|
|
|
|
lnd := test.NewMockLnd()
|
|
|
|
ctx := test.NewContext(t, lnd)
|
2020-05-29 09:27:47 +00:00
|
|
|
server := newServerMock(lnd)
|
2020-06-02 07:31:39 +00:00
|
|
|
|
2020-07-15 14:44:48 +00:00
|
|
|
testReq := *testRequest
|
2021-04-22 07:28:32 +00:00
|
|
|
testReq.SweepConfTarget = 10
|
2020-07-14 12:54:06 +00:00
|
|
|
testReq.Expiry = ctx.Lnd.Height + testLoopOutMinOnChainCltvDelta
|
2020-06-02 07:31:39 +00:00
|
|
|
|
|
|
|
// We set our mock fee estimate for our target sweep confs to be our
|
|
|
|
// max miner fee *2, so that our fee will definitely be above what we
|
|
|
|
// are willing to pay, and we will not sweep.
|
|
|
|
ctx.Lnd.SetFeeEstimate(
|
2020-07-15 14:44:48 +00:00
|
|
|
testReq.SweepConfTarget, chainfee.SatPerKWeight(
|
|
|
|
testReq.MaxMinerFee*2,
|
2020-06-02 07:31:39 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
cfg := newSwapConfig(
|
2024-01-18 11:26:07 +00:00
|
|
|
&lnd.LndServices, loopdb.NewStoreMock(t), server,
|
2020-06-02 07:31:39 +00:00
|
|
|
)
|
|
|
|
|
2020-06-30 12:06:36 +00:00
|
|
|
initResult, err := newLoopOutSwap(
|
2020-07-15 14:44:48 +00:00
|
|
|
context.Background(), cfg, ctx.Lnd.Height, &testReq,
|
2020-06-02 07:31:39 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2020-06-30 12:06:36 +00:00
|
|
|
swap := initResult.swap
|
2020-06-02 07:31:39 +00:00
|
|
|
|
|
|
|
// Set up the required dependencies to execute the swap.
|
|
|
|
sweeper := &sweep.Sweeper{Lnd: &lnd.LndServices}
|
|
|
|
blockEpochChan := make(chan interface{})
|
|
|
|
statusChan := make(chan SwapInfo)
|
|
|
|
expiryChan := make(chan time.Time)
|
|
|
|
timerFactory := func(_ time.Duration) <-chan time.Time {
|
|
|
|
return expiryChan
|
|
|
|
}
|
|
|
|
|
2024-01-18 11:26:14 +00:00
|
|
|
errChan := make(chan error, 2)
|
|
|
|
|
|
|
|
batcherStore := sweepbatcher.NewStoreMock()
|
|
|
|
|
|
|
|
batcher := sweepbatcher.NewBatcher(
|
|
|
|
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
|
|
|
|
mockMuSig2SignSweep, mockVerifySchnorrSigSuccess,
|
|
|
|
lnd.ChainParams, batcherStore, cfg.store,
|
|
|
|
)
|
|
|
|
|
|
|
|
tctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := batcher.Run(tctx)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-06-02 07:31:39 +00:00
|
|
|
go func() {
|
|
|
|
err := swap.execute(context.Background(), &executeConfig{
|
2022-06-24 11:57:42 +00:00
|
|
|
statusChan: statusChan,
|
|
|
|
blockEpochChan: blockEpochChan,
|
|
|
|
timerFactory: timerFactory,
|
|
|
|
sweeper: sweeper,
|
2024-01-18 11:26:14 +00:00
|
|
|
batcher: batcher,
|
2022-06-24 11:57:42 +00:00
|
|
|
cancelSwap: server.CancelLoopOutSwap,
|
|
|
|
verifySchnorrSig: mockVerifySchnorrSigFail,
|
2020-06-02 07:31:39 +00:00
|
|
|
}, ctx.Lnd.Height)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
errChan <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
// The swap should be found in its initial state.
|
2024-01-18 11:26:07 +00:00
|
|
|
cfg.store.(*loopdb.StoreMock).AssertLoopOutStored()
|
2020-06-02 07:31:39 +00:00
|
|
|
state := <-statusChan
|
|
|
|
require.Equal(t, loopdb.StateInitiated, state.State)
|
|
|
|
|
|
|
|
// We'll then pay both the swap and prepay invoice, which should trigger
|
|
|
|
// the server to publish the on-chain HTLC.
|
|
|
|
signalSwapPaymentResult := ctx.AssertPaid(swapInvoiceDesc)
|
|
|
|
signalPrepaymentResult := ctx.AssertPaid(prepayInvoiceDesc)
|
|
|
|
|
|
|
|
signalSwapPaymentResult(nil)
|
|
|
|
signalPrepaymentResult(nil)
|
|
|
|
|
|
|
|
// Notify the confirmation notification for the HTLC.
|
2020-08-04 18:28:06 +00:00
|
|
|
ctx.AssertRegisterConf(false, defaultConfirmations)
|
2020-06-02 07:31:39 +00:00
|
|
|
|
|
|
|
blockEpochChan <- ctx.Lnd.Height + 1
|
|
|
|
|
|
|
|
htlcTx := wire.NewMsgTx(2)
|
|
|
|
htlcTx.AddTxOut(&wire.TxOut{
|
|
|
|
Value: int64(swap.AmountRequested),
|
|
|
|
PkScript: swap.htlc.PkScript,
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.NotifyConf(htlcTx)
|
|
|
|
|
|
|
|
// Assert that we made a query to track our payment, as required for
|
|
|
|
// preimage push tracking.
|
|
|
|
trackPayment := ctx.AssertTrackPayment()
|
|
|
|
|
|
|
|
// Tick the expiry channel, we are still using our client confirmation
|
|
|
|
// target at this stage which has fees higher than our max acceptable
|
|
|
|
// fee. We do not expect a sweep attempt at this point. Since our
|
|
|
|
// preimage is not revealed, we also do not expect a preimage push.
|
|
|
|
expiryChan <- testTime
|
|
|
|
|
2024-01-18 11:26:14 +00:00
|
|
|
// The client should then register for a spend of the HTLC and attempt
|
|
|
|
// to sweep it using the custom confirmation target.
|
|
|
|
ctx.AssertRegisterSpendNtfn(swap.htlc.PkScript)
|
|
|
|
|
|
|
|
ctx.AssertEpochListeners(1)
|
|
|
|
|
|
|
|
err = ctx.Lnd.NotifyHeight(ctx.Lnd.Height + 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
// When using taproot htlcs the flow is different as we do reveal the
|
|
|
|
// preimage before sweeping in order for the server to trust us with
|
|
|
|
// our MuSig2 signing attempts.
|
|
|
|
if IsTaprootSwap(&swap.SwapContract) {
|
2024-01-18 11:26:07 +00:00
|
|
|
cfg.store.(*loopdb.StoreMock).AssertLoopOutState(
|
2022-05-05 09:59:31 +00:00
|
|
|
loopdb.StatePreimageRevealed,
|
|
|
|
)
|
|
|
|
status := <-statusChan
|
|
|
|
require.Equal(
|
|
|
|
t, status.State, loopdb.StatePreimageRevealed,
|
|
|
|
)
|
|
|
|
|
|
|
|
preimage := <-server.preimagePush
|
|
|
|
require.Equal(t, swap.Preimage, preimage)
|
|
|
|
|
|
|
|
<-ctx.Lnd.SignOutputRawChannel
|
|
|
|
|
|
|
|
// We expect the sweep tx to have been published.
|
|
|
|
ctx.ReceiveTx()
|
|
|
|
}
|
|
|
|
|
2022-05-11 18:10:01 +00:00
|
|
|
// Since we don't have a reliable mechanism to non-intrusively avoid
|
|
|
|
// races by setting the fee estimate too soon, let's sleep here a bit
|
|
|
|
// to ensure the first sweep fails.
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
2021-04-22 07:28:32 +00:00
|
|
|
// Now we decrease our fees for the swap's confirmation target to less
|
|
|
|
// than the maximum miner fee.
|
|
|
|
ctx.Lnd.SetFeeEstimate(testReq.SweepConfTarget, chainfee.SatPerKWeight(
|
|
|
|
testReq.MaxMinerFee/2,
|
|
|
|
))
|
2020-06-02 07:31:39 +00:00
|
|
|
|
2021-04-22 07:28:32 +00:00
|
|
|
// Now when we report a new block and tick our expiry fee timer, and
|
|
|
|
// fees are acceptably low so we expect our sweep to be published.
|
|
|
|
blockEpochChan <- ctx.Lnd.Height + 2
|
2024-01-18 11:26:14 +00:00
|
|
|
|
|
|
|
err = ctx.Lnd.NotifyHeight(ctx.Lnd.Height + 2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-06-02 07:31:39 +00:00
|
|
|
expiryChan <- testTime
|
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
if IsTaprootSwap(&swap.SwapContract) {
|
|
|
|
preimage := <-server.preimagePush
|
|
|
|
require.Equal(t, swap.Preimage, preimage)
|
|
|
|
}
|
|
|
|
|
2020-06-02 07:31:39 +00:00
|
|
|
// Expect a signing request for the HTLC success transaction.
|
|
|
|
<-ctx.Lnd.SignOutputRawChannel
|
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
if !IsTaprootSwap(&swap.SwapContract) {
|
|
|
|
// This is the first time we have swept, so we expect our
|
|
|
|
// preimage revealed state to be set.
|
2024-01-18 11:26:07 +00:00
|
|
|
cfg.store.(*loopdb.StoreMock).AssertLoopOutState(
|
2022-05-05 09:59:31 +00:00
|
|
|
loopdb.StatePreimageRevealed,
|
|
|
|
)
|
|
|
|
status := <-statusChan
|
|
|
|
require.Equal(
|
|
|
|
t, status.State, loopdb.StatePreimageRevealed,
|
|
|
|
)
|
|
|
|
}
|
2020-06-02 07:31:39 +00:00
|
|
|
|
|
|
|
// We expect the sweep tx to have been published.
|
|
|
|
ctx.ReceiveTx()
|
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
if !IsTaprootSwap(&swap.SwapContract) {
|
|
|
|
// Once we have published an on chain sweep, we expect a
|
|
|
|
// preimage to have been pushed to the server after the sweep.
|
|
|
|
preimage := <-server.preimagePush
|
|
|
|
require.Equal(t, swap.Preimage, preimage)
|
|
|
|
}
|
2020-06-02 07:31:39 +00:00
|
|
|
|
|
|
|
// To mock a server failure, we do not send a payment settled update
|
|
|
|
// for our off chain payment yet. We also do not confirm our sweep on
|
|
|
|
// chain yet so we can test our preimage push retry logic. Instead, we
|
|
|
|
// tick the expiry chan again to prompt another sweep.
|
|
|
|
expiryChan <- testTime
|
2024-01-18 11:26:14 +00:00
|
|
|
|
|
|
|
err = ctx.Lnd.NotifyHeight(ctx.Lnd.Height + 2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-05 09:59:31 +00:00
|
|
|
if IsTaprootSwap(&swap.SwapContract) {
|
|
|
|
preimage := <-server.preimagePush
|
|
|
|
require.Equal(t, swap.Preimage, preimage)
|
|
|
|
}
|
2020-06-02 07:31:39 +00:00
|
|
|
|
|
|
|
// We expect another signing request for out sweep, and publish of our
|
|
|
|
// sweep transaction.
|
|
|
|
<-ctx.Lnd.SignOutputRawChannel
|
|
|
|
ctx.ReceiveTx()
|
|
|
|
|
|
|
|
// Since we have not yet been notified of an off chain settle, and we
|
|
|
|
// have attempted to sweep again, we expect another preimage push
|
|
|
|
// attempt.
|
2022-05-05 09:59:31 +00:00
|
|
|
|
|
|
|
if !IsTaprootSwap(&swap.SwapContract) {
|
|
|
|
preimage := <-server.preimagePush
|
|
|
|
require.Equal(t, swap.Preimage, preimage)
|
|
|
|
}
|
2020-06-02 07:31:39 +00:00
|
|
|
|
|
|
|
// This time, we send a payment succeeded update into our payment stream
|
|
|
|
// to reflect that the server received our preimage push and settled off
|
|
|
|
// chain.
|
|
|
|
trackPayment.Updates <- lndclient.PaymentStatus{
|
|
|
|
State: lnrpc.Payment_SUCCEEDED,
|
|
|
|
}
|
|
|
|
|
|
|
|
// We tick one last time, this time expecting a sweep but no preimage
|
|
|
|
// push. The test's mocked preimage channel is un-buffered, so our test
|
|
|
|
// would hang if we pushed the preimage here.
|
|
|
|
expiryChan <- testTime
|
2024-01-18 11:26:14 +00:00
|
|
|
|
|
|
|
err = ctx.Lnd.NotifyHeight(ctx.Lnd.Height + 2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-06-02 07:31:39 +00:00
|
|
|
<-ctx.Lnd.SignOutputRawChannel
|
|
|
|
sweepTx := ctx.ReceiveTx()
|
|
|
|
|
|
|
|
// Finally, we put this swap out of its misery and notify a successful
|
2023-09-28 11:16:01 +00:00
|
|
|
// spend our sweepTx and assert that the swap succeeds.
|
2020-06-02 07:31:39 +00:00
|
|
|
ctx.NotifySpend(sweepTx, 0)
|
|
|
|
|
2024-01-18 11:26:14 +00:00
|
|
|
// After receiving the spend ntfn the batch will start monitoring for
|
|
|
|
// confs.
|
|
|
|
ctx.AssertRegisterConf(true, 3)
|
|
|
|
|
2024-01-18 11:26:07 +00:00
|
|
|
cfg.store.(*loopdb.StoreMock).AssertLoopOutState(loopdb.StateSuccess)
|
2022-05-05 09:59:31 +00:00
|
|
|
status := <-statusChan
|
2020-06-02 07:31:39 +00:00
|
|
|
require.Equal(
|
2020-06-23 10:35:22 +00:00
|
|
|
t, status.State, loopdb.StateSuccess,
|
2020-06-02 07:31:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
require.NoError(t, <-errChan)
|
|
|
|
}
|
2021-04-22 07:28:35 +00:00
|
|
|
|
2021-05-24 06:46:03 +00:00
|
|
|
// TestFailedOffChainCancelation tests sending of a cancelation message to
|
|
|
|
// the server when a swap fails due to off-chain routing.
|
|
|
|
func TestFailedOffChainCancelation(t *testing.T) {
|
2022-04-24 20:59:59 +00:00
|
|
|
t.Run("stable protocol", func(t *testing.T) {
|
|
|
|
testFailedOffChainCancelation(t)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("experimental protocol", func(t *testing.T) {
|
|
|
|
loopdb.EnableExperimentalProtocol()
|
|
|
|
defer loopdb.ResetCurrentProtocolVersion()
|
|
|
|
|
|
|
|
testFailedOffChainCancelation(t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFailedOffChainCancelation(t *testing.T) {
|
2021-05-24 06:46:03 +00:00
|
|
|
defer test.Guard(t)()
|
|
|
|
|
|
|
|
lnd := test.NewMockLnd()
|
|
|
|
ctx := test.NewContext(t, lnd)
|
|
|
|
server := newServerMock(lnd)
|
|
|
|
|
|
|
|
testReq := *testRequest
|
|
|
|
testReq.Expiry = lnd.Height + 20
|
|
|
|
|
|
|
|
cfg := newSwapConfig(
|
2024-01-18 11:26:07 +00:00
|
|
|
&lnd.LndServices, loopdb.NewStoreMock(t), server,
|
2021-05-24 06:46:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
initResult, err := newLoopOutSwap(
|
|
|
|
context.Background(), cfg, lnd.Height, &testReq,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
swap := initResult.swap
|
|
|
|
|
|
|
|
// Set up the required dependencies to execute the swap.
|
|
|
|
sweeper := &sweep.Sweeper{Lnd: &lnd.LndServices}
|
|
|
|
blockEpochChan := make(chan interface{})
|
|
|
|
statusChan := make(chan SwapInfo)
|
|
|
|
expiryChan := make(chan time.Time)
|
|
|
|
timerFactory := func(_ time.Duration) <-chan time.Time {
|
|
|
|
return expiryChan
|
|
|
|
}
|
|
|
|
|
|
|
|
errChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
cfg := &executeConfig{
|
2022-06-24 11:57:42 +00:00
|
|
|
statusChan: statusChan,
|
|
|
|
sweeper: sweeper,
|
|
|
|
blockEpochChan: blockEpochChan,
|
|
|
|
timerFactory: timerFactory,
|
|
|
|
cancelSwap: server.CancelLoopOutSwap,
|
|
|
|
verifySchnorrSig: mockVerifySchnorrSigFail,
|
2021-05-24 06:46:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := swap.execute(context.Background(), cfg, ctx.Lnd.Height)
|
|
|
|
errChan <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
// The swap should be found in its initial state.
|
2024-01-18 11:26:07 +00:00
|
|
|
cfg.store.(*loopdb.StoreMock).AssertLoopOutStored()
|
2021-05-24 06:46:03 +00:00
|
|
|
state := <-statusChan
|
|
|
|
require.Equal(t, loopdb.StateInitiated, state.State)
|
|
|
|
|
|
|
|
// Assert that we register for htlc confirmation notifications.
|
|
|
|
ctx.AssertRegisterConf(false, defaultConfirmations)
|
|
|
|
|
|
|
|
// We expect prepayment and invoice to be dispatched, order is unknown.
|
|
|
|
pmt1 := <-ctx.Lnd.RouterSendPaymentChannel
|
|
|
|
pmt2 := <-ctx.Lnd.RouterSendPaymentChannel
|
|
|
|
|
|
|
|
failUpdate := lndclient.PaymentStatus{
|
|
|
|
State: lnrpc.Payment_FAILED,
|
|
|
|
FailureReason: lnrpc.PaymentFailureReason_FAILURE_REASON_ERROR,
|
|
|
|
Htlcs: []*lndclient.HtlcAttempt{
|
|
|
|
{
|
|
|
|
// Include a non-failed htlc to test that we
|
|
|
|
// only report failed htlcs.
|
|
|
|
Status: lnrpc.HTLCAttempt_IN_FLIGHT,
|
|
|
|
},
|
|
|
|
// Add one htlc that failed within the server's
|
|
|
|
// infrastructure.
|
|
|
|
{
|
|
|
|
Status: lnrpc.HTLCAttempt_FAILED,
|
|
|
|
Route: &lnrpc.Route{
|
|
|
|
Hops: []*lnrpc.Hop{
|
|
|
|
{}, {}, {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Failure: &lndclient.HtlcFailure{
|
|
|
|
FailureSourceIndex: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Add one htlc that failed in the network at wide.
|
|
|
|
{
|
|
|
|
Status: lnrpc.HTLCAttempt_FAILED,
|
|
|
|
Route: &lnrpc.Route{
|
|
|
|
Hops: []*lnrpc.Hop{
|
|
|
|
{}, {}, {}, {}, {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Failure: &lndclient.HtlcFailure{
|
|
|
|
FailureSourceIndex: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
successUpdate := lndclient.PaymentStatus{
|
|
|
|
State: lnrpc.Payment_SUCCEEDED,
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to fail our swap payment and succeed the prepush, so we send
|
|
|
|
// a failure update to the payment that has the larger amount.
|
|
|
|
if pmt1.Amount > pmt2.Amount {
|
|
|
|
pmt1.TrackPaymentMessage.Updates <- failUpdate
|
|
|
|
pmt2.TrackPaymentMessage.Updates <- successUpdate
|
|
|
|
} else {
|
|
|
|
pmt1.TrackPaymentMessage.Updates <- successUpdate
|
|
|
|
pmt2.TrackPaymentMessage.Updates <- failUpdate
|
|
|
|
}
|
|
|
|
|
|
|
|
invoice, err := zpay32.Decode(
|
|
|
|
swap.LoopOutContract.SwapInvoice, lnd.ChainParams,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, invoice.PaymentAddr)
|
|
|
|
|
|
|
|
swapCancelation := &outCancelDetails{
|
|
|
|
hash: swap.hash,
|
|
|
|
paymentAddr: *invoice.PaymentAddr,
|
|
|
|
metadata: routeCancelMetadata{
|
|
|
|
paymentType: paymentTypeInvoice,
|
|
|
|
failureReason: failUpdate.FailureReason,
|
|
|
|
attempts: []uint32{
|
|
|
|
2,
|
|
|
|
math.MaxUint32,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
server.assertSwapCanceled(t, swapCancelation)
|
|
|
|
|
|
|
|
// Finally, the swap should be recorded with failed off chain timeout.
|
2024-01-18 11:26:07 +00:00
|
|
|
cfg.store.(*loopdb.StoreMock).AssertLoopOutState(
|
2021-05-24 06:46:03 +00:00
|
|
|
loopdb.StateFailOffchainPayments,
|
|
|
|
)
|
|
|
|
state = <-statusChan
|
|
|
|
require.Equal(t, state.State, loopdb.StateFailOffchainPayments)
|
|
|
|
require.NoError(t, <-errChan)
|
|
|
|
}
|
2022-06-24 11:57:42 +00:00
|
|
|
|
|
|
|
// TestLoopOutMuSig2Sweep tests the loop out sweep flow when the MuSig2 signing
|
|
|
|
// process is successful.
|
|
|
|
func TestLoopOutMuSig2Sweep(t *testing.T) {
|
|
|
|
defer test.Guard(t)()
|
|
|
|
|
|
|
|
// TODO(bhandras): remove when MuSig2 is default.
|
|
|
|
loopdb.EnableExperimentalProtocol()
|
|
|
|
defer loopdb.ResetCurrentProtocolVersion()
|
|
|
|
|
|
|
|
lnd := test.NewMockLnd()
|
|
|
|
ctx := test.NewContext(t, lnd)
|
|
|
|
server := newServerMock(lnd)
|
|
|
|
|
|
|
|
testReq := *testRequest
|
|
|
|
testReq.SweepConfTarget = 10
|
|
|
|
testReq.Expiry = ctx.Lnd.Height + testLoopOutMinOnChainCltvDelta
|
|
|
|
|
|
|
|
// We set our mock fee estimate for our target sweep confs to be our
|
|
|
|
// max miner fee * 2. With MuSig2 we still expect that the client will
|
|
|
|
// publish the sweep but with the fee clamped to the maximum allowed
|
|
|
|
// miner fee as the preimage is revealed before the sweep txn is
|
|
|
|
// published.
|
|
|
|
ctx.Lnd.SetFeeEstimate(
|
|
|
|
testReq.SweepConfTarget, chainfee.SatPerKWeight(
|
|
|
|
testReq.MaxMinerFee*2,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
cfg := newSwapConfig(
|
2024-01-18 11:26:07 +00:00
|
|
|
&lnd.LndServices, loopdb.NewStoreMock(t), server,
|
2022-06-24 11:57:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
initResult, err := newLoopOutSwap(
|
|
|
|
context.Background(), cfg, ctx.Lnd.Height, &testReq,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
swap := initResult.swap
|
|
|
|
|
|
|
|
// Set up the required dependencies to execute the swap.
|
|
|
|
sweeper := &sweep.Sweeper{Lnd: &lnd.LndServices}
|
|
|
|
blockEpochChan := make(chan interface{})
|
|
|
|
statusChan := make(chan SwapInfo)
|
|
|
|
expiryChan := make(chan time.Time)
|
|
|
|
timerFactory := func(_ time.Duration) <-chan time.Time {
|
|
|
|
return expiryChan
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mock a successful signature verify to make sure we don't fail
|
|
|
|
// creating the MuSig2 sweep.
|
|
|
|
mockVerifySchnorrSigSuccess := func(pubKey *btcec.PublicKey, hash,
|
|
|
|
sig []byte) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-18 11:26:14 +00:00
|
|
|
errChan := make(chan error, 2)
|
|
|
|
|
|
|
|
batcherStore := sweepbatcher.NewStoreMock()
|
|
|
|
|
|
|
|
batcher := sweepbatcher.NewBatcher(
|
|
|
|
lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
|
|
|
|
mockMuSig2SignSweep, mockVerifySchnorrSigSuccess,
|
|
|
|
lnd.ChainParams, batcherStore, cfg.store,
|
|
|
|
)
|
|
|
|
|
|
|
|
tctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := batcher.Run(tctx)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-06-24 11:57:42 +00:00
|
|
|
go func() {
|
|
|
|
err := swap.execute(context.Background(), &executeConfig{
|
|
|
|
statusChan: statusChan,
|
|
|
|
blockEpochChan: blockEpochChan,
|
|
|
|
timerFactory: timerFactory,
|
|
|
|
sweeper: sweeper,
|
2024-01-18 11:26:14 +00:00
|
|
|
batcher: batcher,
|
2022-06-24 11:57:42 +00:00
|
|
|
cancelSwap: server.CancelLoopOutSwap,
|
|
|
|
verifySchnorrSig: mockVerifySchnorrSigSuccess,
|
|
|
|
}, ctx.Lnd.Height)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
errChan <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
// The swap should be found in its initial state.
|
2024-01-18 11:26:07 +00:00
|
|
|
cfg.store.(*loopdb.StoreMock).AssertLoopOutStored()
|
2022-06-24 11:57:42 +00:00
|
|
|
state := <-statusChan
|
|
|
|
require.Equal(t, loopdb.StateInitiated, state.State)
|
|
|
|
|
|
|
|
// We'll then pay both the swap and prepay invoice, which should trigger
|
|
|
|
// the server to publish the on-chain HTLC.
|
|
|
|
signalSwapPaymentResult := ctx.AssertPaid(swapInvoiceDesc)
|
|
|
|
signalPrepaymentResult := ctx.AssertPaid(prepayInvoiceDesc)
|
|
|
|
|
|
|
|
signalSwapPaymentResult(nil)
|
|
|
|
signalPrepaymentResult(nil)
|
|
|
|
|
|
|
|
// Notify the confirmation notification for the HTLC.
|
|
|
|
ctx.AssertRegisterConf(false, defaultConfirmations)
|
|
|
|
|
|
|
|
blockEpochChan <- ctx.Lnd.Height + 1
|
|
|
|
|
|
|
|
htlcTx := wire.NewMsgTx(2)
|
|
|
|
htlcTx.AddTxOut(&wire.TxOut{
|
|
|
|
Value: int64(swap.AmountRequested),
|
|
|
|
PkScript: swap.htlc.PkScript,
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.NotifyConf(htlcTx)
|
|
|
|
|
|
|
|
// Assert that we made a query to track our payment, as required for
|
|
|
|
// preimage push tracking.
|
|
|
|
trackPayment := ctx.AssertTrackPayment()
|
|
|
|
|
|
|
|
// Tick the expiry channel, we are still using our client confirmation
|
|
|
|
// target at this stage which has fees higher than our max acceptable
|
|
|
|
// fee. We do not expect a sweep attempt at this point. Since our
|
|
|
|
// preimage is not revealed, we also do not expect a preimage push.
|
|
|
|
expiryChan <- testTime
|
|
|
|
|
2024-01-18 11:26:14 +00:00
|
|
|
// The client should then register for a spend of the HTLC and attempt
|
|
|
|
// to sweep it using the custom confirmation target.
|
|
|
|
ctx.AssertRegisterSpendNtfn(swap.htlc.PkScript)
|
|
|
|
|
|
|
|
ctx.AssertEpochListeners(1)
|
|
|
|
|
|
|
|
err = ctx.Lnd.NotifyHeight(ctx.Lnd.Height + 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-24 11:57:42 +00:00
|
|
|
// When using taproot htlcs the flow is different as we do reveal the
|
|
|
|
// preimage before sweeping in order for the server to trust us with
|
|
|
|
// our MuSig2 signing attempts.
|
2024-01-18 11:26:07 +00:00
|
|
|
cfg.store.(*loopdb.StoreMock).AssertLoopOutState(
|
2022-06-24 11:57:42 +00:00
|
|
|
loopdb.StatePreimageRevealed,
|
|
|
|
)
|
|
|
|
status := <-statusChan
|
|
|
|
require.Equal(
|
|
|
|
t, status.State, loopdb.StatePreimageRevealed,
|
|
|
|
)
|
|
|
|
|
|
|
|
preimage := <-server.preimagePush
|
|
|
|
require.Equal(t, swap.Preimage, preimage)
|
|
|
|
|
|
|
|
// We expect the sweep tx to have been published.
|
|
|
|
ctx.ReceiveTx()
|
|
|
|
|
|
|
|
// Since we don't have a reliable mechanism to non-intrusively avoid
|
|
|
|
// races by setting the fee estimate too soon, let's sleep here a bit
|
|
|
|
// to ensure the first sweep fails.
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
|
|
|
// Now we decrease our fees for the swap's confirmation target to less
|
|
|
|
// than the maximum miner fee.
|
|
|
|
ctx.Lnd.SetFeeEstimate(testReq.SweepConfTarget, chainfee.SatPerKWeight(
|
|
|
|
testReq.MaxMinerFee/2,
|
|
|
|
))
|
|
|
|
|
|
|
|
// Now when we report a new block and tick our expiry fee timer, and
|
|
|
|
// fees are acceptably low so we expect our sweep to be published.
|
|
|
|
blockEpochChan <- ctx.Lnd.Height + 2
|
2024-01-18 11:26:14 +00:00
|
|
|
|
|
|
|
err = ctx.Lnd.NotifyHeight(ctx.Lnd.Height + 2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-06-24 11:57:42 +00:00
|
|
|
expiryChan <- testTime
|
|
|
|
|
|
|
|
preimage = <-server.preimagePush
|
|
|
|
require.Equal(t, swap.Preimage, preimage)
|
|
|
|
|
|
|
|
// We expect the sweep tx to have been published.
|
|
|
|
sweepTx := ctx.ReceiveTx()
|
|
|
|
|
|
|
|
// This time, we send a payment succeeded update into our payment stream
|
|
|
|
// to reflect that the server received our preimage push and settled off
|
|
|
|
// chain.
|
|
|
|
trackPayment.Updates <- lndclient.PaymentStatus{
|
|
|
|
State: lnrpc.Payment_SUCCEEDED,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure our sweep tx has a single witness indicating keyspend.
|
|
|
|
require.Len(t, sweepTx.TxIn[0].Witness, 1)
|
|
|
|
|
|
|
|
// Finally, we put this swap out of its misery and notify a successful
|
2023-09-28 11:16:01 +00:00
|
|
|
// spend our sweepTx and assert that the swap succeeds.
|
2022-06-24 11:57:42 +00:00
|
|
|
ctx.NotifySpend(sweepTx, 0)
|
|
|
|
|
2024-01-18 11:26:14 +00:00
|
|
|
// After receiving the spend ntfn the batch will start monitoring for
|
|
|
|
// confs.
|
|
|
|
ctx.AssertRegisterConf(true, 3)
|
|
|
|
|
2024-01-18 11:26:07 +00:00
|
|
|
cfg.store.(*loopdb.StoreMock).AssertLoopOutState(loopdb.StateSuccess)
|
2022-06-24 11:57:42 +00:00
|
|
|
status = <-statusChan
|
|
|
|
require.Equal(t, status.State, loopdb.StateSuccess)
|
|
|
|
require.NoError(t, <-errChan)
|
|
|
|
}
|