forked from lightninglabs/loop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_mock_test.go
163 lines (129 loc) · 3.8 KB
/
server_mock_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package loop
import (
"context"
"errors"
"testing"
"time"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/lightninglabs/loop/test"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/zpay32"
)
var (
testTime = time.Date(2018, time.January, 9, 14, 00, 00, 0, time.UTC)
testLoopOutOnChainCltvDelta = int32(30)
testChargeOnChainCltvDelta = int32(100)
testCltvDelta = 50
testSwapFeeBase = btcutil.Amount(21)
testSwapFeeRate = int64(100)
testInvoiceExpiry = 180 * time.Second
testFixedPrepayAmount = btcutil.Amount(100)
testMinSwapAmount = btcutil.Amount(10000)
testMaxSwapAmount = btcutil.Amount(1000000)
testTxConfTarget = 2
testRepublishDelay = 10 * time.Second
)
// serverMock is used in client unit tests to simulate swap server behaviour.
type serverMock struct {
t *testing.T
expectedSwapAmt btcutil.Amount
swapInvoiceAmt btcutil.Amount
prepayInvoiceAmt btcutil.Amount
height int32
swapInvoice string
swapHash lntypes.Hash
}
func newServerMock() *serverMock {
return &serverMock{
expectedSwapAmt: 50000,
// Total swap fee: 1000 + 0.01 * 50000 = 1050
swapInvoiceAmt: 50950,
prepayInvoiceAmt: 100,
height: 600,
}
}
func (s *serverMock) NewLoopOutSwap(ctx context.Context,
swapHash lntypes.Hash, amount btcutil.Amount,
receiverKey [33]byte) (
*newLoopOutResponse, error) {
_, 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())
return &newLoopOutResponse{
senderKey: senderKeyArray,
swapInvoice: swapPayReqString,
prepayInvoice: prePayReqString,
expiry: s.height + testLoopOutOnChainCltvDelta,
}, nil
}
func (s *serverMock) GetLoopOutTerms(ctx context.Context) (
*LoopOutTerms, error) {
dest := [33]byte{1, 2, 3}
return &LoopOutTerms{
SwapFeeBase: testSwapFeeBase,
SwapFeeRate: testSwapFeeRate,
SwapPaymentDest: dest,
CltvDelta: testLoopOutOnChainCltvDelta,
MinSwapAmount: testMinSwapAmount,
MaxSwapAmount: testMaxSwapAmount,
PrepayAmt: testFixedPrepayAmount,
}, 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
}
func (s *serverMock) NewLoopInSwap(ctx context.Context,
swapHash lntypes.Hash, amount btcutil.Amount,
senderKey [33]byte, swapInvoice string) (
*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
}
func (s *serverMock) GetLoopInTerms(ctx context.Context) (
*LoopInTerms, error) {
return &LoopInTerms{
SwapFeeBase: testSwapFeeBase,
SwapFeeRate: testSwapFeeRate,
CltvDelta: testChargeOnChainCltvDelta,
MinSwapAmount: testMinSwapAmount,
MaxSwapAmount: testMaxSwapAmount,
}, nil
}