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