-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathcoin_select_test.go
909 lines (807 loc) · 23.1 KB
/
coin_select_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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
package chanfunding
import (
"encoding/hex"
"regexp"
"testing"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/wallet"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/stretchr/testify/require"
)
var (
p2wkhScript, _ = hex.DecodeString(
"001411034bdcb6ccb7744fdfdeea958a6fb0b415a032",
)
np2wkhScript, _ = hex.DecodeString(
"a914f7bd5b8077b9549653dacf96f824af9d931663e687",
)
p2khScript, _ = hex.DecodeString(
"76a91411034bdcb6ccb7744fdfdeea958a6fb0b415a03288ac",
)
defaultChanFundingChangeType = P2TRChangeAddress
)
// fundingFee is a helper method that returns the fee estimate used for a tx
// with the given number of inputs and the optional change output. This matches
// the estimate done by the wallet.
func fundingFee(feeRate chainfee.SatPerKWeight, numInput int, // nolint:unparam
change bool) btcutil.Amount {
var weightEstimate input.TxWeightEstimator
// All inputs.
for i := 0; i < numInput; i++ {
weightEstimate.AddP2WKHInput()
}
// The multisig funding output.
weightEstimate.AddP2WSHOutput()
// Optionally count a change output.
if change {
weightEstimate.AddP2TROutput()
}
totalWeight := weightEstimate.Weight()
return feeRate.FeeForWeight(totalWeight)
}
// TestCalculateFees tests that the helper function to calculate the fees
// both with and without applying a change output is done correctly for
// (N)P2WKH inputs, and should raise an error otherwise.
func TestCalculateFees(t *testing.T) {
t.Parallel()
const feeRate = chainfee.SatPerKWeight(1000)
type testCase struct {
name string
utxos []wallet.Coin
expectedFeeNoChange btcutil.Amount
expectedFeeWithChange btcutil.Amount
expectedErr error
}
testCases := []testCase{
{
name: "one P2WKH input",
utxos: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1,
},
},
},
expectedFeeNoChange: 487,
expectedFeeWithChange: 659,
expectedErr: nil,
},
{
name: "one NP2WKH input",
utxos: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: np2wkhScript,
Value: 1,
},
},
},
expectedFeeNoChange: 579,
expectedFeeWithChange: 751,
expectedErr: nil,
},
{
name: "not supported P2KH input",
utxos: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2khScript,
Value: 1,
},
},
},
expectedErr: &errUnsupportedInput{p2khScript},
},
}
fundingOutputEstimate := input.TxWeightEstimator{}
fundingOutputEstimate.AddP2WSHOutput()
for _, test := range testCases {
test := test
t.Run(test.name, func(t *testing.T) {
feeNoChange, feeWithChange, err := calculateFees(
test.utxos, feeRate, fundingOutputEstimate,
defaultChanFundingChangeType,
)
require.Equal(t, test.expectedErr, err)
// Note: The error-case will have zero values returned
// for fees and therefore anyway pass the following
// requirements.
require.Equal(t, test.expectedFeeNoChange, feeNoChange)
require.Equal(t, test.expectedFeeWithChange, feeWithChange)
})
}
}
// TestCoinSelect tests that we pick coins adding up to the expected amount
// when creating a funding transaction, and that the calculated change is the
// expected amount.
//
// NOTE: coinSelect will always attempt to add a change output (unless the
// ExistingChangeAddress change address type is selected), so we must account
// for this in the tests.
func TestCoinSelect(t *testing.T) {
t.Parallel()
const (
feeRate = chainfee.SatPerKWeight(100)
dustLimit = btcutil.Amount(1000)
fullCoin = btcutil.SatoshiPerBitcoin
)
type testCase struct {
name string
outputValue btcutil.Amount
coins []wallet.Coin
changeType ChangeAddressType
expectedInput []btcutil.Amount
expectedChange btcutil.Amount
expectErr bool
}
testCases := []testCase{
{
// We have 1.0 BTC available, and wants to send 0.5.
// This will obviously lead to a change output of
// almost 0.5 BTC.
name: "big change",
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * fullCoin,
},
},
},
outputValue: 0.5 * fullCoin,
changeType: defaultChanFundingChangeType,
// The one and only input will be selected.
expectedInput: []btcutil.Amount{
1 * fullCoin,
},
// Change will be what's left minus the fee.
expectedChange: 0.5*fullCoin -
fundingFee(feeRate, 1, true),
},
{
// We have 1 BTC available, and we want to send 1 BTC.
// This should lead to an error, as we don't have
// enough funds to pay the fee.
name: "nothing left for fees",
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * fullCoin,
},
},
},
outputValue: 1 * fullCoin,
changeType: defaultChanFundingChangeType,
expectErr: true,
},
{
// We have a 1 BTC input, and want to create an output
// as big as possible, such that the remaining change
// would be dust but instead goes to fees.
name: "dust change",
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * fullCoin,
},
},
},
// We tune the output value by subtracting the expected
// fee and the dust limit.
outputValue: 1*fullCoin -
fundingFee(feeRate, 1, false) - dustLimit,
changeType: defaultChanFundingChangeType,
expectedInput: []btcutil.Amount{
1 * fullCoin,
},
// Change must be zero.
expectedChange: 0,
},
{
// We got just enough funds to create a change output
// above the dust limit.
name: "change right above dust limit",
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: int64(fundingFee(
feeRate, 1, true,
) + 2*(dustLimit+1)),
},
},
},
// We tune the output value to be just above the dust
// limit.
outputValue: dustLimit + 1,
changeType: defaultChanFundingChangeType,
expectedInput: []btcutil.Amount{
fundingFee(feeRate, 1, true) + 2*(dustLimit+1),
},
// After paying for the fee the change output should be
// just above the dust limit.
expectedChange: dustLimit + 1,
},
{
// If more than 20% of funds goes to fees, it should
// fail.
name: "high fee",
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: int64(5 * fundingFee(
feeRate, 1, false,
)),
},
},
},
outputValue: 4 * fundingFee(feeRate, 1, false),
changeType: defaultChanFundingChangeType,
expectErr: true,
},
{
// Fees go to an existing change output.
name: "existing change output",
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1000 + int64(fundingFee(
feeRate, 1, false,
)) + 1,
},
},
},
outputValue: 1000,
changeType: ExistingChangeAddress,
expectedInput: []btcutil.Amount{
1000 + fundingFee(feeRate, 1, false) + 1,
},
expectedChange: 1,
},
}
fundingOutputEstimate := input.TxWeightEstimator{}
fundingOutputEstimate.AddP2WSHOutput()
for _, test := range testCases {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
selected, changeAmt, err := CoinSelect(
feeRate, test.outputValue, dustLimit,
test.coins, wallet.CoinSelectionLargest,
fundingOutputEstimate, test.changeType,
)
if test.expectErr {
require.Error(t, err)
return
}
require.NoError(t, err)
// Check that the selected inputs match what we expect.
require.Len(t, selected, len(test.expectedInput))
for i, coin := range selected {
require.EqualValues(
t, test.expectedInput[i], coin.Value,
)
}
// Assert we got the expected change amount.
require.EqualValues(t, test.expectedChange, changeAmt)
})
}
}
// TestCalculateChangeAmount tests that the change amount calculation performs
// correctly, taking into account the type of change output and whether we want
// to create a change output in the first place.
func TestCalculateChangeAmount(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
totalInputAmt btcutil.Amount
requiredAmt btcutil.Amount
feeNoChange btcutil.Amount
feeWithChange btcutil.Amount
dustLimit btcutil.Amount
changeType ChangeAddressType
expectErr string
expectChangeAmt btcutil.Amount
expectNeedMore btcutil.Amount
}{{
// Coin selection returned a coin larger than the required
// amount, but still not enough to pay for the fees. This should
// trigger another round of coin selection with a larger
// required amount.
name: "need to select more",
totalInputAmt: 500,
requiredAmt: 490,
feeNoChange: 12,
expectNeedMore: 502,
}, {
// We are using an existing change output, so we'll only want
// to make sure to select enough for a TX _without_ a change
// output added. Because we're using an existing output, the
// dust limit calculation should also be skipped.
name: "sufficiently large for existing change output",
totalInputAmt: 500,
requiredAmt: 400,
feeNoChange: 10,
feeWithChange: 10,
dustLimit: 100,
changeType: ExistingChangeAddress,
expectChangeAmt: 90,
}, {
name: "sufficiently large for adding a change output",
totalInputAmt: 500,
requiredAmt: 300,
feeNoChange: 40,
feeWithChange: 50,
dustLimit: 100,
expectChangeAmt: 150,
}, {
name: "sufficiently large for tx without change " +
"amount",
totalInputAmt: 500,
requiredAmt: 460,
feeNoChange: 40,
feeWithChange: 50,
expectChangeAmt: 0,
}, {
name: "fee percent too large",
totalInputAmt: 100,
requiredAmt: 50,
feeNoChange: 10,
feeWithChange: 45,
dustLimit: 5,
expectErr: "fee 0.00000045 BTC on total output value " +
"0.00000055",
}, {
name: "invalid usage of function",
feeNoChange: 5,
feeWithChange: 10,
changeType: ExistingChangeAddress,
expectErr: "fees for with or without change must be the same",
}}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(tt *testing.T) {
changeAmt, needMore, err := CalculateChangeAmount(
tc.totalInputAmt, tc.requiredAmt,
tc.feeNoChange, tc.feeWithChange, tc.dustLimit,
tc.changeType,
)
if tc.expectErr != "" {
require.ErrorContains(tt, err, tc.expectErr)
return
}
require.EqualValues(tt, tc.expectChangeAmt, changeAmt)
require.EqualValues(tt, tc.expectNeedMore, needMore)
})
}
}
// TestCoinSelectSubtractFees tests that we pick coins adding up to the
// expected amount when creating a funding transaction, and that a change
// output is created only when necessary.
func TestCoinSelectSubtractFees(t *testing.T) {
t.Parallel()
const feeRate = chainfee.SatPerKWeight(100)
const highFeeRate = chainfee.SatPerKWeight(1000)
const dustLimit = btcutil.Amount(1000)
const dust = btcutil.Amount(100)
// removeAmounts replaces any amounts in string with "<amt>".
removeAmounts := func(s string) string {
re := regexp.MustCompile(`[[:digit:]]+\.?[[:digit:]]*`)
return re.ReplaceAllString(s, "<amt>")
}
type testCase struct {
name string
highFee bool
spendValue btcutil.Amount
coins []wallet.Coin
expectedInput []btcutil.Amount
expectedFundingAmt btcutil.Amount
expectedChange btcutil.Amount
expectErr string
}
testCases := []testCase{
{
// We have 1.0 BTC available, spend them all. This
// should lead to a funding TX with one output, the
// rest goes to fees.
name: "spend all",
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * btcutil.SatoshiPerBitcoin,
},
},
},
spendValue: 1 * btcutil.SatoshiPerBitcoin,
// The one and only input will be selected.
expectedInput: []btcutil.Amount{
1 * btcutil.SatoshiPerBitcoin,
},
expectedFundingAmt: 1*btcutil.SatoshiPerBitcoin - fundingFee(feeRate, 1, false),
expectedChange: 0,
},
{
// We have 1.0 BTC available and spend half of it. This
// should lead to a funding TX with a change output.
name: "spend with change",
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * btcutil.SatoshiPerBitcoin,
},
},
},
spendValue: 0.5 * btcutil.SatoshiPerBitcoin,
// The one and only input will be selected.
expectedInput: []btcutil.Amount{
1 * btcutil.SatoshiPerBitcoin,
},
expectedFundingAmt: 0.5*btcutil.SatoshiPerBitcoin - fundingFee(feeRate, 1, true),
expectedChange: 0.5 * btcutil.SatoshiPerBitcoin,
},
{
// The total funds available is below the dust limit
// after paying fees.
name: "dust output",
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: int64(fundingFee(feeRate, 1, false) + dustLimit - 1),
},
},
},
spendValue: fundingFee(feeRate, 1, false) + dust,
expectErr: "output amount(<amt> BTC) after subtracting " +
"fees(<amt> BTC) below dust limit(<amt> BTC)",
},
{
// After subtracting fees, the resulting change output
// is below the dust limit. The remainder should go
// towards the funding output.
name: "dust change",
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * btcutil.SatoshiPerBitcoin,
},
},
},
spendValue: 1*btcutil.SatoshiPerBitcoin - dust,
expectedInput: []btcutil.Amount{
1 * btcutil.SatoshiPerBitcoin,
},
expectedFundingAmt: 1*btcutil.SatoshiPerBitcoin - fundingFee(feeRate, 1, false),
expectedChange: 0,
},
{
// We got just enough funds to create an output above the dust limit.
name: "output right above dustlimit",
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: int64(fundingFee(feeRate, 1, false) + dustLimit + 1),
},
},
},
spendValue: fundingFee(feeRate, 1, false) + dustLimit + 1,
expectedInput: []btcutil.Amount{
fundingFee(feeRate, 1, false) + dustLimit + 1,
},
expectedFundingAmt: dustLimit + 1,
expectedChange: 0,
},
{
// Amount left is below dust limit after paying fee for
// a change output, resulting in a no-change tx.
name: "no amount to pay fee for change",
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: int64(fundingFee(feeRate, 1, false) + 2*(dustLimit+1)),
},
},
},
spendValue: fundingFee(feeRate, 1, false) + dustLimit + 1,
expectedInput: []btcutil.Amount{
fundingFee(feeRate, 1, false) + 2*(dustLimit+1),
},
expectedFundingAmt: 2 * (dustLimit + 1),
expectedChange: 0,
},
{
// If more than 20% of funds goes to fees, it should fail.
name: "high fee",
highFee: true,
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: int64(5 * fundingFee(highFeeRate, 1, false)),
},
},
},
spendValue: 5 * fundingFee(highFeeRate, 1, false),
expectErr: "fee <amt> BTC on total output value <amt> BTC",
},
}
fundingOutputEstimate := input.TxWeightEstimator{}
fundingOutputEstimate.AddP2WSHOutput()
for _, test := range testCases {
test := test
t.Run(test.name, func(t *testing.T) {
feeRate := feeRate
if test.highFee {
feeRate = highFeeRate
}
selected, localFundingAmt, changeAmt, err := CoinSelectSubtractFees(
feeRate, test.spendValue, dustLimit, test.coins,
wallet.CoinSelectionLargest,
fundingOutputEstimate,
defaultChanFundingChangeType,
)
if err != nil {
switch {
case test.expectErr == "":
t.Fatalf(err.Error())
case test.expectErr != removeAmounts(err.Error()):
t.Fatalf("expected error '%v', got '%v'",
test.expectErr,
removeAmounts(err.Error()))
// If we got an expected error, there is
// nothing more to test.
default:
return
}
}
// Check that there was no expected error we missed.
if test.expectErr != "" {
t.Fatalf("expected error")
}
// Check that the selected inputs match what we expect.
if len(selected) != len(test.expectedInput) {
t.Fatalf("expected %v inputs, got %v",
len(test.expectedInput), len(selected))
}
for i, coin := range selected {
if coin.Value != int64(test.expectedInput[i]) {
t.Fatalf("expected input %v to have value %v, "+
"had %v", i, test.expectedInput[i],
coin.Value)
}
}
// Assert we got the expected funding amount.
if localFundingAmt != test.expectedFundingAmt {
t.Fatalf("expected %v local funding amt, got %v",
test.expectedFundingAmt, localFundingAmt)
}
// Assert we got the expected change amount.
require.EqualValues(
t, test.expectedChange, changeAmt,
)
})
}
}
// TestCoinSelectUpToAmount tests that we pick coins adding up to the
// expected amount when creating a funding transaction, and that a change
// output is created only when necessary.
func TestCoinSelectUpToAmount(t *testing.T) {
t.Parallel()
const (
feeRate = chainfee.SatPerKWeight(100)
dustLimit = btcutil.Amount(1000)
dust = btcutil.Amount(100)
coin = btcutil.SatoshiPerBitcoin
minValue = 20_000
)
type testCase struct {
name string
minValue btcutil.Amount
maxValue btcutil.Amount
reserved btcutil.Amount
coins []wallet.Coin
expectedInput []btcutil.Amount
expectedFundingAmt btcutil.Amount
expectedChange btcutil.Amount
expectErr string
}
testCases := []testCase{{
// We have 1.0 BTC available, spend them all.
// This should lead to a funding TX with one output, the rest
// goes to fees.
name: "spend exactly all",
coins: []wallet.Coin{{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * coin,
},
}},
minValue: minValue,
maxValue: 1 * coin,
// The one and only input will be selected.
expectedInput: []btcutil.Amount{1 * coin},
expectedFundingAmt: 1*coin - fundingFee(feeRate, 1, false),
expectedChange: 0,
}, {
// We have 1.0 BTC available and want to spend up to 2 BTC.
// This should lead to a funding TX with one output, the rest
// goes to fees.
name: "spend more",
coins: []wallet.Coin{{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * coin,
},
}},
minValue: minValue,
maxValue: 2 * coin,
// The one and only input will be selected.
expectedInput: []btcutil.Amount{1 * coin},
expectedFundingAmt: 1*coin - fundingFee(feeRate, 1, false),
expectedChange: 0,
}, {
// We have 1.0 BTC available and want to spend up to 0.5 BTC.
// This should lead to a funding TX with one output and a
// change to subtract the fees from.
name: "spend far below",
coins: []wallet.Coin{{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * coin,
},
}},
minValue: minValue,
maxValue: 0.5 * coin,
// The one and only input will be selected.
expectedInput: []btcutil.Amount{1 * coin},
expectedFundingAmt: 0.5 * coin,
expectedChange: 0.5*coin - fundingFee(feeRate, 1, true),
}, {
// We have 1.0 BTC available and want to spend just 1 Satoshi
// below that amount.
// This should lead to a funding TX with one output where the
// fee is subtracted from the total 1 BTC input value.
name: "spend little below",
coins: []wallet.Coin{{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * coin,
},
}},
minValue: minValue,
maxValue: 1*coin - 1,
// The one and only input will be selected.
expectedInput: []btcutil.Amount{
1 * coin,
},
expectedFundingAmt: 1*coin - fundingFee(feeRate, 1, false),
expectedChange: 0,
}, {
// The total funds available is below the dust limit after
// paying fees.
name: "dust output",
coins: []wallet.Coin{{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: int64(
fundingFee(feeRate, 1, false) + dust,
),
},
}},
minValue: minValue,
maxValue: fundingFee(feeRate, 1, false) + dust,
expectErr: "output amount(0.00000100 BTC) after subtracting " +
"fees(0.00000048 BTC) below dust limit(0.00001000 BTC)",
}, {
// If more than 20% of available wallet funds goes to fees, it
// should fail.
name: "high fee",
coins: []wallet.Coin{{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: int64(
20 * fundingFee(feeRate, 1, false),
),
},
}},
minValue: minValue,
maxValue: 16 * fundingFee(feeRate, 1, false),
expectErr: "fee 0.00000192 BTC on total output value " +
"0.00000768 BTC",
}, {
// This test makes sure that the implementation detail of using
// CoinSelect and CoinSelectSubtractFees is done correctly.
// CoinSelect always defaults to use a fee for single input -
// one change tx, whereas CoinSelectSubtractFees will use a fee
// of single input - no change output, which without a sanity
// check could result in a local amount higher than the maximum
// amount that was expected.
name: "sanity check for correct maximum amount",
coins: []wallet.Coin{{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * coin,
},
}},
minValue: minValue,
maxValue: 1*coin - fundingFee(feeRate, 1, false) - 1,
expectedInput: []btcutil.Amount{1 * coin},
expectedFundingAmt: 1*coin - fundingFee(feeRate, 1, false) - 1,
expectedChange: 0,
}, {
// This test makes sure that if a reserved value is required
// then it is handled correctly by leaving exactly the reserved
// value as change and still maxing out the funding amount.
name: "sanity check for correct reserved amount subtract " +
"from total",
coins: []wallet.Coin{{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * coin,
},
}},
minValue: minValue,
maxValue: 1*coin - 9000,
reserved: 10000,
expectedInput: []btcutil.Amount{1 * coin},
expectedFundingAmt: 1*coin -
fundingFee(feeRate, 1, true) - 10000,
expectedChange: 10000,
}}
fundingOutputEstimate := input.TxWeightEstimator{}
fundingOutputEstimate.AddP2WSHOutput()
for _, test := range testCases {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
selected, localFundingAmt, changeAmt,
err := CoinSelectUpToAmount(
feeRate, test.minValue, test.maxValue,
test.reserved, dustLimit, test.coins,
wallet.CoinSelectionLargest,
fundingOutputEstimate,
defaultChanFundingChangeType,
)
if len(test.expectErr) == 0 && err != nil {
t.Fatal(err.Error())
}
if changeAmt != test.expectedChange {
t.Fatalf("expected %v change amt, got %v",
test.expectedChange, changeAmt)
}
if len(test.expectErr) > 0 && err == nil {
t.Fatalf("expected error: %v", test.expectErr)
}
if len(test.expectErr) > 0 && err != nil {
require.EqualError(t, err, test.expectErr)
}
// Check that the selected inputs match what we expect.
require.Equal(t, len(test.expectedInput), len(selected))
for i, coin := range selected {
require.EqualValues(
t, test.expectedInput[i], coin.Value,
)
}
// Assert we got the expected funding amount.
require.Equal(
t, test.expectedFundingAmt, localFundingAmt,
)
// Assert we got the expected change amount.
require.Equal(
t, test.expectedChange, changeAmt,
)
})
}
}