Bitcoin Core  31.0.0
P2P Digital Currency
coins_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-present 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 <addresstype.h>
6 #include <clientversion.h>
7 #include <coins.h>
8 #include <streams.h>
9 #include <test/util/common.h>
11 #include <test/util/random.h>
12 #include <test/util/setup_common.h>
13 #include <txdb.h>
14 #include <uint256.h>
15 #include <undo.h>
16 #include <util/strencodings.h>
17 
18 #include <map>
19 #include <string>
20 #include <variant>
21 #include <vector>
22 
23 #include <boost/test/unit_test.hpp>
24 
25 using namespace util::hex_literals;
26 
27 int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out);
28 void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight);
29 
30 namespace
31 {
33 bool operator==(const Coin &a, const Coin &b) {
34  // Empty Coin objects are always equal.
35  if (a.IsSpent() && b.IsSpent()) return true;
36  return a.fCoinBase == b.fCoinBase &&
37  a.nHeight == b.nHeight &&
38  a.out == b.out;
39 }
40 
41 class CCoinsViewTest : public CCoinsView
42 {
43  FastRandomContext& m_rng;
44  uint256 hashBestBlock_;
45  std::map<COutPoint, Coin> map_;
46 
47 public:
48  CCoinsViewTest(FastRandomContext& rng) : m_rng{rng} {}
49 
50  std::optional<Coin> GetCoin(const COutPoint& outpoint) const override
51  {
52  if (auto it{map_.find(outpoint)}; it != map_.end() && !it->second.IsSpent()) return it->second;
53  return std::nullopt;
54  }
55 
56  uint256 GetBestBlock() const override { return hashBestBlock_; }
57 
58  void BatchWrite(CoinsViewCacheCursor& cursor, const uint256& hashBlock) override
59  {
60  for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)){
61  if (it->second.IsDirty()) {
62  // Same optimization used in CCoinsViewDB is to only write dirty entries.
63  map_[it->first] = it->second.coin;
64  if (it->second.coin.IsSpent() && m_rng.randrange(3) == 0) {
65  // Randomly delete empty entries on write.
66  map_.erase(it->first);
67  }
68  }
69  }
70  if (!hashBlock.IsNull())
71  hashBestBlock_ = hashBlock;
72  }
73 };
74 
75 class CCoinsViewCacheTest : public CCoinsViewCache
76 {
77 public:
78  explicit CCoinsViewCacheTest(CCoinsView* _base) : CCoinsViewCache(_base) {}
79 
80  void SelfTest(bool sanity_check = true) 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  }
89  BOOST_CHECK_EQUAL(GetCacheSize(), count);
90  BOOST_CHECK_EQUAL(DynamicMemoryUsage(), ret);
91  if (sanity_check) {
92  SanityCheck();
93  }
94  }
95 
96  CCoinsMap& map() const { return cacheCoins; }
97  CoinsCachePair& sentinel() const { return m_sentinel; }
98  size_t& usage() const { return cachedCoinsUsage; }
99  size_t& dirty() const { return m_dirty_count; }
100 };
101 
102 } // namespace
103 
104 static const unsigned int NUM_SIMULATION_ITERATIONS = 40000;
105 
107 // This is a large randomized insert/remove simulation test on a variable-size
108 // stack of caches on top of CCoinsViewTest.
109 //
110 // It will randomly create/update/delete Coin entries to a tip of caches, with
111 // txids picked from a limited list of random 256-bit hashes. Occasionally, a
112 // new tip is added to the stack of caches, or the tip is flushed and removed.
113 //
114 // During the process, booleans are kept to make sure that the randomized
115 // operation hits all branches.
116 //
117 // If fake_best_block is true, assign a random uint256 to mock the recording
118 // of best block on flush. This is necessary when using CCoinsViewDB as the base,
119 // otherwise we'll hit an assertion in BatchWrite.
120 //
121 void SimulationTest(CCoinsView* base, bool fake_best_block)
122 {
123  // Various coverage trackers.
124  bool removed_all_caches = false;
125  bool reached_4_caches = false;
126  bool added_an_entry = false;
127  bool added_an_unspendable_entry = false;
128  bool removed_an_entry = false;
129  bool updated_an_entry = false;
130  bool found_an_entry = false;
131  bool missed_an_entry = false;
132  bool uncached_an_entry = false;
133  bool flushed_without_erase = false;
134 
135  // A simple map to track what we expect the cache stack to represent.
136  std::map<COutPoint, Coin> result;
137 
138  // The cache stack.
139  std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
140  stack.push_back(std::make_unique<CCoinsViewCacheTest>(base)); // Start with one cache.
141 
142  // Use a limited set of random transaction ids, so we do test overwriting entries.
143  std::vector<Txid> txids;
144  txids.resize(NUM_SIMULATION_ITERATIONS / 8);
145  for (unsigned int i = 0; i < txids.size(); i++) {
146  txids[i] = Txid::FromUint256(m_rng.rand256());
147  }
148 
149  for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
150  // Do a random modification.
151  {
152  auto txid = txids[m_rng.randrange(txids.size())]; // txid we're going to modify in this iteration.
153  Coin& coin = result[COutPoint(txid, 0)];
154 
155  // Determine whether to test HaveCoin before or after Access* (or both). As these functions
156  // can influence each other's behaviour by pulling things into the cache, all combinations
157  // are tested.
158  bool test_havecoin_before = m_rng.randbits(2) == 0;
159  bool test_havecoin_after = m_rng.randbits(2) == 0;
160 
161  bool result_havecoin = test_havecoin_before ? stack.back()->HaveCoin(COutPoint(txid, 0)) : false;
162 
163  // Infrequently, test usage of AccessByTxid instead of AccessCoin - the
164  // former just delegates to the latter and returns the first unspent in a txn.
165  const Coin& entry = (m_rng.randrange(500) == 0) ?
166  AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
167  BOOST_CHECK(coin == entry);
168 
169  if (test_havecoin_before) {
170  BOOST_CHECK(result_havecoin == !entry.IsSpent());
171  }
172 
173  if (test_havecoin_after) {
174  bool ret = stack.back()->HaveCoin(COutPoint(txid, 0));
175  BOOST_CHECK(ret == !entry.IsSpent());
176  }
177 
178  if (m_rng.randrange(5) == 0 || coin.IsSpent()) {
179  Coin newcoin;
180  newcoin.out.nValue = RandMoney(m_rng);
181  newcoin.nHeight = 1;
182 
183  // Infrequently test adding unspendable coins.
184  if (m_rng.randrange(16) == 0 && coin.IsSpent()) {
185  newcoin.out.scriptPubKey.assign(1 + m_rng.randbits(6), OP_RETURN);
187  added_an_unspendable_entry = true;
188  } else {
189  // Random sizes so we can test memory usage accounting
190  newcoin.out.scriptPubKey.assign(m_rng.randbits(6), 0);
191  (coin.IsSpent() ? added_an_entry : updated_an_entry) = true;
192  coin = newcoin;
193  }
194  if (COutPoint op(txid, 0); !stack.back()->map().contains(op) && !newcoin.out.scriptPubKey.IsUnspendable() && m_rng.randbool()) {
195  stack.back()->EmplaceCoinInternalDANGER(std::move(op), std::move(newcoin));
196  } else {
197  stack.back()->AddCoin(op, std::move(newcoin), /*possible_overwrite=*/!coin.IsSpent() || m_rng.randbool());
198  }
199  } else {
200  // Spend the coin.
201  removed_an_entry = true;
202  coin.Clear();
203  BOOST_CHECK(stack.back()->SpendCoin(COutPoint(txid, 0)));
204  }
205  }
206 
207  // Once every 10 iterations, remove a random entry from the cache
208  if (m_rng.randrange(10) == 0) {
209  COutPoint out(txids[m_rng.rand32() % txids.size()], 0);
210  int cacheid = m_rng.rand32() % stack.size();
211  stack[cacheid]->Uncache(out);
212  uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out);
213  }
214 
215  // Once every 1000 iterations and at the end, verify the full cache.
216  if (m_rng.randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
217  for (const auto& entry : result) {
218  bool have = stack.back()->HaveCoin(entry.first);
219  const Coin& coin = stack.back()->AccessCoin(entry.first);
220  BOOST_CHECK(have == !coin.IsSpent());
221  BOOST_CHECK(coin == entry.second);
222  if (coin.IsSpent()) {
223  missed_an_entry = true;
224  } else {
225  BOOST_CHECK(stack.back()->HaveCoinInCache(entry.first));
226  found_an_entry = true;
227  }
228  }
229  for (const auto& test : stack) {
230  test->SelfTest();
231  }
232  }
233 
234  if (m_rng.randrange(100) == 0) {
235  // Every 100 iterations, flush an intermediate cache
236  if (stack.size() > 1 && m_rng.randbool() == 0) {
237  unsigned int flushIndex = m_rng.randrange(stack.size() - 1);
238  if (fake_best_block) stack[flushIndex]->SetBestBlock(m_rng.rand256());
239  bool should_erase = m_rng.randrange(4) < 3;
240  should_erase ? stack[flushIndex]->Flush() : stack[flushIndex]->Sync();
241  flushed_without_erase |= !should_erase;
242  }
243  }
244  if (m_rng.randrange(100) == 0) {
245  // Every 100 iterations, change the cache stack.
246  if (stack.size() > 0 && m_rng.randbool() == 0) {
247  //Remove the top cache
248  if (fake_best_block) stack.back()->SetBestBlock(m_rng.rand256());
249  bool should_erase = m_rng.randrange(4) < 3;
250  should_erase ? stack.back()->Flush() : stack.back()->Sync();
251  flushed_without_erase |= !should_erase;
252  stack.pop_back();
253  }
254  if (stack.size() == 0 || (stack.size() < 4 && m_rng.randbool())) {
255  //Add a new cache
256  CCoinsView* tip = base;
257  if (stack.size() > 0) {
258  tip = stack.back().get();
259  } else {
260  removed_all_caches = true;
261  }
262  stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
263  if (stack.size() == 4) {
264  reached_4_caches = true;
265  }
266  }
267  }
268  }
269 
270  // Verify coverage.
271  BOOST_CHECK(removed_all_caches);
272  BOOST_CHECK(reached_4_caches);
273  BOOST_CHECK(added_an_entry);
274  BOOST_CHECK(added_an_unspendable_entry);
275  BOOST_CHECK(removed_an_entry);
276  BOOST_CHECK(updated_an_entry);
277  BOOST_CHECK(found_an_entry);
278  BOOST_CHECK(missed_an_entry);
279  BOOST_CHECK(uncached_an_entry);
280  BOOST_CHECK(flushed_without_erase);
281 }
282 }; // struct CacheTest
283 
285 
286 // Run the above simulation for multiple base types.
287 BOOST_FIXTURE_TEST_CASE(coins_cache_base_simulation_test, CacheTest)
288 {
289  CCoinsViewTest base{m_rng};
290  SimulationTest(&base, false);
291 }
292 
294 
295 BOOST_FIXTURE_TEST_SUITE(coins_tests_dbbase, BasicTestingSetup)
296 
297 BOOST_FIXTURE_TEST_CASE(coins_cache_dbbase_simulation_test, CacheTest)
298 {
299  CCoinsViewDB db_base{{.path = "test", .cache_bytes = 1 << 23, .memory_only = true}, {}};
300  SimulationTest(&db_base, true);
301 }
302 
304 
306 
308 // Store of all necessary tx and undo data for next test
309 typedef std::map<COutPoint, std::tuple<CTransaction,CTxUndo,Coin>> UtxoData;
311 
312 UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
313  assert(utxoSet.size());
314  auto utxoSetIt = utxoSet.lower_bound(COutPoint(Txid::FromUint256(m_rng.rand256()), 0));
315  if (utxoSetIt == utxoSet.end()) {
316  utxoSetIt = utxoSet.begin();
317  }
318  auto utxoDataIt = utxoData.find(*utxoSetIt);
319  assert(utxoDataIt != utxoData.end());
320  return utxoDataIt;
321 }
322 }; // struct UpdateTest
323 
324 
325 // This test is similar to the previous test
326 // except the emphasis is on testing the functionality of UpdateCoins
327 // random txs are created and UpdateCoins is used to update the cache stack
328 // In particular it is tested that spending a duplicate coinbase tx
329 // has the expected effect (the other duplicate is overwritten at all cache levels)
330 BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
331 {
332  SeedRandomForTest(SeedRand::ZEROS);
333 
334  bool spent_a_duplicate_coinbase = false;
335  // A simple map to track what we expect the cache stack to represent.
336  std::map<COutPoint, Coin> result;
337 
338  // The cache stack.
339  CCoinsViewTest base{m_rng}; // A CCoinsViewTest at the bottom.
340  std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
341  stack.push_back(std::make_unique<CCoinsViewCacheTest>(&base)); // Start with one cache.
342 
343  // Track the txids we've used in various sets
344  std::set<COutPoint> coinbase_coins;
345  std::set<COutPoint> disconnected_coins;
346  std::set<COutPoint> duplicate_coins;
347  std::set<COutPoint> utxoset;
348 
349  for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
350  uint32_t randiter = m_rng.rand32();
351 
352  // 19/20 txs add a new transaction
353  if (randiter % 20 < 19) {
355  tx.vin.resize(1);
356  tx.vout.resize(1);
357  tx.vout[0].nValue = i; //Keep txs unique unless intended to duplicate
358  tx.vout[0].scriptPubKey.assign(m_rng.rand32() & 0x3F, 0); // Random sizes so we can test memory usage accounting
359  const int height{int(m_rng.rand32() >> 1)};
360  Coin old_coin;
361 
362  // 2/20 times create a new coinbase
363  if (randiter % 20 < 2 || coinbase_coins.size() < 10) {
364  // 1/10 of those times create a duplicate coinbase
365  if (m_rng.randrange(10) == 0 && coinbase_coins.size()) {
366  auto utxod = FindRandomFrom(coinbase_coins);
367  // Reuse the exact same coinbase
368  tx = CMutableTransaction{std::get<0>(utxod->second)};
369  // shouldn't be available for reconnection if it's been duplicated
370  disconnected_coins.erase(utxod->first);
371 
372  duplicate_coins.insert(utxod->first);
373  }
374  else {
375  coinbase_coins.insert(COutPoint(tx.GetHash(), 0));
376  }
377  assert(CTransaction(tx).IsCoinBase());
378  }
379 
380  // 17/20 times reconnect previous or add a regular tx
381  else {
382 
383  COutPoint prevout;
384  // 1/20 times reconnect a previously disconnected tx
385  if (randiter % 20 == 2 && disconnected_coins.size()) {
386  auto utxod = FindRandomFrom(disconnected_coins);
387  tx = CMutableTransaction{std::get<0>(utxod->second)};
388  prevout = tx.vin[0].prevout;
389  if (!CTransaction(tx).IsCoinBase() && !utxoset.contains(prevout)) {
390  disconnected_coins.erase(utxod->first);
391  continue;
392  }
393 
394  // If this tx is already IN the UTXO, then it must be a coinbase, and it must be a duplicate
395  if (utxoset.contains(utxod->first)) {
396  assert(CTransaction(tx).IsCoinBase());
397  assert(duplicate_coins.contains(utxod->first));
398  }
399  disconnected_coins.erase(utxod->first);
400  }
401 
402  // 16/20 times create a regular tx
403  else {
404  auto utxod = FindRandomFrom(utxoset);
405  prevout = utxod->first;
406 
407  // Construct the tx to spend the coins of prevouthash
408  tx.vin[0].prevout = prevout;
409  assert(!CTransaction(tx).IsCoinBase());
410  }
411  // In this simple test coins only have two states, spent or unspent, save the unspent state to restore
412  old_coin = result[prevout];
413  // Update the expected result of prevouthash to know these coins are spent
414  result[prevout].Clear();
415 
416  utxoset.erase(prevout);
417 
418  // The test is designed to ensure spending a duplicate coinbase will work properly
419  // if that ever happens and not resurrect the previously overwritten coinbase
420  if (duplicate_coins.contains(prevout)) {
421  spent_a_duplicate_coinbase = true;
422  }
423 
424  }
425  // Update the expected result to know about the new output coins
426  assert(tx.vout.size() == 1);
427  const COutPoint outpoint(tx.GetHash(), 0);
428  result[outpoint] = Coin{tx.vout[0], height, CTransaction{tx}.IsCoinBase()};
429 
430  // Call UpdateCoins on the top cache
431  CTxUndo undo;
432  UpdateCoins(CTransaction{tx}, *(stack.back()), undo, height);
433 
434  // Update the utxo set for future spends
435  utxoset.insert(outpoint);
436 
437  // Track this tx and undo info to use later
438  utxoData.emplace(outpoint, std::make_tuple(tx,undo,old_coin));
439  } else if (utxoset.size()) {
440  //1/20 times undo a previous transaction
441  auto utxod = FindRandomFrom(utxoset);
442 
443  CTransaction &tx = std::get<0>(utxod->second);
444  CTxUndo &undo = std::get<1>(utxod->second);
445  Coin &orig_coin = std::get<2>(utxod->second);
446 
447  // Update the expected result
448  // Remove new outputs
449  result[utxod->first].Clear();
450  // If not coinbase restore prevout
451  if (!tx.IsCoinBase()) {
452  result[tx.vin[0].prevout] = orig_coin;
453  }
454 
455  // Disconnect the tx from the current UTXO
456  // See code in DisconnectBlock
457  // remove outputs
458  BOOST_CHECK(stack.back()->SpendCoin(utxod->first));
459  // restore inputs
460  if (!tx.IsCoinBase()) {
461  const COutPoint &out = tx.vin[0].prevout;
462  Coin coin = undo.vprevout[0];
463  ApplyTxInUndo(std::move(coin), *(stack.back()), out);
464  }
465  // Store as a candidate for reconnection
466  disconnected_coins.insert(utxod->first);
467 
468  // Update the utxoset
469  utxoset.erase(utxod->first);
470  if (!tx.IsCoinBase())
471  utxoset.insert(tx.vin[0].prevout);
472  }
473 
474  // Once every 1000 iterations and at the end, verify the full cache.
475  if (m_rng.randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
476  for (const auto& entry : result) {
477  bool have = stack.back()->HaveCoin(entry.first);
478  const Coin& coin = stack.back()->AccessCoin(entry.first);
479  BOOST_CHECK(have == !coin.IsSpent());
480  BOOST_CHECK(coin == entry.second);
481  }
482  }
483 
484  // One every 10 iterations, remove a random entry from the cache
485  if (utxoset.size() > 1 && m_rng.randrange(30) == 0) {
486  stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
487  }
488  if (disconnected_coins.size() > 1 && m_rng.randrange(30) == 0) {
489  stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
490  }
491  if (duplicate_coins.size() > 1 && m_rng.randrange(30) == 0) {
492  stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
493  }
494 
495  if (m_rng.randrange(100) == 0) {
496  // Every 100 iterations, flush an intermediate cache
497  if (stack.size() > 1 && m_rng.randbool() == 0) {
498  unsigned int flushIndex = m_rng.randrange(stack.size() - 1);
499  stack[flushIndex]->Flush();
500  }
501  }
502  if (m_rng.randrange(100) == 0) {
503  // Every 100 iterations, change the cache stack.
504  if (stack.size() > 0 && m_rng.randbool() == 0) {
505  stack.back()->Flush();
506  stack.pop_back();
507  }
508  if (stack.size() == 0 || (stack.size() < 4 && m_rng.randbool())) {
509  CCoinsView* tip = &base;
510  if (stack.size() > 0) {
511  tip = stack.back().get();
512  }
513  stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
514  }
515  }
516  }
517 
518  // Verify coverage.
519  BOOST_CHECK(spent_a_duplicate_coinbase);
520 }
521 
522 BOOST_AUTO_TEST_CASE(ccoins_serialization)
523 {
524  // Good example
525  Coin cc1;
526  SpanReader{"97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35"_hex} >> cc1;
527  BOOST_CHECK_EQUAL(cc1.fCoinBase, false);
528  BOOST_CHECK_EQUAL(cc1.nHeight, 203998U);
529  BOOST_CHECK_EQUAL(cc1.out.nValue, CAmount{60000000000});
530  BOOST_CHECK_EQUAL(HexStr(cc1.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("816115944e077fe7c803cfa57f29b36bf87c1d35"_hex_u8)))));
531 
532  // Good example
533  Coin cc2;
534  SpanReader{"8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex} >> cc2;
535  BOOST_CHECK_EQUAL(cc2.fCoinBase, true);
536  BOOST_CHECK_EQUAL(cc2.nHeight, 120891U);
537  BOOST_CHECK_EQUAL(cc2.out.nValue, 110397);
538  BOOST_CHECK_EQUAL(HexStr(cc2.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex_u8)))));
539 
540  // Smallest possible example
541  Coin cc3;
542  SpanReader{"000006"_hex} >> cc3;
543  BOOST_CHECK_EQUAL(cc3.fCoinBase, false);
544  BOOST_CHECK_EQUAL(cc3.nHeight, 0U);
545  BOOST_CHECK_EQUAL(cc3.out.nValue, 0);
547 
548  // scriptPubKey that ends beyond the end of the stream
549  try {
550  Coin cc4;
551  SpanReader{"000007"_hex} >> cc4;
552  BOOST_CHECK_MESSAGE(false, "We should have thrown");
553  } catch (const std::ios_base::failure&) {
554  }
555 
556  // Very large scriptPubKey (3*10^9 bytes) past the end of the stream
557  DataStream tmp{};
558  uint64_t x = 3000000000ULL;
559  tmp << VARINT(x);
560  BOOST_CHECK_EQUAL(HexStr(tmp), "8a95c0bb00");
561  try {
562  Coin cc5;
563  SpanReader{"00008a95c0bb00"_hex} >> cc5;
564  BOOST_CHECK_MESSAGE(false, "We should have thrown");
565  } catch (const std::ios_base::failure&) {
566  }
567 }
568 
569 const static COutPoint OUTPOINT;
570 constexpr CAmount SPENT {-1};
571 constexpr CAmount ABSENT{-2};
572 constexpr CAmount VALUE1{100};
573 constexpr CAmount VALUE2{200};
574 constexpr CAmount VALUE3{300};
575 
576 struct CoinEntry {
577  enum class State { CLEAN, DIRTY, FRESH, DIRTY_FRESH };
578 
579  const CAmount value;
580  const State state;
581 
582  constexpr CoinEntry(const CAmount v, const State s) : value{v}, state{s} {}
583 
584  bool operator==(const CoinEntry& o) const = default;
585  friend std::ostream& operator<<(std::ostream& os, const CoinEntry& e) { return os << e.value << ", " << e.state; }
586 
587  constexpr bool IsDirtyFresh() const { return state == State::DIRTY_FRESH; }
588  constexpr bool IsDirty() const { return state == State::DIRTY || IsDirtyFresh(); }
589  constexpr bool IsFresh() const { return state == State::FRESH || IsDirtyFresh(); }
590 
591  static constexpr State ToState(const bool is_dirty, const bool is_fresh) {
592  if (is_dirty && is_fresh) return State::DIRTY_FRESH;
593  if (is_dirty) return State::DIRTY;
594  if (is_fresh) return State::FRESH;
595  return State::CLEAN;
596  }
597 };
598 
599 using MaybeCoin = std::optional<CoinEntry>;
600 using CoinOrError = std::variant<MaybeCoin, std::string>;
601 
602 constexpr MaybeCoin MISSING {std::nullopt};
603 constexpr MaybeCoin SPENT_DIRTY {{SPENT, CoinEntry::State::DIRTY}};
604 constexpr MaybeCoin SPENT_DIRTY_FRESH {{SPENT, CoinEntry::State::DIRTY_FRESH}};
605 constexpr MaybeCoin SPENT_FRESH {{SPENT, CoinEntry::State::FRESH}};
606 constexpr MaybeCoin SPENT_CLEAN {{SPENT, CoinEntry::State::CLEAN}};
607 constexpr MaybeCoin VALUE1_DIRTY {{VALUE1, CoinEntry::State::DIRTY}};
608 constexpr MaybeCoin VALUE1_DIRTY_FRESH{{VALUE1, CoinEntry::State::DIRTY_FRESH}};
609 constexpr MaybeCoin VALUE1_FRESH {{VALUE1, CoinEntry::State::FRESH}};
610 constexpr MaybeCoin VALUE1_CLEAN {{VALUE1, CoinEntry::State::CLEAN}};
611 constexpr MaybeCoin VALUE2_DIRTY {{VALUE2, CoinEntry::State::DIRTY}};
612 constexpr MaybeCoin VALUE2_DIRTY_FRESH{{VALUE2, CoinEntry::State::DIRTY_FRESH}};
613 constexpr MaybeCoin VALUE2_FRESH {{VALUE2, CoinEntry::State::FRESH}};
614 constexpr MaybeCoin VALUE2_CLEAN {{VALUE2, CoinEntry::State::CLEAN}};
615 constexpr MaybeCoin VALUE3_DIRTY {{VALUE3, CoinEntry::State::DIRTY}};
616 constexpr MaybeCoin VALUE3_DIRTY_FRESH{{VALUE3, CoinEntry::State::DIRTY_FRESH}};
617 
618 constexpr auto EX_OVERWRITE_UNSPENT{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"};
619 constexpr auto EX_FRESH_MISAPPLIED {"FRESH flag misapplied to coin that exists in parent cache"};
620 
621 static void SetCoinsValue(const CAmount value, Coin& coin)
622 {
623  assert(value != ABSENT);
624  coin.Clear();
625  assert(coin.IsSpent());
626  if (value != SPENT) {
627  coin.out.nValue = value;
628  coin.nHeight = 1;
629  assert(!coin.IsSpent());
630  }
631 }
632 
633 static size_t InsertCoinsMapEntry(CCoinsMap& map, CoinsCachePair& sentinel, const CoinEntry& cache_coin)
634 {
635  CCoinsCacheEntry entry;
636  SetCoinsValue(cache_coin.value, entry.coin);
637  auto [iter, inserted] = map.emplace(OUTPOINT, std::move(entry));
638  assert(inserted);
639  if (cache_coin.IsDirty()) CCoinsCacheEntry::SetDirty(*iter, sentinel);
640  if (cache_coin.IsFresh()) CCoinsCacheEntry::SetFresh(*iter, sentinel);
641  return iter->second.coin.DynamicMemoryUsage();
642 }
643 
644 static MaybeCoin GetCoinsMapEntry(const CCoinsMap& map, const COutPoint& outp = OUTPOINT)
645 {
646  if (auto it{map.find(outp)}; it != map.end()) {
647  return CoinEntry{
648  it->second.coin.IsSpent() ? SPENT : it->second.coin.out.nValue,
649  CoinEntry::ToState(it->second.IsDirty(), it->second.IsFresh())};
650  }
651  return MISSING;
652 }
653 
654 static void WriteCoinsViewEntry(CCoinsView& view, const MaybeCoin& cache_coin)
655 {
656  CoinsCachePair sentinel{};
657  sentinel.second.SelfRef(sentinel);
658  CCoinsMapMemoryResource resource;
659  CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
660  if (cache_coin) InsertCoinsMapEntry(map, sentinel, *cache_coin);
661  size_t dirty_count{cache_coin && cache_coin->IsDirty()};
662  auto cursor{CoinsViewCacheCursor(dirty_count, sentinel, map, /*will_erase=*/true)};
663  view.BatchWrite(cursor, {});
664  BOOST_CHECK_EQUAL(dirty_count, 0U);
665 }
666 
668 {
669 public:
670  SingleEntryCacheTest(const CAmount base_value, const MaybeCoin& cache_coin)
671  {
672  auto base_cache_coin{base_value == ABSENT ? MISSING : CoinEntry{base_value, CoinEntry::State::DIRTY}};
673  WriteCoinsViewEntry(base, base_cache_coin);
674  if (cache_coin) {
675  cache.usage() += InsertCoinsMapEntry(cache.map(), cache.sentinel(), *cache_coin);
676  cache.dirty() += cache_coin->IsDirty();
677  }
678  }
679 
681  CCoinsViewCacheTest base{&root};
682  CCoinsViewCacheTest cache{&base};
683 };
684 
685 static void CheckAccessCoin(const CAmount base_value, const MaybeCoin& cache_coin, const MaybeCoin& expected)
686 {
687  SingleEntryCacheTest test{base_value, cache_coin};
688  auto& coin = test.cache.AccessCoin(OUTPOINT);
689  BOOST_CHECK_EQUAL(coin.IsSpent(), !test.cache.GetCoin(OUTPOINT));
690  test.cache.SelfTest(/*sanity_check=*/false);
691  BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), expected);
692 }
693 
694 BOOST_AUTO_TEST_CASE(ccoins_access)
695 {
696  /* Check AccessCoin behavior, requesting a coin from a cache view layered on
697  * top of a base view, and checking the resulting entry in the cache after
698  * the access.
699  * Base Cache Expected
700  */
701  for (auto base_value : {ABSENT, SPENT, VALUE1}) {
702  CheckAccessCoin(base_value, MISSING, base_value == VALUE1 ? VALUE1_CLEAN : MISSING);
703 
704  CheckAccessCoin(base_value, SPENT_CLEAN, SPENT_CLEAN );
705  CheckAccessCoin(base_value, SPENT_FRESH, SPENT_FRESH );
706  CheckAccessCoin(base_value, SPENT_DIRTY, SPENT_DIRTY );
708 
713  }
714 }
715 
716 static void CheckSpendCoins(const CAmount base_value, const MaybeCoin& cache_coin, const MaybeCoin& expected)
717 {
718  SingleEntryCacheTest test{base_value, cache_coin};
719  test.cache.SpendCoin(OUTPOINT);
720  test.cache.SelfTest();
721  BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), expected);
722 }
723 
724 BOOST_AUTO_TEST_CASE(ccoins_spend)
725 {
726  /* Check SpendCoin behavior, requesting a coin from a cache view layered on
727  * top of a base view, spending, and then checking
728  * the resulting entry in the cache after the modification.
729  * Base Cache Expected
730  */
731  for (auto base_value : {ABSENT, SPENT, VALUE1}) {
732  CheckSpendCoins(base_value, MISSING, base_value == VALUE1 ? SPENT_DIRTY : MISSING);
733 
735  CheckSpendCoins(base_value, SPENT_FRESH, MISSING );
738 
740  CheckSpendCoins(base_value, VALUE2_FRESH, MISSING );
743  }
744 }
745 
746 static void CheckAddCoin(const CAmount base_value, const MaybeCoin& cache_coin, const CAmount modify_value, const CoinOrError& expected, const bool coinbase)
747 {
748  SingleEntryCacheTest test{base_value, cache_coin};
749  bool possible_overwrite{coinbase};
750  auto add_coin{[&] { test.cache.AddCoin(OUTPOINT, Coin{CTxOut{modify_value, CScript{}}, 1, coinbase}, possible_overwrite); }};
751  if (auto* expected_coin{std::get_if<MaybeCoin>(&expected)}) {
752  add_coin();
753  test.cache.SelfTest();
754  BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), *expected_coin);
755  } else {
756  BOOST_CHECK_EXCEPTION(add_coin(), std::logic_error, HasReason(std::get<std::string>(expected)));
757  }
758 }
759 
761 {
762  /* Check AddCoin behavior, requesting a new coin from a cache view,
763  * writing a modification to the coin, and then checking the resulting
764  * entry in the cache after the modification. Verify behavior with the
765  * AddCoin coinbase argument set to false, and to true.
766  * Base Cache Write Expected Coinbase
767  */
768  for (auto base_value : {ABSENT, SPENT, VALUE1}) {
769  CheckAddCoin(base_value, MISSING, VALUE3, VALUE3_DIRTY_FRESH, false);
770  CheckAddCoin(base_value, MISSING, VALUE3, VALUE3_DIRTY, true );
771 
772  CheckAddCoin(base_value, SPENT_CLEAN, VALUE3, VALUE3_DIRTY_FRESH, false);
773  CheckAddCoin(base_value, SPENT_CLEAN, VALUE3, VALUE3_DIRTY, true );
774  CheckAddCoin(base_value, SPENT_FRESH, VALUE3, VALUE3_DIRTY_FRESH, false);
775  CheckAddCoin(base_value, SPENT_FRESH, VALUE3, VALUE3_DIRTY_FRESH, true );
776  CheckAddCoin(base_value, SPENT_DIRTY, VALUE3, VALUE3_DIRTY, false);
777  CheckAddCoin(base_value, SPENT_DIRTY, VALUE3, VALUE3_DIRTY, true );
780 
782  CheckAddCoin(base_value, VALUE2_CLEAN, VALUE3, VALUE3_DIRTY, true );
784  CheckAddCoin(base_value, VALUE2_FRESH, VALUE3, VALUE3_DIRTY_FRESH, true );
786  CheckAddCoin(base_value, VALUE2_DIRTY, VALUE3, VALUE3_DIRTY, true );
789  }
790 }
791 
792 static void CheckWriteCoins(const MaybeCoin& parent, const MaybeCoin& child, const CoinOrError& expected)
793 {
794  SingleEntryCacheTest test{ABSENT, parent};
795  auto write_coins{[&] { WriteCoinsViewEntry(test.cache, child); }};
796  if (auto* expected_coin{std::get_if<MaybeCoin>(&expected)}) {
797  write_coins();
798  test.cache.SelfTest(/*sanity_check=*/false);
799  BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), *expected_coin);
800  } else {
801  BOOST_CHECK_EXCEPTION(write_coins(), std::logic_error, HasReason(std::get<std::string>(expected)));
802  }
803 }
804 
805 BOOST_AUTO_TEST_CASE(ccoins_write)
806 {
807  /* Check BatchWrite behavior, flushing one entry from a child cache to a
808  * parent cache, and checking the resulting entry in the parent cache
809  * after the write.
810  * Parent Child Expected
811  */
821 
830 
839 
852 
861 
862  // The checks above omit cases where the child state is not DIRTY, since
863  // they would be too repetitive (the parent cache is never updated in these
864  // cases). The loop below covers these cases and makes sure the parent cache
865  // is always left unchanged.
866  for (const MaybeCoin& parent : {MISSING,
869  for (const MaybeCoin& child : {MISSING,
872  auto expected{CoinOrError{parent}}; // TODO test failure cases as well
873  CheckWriteCoins(parent, child, expected);
874  }
875  }
876 }
877 
880 {
881  Coin coin;
882  coin.out.nValue = m_rng.rand32();
883  coin.nHeight = m_rng.randrange(4096);
884  coin.fCoinBase = 0;
885  return coin;
886 }
887 
888 
900  CCoinsViewCacheTest* view,
901  CCoinsViewDB& base,
902  std::vector<std::unique_ptr<CCoinsViewCacheTest>>& all_caches,
903  bool do_erasing_flush)
904 {
905  size_t cache_usage;
906  size_t cache_size;
907 
908  auto flush_all = [this, &all_caches](bool erase) {
909  // Flush in reverse order to ensure that flushes happen from children up.
910  for (auto i = all_caches.rbegin(); i != all_caches.rend(); ++i) {
911  auto& cache = *i;
912  cache->SanityCheck();
913  // hashBlock must be filled before flushing to disk; value is
914  // unimportant here. This is normally done during connect/disconnect block.
915  cache->SetBestBlock(m_rng.rand256());
916  erase ? cache->Flush() : cache->Sync();
917  }
918  };
919 
920  Txid txid = Txid::FromUint256(m_rng.rand256());
921  COutPoint outp = COutPoint(txid, 0);
922  Coin coin = MakeCoin();
923  // Ensure the coins views haven't seen this coin before.
924  BOOST_CHECK(!base.HaveCoin(outp));
925  BOOST_CHECK(!view->HaveCoin(outp));
926 
927  // --- 1. Adding a random coin to the child cache
928  //
929  view->AddCoin(outp, Coin(coin), false);
930 
931  cache_usage = view->DynamicMemoryUsage();
932  cache_size = view->map().size();
933 
934  // `base` shouldn't have coin (no flush yet) but `view` should have cached it.
935  BOOST_CHECK(!base.HaveCoin(outp));
936  BOOST_CHECK(view->HaveCoin(outp));
937 
938  BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), CoinEntry(coin.out.nValue, CoinEntry::State::DIRTY_FRESH));
939 
940  // --- 2. Flushing all caches (without erasing)
941  //
942  flush_all(/*erase=*/ false);
943 
944  // CoinsMap usage should be unchanged since we didn't erase anything.
945  BOOST_CHECK_EQUAL(cache_usage, view->DynamicMemoryUsage());
946  BOOST_CHECK_EQUAL(cache_size, view->map().size());
947 
948  // --- 3. Ensuring the entry still exists in the cache and has been written to parent
949  //
950  BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), CoinEntry(coin.out.nValue, CoinEntry::State::CLEAN)); // State should have been wiped.
951 
952  // Both views should now have the coin.
953  BOOST_CHECK(base.HaveCoin(outp));
954  BOOST_CHECK(view->HaveCoin(outp));
955 
956  if (do_erasing_flush) {
957  // --- 4. Flushing the caches again (with erasing)
958  //
959  flush_all(/*erase=*/ true);
960 
961  // Memory does not necessarily go down due to the map using a memory pool
962  BOOST_TEST(view->DynamicMemoryUsage() <= cache_usage);
963  // Size of the cache must go down though
964  BOOST_TEST(view->map().size() < cache_size);
965 
966  // --- 5. Ensuring the entry is no longer in the cache
967  //
968  BOOST_CHECK(!GetCoinsMapEntry(view->map(), outp));
969  view->AccessCoin(outp);
970  BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), CoinEntry(coin.out.nValue, CoinEntry::State::CLEAN));
971  }
972 
973  // Can't overwrite an entry without specifying that an overwrite is
974  // expected.
976  view->AddCoin(outp, Coin(coin), /*possible_overwrite=*/ false),
977  std::logic_error);
978 
979  // --- 6. Spend the coin.
980  //
981  BOOST_CHECK(view->SpendCoin(outp));
982 
983  // The coin should be in the cache, but spent and marked dirty.
984  BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), SPENT_DIRTY);
985  BOOST_CHECK(!view->HaveCoin(outp)); // Coin should be considered spent in `view`.
986  BOOST_CHECK(base.HaveCoin(outp)); // But coin should still be unspent in `base`.
987 
988  flush_all(/*erase=*/ false);
989 
990  // Coin should be considered spent in both views.
991  BOOST_CHECK(!view->HaveCoin(outp));
992  BOOST_CHECK(!base.HaveCoin(outp));
993 
994  // Spent coin should not be spendable.
995  BOOST_CHECK(!view->SpendCoin(outp));
996 
997  // --- Bonus check: ensure that a coin added to the base view via one cache
998  // can be spent by another cache which has never seen it.
999  //
1000  txid = Txid::FromUint256(m_rng.rand256());
1001  outp = COutPoint(txid, 0);
1002  coin = MakeCoin();
1003  BOOST_CHECK(!base.HaveCoin(outp));
1004  BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
1005  BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
1006 
1007  all_caches[0]->AddCoin(outp, std::move(coin), false);
1008  all_caches[0]->Sync();
1009  BOOST_CHECK(base.HaveCoin(outp));
1010  BOOST_CHECK(all_caches[0]->HaveCoin(outp));
1011  BOOST_CHECK(!all_caches[1]->HaveCoinInCache(outp));
1012 
1013  BOOST_CHECK(all_caches[1]->SpendCoin(outp));
1014  flush_all(/*erase=*/ false);
1015  BOOST_CHECK(!base.HaveCoin(outp));
1016  BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
1017  BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
1018 
1019  flush_all(/*erase=*/ true); // Erase all cache content.
1020 
1021  // --- Bonus check 2: ensure that a FRESH, spent coin is deleted by Sync()
1022  //
1023  txid = Txid::FromUint256(m_rng.rand256());
1024  outp = COutPoint(txid, 0);
1025  coin = MakeCoin();
1026  CAmount coin_val = coin.out.nValue;
1027  BOOST_CHECK(!base.HaveCoin(outp));
1028  BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
1029  BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
1030 
1031  // Add and spend from same cache without flushing.
1032  all_caches[0]->AddCoin(outp, std::move(coin), false);
1033 
1034  // Coin should be FRESH in the cache.
1035  BOOST_CHECK_EQUAL(GetCoinsMapEntry(all_caches[0]->map(), outp), CoinEntry(coin_val, CoinEntry::State::DIRTY_FRESH));
1036  // Base shouldn't have seen coin.
1037  BOOST_CHECK(!base.HaveCoin(outp));
1038 
1039  BOOST_CHECK(all_caches[0]->SpendCoin(outp));
1040  all_caches[0]->Sync();
1041 
1042  // Ensure there is no sign of the coin after spend/flush.
1043  BOOST_CHECK(!GetCoinsMapEntry(all_caches[0]->map(), outp));
1044  BOOST_CHECK(!all_caches[0]->HaveCoinInCache(outp));
1045  BOOST_CHECK(!base.HaveCoin(outp));
1046 }
1047 }; // struct FlushTest
1048 
1049 BOOST_FIXTURE_TEST_CASE(ccoins_flush_behavior, FlushTest)
1050 {
1051  // Create two in-memory caches atop a leveldb view.
1052  CCoinsViewDB base{{.path = "test", .cache_bytes = 1 << 23, .memory_only = true}, {}};
1053  std::vector<std::unique_ptr<CCoinsViewCacheTest>> caches;
1054  caches.push_back(std::make_unique<CCoinsViewCacheTest>(&base));
1055  caches.push_back(std::make_unique<CCoinsViewCacheTest>(caches.back().get()));
1056 
1057  for (const auto& view : caches) {
1058  TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/false);
1059  TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/true);
1060  }
1061 }
1062 
1063 BOOST_AUTO_TEST_CASE(coins_resource_is_used)
1064 {
1065  CCoinsMapMemoryResource resource;
1067 
1068  {
1069  CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
1070  BOOST_TEST(memusage::DynamicUsage(map) >= resource.ChunkSizeBytes());
1071 
1072  map.reserve(1000);
1073 
1074  // The resource has preallocated a chunk, so we should have space for at several nodes without the need to allocate anything else.
1075  const auto usage_before = memusage::DynamicUsage(map);
1076 
1077  COutPoint out_point{};
1078  for (size_t i = 0; i < 1000; ++i) {
1079  out_point.n = i;
1080  map[out_point];
1081  }
1082  BOOST_TEST(usage_before == memusage::DynamicUsage(map));
1083  }
1084 
1086 }
1087 
1088 BOOST_AUTO_TEST_CASE(ccoins_addcoin_exception_keeps_usage_balanced)
1089 {
1090  CCoinsView root;
1091  CCoinsViewCacheTest cache{&root};
1092 
1093  const COutPoint outpoint{Txid::FromUint256(m_rng.rand256()), m_rng.rand32()};
1094 
1095  const Coin coin1{CTxOut{m_rng.randrange(10), CScript{} << m_rng.randbytes(CScriptBase::STATIC_SIZE + 1)}, 1, false};
1096  cache.AddCoin(outpoint, Coin{coin1}, /*possible_overwrite=*/false);
1097  cache.SelfTest();
1098 
1099  const Coin coin2{CTxOut{m_rng.randrange(20), CScript{} << m_rng.randbytes(CScriptBase::STATIC_SIZE + 2)}, 2, false};
1100  BOOST_CHECK_THROW(cache.AddCoin(outpoint, Coin{coin2}, /*possible_overwrite=*/false), std::logic_error);
1101  cache.SelfTest();
1102 
1103  BOOST_CHECK(cache.AccessCoin(outpoint) == coin1);
1104 }
1105 
1106 BOOST_AUTO_TEST_CASE(ccoins_emplace_duplicate_keeps_usage_balanced)
1107 {
1108  CCoinsView root;
1109  CCoinsViewCacheTest cache{&root};
1110 
1111  const COutPoint outpoint{Txid::FromUint256(m_rng.rand256()), m_rng.rand32()};
1112 
1113  const Coin coin1{CTxOut{m_rng.randrange(10), CScript{} << m_rng.randbytes(CScriptBase::STATIC_SIZE + 1)}, 1, false};
1114  cache.EmplaceCoinInternalDANGER(COutPoint{outpoint}, Coin{coin1});
1115  cache.SelfTest();
1116 
1117  const Coin coin2{CTxOut{m_rng.randrange(20), CScript{} << m_rng.randbytes(CScriptBase::STATIC_SIZE + 2)}, 2, false};
1118  cache.EmplaceCoinInternalDANGER(COutPoint{outpoint}, Coin{coin2});
1119  cache.SelfTest();
1120 
1121  BOOST_CHECK(cache.AccessCoin(outpoint) == coin1);
1122 }
1123 
1124 BOOST_AUTO_TEST_CASE(ccoins_reset_guard)
1125 {
1126  CCoinsViewTest root{m_rng};
1127  CCoinsViewCache root_cache{&root};
1128  uint256 base_best_block{m_rng.rand256()};
1129  root_cache.SetBestBlock(base_best_block);
1130  root_cache.Flush();
1131 
1132  CCoinsViewCache cache{&root};
1133 
1134  const COutPoint outpoint{Txid::FromUint256(m_rng.rand256()), m_rng.rand32()};
1135 
1136  const Coin coin{CTxOut{m_rng.randrange(10), CScript{} << m_rng.randbytes(CScriptBase::STATIC_SIZE + 1)}, 1, false};
1137  cache.EmplaceCoinInternalDANGER(COutPoint{outpoint}, Coin{coin});
1138  BOOST_CHECK_EQUAL(cache.GetDirtyCount(), 1U);
1139 
1140  uint256 cache_best_block{m_rng.rand256()};
1141  cache.SetBestBlock(cache_best_block);
1142 
1143  {
1144  const auto reset_guard{cache.CreateResetGuard()};
1145  BOOST_CHECK(cache.AccessCoin(outpoint) == coin);
1146  BOOST_CHECK(!cache.AccessCoin(outpoint).IsSpent());
1147  BOOST_CHECK_EQUAL(cache.GetCacheSize(), 1);
1148  BOOST_CHECK_EQUAL(cache.GetDirtyCount(), 1);
1149  BOOST_CHECK_EQUAL(cache.GetBestBlock(), cache_best_block);
1150  BOOST_CHECK(!root_cache.HaveCoinInCache(outpoint));
1151  }
1152 
1153  BOOST_CHECK(cache.AccessCoin(outpoint).IsSpent());
1154  BOOST_CHECK_EQUAL(cache.GetCacheSize(), 0);
1155  BOOST_CHECK_EQUAL(cache.GetDirtyCount(), 0);
1156  BOOST_CHECK_EQUAL(cache.GetBestBlock(), base_best_block);
1157  BOOST_CHECK(!root_cache.HaveCoinInCache(outpoint));
1158 
1159  // Using a reset guard again is idempotent
1160  {
1161  const auto reset_guard{cache.CreateResetGuard()};
1162  }
1163 
1164  BOOST_CHECK(cache.AccessCoin(outpoint).IsSpent());
1165  BOOST_CHECK_EQUAL(cache.GetCacheSize(), 0);
1166  BOOST_CHECK_EQUAL(cache.GetDirtyCount(), 0U);
1167  BOOST_CHECK_EQUAL(cache.GetBestBlock(), base_best_block);
1168  BOOST_CHECK(!root_cache.HaveCoinInCache(outpoint));
1169 
1170  // Flush should be a no-op after reset.
1171  cache.Flush();
1172  BOOST_CHECK_EQUAL(cache.GetDirtyCount(), 0U);
1173 }
1174 
1175 BOOST_AUTO_TEST_CASE(ccoins_peekcoin)
1176 {
1177  CCoinsViewTest base{m_rng};
1178 
1179  // Populate the base view with a coin.
1180  const COutPoint outpoint{Txid::FromUint256(m_rng.rand256()), m_rng.rand32()};
1181  const Coin coin{CTxOut{m_rng.randrange(10), CScript{}}, 1, false};
1182  {
1183  CCoinsViewCache cache{&base};
1184  cache.AddCoin(outpoint, Coin{coin}, /*possible_overwrite=*/false);
1185  cache.Flush();
1186  }
1187 
1188  // Verify PeekCoin can read through the cache stack without mutating the intermediate cache.
1189  CCoinsViewCacheTest main_cache{&base};
1190  const auto fetched{main_cache.PeekCoin(outpoint)};
1191  BOOST_CHECK(fetched.has_value());
1192  BOOST_CHECK(*fetched == coin);
1193  BOOST_CHECK(!main_cache.HaveCoinInCache(outpoint));
1194 }
1195 
CoinsCachePair * NextAndMaybeErase(CoinsCachePair &current) noexcept
Return the next entry after current, possibly erasing current.
Definition: coins.h:279
std::vector< B > randbytes(size_t len) noexcept
Generate random bytes.
Definition: random.h:297
CAmount nValue
Definition: transaction.h:142
static const unsigned int NUM_SIMULATION_ITERATIONS
#define VARINT(obj)
Definition: serialize.h:491
std::optional< CoinEntry > MaybeCoin
bool IsSpent() const
Either this coin never existed (see e.g.
Definition: coins.h:83
int ret
constexpr CAmount ABSENT
static OutputGroup MakeCoin(const CAmount &amount, bool is_eff_value=true, CoinSelectionParams cs_params=default_cs_params, int custom_spending_vsize=P2WPKH_INPUT_VSIZE)
Make one OutputGroup with a single UTXO that either has a given effective value (default) or a given ...
int ApplyTxInUndo(Coin &&undo, CCoinsViewCache &view, const COutPoint &out)
Restore the UTXO in a Coin at a given COutPoint.
void assign(size_type n, const T &val)
Definition: prevector.h:176
bool randbool() noexcept
Generate a random boolean.
Definition: random.h:325
A Coin in one level of the coins database caching hierarchy.
Definition: coins.h:109
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:18
assert(!tx.IsCoinBase())
CScript scriptPubKey
Definition: transaction.h:143
bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:603
static void CheckAccessCoin(const CAmount base_value, const MaybeCoin &cache_coin, const MaybeCoin &expected)
constexpr CAmount VALUE3
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher, std::equal_to< COutPoint >, PoolAllocator< CoinsCachePair, sizeof(CoinsCachePair)+sizeof(void *) *4 > > CCoinsMap
PoolAllocator&#39;s MAX_BLOCK_SIZE_BYTES parameter here uses sizeof the data, and adds the size of 4 poin...
Definition: coins.h:224
A UTXO entry.
Definition: coins.h:34
std::map< COutPoint, std::tuple< CTransaction, CTxUndo, Coin > > UtxoData
CoinsCachePair * End() const noexcept
Definition: coins.h:276
constexpr MaybeCoin VALUE3_DIRTY_FRESH
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition: memusage.h:31
std::vector< CTxIn > vin
Definition: transaction.h:359
static void CheckWriteCoins(const MaybeCoin &parent, const MaybeCoin &child, const CoinOrError &expected)
constexpr bool IsDirtyFresh() const
constexpr MaybeCoin VALUE1_DIRTY
CTxOut out
unspent transaction output
Definition: coins.h:38
unsigned int fCoinBase
whether containing transaction was a coinbase
Definition: coins.h:41
friend std::ostream & operator<<(std::ostream &os, const CoinEntry &e)
constexpr MaybeCoin MISSING
static void SetCoinsValue(const CAmount value, Coin &coin)
constexpr auto EX_OVERWRITE_UNSPENT
std::variant< MaybeCoin, std::string > CoinOrError
static MaybeCoin GetCoinsMapEntry(const CCoinsMap &map, const COutPoint &outp=OUTPOINT)
constexpr CAmount VALUE1
CCoinsMap::allocator_type::ResourceType CCoinsMapMemoryResource
Definition: coins.h:226
UtxoData utxoData
bool IsCoinBase() const
Definition: transaction.h:341
constexpr MaybeCoin SPENT_DIRTY
const std::vector< CTxIn > vin
Definition: transaction.h:291
CAmount RandMoney(Rng &&rng)
Definition: random.h:35
constexpr MaybeCoin VALUE1_DIRTY_FRESH
Basic testing setup.
Definition: setup_common.h:64
const Coin & AccessByTxid(const CCoinsViewCache &view, const Txid &txid)
Utility function to find any unspent output with a given txid.
Definition: coins.cpp:386
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
Definition: script.h:563
Minimal stream for reading from an existing byte array by std::span.
Definition: streams.h:82
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static constexpr unsigned int STATIC_SIZE
Definition: prevector.h:41
uint32_t nHeight
at which height this containing transaction was included in the active block chain ...
Definition: coins.h:44
std::pair< const COutPoint, CCoinsCacheEntry > CoinsCachePair
Definition: coins.h:93
constexpr MaybeCoin VALUE2_CLEAN
SingleEntryCacheTest(const CAmount base_value, const MaybeCoin &cache_coin)
Abstract view on the open txout dataset.
Definition: coins.h:307
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:132
constexpr MaybeCoin VALUE3_DIRTY
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
Definition: coins.h:260
BOOST_AUTO_TEST_CASE(ccoins_serialization)
void SimulationTest(CCoinsView *base, bool fake_best_block)
Fast randomness source.
Definition: random.h:385
BOOST_AUTO_TEST_SUITE_END()
constexpr MaybeCoin VALUE2_FRESH
static void CheckSpendCoins(const CAmount base_value, const MaybeCoin &cache_coin, const MaybeCoin &expected)
uint32_t n
Definition: transaction.h:32
constexpr MaybeCoin SPENT_DIRTY_FRESH
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: common.h:17
CCoinsView root
uint32_t rand32() noexcept
Generate a random 32-bit integer.
Definition: random.h:314
State
An output of a transaction.
Definition: transaction.h:139
constexpr MaybeCoin VALUE1_CLEAN
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
Definition: strencodings.h:400
UtxoData::iterator FindRandomFrom(const std::set< COutPoint > &utxoSet)
virtual void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock)
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: coins.cpp:21
static void WriteCoinsViewEntry(CCoinsView &view, const MaybeCoin &cache_coin)
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:69
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:28
std::vector< CTxOut > vout
Definition: transaction.h:360
static void add_coin(const CAmount &nValue, int nInput, std::vector< OutputGroup > &set)
constexpr bool IsNull() const
Definition: uint256.h:48
unsigned int nHeight
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:81
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
Definition: coins.cpp:89
static void CheckAddCoin(const CAmount base_value, const MaybeCoin &cache_coin, const CAmount modify_value, const CoinOrError &expected, const bool coinbase)
void UpdateCoins(const CTransaction &tx, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight)
void TestFlushBehavior(CCoinsViewCacheTest *view, CCoinsViewDB &base, std::vector< std::unique_ptr< CCoinsViewCacheTest >> &all_caches, bool do_erasing_flush)
For CCoinsViewCache instances backed by either another cache instance or leveldb, test cache behavior...
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
CoinsCachePair * Begin() const noexcept
Definition: coins.h:275
256-bit opaque blob.
Definition: uint256.h:195
static void CheckAllDataAccountedFor(const PoolResource< MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES > &resource)
Once all blocks are given back to the resource, tests that the freelists are consistent: ...
static bool sanity_check(const std::vector< CTransactionRef > &transactions, const std::map< COutPoint, CAmount > &bumpfees)
static const COutPoint OUTPOINT
auto result
Definition: common-types.h:74
const State state
constexpr CoinEntry(const CAmount v, const State s)
constexpr CAmount SPENT
static void SetDirty(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
Definition: coins.h:172
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
static transaction_identifier FromUint256(const uint256 &id)
Undo information for a CTransaction.
Definition: undo.h:52
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:34
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
Definition: random.h:204
const CAmount value
uint256 rand256() noexcept
generate a random uint256.
Definition: random.h:317
constexpr MaybeCoin SPENT_FRESH
160-bit opaque blob.
Definition: uint256.h:183
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
Definition: random.h:254
static constexpr State ToState(const bool is_dirty, const bool is_fresh)
constexpr MaybeCoin SPENT_CLEAN
static int count
constexpr bool IsFresh() const
void Clear()
Definition: coins.h:50
A mutable version of CTransaction.
Definition: transaction.h:357
size_type size() const
Definition: prevector.h:247
constexpr MaybeCoin VALUE1_FRESH
constexpr MaybeCoin VALUE2_DIRTY_FRESH
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:280
constexpr bool IsDirty() const
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:367
constexpr MaybeCoin VALUE2_DIRTY
static void SetFresh(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
Definition: coins.h:173
Seed with a compile time constant of zeros.
constexpr auto EX_FRESH_MISAPPLIED
constexpr CAmount VALUE2
BOOST_FIXTURE_TEST_CASE(coins_cache_base_simulation_test, CacheTest)
Coin MakeCoin()
static size_t InsertCoinsMapEntry(CCoinsMap &map, CoinsCachePair &sentinel, const CoinEntry &cache_coin)
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:30
CCoinsViewCacheTest cache
Coin coin
Definition: coins.h:142
#define BOOST_CHECK(expr)
Definition: object.cpp:16