BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
coinselector_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2018 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <wallet/wallet.h>
6 #include <wallet/coinselection.h>
7 #include <wallet/coincontrol.h>
8 #include <amount.h>
10 #include <random.h>
11 #include <test/test_bitcoin.h>
13 
14 #include <boost/test/unit_test.hpp>
15 #include <random>
16 
18 
19 // how many times to run all the tests to have a chance to catch errors that only show up with particular random shuffles
20 #define RUN_TESTS 100
21 
22 // some tests fail 1% of the time due to bad luck.
23 // we repeat those tests this many times and only complain if all iterations of the test fail
24 #define RANDOM_REPEATS 5
25 
26 std::vector<std::unique_ptr<CWalletTx>> wtxn;
27 
28 typedef std::set<CInputCoin> CoinSet;
29 
30 static std::vector<COutput> vCoins;
31 static CWallet testWallet("dummy", WalletDatabase::CreateDummy());
32 static CAmount balance = 0;
33 
38 
39 static void add_coin(const CAmount& nValue, int nInput, std::vector<CInputCoin>& set)
40 {
42  tx.vout.resize(nInput + 1);
43  tx.vout[nInput].nValue = nValue;
44  set.emplace_back(MakeTransactionRef(tx), nInput);
45 }
46 
47 static void add_coin(const CAmount& nValue, int nInput, CoinSet& set)
48 {
50  tx.vout.resize(nInput + 1);
51  tx.vout[nInput].nValue = nValue;
52  set.emplace(MakeTransactionRef(tx), nInput);
53 }
54 
55 static void add_coin(const CAmount& nValue, int nAge = 6*24, bool fIsFromMe = false, int nInput=0)
56 {
57  balance += nValue;
58  static int nextLockTime = 0;
60  tx.nLockTime = nextLockTime++; // so all transactions get different hashes
61  tx.vout.resize(nInput + 1);
62  tx.vout[nInput].nValue = nValue;
63  if (fIsFromMe) {
64  // IsFromMe() returns (GetDebit() > 0), and GetDebit() is 0 if vin.empty(),
65  // so stop vin being empty, and cache a non-zero Debit to fake out IsFromMe()
66  tx.vin.resize(1);
67  }
68  std::unique_ptr<CWalletTx> wtx = MakeUnique<CWalletTx>(&testWallet, MakeTransactionRef(std::move(tx)));
69  if (fIsFromMe)
70  {
71  wtx->fDebitCached = true;
72  wtx->nDebitCached = 1;
73  }
74  COutput output(wtx.get(), nInput, nAge, true /* spendable */, true /* solvable */, true /* safe */);
75  vCoins.push_back(output);
76  testWallet.AddToWallet(*wtx.get());
77  wtxn.emplace_back(std::move(wtx));
78 }
79 
80 static void empty_wallet(void)
81 {
82  vCoins.clear();
83  wtxn.clear();
84  balance = 0;
85 }
86 
87 static bool equal_sets(CoinSet a, CoinSet b)
88 {
89  std::pair<CoinSet::iterator, CoinSet::iterator> ret = mismatch(a.begin(), a.end(), b.begin());
90  return ret.first == a.end() && ret.second == b.end();
91 }
92 
93 static CAmount make_hard_case(int utxos, std::vector<CInputCoin>& utxo_pool)
94 {
95  utxo_pool.clear();
96  CAmount target = 0;
97  for (int i = 0; i < utxos; ++i) {
98  target += (CAmount)1 << (utxos+i);
99  add_coin((CAmount)1 << (utxos+i), 2*i, utxo_pool);
100  add_coin(((CAmount)1 << (utxos+i)) + ((CAmount)1 << (utxos-1-i)), 2*i + 1, utxo_pool);
101  }
102  return target;
103 }
104 
105 inline std::vector<OutputGroup>& GroupCoins(const std::vector<CInputCoin>& coins)
106 {
107  static std::vector<OutputGroup> static_groups;
108  static_groups.clear();
109  for (auto& coin : coins) static_groups.emplace_back(coin, 0, true, 0, 0);
110  return static_groups;
111 }
112 
113 inline std::vector<OutputGroup>& GroupCoins(const std::vector<COutput>& coins)
114 {
115  static std::vector<OutputGroup> static_groups;
116  static_groups.clear();
117  for (auto& coin : coins) static_groups.emplace_back(coin.GetInputCoin(), coin.nDepth, coin.tx->fDebitCached && coin.tx->nDebitCached == 1 /* HACK: we can't figure out the is_me flag so we use the conditions defined above; perhaps set safe to false for !fIsFromMe in add_coin() */, 0, 0);
118  return static_groups;
119 }
120 
121 // Branch and bound coin selection tests
122 BOOST_AUTO_TEST_CASE(bnb_search_test)
123 {
124 
125  LOCK(testWallet.cs_wallet);
126 
127  // Setup
128  std::vector<CInputCoin> utxo_pool;
129  CoinSet selection;
130  CoinSet actual_selection;
131  CAmount value_ret = 0;
132  CAmount not_input_fees = 0;
133 
135  // Known Outcome tests //
137  BOOST_TEST_MESSAGE("Testing known outcomes");
138 
139  // Empty utxo pool
140  BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 1 * CENT, 0.5 * CENT, selection, value_ret, not_input_fees));
141  selection.clear();
142 
143  // Add utxos
144  add_coin(1 * CENT, 1, utxo_pool);
145  add_coin(2 * CENT, 2, utxo_pool);
146  add_coin(3 * CENT, 3, utxo_pool);
147  add_coin(4 * CENT, 4, utxo_pool);
148 
149  // Select 1 Cent
150  add_coin(1 * CENT, 1, actual_selection);
151  BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 1 * CENT, 0.5 * CENT, selection, value_ret, not_input_fees));
152  BOOST_CHECK(equal_sets(selection, actual_selection));
153  BOOST_CHECK_EQUAL(value_ret, 1 * CENT);
154  actual_selection.clear();
155  selection.clear();
156 
157  // Select 2 Cent
158  add_coin(2 * CENT, 2, actual_selection);
159  BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 2 * CENT, 0.5 * CENT, selection, value_ret, not_input_fees));
160  BOOST_CHECK(equal_sets(selection, actual_selection));
161  BOOST_CHECK_EQUAL(value_ret, 2 * CENT);
162  actual_selection.clear();
163  selection.clear();
164 
165  // Select 5 Cent
166  add_coin(3 * CENT, 3, actual_selection);
167  add_coin(2 * CENT, 2, actual_selection);
168  BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 5 * CENT, 0.5 * CENT, selection, value_ret, not_input_fees));
169  BOOST_CHECK(equal_sets(selection, actual_selection));
170  BOOST_CHECK_EQUAL(value_ret, 5 * CENT);
171  actual_selection.clear();
172  selection.clear();
173 
174  // Select 11 Cent, not possible
175  BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 11 * CENT, 0.5 * CENT, selection, value_ret, not_input_fees));
176  actual_selection.clear();
177  selection.clear();
178 
179  // Select 10 Cent
180  add_coin(5 * CENT, 5, utxo_pool);
181  add_coin(4 * CENT, 4, actual_selection);
182  add_coin(3 * CENT, 3, actual_selection);
183  add_coin(2 * CENT, 2, actual_selection);
184  add_coin(1 * CENT, 1, actual_selection);
185  BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 10 * CENT, 0.5 * CENT, selection, value_ret, not_input_fees));
186  BOOST_CHECK(equal_sets(selection, actual_selection));
187  BOOST_CHECK_EQUAL(value_ret, 10 * CENT);
188  actual_selection.clear();
189  selection.clear();
190 
191  // Negative effective value
192  // Select 10 Cent but have 1 Cent not be possible because too small
193  add_coin(5 * CENT, 5, actual_selection);
194  add_coin(3 * CENT, 3, actual_selection);
195  add_coin(2 * CENT, 2, actual_selection);
196  BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 10 * CENT, 5000, selection, value_ret, not_input_fees));
197  BOOST_CHECK_EQUAL(value_ret, 10 * CENT);
198  // FIXME: this test is redundant with the above, because 1 Cent is selected, not "too small"
199  // BOOST_CHECK(equal_sets(selection, actual_selection));
200 
201  // Select 0.25 Cent, not possible
202  BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 0.25 * CENT, 0.5 * CENT, selection, value_ret, not_input_fees));
203  actual_selection.clear();
204  selection.clear();
205 
206  // Iteration exhaustion test
207  CAmount target = make_hard_case(17, utxo_pool);
208  BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), target, 0, selection, value_ret, not_input_fees)); // Should exhaust
209  target = make_hard_case(14, utxo_pool);
210  BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), target, 0, selection, value_ret, not_input_fees)); // Should not exhaust
211 
212  // Test same value early bailout optimization
213  utxo_pool.clear();
214  add_coin(7 * CENT, 7, actual_selection);
215  add_coin(7 * CENT, 7, actual_selection);
216  add_coin(7 * CENT, 7, actual_selection);
217  add_coin(7 * CENT, 7, actual_selection);
218  add_coin(2 * CENT, 7, actual_selection);
219  add_coin(7 * CENT, 7, utxo_pool);
220  add_coin(7 * CENT, 7, utxo_pool);
221  add_coin(7 * CENT, 7, utxo_pool);
222  add_coin(7 * CENT, 7, utxo_pool);
223  add_coin(2 * CENT, 7, utxo_pool);
224  for (int i = 0; i < 50000; ++i) {
225  add_coin(5 * CENT, 7, utxo_pool);
226  }
227  BOOST_CHECK(SelectCoinsBnB(GroupCoins(utxo_pool), 30 * CENT, 5000, selection, value_ret, not_input_fees));
228  BOOST_CHECK_EQUAL(value_ret, 30 * CENT);
229  BOOST_CHECK(equal_sets(selection, actual_selection));
230 
232  // Behavior tests //
234  // Select 1 Cent with pool of only greater than 5 Cent
235  utxo_pool.clear();
236  for (int i = 5; i <= 20; ++i) {
237  add_coin(i * CENT, i, utxo_pool);
238  }
239  // Run 100 times, to make sure it is never finding a solution
240  for (int i = 0; i < 100; ++i) {
241  BOOST_CHECK(!SelectCoinsBnB(GroupCoins(utxo_pool), 1 * CENT, 2 * CENT, selection, value_ret, not_input_fees));
242  }
243 
244  // Make sure that effective value is working in SelectCoinsMinConf when BnB is used
245  CoinSelectionParams coin_selection_params_bnb(true, 0, 0, CFeeRate(3000), 0);
246  CoinSet setCoinsRet;
247  CAmount nValueRet;
248  bool bnb_used;
249  empty_wallet();
250  add_coin(1);
251  vCoins.at(0).nInputBytes = 40; // Make sure that it has a negative effective value. The next check should assert if this somehow got through. Otherwise it will fail
252  BOOST_CHECK(!testWallet.SelectCoinsMinConf( 1 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params_bnb, bnb_used));
253 
254  // Make sure that we aren't using BnB when there are preset inputs
255  empty_wallet();
256  add_coin(5 * CENT);
257  add_coin(3 * CENT);
258  add_coin(2 * CENT);
259  CCoinControl coin_control;
260  coin_control.fAllowOtherInputs = true;
261  coin_control.Select(COutPoint(vCoins.at(0).tx->GetHash(), vCoins.at(0).i));
262  BOOST_CHECK(testWallet.SelectCoins(vCoins, 10 * CENT, setCoinsRet, nValueRet, coin_control, coin_selection_params_bnb, bnb_used));
263  BOOST_CHECK(!bnb_used);
264  BOOST_CHECK(!coin_selection_params_bnb.use_bnb);
265 }
266 
267 BOOST_AUTO_TEST_CASE(knapsack_solver_test)
268 {
269  CoinSet setCoinsRet, setCoinsRet2;
270  CAmount nValueRet;
271  bool bnb_used;
272 
273  LOCK(testWallet.cs_wallet);
274 
275  // test multiple times to allow for differences in the shuffle order
276  for (int i = 0; i < RUN_TESTS; i++)
277  {
278  empty_wallet();
279 
280  // with an empty wallet we can't even pay one cent
281  BOOST_CHECK(!testWallet.SelectCoinsMinConf( 1 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
282 
283  add_coin(1*CENT, 4); // add a new 1 cent coin
284 
285  // with a new 1 cent coin, we still can't find a mature 1 cent
286  BOOST_CHECK(!testWallet.SelectCoinsMinConf( 1 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
287 
288  // but we can find a new 1 cent
289  BOOST_CHECK( testWallet.SelectCoinsMinConf( 1 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
290  BOOST_CHECK_EQUAL(nValueRet, 1 * CENT);
291 
292  add_coin(2*CENT); // add a mature 2 cent coin
293 
294  // we can't make 3 cents of mature coins
295  BOOST_CHECK(!testWallet.SelectCoinsMinConf( 3 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
296 
297  // we can make 3 cents of new coins
298  BOOST_CHECK( testWallet.SelectCoinsMinConf( 3 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
299  BOOST_CHECK_EQUAL(nValueRet, 3 * CENT);
300 
301  add_coin(5*CENT); // add a mature 5 cent coin,
302  add_coin(10*CENT, 3, true); // a new 10 cent coin sent from one of our own addresses
303  add_coin(20*CENT); // and a mature 20 cent coin
304 
305  // now we have new: 1+10=11 (of which 10 was self-sent), and mature: 2+5+20=27. total = 38
306 
307  // we can't make 38 cents only if we disallow new coins:
308  BOOST_CHECK(!testWallet.SelectCoinsMinConf(38 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
309  // we can't even make 37 cents if we don't allow new coins even if they're from us
310  BOOST_CHECK(!testWallet.SelectCoinsMinConf(38 * CENT, filter_standard_extra, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
311  // but we can make 37 cents if we accept new coins from ourself
312  BOOST_CHECK( testWallet.SelectCoinsMinConf(37 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
313  BOOST_CHECK_EQUAL(nValueRet, 37 * CENT);
314  // and we can make 38 cents if we accept all new coins
315  BOOST_CHECK( testWallet.SelectCoinsMinConf(38 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
316  BOOST_CHECK_EQUAL(nValueRet, 38 * CENT);
317 
318  // try making 34 cents from 1,2,5,10,20 - we can't do it exactly
319  BOOST_CHECK( testWallet.SelectCoinsMinConf(34 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
320  BOOST_CHECK_EQUAL(nValueRet, 35 * CENT); // but 35 cents is closest
321  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)
322 
323  // when we try making 7 cents, the smaller coins (1,2,5) are enough. We should see just 2+5
324  BOOST_CHECK( testWallet.SelectCoinsMinConf( 7 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
325  BOOST_CHECK_EQUAL(nValueRet, 7 * CENT);
326  BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
327 
328  // when we try making 8 cents, the smaller coins (1,2,5) are exactly enough.
329  BOOST_CHECK( testWallet.SelectCoinsMinConf( 8 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
330  BOOST_CHECK(nValueRet == 8 * CENT);
331  BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
332 
333  // when we try making 9 cents, no subset of smaller coins is enough, and we get the next bigger coin (10)
334  BOOST_CHECK( testWallet.SelectCoinsMinConf( 9 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
335  BOOST_CHECK_EQUAL(nValueRet, 10 * CENT);
336  BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
337 
338  // now clear out the wallet and start again to test choosing between subsets of smaller coins and the next biggest coin
339  empty_wallet();
340 
341  add_coin( 6*CENT);
342  add_coin( 7*CENT);
343  add_coin( 8*CENT);
344  add_coin(20*CENT);
345  add_coin(30*CENT); // now we have 6+7+8+20+30 = 71 cents total
346 
347  // check that we have 71 and not 72
348  BOOST_CHECK( testWallet.SelectCoinsMinConf(71 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
349  BOOST_CHECK(!testWallet.SelectCoinsMinConf(72 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
350 
351  // 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
352  BOOST_CHECK( testWallet.SelectCoinsMinConf(16 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
353  BOOST_CHECK_EQUAL(nValueRet, 20 * CENT); // we should get 20 in one coin
354  BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
355 
356  add_coin( 5*CENT); // now we have 5+6+7+8+20+30 = 75 cents total
357 
358  // 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
359  BOOST_CHECK( testWallet.SelectCoinsMinConf(16 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
360  BOOST_CHECK_EQUAL(nValueRet, 18 * CENT); // we should get 18 in 3 coins
361  BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
362 
363  add_coin( 18*CENT); // now we have 5+6+7+8+18+20+30
364 
365  // 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
366  BOOST_CHECK( testWallet.SelectCoinsMinConf(16 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
367  BOOST_CHECK_EQUAL(nValueRet, 18 * CENT); // we should get 18 in 1 coin
368  BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U); // because in the event of a tie, the biggest coin wins
369 
370  // now try making 11 cents. we should get 5+6
371  BOOST_CHECK( testWallet.SelectCoinsMinConf(11 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
372  BOOST_CHECK_EQUAL(nValueRet, 11 * CENT);
373  BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
374 
375  // check that the smallest bigger coin is used
376  add_coin( 1*COIN);
377  add_coin( 2*COIN);
378  add_coin( 3*COIN);
379  add_coin( 4*COIN); // now we have 5+6+7+8+18+20+30+100+200+300+400 = 1094 cents
380  BOOST_CHECK( testWallet.SelectCoinsMinConf(95 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
381  BOOST_CHECK_EQUAL(nValueRet, 1 * COIN); // we should get 1 BTC in 1 coin
382  BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
383 
384  BOOST_CHECK( testWallet.SelectCoinsMinConf(195 * CENT, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
385  BOOST_CHECK_EQUAL(nValueRet, 2 * COIN); // we should get 2 BTC in 1 coin
386  BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
387 
388  // empty the wallet and start again, now with fractions of a cent, to test small change avoidance
389 
390  empty_wallet();
391  add_coin(MIN_CHANGE * 1 / 10);
392  add_coin(MIN_CHANGE * 2 / 10);
393  add_coin(MIN_CHANGE * 3 / 10);
394  add_coin(MIN_CHANGE * 4 / 10);
395  add_coin(MIN_CHANGE * 5 / 10);
396 
397  // try making 1 * MIN_CHANGE from the 1.5 * MIN_CHANGE
398  // we'll get change smaller than MIN_CHANGE whatever happens, so can expect MIN_CHANGE exactly
399  BOOST_CHECK( testWallet.SelectCoinsMinConf(MIN_CHANGE, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
400  BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE);
401 
402  // but if we add a bigger coin, small change is avoided
403  add_coin(1111*MIN_CHANGE);
404 
405  // try making 1 from 0.1 + 0.2 + 0.3 + 0.4 + 0.5 + 1111 = 1112.5
406  BOOST_CHECK( testWallet.SelectCoinsMinConf(1 * MIN_CHANGE, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
407  BOOST_CHECK_EQUAL(nValueRet, 1 * MIN_CHANGE); // we should get the exact amount
408 
409  // if we add more small coins:
410  add_coin(MIN_CHANGE * 6 / 10);
411  add_coin(MIN_CHANGE * 7 / 10);
412 
413  // and try again to make 1.0 * MIN_CHANGE
414  BOOST_CHECK( testWallet.SelectCoinsMinConf(1 * MIN_CHANGE, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
415  BOOST_CHECK_EQUAL(nValueRet, 1 * MIN_CHANGE); // we should get the exact amount
416 
417  // run the 'mtgox' test (see http://blockexplorer.com/tx/29a3efd3ef04f9153d47a990bd7b048a4b2d213daaa5fb8ed670fb85f13bdbcf)
418  // they tried to consolidate 10 50k coins into one 500k coin, and ended up with 50k in change
419  empty_wallet();
420  for (int j = 0; j < 20; j++)
421  add_coin(50000 * COIN);
422 
423  BOOST_CHECK( testWallet.SelectCoinsMinConf(500000 * COIN, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
424  BOOST_CHECK_EQUAL(nValueRet, 500000 * COIN); // we should get the exact amount
425  BOOST_CHECK_EQUAL(setCoinsRet.size(), 10U); // in ten coins
426 
427  // if there's not enough in the smaller coins to make at least 1 * MIN_CHANGE change (0.5+0.6+0.7 < 1.0+1.0),
428  // we need to try finding an exact subset anyway
429 
430  // sometimes it will fail, and so we use the next biggest coin:
431  empty_wallet();
432  add_coin(MIN_CHANGE * 5 / 10);
433  add_coin(MIN_CHANGE * 6 / 10);
434  add_coin(MIN_CHANGE * 7 / 10);
435  add_coin(1111 * MIN_CHANGE);
436  BOOST_CHECK( testWallet.SelectCoinsMinConf(1 * MIN_CHANGE, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
437  BOOST_CHECK_EQUAL(nValueRet, 1111 * MIN_CHANGE); // we get the bigger coin
438  BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
439 
440  // but sometimes it's possible, and we use an exact subset (0.4 + 0.6 = 1.0)
441  empty_wallet();
442  add_coin(MIN_CHANGE * 4 / 10);
443  add_coin(MIN_CHANGE * 6 / 10);
444  add_coin(MIN_CHANGE * 8 / 10);
445  add_coin(1111 * MIN_CHANGE);
446  BOOST_CHECK( testWallet.SelectCoinsMinConf(MIN_CHANGE, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
447  BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE); // we should get the exact amount
448  BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U); // in two coins 0.4+0.6
449 
450  // test avoiding small change
451  empty_wallet();
452  add_coin(MIN_CHANGE * 5 / 100);
453  add_coin(MIN_CHANGE * 1);
454  add_coin(MIN_CHANGE * 100);
455 
456  // trying to make 100.01 from these three coins
457  BOOST_CHECK(testWallet.SelectCoinsMinConf(MIN_CHANGE * 10001 / 100, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
458  BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE * 10105 / 100); // we should get all coins
459  BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
460 
461  // but if we try to make 99.9, we should take the bigger of the two small coins to avoid small change
462  BOOST_CHECK(testWallet.SelectCoinsMinConf(MIN_CHANGE * 9990 / 100, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
463  BOOST_CHECK_EQUAL(nValueRet, 101 * MIN_CHANGE);
464  BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
465  }
466 
467  // test with many inputs
468  for (CAmount amt=1500; amt < COIN; amt*=10) {
469  empty_wallet();
470  // Create 676 inputs (= (old MAX_STANDARD_TX_SIZE == 100000) / 148 bytes per input)
471  for (uint16_t j = 0; j < 676; j++)
472  add_coin(amt);
473 
474  // We only create the wallet once to save time, but we still run the coin selection RUN_TESTS times.
475  for (int i = 0; i < RUN_TESTS; i++) {
476  BOOST_CHECK(testWallet.SelectCoinsMinConf(2000, filter_confirmed, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
477 
478  if (amt - 2000 < MIN_CHANGE) {
479  // needs more than one input:
480  uint16_t returnSize = std::ceil((2000.0 + MIN_CHANGE)/amt);
481  CAmount returnValue = amt * returnSize;
482  BOOST_CHECK_EQUAL(nValueRet, returnValue);
483  BOOST_CHECK_EQUAL(setCoinsRet.size(), returnSize);
484  } else {
485  // one input is sufficient:
486  BOOST_CHECK_EQUAL(nValueRet, amt);
487  BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
488  }
489  }
490  }
491 
492  // test randomness
493  {
494  empty_wallet();
495  for (int i2 = 0; i2 < 100; i2++)
496  add_coin(COIN);
497 
498  // Again, we only create the wallet once to save time, but we still run the coin selection RUN_TESTS times.
499  for (int i = 0; i < RUN_TESTS; i++) {
500  // picking 50 from 100 coins doesn't depend on the shuffle,
501  // but does depend on randomness in the stochastic approximation code
502  BOOST_CHECK(testWallet.SelectCoinsMinConf(50 * COIN, filter_standard, GroupCoins(vCoins), setCoinsRet , nValueRet, coin_selection_params, bnb_used));
503  BOOST_CHECK(testWallet.SelectCoinsMinConf(50 * COIN, filter_standard, GroupCoins(vCoins), setCoinsRet2, nValueRet, coin_selection_params, bnb_used));
504  BOOST_CHECK(!equal_sets(setCoinsRet, setCoinsRet2));
505 
506  int fails = 0;
507  for (int j = 0; j < RANDOM_REPEATS; j++)
508  {
509  // selecting 1 from 100 identical coins depends on the shuffle; this test will fail 1% of the time
510  // run the test RANDOM_REPEATS times and only complain if all of them fail
511  BOOST_CHECK(testWallet.SelectCoinsMinConf(COIN, filter_standard, GroupCoins(vCoins), setCoinsRet , nValueRet, coin_selection_params, bnb_used));
512  BOOST_CHECK(testWallet.SelectCoinsMinConf(COIN, filter_standard, GroupCoins(vCoins), setCoinsRet2, nValueRet, coin_selection_params, bnb_used));
513  if (equal_sets(setCoinsRet, setCoinsRet2))
514  fails++;
515  }
516  BOOST_CHECK_NE(fails, RANDOM_REPEATS);
517  }
518 
519  // add 75 cents in small change. not enough to make 90 cents,
520  // then try making 90 cents. there are multiple competing "smallest bigger" coins,
521  // one of which should be picked at random
522  add_coin(5 * CENT);
523  add_coin(10 * CENT);
524  add_coin(15 * CENT);
525  add_coin(20 * CENT);
526  add_coin(25 * CENT);
527 
528  for (int i = 0; i < RUN_TESTS; i++) {
529  int fails = 0;
530  for (int j = 0; j < RANDOM_REPEATS; j++)
531  {
532  // selecting 1 from 100 identical coins depends on the shuffle; this test will fail 1% of the time
533  // run the test RANDOM_REPEATS times and only complain if all of them fail
534  BOOST_CHECK(testWallet.SelectCoinsMinConf(90*CENT, filter_standard, GroupCoins(vCoins), setCoinsRet , nValueRet, coin_selection_params, bnb_used));
535  BOOST_CHECK(testWallet.SelectCoinsMinConf(90*CENT, filter_standard, GroupCoins(vCoins), setCoinsRet2, nValueRet, coin_selection_params, bnb_used));
536  if (equal_sets(setCoinsRet, setCoinsRet2))
537  fails++;
538  }
539  BOOST_CHECK_NE(fails, RANDOM_REPEATS);
540  }
541  }
542 
543  empty_wallet();
544 }
545 
546 BOOST_AUTO_TEST_CASE(ApproximateBestSubset)
547 {
548  CoinSet setCoinsRet;
549  CAmount nValueRet;
550  bool bnb_used;
551 
552  LOCK(testWallet.cs_wallet);
553 
554  empty_wallet();
555 
556  // Test vValue sort order
557  for (int i = 0; i < 1000; i++)
558  add_coin(1000 * COIN);
559  add_coin(3 * COIN);
560 
561  BOOST_CHECK(testWallet.SelectCoinsMinConf(1003 * COIN, filter_standard, GroupCoins(vCoins), setCoinsRet, nValueRet, coin_selection_params, bnb_used));
562  BOOST_CHECK_EQUAL(nValueRet, 1003 * COIN);
563  BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
564 
565  empty_wallet();
566 }
567 
568 // Tests that with the ideal conditions, the coin selector will always be able to find a solution that can pay the target value
569 BOOST_AUTO_TEST_CASE(SelectCoins_test)
570 {
571  // Random generator stuff
572  std::default_random_engine generator;
573  std::exponential_distribution<double> distribution (100);
574  FastRandomContext rand;
575 
576  // Run this test 100 times
577  for (int i = 0; i < 100; ++i)
578  {
579  empty_wallet();
580 
581  // Make a wallet with 1000 exponentially distributed random inputs
582  for (int j = 0; j < 1000; ++j)
583  {
584  add_coin((CAmount)(distribution(generator)*10000000));
585  }
586 
587  // Generate a random fee rate in the range of 100 - 400
588  CFeeRate rate(rand.randrange(300) + 100);
589 
590  // Generate a random target value between 1000 and wallet balance
591  CAmount target = rand.randrange(balance - 1000) + 1000;
592 
593  // Perform selection
594  CoinSelectionParams coin_selection_params_knapsack(false, 34, 148, CFeeRate(0), 0);
595  CoinSelectionParams coin_selection_params_bnb(true, 34, 148, CFeeRate(0), 0);
596  CoinSet out_set;
597  CAmount out_value = 0;
598  bool bnb_used = false;
599  BOOST_CHECK(testWallet.SelectCoinsMinConf(target, filter_standard, GroupCoins(vCoins), out_set, out_value, coin_selection_params_bnb, bnb_used) ||
600  testWallet.SelectCoinsMinConf(target, filter_standard, GroupCoins(vCoins), out_set, out_value, coin_selection_params_knapsack, bnb_used));
601  BOOST_CHECK_GE(out_value, target);
602  }
603 }
604 
static std::unique_ptr< BerkeleyDatabase > CreateDummy()
Return object for accessing dummy database with no read/write capabilities.
Definition: db.h:132
std::set< CInputCoin > CoinSet
std::vector< CTxIn > vin
Definition: transaction.h:362
UniValue ret(UniValue::VARR)
Definition: rpcwallet.cpp:1140
bool SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &target_value, const CAmount &cost_of_change, std::set< CInputCoin > &out_set, CAmount &value_ret, CAmount not_input_fees)
bool SelectCoinsMinConf(const CAmount &nTargetValue, const CoinEligibilityFilter &eligibility_filter, std::vector< OutputGroup > groups, std::set< CInputCoin > &setCoinsRet, CAmount &nValueRet, const CoinSelectionParams &coin_selection_params, bool &bnb_used) const
Shuffle and select coins until nTargetValue is reached while avoiding small change; This method is st...
Definition: wallet.cpp:2315
uint64_t randrange(uint64_t range)
Generate a random integer in the range [0..range).
Definition: random.h:104
CoinEligibilityFilter filter_standard(1, 6, 0)
BOOST_AUTO_TEST_CASE(bnb_search_test)
#define RANDOM_REPEATS
Coin Control Features.
Definition: coincontrol.h:16
std::set< CInputCoin > CoinSet
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
bool SelectCoins(const std::vector< COutput > &vAvailableCoins, const CAmount &nTargetValue, std::set< CInputCoin > &setCoinsRet, CAmount &nValueRet, const CCoinControl &coin_control, CoinSelectionParams &coin_selection_params, bool &bnb_used) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Select a set of coins such that nValueRet >= nTargetValue and at least all coins from coinControl are...
Definition: wallet.cpp:2369
#define LOCK(cs)
Definition: sync.h:181
Fast randomness source.
Definition: random.h:45
void Select(const COutPoint &output)
Definition: coincontrol.h:57
bool fAllowOtherInputs
If false, allows unselected inputs, but requires all selected inputs be used.
Definition: coincontrol.h:24
#define RUN_TESTS
std::vector< OutputGroup > & GroupCoins(const std::vector< CInputCoin > &coins)
CoinEligibilityFilter filter_confirmed(1, 1, 0)
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
CCriticalSection cs_wallet
Definition: wallet.h:709
std::vector< CTxOut > vout
Definition: transaction.h:363
bool AddToWallet(const CWalletTx &wtxIn, bool fFlushOnClose=true)
Definition: wallet.cpp:843
CoinSelectionParams coin_selection_params(false, 0, 0, CFeeRate(0), 0)
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
Testing setup and teardown for wallet.
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
CoinEligibilityFilter filter_standard_extra(6, 6, 0)
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:599
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
A mutable version of CTransaction.
Definition: transaction.h:360
std::vector< std::unique_ptr< CWalletTx > > wtxn
#define BOOST_CHECK(expr)
Definition: object.cpp:17