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