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