@@ -26,10 +26,13 @@ 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.
2930func TestSelectDeposits (t * testing.T ) {
31+ // Note: confirmations = blockHeight - ConfirmationHeight
32+ // Lower ConfirmationHeight means more confirmations at a given block.
3033 d1 , d2 , d3 , d4 := & deposit.Deposit {
3134 Value : 1_000_000 ,
32- ConfirmationHeight : 5_000 ,
35+ ConfirmationHeight : 5_000 , // most confs at height 5100
3336 }, & deposit.Deposit {
3437 Value : 2_000_000 ,
3538 ConfirmationHeight : 5_001 ,
@@ -38,7 +41,7 @@ func TestSelectDeposits(t *testing.T) {
3841 ConfirmationHeight : 5_002 ,
3942 }, & deposit.Deposit {
4043 Value : 3_000_000 ,
41- ConfirmationHeight : 5_003 ,
44+ ConfirmationHeight : 5_003 , // fewest confs at height 5100
4245 }
4346 d1 .Hash = chainhash.Hash {1 }
4447 d1 .Index = 0
@@ -49,75 +52,108 @@ func TestSelectDeposits(t *testing.T) {
4952 d4 .Hash = chainhash.Hash {4 }
5053 d4 .Index = 0
5154
55+ // Use a realistic block height and csv expiry for all standard
56+ // test cases. csvExpiry must be large enough that deposits remain
57+ // swappable at this block height.
58+ const (
59+ testBlockHeight uint32 = 5_100
60+ testCsvExpiry uint32 = 2_500
61+ )
62+
5263 testCases := []testCase {
5364 {
5465 name : "single deposit exact target" ,
5566 deposits : []* deposit.Deposit {d1 },
5667 targetValue : 1_000_000 ,
68+ csvExpiry : testCsvExpiry ,
69+ blockHeight : testBlockHeight ,
5770 expected : []* deposit.Deposit {d1 },
5871 expectedErr : "" ,
5972 },
6073 {
61- name : "prefer larger deposit when both cover" ,
74+ // d1 has more confirmations, so it's preferred even
75+ // though d2 is larger.
76+ name : "prefer more confirmed deposit over larger" ,
6277 deposits : []* deposit.Deposit {d1 , d2 },
6378 targetValue : 1_000_000 ,
64- expected : []* deposit.Deposit {d2 },
79+ csvExpiry : testCsvExpiry ,
80+ blockHeight : testBlockHeight ,
81+ expected : []* deposit.Deposit {d1 },
6582 expectedErr : "" ,
6683 },
6784 {
68- name : "prefer largest among three when one is enough" ,
85+ // d1 has the most confirmations among d1, d2, d3.
86+ name : "prefer most confirmed among three" ,
6987 deposits : []* deposit.Deposit {d1 , d2 , d3 },
7088 targetValue : 1_000_000 ,
71- expected : []* deposit.Deposit {d3 },
89+ csvExpiry : testCsvExpiry ,
90+ blockHeight : testBlockHeight ,
91+ expected : []* deposit.Deposit {d1 },
7292 expectedErr : "" ,
7393 },
7494 {
7595 name : "single deposit insufficient by 1" ,
7696 deposits : []* deposit.Deposit {d1 },
7797 targetValue : 1_000_001 ,
98+ csvExpiry : testCsvExpiry ,
99+ blockHeight : testBlockHeight ,
78100 expected : []* deposit.Deposit {},
79101 expectedErr : "not enough deposits to cover" ,
80102 },
81103 {
82104 name : "target leaves exact dust limit change" ,
83105 deposits : []* deposit.Deposit {d1 },
84106 targetValue : 1_000_000 - dustLimit ,
107+ csvExpiry : testCsvExpiry ,
108+ blockHeight : testBlockHeight ,
85109 expected : []* deposit.Deposit {d1 },
86110 expectedErr : "" ,
87111 },
88112 {
89113 name : "target leaves dust change (just over)" ,
90114 deposits : []* deposit.Deposit {d1 },
91115 targetValue : 1_000_000 - dustLimit + 1 ,
116+ csvExpiry : testCsvExpiry ,
117+ blockHeight : testBlockHeight ,
92118 expected : []* deposit.Deposit {},
93119 expectedErr : "not enough deposits to cover" ,
94120 },
95121 {
96122 name : "all deposits exactly match target" ,
97123 deposits : []* deposit.Deposit {d1 , d2 , d3 },
98124 targetValue : d1 .Value + d2 .Value + d3 .Value ,
125+ csvExpiry : testCsvExpiry ,
126+ blockHeight : testBlockHeight ,
99127 expected : []* deposit.Deposit {d1 , d2 , d3 },
100128 expectedErr : "" ,
101129 },
102130 {
103131 name : "sum minus dust limit is allowed (change == dust)" ,
104132 deposits : []* deposit.Deposit {d1 , d2 , d3 },
105133 targetValue : d1 .Value + d2 .Value + d3 .Value - dustLimit ,
134+ csvExpiry : testCsvExpiry ,
135+ blockHeight : testBlockHeight ,
106136 expected : []* deposit.Deposit {d1 , d2 , d3 },
107137 expectedErr : "" ,
108138 },
109139 {
110140 name : "sum minus dust limit plus 1 is not allowed (dust change)" ,
111141 deposits : []* deposit.Deposit {d1 , d2 , d3 },
112142 targetValue : d1 .Value + d2 .Value + d3 .Value - dustLimit + 1 ,
143+ csvExpiry : testCsvExpiry ,
144+ blockHeight : testBlockHeight ,
113145 expected : []* deposit.Deposit {},
114146 expectedErr : "not enough deposits to cover" ,
115147 },
116148 {
117- name : "tie by value, prefer earlier expiry" ,
149+ // d3 and d4 have the same value but d3 has more
150+ // confirmations (lower ConfirmationHeight), so it
151+ // wins at the primary sort level.
152+ name : "same value, prefer more confirmed" ,
118153 deposits : []* deposit.Deposit {d3 , d4 },
119- targetValue : d4 .Value - dustLimit , // d3/d4 have the
120- // same value but different expiration.
154+ targetValue : d4 .Value - dustLimit ,
155+ csvExpiry : testCsvExpiry ,
156+ blockHeight : testBlockHeight ,
121157 expected : []* deposit.Deposit {d3 },
122158 expectedErr : "" ,
123159 },
0 commit comments