@@ -26,14 +26,10 @@ type testCase struct {
2626
2727// TestSelectDeposits tests the selectDeposits function, which selects
2828// deposits that can cover a target value while respecting the dust limit.
29- // Sorting priority: 1) more confirmations first, 2) larger amounts first,
30- // 3) expiring sooner first.
3129func TestSelectDeposits (t * testing.T ) {
32- // Note: confirmations = blockHeight - ConfirmationHeight
33- // Lower ConfirmationHeight means more confirmations at a given block.
3430 d1 , d2 , d3 , d4 := & deposit.Deposit {
3531 Value : 1_000_000 ,
36- ConfirmationHeight : 5_000 , // most confs at height 5100
32+ ConfirmationHeight : 5_000 ,
3733 }, & deposit.Deposit {
3834 Value : 2_000_000 ,
3935 ConfirmationHeight : 5_001 ,
@@ -42,7 +38,7 @@ func TestSelectDeposits(t *testing.T) {
4238 ConfirmationHeight : 5_002 ,
4339 }, & deposit.Deposit {
4440 Value : 3_000_000 ,
45- ConfirmationHeight : 5_003 , // fewest confs at height 5100
41+ ConfirmationHeight : 5_003 ,
4642 }
4743 d1 .Hash = chainhash.Hash {1 }
4844 d1 .Index = 0
@@ -53,108 +49,75 @@ func TestSelectDeposits(t *testing.T) {
5349 d4 .Hash = chainhash.Hash {4 }
5450 d4 .Index = 0
5551
56- // Use a realistic block height and csv expiry for all standard
57- // test cases. csvExpiry must be large enough that deposits remain
58- // swappable at this block height.
59- const (
60- testBlockHeight uint32 = 5_100
61- testCsvExpiry uint32 = 2_500
62- )
63-
6452 testCases := []testCase {
6553 {
6654 name : "single deposit exact target" ,
6755 deposits : []* deposit.Deposit {d1 },
6856 targetValue : 1_000_000 ,
69- csvExpiry : testCsvExpiry ,
70- blockHeight : testBlockHeight ,
7157 expected : []* deposit.Deposit {d1 },
7258 expectedErr : "" ,
7359 },
7460 {
75- // d1 has more confirmations, so it's preferred even
76- // though d2 is larger.
77- name : "prefer more confirmed deposit over larger" ,
61+ name : "prefer larger deposit when both cover" ,
7862 deposits : []* deposit.Deposit {d1 , d2 },
7963 targetValue : 1_000_000 ,
80- csvExpiry : testCsvExpiry ,
81- blockHeight : testBlockHeight ,
82- expected : []* deposit.Deposit {d1 },
64+ expected : []* deposit.Deposit {d2 },
8365 expectedErr : "" ,
8466 },
8567 {
86- // d1 has the most confirmations among d1, d2, d3.
87- name : "prefer most confirmed among three" ,
68+ name : "prefer largest among three when one is enough" ,
8869 deposits : []* deposit.Deposit {d1 , d2 , d3 },
8970 targetValue : 1_000_000 ,
90- csvExpiry : testCsvExpiry ,
91- blockHeight : testBlockHeight ,
92- expected : []* deposit.Deposit {d1 },
71+ expected : []* deposit.Deposit {d3 },
9372 expectedErr : "" ,
9473 },
9574 {
9675 name : "single deposit insufficient by 1" ,
9776 deposits : []* deposit.Deposit {d1 },
9877 targetValue : 1_000_001 ,
99- csvExpiry : testCsvExpiry ,
100- blockHeight : testBlockHeight ,
10178 expected : []* deposit.Deposit {},
10279 expectedErr : "not enough deposits to cover" ,
10380 },
10481 {
10582 name : "target leaves exact dust limit change" ,
10683 deposits : []* deposit.Deposit {d1 },
10784 targetValue : 1_000_000 - dustLimit ,
108- csvExpiry : testCsvExpiry ,
109- blockHeight : testBlockHeight ,
11085 expected : []* deposit.Deposit {d1 },
11186 expectedErr : "" ,
11287 },
11388 {
11489 name : "target leaves dust change (just over)" ,
11590 deposits : []* deposit.Deposit {d1 },
11691 targetValue : 1_000_000 - dustLimit + 1 ,
117- csvExpiry : testCsvExpiry ,
118- blockHeight : testBlockHeight ,
11992 expected : []* deposit.Deposit {},
12093 expectedErr : "not enough deposits to cover" ,
12194 },
12295 {
12396 name : "all deposits exactly match target" ,
12497 deposits : []* deposit.Deposit {d1 , d2 , d3 },
12598 targetValue : d1 .Value + d2 .Value + d3 .Value ,
126- csvExpiry : testCsvExpiry ,
127- blockHeight : testBlockHeight ,
12899 expected : []* deposit.Deposit {d1 , d2 , d3 },
129100 expectedErr : "" ,
130101 },
131102 {
132103 name : "sum minus dust limit is allowed (change == dust)" ,
133104 deposits : []* deposit.Deposit {d1 , d2 , d3 },
134105 targetValue : d1 .Value + d2 .Value + d3 .Value - dustLimit ,
135- csvExpiry : testCsvExpiry ,
136- blockHeight : testBlockHeight ,
137106 expected : []* deposit.Deposit {d1 , d2 , d3 },
138107 expectedErr : "" ,
139108 },
140109 {
141110 name : "sum minus dust limit plus 1 is not allowed (dust change)" ,
142111 deposits : []* deposit.Deposit {d1 , d2 , d3 },
143112 targetValue : d1 .Value + d2 .Value + d3 .Value - dustLimit + 1 ,
144- csvExpiry : testCsvExpiry ,
145- blockHeight : testBlockHeight ,
146113 expected : []* deposit.Deposit {},
147114 expectedErr : "not enough deposits to cover" ,
148115 },
149116 {
150- // d3 and d4 have the same value but d3 has more
151- // confirmations (lower ConfirmationHeight), so it
152- // wins at the primary sort level.
153- name : "same value, prefer more confirmed" ,
117+ name : "tie by value, prefer earlier expiry" ,
154118 deposits : []* deposit.Deposit {d3 , d4 },
155- targetValue : d4 .Value - dustLimit ,
156- csvExpiry : testCsvExpiry ,
157- blockHeight : testBlockHeight ,
119+ targetValue : d4 .Value - dustLimit , // d3/d4 have the
120+ // same value but different expiration.
158121 expected : []* deposit.Deposit {d3 },
159122 expectedErr : "" ,
160123 },
0 commit comments