forked from lightninglabs/loop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loopout_feerate_test.go
278 lines (245 loc) · 7.13 KB
/
loopout_feerate_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package loop
import (
"context"
"fmt"
"testing"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/sweep"
"github.com/lightninglabs/loop/test"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/stretchr/testify/require"
)
// testSweeper is implementation of sweeper.Sweeper for test.
type testSweeper struct {
}
// GetSweepFeeDetails calculates the required tx fee to spend to destAddr. It
// takes a function that is expected to add the weight of the input to the
// weight estimator. It returns also the fee rate and transaction weight.
func (s testSweeper) GetSweepFeeDetails(ctx context.Context,
addInputEstimate func(*input.TxWeightEstimator) error,
destAddr btcutil.Address, sweepConfTarget int32,
label string) (btcutil.Amount, chainfee.SatPerKWeight,
lntypes.WeightUnit, error) {
var feeRate chainfee.SatPerKWeight
switch {
case sweepConfTarget == 0:
return 0, 0, 0, fmt.Errorf("zero sweepConfTarget")
case sweepConfTarget == 1:
feeRate = 30000
case sweepConfTarget == 2:
feeRate = 25000
case sweepConfTarget == 3:
feeRate = 20000
case sweepConfTarget < 10:
feeRate = 8000
case sweepConfTarget < 100:
feeRate = 5000
case sweepConfTarget < 1000:
feeRate = 2000
default:
feeRate = 250
}
// Calculate weight for this tx.
var weightEstimate input.TxWeightEstimator
// Add output.
err := sweep.AddOutputEstimate(&weightEstimate, destAddr)
if err != nil {
return 0, 0, 0, fmt.Errorf("failed to add output weight "+
"estimate: %w", err)
}
// Add input.
err = addInputEstimate(&weightEstimate)
if err != nil {
return 0, 0, 0, fmt.Errorf("failed to add input weight "+
"estimate: %w", err)
}
// Find weight.
weight := weightEstimate.Weight()
return feeRate.FeeForWeight(weight), feeRate, weight, nil
}
// TestLoopOutSweepFeerateProvider tests that loopOutSweepFeerateProvider
// provides correct fee rate for loop-out swaps.
func TestLoopOutSweepFeerateProvider(t *testing.T) {
htlcKeys := func() loopdb.HtlcKeys {
var senderKey, receiverKey [33]byte
// 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,
}
}()
var destAddr *btcutil.AddressTaproot
swapInvoice := "lntb1230n1pjjszzgpp5j76f03wrkya4sm4gxv6az5nmz5aqsvmn4" +
"tpguu2sdvdyygedqjgqdq9xyerxcqzzsxqr23ssp5rwzmwtfjmsgranfk8sr" +
"4p4gcgmvyd42uug8pxteg2mkk23ndvkqs9qyyssq44ruk3ex59cmv4dm6k4v" +
"0kc6c0gcqjs0gkljfyd6c6uatqa2f67xlx3pcg5tnvcae5p3jju8ra77e87d" +
"vhhs0jrx53wnc0fq9rkrhmqqelyx7l"
cases := []struct {
name string
cltvExpiry int32
height int32
amount btcutil.Amount
protocolVersion loopdb.ProtocolVersion
wantConfTarget int32
wantFeeRate chainfee.SatPerKWeight
wantError string
}{
{
name: "simple case",
cltvExpiry: 801_000,
height: 800_900,
amount: 1_000_000,
protocolVersion: loopdb.ProtocolVersionMuSig2,
wantConfTarget: 100,
wantFeeRate: 2000,
},
{
name: "zero height",
cltvExpiry: 801_000,
height: 0,
amount: 1_000_000,
protocolVersion: loopdb.ProtocolVersionMuSig2,
wantError: "got zero best block height",
},
{
name: "huge amount, no proportional fee",
cltvExpiry: 801_000,
height: 800_900,
amount: 100_000_000,
protocolVersion: loopdb.ProtocolVersionMuSig2,
wantConfTarget: 100,
wantFeeRate: 2000,
},
{
name: "huge amount, no proportional fee, v2",
cltvExpiry: 801_000,
height: 800_900,
amount: 100_000_000,
protocolVersion: loopdb.ProtocolVersionLoopOutCancel,
wantConfTarget: 100,
wantFeeRate: 2000,
},
{
name: "huge amount, no proportional fee, " +
"capped by urgent fee",
cltvExpiry: 801_000,
height: 800_900,
amount: 200_000_000,
protocolVersion: loopdb.ProtocolVersionMuSig2,
wantConfTarget: 100,
wantFeeRate: 2000,
},
{
name: "11 blocks until expiry",
cltvExpiry: 801_000,
height: 800_989,
amount: 1_000_000,
protocolVersion: loopdb.ProtocolVersionMuSig2,
wantConfTarget: 11,
wantFeeRate: 5000,
},
{
name: "10 blocks until expiry",
cltvExpiry: 801_000,
height: 800_990,
amount: 1_000_000,
protocolVersion: loopdb.ProtocolVersionMuSig2,
wantConfTarget: 3,
wantFeeRate: 22000,
},
{
name: "9 blocks until expiry",
cltvExpiry: 801_000,
height: 800_991,
amount: 1_000_000,
protocolVersion: loopdb.ProtocolVersionMuSig2,
wantConfTarget: 3,
wantFeeRate: 22000,
},
{
name: "3 blocks until expiry",
cltvExpiry: 801_000,
height: 800_997,
amount: 1_000_000,
protocolVersion: loopdb.ProtocolVersionMuSig2,
wantConfTarget: 3,
wantFeeRate: 22000,
},
{
name: "2 blocks until expiry",
cltvExpiry: 801_000,
height: 800_998,
amount: 1_000_000,
protocolVersion: loopdb.ProtocolVersionMuSig2,
wantConfTarget: 2,
wantFeeRate: 27500,
},
{
name: "1 blocks until expiry",
cltvExpiry: 801_000,
height: 800_999,
amount: 1_000_000,
protocolVersion: loopdb.ProtocolVersionMuSig2,
wantConfTarget: 1,
wantFeeRate: 33000,
},
{
name: "expired",
cltvExpiry: 801_000,
height: 801_000,
amount: 1_000_000,
protocolVersion: loopdb.ProtocolVersionMuSig2,
wantConfTarget: 1,
wantFeeRate: 33000,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
store := loopdb.NewStoreMock(t)
ctx := context.Background()
swapHash := lntypes.Hash{1, 1, 1}
swap := &loopdb.LoopOutContract{
SwapContract: loopdb.SwapContract{
CltvExpiry: tc.cltvExpiry,
AmountRequested: tc.amount,
ProtocolVersion: tc.protocolVersion,
HtlcKeys: htlcKeys,
},
DestAddr: destAddr,
SwapInvoice: swapInvoice,
SweepConfTarget: 100,
}
err := store.CreateLoopOut(ctx, swapHash, swap)
require.NoError(t, err)
store.AssertLoopOutStored()
getHeight := func() int32 {
return tc.height
}
p := newLoopOutSweepFeerateProvider(
testSweeper{}, store,
&chaincfg.RegressionNetParams, getHeight,
)
confTarget, feeRate, err := p.GetConfTargetAndFeeRate(
ctx, swapHash,
)
if tc.wantError != "" {
require.ErrorContains(t, err, tc.wantError)
return
}
require.NoError(t, err)
require.Equal(t, tc.wantConfTarget, confTarget)
require.Equal(t, tc.wantFeeRate, feeRate)
})
}
}