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
|
|
|
"testing"
|
2019-03-06 20:13:50 +00:00
|
|
|
"time"
|
|
|
|
|
2022-03-14 12:36:02 +00:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2024-01-18 11:26:07 +00:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2020-05-29 09:27:47 +00:00
|
|
|
"github.com/lightninglabs/lndclient"
|
2022-05-04 20:26:24 +00:00
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
2019-03-06 23:29:44 +00:00
|
|
|
"github.com/lightninglabs/loop/test"
|
2023-01-09 15:36:52 +00:00
|
|
|
invpkg "github.com/lightningnetwork/lnd/invoices"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2020-02-11 12:58:55 +00:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/lightningnetwork/lnd/zpay32"
|
2021-05-24 06:46:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testTime = time.Date(2018, time.January, 9, 14, 00, 00, 0, time.UTC)
|
|
|
|
|
2020-07-14 12:54:06 +00:00
|
|
|
testLoopOutMinOnChainCltvDelta = int32(30)
|
|
|
|
testLoopOutMaxOnChainCltvDelta = int32(40)
|
|
|
|
testChargeOnChainCltvDelta = int32(100)
|
|
|
|
testSwapFee = btcutil.Amount(210)
|
|
|
|
testFixedPrepayAmount = btcutil.Amount(100)
|
|
|
|
testMinSwapAmount = btcutil.Amount(10000)
|
|
|
|
testMaxSwapAmount = btcutil.Amount(1000000)
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// serverMock is used in client unit tests to simulate swap server behaviour.
|
|
|
|
type serverMock struct {
|
|
|
|
expectedSwapAmt btcutil.Amount
|
|
|
|
swapInvoiceAmt btcutil.Amount
|
|
|
|
prepayInvoiceAmt btcutil.Amount
|
|
|
|
|
|
|
|
height int32
|
|
|
|
|
|
|
|
swapInvoice string
|
|
|
|
swapHash lntypes.Hash
|
2020-06-02 07:31:39 +00:00
|
|
|
|
|
|
|
// preimagePush is a channel that preimage pushes are sent into.
|
|
|
|
preimagePush chan lntypes.Preimage
|
2020-05-29 09:27:47 +00:00
|
|
|
|
2024-01-12 12:56:29 +00:00
|
|
|
// cancelSwap is a channel that swap cancellations are sent into.
|
2021-05-24 06:40:13 +00:00
|
|
|
cancelSwap chan *outCancelDetails
|
|
|
|
|
2020-05-29 09:27:47 +00:00
|
|
|
lnd *test.LndMockServices
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 20:28:20 +00:00
|
|
|
var _ swapServerClient = (*serverMock)(nil)
|
|
|
|
|
2020-05-29 09:27:47 +00:00
|
|
|
func newServerMock(lnd *test.LndMockServices) *serverMock {
|
2019-03-06 20:13:50 +00:00
|
|
|
return &serverMock{
|
|
|
|
expectedSwapAmt: 50000,
|
|
|
|
|
|
|
|
// Total swap fee: 1000 + 0.01 * 50000 = 1050
|
|
|
|
swapInvoiceAmt: 50950,
|
|
|
|
prepayInvoiceAmt: 100,
|
|
|
|
|
|
|
|
height: 600,
|
2020-06-02 07:31:39 +00:00
|
|
|
|
|
|
|
preimagePush: make(chan lntypes.Preimage),
|
2021-05-24 06:40:13 +00:00
|
|
|
cancelSwap: make(chan *outCancelDetails),
|
2020-05-29 09:27:47 +00:00
|
|
|
|
|
|
|
lnd: lnd,
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 09:43:03 +00:00
|
|
|
func (s *serverMock) NewLoopOutSwap(_ context.Context, swapHash lntypes.Hash,
|
|
|
|
amount btcutil.Amount, _ int32, _ [33]byte, _ time.Time,
|
|
|
|
_ string) (*newLoopOutResponse, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
_, senderKey := test.CreateKey(100)
|
|
|
|
|
|
|
|
if amount != s.expectedSwapAmt {
|
|
|
|
return nil, errors.New("unexpected test swap amount")
|
|
|
|
}
|
|
|
|
|
|
|
|
swapPayReqString, err := getInvoice(swapHash, s.swapInvoiceAmt,
|
|
|
|
swapInvoiceDesc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
prePayReqString, err := getInvoice(swapHash, s.prepayInvoiceAmt,
|
|
|
|
prepayInvoiceDesc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var senderKeyArray [33]byte
|
|
|
|
copy(senderKeyArray[:], senderKey.SerializeCompressed())
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
return &newLoopOutResponse{
|
2019-03-06 20:13:50 +00:00
|
|
|
senderKey: senderKeyArray,
|
|
|
|
swapInvoice: swapPayReqString,
|
|
|
|
prepayInvoice: prePayReqString,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-07-28 11:33:45 +00:00
|
|
|
func (s *serverMock) GetLoopOutTerms(ctx context.Context, initiator string) (
|
2019-03-07 04:32:24 +00:00
|
|
|
*LoopOutTerms, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-10-08 20:28:20 +00:00
|
|
|
return &LoopOutTerms{
|
|
|
|
MinSwapAmount: testMinSwapAmount,
|
|
|
|
MaxSwapAmount: testMaxSwapAmount,
|
2020-07-14 12:54:06 +00:00
|
|
|
MinCltvDelta: testLoopOutMinOnChainCltvDelta,
|
|
|
|
MaxCltvDelta: testLoopOutMaxOnChainCltvDelta,
|
2019-10-08 20:28:20 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-07 12:53:18 +00:00
|
|
|
func (s *serverMock) GetLoopOutQuote(ctx context.Context, amt btcutil.Amount,
|
2023-07-28 11:33:45 +00:00
|
|
|
expiry int32, _ time.Time, initiator string) (*LoopOutQuote, error) {
|
2019-10-08 20:28:20 +00:00
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
dest := [33]byte{1, 2, 3}
|
|
|
|
|
2019-10-08 20:28:20 +00:00
|
|
|
return &LoopOutQuote{
|
|
|
|
SwapFee: testSwapFee,
|
2019-03-06 20:13:50 +00:00
|
|
|
SwapPaymentDest: dest,
|
2019-10-08 20:28:20 +00:00
|
|
|
PrepayAmount: testFixedPrepayAmount,
|
2019-03-06 20:13:50 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInvoice(hash lntypes.Hash, amt btcutil.Amount, memo string) (string, error) {
|
2021-05-24 06:46:03 +00:00
|
|
|
// Set different payment addresses for swap invoices.
|
|
|
|
payAddr := [32]byte{1, 2, 3}
|
|
|
|
if memo == swapInvoiceDesc {
|
|
|
|
payAddr = [32]byte{3, 2, 1}
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
req, err := zpay32.NewInvoice(
|
|
|
|
&chaincfg.TestNet3Params, hash, testTime,
|
|
|
|
zpay32.Description(memo),
|
|
|
|
zpay32.Amount(lnwire.MilliSatoshi(1000*amt)),
|
2021-05-24 06:46:03 +00:00
|
|
|
zpay32.PaymentAddr(payAddr),
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
reqString, err := test.EncodePayReq(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reqString, nil
|
|
|
|
}
|
2019-03-12 15:10:37 +00:00
|
|
|
|
2020-11-06 09:43:03 +00:00
|
|
|
func (s *serverMock) NewLoopInSwap(_ context.Context, swapHash lntypes.Hash,
|
2023-01-06 16:16:17 +00:00
|
|
|
amount btcutil.Amount, _, _ [33]byte, swapInvoice, _ string,
|
2020-11-06 09:43:03 +00:00
|
|
|
_ *route.Vertex, _ string) (*newLoopInResponse, error) {
|
2019-03-12 15:10:37 +00:00
|
|
|
|
|
|
|
_, receiverKey := test.CreateKey(101)
|
2023-01-06 16:16:17 +00:00
|
|
|
_, receiverInternalKey := test.CreateKey(102)
|
2019-03-12 15:10:37 +00:00
|
|
|
|
|
|
|
if amount != s.expectedSwapAmt {
|
|
|
|
return nil, errors.New("unexpected test swap amount")
|
|
|
|
}
|
|
|
|
|
2023-01-06 16:16:17 +00:00
|
|
|
var receiverKeyArray, receiverInternalKeyArray [33]byte
|
2019-03-12 15:10:37 +00:00
|
|
|
copy(receiverKeyArray[:], receiverKey.SerializeCompressed())
|
2023-01-06 16:16:17 +00:00
|
|
|
copy(
|
|
|
|
receiverInternalKeyArray[:],
|
|
|
|
receiverInternalKey.SerializeCompressed(),
|
|
|
|
)
|
2019-03-12 15:10:37 +00:00
|
|
|
|
|
|
|
s.swapInvoice = swapInvoice
|
|
|
|
s.swapHash = swapHash
|
|
|
|
|
2020-05-29 09:27:47 +00:00
|
|
|
// Simulate the server paying the probe invoice and expect the client to
|
|
|
|
// cancel the probe payment.
|
|
|
|
probeSub := <-s.lnd.SingleInvoiceSubcribeChannel
|
|
|
|
probeSub.Update <- lndclient.InvoiceUpdate{
|
2023-01-09 15:36:52 +00:00
|
|
|
State: invpkg.ContractAccepted,
|
2020-05-29 09:27:47 +00:00
|
|
|
}
|
|
|
|
<-s.lnd.FailInvoiceChannel
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
resp := &newLoopInResponse{
|
2023-01-06 16:16:17 +00:00
|
|
|
expiry: s.height + testChargeOnChainCltvDelta,
|
|
|
|
receiverKey: receiverKeyArray,
|
|
|
|
receiverInternalKey: receiverInternalKeyArray,
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2020-06-02 07:31:39 +00:00
|
|
|
func (s *serverMock) PushLoopOutPreimage(_ context.Context,
|
|
|
|
preimage lntypes.Preimage) error {
|
|
|
|
|
|
|
|
// Push the preimage into the mock's preimage channel.
|
|
|
|
s.preimagePush <- preimage
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 06:40:13 +00:00
|
|
|
// CancelLoopOutSwap pushes a request to cancel a swap into our mock's channel.
|
|
|
|
func (s *serverMock) CancelLoopOutSwap(ctx context.Context,
|
|
|
|
details *outCancelDetails) error {
|
|
|
|
|
|
|
|
s.cancelSwap <- details
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 06:46:03 +00:00
|
|
|
func (s *serverMock) assertSwapCanceled(t *testing.T, details *outCancelDetails) {
|
|
|
|
require.Equal(t, details, <-s.cancelSwap)
|
|
|
|
}
|
|
|
|
|
2023-07-28 11:33:45 +00:00
|
|
|
func (s *serverMock) GetLoopInTerms(ctx context.Context, initiator string) (
|
2019-03-12 15:10:37 +00:00
|
|
|
*LoopInTerms, error) {
|
|
|
|
|
|
|
|
return &LoopInTerms{
|
|
|
|
MinSwapAmount: testMinSwapAmount,
|
|
|
|
MaxSwapAmount: testMaxSwapAmount,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-10-08 20:28:20 +00:00
|
|
|
|
2021-05-10 14:55:53 +00:00
|
|
|
func (s *serverMock) GetLoopInQuote(context.Context, btcutil.Amount,
|
2023-07-28 11:33:45 +00:00
|
|
|
route.Vertex, *route.Vertex, [][]zpay32.HopHint, string) (*LoopInQuote, error) {
|
2019-10-08 20:28:20 +00:00
|
|
|
|
|
|
|
return &LoopInQuote{
|
|
|
|
SwapFee: testSwapFee,
|
|
|
|
CltvDelta: testChargeOnChainCltvDelta,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-07-16 10:01:02 +00:00
|
|
|
|
|
|
|
// SubscribeLoopOutUpdates provides a mocked implementation of state
|
|
|
|
// subscriptions.
|
|
|
|
func (s *serverMock) SubscribeLoopOutUpdates(_ context.Context,
|
|
|
|
_ lntypes.Hash) (<-chan *ServerUpdate, <-chan error, error) {
|
|
|
|
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeLoopInUpdates provides a mocked implementation of state subscriptions.
|
|
|
|
func (s *serverMock) SubscribeLoopInUpdates(_ context.Context,
|
|
|
|
_ lntypes.Hash) (<-chan *ServerUpdate, <-chan error, error) {
|
|
|
|
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2021-05-10 14:55:53 +00:00
|
|
|
|
|
|
|
func (s *serverMock) Probe(ctx context.Context, amt btcutil.Amount,
|
|
|
|
pubKey route.Vertex, lastHop *route.Vertex,
|
|
|
|
routeHints [][]zpay32.HopHint) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-28 18:49:59 +00:00
|
|
|
|
2022-02-04 14:18:41 +00:00
|
|
|
func (s *serverMock) RecommendRoutingPlugin(_ context.Context, _ lntypes.Hash,
|
|
|
|
_ [32]byte) (RoutingPluginType, error) {
|
2021-11-28 18:49:59 +00:00
|
|
|
|
|
|
|
return RoutingPluginNone, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serverMock) ReportRoutingResult(_ context.Context, _ lntypes.Hash,
|
2022-02-04 14:18:41 +00:00
|
|
|
_ [32]byte, _ RoutingPluginType, _ bool, _ int32, _ int64) error {
|
2021-11-28 18:49:59 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-04 20:26:24 +00:00
|
|
|
|
|
|
|
func (s *serverMock) MuSig2SignSweep(_ context.Context, _ loopdb.ProtocolVersion,
|
2022-07-08 12:33:06 +00:00
|
|
|
_ lntypes.Hash, _ [32]byte, _ []byte, _ []byte) ([]byte,
|
|
|
|
[]byte, error) {
|
2022-05-04 20:26:24 +00:00
|
|
|
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2023-01-06 17:02:29 +00:00
|
|
|
|
2024-01-18 11:26:07 +00:00
|
|
|
func (s *serverMock) MultiMuSig2SignSweep(ctx context.Context,
|
|
|
|
protocolVersion loopdb.ProtocolVersion, swapHash lntypes.Hash,
|
|
|
|
paymentAddr [32]byte, nonce []byte, sweepTxPsbt []byte,
|
|
|
|
prevoutMap map[wire.OutPoint]*wire.TxOut) (
|
|
|
|
[]byte, []byte, error) {
|
|
|
|
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2023-01-06 17:02:29 +00:00
|
|
|
func (s *serverMock) PushKey(_ context.Context, _ loopdb.ProtocolVersion,
|
|
|
|
_ lntypes.Hash, _ [32]byte) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-11 20:05:13 +00:00
|
|
|
|
|
|
|
func (s *serverMock) FetchL402(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|