Bitcoin Core  24.1.0
P2P Digital Currency
coins_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2021 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 <clientversion.h>
6 #include <coins.h>
7 #include <script/standard.h>
8 #include <streams.h>
10 #include <txdb.h>
11 #include <uint256.h>
12 #include <undo.h>
13 #include <util/strencodings.h>
14 
15 #include <map>
16 #include <vector>
17 
18 #include <boost/test/unit_test.hpp>
19 
20 int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out);
21 void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight);
22 
23 namespace
24 {
26 bool operator==(const Coin &a, const Coin &b) {
27  // Empty Coin objects are always equal.
28  if (a.IsSpent() && b.IsSpent()) return true;
29  return a.fCoinBase == b.fCoinBase &&
30  a.nHeight == b.nHeight &&
31  a.out == b.out;
32 }
33 
34 class CCoinsViewTest : public CCoinsView
35 {
36  uint256 hashBestBlock_;
37  std::map<COutPoint, Coin> map_;
38 
39 public:
40  [[nodiscard]] bool GetCoin(const COutPoint& outpoint, Coin& coin) const override
41  {
42  std::map<COutPoint, Coin>::const_iterator it = map_.find(outpoint);
43  if (it == map_.end()) {
44  return false;
45  }
46  coin = it->second;
47  if (coin.IsSpent() && InsecureRandBool() == 0) {
48  // Randomly return false in case of an empty entry.
49  return false;
50  }
51  return true;
52  }
53 
54  uint256 GetBestBlock() const override { return hashBestBlock_; }
55 
56  bool BatchWrite(CCoinsMap& mapCoins, const uint256& hashBlock) override
57  {
58  for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); ) {
59  if (it->second.flags & CCoinsCacheEntry::DIRTY) {
60  // Same optimization used in CCoinsViewDB is to only write dirty entries.
61  map_[it->first] = it->second.coin;
62  if (it->second.coin.IsSpent() && InsecureRandRange(3) == 0) {
63  // Randomly delete empty entries on write.
64  map_.erase(it->first);
65  }
66  }
67  mapCoins.erase(it++);
68  }
69  if (!hashBlock.IsNull())
70  hashBestBlock_ = hashBlock;
71  return true;
72  }
73 };
74 
75 class CCoinsViewCacheTest : public CCoinsViewCache
76 {
77 public:
78  explicit CCoinsViewCacheTest(CCoinsView* _base) : CCoinsViewCache(_base) {}
79 
80  void SelfTest() const
81  {
82  // Manually recompute the dynamic usage of the whole data, and compare it.
83  size_t ret = memusage::DynamicUsage(cacheCoins);
84  size_t count = 0;
85  for (const auto& entry : cacheCoins) {
86  ret += entry.second.coin.DynamicMemoryUsage();
87  ++count;
88  }
91  }
92 
93  CCoinsMap& map() const { return cacheCoins; }
94  size_t& usage() const { return cachedCoinsUsage; }
95 };
96 
97 } // namespace
98 
99 BOOST_FIXTURE_TEST_SUITE(coins_tests, BasicTestingSetup)
100 
101 static const unsigned int NUM_SIMULATION_ITERATIONS = 40000;
102 
103 // This is a large randomized insert/remove simulation test on a variable-size
104 // stack of caches on top of CCoinsViewTest.
105 //
106 // It will randomly create/update/delete Coin entries to a tip of caches, with
107 // txids picked from a limited list of random 256-bit hashes. Occasionally, a
108 // new tip is added to the stack of caches, or the tip is flushed and removed.
109 //
110 // During the process, booleans are kept to make sure that the randomized
111 // operation hits all branches.
112 //
113 // If fake_best_block is true, assign a random uint256 to mock the recording
114 // of best block on flush. This is necessary when using CCoinsViewDB as the base,
115 // otherwise we'll hit an assertion in BatchWrite.
116 //
117 void SimulationTest(CCoinsView* base, bool fake_best_block)
118 {
119  // Various coverage trackers.
120  bool removed_all_caches = false;
121  bool reached_4_caches = false;
122  bool added_an_entry = false;
123  bool added_an_unspendable_entry = false;
124  bool removed_an_entry = false;
125  bool updated_an_entry = false;
126  bool found_an_entry = false;
127  bool missed_an_entry = false;
128  bool uncached_an_entry = false;
129 
130  // A simple map to track what we expect the cache stack to represent.
131  std::map<COutPoint, Coin> result;
132 
133  // The cache stack.
134  std::vector<CCoinsViewCacheTest*> stack; // A stack of CCoinsViewCaches on top.
135  stack.push_back(new CCoinsViewCacheTest(base)); // Start with one cache.
136 
137  // Use a limited set of random transaction ids, so we do test overwriting entries.
138  std::vector<uint256> txids;
139  txids.resize(NUM_SIMULATION_ITERATIONS / 8);
140  for (unsigned int i = 0; i < txids.size(); i++) {
141  txids[i] = InsecureRand256();
142  }
143 
144  for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
145  // Do a random modification.
146  {
147  uint256 txid = txids[InsecureRandRange(txids.size())]; // txid we're going to modify in this iteration.
148  Coin& coin = result[COutPoint(txid, 0)];
149 
150  // Determine whether to test HaveCoin before or after Access* (or both). As these functions
151  // can influence each other's behaviour by pulling things into the cache, all combinations
152  // are tested.
153  bool test_havecoin_before = InsecureRandBits(2) == 0;
154  bool test_havecoin_after = InsecureRandBits(2) == 0;
155 
156  bool result_havecoin = test_havecoin_before ? stack.back()->HaveCoin(COutPoint(txid, 0)) : false;
157  const Coin& entry = (InsecureRandRange(500) == 0) ? AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
158  BOOST_CHECK(coin == entry);
159  BOOST_CHECK(!test_havecoin_before || result_havecoin == !entry.IsSpent());
160 
161  if (test_havecoin_after) {
162  bool ret = stack.back()->HaveCoin(COutPoint(txid, 0));
163  BOOST_CHECK(ret == !entry.IsSpent());
164  }
165 
166  if (InsecureRandRange(5) == 0 || coin.IsSpent()) {
167  Coin newcoin;
168  newcoin.out.nValue = InsecureRand32();
169  newcoin.nHeight = 1;
170  if (InsecureRandRange(16) == 0 && coin.IsSpent()) {
173  added_an_unspendable_entry = true;
174  } else {
175  newcoin.out.scriptPubKey.assign(InsecureRandBits(6), 0); // Random sizes so we can test memory usage accounting
176  (coin.IsSpent() ? added_an_entry : updated_an_entry) = true;
177  coin = newcoin;
178  }
179  stack.back()->AddCoin(COutPoint(txid, 0), std::move(newcoin), !coin.IsSpent() || InsecureRand32() & 1);
180  } else {
181  removed_an_entry = true;
182  coin.Clear();
183  BOOST_CHECK(stack.back()->SpendCoin(COutPoint(txid, 0)));
184  }
185  }
186 
187  // One every 10 iterations, remove a random entry from the cache
188  if (InsecureRandRange(10) == 0) {
189  COutPoint out(txids[InsecureRand32() % txids.size()], 0);
190  int cacheid = InsecureRand32() % stack.size();
191  stack[cacheid]->Uncache(out);
192  uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out);
193  }
194 
195  // Once every 1000 iterations and at the end, verify the full cache.
196  if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
197  for (const auto& entry : result) {
198  bool have = stack.back()->HaveCoin(entry.first);
199  const Coin& coin = stack.back()->AccessCoin(entry.first);
200  BOOST_CHECK(have == !coin.IsSpent());
201  BOOST_CHECK(coin == entry.second);
202  if (coin.IsSpent()) {
203  missed_an_entry = true;
204  } else {
205  BOOST_CHECK(stack.back()->HaveCoinInCache(entry.first));
206  found_an_entry = true;
207  }
208  }
209  for (const CCoinsViewCacheTest *test : stack) {
210  test->SelfTest();
211  }
212  }
213 
214  if (InsecureRandRange(100) == 0) {
215  // Every 100 iterations, flush an intermediate cache
216  if (stack.size() > 1 && InsecureRandBool() == 0) {
217  unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
218  if (fake_best_block) stack[flushIndex]->SetBestBlock(InsecureRand256());
219  BOOST_CHECK(stack[flushIndex]->Flush());
220  }
221  }
222  if (InsecureRandRange(100) == 0) {
223  // Every 100 iterations, change the cache stack.
224  if (stack.size() > 0 && InsecureRandBool() == 0) {
225  //Remove the top cache
226  if (fake_best_block) stack.back()->SetBestBlock(InsecureRand256());
227  BOOST_CHECK(stack.back()->Flush());
228  delete stack.back();
229  stack.pop_back();
230  }
231  if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
232  //Add a new cache
233  CCoinsView* tip = base;
234  if (stack.size() > 0) {
235  tip = stack.back();
236  } else {
237  removed_all_caches = true;
238  }
239  stack.push_back(new CCoinsViewCacheTest(tip));
240  if (stack.size() == 4) {
241  reached_4_caches = true;
242  }
243  }
244  }
245  }
246 
247  // Clean up the stack.
248  while (stack.size() > 0) {
249  delete stack.back();
250  stack.pop_back();
251  }
252 
253  // Verify coverage.
254  BOOST_CHECK(removed_all_caches);
255  BOOST_CHECK(reached_4_caches);
256  BOOST_CHECK(added_an_entry);
257  BOOST_CHECK(added_an_unspendable_entry);
258  BOOST_CHECK(removed_an_entry);
259  BOOST_CHECK(updated_an_entry);
260  BOOST_CHECK(found_an_entry);
261  BOOST_CHECK(missed_an_entry);
262  BOOST_CHECK(uncached_an_entry);
263 }
264 
265 // Run the above simulation for multiple base types.
266 BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
267 {
268  CCoinsViewTest base;
269  SimulationTest(&base, false);
270 
271  CCoinsViewDB db_base{"test", /*nCacheSize=*/1 << 23, /*fMemory=*/true, /*fWipe=*/false};
272  SimulationTest(&db_base, true);
273 }
274 
275 // Store of all necessary tx and undo data for next test
276 typedef std::map<COutPoint, std::tuple<CTransaction,CTxUndo,Coin>> UtxoData;
278 
279 UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
280  assert(utxoSet.size());
281  auto utxoSetIt = utxoSet.lower_bound(COutPoint(InsecureRand256(), 0));
282  if (utxoSetIt == utxoSet.end()) {
283  utxoSetIt = utxoSet.begin();
284  }
285  auto utxoDataIt = utxoData.find(*utxoSetIt);
286  assert(utxoDataIt != utxoData.end());
287  return utxoDataIt;
288 }
289 
290 
291 // This test is similar to the previous test
292 // except the emphasis is on testing the functionality of UpdateCoins
293 // random txs are created and UpdateCoins is used to update the cache stack
294 // In particular it is tested that spending a duplicate coinbase tx
295 // has the expected effect (the other duplicate is overwritten at all cache levels)
296 BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
297 {
300 
301  bool spent_a_duplicate_coinbase = false;
302  // A simple map to track what we expect the cache stack to represent.
303  std::map<COutPoint, Coin> result;
304 
305  // The cache stack.
306  CCoinsViewTest base; // A CCoinsViewTest at the bottom.
307  std::vector<CCoinsViewCacheTest*> stack; // A stack of CCoinsViewCaches on top.
308  stack.push_back(new CCoinsViewCacheTest(&base)); // Start with one cache.
309 
310  // Track the txids we've used in various sets
311  std::set<COutPoint> coinbase_coins;
312  std::set<COutPoint> disconnected_coins;
313  std::set<COutPoint> duplicate_coins;
314  std::set<COutPoint> utxoset;
315 
316  for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
317  uint32_t randiter = InsecureRand32();
318 
319  // 19/20 txs add a new transaction
320  if (randiter % 20 < 19) {
322  tx.vin.resize(1);
323  tx.vout.resize(1);
324  tx.vout[0].nValue = i; //Keep txs unique unless intended to duplicate
325  tx.vout[0].scriptPubKey.assign(InsecureRand32() & 0x3F, 0); // Random sizes so we can test memory usage accounting
326  const int height{int(InsecureRand32() >> 1)};
327  Coin old_coin;
328 
329  // 2/20 times create a new coinbase
330  if (randiter % 20 < 2 || coinbase_coins.size() < 10) {
331  // 1/10 of those times create a duplicate coinbase
332  if (InsecureRandRange(10) == 0 && coinbase_coins.size()) {
333  auto utxod = FindRandomFrom(coinbase_coins);
334  // Reuse the exact same coinbase
335  tx = CMutableTransaction{std::get<0>(utxod->second)};
336  // shouldn't be available for reconnection if it's been duplicated
337  disconnected_coins.erase(utxod->first);
338 
339  duplicate_coins.insert(utxod->first);
340  }
341  else {
342  coinbase_coins.insert(COutPoint(tx.GetHash(), 0));
343  }
344  assert(CTransaction(tx).IsCoinBase());
345  }
346 
347  // 17/20 times reconnect previous or add a regular tx
348  else {
349 
350  COutPoint prevout;
351  // 1/20 times reconnect a previously disconnected tx
352  if (randiter % 20 == 2 && disconnected_coins.size()) {
353  auto utxod = FindRandomFrom(disconnected_coins);
354  tx = CMutableTransaction{std::get<0>(utxod->second)};
355  prevout = tx.vin[0].prevout;
356  if (!CTransaction(tx).IsCoinBase() && !utxoset.count(prevout)) {
357  disconnected_coins.erase(utxod->first);
358  continue;
359  }
360 
361  // If this tx is already IN the UTXO, then it must be a coinbase, and it must be a duplicate
362  if (utxoset.count(utxod->first)) {
363  assert(CTransaction(tx).IsCoinBase());
364  assert(duplicate_coins.count(utxod->first));
365  }
366  disconnected_coins.erase(utxod->first);
367  }
368 
369  // 16/20 times create a regular tx
370  else {
371  auto utxod = FindRandomFrom(utxoset);
372  prevout = utxod->first;
373 
374  // Construct the tx to spend the coins of prevouthash
375  tx.vin[0].prevout = prevout;
376  assert(!CTransaction(tx).IsCoinBase());
377  }
378  // In this simple test coins only have two states, spent or unspent, save the unspent state to restore
379  old_coin = result[prevout];
380  // Update the expected result of prevouthash to know these coins are spent
381  result[prevout].Clear();
382 
383  utxoset.erase(prevout);
384 
385  // The test is designed to ensure spending a duplicate coinbase will work properly
386  // if that ever happens and not resurrect the previously overwritten coinbase
387  if (duplicate_coins.count(prevout)) {
388  spent_a_duplicate_coinbase = true;
389  }
390 
391  }
392  // Update the expected result to know about the new output coins
393  assert(tx.vout.size() == 1);
394  const COutPoint outpoint(tx.GetHash(), 0);
395  result[outpoint] = Coin{tx.vout[0], height, CTransaction{tx}.IsCoinBase()};
396 
397  // Call UpdateCoins on the top cache
398  CTxUndo undo;
399  UpdateCoins(CTransaction{tx}, *(stack.back()), undo, height);
400 
401  // Update the utxo set for future spends
402  utxoset.insert(outpoint);
403 
404  // Track this tx and undo info to use later
405  utxoData.emplace(outpoint, std::make_tuple(tx,undo,old_coin));
406  } else if (utxoset.size()) {
407  //1/20 times undo a previous transaction
408  auto utxod = FindRandomFrom(utxoset);
409 
410  CTransaction &tx = std::get<0>(utxod->second);
411  CTxUndo &undo = std::get<1>(utxod->second);
412  Coin &orig_coin = std::get<2>(utxod->second);
413 
414  // Update the expected result
415  // Remove new outputs
416  result[utxod->first].Clear();
417  // If not coinbase restore prevout
418  if (!tx.IsCoinBase()) {
419  result[tx.vin[0].prevout] = orig_coin;
420  }
421 
422  // Disconnect the tx from the current UTXO
423  // See code in DisconnectBlock
424  // remove outputs
425  BOOST_CHECK(stack.back()->SpendCoin(utxod->first));
426  // restore inputs
427  if (!tx.IsCoinBase()) {
428  const COutPoint &out = tx.vin[0].prevout;
429  Coin coin = undo.vprevout[0];
430  ApplyTxInUndo(std::move(coin), *(stack.back()), out);
431  }
432  // Store as a candidate for reconnection
433  disconnected_coins.insert(utxod->first);
434 
435  // Update the utxoset
436  utxoset.erase(utxod->first);
437  if (!tx.IsCoinBase())
438  utxoset.insert(tx.vin[0].prevout);
439  }
440 
441  // Once every 1000 iterations and at the end, verify the full cache.
442  if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
443  for (const auto& entry : result) {
444  bool have = stack.back()->HaveCoin(entry.first);
445  const Coin& coin = stack.back()->AccessCoin(entry.first);
446  BOOST_CHECK(have == !coin.IsSpent());
447  BOOST_CHECK(coin == entry.second);
448  }
449  }
450 
451  // One every 10 iterations, remove a random entry from the cache
452  if (utxoset.size() > 1 && InsecureRandRange(30) == 0) {
453  stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
454  }
455  if (disconnected_coins.size() > 1 && InsecureRandRange(30) == 0) {
456  stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
457  }
458  if (duplicate_coins.size() > 1 && InsecureRandRange(30) == 0) {
459  stack[InsecureRand32() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
460  }
461 
462  if (InsecureRandRange(100) == 0) {
463  // Every 100 iterations, flush an intermediate cache
464  if (stack.size() > 1 && InsecureRandBool() == 0) {
465  unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
466  BOOST_CHECK(stack[flushIndex]->Flush());
467  }
468  }
469  if (InsecureRandRange(100) == 0) {
470  // Every 100 iterations, change the cache stack.
471  if (stack.size() > 0 && InsecureRandBool() == 0) {
472  BOOST_CHECK(stack.back()->Flush());
473  delete stack.back();
474  stack.pop_back();
475  }
476  if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) {
477  CCoinsView* tip = &base;
478  if (stack.size() > 0) {
479  tip = stack.back();
480  }
481  stack.push_back(new CCoinsViewCacheTest(tip));
482  }
483  }
484  }
485 
486  // Clean up the stack.
487  while (stack.size() > 0) {
488  delete stack.back();
489  stack.pop_back();
490  }
491 
492  // Verify coverage.
493  BOOST_CHECK(spent_a_duplicate_coinbase);
494 
496 }
497 
498 BOOST_AUTO_TEST_CASE(ccoins_serialization)
499 {
500  // Good example
501  CDataStream ss1(ParseHex("97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35"), SER_DISK, CLIENT_VERSION);
502  Coin cc1;
503  ss1 >> cc1;
504  BOOST_CHECK_EQUAL(cc1.fCoinBase, false);
505  BOOST_CHECK_EQUAL(cc1.nHeight, 203998U);
506  BOOST_CHECK_EQUAL(cc1.out.nValue, CAmount{60000000000});
507  BOOST_CHECK_EQUAL(HexStr(cc1.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160(ParseHex("816115944e077fe7c803cfa57f29b36bf87c1d35"))))));
508 
509  // Good example
510  CDataStream ss2(ParseHex("8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4"), SER_DISK, CLIENT_VERSION);
511  Coin cc2;
512  ss2 >> cc2;
513  BOOST_CHECK_EQUAL(cc2.fCoinBase, true);
514  BOOST_CHECK_EQUAL(cc2.nHeight, 120891U);
515  BOOST_CHECK_EQUAL(cc2.out.nValue, 110397);
516  BOOST_CHECK_EQUAL(HexStr(cc2.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160(ParseHex("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"))))));
517 
518  // Smallest possible example
519  CDataStream ss3(ParseHex("000006"), SER_DISK, CLIENT_VERSION);
520  Coin cc3;
521  ss3 >> cc3;
522  BOOST_CHECK_EQUAL(cc3.fCoinBase, false);
523  BOOST_CHECK_EQUAL(cc3.nHeight, 0U);
524  BOOST_CHECK_EQUAL(cc3.out.nValue, 0);
525  BOOST_CHECK_EQUAL(cc3.out.scriptPubKey.size(), 0U);
526 
527  // scriptPubKey that ends beyond the end of the stream
528  CDataStream ss4(ParseHex("000007"), SER_DISK, CLIENT_VERSION);
529  try {
530  Coin cc4;
531  ss4 >> cc4;
532  BOOST_CHECK_MESSAGE(false, "We should have thrown");
533  } catch (const std::ios_base::failure&) {
534  }
535 
536  // Very large scriptPubKey (3*10^9 bytes) past the end of the stream
538  uint64_t x = 3000000000ULL;
539  tmp << VARINT(x);
540  BOOST_CHECK_EQUAL(HexStr(tmp), "8a95c0bb00");
541  CDataStream ss5(ParseHex("00008a95c0bb00"), SER_DISK, CLIENT_VERSION);
542  try {
543  Coin cc5;
544  ss5 >> cc5;
545  BOOST_CHECK_MESSAGE(false, "We should have thrown");
546  } catch (const std::ios_base::failure&) {
547  }
548 }
549 
550 const static COutPoint OUTPOINT;
551 const static CAmount SPENT = -1;
552 const static CAmount ABSENT = -2;
553 const static CAmount FAIL = -3;
554 const static CAmount VALUE1 = 100;
555 const static CAmount VALUE2 = 200;
556 const static CAmount VALUE3 = 300;
557 const static char DIRTY = CCoinsCacheEntry::DIRTY;
558 const static char FRESH = CCoinsCacheEntry::FRESH;
559 const static char NO_ENTRY = -1;
560 
561 const static auto FLAGS = {char(0), FRESH, DIRTY, char(DIRTY | FRESH)};
562 const static auto CLEAN_FLAGS = {char(0), FRESH};
563 const static auto ABSENT_FLAGS = {NO_ENTRY};
564 
565 static void SetCoinsValue(CAmount value, Coin& coin)
566 {
567  assert(value != ABSENT);
568  coin.Clear();
569  assert(coin.IsSpent());
570  if (value != SPENT) {
571  coin.out.nValue = value;
572  coin.nHeight = 1;
573  assert(!coin.IsSpent());
574  }
575 }
576 
577 static size_t InsertCoinsMapEntry(CCoinsMap& map, CAmount value, char flags)
578 {
579  if (value == ABSENT) {
580  assert(flags == NO_ENTRY);
581  return 0;
582  }
583  assert(flags != NO_ENTRY);
584  CCoinsCacheEntry entry;
585  entry.flags = flags;
586  SetCoinsValue(value, entry.coin);
587  auto inserted = map.emplace(OUTPOINT, std::move(entry));
588  assert(inserted.second);
589  return inserted.first->second.coin.DynamicMemoryUsage();
590 }
591 
592 void GetCoinsMapEntry(const CCoinsMap& map, CAmount& value, char& flags)
593 {
594  auto it = map.find(OUTPOINT);
595  if (it == map.end()) {
596  value = ABSENT;
597  flags = NO_ENTRY;
598  } else {
599  if (it->second.coin.IsSpent()) {
600  value = SPENT;
601  } else {
602  value = it->second.coin.out.nValue;
603  }
604  flags = it->second.flags;
605  assert(flags != NO_ENTRY);
606  }
607 }
608 
609 void WriteCoinsViewEntry(CCoinsView& view, CAmount value, char flags)
610 {
611  CCoinsMap map;
612  InsertCoinsMapEntry(map, value, flags);
613  BOOST_CHECK(view.BatchWrite(map, {}));
614 }
615 
617 {
618 public:
619  SingleEntryCacheTest(CAmount base_value, CAmount cache_value, char cache_flags)
620  {
621  WriteCoinsViewEntry(base, base_value, base_value == ABSENT ? NO_ENTRY : DIRTY);
622  cache.usage() += InsertCoinsMapEntry(cache.map(), cache_value, cache_flags);
623  }
624 
626  CCoinsViewCacheTest base{&root};
627  CCoinsViewCacheTest cache{&base};
628 };
629 
630 static void CheckAccessCoin(CAmount base_value, CAmount cache_value, CAmount expected_value, char cache_flags, char expected_flags)
631 {
632  SingleEntryCacheTest test(base_value, cache_value, cache_flags);
633  test.cache.AccessCoin(OUTPOINT);
634  test.cache.SelfTest();
635 
636  CAmount result_value;
637  char result_flags;
638  GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
639  BOOST_CHECK_EQUAL(result_value, expected_value);
640  BOOST_CHECK_EQUAL(result_flags, expected_flags);
641 }
642 
643 BOOST_AUTO_TEST_CASE(ccoins_access)
644 {
645  /* Check AccessCoin behavior, requesting a coin from a cache view layered on
646  * top of a base view, and checking the resulting entry in the cache after
647  * the access.
648  *
649  * Base Cache Result Cache Result
650  * Value Value Value Flags Flags
651  */
653  CheckAccessCoin(ABSENT, SPENT , SPENT , 0 , 0 );
657  CheckAccessCoin(ABSENT, VALUE2, VALUE2, 0 , 0 );
662  CheckAccessCoin(SPENT , SPENT , SPENT , 0 , 0 );
666  CheckAccessCoin(SPENT , VALUE2, VALUE2, 0 , 0 );
671  CheckAccessCoin(VALUE1, SPENT , SPENT , 0 , 0 );
675  CheckAccessCoin(VALUE1, VALUE2, VALUE2, 0 , 0 );
679 }
680 
681 static void CheckSpendCoins(CAmount base_value, CAmount cache_value, CAmount expected_value, char cache_flags, char expected_flags)
682 {
683  SingleEntryCacheTest test(base_value, cache_value, cache_flags);
684  test.cache.SpendCoin(OUTPOINT);
685  test.cache.SelfTest();
686 
687  CAmount result_value;
688  char result_flags;
689  GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
690  BOOST_CHECK_EQUAL(result_value, expected_value);
691  BOOST_CHECK_EQUAL(result_flags, expected_flags);
692 };
693 
694 BOOST_AUTO_TEST_CASE(ccoins_spend)
695 {
696  /* Check SpendCoin behavior, requesting a coin from a cache view layered on
697  * top of a base view, spending, and then checking
698  * the resulting entry in the cache after the modification.
699  *
700  * Base Cache Result Cache Result
701  * Value Value Value Flags Flags
702  */
713  CheckSpendCoins(SPENT , SPENT , SPENT , 0 , DIRTY );
730 }
731 
732 static void CheckAddCoinBase(CAmount base_value, CAmount cache_value, CAmount modify_value, CAmount expected_value, char cache_flags, char expected_flags, bool coinbase)
733 {
734  SingleEntryCacheTest test(base_value, cache_value, cache_flags);
735 
736  CAmount result_value;
737  char result_flags;
738  try {
739  CTxOut output;
740  output.nValue = modify_value;
741  test.cache.AddCoin(OUTPOINT, Coin(std::move(output), 1, coinbase), coinbase);
742  test.cache.SelfTest();
743  GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
744  } catch (std::logic_error&) {
745  result_value = FAIL;
746  result_flags = NO_ENTRY;
747  }
748 
749  BOOST_CHECK_EQUAL(result_value, expected_value);
750  BOOST_CHECK_EQUAL(result_flags, expected_flags);
751 }
752 
753 // Simple wrapper for CheckAddCoinBase function above that loops through
754 // different possible base_values, making sure each one gives the same results.
755 // This wrapper lets the coins_add test below be shorter and less repetitive,
756 // while still verifying that the CoinsViewCache::AddCoin implementation
757 // ignores base values.
758 template <typename... Args>
759 static void CheckAddCoin(Args&&... args)
760 {
761  for (const CAmount base_value : {ABSENT, SPENT, VALUE1})
762  CheckAddCoinBase(base_value, std::forward<Args>(args)...);
763 }
764 
766 {
767  /* Check AddCoin behavior, requesting a new coin from a cache view,
768  * writing a modification to the coin, and then checking the resulting
769  * entry in the cache after the modification. Verify behavior with the
770  * AddCoin possible_overwrite argument set to false, and to true.
771  *
772  * Cache Write Result Cache Result possible_overwrite
773  * Value Value Value Flags Flags
774  */
777  CheckAddCoin(SPENT , VALUE3, VALUE3, 0 , DIRTY|FRESH, false);
778  CheckAddCoin(SPENT , VALUE3, VALUE3, 0 , DIRTY , true );
781  CheckAddCoin(SPENT , VALUE3, VALUE3, DIRTY , DIRTY , false);
782  CheckAddCoin(SPENT , VALUE3, VALUE3, DIRTY , DIRTY , true );
785  CheckAddCoin(VALUE2, VALUE3, FAIL , 0 , NO_ENTRY , false);
786  CheckAddCoin(VALUE2, VALUE3, VALUE3, 0 , DIRTY , true );
787  CheckAddCoin(VALUE2, VALUE3, FAIL , FRESH , NO_ENTRY , false);
789  CheckAddCoin(VALUE2, VALUE3, FAIL , DIRTY , NO_ENTRY , false);
790  CheckAddCoin(VALUE2, VALUE3, VALUE3, DIRTY , DIRTY , true );
793 }
794 
795 void CheckWriteCoins(CAmount parent_value, CAmount child_value, CAmount expected_value, char parent_flags, char child_flags, char expected_flags)
796 {
797  SingleEntryCacheTest test(ABSENT, parent_value, parent_flags);
798 
799  CAmount result_value;
800  char result_flags;
801  try {
802  WriteCoinsViewEntry(test.cache, child_value, child_flags);
803  test.cache.SelfTest();
804  GetCoinsMapEntry(test.cache.map(), result_value, result_flags);
805  } catch (std::logic_error&) {
806  result_value = FAIL;
807  result_flags = NO_ENTRY;
808  }
809 
810  BOOST_CHECK_EQUAL(result_value, expected_value);
811  BOOST_CHECK_EQUAL(result_flags, expected_flags);
812 }
813 
814 BOOST_AUTO_TEST_CASE(ccoins_write)
815 {
816  /* Check BatchWrite behavior, flushing one entry from a child cache to a
817  * parent cache, and checking the resulting entry in the parent cache
818  * after the write.
819  *
820  * Parent Child Result Parent Child Result
821  * Value Value Value Flags Flags Flags
822  */
828  CheckWriteCoins(SPENT , ABSENT, SPENT , 0 , NO_ENTRY , 0 );
832  CheckWriteCoins(SPENT , SPENT , SPENT , 0 , DIRTY , DIRTY );
868 
869  // The checks above omit cases where the child flags are not DIRTY, since
870  // they would be too repetitive (the parent cache is never updated in these
871  // cases). The loop below covers these cases and makes sure the parent cache
872  // is always left unchanged.
873  for (const CAmount parent_value : {ABSENT, SPENT, VALUE1})
874  for (const CAmount child_value : {ABSENT, SPENT, VALUE2})
875  for (const char parent_flags : parent_value == ABSENT ? ABSENT_FLAGS : FLAGS)
876  for (const char child_flags : child_value == ABSENT ? ABSENT_FLAGS : CLEAN_FLAGS)
877  CheckWriteCoins(parent_value, child_value, parent_value, parent_flags, child_flags, parent_flags);
878 }
879 
CAmount nValue
Definition: transaction.h:159
const Coin & AccessByTxid(const CCoinsViewCache &view, const uint256 &txid)
Utility function to find any unspent output with a given txid.
Definition: coins.cpp:284
static const unsigned int NUM_SIMULATION_ITERATIONS
#define VARINT(obj)
Definition: serialize.h:436
static bool InsecureRandBool()
Definition: setup_common.h:76
bool IsSpent() const
Either this coin never existed (see e.g.
Definition: coins.h:79
int ret
static const auto ABSENT_FLAGS
int ApplyTxInUndo(Coin &&undo, CCoinsViewCache &view, const COutPoint &out)
Restore the UTXO in a Coin at a given COutPoint.
static const auto FLAGS
void assign(size_type n, const T &val)
Definition: prevector.h:220
static const CAmount ABSENT
A Coin in one level of the coins database caching hierarchy.
Definition: coins.h:103
assert(!tx.IsCoinBase())
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: coins.cpp:13
CScript scriptPubKey
Definition: transaction.h:160
bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:630
A UTXO entry.
Definition: coins.h:30
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition: memusage.h:29
std::vector< CTxIn > vin
Definition: transaction.h:374
unsigned int nHeight
static const CAmount SPENT
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
Definition: coins.cpp:37
CTxOut out
unspent transaction output
Definition: coins.h:34
virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock)
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: coins.cpp:16
unsigned int fCoinBase
whether containing transaction was a coinbase
Definition: coins.h:37
void CheckWriteCoins(CAmount parent_value, CAmount child_value, CAmount expected_value, char parent_flags, char child_flags, char expected_flags)
static void CheckSpendCoins(CAmount base_value, CAmount cache_value, CAmount expected_value, char cache_flags, char expected_flags)
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:185
static uint32_t InsecureRand32()
Definition: setup_common.h:72
UtxoData utxoData
static const CAmount VALUE2
bool IsNull() const
Definition: uint256.h:34
bool IsCoinBase() const
Definition: transaction.h:343
ArgsManager args
static const char NO_ENTRY
static void CheckAccessCoin(CAmount base_value, CAmount cache_value, CAmount expected_value, char cache_flags, char expected_flags)
const std::vector< CTxIn > vin
Definition: transaction.h:298
static const CAmount VALUE1
BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
DIRTY means the CCoinsCacheEntry is potentially different from the version in the parent cache...
Definition: coins.h:116
Basic testing setup.
Definition: setup_common.h:83
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
Definition: script.h:549
static void SeedInsecureRand(SeedRand seed=SeedRand::SEED)
Definition: setup_common.h:63
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
uint32_t nHeight
at which height this containing transaction was included in the active block chain ...
Definition: coins.h:40
static size_t InsertCoinsMapEntry(CCoinsMap &map, CAmount value, char flags)
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
Definition: coins.cpp:257
CCoinsMap cacheCoins
Definition: coins.h:220
std::vector< Byte > ParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
static uint64_t InsecureRandRange(uint64_t range)
Definition: setup_common.h:75
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Abstract view on the open txout dataset.
Definition: coins.h:156
BOOST_AUTO_TEST_SUITE_END()
static void SetCoinsValue(CAmount value, Coin &coin)
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:134
CCoinsView root
void WriteCoinsViewEntry(CCoinsView &view, CAmount value, char flags)
static uint64_t InsecureRandBits(int bits)
Definition: setup_common.h:74
An output of a transaction.
Definition: transaction.h:156
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:334
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:34
std::vector< CTxOut > vout
Definition: transaction.h:375
SingleEntryCacheTest(CAmount base_value, CAmount cache_value, char cache_flags)
void UpdateCoins(const CTransaction &tx, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight)
static const CAmount FAIL
void SimulationTest(CCoinsView *base, bool fake_best_block)
int flags
Definition: bitcoin-tx.cpp:525
UtxoData::iterator FindRandomFrom(const std::set< COutPoint > &utxoSet)
std::map< COutPoint, std::tuple< CTransaction, CTxUndo, Coin > > UtxoData
256-bit opaque blob.
Definition: uint256.h:119
static const COutPoint OUTPOINT
static void CheckAddCoin(Args &&... args)
FRESH means the parent cache does not have this coin or that it is a spent coin in the parent cache...
Definition: coins.h:126
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:68
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
Undo information for a CTransaction.
Definition: undo.h:53
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:50
static const CAmount VALUE3
virtual uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:14
static const char DIRTY
CCoinsViewCacheTest base
static void CheckAddCoinBase(CAmount base_value, CAmount cache_value, CAmount modify_value, CAmount expected_value, char cache_flags, char expected_flags, bool coinbase)
160-bit opaque blob.
Definition: uint256.h:108
static int count
Definition: tests.c:33
size_t cachedCoinsUsage
Definition: coins.h:223
void Clear()
Definition: coins.h:46
A mutable version of CTransaction.
Definition: transaction.h:372
static const auto CLEAN_FLAGS
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:287
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:212
bool g_mock_deterministic_tests
Flag to make GetRand in random.h return the same number.
Definition: random.cpp:585
static uint256 InsecureRand256()
Definition: setup_common.h:73
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
unsigned char flags
Definition: coins.h:106
CCoinsViewCacheTest cache
void GetCoinsMapEntry(const CCoinsMap &map, CAmount &value, char &flags)
Seed with a compile time constant of zeros.
static const char FRESH
Coin coin
Definition: coins.h:105
#define BOOST_CHECK(expr)
Definition: object.cpp:16