|
|
|
@ -4,6 +4,7 @@ import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"sync"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
@ -12,11 +13,12 @@ import (
|
|
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
|
"github.com/btcsuite/btclog"
|
|
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
|
|
|
|
"github.com/lightninglabs/loop/test"
|
|
|
|
|
"github.com/lightninglabs/loop/utils"
|
|
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
|
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
|
|
|
|
"github.com/lightningnetwork/lnd/input"
|
|
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
@ -45,15 +47,22 @@ var destAddr = func() btcutil.Address {
|
|
|
|
|
return addr
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
var senderKey, receiverKey [33]byte
|
|
|
|
|
var htlcKeys = func() loopdb.HtlcKeys {
|
|
|
|
|
var senderKey, receiverKey [33]byte
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
// Generate keys.
|
|
|
|
|
_, senderPubKey := test.CreateKey(1)
|
|
|
|
|
copy(senderKey[:], senderPubKey.SerializeCompressed())
|
|
|
|
|
_, receiverPubKey := test.CreateKey(2)
|
|
|
|
|
copy(receiverKey[:], receiverPubKey.SerializeCompressed())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return loopdb.HtlcKeys{
|
|
|
|
|
SenderScriptKey: senderKey,
|
|
|
|
|
ReceiverScriptKey: receiverKey,
|
|
|
|
|
SenderInternalPubKey: senderKey,
|
|
|
|
|
ReceiverInternalPubKey: receiverKey,
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
func testVerifySchnorrSig(pubKey *btcec.PublicKey, hash, sig []byte) error {
|
|
|
|
|
return nil
|
|
|
|
@ -68,6 +77,18 @@ func testMuSig2SignSweep(ctx context.Context,
|
|
|
|
|
return nil, nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var customSignature = func() []byte {
|
|
|
|
|
sig := [64]byte{10, 20, 30}
|
|
|
|
|
return sig[:]
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
func testSignMuSig2func(ctx context.Context, muSig2Version input.MuSig2Version,
|
|
|
|
|
swapHash lntypes.Hash, rootHash chainhash.Hash,
|
|
|
|
|
sigHash [32]byte) ([]byte, error) {
|
|
|
|
|
|
|
|
|
|
return customSignature, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dummyNotifier = SpendNotifier{
|
|
|
|
|
SpendChan: make(chan *SpendDetail, ntfnBufferSize),
|
|
|
|
|
SpendErrChan: make(chan error, ntfnBufferSize),
|
|
|
|
@ -112,8 +133,8 @@ func testSweepBatcherBatchCreation(t *testing.T, store testStore,
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
|
|
|
|
|
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore,
|
|
|
|
|
sweepStore)
|
|
|
|
|
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
|
|
|
|
|
batcherStore, sweepStore)
|
|
|
|
|
go func() {
|
|
|
|
|
err := batcher.Run(ctx)
|
|
|
|
|
checkBatcherError(t, err)
|
|
|
|
@ -134,10 +155,13 @@ func testSweepBatcherBatchCreation(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111,
|
|
|
|
|
AmountRequested: 111,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = store.CreateLoopOut(ctx, sweepReq1.SwapHash, swap1)
|
|
|
|
@ -160,6 +184,9 @@ func testSweepBatcherBatchCreation(t *testing.T, store testStore,
|
|
|
|
|
return len(batcher.batches) == 1
|
|
|
|
|
}, test.Timeout, eventuallyCheckFrequency)
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Create a second sweep request that has a timeout distance less than
|
|
|
|
|
// our configured threshold.
|
|
|
|
|
sweepReq2 := SweepRequest{
|
|
|
|
@ -176,12 +203,16 @@ func testSweepBatcherBatchCreation(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111 + defaultMaxTimeoutDistance - 1,
|
|
|
|
|
AmountRequested: 222,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
|
|
|
|
|
// Make preimage unique to pass SQL constraints.
|
|
|
|
|
Preimage: lntypes.Preimage{2},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = store.CreateLoopOut(ctx, sweepReq2.SwapHash, swap2)
|
|
|
|
@ -190,6 +221,13 @@ func testSweepBatcherBatchCreation(t *testing.T, store testStore,
|
|
|
|
|
|
|
|
|
|
require.NoError(t, batcher.AddSweep(&sweepReq2))
|
|
|
|
|
|
|
|
|
|
// Tick tock next block.
|
|
|
|
|
err = lnd.NotifyHeight(601)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Batcher should not create a second batch as timeout distance is small
|
|
|
|
|
// enough.
|
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
@ -212,12 +250,16 @@ func testSweepBatcherBatchCreation(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111 + defaultMaxTimeoutDistance + 1,
|
|
|
|
|
AmountRequested: 333,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
|
|
|
|
|
// Make preimage unique to pass SQL constraints.
|
|
|
|
|
Preimage: lntypes.Preimage{3},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = store.CreateLoopOut(ctx, sweepReq3.SwapHash, swap3)
|
|
|
|
@ -236,6 +278,9 @@ func testSweepBatcherBatchCreation(t *testing.T, store testStore,
|
|
|
|
|
// primary sweep's spend.
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
|
// Verify that each batch has the correct number of sweeps
|
|
|
|
|
// in it.
|
|
|
|
@ -298,11 +343,7 @@ func testFeeBumping(t *testing.T, store testStore,
|
|
|
|
|
Hash: chainhash.Hash{1, 1},
|
|
|
|
|
Index: 1,
|
|
|
|
|
},
|
|
|
|
|
Notifier: &SpendNotifier{
|
|
|
|
|
SpendChan: make(chan *SpendDetail, ntfnBufferSize),
|
|
|
|
|
SpendErrChan: make(chan error, ntfnBufferSize),
|
|
|
|
|
QuitChan: make(chan bool, ntfnBufferSize),
|
|
|
|
|
},
|
|
|
|
|
Notifier: &dummyNotifier,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
swap1 := &loopdb.LoopOutContract{
|
|
|
|
@ -310,16 +351,7 @@ func testFeeBumping(t *testing.T, store testStore,
|
|
|
|
|
CltvExpiry: 111,
|
|
|
|
|
AmountRequested: 1_000_000,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: loopdb.HtlcKeys{
|
|
|
|
|
SenderScriptKey: senderKey,
|
|
|
|
|
ReceiverScriptKey: receiverKey,
|
|
|
|
|
SenderInternalPubKey: senderKey,
|
|
|
|
|
ReceiverInternalPubKey: receiverKey,
|
|
|
|
|
ClientScriptKeyLocator: keychain.KeyLocator{
|
|
|
|
|
Family: 1,
|
|
|
|
|
Index: 2,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
@ -374,8 +406,8 @@ func testSweepBatcherSimpleLifecycle(t *testing.T, store testStore,
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
|
|
|
|
|
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore,
|
|
|
|
|
sweepStore)
|
|
|
|
|
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
|
|
|
|
|
batcherStore, sweepStore)
|
|
|
|
|
go func() {
|
|
|
|
|
err := batcher.Run(ctx)
|
|
|
|
|
checkBatcherError(t, err)
|
|
|
|
@ -396,7 +428,10 @@ func testSweepBatcherSimpleLifecycle(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111,
|
|
|
|
|
AmountRequested: 111,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
@ -434,6 +469,9 @@ func testSweepBatcherSimpleLifecycle(t *testing.T, store testStore,
|
|
|
|
|
// The primary sweep id should be that of the first inserted sweep.
|
|
|
|
|
require.Equal(t, batch.primarySweepID, sweepReq1.SwapHash)
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
err = lnd.NotifyHeight(601)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
@ -443,6 +481,9 @@ func testSweepBatcherSimpleLifecycle(t *testing.T, store testStore,
|
|
|
|
|
return batch.currentHeight == 601
|
|
|
|
|
}, test.Timeout, eventuallyCheckFrequency)
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Create the spending tx that will trigger the spend monitor of the
|
|
|
|
|
// batch.
|
|
|
|
|
spendingTx := &wire.MsgTx{
|
|
|
|
@ -514,8 +555,8 @@ func testSweepBatcherSweepReentry(t *testing.T, store testStore,
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
|
|
|
|
|
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore,
|
|
|
|
|
sweepStore)
|
|
|
|
|
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
|
|
|
|
|
batcherStore, sweepStore)
|
|
|
|
|
go func() {
|
|
|
|
|
err := batcher.Run(ctx)
|
|
|
|
|
checkBatcherError(t, err)
|
|
|
|
@ -537,6 +578,8 @@ func testSweepBatcherSweepReentry(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111,
|
|
|
|
|
AmountRequested: 111,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
},
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
@ -561,6 +604,8 @@ func testSweepBatcherSweepReentry(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111,
|
|
|
|
|
AmountRequested: 222,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
|
|
|
|
|
// Make preimage unique to pass SQL constraints.
|
|
|
|
|
Preimage: lntypes.Preimage{2},
|
|
|
|
@ -588,6 +633,8 @@ func testSweepBatcherSweepReentry(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111,
|
|
|
|
|
AmountRequested: 333,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
|
|
|
|
|
// Make preimage unique to pass SQL constraints.
|
|
|
|
|
Preimage: lntypes.Preimage{3},
|
|
|
|
@ -608,10 +655,29 @@ func testSweepBatcherSweepReentry(t *testing.T, store testStore,
|
|
|
|
|
// registered.
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Add the second sweep.
|
|
|
|
|
require.NoError(t, batcher.AddSweep(&sweepReq2))
|
|
|
|
|
|
|
|
|
|
// Add next block to trigger batch publishing.
|
|
|
|
|
err = lnd.NotifyHeight(601)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Add the third sweep.
|
|
|
|
|
require.NoError(t, batcher.AddSweep(&sweepReq3))
|
|
|
|
|
|
|
|
|
|
// Add next block to trigger batch publishing.
|
|
|
|
|
err = lnd.NotifyHeight(602)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Batcher should create a batch for the sweeps.
|
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
|
return len(batcher.batches) == 1
|
|
|
|
@ -663,7 +729,7 @@ func testSweepBatcherSweepReentry(t *testing.T, store testStore,
|
|
|
|
|
SpendingTx: spendingTx,
|
|
|
|
|
SpenderTxHash: &spendingTxHash,
|
|
|
|
|
SpenderInputIndex: 0,
|
|
|
|
|
SpendingHeight: 601,
|
|
|
|
|
SpendingHeight: 603,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send the spending notification to the mock channel.
|
|
|
|
@ -695,6 +761,15 @@ func testSweepBatcherSweepReentry(t *testing.T, store testStore,
|
|
|
|
|
Tx: spendingTx,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
// Here is a race condition, which is unlikely to cause a crash: if we
|
|
|
|
|
// wait for publish tx before sending a conf notification (previous
|
|
|
|
|
// action), then conf notification can go to the second batch (since
|
|
|
|
|
// the mock does not have a way to direct a notification to proper
|
|
|
|
|
// subscriber) and the first batch does not exit, waiting for the
|
|
|
|
|
// confirmation forever.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Eventually the batch receives the confirmation notification,
|
|
|
|
|
// gracefully exits and the batcher deletes it.
|
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
@ -732,8 +807,8 @@ func testSweepBatcherNonWalletAddr(t *testing.T, store testStore,
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
|
|
|
|
|
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore,
|
|
|
|
|
sweepStore)
|
|
|
|
|
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
|
|
|
|
|
batcherStore, sweepStore)
|
|
|
|
|
go func() {
|
|
|
|
|
err := batcher.Run(ctx)
|
|
|
|
|
checkBatcherError(t, err)
|
|
|
|
@ -754,10 +829,13 @@ func testSweepBatcherNonWalletAddr(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111,
|
|
|
|
|
AmountRequested: 111,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
},
|
|
|
|
|
IsExternalAddr: true,
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = store.CreateLoopOut(ctx, sweepReq1.SwapHash, swap1)
|
|
|
|
@ -777,6 +855,9 @@ func testSweepBatcherNonWalletAddr(t *testing.T, store testStore,
|
|
|
|
|
// sweep's spend.
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Insert the same swap twice, this should be a noop.
|
|
|
|
|
require.NoError(t, batcher.AddSweep(&sweepReq1))
|
|
|
|
|
|
|
|
|
@ -796,13 +877,16 @@ func testSweepBatcherNonWalletAddr(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111 + defaultMaxTimeoutDistance - 1,
|
|
|
|
|
AmountRequested: 222,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
|
|
|
|
|
// Make preimage unique to pass SQL constraints.
|
|
|
|
|
Preimage: lntypes.Preimage{2},
|
|
|
|
|
},
|
|
|
|
|
IsExternalAddr: true,
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
IsExternalAddr: true,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = store.CreateLoopOut(ctx, sweepReq2.SwapHash, swap2)
|
|
|
|
@ -821,6 +905,9 @@ func testSweepBatcherNonWalletAddr(t *testing.T, store testStore,
|
|
|
|
|
// sweep's spend.
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for second batch to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Create a third sweep request that has more timeout distance than
|
|
|
|
|
// the default.
|
|
|
|
|
sweepReq3 := SweepRequest{
|
|
|
|
@ -836,14 +923,17 @@ func testSweepBatcherNonWalletAddr(t *testing.T, store testStore,
|
|
|
|
|
swap3 := &loopdb.LoopOutContract{
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111 + defaultMaxTimeoutDistance + 1,
|
|
|
|
|
AmountRequested: 333,
|
|
|
|
|
AmountRequested: 222,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
|
|
|
|
|
// Make preimage unique to pass SQL constraints.
|
|
|
|
|
Preimage: lntypes.Preimage{3},
|
|
|
|
|
},
|
|
|
|
|
IsExternalAddr: true,
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
IsExternalAddr: true,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = store.CreateLoopOut(ctx, sweepReq3.SwapHash, swap3)
|
|
|
|
@ -862,6 +952,9 @@ func testSweepBatcherNonWalletAddr(t *testing.T, store testStore,
|
|
|
|
|
// sweep's spend.
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published for 3rd batch.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
|
// Verify that each batch has the correct number of sweeps
|
|
|
|
|
// in it.
|
|
|
|
@ -908,8 +1001,8 @@ func testSweepBatcherComposite(t *testing.T, store testStore,
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
|
|
|
|
|
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore,
|
|
|
|
|
sweepStore)
|
|
|
|
|
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
|
|
|
|
|
batcherStore, sweepStore)
|
|
|
|
|
go func() {
|
|
|
|
|
err := batcher.Run(ctx)
|
|
|
|
|
checkBatcherError(t, err)
|
|
|
|
@ -930,10 +1023,12 @@ func testSweepBatcherComposite(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111,
|
|
|
|
|
AmountRequested: 111,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = store.CreateLoopOut(ctx, sweepReq1.SwapHash, swap1)
|
|
|
|
@ -956,12 +1051,15 @@ func testSweepBatcherComposite(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111 + defaultMaxTimeoutDistance - 1,
|
|
|
|
|
AmountRequested: 222,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
|
|
|
|
|
// Make preimage unique to pass SQL constraints.
|
|
|
|
|
Preimage: lntypes.Preimage{2},
|
|
|
|
|
},
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = store.CreateLoopOut(ctx, sweepReq2.SwapHash, swap2)
|
|
|
|
@ -984,12 +1082,15 @@ func testSweepBatcherComposite(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111 + defaultMaxTimeoutDistance - 3,
|
|
|
|
|
AmountRequested: 333,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
|
|
|
|
|
// Make preimage unique to pass SQL constraints.
|
|
|
|
|
Preimage: lntypes.Preimage{3},
|
|
|
|
|
},
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
IsExternalAddr: true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -1013,12 +1114,15 @@ func testSweepBatcherComposite(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111 + defaultMaxTimeoutDistance + 1,
|
|
|
|
|
AmountRequested: 444,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
|
|
|
|
|
// Make preimage unique to pass SQL constraints.
|
|
|
|
|
Preimage: lntypes.Preimage{4},
|
|
|
|
|
},
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = store.CreateLoopOut(ctx, sweepReq4.SwapHash, swap4)
|
|
|
|
@ -1041,12 +1145,15 @@ func testSweepBatcherComposite(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111 + defaultMaxTimeoutDistance + 5,
|
|
|
|
|
AmountRequested: 555,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
|
|
|
|
|
// Make preimage unique to pass SQL constraints.
|
|
|
|
|
Preimage: lntypes.Preimage{5},
|
|
|
|
|
},
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = store.CreateLoopOut(ctx, sweepReq5.SwapHash, swap5)
|
|
|
|
@ -1069,12 +1176,15 @@ func testSweepBatcherComposite(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111 + defaultMaxTimeoutDistance + 6,
|
|
|
|
|
AmountRequested: 666,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
|
|
|
|
|
// Make preimage unique to pass SQL constraints.
|
|
|
|
|
Preimage: lntypes.Preimage{6},
|
|
|
|
|
},
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
IsExternalAddr: true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -1095,6 +1205,9 @@ func testSweepBatcherComposite(t *testing.T, store testStore,
|
|
|
|
|
// sweep's spend.
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Insert the same swap twice, this should be a noop.
|
|
|
|
|
require.NoError(t, batcher.AddSweep(&sweepReq1))
|
|
|
|
|
|
|
|
|
@ -1106,6 +1219,14 @@ func testSweepBatcherComposite(t *testing.T, store testStore,
|
|
|
|
|
return len(batcher.batches) == 1
|
|
|
|
|
}, test.Timeout, eventuallyCheckFrequency)
|
|
|
|
|
|
|
|
|
|
// Publish a block to trigger batch 1 republishing.
|
|
|
|
|
err = lnd.NotifyHeight(601)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Wait for tx for the first batch to be published (2 sweeps).
|
|
|
|
|
tx := <-lnd.TxPublishChannel
|
|
|
|
|
require.Equal(t, 2, len(tx.TxIn))
|
|
|
|
|
|
|
|
|
|
require.NoError(t, batcher.AddSweep(&sweepReq3))
|
|
|
|
|
|
|
|
|
|
// Batcher should create a second batch as this sweep pays to a non
|
|
|
|
@ -1118,6 +1239,10 @@ func testSweepBatcherComposite(t *testing.T, store testStore,
|
|
|
|
|
// sweep's spend.
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for tx for the second batch to be published (1 sweep).
|
|
|
|
|
tx = <-lnd.TxPublishChannel
|
|
|
|
|
require.Equal(t, 1, len(tx.TxIn))
|
|
|
|
|
|
|
|
|
|
require.NoError(t, batcher.AddSweep(&sweepReq4))
|
|
|
|
|
|
|
|
|
|
// Batcher should create a third batch as timeout distance is greater
|
|
|
|
@ -1130,8 +1255,21 @@ func testSweepBatcherComposite(t *testing.T, store testStore,
|
|
|
|
|
// sweep's spend.
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for tx for the third batch to be published (1 sweep).
|
|
|
|
|
tx = <-lnd.TxPublishChannel
|
|
|
|
|
require.Equal(t, 1, len(tx.TxIn))
|
|
|
|
|
|
|
|
|
|
require.NoError(t, batcher.AddSweep(&sweepReq5))
|
|
|
|
|
|
|
|
|
|
// Publish a block to trigger batch 3 republishing.
|
|
|
|
|
err = lnd.NotifyHeight(601)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Wait for 3 txs for the 3 batches.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Batcher should not create a fourth batch as timeout distance is small
|
|
|
|
|
// enough for it to join the last batch.
|
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
@ -1150,6 +1288,10 @@ func testSweepBatcherComposite(t *testing.T, store testStore,
|
|
|
|
|
// sweep's spend.
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for tx for the 4th batch to be published (1 sweep).
|
|
|
|
|
tx = <-lnd.TxPublishChannel
|
|
|
|
|
require.Equal(t, 1, len(tx.TxIn))
|
|
|
|
|
|
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
|
// Verify that each batch has the correct number of sweeps in
|
|
|
|
|
// it.
|
|
|
|
@ -1253,8 +1395,8 @@ func testRestoringEmptyBatch(t *testing.T, store testStore,
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
|
|
|
|
|
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore,
|
|
|
|
|
sweepStore)
|
|
|
|
|
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
|
|
|
|
|
batcherStore, sweepStore)
|
|
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(1)
|
|
|
|
@ -1283,10 +1425,12 @@ func testRestoringEmptyBatch(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111,
|
|
|
|
|
AmountRequested: 111,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = store.CreateLoopOut(ctx, sweepReq.SwapHash, swap)
|
|
|
|
@ -1300,6 +1444,9 @@ func testRestoringEmptyBatch(t *testing.T, store testStore,
|
|
|
|
|
// sweep's spend.
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Once batcher receives sweep request it will eventually spin up a
|
|
|
|
|
// batch.
|
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
@ -1422,8 +1569,8 @@ func testHandleSweepTwice(t *testing.T, backend testStore,
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
|
|
|
|
|
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore,
|
|
|
|
|
sweepStore)
|
|
|
|
|
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
|
|
|
|
|
batcherStore, sweepStore)
|
|
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(1)
|
|
|
|
@ -1460,9 +1607,12 @@ func testHandleSweepTwice(t *testing.T, backend testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: shortCltv,
|
|
|
|
|
AmountRequested: 111,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
},
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -1484,9 +1634,12 @@ func testHandleSweepTwice(t *testing.T, backend testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: longCltv,
|
|
|
|
|
AmountRequested: 222,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
},
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -1500,11 +1653,17 @@ func testHandleSweepTwice(t *testing.T, backend testStore,
|
|
|
|
|
// primary sweep's spend.
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Deliver the second sweep. It will go to a separate batch,
|
|
|
|
|
// since CltvExpiry values are distant enough.
|
|
|
|
|
require.NoError(t, batcher.AddSweep(&sweepReq2))
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Once batcher receives sweep request it will eventually spin up
|
|
|
|
|
// batches.
|
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
@ -1525,9 +1684,12 @@ func testHandleSweepTwice(t *testing.T, backend testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: shortCltv,
|
|
|
|
|
AmountRequested: 222,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
},
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
store.putLoopOutSwap(sweepReq2.SwapHash, loopOut2)
|
|
|
|
@ -1569,6 +1731,14 @@ func testHandleSweepTwice(t *testing.T, backend testStore,
|
|
|
|
|
require.Equal(t, 1, len(batch.sweeps))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Publish a block to trigger batch 2 republishing.
|
|
|
|
|
err = lnd.NotifyHeight(601)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Wait for txs to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Now make the batcher quit by canceling the context.
|
|
|
|
|
cancel()
|
|
|
|
|
wg.Wait()
|
|
|
|
@ -1590,8 +1760,8 @@ func testRestoringPreservesConfTarget(t *testing.T, store testStore,
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
|
|
|
|
|
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore,
|
|
|
|
|
sweepStore)
|
|
|
|
|
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
|
|
|
|
|
batcherStore, sweepStore)
|
|
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(1)
|
|
|
|
@ -1620,6 +1790,8 @@ func testRestoringPreservesConfTarget(t *testing.T, store testStore,
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111,
|
|
|
|
|
AmountRequested: 111,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
@ -1638,6 +1810,9 @@ func testRestoringPreservesConfTarget(t *testing.T, store testStore,
|
|
|
|
|
// sweep's spend.
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Once batcher receives sweep request it will eventually spin up a
|
|
|
|
|
// batch.
|
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
@ -1677,8 +1852,8 @@ func testRestoringPreservesConfTarget(t *testing.T, store testStore,
|
|
|
|
|
|
|
|
|
|
// Now launch it again.
|
|
|
|
|
batcher = NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
|
|
|
|
|
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore,
|
|
|
|
|
sweepStore)
|
|
|
|
|
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
|
|
|
|
|
batcherStore, sweepStore)
|
|
|
|
|
ctx, cancel = context.WithCancel(context.Background())
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
@ -1714,6 +1889,9 @@ func testRestoringPreservesConfTarget(t *testing.T, store testStore,
|
|
|
|
|
// Expect registration for spend notification.
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
<-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Now make the batcher quit by canceling the context.
|
|
|
|
|
cancel()
|
|
|
|
|
wg.Wait()
|
|
|
|
@ -1761,12 +1939,7 @@ func testSweepFetcher(t *testing.T, store testStore,
|
|
|
|
|
CltvExpiry: 222,
|
|
|
|
|
AmountRequested: amt,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: loopdb.HtlcKeys{
|
|
|
|
|
SenderScriptKey: senderKey,
|
|
|
|
|
ReceiverScriptKey: receiverKey,
|
|
|
|
|
SenderInternalPubKey: senderKey,
|
|
|
|
|
ReceiverInternalPubKey: receiverKey,
|
|
|
|
|
},
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
},
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
@ -1784,12 +1957,7 @@ func testSweepFetcher(t *testing.T, store testStore,
|
|
|
|
|
SwapInvoicePaymentAddr: *swapPaymentAddr,
|
|
|
|
|
MinFeeRate: feeRate,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HTLCKeys: loopdb.HtlcKeys{
|
|
|
|
|
SenderScriptKey: senderKey,
|
|
|
|
|
ReceiverScriptKey: receiverKey,
|
|
|
|
|
SenderInternalPubKey: senderKey,
|
|
|
|
|
ReceiverInternalPubKey: receiverKey,
|
|
|
|
|
},
|
|
|
|
|
HTLCKeys: htlcKeys,
|
|
|
|
|
HTLC: *htlc,
|
|
|
|
|
HTLCSuccessEstimator: htlc.AddSuccessToEstimator,
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
@ -1901,8 +2069,8 @@ func testSweepBatcherCloseDuringAdding(t *testing.T, store testStore,
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
|
|
|
|
|
testMuSig2SignSweep, nil, lnd.ChainParams, batcherStore,
|
|
|
|
|
sweepStore)
|
|
|
|
|
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
|
|
|
|
|
batcherStore, sweepStore)
|
|
|
|
|
go func() {
|
|
|
|
|
err := batcher.Run(ctx)
|
|
|
|
|
checkBatcherError(t, err)
|
|
|
|
@ -1985,6 +2153,84 @@ func testSweepBatcherCloseDuringAdding(t *testing.T, store testStore,
|
|
|
|
|
<-registrationChan
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// testCustomSignMuSig2 tests the operation with custom musig2 signer.
|
|
|
|
|
func testCustomSignMuSig2(t *testing.T, store testStore,
|
|
|
|
|
batcherStore testBatcherStore) {
|
|
|
|
|
|
|
|
|
|
defer test.Guard(t)()
|
|
|
|
|
|
|
|
|
|
lnd := test.NewMockLnd()
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
|
|
sweepStore, err := NewSweepFetcherFromSwapStore(store, lnd.ChainParams)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Use custom MuSig2 signer function.
|
|
|
|
|
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
|
|
|
|
|
nil, testVerifySchnorrSig, lnd.ChainParams, batcherStore,
|
|
|
|
|
sweepStore, WithCustomSignMuSig2(testSignMuSig2func))
|
|
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
|
|
var runErr error
|
|
|
|
|
go func() {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
runErr = batcher.Run(ctx)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// Wait for the batcher to be initialized.
|
|
|
|
|
<-batcher.initDone
|
|
|
|
|
|
|
|
|
|
// Create a sweep request.
|
|
|
|
|
sweepReq := SweepRequest{
|
|
|
|
|
SwapHash: lntypes.Hash{1, 1, 1},
|
|
|
|
|
Value: 111,
|
|
|
|
|
Outpoint: wire.OutPoint{
|
|
|
|
|
Hash: chainhash.Hash{1, 1},
|
|
|
|
|
Index: 1,
|
|
|
|
|
},
|
|
|
|
|
Notifier: &dummyNotifier,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
swap := &loopdb.LoopOutContract{
|
|
|
|
|
SwapContract: loopdb.SwapContract{
|
|
|
|
|
CltvExpiry: 111,
|
|
|
|
|
AmountRequested: 111,
|
|
|
|
|
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
|
|
|
|
|
HtlcKeys: htlcKeys,
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
DestAddr: destAddr,
|
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
|
SweepConfTarget: 111,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = store.CreateLoopOut(ctx, sweepReq.SwapHash, swap)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
store.AssertLoopOutStored()
|
|
|
|
|
|
|
|
|
|
// Deliver sweep request to batcher.
|
|
|
|
|
require.NoError(t, batcher.AddSweep(&sweepReq))
|
|
|
|
|
|
|
|
|
|
// Since a batch was created we check that it registered for its primary
|
|
|
|
|
// sweep's spend.
|
|
|
|
|
<-lnd.RegisterSpendChannel
|
|
|
|
|
|
|
|
|
|
// Wait for tx to be published.
|
|
|
|
|
tx := <-lnd.TxPublishChannel
|
|
|
|
|
|
|
|
|
|
// Check the signature.
|
|
|
|
|
gotSig := tx.TxIn[0].Witness[0]
|
|
|
|
|
require.Equal(t, customSignature, gotSig, "signatures don't match")
|
|
|
|
|
|
|
|
|
|
// Now make the batcher quit by canceling the context.
|
|
|
|
|
cancel()
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
|
|
checkBatcherError(t, runErr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestSweepBatcherBatchCreation tests that sweep requests enter the expected
|
|
|
|
|
// batch based on their timeout distance.
|
|
|
|
|
func TestSweepBatcherBatchCreation(t *testing.T) {
|
|
|
|
@ -2070,6 +2316,11 @@ func TestSweepBatcherCloseDuringAdding(t *testing.T) {
|
|
|
|
|
runTests(t, testSweepBatcherCloseDuringAdding)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestCustomSignMuSig2 tests the operation with custom musig2 signer.
|
|
|
|
|
func TestCustomSignMuSig2(t *testing.T) {
|
|
|
|
|
runTests(t, testCustomSignMuSig2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// testBatcherStore is BatcherStore used in tests.
|
|
|
|
|
type testBatcherStore interface {
|
|
|
|
|
BatcherStore
|
|
|
|
@ -2155,6 +2406,10 @@ func (s *loopdbStore) AssertLoopOutStored() {
|
|
|
|
|
func runTests(t *testing.T, testFn func(t *testing.T, store testStore,
|
|
|
|
|
batcherStore testBatcherStore)) {
|
|
|
|
|
|
|
|
|
|
logger := btclog.NewBackend(os.Stdout).Logger("SWEEP")
|
|
|
|
|
logger.SetLevel(btclog.LevelTrace)
|
|
|
|
|
UseLogger(logger)
|
|
|
|
|
|
|
|
|
|
t.Run("mocks", func(t *testing.T) {
|
|
|
|
|
store := loopdb.NewStoreMock(t)
|
|
|
|
|
batcherStore := NewStoreMock()
|
|
|
|
|