You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wallet_tests.cpp 14KB

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