Skip to content

Commit 7d77eb1

Browse files
committed
Use a struct for output eligibility
Instead of specifying 3 parameters, use a struct for those parameters in order to reduce the number of arguments to SelectCoinsMinConf.
1 parent ce7435c commit 7d77eb1

File tree

4 files changed

+65
-52
lines changed

4 files changed

+65
-52
lines changed

src/bench/coin_selection.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ static void CoinSelection(benchmark::State& state)
4444

4545
std::set<CInputCoin> setCoinsRet;
4646
CAmount nValueRet;
47-
bool success = wallet.SelectCoinsMinConf(1003 * COIN, 1, 6, 0, vCoins, setCoinsRet, nValueRet);
47+
CoinEligibilityFilter filter_standard(1, 6, 0);
48+
bool success = wallet.SelectCoinsMinConf(1003 * COIN, filter_standard, vCoins, setCoinsRet, nValueRet);
4849
assert(success);
4950
assert(nValueRet == 1003 * COIN);
5051
assert(setCoinsRet.size() == 2);

src/wallet/test/wallet_tests.cpp

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ static bool equal_sets(CoinSet a, CoinSet b)
7474
return ret.first == a.end() && ret.second == b.end();
7575
}
7676

77+
CoinEligibilityFilter filter_standard(1, 6, 0);
78+
CoinEligibilityFilter filter_confirmed(1, 1, 0);
79+
CoinEligibilityFilter filter_standard_extra(6, 6, 0);
80+
7781
BOOST_AUTO_TEST_CASE(coin_selection_tests)
7882
{
7983
CoinSet setCoinsRet, setCoinsRet2;
@@ -87,24 +91,24 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
8791
empty_wallet();
8892

8993
// with an empty wallet we can't even pay one cent
90-
BOOST_CHECK(!testWallet.SelectCoinsMinConf( 1 * CENT, 1, 6, 0, vCoins, setCoinsRet, nValueRet));
94+
BOOST_CHECK(!testWallet.SelectCoinsMinConf( 1 * CENT, filter_standard, vCoins, setCoinsRet, nValueRet));
9195

9296
add_coin(1*CENT, 4); // add a new 1 cent coin
9397

9498
// with a new 1 cent coin, we still can't find a mature 1 cent
95-
BOOST_CHECK(!testWallet.SelectCoinsMinConf( 1 * CENT, 1, 6, 0, vCoins, setCoinsRet, nValueRet));
99+
BOOST_CHECK(!testWallet.SelectCoinsMinConf( 1 * CENT, filter_standard, vCoins, setCoinsRet, nValueRet));
96100

97101
// but we can find a new 1 cent
98-
BOOST_CHECK( testWallet.SelectCoinsMinConf( 1 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
102+
BOOST_CHECK( testWallet.SelectCoinsMinConf( 1 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
99103
BOOST_CHECK_EQUAL(nValueRet, 1 * CENT);
100104

101105
add_coin(2*CENT); // add a mature 2 cent coin
102106

103107
// we can't make 3 cents of mature coins
104-
BOOST_CHECK(!testWallet.SelectCoinsMinConf( 3 * CENT, 1, 6, 0, vCoins, setCoinsRet, nValueRet));
108+
BOOST_CHECK(!testWallet.SelectCoinsMinConf( 3 * CENT, filter_standard, vCoins, setCoinsRet, nValueRet));
105109

106110
// we can make 3 cents of new coins
107-
BOOST_CHECK( testWallet.SelectCoinsMinConf( 3 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
111+
BOOST_CHECK( testWallet.SelectCoinsMinConf( 3 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
108112
BOOST_CHECK_EQUAL(nValueRet, 3 * CENT);
109113

110114
add_coin(5*CENT); // add a mature 5 cent coin,
@@ -114,33 +118,33 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
114118
// now we have new: 1+10=11 (of which 10 was self-sent), and mature: 2+5+20=27. total = 38
115119

116120
// we can't make 38 cents only if we disallow new coins:
117-
BOOST_CHECK(!testWallet.SelectCoinsMinConf(38 * CENT, 1, 6, 0, vCoins, setCoinsRet, nValueRet));
121+
BOOST_CHECK(!testWallet.SelectCoinsMinConf(38 * CENT, filter_standard, vCoins, setCoinsRet, nValueRet));
118122
// we can't even make 37 cents if we don't allow new coins even if they're from us
119-
BOOST_CHECK(!testWallet.SelectCoinsMinConf(38 * CENT, 6, 6, 0, vCoins, setCoinsRet, nValueRet));
123+
BOOST_CHECK(!testWallet.SelectCoinsMinConf(38 * CENT, filter_standard_extra, vCoins, setCoinsRet, nValueRet));
120124
// but we can make 37 cents if we accept new coins from ourself
121-
BOOST_CHECK( testWallet.SelectCoinsMinConf(37 * CENT, 1, 6, 0, vCoins, setCoinsRet, nValueRet));
125+
BOOST_CHECK( testWallet.SelectCoinsMinConf(37 * CENT, filter_standard, vCoins, setCoinsRet, nValueRet));
122126
BOOST_CHECK_EQUAL(nValueRet, 37 * CENT);
123127
// and we can make 38 cents if we accept all new coins
124-
BOOST_CHECK( testWallet.SelectCoinsMinConf(38 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
128+
BOOST_CHECK( testWallet.SelectCoinsMinConf(38 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
125129
BOOST_CHECK_EQUAL(nValueRet, 38 * CENT);
126130

127131
// try making 34 cents from 1,2,5,10,20 - we can't do it exactly
128-
BOOST_CHECK( testWallet.SelectCoinsMinConf(34 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
132+
BOOST_CHECK( testWallet.SelectCoinsMinConf(34 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
129133
BOOST_CHECK_EQUAL(nValueRet, 35 * CENT); // but 35 cents is closest
130134
BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U); // the best should be 20+10+5. it's incredibly unlikely the 1 or 2 got included (but possible)
131135

132136
// when we try making 7 cents, the smaller coins (1,2,5) are enough. We should see just 2+5
133-
BOOST_CHECK( testWallet.SelectCoinsMinConf( 7 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
137+
BOOST_CHECK( testWallet.SelectCoinsMinConf( 7 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
134138
BOOST_CHECK_EQUAL(nValueRet, 7 * CENT);
135139
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
136140

137141
// when we try making 8 cents, the smaller coins (1,2,5) are exactly enough.
138-
BOOST_CHECK( testWallet.SelectCoinsMinConf( 8 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
142+
BOOST_CHECK( testWallet.SelectCoinsMinConf( 8 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
139143
BOOST_CHECK(nValueRet == 8 * CENT);
140144
BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
141145

142146
// when we try making 9 cents, no subset of smaller coins is enough, and we get the next bigger coin (10)
143-
BOOST_CHECK( testWallet.SelectCoinsMinConf( 9 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
147+
BOOST_CHECK( testWallet.SelectCoinsMinConf( 9 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
144148
BOOST_CHECK_EQUAL(nValueRet, 10 * CENT);
145149
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
146150

@@ -154,30 +158,30 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
154158
add_coin(30*CENT); // now we have 6+7+8+20+30 = 71 cents total
155159

156160
// check that we have 71 and not 72
157-
BOOST_CHECK( testWallet.SelectCoinsMinConf(71 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
158-
BOOST_CHECK(!testWallet.SelectCoinsMinConf(72 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
161+
BOOST_CHECK( testWallet.SelectCoinsMinConf(71 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
162+
BOOST_CHECK(!testWallet.SelectCoinsMinConf(72 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
159163

160164
// now try making 16 cents. the best smaller coins can do is 6+7+8 = 21; not as good at the next biggest coin, 20
161-
BOOST_CHECK( testWallet.SelectCoinsMinConf(16 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
165+
BOOST_CHECK( testWallet.SelectCoinsMinConf(16 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
162166
BOOST_CHECK_EQUAL(nValueRet, 20 * CENT); // we should get 20 in one coin
163167
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
164168

165169
add_coin( 5*CENT); // now we have 5+6+7+8+20+30 = 75 cents total
166170

167171
// now if we try making 16 cents again, the smaller coins can make 5+6+7 = 18 cents, better than the next biggest coin, 20
168-
BOOST_CHECK( testWallet.SelectCoinsMinConf(16 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
172+
BOOST_CHECK( testWallet.SelectCoinsMinConf(16 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
169173
BOOST_CHECK_EQUAL(nValueRet, 18 * CENT); // we should get 18 in 3 coins
170174
BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
171175

172176
add_coin( 18*CENT); // now we have 5+6+7+8+18+20+30
173177

174178
// and now if we try making 16 cents again, the smaller coins can make 5+6+7 = 18 cents, the same as the next biggest coin, 18
175-
BOOST_CHECK( testWallet.SelectCoinsMinConf(16 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
179+
BOOST_CHECK( testWallet.SelectCoinsMinConf(16 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
176180
BOOST_CHECK_EQUAL(nValueRet, 18 * CENT); // we should get 18 in 1 coin
177181
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U); // because in the event of a tie, the biggest coin wins
178182

179183
// now try making 11 cents. we should get 5+6
180-
BOOST_CHECK( testWallet.SelectCoinsMinConf(11 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
184+
BOOST_CHECK( testWallet.SelectCoinsMinConf(11 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
181185
BOOST_CHECK_EQUAL(nValueRet, 11 * CENT);
182186
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
183187

@@ -186,11 +190,11 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
186190
add_coin( 2*COIN);
187191
add_coin( 3*COIN);
188192
add_coin( 4*COIN); // now we have 5+6+7+8+18+20+30+100+200+300+400 = 1094 cents
189-
BOOST_CHECK( testWallet.SelectCoinsMinConf(95 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
193+
BOOST_CHECK( testWallet.SelectCoinsMinConf(95 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
190194
BOOST_CHECK_EQUAL(nValueRet, 1 * COIN); // we should get 1 BTC in 1 coin
191195
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
192196

193-
BOOST_CHECK( testWallet.SelectCoinsMinConf(195 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
197+
BOOST_CHECK( testWallet.SelectCoinsMinConf(195 * CENT, filter_confirmed, vCoins, setCoinsRet, nValueRet));
194198
BOOST_CHECK_EQUAL(nValueRet, 2 * COIN); // we should get 2 BTC in 1 coin
195199
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
196200

@@ -205,22 +209,22 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
205209

206210
// try making 1 * MIN_CHANGE from the 1.5 * MIN_CHANGE
207211
// we'll get change smaller than MIN_CHANGE whatever happens, so can expect MIN_CHANGE exactly
208-
BOOST_CHECK( testWallet.SelectCoinsMinConf(MIN_CHANGE, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
212+
BOOST_CHECK( testWallet.SelectCoinsMinConf(MIN_CHANGE, filter_confirmed, vCoins, setCoinsRet, nValueRet));
209213
BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE);
210214

211215
// but if we add a bigger coin, small change is avoided
212216
add_coin(1111*MIN_CHANGE);
213217

214218
// try making 1 from 0.1 + 0.2 + 0.3 + 0.4 + 0.5 + 1111 = 1112.5
215-
BOOST_CHECK( testWallet.SelectCoinsMinConf(1 * MIN_CHANGE, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
219+
BOOST_CHECK( testWallet.SelectCoinsMinConf(1 * MIN_CHANGE, filter_confirmed, vCoins, setCoinsRet, nValueRet));
216220
BOOST_CHECK_EQUAL(nValueRet, 1 * MIN_CHANGE); // we should get the exact amount
217221

218222
// if we add more small coins:
219223
add_coin(MIN_CHANGE * 6 / 10);
220224
add_coin(MIN_CHANGE * 7 / 10);
221225

222226
// and try again to make 1.0 * MIN_CHANGE
223-
BOOST_CHECK( testWallet.SelectCoinsMinConf(1 * MIN_CHANGE, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
227+
BOOST_CHECK( testWallet.SelectCoinsMinConf(1 * MIN_CHANGE, filter_confirmed, vCoins, setCoinsRet, nValueRet));
224228
BOOST_CHECK_EQUAL(nValueRet, 1 * MIN_CHANGE); // we should get the exact amount
225229

226230
// run the 'mtgox' test (see http://blockexplorer.com/tx/29a3efd3ef04f9153d47a990bd7b048a4b2d213daaa5fb8ed670fb85f13bdbcf)
@@ -229,7 +233,7 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
229233
for (int j = 0; j < 20; j++)
230234
add_coin(50000 * COIN);
231235

232-
BOOST_CHECK( testWallet.SelectCoinsMinConf(500000 * COIN, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
236+
BOOST_CHECK( testWallet.SelectCoinsMinConf(500000 * COIN, filter_confirmed, vCoins, setCoinsRet, nValueRet));
233237
BOOST_CHECK_EQUAL(nValueRet, 500000 * COIN); // we should get the exact amount
234238
BOOST_CHECK_EQUAL(setCoinsRet.size(), 10U); // in ten coins
235239

@@ -242,7 +246,7 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
242246
add_coin(MIN_CHANGE * 6 / 10);
243247
add_coin(MIN_CHANGE * 7 / 10);
244248
add_coin(1111 * MIN_CHANGE);
245-
BOOST_CHECK( testWallet.SelectCoinsMinConf(1 * MIN_CHANGE, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
249+
BOOST_CHECK( testWallet.SelectCoinsMinConf(1 * MIN_CHANGE, filter_confirmed, vCoins, setCoinsRet, nValueRet));
246250
BOOST_CHECK_EQUAL(nValueRet, 1111 * MIN_CHANGE); // we get the bigger coin
247251
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
248252

@@ -252,7 +256,7 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
252256
add_coin(MIN_CHANGE * 6 / 10);
253257
add_coin(MIN_CHANGE * 8 / 10);
254258
add_coin(1111 * MIN_CHANGE);
255-
BOOST_CHECK( testWallet.SelectCoinsMinConf(MIN_CHANGE, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
259+
BOOST_CHECK( testWallet.SelectCoinsMinConf(MIN_CHANGE, filter_confirmed, vCoins, setCoinsRet, nValueRet));
256260
BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE); // we should get the exact amount
257261
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U); // in two coins 0.4+0.6
258262

@@ -263,12 +267,12 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
263267
add_coin(MIN_CHANGE * 100);
264268

265269
// trying to make 100.01 from these three coins
266-
BOOST_CHECK(testWallet.SelectCoinsMinConf(MIN_CHANGE * 10001 / 100, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
270+
BOOST_CHECK(testWallet.SelectCoinsMinConf(MIN_CHANGE * 10001 / 100, filter_confirmed, vCoins, setCoinsRet, nValueRet));
267271
BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE * 10105 / 100); // we should get all coins
268272
BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
269273

270274
// but if we try to make 99.9, we should take the bigger of the two small coins to avoid small change
271-
BOOST_CHECK(testWallet.SelectCoinsMinConf(MIN_CHANGE * 9990 / 100, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
275+
BOOST_CHECK(testWallet.SelectCoinsMinConf(MIN_CHANGE * 9990 / 100, filter_confirmed, vCoins, setCoinsRet, nValueRet));
272276
BOOST_CHECK_EQUAL(nValueRet, 101 * MIN_CHANGE);
273277
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
274278

@@ -278,7 +282,7 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
278282
// Create 676 inputs (= (old MAX_STANDARD_TX_SIZE == 100000) / 148 bytes per input)
279283
for (uint16_t j = 0; j < 676; j++)
280284
add_coin(amt);
281-
BOOST_CHECK(testWallet.SelectCoinsMinConf(2000, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
285+
BOOST_CHECK(testWallet.SelectCoinsMinConf(2000, filter_confirmed, vCoins, setCoinsRet, nValueRet));
282286
if (amt - 2000 < MIN_CHANGE) {
283287
// needs more than one input:
284288
uint16_t returnSize = std::ceil((2000.0 + MIN_CHANGE)/amt);
@@ -300,17 +304,17 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
300304

301305
// picking 50 from 100 coins doesn't depend on the shuffle,
302306
// but does depend on randomness in the stochastic approximation code
303-
BOOST_CHECK(testWallet.SelectCoinsMinConf(50 * COIN, 1, 6, 0, vCoins, setCoinsRet , nValueRet));
304-
BOOST_CHECK(testWallet.SelectCoinsMinConf(50 * COIN, 1, 6, 0, vCoins, setCoinsRet2, nValueRet));
307+
BOOST_CHECK(testWallet.SelectCoinsMinConf(50 * COIN, filter_standard, vCoins, setCoinsRet , nValueRet));
308+
BOOST_CHECK(testWallet.SelectCoinsMinConf(50 * COIN, filter_standard, vCoins, setCoinsRet2, nValueRet));
305309
BOOST_CHECK(!equal_sets(setCoinsRet, setCoinsRet2));
306310

307311
int fails = 0;
308312
for (int j = 0; j < RANDOM_REPEATS; j++)
309313
{
310314
// selecting 1 from 100 identical coins depends on the shuffle; this test will fail 1% of the time
311315
// run the test RANDOM_REPEATS times and only complain if all of them fail
312-
BOOST_CHECK(testWallet.SelectCoinsMinConf(COIN, 1, 6, 0, vCoins, setCoinsRet , nValueRet));
313-
BOOST_CHECK(testWallet.SelectCoinsMinConf(COIN, 1, 6, 0, vCoins, setCoinsRet2, nValueRet));
316+
BOOST_CHECK(testWallet.SelectCoinsMinConf(COIN, filter_standard, vCoins, setCoinsRet , nValueRet));
317+
BOOST_CHECK(testWallet.SelectCoinsMinConf(COIN, filter_standard, vCoins, setCoinsRet2, nValueRet));
314318
if (equal_sets(setCoinsRet, setCoinsRet2))
315319
fails++;
316320
}
@@ -330,8 +334,8 @@ BOOST_AUTO_TEST_CASE(coin_selection_tests)
330334
{
331335
// selecting 1 from 100 identical coins depends on the shuffle; this test will fail 1% of the time
332336
// run the test RANDOM_REPEATS times and only complain if all of them fail
333-
BOOST_CHECK(testWallet.SelectCoinsMinConf(90*CENT, 1, 6, 0, vCoins, setCoinsRet , nValueRet));
334-
BOOST_CHECK(testWallet.SelectCoinsMinConf(90*CENT, 1, 6, 0, vCoins, setCoinsRet2, nValueRet));
337+
BOOST_CHECK(testWallet.SelectCoinsMinConf(90*CENT, filter_standard, vCoins, setCoinsRet , nValueRet));
338+
BOOST_CHECK(testWallet.SelectCoinsMinConf(90*CENT, filter_standard, vCoins, setCoinsRet2, nValueRet));
335339
if (equal_sets(setCoinsRet, setCoinsRet2))
336340
fails++;
337341
}
@@ -355,7 +359,7 @@ BOOST_AUTO_TEST_CASE(ApproximateBestSubset)
355359
add_coin(1000 * COIN);
356360
add_coin(3 * COIN);
357361

358-
BOOST_CHECK(testWallet.SelectCoinsMinConf(1003 * COIN, 1, 6, 0, vCoins, setCoinsRet, nValueRet));
362+
BOOST_CHECK(testWallet.SelectCoinsMinConf(1003 * COIN, filter_standard, vCoins, setCoinsRet, nValueRet));
359363
BOOST_CHECK_EQUAL(nValueRet, 1003 * COIN);
360364
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
361365

0 commit comments

Comments
 (0)