Fabcoin Core  0.16.2
P2P Digital Currency
wallet_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2017 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 <consensus/validation.h>
13 #include <consensus/consensus.h>
14 #include <rpc/server.h>
15 #include <test/test_fabcoin.h>
16 #include <validation.h>
17 #include <wallet/coincontrol.h>
19 
20 #include <boost/test/unit_test.hpp>
21 #include <univalue.h>
22 
23 #include <timedata.h>
24 #include <random.h>
25 
26 extern CWallet* pwalletMain;
27 
28 extern UniValue importmulti(const JSONRPCRequest& request);
29 extern UniValue dumpwallet(const JSONRPCRequest& request);
30 extern UniValue importwallet(const JSONRPCRequest& request);
31 
32 // how many times to run all the tests to have a chance to catch errors that only show up with particular random shuffles
33 #define RUN_TESTS 100
34 
35 // some tests fail 1% of the time due to bad luck.
36 // we repeat those tests this many times and only complain if all iterations of the test fail
37 #define RANDOM_REPEATS 5
38 
39 std::vector<std::unique_ptr<CWalletTx>> wtxn;
40 
41 typedef std::set<CInputCoin> CoinSet;
42 
44 
45 static const CWallet testWallet;
46 static std::vector<COutput> vCoins;
47 
48 static void add_coin(const CAmount& nValue, int nAge = 6*24, bool fIsFromMe = false, int nInput=0)
49 {
50  static int nextLockTime = 0;
52  tx.nLockTime = nextLockTime++; // so all transactions get different hashes
53  tx.vout.resize(nInput+1);
54  tx.vout[nInput].nValue = nValue;
55  if (fIsFromMe) {
56  // IsFromMe() returns (GetDebit() > 0), and GetDebit() is 0 if vin.empty(),
57  // so stop vin being empty, and cache a non-zero Debit to fake out IsFromMe()
58  tx.vin.resize(1);
59  }
60  std::unique_ptr<CWalletTx> wtx(new CWalletTx(&testWallet, MakeTransactionRef(std::move(tx))));
61  if (fIsFromMe)
62  {
63  wtx->fDebitCached = true;
64  wtx->nDebitCached = 1;
65  }
66  COutput output(wtx.get(), nInput, nAge, true /* spendable */, true /* solvable */, true /* safe */);
67  vCoins.push_back(output);
68  wtxn.emplace_back(std::move(wtx));
69 }
70 
71 static void empty_wallet(void)
72 {
73  vCoins.clear();
74  wtxn.clear();
75 }
76 
77 static bool equal_sets(CoinSet a, CoinSet b)
78 {
79  std::pair<CoinSet::iterator, CoinSet::iterator> ret = mismatch(a.begin(), a.end(), b.begin());
80  return ret.first == a.end() && ret.second == b.end();
81 }
82 
83 BOOST_AUTO_TEST_CASE(coin_selection_tests)
84 {
85  CoinSet setCoinsRet, setCoinsRet2;
86  CAmount nValueRet;
87 
88  LOCK(testWallet.cs_wallet);
89 
90  // test multiple times to allow for differences in the shuffle order
91  for (int i = 0; i < RUN_TESTS; i++)
92  {
93  empty_wallet();
94 
95  // with an empty wallet we can't even pay one cent
96  BOOST_CHECK(!testWallet.SelectCoinsMinConf( 1 * CENT, 1, 6, 0, vCoins, setCoinsRet, nValueRet));
97 
98  add_coin(1*CENT, 4); // add a new 1 cent coin
99 
100  // with a new 1 cent coin, we still can't find a mature 1 cent
101  BOOST_CHECK(!testWallet.SelectCoinsMinConf( 1 * CENT, 1, 6, 0, vCoins, setCoinsRet, nValueRet));
102 
103  // but we can find a new 1 cent
104  BOOST_CHECK( testWallet.SelectCoinsMinConf( 1 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
105  BOOST_CHECK_EQUAL(nValueRet, 1 * CENT);
106 
107  add_coin(2*CENT); // add a mature 2 cent coin
108 
109  // we can't make 3 cents of mature coins
110  BOOST_CHECK(!testWallet.SelectCoinsMinConf( 3 * CENT, 1, 6, 0, vCoins, setCoinsRet, nValueRet));
111 
112  // we can make 3 cents of new coins
113  BOOST_CHECK( testWallet.SelectCoinsMinConf( 3 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
114  BOOST_CHECK_EQUAL(nValueRet, 3 * CENT);
115 
116  add_coin(5*CENT); // add a mature 5 cent coin,
117  add_coin(10*CENT, 3, true); // a new 10 cent coin sent from one of our own addresses
118  add_coin(20*CENT); // and a mature 20 cent coin
119 
120  // now we have new: 1+10=11 (of which 10 was self-sent), and mature: 2+5+20=27. total = 38
121 
122  // we can't make 38 cents only if we disallow new coins:
123  BOOST_CHECK(!testWallet.SelectCoinsMinConf(38 * CENT, 1, 6, 0, vCoins, setCoinsRet, nValueRet));
124  // we can't even make 37 cents if we don't allow new coins even if they're from us
125  BOOST_CHECK(!testWallet.SelectCoinsMinConf(38 * CENT, 6, 6, 0, vCoins, setCoinsRet, nValueRet));
126  // but we can make 37 cents if we accept new coins from ourself
127  BOOST_CHECK( testWallet.SelectCoinsMinConf(37 * CENT, 1, 6, 0, vCoins, setCoinsRet, nValueRet));
128  BOOST_CHECK_EQUAL(nValueRet, 37 * CENT);
129  // and we can make 38 cents if we accept all new coins
130  BOOST_CHECK( testWallet.SelectCoinsMinConf(38 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
131  BOOST_CHECK_EQUAL(nValueRet, 38 * CENT);
132 
133  // try making 34 cents from 1,2,5,10,20 - we can't do it exactly
134  BOOST_CHECK( testWallet.SelectCoinsMinConf(34 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
135  BOOST_CHECK_EQUAL(nValueRet, 35 * CENT); // but 35 cents is closest
136  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)
137 
138  // when we try making 7 cents, the smaller coins (1,2,5) are enough. We should see just 2+5
139  BOOST_CHECK( testWallet.SelectCoinsMinConf( 7 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
140  BOOST_CHECK_EQUAL(nValueRet, 7 * CENT);
141  BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
142 
143  // when we try making 8 cents, the smaller coins (1,2,5) are exactly enough.
144  BOOST_CHECK( testWallet.SelectCoinsMinConf( 8 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
145  BOOST_CHECK(nValueRet == 8 * CENT);
146  BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
147 
148  // when we try making 9 cents, no subset of smaller coins is enough, and we get the next bigger coin (10)
149  BOOST_CHECK( testWallet.SelectCoinsMinConf( 9 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
150  BOOST_CHECK_EQUAL(nValueRet, 10 * CENT);
151  BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
152 
153  // now clear out the wallet and start again to test choosing between subsets of smaller coins and the next biggest coin
154  empty_wallet();
155 
156  add_coin( 6*CENT);
157  add_coin( 7*CENT);
158  add_coin( 8*CENT);
159  add_coin(20*CENT);
160  add_coin(30*CENT); // now we have 6+7+8+20+30 = 71 cents total
161 
162  // check that we have 71 and not 72
163  BOOST_CHECK( testWallet.SelectCoinsMinConf(71 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
164  BOOST_CHECK(!testWallet.SelectCoinsMinConf(72 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
165 
166  // 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
167  BOOST_CHECK( testWallet.SelectCoinsMinConf(16 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
168  BOOST_CHECK_EQUAL(nValueRet, 20 * CENT); // we should get 20 in one coin
169  BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
170 
171  add_coin( 5*CENT); // now we have 5+6+7+8+20+30 = 75 cents total
172 
173  // 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
174  BOOST_CHECK( testWallet.SelectCoinsMinConf(16 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
175  BOOST_CHECK_EQUAL(nValueRet, 18 * CENT); // we should get 18 in 3 coins
176  BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
177 
178  add_coin( 18*CENT); // now we have 5+6+7+8+18+20+30
179 
180  // 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
181  BOOST_CHECK( testWallet.SelectCoinsMinConf(16 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
182  BOOST_CHECK_EQUAL(nValueRet, 18 * CENT); // we should get 18 in 1 coin
183  BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U); // because in the event of a tie, the biggest coin wins
184 
185  // now try making 11 cents. we should get 5+6
186  BOOST_CHECK( testWallet.SelectCoinsMinConf(11 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
187  BOOST_CHECK_EQUAL(nValueRet, 11 * CENT);
188  BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
189 
190  // check that the smallest bigger coin is used
191  add_coin( 1*COIN);
192  add_coin( 2*COIN);
193  add_coin( 3*COIN);
194  add_coin( 4*COIN); // now we have 5+6+7+8+18+20+30+100+200+300+400 = 1094 cents
195  BOOST_CHECK( testWallet.SelectCoinsMinConf(95 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
196  BOOST_CHECK_EQUAL(nValueRet, 1 * COIN); // we should get 1 FAB in 1 coin
197  BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
198 
199  BOOST_CHECK( testWallet.SelectCoinsMinConf(195 * CENT, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
200  BOOST_CHECK_EQUAL(nValueRet, 2 * COIN); // we should get 2 FAB in 1 coin
201  BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
202 
203  // empty the wallet and start again, now with fractions of a cent, to test small change avoidance
204 
205  empty_wallet();
206  add_coin(MIN_CHANGE * 1 / 10);
207  add_coin(MIN_CHANGE * 2 / 10);
208  add_coin(MIN_CHANGE * 3 / 10);
209  add_coin(MIN_CHANGE * 4 / 10);
210  add_coin(MIN_CHANGE * 5 / 10);
211 
212  // try making 1 * MIN_CHANGE from the 1.5 * MIN_CHANGE
213  // we'll get change smaller than MIN_CHANGE whatever happens, so can expect MIN_CHANGE exactly
214  BOOST_CHECK( testWallet.SelectCoinsMinConf(MIN_CHANGE, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
215  BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE);
216 
217  // but if we add a bigger coin, small change is avoided
218  add_coin(1111*MIN_CHANGE);
219 
220  // try making 1 from 0.1 + 0.2 + 0.3 + 0.4 + 0.5 + 1111 = 1112.5
221  BOOST_CHECK( testWallet.SelectCoinsMinConf(1 * MIN_CHANGE, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
222  BOOST_CHECK_EQUAL(nValueRet, 1 * MIN_CHANGE); // we should get the exact amount
223 
224  // if we add more small coins:
225  add_coin(MIN_CHANGE * 6 / 10);
226  add_coin(MIN_CHANGE * 7 / 10);
227 
228  // and try again to make 1.0 * MIN_CHANGE
229  BOOST_CHECK( testWallet.SelectCoinsMinConf(1 * MIN_CHANGE, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
230  BOOST_CHECK_EQUAL(nValueRet, 1 * MIN_CHANGE); // we should get the exact amount
231 
232  // run the 'mtgox' test (see http://blockexplorer.com/tx/29a3efd3ef04f9153d47a990bd7b048a4b2d213daaa5fb8ed670fb85f13bdbcf)
233  // they tried to consolidate 10 50k coins into one 500k coin, and ended up with 50k in change
234  empty_wallet();
235  for (int j = 0; j < 20; j++)
236  add_coin(50000 * COIN);
237 
238  BOOST_CHECK( testWallet.SelectCoinsMinConf(500000 * COIN, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
239  BOOST_CHECK_EQUAL(nValueRet, 500000 * COIN); // we should get the exact amount
240  BOOST_CHECK_EQUAL(setCoinsRet.size(), 10U); // in ten coins
241 
242  // 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),
243  // we need to try finding an exact subset anyway
244 
245  // sometimes it will fail, and so we use the next biggest coin:
246  empty_wallet();
247  add_coin(MIN_CHANGE * 5 / 10);
248  add_coin(MIN_CHANGE * 6 / 10);
249  add_coin(MIN_CHANGE * 7 / 10);
250  add_coin(1111 * MIN_CHANGE);
251  BOOST_CHECK( testWallet.SelectCoinsMinConf(1 * MIN_CHANGE, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
252  BOOST_CHECK_EQUAL(nValueRet, 1111 * MIN_CHANGE); // we get the bigger coin
253  BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
254 
255  // but sometimes it's possible, and we use an exact subset (0.4 + 0.6 = 1.0)
256  empty_wallet();
257  add_coin(MIN_CHANGE * 4 / 10);
258  add_coin(MIN_CHANGE * 6 / 10);
259  add_coin(MIN_CHANGE * 8 / 10);
260  add_coin(1111 * MIN_CHANGE);
261  BOOST_CHECK( testWallet.SelectCoinsMinConf(MIN_CHANGE, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
262  BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE); // we should get the exact amount
263  BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U); // in two coins 0.4+0.6
264 
265  // test avoiding small change
266  empty_wallet();
267  add_coin(MIN_CHANGE * 5 / 100);
268  add_coin(MIN_CHANGE * 1);
269  add_coin(MIN_CHANGE * 100);
270 
271  // trying to make 100.01 from these three coins
272  BOOST_CHECK(testWallet.SelectCoinsMinConf(MIN_CHANGE * 10001 / 100, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
273  BOOST_CHECK_EQUAL(nValueRet, MIN_CHANGE * 10105 / 100); // we should get all coins
274  BOOST_CHECK_EQUAL(setCoinsRet.size(), 3U);
275 
276  // but if we try to make 99.9, we should take the bigger of the two small coins to avoid small change
277  BOOST_CHECK(testWallet.SelectCoinsMinConf(MIN_CHANGE * 9990 / 100, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
278  BOOST_CHECK_EQUAL(nValueRet, 101 * MIN_CHANGE);
279  BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
280 
281  // test with many inputs
282  for (CAmount amt=1500; amt < COIN; amt*=10) {
283  empty_wallet();
284  // Create 676 inputs (= (old MAX_STANDARD_TX_SIZE == 100000) / 148 bytes per input)
285  for (uint16_t j = 0; j < 676; j++)
286  add_coin(amt);
287  BOOST_CHECK(testWallet.SelectCoinsMinConf(2000, 1, 1, 0, vCoins, setCoinsRet, nValueRet));
288  if (amt - 2000 < MIN_CHANGE) {
289  // needs more than one input:
290  uint16_t returnSize = std::ceil((2000.0 + MIN_CHANGE)/amt);
291  CAmount returnValue = amt * returnSize;
292  BOOST_CHECK_EQUAL(nValueRet, returnValue);
293  BOOST_CHECK_EQUAL(setCoinsRet.size(), returnSize);
294  } else {
295  // one input is sufficient:
296  BOOST_CHECK_EQUAL(nValueRet, amt);
297  BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U);
298  }
299  }
300 
301  // test randomness
302  {
303  empty_wallet();
304  for (int i2 = 0; i2 < 100; i2++)
305  add_coin(COIN);
306 
307  // picking 50 from 100 coins doesn't depend on the shuffle,
308  // but does depend on randomness in the stochastic approximation code
309  BOOST_CHECK(testWallet.SelectCoinsMinConf(50 * COIN, 1, 6, 0, vCoins, setCoinsRet , nValueRet));
310  BOOST_CHECK(testWallet.SelectCoinsMinConf(50 * COIN, 1, 6, 0, vCoins, setCoinsRet2, nValueRet));
311  BOOST_CHECK(!equal_sets(setCoinsRet, setCoinsRet2));
312 
313  int fails = 0;
314  for (int j = 0; j < RANDOM_REPEATS; j++)
315  {
316  // selecting 1 from 100 identical coins depends on the shuffle; this test will fail 1% of the time
317  // run the test RANDOM_REPEATS times and only complain if all of them fail
318  BOOST_CHECK(testWallet.SelectCoinsMinConf(COIN, 1, 6, 0, vCoins, setCoinsRet , nValueRet));
319  BOOST_CHECK(testWallet.SelectCoinsMinConf(COIN, 1, 6, 0, vCoins, setCoinsRet2, nValueRet));
320  if (equal_sets(setCoinsRet, setCoinsRet2))
321  fails++;
322  }
323  BOOST_CHECK_NE(fails, RANDOM_REPEATS);
324 
325  // add 75 cents in small change. not enough to make 90 cents,
326  // then try making 90 cents. there are multiple competing "smallest bigger" coins,
327  // one of which should be picked at random
328  add_coin(5 * CENT);
329  add_coin(10 * CENT);
330  add_coin(15 * CENT);
331  add_coin(20 * CENT);
332  add_coin(25 * CENT);
333 
334  fails = 0;
335  for (int j = 0; j < RANDOM_REPEATS; j++)
336  {
337  // selecting 1 from 100 identical coins depends on the shuffle; this test will fail 1% of the time
338  // run the test RANDOM_REPEATS times and only complain if all of them fail
339  BOOST_CHECK(testWallet.SelectCoinsMinConf(90*CENT, 1, 6, 0, vCoins, setCoinsRet , nValueRet));
340  BOOST_CHECK(testWallet.SelectCoinsMinConf(90*CENT, 1, 6, 0, vCoins, setCoinsRet2, nValueRet));
341  if (equal_sets(setCoinsRet, setCoinsRet2))
342  fails++;
343  }
344  BOOST_CHECK_NE(fails, RANDOM_REPEATS);
345  }
346  }
347  empty_wallet();
348 }
349 
350 BOOST_AUTO_TEST_CASE(ApproximateBestSubset)
351 {
352  CoinSet setCoinsRet;
353  CAmount nValueRet;
354 
355  LOCK(testWallet.cs_wallet);
356 
357  empty_wallet();
358 
359  // Test vValue sort order
360  for (int i = 0; i < 1000; i++)
361  add_coin(1000 * COIN);
362  add_coin(3 * COIN);
363 
364  BOOST_CHECK(testWallet.SelectCoinsMinConf(1003 * COIN, 1, 6, 0, vCoins, setCoinsRet, nValueRet));
365  BOOST_CHECK_EQUAL(nValueRet, 1003 * COIN);
366  BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U);
367 
368  empty_wallet();
369 }
370 
371 static void AddKey(CWallet& wallet, const CKey& key)
372 {
373  LOCK(wallet.cs_wallet);
374  wallet.AddKeyPubKey(key, key.GetPubKey());
375 }
376 
377 BOOST_FIXTURE_TEST_CASE(rescan, TestChain800Setup)
378 {
379  LOCK(cs_main);
380 
381  // Cap last block file size, and mine new block in a new block file.
382  CBlockIndex* const nullBlock = nullptr;
383  CBlockIndex* oldTip = chainActive.Tip();
384  GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE;
385  CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
386  CBlockIndex* newTip = chainActive.Tip();
387 
388  // Verify ScanForWalletTransactions picks up transactions in both the old
389  // and new block files.
390  {
391  CWallet wallet;
392  AddKey(wallet, coinbaseKey);
393  BOOST_CHECK_EQUAL(nullBlock, wallet.ScanForWalletTransactions(oldTip));
394  BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 2* INITIAL_BLOCK_REWARD_REGTEST*COIN);
395  }
396 
397  // Prune the older block file.
399  UnlinkPrunedFiles({oldTip->GetBlockPos().nFile});
400 
401  // Verify ScanForWalletTransactions only picks transactions in the new block
402  // file.
403  {
404  CWallet wallet;
405  AddKey(wallet, coinbaseKey);
406  BOOST_CHECK_EQUAL(oldTip, wallet.ScanForWalletTransactions(oldTip));
407  BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), INITIAL_BLOCK_REWARD_REGTEST*COIN);
408  }
409 
410  // Verify importmulti RPC returns failure for a key whose creation time is
411  // before the missing block, and success for a key whose creation time is
412  // after.
413  {
414  CWallet wallet;
415  vpwallets.insert(vpwallets.begin(), &wallet);
416  UniValue keys;
417  keys.setArray();
418  UniValue key;
419  key.setObject();
420  key.pushKV("scriptPubKey", HexStr(GetScriptForRawPubKey(coinbaseKey.GetPubKey())));
421  key.pushKV("timestamp", 0);
422  key.pushKV("internal", UniValue(true));
423  keys.push_back(key);
424  key.clear();
425  key.setObject();
426  CKey futureKey;
427  futureKey.MakeNewKey(true);
428  key.pushKV("scriptPubKey", HexStr(GetScriptForRawPubKey(futureKey.GetPubKey())));
429  key.pushKV("timestamp", newTip->GetBlockTimeMax() + TIMESTAMP_WINDOW + 1);
430  key.pushKV("internal", UniValue(true));
431  keys.push_back(key);
432  JSONRPCRequest request;
433  request.params.setArray();
434  request.params.push_back(keys);
435 
436  UniValue response = importmulti(request);
437  BOOST_CHECK_EQUAL(response.write(),
438  strprintf("[{\"success\":false,\"error\":{\"code\":-1,\"message\":\"Rescan failed for key with creation "
439  "timestamp %d. There was an error reading a block from time %d, which is after or within %d "
440  "seconds of key creation, and could contain transactions pertaining to the key. As a result, "
441  "transactions and coins using this key may not appear in the wallet. This error could be caused "
442  "by pruning or data corruption (see fabcoind log for details) and could be dealt with by "
443  "downloading and rescanning the relevant blocks (see -reindex and -rescan "
444  "options).\"}},{\"success\":true}]",
445  0, oldTip->GetBlockTimeMax(), TIMESTAMP_WINDOW));
446  vpwallets.erase(vpwallets.begin());
447  }
448 }
449 
450 // Verify importwallet RPC starts rescan at earliest block with timestamp
451 // greater or equal than key birthday. Previously there was a bug where
452 // importwallet RPC would start the scan at the latest block with timestamp less
453 // than or equal to key birthday.
454 BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain800Setup)
455 {
456  LOCK(cs_main);
457 
458  // Create two blocks with same timestamp to verify that importwallet rescan
459  // will pick up both blocks, not just the first.
460  const int64_t BLOCK_TIME = chainActive.Tip()->GetBlockTimeMax() + 5;
461  SetMockTime(BLOCK_TIME);
462  coinbaseTxns.emplace_back(*CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
463  coinbaseTxns.emplace_back(*CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
464 
465  // Set key birthday to block time increased by the timestamp window, so
466  // rescan will start at the block time.
467  const int64_t KEY_TIME = BLOCK_TIME + TIMESTAMP_WINDOW;
468  SetMockTime(KEY_TIME);
469  coinbaseTxns.emplace_back(*CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]);
470 
471  // Import key into wallet and call dumpwallet to create backup file.
472  {
473  CWallet wallet;
474  LOCK(wallet.cs_wallet);
475  wallet.mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = KEY_TIME;
476  wallet.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
477 
478  JSONRPCRequest request;
479  request.params.setArray();
480  request.params.push_back((pathTemp / "wallet.backup").string());
481  vpwallets.insert(vpwallets.begin(), &wallet);
482  ::dumpwallet(request);
483  }
484 
485  // Call importwallet RPC and verify all blocks with timestamps >= BLOCK_TIME
486  // were scanned, and no prior blocks were scanned.
487  {
488  CWallet wallet;
489 
490  JSONRPCRequest request;
491  request.params.setArray();
492  request.params.push_back((pathTemp / "wallet.backup").string());
493  vpwallets[0] = &wallet;
494  ::importwallet(request);
495 
496  BOOST_CHECK_EQUAL(wallet.mapWallet.size(), 3);
497  BOOST_CHECK_EQUAL(coinbaseTxns.size(), 803);
498  for (size_t i = 0; i < coinbaseTxns.size(); ++i) {
499  bool found = wallet.GetWalletTx(coinbaseTxns[i].GetHash());
500  bool expected = i >= 800;
501  BOOST_CHECK_EQUAL(found, expected);
502  }
503  }
504 
505  SetMockTime(0);
506  vpwallets.erase(vpwallets.begin());
507 }
508 
509 // Check that GetImmatureCredit() returns a newly calculated value instead of
510 // the cached value after a MarkDirty() call.
511 //
512 // This is a regression test written to verify a bugfix for the immature credit
513 // function. Similar tests probably should be written for the other credit and
514 // debit functions.
515 BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain800Setup)
516 {
517  CWallet wallet;
518  CWalletTx wtx(&wallet, MakeTransactionRef(coinbaseTxns.back()));
519  LOCK2(cs_main, wallet.cs_wallet);
521  wtx.nIndex = 0;
522 
523  // Call GetImmatureCredit() once before adding the key to the wallet to
524  // cache the current immature credit amount, which is 0.
526 
527  // Invalidate the cached value, add the key, and make sure a new immature
528  // credit amount is calculated.
529  wtx.MarkDirty();
530  wallet.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
531  BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), INITIAL_BLOCK_REWARD_REGTEST*COIN);
532 }
533 
534 static int64_t AddTx(CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
535 {
537  tx.nLockTime = lockTime;
538  SetMockTime(mockTime);
539  CBlockIndex* block = nullptr;
540  if (blockTime > 0) {
541  auto inserted = mapBlockIndex.emplace(GetRandHash(), new CBlockIndex);
542  assert(inserted.second);
543  const uint256& hash = inserted.first->first;
544  block = inserted.first->second;
545  block->nTime = blockTime;
546  block->phashBlock = &hash;
547  }
548 
549  CWalletTx wtx(&wallet, MakeTransactionRef(tx));
550  if (block) {
551  wtx.SetMerkleBranch(block, 0);
552  }
553  wallet.AddToWallet(wtx);
554  return wallet.mapWallet.at(wtx.GetHash()).nTimeSmart;
555 }
556 
557 // Simple test to verify assignment of CWalletTx::nSmartTime value. Could be
558 // expanded to cover more corner cases of smart time logic.
559 BOOST_AUTO_TEST_CASE(ComputeTimeSmart)
560 {
561  CWallet wallet;
562 
563  // New transaction should use clock time if lower than block time.
564  BOOST_CHECK_EQUAL(AddTx(wallet, 1, 100, 120), 100);
565 
566  // Test that updating existing transaction does not change smart time.
567  BOOST_CHECK_EQUAL(AddTx(wallet, 1, 200, 220), 100);
568 
569  // New transaction should use clock time if there's no block time.
570  BOOST_CHECK_EQUAL(AddTx(wallet, 2, 300, 0), 300);
571 
572  // New transaction should use block time if lower than clock time.
573  BOOST_CHECK_EQUAL(AddTx(wallet, 3, 420, 400), 400);
574 
575  // New transaction should use latest entry time if higher than
576  // min(block time, clock time).
577  BOOST_CHECK_EQUAL(AddTx(wallet, 4, 500, 390), 400);
578 
579  // If there are future entries, new transaction should use time of the
580  // newest entry that is no more than 300 seconds ahead of the clock time.
581  BOOST_CHECK_EQUAL(AddTx(wallet, 5, 50, 600), 300);
582 
583  // Reset mock time for other tests.
584  SetMockTime(0);
585 }
586 
587 BOOST_AUTO_TEST_CASE(LoadReceiveRequests)
588 {
589  CTxDestination dest = CKeyID();
590  pwalletMain->AddDestData(dest, "misc", "val_misc");
591  pwalletMain->AddDestData(dest, "rr0", "val_rr0");
592  pwalletMain->AddDestData(dest, "rr1", "val_rr1");
593 
594  auto values = pwalletMain->GetDestValues("rr");
595  BOOST_CHECK_EQUAL(values.size(), 2);
596  BOOST_CHECK_EQUAL(values[0], "val_rr0");
597  BOOST_CHECK_EQUAL(values[1], "val_rr1");
598 }
599 
600 class ListCoinsTestingSetup : public TestChain800Setup
601 {
602 public:
604  {
605  CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
606  ::bitdb.MakeMock();
607  wallet.reset(new CWallet(std::unique_ptr<CWalletDBWrapper>(new CWalletDBWrapper(&bitdb, "wallet_test.dat"))));
608  bool firstRun;
609  wallet->LoadWallet(firstRun);
610  AddKey(*wallet, coinbaseKey);
612  }
613 
615  {
616  wallet.reset();
617  ::bitdb.Flush(true);
618  ::bitdb.Reset();
619  }
620 
622  {
623  CWalletTx wtx;
624  CReserveKey reservekey(wallet.get());
625  CAmount fee;
626  int changePos = -1;
627  std::string error;
628  CCoinControl dummy;
629  BOOST_CHECK(wallet->CreateTransaction({recipient}, wtx, reservekey, fee, changePos, error, dummy));
630  CValidationState state;
631  BOOST_CHECK(wallet->CommitTransaction(wtx, reservekey, nullptr, state));
632  auto it = wallet->mapWallet.find(wtx.GetHash());
633  BOOST_CHECK(it != wallet->mapWallet.end());
634  CreateAndProcessBlock({CMutableTransaction(*it->second.tx)}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
635  it->second.SetMerkleBranch(chainActive.Tip(), 1);
636  return it->second;
637  }
638 
639  std::unique_ptr<CWallet> wallet;
640 };
641 
643 {
644  std::string coinbaseAddress = coinbaseKey.GetPubKey().GetID().ToString();
645  LOCK2(cs_main, wallet->cs_wallet);
646 
647  // Confirm ListCoins initially returns 1 coin grouped under coinbaseKey
648  // address.
649  auto list = wallet->ListCoins();
650  BOOST_CHECK_EQUAL(list.size(), 1);
651  BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
652  BOOST_CHECK_EQUAL(list.begin()->second.size(), 1);
653 
654  // Check initial balance from one mature coinbase transaction.
655  BOOST_CHECK_EQUAL(INITIAL_BLOCK_REWARD_REGTEST*COIN, wallet->GetAvailableBalance());
656 
657  // Add a transaction creating a change address, and confirm ListCoins still
658  // returns the coin associated with the change address underneath the
659  // coinbaseKey pubkey, even though the change address has a different
660  // pubkey.
661  AddTx(CRecipient{GetScriptForRawPubKey({}), 1 * COIN, false /* subtract fee */});
662  list = wallet->ListCoins();
663  BOOST_CHECK_EQUAL(list.size(), 1);
664  BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
665  BOOST_CHECK_EQUAL(list.begin()->second.size(), 2);
666 
667  // Lock both coins. Confirm number of available coins drops to 0.
668  std::vector<COutput> available;
669  wallet->AvailableCoins(available);
670  BOOST_CHECK_EQUAL(available.size(), 2);
671  for (const auto& group : list) {
672  for (const auto& coin : group.second) {
673  wallet->LockCoin(COutPoint(coin.tx->GetHash(), coin.i));
674  }
675  }
676  wallet->AvailableCoins(available);
677  BOOST_CHECK_EQUAL(available.size(), 0);
678 
679  // Confirm ListCoins still returns same result as before, despite coins
680  // being locked.
681  list = wallet->ListCoins();
682  BOOST_CHECK_EQUAL(list.size(), 1);
683  BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
684  BOOST_CHECK_EQUAL(list.begin()->second.size(), 2);
685 }
686 
#define RANDOM_REPEATS
CBlockIndex * ScanForWalletTransactions(CBlockIndex *pindexStart, bool fUpdate=false)
Scan the block chain (starting in pindexStart) for transactions from or to us.
Definition: wallet.cpp:1643
bool error(const char *fmt, const Args &...args)
Definition: util.h:178
CDiskBlockPos GetBlockPos() const
Definition: chain.h:288
void SetMerkleBranch(const CBlockIndex *pIndex, int posInBlock)
Definition: wallet.cpp:4477
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:79
std::vector< CWalletRef > vpwallets
Definition: wallet.cpp:41
uint256 GetRandHash()
Definition: random.cpp:372
void AvailableCoins(std::vector< COutput > &vCoins, bool fOnlySafe=true, const CCoinControl *coinControl=nullptr, const CAmount &nMinimumAmount=1, const CAmount &nMaximumAmount=MAX_MONEY, const CAmount &nMinimumSumAmount=MAX_MONEY, const uint64_t &nMaximumCount=0, const int &nMinDepth=0, const int &nMaxDepth=9999999) const
populate vCoins with vector of available COutputs.
Definition: wallet.cpp:2167
CAmount GetAvailableBalance(const CCoinControl *coinControl=nullptr) const
Definition: wallet.cpp:2152
void Reset()
Definition: db.cpp:76
CCriticalSection cs_wallet
Definition: wallet.h:748
const uint256 & GetHash() const
Definition: wallet.h:278
#define strprintf
Definition: tinyformat.h:1054
int nIndex
Definition: wallet.h:219
std::vector< CTxIn > vin
Definition: transaction.h:392
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override
Adds a key to the store, and saves it to disk.
Definition: wallet.cpp:265
uint256 hashBlock
Definition: wallet.h:212
void SetMockTime(int64_t nMockTimeIn)
Definition: utiltime.cpp:29
CCriticalSection cs_main
Definition: validation.cpp:77
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Definition: standard.cpp:378
void Flush(bool fShutdown)
Definition: db.cpp:598
bool SelectCoinsMinConf(const CAmount &nTargetValue, int nConfMine, int nConfTheirs, uint64_t nMaxAncestors, std::vector< COutput > vCoins, std::set< CInputCoin > &setCoinsRet, CAmount &nValueRet) const
Shuffle and select coins until nTargetValue is reached while avoiding small change; This method is st...
Definition: wallet.cpp:2387
std::hash for asio::adress
Definition: Common.h:323
assert(len-trim+(2 *lenIndices)<=WIDTH)
void MarkDirty()
make sure balances are recalculated
Definition: wallet.h:446
std::map< CTxDestination, CKeyMetadata > mapKeyMetadata
Definition: wallet.h:773
CAmount GetImmatureCredit(bool fUseCache=true) const
Definition: wallet.cpp:1818
Coin Control Features.
Definition: coincontrol.h:16
CAmount GetImmatureBalance() const
Definition: wallet.cpp:2050
CWalletTx & AddTx(CRecipient recipient)
bool CreateTransaction(const std::vector< CRecipient > &vecSend, CWalletTx &wtxNew, CReserveKey &reservekey, CAmount &nFeeRet, int &nChangePosInOut, std::string &strFailReason, const CCoinControl &coin_control, bool sign=true, CAmount nGasFee=0, bool hasSender=false)
Create a new transaction paying the recipients with a set of coins selected by SelectCoins(); Also cr...
Definition: wallet.cpp:2650
CDBEnv bitdb
Definition: db.cpp:61
int64_t CAmount
Amount in lius (Can be negative)
Definition: amount.h:15
int64_t GetBlockTimeMax() const
Definition: chain.h:334
#define a(i)
#define RUN_TESTS
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:512
#define LOCK2(cs1, cs2)
Definition: sync.h:176
void MakeMock()
Definition: db.cpp:148
An instance of this class represents one database.
Definition: db.h:93
unsigned int nTime
Definition: chain.h:223
bool push_back(const UniValue &val)
Definition: univalue.cpp:110
void PruneOneBlockFile(const int fileNumber)
Mark one block file as pruned.
DBErrors LoadWallet(bool &fFirstRunRet)
Definition: wallet.cpp:3175
BOOST_AUTO_TEST_CASE(coin_selection_tests)
std::unique_ptr< CWallet > wallet
UniValue params
Definition: server.h:59
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:147
#define LOCK(cs)
Definition: sync.h:175
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:126
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune)
Actually unlink the specified files.
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:135
CChain chainActive
The currently-connected chain of blocks (protected by cs_main).
Definition: validation.cpp:80
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
std::vector< CTxOut > vout
Definition: transaction.h:393
void LockCoin(const COutPoint &output)
Definition: wallet.cpp:3755
bool AddToWallet(const CWalletTx &wtxIn, bool fFlushOnClose=true)
Definition: wallet.cpp:953
#define b(i, j)
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:287
UniValue importmulti(const JSONRPCRequest &request)
Definition: rpcdump.cpp:1030
std::map< CTxDestination, std::vector< COutput > > ListCoins() const
Return list of available coins and locked coins grouped by non-change output address.
Definition: wallet.cpp:2278
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:507
UniValue dumpwallet(const JSONRPCRequest &request)
Definition: rpcdump.cpp:588
Capture information about block/transaction validation.
Definition: validation.h:27
256-bit opaque blob.
Definition: uint256.h:132
const CWalletTx * GetWalletTx(const uint256 &hash) const
Definition: wallet.cpp:139
bool setArray()
Definition: univalue.cpp:96
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
A key allocated from the key pool.
Definition: wallet.h:1209
Testing setup and teardown for wallet.
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:177
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
#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:672
bool setObject()
Definition: univalue.cpp:103
std::map< uint256, CWalletTx > mapWallet
Definition: wallet.h:815
A mutable version of CTransaction.
Definition: transaction.h:390
std::vector< std::string > GetDestValues(const std::string &prefix) const
Get all destination values matching a prefix.
Definition: wallet.cpp:3950
CWallet * pwalletMain
std::set< CInputCoin > CoinSet
void clear()
Definition: univalue.cpp:17
An encapsulated private key.
Definition: key.h:35
bool AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
Adds a destination data tuple to the store, and saves it to disk.
Definition: wallet.cpp:3912
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
int nFile
Definition: chain.h:94
BOOST_FIXTURE_TEST_CASE(rescan, TestChain800Setup)
BlockMap mapBlockIndex
Definition: validation.cpp:79
uint256 GetBlockHash() const
Definition: chain.h:324
UniValue importwallet(const JSONRPCRequest &request)
Definition: rpcdump.cpp:446
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::vector< std::unique_ptr< CWalletTx > > wtxn
bool CommitTransaction(CWalletTx &wtxNew, CReserveKey &reservekey, CConnman *connman, CValidationState &state)
Call after CreateTransaction unless you want to abort.
Definition: wallet.cpp:3045
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
Definition: chain.h:181