@@ -26,10 +26,14 @@ 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.
2931func TestSelectDeposits (t * testing.T ) {
32+ // Note: confirmations = blockHeight - ConfirmationHeight
33+ // Lower ConfirmationHeight means more confirmations at a given block.
3034 d1 , d2 , d3 , d4 := & deposit.Deposit {
3135 Value : 1_000_000 ,
32- ConfirmationHeight : 5_000 ,
36+ ConfirmationHeight : 5_000 , // most confs at height 5100
3337 }, & deposit.Deposit {
3438 Value : 2_000_000 ,
3539 ConfirmationHeight : 5_001 ,
@@ -38,7 +42,7 @@ func TestSelectDeposits(t *testing.T) {
3842 ConfirmationHeight : 5_002 ,
3943 }, & deposit.Deposit {
4044 Value : 3_000_000 ,
41- ConfirmationHeight : 5_003 ,
45+ ConfirmationHeight : 5_003 , // fewest confs at height 5100
4246 }
4347 d1 .Hash = chainhash.Hash {1 }
4448 d1 .Index = 0
@@ -49,75 +53,108 @@ func TestSelectDeposits(t *testing.T) {
4953 d4 .Hash = chainhash.Hash {4 }
5054 d4 .Index = 0
5155
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+
5264 testCases := []testCase {
5365 {
5466 name : "single deposit exact target" ,
5567 deposits : []* deposit.Deposit {d1 },
5668 targetValue : 1_000_000 ,
69+ csvExpiry : testCsvExpiry ,
70+ blockHeight : testBlockHeight ,
5771 expected : []* deposit.Deposit {d1 },
5872 expectedErr : "" ,
5973 },
6074 {
61- name : "prefer larger deposit when both cover" ,
75+ // d1 has more confirmations, so it's preferred even
76+ // though d2 is larger.
77+ name : "prefer more confirmed deposit over larger" ,
6278 deposits : []* deposit.Deposit {d1 , d2 },
6379 targetValue : 1_000_000 ,
64- expected : []* deposit.Deposit {d2 },
80+ csvExpiry : testCsvExpiry ,
81+ blockHeight : testBlockHeight ,
82+ expected : []* deposit.Deposit {d1 },
6583 expectedErr : "" ,
6684 },
6785 {
68- name : "prefer largest among three when one is enough" ,
86+ // d1 has the most confirmations among d1, d2, d3.
87+ name : "prefer most confirmed among three" ,
6988 deposits : []* deposit.Deposit {d1 , d2 , d3 },
7089 targetValue : 1_000_000 ,
71- expected : []* deposit.Deposit {d3 },
90+ csvExpiry : testCsvExpiry ,
91+ blockHeight : testBlockHeight ,
92+ expected : []* deposit.Deposit {d1 },
7293 expectedErr : "" ,
7394 },
7495 {
7596 name : "single deposit insufficient by 1" ,
7697 deposits : []* deposit.Deposit {d1 },
7798 targetValue : 1_000_001 ,
99+ csvExpiry : testCsvExpiry ,
100+ blockHeight : testBlockHeight ,
78101 expected : []* deposit.Deposit {},
79102 expectedErr : "not enough deposits to cover" ,
80103 },
81104 {
82105 name : "target leaves exact dust limit change" ,
83106 deposits : []* deposit.Deposit {d1 },
84107 targetValue : 1_000_000 - dustLimit ,
108+ csvExpiry : testCsvExpiry ,
109+ blockHeight : testBlockHeight ,
85110 expected : []* deposit.Deposit {d1 },
86111 expectedErr : "" ,
87112 },
88113 {
89114 name : "target leaves dust change (just over)" ,
90115 deposits : []* deposit.Deposit {d1 },
91116 targetValue : 1_000_000 - dustLimit + 1 ,
117+ csvExpiry : testCsvExpiry ,
118+ blockHeight : testBlockHeight ,
92119 expected : []* deposit.Deposit {},
93120 expectedErr : "not enough deposits to cover" ,
94121 },
95122 {
96123 name : "all deposits exactly match target" ,
97124 deposits : []* deposit.Deposit {d1 , d2 , d3 },
98125 targetValue : d1 .Value + d2 .Value + d3 .Value ,
126+ csvExpiry : testCsvExpiry ,
127+ blockHeight : testBlockHeight ,
99128 expected : []* deposit.Deposit {d1 , d2 , d3 },
100129 expectedErr : "" ,
101130 },
102131 {
103132 name : "sum minus dust limit is allowed (change == dust)" ,
104133 deposits : []* deposit.Deposit {d1 , d2 , d3 },
105134 targetValue : d1 .Value + d2 .Value + d3 .Value - dustLimit ,
135+ csvExpiry : testCsvExpiry ,
136+ blockHeight : testBlockHeight ,
106137 expected : []* deposit.Deposit {d1 , d2 , d3 },
107138 expectedErr : "" ,
108139 },
109140 {
110141 name : "sum minus dust limit plus 1 is not allowed (dust change)" ,
111142 deposits : []* deposit.Deposit {d1 , d2 , d3 },
112143 targetValue : d1 .Value + d2 .Value + d3 .Value - dustLimit + 1 ,
144+ csvExpiry : testCsvExpiry ,
145+ blockHeight : testBlockHeight ,
113146 expected : []* deposit.Deposit {},
114147 expectedErr : "not enough deposits to cover" ,
115148 },
116149 {
117- name : "tie by value, prefer earlier expiry" ,
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" ,
118154 deposits : []* deposit.Deposit {d3 , d4 },
119- targetValue : d4 .Value - dustLimit , // d3/d4 have the
120- // same value but different expiration.
155+ targetValue : d4 .Value - dustLimit ,
156+ csvExpiry : testCsvExpiry ,
157+ blockHeight : testBlockHeight ,
121158 expected : []* deposit.Deposit {d3 },
122159 expectedErr : "" ,
123160 },
0 commit comments