2019-03-07 02:22:46 +00:00
|
|
|
package loop
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2019-03-06 23:29:44 +00:00
|
|
|
"github.com/lightninglabs/loop/test"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testTime = time.Date(2018, time.January, 9, 14, 00, 00, 0, time.UTC)
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
testLoopOutOnChainCltvDelta = int32(30)
|
2019-03-12 15:10:37 +00:00
|
|
|
testChargeOnChainCltvDelta = int32(100)
|
2019-10-08 20:28:20 +00:00
|
|
|
testSwapFee = btcutil.Amount(210)
|
2019-03-07 04:32:24 +00:00
|
|
|
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
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 20:28:20 +00:00
|
|
|
var _ swapServerClient = (*serverMock)(nil)
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
func newServerMock() *serverMock {
|
|
|
|
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),
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *serverMock) NewLoopOutSwap(ctx context.Context,
|
2019-03-06 20:13:50 +00:00
|
|
|
swapHash lntypes.Hash, amount btcutil.Amount,
|
2019-11-14 09:35:32 +00:00
|
|
|
receiverKey [33]byte, _ time.Time) (
|
2019-03-07 04:32:24 +00:00
|
|
|
*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,
|
2019-03-07 04:32:24 +00:00
|
|
|
expiry: s.height + testLoopOutOnChainCltvDelta,
|
2019-03-06 20:13:50 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *serverMock) GetLoopOutTerms(ctx context.Context) (
|
|
|
|
*LoopOutTerms, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-10-08 20:28:20 +00:00
|
|
|
return &LoopOutTerms{
|
|
|
|
MinSwapAmount: testMinSwapAmount,
|
|
|
|
MaxSwapAmount: testMaxSwapAmount,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-07 12:53:18 +00:00
|
|
|
func (s *serverMock) GetLoopOutQuote(ctx context.Context, amt btcutil.Amount,
|
|
|
|
_ time.Time) (*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-03-07 04:32:24 +00:00
|
|
|
CltvDelta: testLoopOutOnChainCltvDelta,
|
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) {
|
|
|
|
req, err := zpay32.NewInvoice(
|
|
|
|
&chaincfg.TestNet3Params, hash, testTime,
|
|
|
|
zpay32.Description(memo),
|
|
|
|
zpay32.Amount(lnwire.MilliSatoshi(1000*amt)),
|
|
|
|
)
|
|
|
|
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
|
|
|
|
|
|
|
func (s *serverMock) NewLoopInSwap(ctx context.Context,
|
|
|
|
swapHash lntypes.Hash, amount btcutil.Amount,
|
2020-02-11 12:58:55 +00:00
|
|
|
senderKey [33]byte, swapInvoice string, lastHop *route.Vertex) (
|
2019-03-12 15:10:37 +00:00
|
|
|
*newLoopInResponse, error) {
|
|
|
|
|
|
|
|
_, receiverKey := test.CreateKey(101)
|
|
|
|
|
|
|
|
if amount != s.expectedSwapAmt {
|
|
|
|
return nil, errors.New("unexpected test swap amount")
|
|
|
|
}
|
|
|
|
|
|
|
|
var receiverKeyArray [33]byte
|
|
|
|
copy(receiverKeyArray[:], receiverKey.SerializeCompressed())
|
|
|
|
|
|
|
|
s.swapInvoice = swapInvoice
|
|
|
|
s.swapHash = swapHash
|
|
|
|
|
|
|
|
resp := &newLoopInResponse{
|
|
|
|
expiry: s.height + testChargeOnChainCltvDelta,
|
|
|
|
receiverKey: receiverKeyArray,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
func (s *serverMock) GetLoopInTerms(ctx context.Context) (
|
|
|
|
*LoopInTerms, error) {
|
|
|
|
|
|
|
|
return &LoopInTerms{
|
|
|
|
MinSwapAmount: testMinSwapAmount,
|
|
|
|
MaxSwapAmount: testMaxSwapAmount,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-10-08 20:28:20 +00:00
|
|
|
|
|
|
|
func (s *serverMock) GetLoopInQuote(ctx context.Context, amt btcutil.Amount) (
|
|
|
|
*LoopInQuote, error) {
|
|
|
|
|
|
|
|
return &LoopInQuote{
|
|
|
|
SwapFee: testSwapFee,
|
|
|
|
CltvDelta: testChargeOnChainCltvDelta,
|
|
|
|
}, nil
|
|
|
|
}
|