wallet_tests.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // ECOin - Copyright (c) - 2014/2021 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include <boost/test/unit_test.hpp>
  3. #include "main.h"
  4. #include "wallet.h"
  5. // how many times to run all the tests to have a chance to catch errors that only show up with particular random shuffles
  6. #define RUN_TESTS 100
  7. // some tests fail 1% of the time due to bad luck.
  8. // we repeat those tests this many times and only complain if all iterations of the test fail
  9. #define RANDOM_REPEATS 5
  10. using namespace std;
  11. typedef set<pair<const CWalletTx*,unsigned int> > CoinSet;
  12. BOOST_AUTO_TEST_SUITE(wallet_tests)
  13. static CWallet wallet;
  14. static vector<COutput> vCoins;
  15. static void add_coin(int64 nValue, int nAge = 6*24, bool fIsFromMe = false, int nInput=0)
  16. {
  17. static int i;
  18. CTransaction* tx = new CTransaction;
  19. tx->nLockTime = i++; // so all transactions get different hashes
  20. tx->vout.resize(nInput+1);
  21. tx->vout[nInput].nValue = nValue;
  22. CWalletTx* wtx = new CWalletTx(&wallet, *tx);
  23. delete tx;
  24. if (fIsFromMe)
  25. {
  26. // IsFromMe() returns (GetDebit() > 0), and GetDebit() is 0 if vin.empty(),
  27. // so stop vin being empty, and cache a non-zero Debit to fake out IsFromMe()
  28. wtx->vin.resize(1);
  29. wtx->fDebitCached = true;
  30. wtx->nDebitCached = 1;
  31. }
  32. COutput output(wtx, nInput, nAge);
  33. vCoins.push_back(output);
  34. }
  35. static void empty_wallet(void)
  36. {
  37. BOOST_FOREACH(COutput output, vCoins)
  38. delete output.tx;
  39. vCoins.clear();
  40. }
  41. static bool equal_sets(CoinSet a, CoinSet b)
  42. {
  43. pair<CoinSet::iterator, CoinSet::iterator> ret = mismatch(a.begin(), a.end(), b.begin());
  44. return ret.first == a.end() && ret.second == b.end();
  45. }
  46. BOOST_AUTO_TEST_CASE(coin_selection_tests)
  47. {
  48. static CoinSet setCoinsRet, setCoinsRet2;
  49. static int64 nValueRet;
  50. // test multiple times to allow for differences in the shuffle order
  51. for (int i = 0; i < RUN_TESTS; i++)
  52. {
  53. empty_wallet();
  54. // with an empty wallet we can't even pay one cent
  55. BOOST_CHECK(!wallet.SelectCoinsMinConf( 1 * CENT, 1, 6, vCoins, setCoinsRet, nValueRet));
  56. add_coin(1*CENT, 4); // add a new 1 cent coin
  57. // with a new 1 cent coin, we still can't find a mature 1 cent
  58. BOOST_CHECK(!wallet.SelectCoinsMinConf( 1 * CENT, 1, 6, vCoins, setCoinsRet, nValueRet));
  59. // but we can find a new 1 cent
  60. BOOST_CHECK( wallet.SelectCoinsMinConf( 1 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  61. BOOST_CHECK_EQUAL(nValueRet, 1 * CENT);
  62. add_coin(2*CENT); // add a mature 2 cent coin
  63. // we can't make 3 cents of mature coins
  64. BOOST_CHECK(!wallet.SelectCoinsMinConf( 3 * CENT, 1, 6, vCoins, setCoinsRet, nValueRet));
  65. // we can make 3 cents of new coins
  66. BOOST_CHECK( wallet.SelectCoinsMinConf( 3 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  67. BOOST_CHECK_EQUAL(nValueRet, 3 * CENT);
  68. add_coin(5*CENT); // add a mature 5 cent coin,
  69. add_coin(10*CENT, 3, true); // a new 10 cent coin sent from one of our own addresses
  70. add_coin(20*CENT); // and a mature 20 cent coin
  71. // now we have new: 1+10=11 (of which 10 was self-sent), and mature: 2+5+20=27. total = 38
  72. // we can't make 38 cents only if we disallow new coins:
  73. BOOST_CHECK(!wallet.SelectCoinsMinConf(38 * CENT, 1, 6, vCoins, setCoinsRet, nValueRet));
  74. // we can't even make 37 cents if we don't allow new coins even if they're from us
  75. BOOST_CHECK(!wallet.SelectCoinsMinConf(38 * CENT, 6, 6, vCoins, setCoinsRet, nValueRet));
  76. // but we can make 37 cents if we accept new coins from ourself
  77. BOOST_CHECK( wallet.SelectCoinsMinConf(37 * CENT, 1, 6, vCoins, setCoinsRet, nValueRet));
  78. BOOST_CHECK_EQUAL(nValueRet, 37 * CENT);
  79. // and we can make 38 cents if we accept all new coins
  80. BOOST_CHECK( wallet.SelectCoinsMinConf(38 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  81. BOOST_CHECK_EQUAL(nValueRet, 38 * CENT);
  82. // try making 34 cents from 1,2,5,10,20 - we can't do it exactly
  83. BOOST_CHECK( wallet.SelectCoinsMinConf(34 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  84. BOOST_CHECK_GT(nValueRet, 34 * CENT); // but should get more than 34 cents
  85. BOOST_CHECK_EQUAL(setCoinsRet.size(), 3); // the best should be 20+10+5. it's incredibly unlikely the 1 or 2 got included (but possible)
  86. // when we try making 7 cents, the smaller coins (1,2,5) are enough. We should see just 2+5
  87. BOOST_CHECK( wallet.SelectCoinsMinConf( 7 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  88. BOOST_CHECK_EQUAL(nValueRet, 7 * CENT);
  89. BOOST_CHECK_EQUAL(setCoinsRet.size(), 2);
  90. // when we try making 8 cents, the smaller coins (1,2,5) are exactly enough.
  91. BOOST_CHECK( wallet.SelectCoinsMinConf( 8 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  92. BOOST_CHECK(nValueRet == 8 * CENT);
  93. BOOST_CHECK_EQUAL(setCoinsRet.size(), 3);
  94. // when we try making 9 cents, no subset of smaller coins is enough, and we get the next bigger coin (10)
  95. BOOST_CHECK( wallet.SelectCoinsMinConf( 9 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  96. BOOST_CHECK_EQUAL(nValueRet, 10 * CENT);
  97. BOOST_CHECK_EQUAL(setCoinsRet.size(), 1);
  98. // now clear out the wallet and start again to test choosing between subsets of smaller coins and the next biggest coin
  99. empty_wallet();
  100. add_coin( 6*CENT);
  101. add_coin( 7*CENT);
  102. add_coin( 8*CENT);
  103. add_coin(20*CENT);
  104. add_coin(30*CENT); // now we have 6+7+8+20+30 = 71 cents total
  105. // check that we have 71 and not 72
  106. BOOST_CHECK( wallet.SelectCoinsMinConf(71 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  107. BOOST_CHECK(!wallet.SelectCoinsMinConf(72 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  108. // 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
  109. BOOST_CHECK( wallet.SelectCoinsMinConf(16 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  110. BOOST_CHECK_EQUAL(nValueRet, 20 * CENT); // we should get 20 in one coin
  111. BOOST_CHECK_EQUAL(setCoinsRet.size(), 1);
  112. add_coin( 5*CENT); // now we have 5+6+7+8+20+30 = 75 cents total
  113. // 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
  114. BOOST_CHECK( wallet.SelectCoinsMinConf(16 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  115. BOOST_CHECK_EQUAL(nValueRet, 18 * CENT); // we should get 18 in 3 coins
  116. BOOST_CHECK_EQUAL(setCoinsRet.size(), 3);
  117. add_coin( 18*CENT); // now we have 5+6+7+8+18+20+30
  118. // 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
  119. BOOST_CHECK( wallet.SelectCoinsMinConf(16 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  120. BOOST_CHECK_EQUAL(nValueRet, 18 * CENT); // we should get 18 in 1 coin
  121. BOOST_CHECK_EQUAL(setCoinsRet.size(), 1); // because in the event of a tie, the biggest coin wins
  122. // now try making 11 cents. we should get 5+6
  123. BOOST_CHECK( wallet.SelectCoinsMinConf(11 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  124. BOOST_CHECK_EQUAL(nValueRet, 11 * CENT);
  125. BOOST_CHECK_EQUAL(setCoinsRet.size(), 2);
  126. // check that the smallest bigger coin is used
  127. add_coin( 1*COIN);
  128. add_coin( 2*COIN);
  129. add_coin( 3*COIN);
  130. add_coin( 4*COIN); // now we have 5+6+7+8+18+20+30+100+200+300+400 = 1094 cents
  131. BOOST_CHECK( wallet.SelectCoinsMinConf(95 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  132. BOOST_CHECK_EQUAL(nValueRet, 1 * COIN); // we should get 1 ECO in 1 coin
  133. BOOST_CHECK_EQUAL(setCoinsRet.size(), 1);
  134. BOOST_CHECK( wallet.SelectCoinsMinConf(195 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  135. BOOST_CHECK_EQUAL(nValueRet, 2 * COIN); // we should get 2 ECO in 1 coin
  136. BOOST_CHECK_EQUAL(setCoinsRet.size(), 1);
  137. // empty the wallet and start again, now with fractions of a cent, to test sub-cent change avoidance
  138. empty_wallet();
  139. add_coin(0.1*CENT);
  140. add_coin(0.2*CENT);
  141. add_coin(0.3*CENT);
  142. add_coin(0.4*CENT);
  143. add_coin(0.5*CENT);
  144. // try making 1 cent from 0.1 + 0.2 + 0.3 + 0.4 + 0.5 = 1.5 cents
  145. // we'll get sub-cent change whatever happens, so can expect 1.0 exactly
  146. BOOST_CHECK( wallet.SelectCoinsMinConf(1 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  147. BOOST_CHECK_EQUAL(nValueRet, 1 * CENT);
  148. // but if we add a bigger coin, making it possible to avoid sub-cent change, things change:
  149. add_coin(1111*CENT);
  150. // try making 1 cent from 0.1 + 0.2 + 0.3 + 0.4 + 0.5 + 1111 = 1112.5 cents
  151. BOOST_CHECK( wallet.SelectCoinsMinConf(1 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  152. BOOST_CHECK_EQUAL(nValueRet, 1 * CENT); // we should get the exact amount
  153. // if we add more sub-cent coins:
  154. add_coin(0.6*CENT);
  155. add_coin(0.7*CENT);
  156. // and try again to make 1.0 cents, we can still make 1.0 cents
  157. BOOST_CHECK( wallet.SelectCoinsMinConf(1 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  158. BOOST_CHECK_EQUAL(nValueRet, 1 * CENT); // we should get the exact amount
  159. // run the 'mtgox' test (see http://blockexplorer.com/tx/29a3efd3ef04f9153d47a990bd7b048a4b2d213daaa5fb8ed670fb85f13bdbcf)
  160. // they tried to consolidate 10 50k coins into one 500k coin, and ended up with 50k in change
  161. empty_wallet();
  162. for (int i = 0; i < 20; i++)
  163. add_coin(50000 * COIN);
  164. BOOST_CHECK( wallet.SelectCoinsMinConf(500000 * COIN, 1, 1, vCoins, setCoinsRet, nValueRet));
  165. BOOST_CHECK_EQUAL(nValueRet, 500000 * COIN); // we should get the exact amount
  166. BOOST_CHECK_EQUAL(setCoinsRet.size(), 10); // in ten coins
  167. // if there's not enough in the smaller coins to make at least 1 cent change (0.5+0.6+0.7 < 1.0+1.0),
  168. // we need to try finding an exact subset anyway
  169. // sometimes it will fail, and so we use the next biggest coin:
  170. empty_wallet();
  171. add_coin(0.5 * CENT);
  172. add_coin(0.6 * CENT);
  173. add_coin(0.7 * CENT);
  174. add_coin(1111 * CENT);
  175. BOOST_CHECK( wallet.SelectCoinsMinConf(1 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  176. BOOST_CHECK_EQUAL(nValueRet, 1111 * CENT); // we get the bigger coin
  177. BOOST_CHECK_EQUAL(setCoinsRet.size(), 1);
  178. // but sometimes it's possible, and we use an exact subset (0.4 + 0.6 = 1.0)
  179. empty_wallet();
  180. add_coin(0.4 * CENT);
  181. add_coin(0.6 * CENT);
  182. add_coin(0.8 * CENT);
  183. add_coin(1111 * CENT);
  184. BOOST_CHECK( wallet.SelectCoinsMinConf(1 * CENT, 1, 1, vCoins, setCoinsRet, nValueRet));
  185. BOOST_CHECK_EQUAL(nValueRet, 1 * CENT); // we should get the exact amount
  186. BOOST_CHECK_EQUAL(setCoinsRet.size(), 2); // in two coins 0.4+0.6
  187. // test avoiding sub-cent change
  188. empty_wallet();
  189. add_coin(0.0005 * COIN);
  190. add_coin(0.01 * COIN);
  191. add_coin(1 * COIN);
  192. // trying to make 1.0001 from these three coins
  193. BOOST_CHECK( wallet.SelectCoinsMinConf(1.0001 * COIN, 1, 1, vCoins, setCoinsRet, nValueRet));
  194. BOOST_CHECK_EQUAL(nValueRet, 1.0105 * COIN); // we should get all coins
  195. BOOST_CHECK_EQUAL(setCoinsRet.size(), 3);
  196. // but if we try to make 0.999, we should take the bigger of the two small coins to avoid sub-cent change
  197. BOOST_CHECK( wallet.SelectCoinsMinConf(0.999 * COIN, 1, 1, vCoins, setCoinsRet, nValueRet));
  198. BOOST_CHECK_EQUAL(nValueRet, 1.01 * COIN); // we should get 1 + 0.01
  199. BOOST_CHECK_EQUAL(setCoinsRet.size(), 2);
  200. // test randomness
  201. {
  202. empty_wallet();
  203. for (int i2 = 0; i2 < 100; i2++)
  204. add_coin(COIN);
  205. // picking 50 from 100 coins doesn't depend on the shuffle,
  206. // but does depend on randomness in the stochastic approximation code
  207. BOOST_CHECK(wallet.SelectCoinsMinConf(50 * COIN, 1, 6, vCoins, setCoinsRet , nValueRet));
  208. BOOST_CHECK(wallet.SelectCoinsMinConf(50 * COIN, 1, 6, vCoins, setCoinsRet2, nValueRet));
  209. BOOST_CHECK(!equal_sets(setCoinsRet, setCoinsRet2));
  210. int fails = 0;
  211. for (int i = 0; i < RANDOM_REPEATS; i++)
  212. {
  213. // selecting 1 from 100 identical coins depends on the shuffle; this test will fail 1% of the time
  214. // run the test RANDOM_REPEATS times and only complain if all of them fail
  215. BOOST_CHECK(wallet.SelectCoinsMinConf(COIN, 1, 6, vCoins, setCoinsRet , nValueRet));
  216. BOOST_CHECK(wallet.SelectCoinsMinConf(COIN, 1, 6, vCoins, setCoinsRet2, nValueRet));
  217. if (equal_sets(setCoinsRet, setCoinsRet2))
  218. fails++;
  219. }
  220. BOOST_CHECK_NE(fails, RANDOM_REPEATS);
  221. // add 75 cents in small change. not enough to make 90 cents,
  222. // then try making 90 cents. there are multiple competing "smallest bigger" coins,
  223. // one of which should be picked at random
  224. add_coin( 5*CENT); add_coin(10*CENT); add_coin(15*CENT); add_coin(20*CENT); add_coin(25*CENT);
  225. fails = 0;
  226. for (int i = 0; i < RANDOM_REPEATS; i++)
  227. {
  228. // selecting 1 from 100 identical coins depends on the shuffle; this test will fail 1% of the time
  229. // run the test RANDOM_REPEATS times and only complain if all of them fail
  230. BOOST_CHECK(wallet.SelectCoinsMinConf(90*CENT, 1, 6, vCoins, setCoinsRet , nValueRet));
  231. BOOST_CHECK(wallet.SelectCoinsMinConf(90*CENT, 1, 6, vCoins, setCoinsRet2, nValueRet));
  232. if (equal_sets(setCoinsRet, setCoinsRet2))
  233. fails++;
  234. }
  235. BOOST_CHECK_NE(fails, RANDOM_REPEATS);
  236. }
  237. }
  238. }
  239. BOOST_AUTO_TEST_SUITE_END()