coins_tests.cpp raw

   1  // Copyright (c) 2014-2022 The Limenka 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/poolresourcetester.h>
  10  #include <test/util/random.h>
  11  #include <test/util/setup_common.h>
  12  #include <txdb.h>
  13  #include <uint256.h>
  14  #include <undo.h>
  15  #include <util/strencodings.h>
  16  
  17  #include <map>
  18  #include <string>
  19  #include <variant>
  20  #include <vector>
  21  
  22  #include <boost/test/unit_test.hpp>
  23  
  24  using namespace util::hex_literals;
  25  
  26  int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out);
  27  void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight);
  28  
  29  namespace
  30  {
  31  //! equality test
  32  bool operator==(const Coin &a, const Coin &b) {
  33      // Empty Coin objects are always equal.
  34      if (a.IsSpent() && b.IsSpent()) return true;
  35      return a.fCoinBase == b.fCoinBase &&
  36             a.nHeight == b.nHeight &&
  37             a.out == b.out;
  38  }
  39  
  40  class CCoinsViewTest : public CCoinsView
  41  {
  42      FastRandomContext& m_rng;
  43      uint256 hashBestBlock_;
  44      std::map<COutPoint, Coin> map_;
  45  
  46  public:
  47      CCoinsViewTest(FastRandomContext& rng) : m_rng{rng} {}
  48  
  49      std::optional<Coin> GetCoin(const COutPoint& outpoint) const override
  50      {
  51          if (auto it{map_.find(outpoint)}; it != map_.end()) {
  52              if (!it->second.IsSpent() || m_rng.randbool()) {
  53                  return it->second; // TODO spent coins shouldn't be returned
  54              }
  55          }
  56          return std::nullopt;
  57      }
  58  
  59      uint256 GetBestBlock() const override { return hashBestBlock_; }
  60  
  61      bool BatchWrite(CoinsViewCacheCursor& cursor, const uint256& hashBlock) override
  62      {
  63          for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)){
  64              if (it->second.IsDirty()) {
  65                  // Same optimization used in CCoinsViewDB is to only write dirty entries.
  66                  map_[it->first] = it->second.coin;
  67                  if (it->second.coin.IsSpent() && m_rng.randrange(3) == 0) {
  68                      // Randomly delete empty entries on write.
  69                      map_.erase(it->first);
  70                  }
  71              }
  72          }
  73          if (!hashBlock.IsNull())
  74              hashBestBlock_ = hashBlock;
  75          return true;
  76      }
  77  };
  78  
  79  class CCoinsViewCacheTest : public CCoinsViewCache
  80  {
  81  public:
  82      explicit CCoinsViewCacheTest(CCoinsView* _base) : CCoinsViewCache(_base) {}
  83  
  84      void SelfTest(bool sanity_check = true) const
  85      {
  86          // Manually recompute the dynamic usage of the whole data, and compare it.
  87          size_t ret = memusage::DynamicUsage(cacheCoins);
  88          size_t count = 0;
  89          for (const auto& entry : cacheCoins) {
  90              ret += entry.second.coin.DynamicMemoryUsage();
  91              ++count;
  92          }
  93          BOOST_CHECK_EQUAL(GetCacheSize(), count);
  94          BOOST_CHECK_EQUAL(DynamicMemoryUsage(), ret);
  95          if (sanity_check) {
  96              SanityCheck();
  97          }
  98      }
  99  
 100      CCoinsMap& map() const { return cacheCoins; }
 101      CoinsCachePair& sentinel() const { return m_sentinel; }
 102      size_t& usage() const { return cachedCoinsUsage; }
 103  };
 104  
 105  } // namespace
 106  
 107  BOOST_FIXTURE_TEST_SUITE(coins_tests, BasicTestingSetup)
 108  
 109  static const unsigned int NUM_SIMULATION_ITERATIONS = 40000;
 110  
 111  struct CacheTest : BasicTestingSetup {
 112  // This is a large randomized insert/remove simulation test on a variable-size
 113  // stack of caches on top of CCoinsViewTest.
 114  //
 115  // It will randomly create/update/delete Coin entries to a tip of caches, with
 116  // txids picked from a limited list of random 256-bit hashes. Occasionally, a
 117  // new tip is added to the stack of caches, or the tip is flushed and removed.
 118  //
 119  // During the process, booleans are kept to make sure that the randomized
 120  // operation hits all branches.
 121  //
 122  // If fake_best_block is true, assign a random uint256 to mock the recording
 123  // of best block on flush. This is necessary when using CCoinsViewDB as the base,
 124  // otherwise we'll hit an assertion in BatchWrite.
 125  //
 126  void SimulationTest(CCoinsView* base, bool fake_best_block)
 127  {
 128      // Various coverage trackers.
 129      bool removed_all_caches = false;
 130      bool reached_4_caches = false;
 131      bool added_an_entry = false;
 132      bool added_an_unspendable_entry = false;
 133      bool removed_an_entry = false;
 134      bool updated_an_entry = false;
 135      bool found_an_entry = false;
 136      bool missed_an_entry = false;
 137      bool uncached_an_entry = false;
 138      bool flushed_without_erase = false;
 139  
 140      // A simple map to track what we expect the cache stack to represent.
 141      std::map<COutPoint, Coin> result;
 142  
 143      // The cache stack.
 144      std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
 145      stack.push_back(std::make_unique<CCoinsViewCacheTest>(base)); // Start with one cache.
 146  
 147      // Use a limited set of random transaction ids, so we do test overwriting entries.
 148      std::vector<Txid> txids;
 149      txids.resize(NUM_SIMULATION_ITERATIONS / 8);
 150      for (unsigned int i = 0; i < txids.size(); i++) {
 151          txids[i] = Txid::FromUint256(m_rng.rand256());
 152      }
 153  
 154      for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
 155          // Do a random modification.
 156          {
 157              auto txid = txids[m_rng.randrange(txids.size())]; // txid we're going to modify in this iteration.
 158              Coin& coin = result[COutPoint(txid, 0)];
 159  
 160              // Determine whether to test HaveCoin before or after Access* (or both). As these functions
 161              // can influence each other's behaviour by pulling things into the cache, all combinations
 162              // are tested.
 163              bool test_havecoin_before = m_rng.randbits(2) == 0;
 164              bool test_havecoin_after = m_rng.randbits(2) == 0;
 165  
 166              bool result_havecoin = test_havecoin_before ? stack.back()->HaveCoin(COutPoint(txid, 0)) : false;
 167  
 168              // Infrequently, test usage of AccessByTxid instead of AccessCoin - the
 169              // former just delegates to the latter and returns the first unspent in a txn.
 170              const Coin& entry = (m_rng.randrange(500) == 0) ?
 171                  AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
 172              BOOST_CHECK(coin == entry);
 173  
 174              if (test_havecoin_before) {
 175                  BOOST_CHECK(result_havecoin == !entry.IsSpent());
 176              }
 177  
 178              if (test_havecoin_after) {
 179                  bool ret = stack.back()->HaveCoin(COutPoint(txid, 0));
 180                  BOOST_CHECK(ret == !entry.IsSpent());
 181              }
 182  
 183              if (m_rng.randrange(5) == 0 || coin.IsSpent()) {
 184                  Coin newcoin;
 185                  newcoin.out.nValue = RandMoney(m_rng);
 186                  newcoin.nHeight = 1;
 187  
 188                  // Infrequently test adding unspendable coins.
 189                  if (m_rng.randrange(16) == 0 && coin.IsSpent()) {
 190                      newcoin.out.scriptPubKey.assign(1 + m_rng.randbits(6), OP_RETURN);
 191                      BOOST_CHECK(newcoin.out.scriptPubKey.IsUnspendable());
 192                      added_an_unspendable_entry = true;
 193                  } else {
 194                      // Random sizes so we can test memory usage accounting
 195                      newcoin.out.scriptPubKey.assign(m_rng.randbits(6), 0);
 196                      (coin.IsSpent() ? added_an_entry : updated_an_entry) = true;
 197                      coin = newcoin;
 198                  }
 199                  bool is_overwrite = !coin.IsSpent() || m_rng.rand32() & 1;
 200                  stack.back()->AddCoin(COutPoint(txid, 0), std::move(newcoin), is_overwrite);
 201              } else {
 202                  // Spend the coin.
 203                  removed_an_entry = true;
 204                  coin.Clear();
 205                  BOOST_CHECK(stack.back()->SpendCoin(COutPoint(txid, 0)));
 206              }
 207          }
 208  
 209          // Once every 10 iterations, remove a random entry from the cache
 210          if (m_rng.randrange(10) == 0) {
 211              COutPoint out(txids[m_rng.rand32() % txids.size()], 0);
 212              int cacheid = m_rng.rand32() % stack.size();
 213              stack[cacheid]->Uncache(out);
 214              uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out);
 215          }
 216  
 217          // Once every 1000 iterations and at the end, verify the full cache.
 218          if (m_rng.randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
 219              for (const auto& entry : result) {
 220                  bool have = stack.back()->HaveCoin(entry.first);
 221                  const Coin& coin = stack.back()->AccessCoin(entry.first);
 222                  BOOST_CHECK(have == !coin.IsSpent());
 223                  BOOST_CHECK(coin == entry.second);
 224                  if (coin.IsSpent()) {
 225                      missed_an_entry = true;
 226                  } else {
 227                      BOOST_CHECK(stack.back()->HaveCoinInCache(entry.first));
 228                      found_an_entry = true;
 229                  }
 230              }
 231              for (const auto& test : stack) {
 232                  test->SelfTest();
 233              }
 234          }
 235  
 236          if (m_rng.randrange(100) == 0) {
 237              // Every 100 iterations, flush an intermediate cache
 238              if (stack.size() > 1 && m_rng.randbool() == 0) {
 239                  unsigned int flushIndex = m_rng.randrange(stack.size() - 1);
 240                  if (fake_best_block) stack[flushIndex]->SetBestBlock(m_rng.rand256());
 241                  bool should_erase = m_rng.randrange(4) < 3;
 242                  BOOST_CHECK(should_erase ? stack[flushIndex]->Flush() : stack[flushIndex]->Sync());
 243                  flushed_without_erase |= !should_erase;
 244              }
 245          }
 246          if (m_rng.randrange(100) == 0) {
 247              // Every 100 iterations, change the cache stack.
 248              if (stack.size() > 0 && m_rng.randbool() == 0) {
 249                  //Remove the top cache
 250                  if (fake_best_block) stack.back()->SetBestBlock(m_rng.rand256());
 251                  bool should_erase = m_rng.randrange(4) < 3;
 252                  BOOST_CHECK(should_erase ? stack.back()->Flush() : stack.back()->Sync());
 253                  flushed_without_erase |= !should_erase;
 254                  stack.pop_back();
 255              }
 256              if (stack.size() == 0 || (stack.size() < 4 && m_rng.randbool())) {
 257                  //Add a new cache
 258                  CCoinsView* tip = base;
 259                  if (stack.size() > 0) {
 260                      tip = stack.back().get();
 261                  } else {
 262                      removed_all_caches = true;
 263                  }
 264                  stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
 265                  if (stack.size() == 4) {
 266                      reached_4_caches = true;
 267                  }
 268              }
 269          }
 270      }
 271  
 272      // Verify coverage.
 273      BOOST_CHECK(removed_all_caches);
 274      BOOST_CHECK(reached_4_caches);
 275      BOOST_CHECK(added_an_entry);
 276      BOOST_CHECK(added_an_unspendable_entry);
 277      BOOST_CHECK(removed_an_entry);
 278      BOOST_CHECK(updated_an_entry);
 279      BOOST_CHECK(found_an_entry);
 280      BOOST_CHECK(missed_an_entry);
 281      BOOST_CHECK(uncached_an_entry);
 282      BOOST_CHECK(flushed_without_erase);
 283  }
 284  }; // struct CacheTest
 285  
 286  // Run the above simulation for multiple base types.
 287  BOOST_FIXTURE_TEST_CASE(coins_cache_simulation_test, CacheTest)
 288  {
 289      CCoinsViewTest base{m_rng};
 290      SimulationTest(&base, false);
 291  
 292      CCoinsViewDB db_base{{.path = "test", .cache_bytes = 1 << 23, .memory_only = true}, {}};
 293      SimulationTest(&db_base, true);
 294  }
 295  
 296  struct UpdateTest : BasicTestingSetup {
 297  // Store of all necessary tx and undo data for next test
 298  typedef std::map<COutPoint, std::tuple<CTransaction,CTxUndo,Coin>> UtxoData;
 299  UtxoData utxoData;
 300  
 301  UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
 302      assert(utxoSet.size());
 303      auto utxoSetIt = utxoSet.lower_bound(COutPoint(Txid::FromUint256(m_rng.rand256()), 0));
 304      if (utxoSetIt == utxoSet.end()) {
 305          utxoSetIt = utxoSet.begin();
 306      }
 307      auto utxoDataIt = utxoData.find(*utxoSetIt);
 308      assert(utxoDataIt != utxoData.end());
 309      return utxoDataIt;
 310  }
 311  }; // struct UpdateTest
 312  
 313  
 314  // This test is similar to the previous test
 315  // except the emphasis is on testing the functionality of UpdateCoins
 316  // random txs are created and UpdateCoins is used to update the cache stack
 317  // In particular it is tested that spending a duplicate coinbase tx
 318  // has the expected effect (the other duplicate is overwritten at all cache levels)
 319  BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
 320  {
 321      SeedRandomForTest(SeedRand::ZEROS);
 322  
 323      bool spent_a_duplicate_coinbase = false;
 324      // A simple map to track what we expect the cache stack to represent.
 325      std::map<COutPoint, Coin> result;
 326  
 327      // The cache stack.
 328      CCoinsViewTest base{m_rng}; // A CCoinsViewTest at the bottom.
 329      std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
 330      stack.push_back(std::make_unique<CCoinsViewCacheTest>(&base)); // Start with one cache.
 331  
 332      // Track the txids we've used in various sets
 333      std::set<COutPoint> coinbase_coins;
 334      std::set<COutPoint> disconnected_coins;
 335      std::set<COutPoint> duplicate_coins;
 336      std::set<COutPoint> utxoset;
 337  
 338      for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
 339          uint32_t randiter = m_rng.rand32();
 340  
 341          // 19/20 txs add a new transaction
 342          if (randiter % 20 < 19) {
 343              CMutableTransaction tx;
 344              tx.vin.resize(1);
 345              tx.vout.resize(1);
 346              tx.vout[0].nValue = i; //Keep txs unique unless intended to duplicate
 347              tx.vout[0].scriptPubKey.assign(m_rng.rand32() & 0x3F, 0); // Random sizes so we can test memory usage accounting
 348              const int height{int(m_rng.rand32() >> 1)};
 349              Coin old_coin;
 350  
 351              // 2/20 times create a new coinbase
 352              if (randiter % 20 < 2 || coinbase_coins.size() < 10) {
 353                  // 1/10 of those times create a duplicate coinbase
 354                  if (m_rng.randrange(10) == 0 && coinbase_coins.size()) {
 355                      auto utxod = FindRandomFrom(coinbase_coins);
 356                      // Reuse the exact same coinbase
 357                      tx = CMutableTransaction{std::get<0>(utxod->second)};
 358                      // shouldn't be available for reconnection if it's been duplicated
 359                      disconnected_coins.erase(utxod->first);
 360  
 361                      duplicate_coins.insert(utxod->first);
 362                  }
 363                  else {
 364                      coinbase_coins.insert(COutPoint(tx.GetHash(), 0));
 365                  }
 366                  assert(CTransaction(tx).IsCoinBase());
 367              }
 368  
 369              // 17/20 times reconnect previous or add a regular tx
 370              else {
 371  
 372                  COutPoint prevout;
 373                  // 1/20 times reconnect a previously disconnected tx
 374                  if (randiter % 20 == 2 && disconnected_coins.size()) {
 375                      auto utxod = FindRandomFrom(disconnected_coins);
 376                      tx = CMutableTransaction{std::get<0>(utxod->second)};
 377                      prevout = tx.vin[0].prevout;
 378                      if (!CTransaction(tx).IsCoinBase() && !utxoset.count(prevout)) {
 379                          disconnected_coins.erase(utxod->first);
 380                          continue;
 381                      }
 382  
 383                      // If this tx is already IN the UTXO, then it must be a coinbase, and it must be a duplicate
 384                      if (utxoset.count(utxod->first)) {
 385                          assert(CTransaction(tx).IsCoinBase());
 386                          assert(duplicate_coins.count(utxod->first));
 387                      }
 388                      disconnected_coins.erase(utxod->first);
 389                  }
 390  
 391                  // 16/20 times create a regular tx
 392                  else {
 393                      auto utxod = FindRandomFrom(utxoset);
 394                      prevout = utxod->first;
 395  
 396                      // Construct the tx to spend the coins of prevouthash
 397                      tx.vin[0].prevout = prevout;
 398                      assert(!CTransaction(tx).IsCoinBase());
 399                  }
 400                  // In this simple test coins only have two states, spent or unspent, save the unspent state to restore
 401                  old_coin = result[prevout];
 402                  // Update the expected result of prevouthash to know these coins are spent
 403                  result[prevout].Clear();
 404  
 405                  utxoset.erase(prevout);
 406  
 407                  // The test is designed to ensure spending a duplicate coinbase will work properly
 408                  // if that ever happens and not resurrect the previously overwritten coinbase
 409                  if (duplicate_coins.count(prevout)) {
 410                      spent_a_duplicate_coinbase = true;
 411                  }
 412  
 413              }
 414              // Update the expected result to know about the new output coins
 415              assert(tx.vout.size() == 1);
 416              const COutPoint outpoint(tx.GetHash(), 0);
 417              result[outpoint] = Coin{tx.vout[0], height, CTransaction{tx}.IsCoinBase()};
 418  
 419              // Call UpdateCoins on the top cache
 420              CTxUndo undo;
 421              UpdateCoins(CTransaction{tx}, *(stack.back()), undo, height);
 422  
 423              // Update the utxo set for future spends
 424              utxoset.insert(outpoint);
 425  
 426              // Track this tx and undo info to use later
 427              utxoData.emplace(outpoint, std::make_tuple(tx,undo,old_coin));
 428          } else if (utxoset.size()) {
 429              //1/20 times undo a previous transaction
 430              auto utxod = FindRandomFrom(utxoset);
 431  
 432              CTransaction &tx = std::get<0>(utxod->second);
 433              CTxUndo &undo = std::get<1>(utxod->second);
 434              Coin &orig_coin = std::get<2>(utxod->second);
 435  
 436              // Update the expected result
 437              // Remove new outputs
 438              result[utxod->first].Clear();
 439              // If not coinbase restore prevout
 440              if (!tx.IsCoinBase()) {
 441                  result[tx.vin[0].prevout] = orig_coin;
 442              }
 443  
 444              // Disconnect the tx from the current UTXO
 445              // See code in DisconnectBlock
 446              // remove outputs
 447              BOOST_CHECK(stack.back()->SpendCoin(utxod->first));
 448              // restore inputs
 449              if (!tx.IsCoinBase()) {
 450                  const COutPoint &out = tx.vin[0].prevout;
 451                  Coin coin = undo.vprevout[0];
 452                  ApplyTxInUndo(std::move(coin), *(stack.back()), out);
 453              }
 454              // Store as a candidate for reconnection
 455              disconnected_coins.insert(utxod->first);
 456  
 457              // Update the utxoset
 458              utxoset.erase(utxod->first);
 459              if (!tx.IsCoinBase())
 460                  utxoset.insert(tx.vin[0].prevout);
 461          }
 462  
 463          // Once every 1000 iterations and at the end, verify the full cache.
 464          if (m_rng.randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
 465              for (const auto& entry : result) {
 466                  bool have = stack.back()->HaveCoin(entry.first);
 467                  const Coin& coin = stack.back()->AccessCoin(entry.first);
 468                  BOOST_CHECK(have == !coin.IsSpent());
 469                  BOOST_CHECK(coin == entry.second);
 470              }
 471          }
 472  
 473          // One every 10 iterations, remove a random entry from the cache
 474          if (utxoset.size() > 1 && m_rng.randrange(30) == 0) {
 475              stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
 476          }
 477          if (disconnected_coins.size() > 1 && m_rng.randrange(30) == 0) {
 478              stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
 479          }
 480          if (duplicate_coins.size() > 1 && m_rng.randrange(30) == 0) {
 481              stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
 482          }
 483  
 484          if (m_rng.randrange(100) == 0) {
 485              // Every 100 iterations, flush an intermediate cache
 486              if (stack.size() > 1 && m_rng.randbool() == 0) {
 487                  unsigned int flushIndex = m_rng.randrange(stack.size() - 1);
 488                  BOOST_CHECK(stack[flushIndex]->Flush());
 489              }
 490          }
 491          if (m_rng.randrange(100) == 0) {
 492              // Every 100 iterations, change the cache stack.
 493              if (stack.size() > 0 && m_rng.randbool() == 0) {
 494                  BOOST_CHECK(stack.back()->Flush());
 495                  stack.pop_back();
 496              }
 497              if (stack.size() == 0 || (stack.size() < 4 && m_rng.randbool())) {
 498                  CCoinsView* tip = &base;
 499                  if (stack.size() > 0) {
 500                      tip = stack.back().get();
 501                  }
 502                  stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
 503              }
 504          }
 505      }
 506  
 507      // Verify coverage.
 508      BOOST_CHECK(spent_a_duplicate_coinbase);
 509  }
 510  
 511  BOOST_AUTO_TEST_CASE(ccoins_serialization)
 512  {
 513      // Good example
 514      DataStream ss1{"97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35"_hex};
 515      Coin cc1;
 516      ss1 >> cc1;
 517      BOOST_CHECK_EQUAL(cc1.fCoinBase, false);
 518      BOOST_CHECK_EQUAL(cc1.nHeight, 203998U);
 519      BOOST_CHECK_EQUAL(cc1.out.nValue, CAmount{60000000000});
 520      BOOST_CHECK_EQUAL(HexStr(cc1.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("816115944e077fe7c803cfa57f29b36bf87c1d35"_hex_u8)))));
 521  
 522      // Good example
 523      DataStream ss2{"8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex};
 524      Coin cc2;
 525      ss2 >> cc2;
 526      BOOST_CHECK_EQUAL(cc2.fCoinBase, true);
 527      BOOST_CHECK_EQUAL(cc2.nHeight, 120891U);
 528      BOOST_CHECK_EQUAL(cc2.out.nValue, 110397);
 529      BOOST_CHECK_EQUAL(HexStr(cc2.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex_u8)))));
 530  
 531      // Smallest possible example
 532      DataStream ss3{"000006"_hex};
 533      Coin cc3;
 534      ss3 >> cc3;
 535      BOOST_CHECK_EQUAL(cc3.fCoinBase, false);
 536      BOOST_CHECK_EQUAL(cc3.nHeight, 0U);
 537      BOOST_CHECK_EQUAL(cc3.out.nValue, 0);
 538      BOOST_CHECK_EQUAL(cc3.out.scriptPubKey.size(), 0U);
 539  
 540      // scriptPubKey that ends beyond the end of the stream
 541      DataStream ss4{"000007"_hex};
 542      try {
 543          Coin cc4;
 544          ss4 >> cc4;
 545          BOOST_CHECK_MESSAGE(false, "We should have thrown");
 546      } catch (const std::ios_base::failure&) {
 547      }
 548  
 549      // Very large scriptPubKey (3*10^9 bytes) past the end of the stream
 550      DataStream tmp{};
 551      uint64_t x = 3000000000ULL;
 552      tmp << VARINT(x);
 553      BOOST_CHECK_EQUAL(HexStr(tmp), "8a95c0bb00");
 554      DataStream ss5{"00008a95c0bb00"_hex};
 555      try {
 556          Coin cc5;
 557          ss5 >> cc5;
 558          BOOST_CHECK_MESSAGE(false, "We should have thrown");
 559      } catch (const std::ios_base::failure&) {
 560      }
 561  }
 562  
 563  const static COutPoint OUTPOINT;
 564  constexpr CAmount SPENT {-1};
 565  constexpr CAmount ABSENT{-2};
 566  constexpr CAmount VALUE1{100};
 567  constexpr CAmount VALUE2{200};
 568  constexpr CAmount VALUE3{300};
 569  
 570  struct CoinEntry {
 571      enum class State { CLEAN, DIRTY, FRESH, DIRTY_FRESH };
 572  
 573      const CAmount value;
 574      const State state;
 575  
 576      constexpr CoinEntry(const CAmount v, const State s) : value{v}, state{s} {}
 577  
 578      bool operator==(const CoinEntry& o) const = default;
 579      friend std::ostream& operator<<(std::ostream& os, const CoinEntry& e) { return os << e.value << ", " << e.state; }
 580  
 581      constexpr bool IsDirtyFresh() const { return state == State::DIRTY_FRESH; }
 582      constexpr bool IsDirty() const { return state == State::DIRTY || IsDirtyFresh(); }
 583      constexpr bool IsFresh() const { return state == State::FRESH || IsDirtyFresh(); }
 584  
 585      static constexpr State ToState(const bool is_dirty, const bool is_fresh) {
 586          if (is_dirty && is_fresh) return State::DIRTY_FRESH;
 587          if (is_dirty) return State::DIRTY;
 588          if (is_fresh) return State::FRESH;
 589          return State::CLEAN;
 590      }
 591  };
 592  
 593  using MaybeCoin   = std::optional<CoinEntry>;
 594  using CoinOrError = std::variant<MaybeCoin, std::string>;
 595  
 596  constexpr MaybeCoin MISSING           {std::nullopt};
 597  constexpr MaybeCoin SPENT_DIRTY       {{SPENT,  CoinEntry::State::DIRTY}};
 598  constexpr MaybeCoin SPENT_DIRTY_FRESH {{SPENT,  CoinEntry::State::DIRTY_FRESH}};
 599  constexpr MaybeCoin SPENT_FRESH       {{SPENT,  CoinEntry::State::FRESH}};
 600  constexpr MaybeCoin SPENT_CLEAN       {{SPENT,  CoinEntry::State::CLEAN}};
 601  constexpr MaybeCoin VALUE1_DIRTY      {{VALUE1, CoinEntry::State::DIRTY}};
 602  constexpr MaybeCoin VALUE1_DIRTY_FRESH{{VALUE1, CoinEntry::State::DIRTY_FRESH}};
 603  constexpr MaybeCoin VALUE1_FRESH      {{VALUE1, CoinEntry::State::FRESH}};
 604  constexpr MaybeCoin VALUE1_CLEAN      {{VALUE1, CoinEntry::State::CLEAN}};
 605  constexpr MaybeCoin VALUE2_DIRTY      {{VALUE2, CoinEntry::State::DIRTY}};
 606  constexpr MaybeCoin VALUE2_DIRTY_FRESH{{VALUE2, CoinEntry::State::DIRTY_FRESH}};
 607  constexpr MaybeCoin VALUE2_FRESH      {{VALUE2, CoinEntry::State::FRESH}};
 608  constexpr MaybeCoin VALUE2_CLEAN      {{VALUE2, CoinEntry::State::CLEAN}};
 609  constexpr MaybeCoin VALUE3_DIRTY      {{VALUE3, CoinEntry::State::DIRTY}};
 610  constexpr MaybeCoin VALUE3_DIRTY_FRESH{{VALUE3, CoinEntry::State::DIRTY_FRESH}};
 611  
 612  constexpr auto EX_OVERWRITE_UNSPENT{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"};
 613  constexpr auto EX_FRESH_MISAPPLIED {"FRESH flag misapplied to coin that exists in parent cache"};
 614  
 615  static void SetCoinsValue(const CAmount value, Coin& coin)
 616  {
 617      assert(value != ABSENT);
 618      coin.Clear();
 619      assert(coin.IsSpent());
 620      if (value != SPENT) {
 621          coin.out.nValue = value;
 622          coin.nHeight = 1;
 623          assert(!coin.IsSpent());
 624      }
 625  }
 626  
 627  static size_t InsertCoinsMapEntry(CCoinsMap& map, CoinsCachePair& sentinel, const CoinEntry& cache_coin)
 628  {
 629      CCoinsCacheEntry entry;
 630      SetCoinsValue(cache_coin.value, entry.coin);
 631      auto [iter, inserted] = map.emplace(OUTPOINT, std::move(entry));
 632      assert(inserted);
 633      if (cache_coin.IsDirty()) CCoinsCacheEntry::SetDirty(*iter, sentinel);
 634      if (cache_coin.IsFresh()) CCoinsCacheEntry::SetFresh(*iter, sentinel);
 635      return iter->second.coin.DynamicMemoryUsage();
 636  }
 637  
 638  static MaybeCoin GetCoinsMapEntry(const CCoinsMap& map, const COutPoint& outp = OUTPOINT)
 639  {
 640      if (auto it{map.find(outp)}; it != map.end()) {
 641          return CoinEntry{
 642              it->second.coin.IsSpent() ? SPENT : it->second.coin.out.nValue,
 643              CoinEntry::ToState(it->second.IsDirty(), it->second.IsFresh())};
 644      }
 645      return MISSING;
 646  }
 647  
 648  static void WriteCoinsViewEntry(CCoinsView& view, const MaybeCoin& cache_coin)
 649  {
 650      CoinsCachePair sentinel{};
 651      sentinel.second.SelfRef(sentinel);
 652      CCoinsMapMemoryResource resource;
 653      CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
 654      auto usage{cache_coin ? InsertCoinsMapEntry(map, sentinel, *cache_coin) : 0};
 655      auto cursor{CoinsViewCacheCursor(usage, sentinel, map, /*will_erase=*/true)};
 656      BOOST_CHECK(view.BatchWrite(cursor, {}));
 657  }
 658  
 659  class SingleEntryCacheTest
 660  {
 661  public:
 662      SingleEntryCacheTest(const CAmount base_value, const MaybeCoin& cache_coin)
 663      {
 664          auto base_cache_coin{base_value == ABSENT ? MISSING : CoinEntry{base_value, CoinEntry::State::DIRTY}};
 665          WriteCoinsViewEntry(base, base_cache_coin);
 666          if (cache_coin) cache.usage() += InsertCoinsMapEntry(cache.map(), cache.sentinel(), *cache_coin);
 667      }
 668  
 669      CCoinsView root;
 670      CCoinsViewCacheTest base{&root};
 671      CCoinsViewCacheTest cache{&base};
 672  };
 673  
 674  static void CheckAccessCoin(const CAmount base_value, const MaybeCoin& cache_coin, const MaybeCoin& expected)
 675  {
 676      SingleEntryCacheTest test{base_value, cache_coin};
 677      auto& coin = test.cache.AccessCoin(OUTPOINT);
 678      BOOST_CHECK_EQUAL(coin.IsSpent(), !test.cache.GetCoin(OUTPOINT));
 679      test.cache.SelfTest(/*sanity_check=*/false);
 680      BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), expected);
 681  }
 682  
 683  BOOST_AUTO_TEST_CASE(ccoins_access)
 684  {
 685      /* Check AccessCoin behavior, requesting a coin from a cache view layered on
 686       * top of a base view, and checking the resulting entry in the cache after
 687       * the access.
 688       *                  Base        Cache               Expected
 689       */
 690      for (auto base_value : {ABSENT, SPENT, VALUE1}) {
 691          CheckAccessCoin(base_value, MISSING,            base_value == VALUE1 ? VALUE1_CLEAN : MISSING);
 692  
 693          CheckAccessCoin(base_value, SPENT_CLEAN,        SPENT_CLEAN       );
 694          CheckAccessCoin(base_value, SPENT_FRESH,        SPENT_FRESH       );
 695          CheckAccessCoin(base_value, SPENT_DIRTY,        SPENT_DIRTY       );
 696          CheckAccessCoin(base_value, SPENT_DIRTY_FRESH,  SPENT_DIRTY_FRESH );
 697  
 698          CheckAccessCoin(base_value, VALUE2_CLEAN,       VALUE2_CLEAN      );
 699          CheckAccessCoin(base_value, VALUE2_FRESH,       VALUE2_FRESH      );
 700          CheckAccessCoin(base_value, VALUE2_DIRTY,       VALUE2_DIRTY      );
 701          CheckAccessCoin(base_value, VALUE2_DIRTY_FRESH, VALUE2_DIRTY_FRESH);
 702      }
 703  }
 704  
 705  static void CheckSpendCoins(const CAmount base_value, const MaybeCoin& cache_coin, const MaybeCoin& expected)
 706  {
 707      SingleEntryCacheTest test{base_value, cache_coin};
 708      test.cache.SpendCoin(OUTPOINT);
 709      test.cache.SelfTest();
 710      BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), expected);
 711  }
 712  
 713  BOOST_AUTO_TEST_CASE(ccoins_spend)
 714  {
 715      /* Check SpendCoin behavior, requesting a coin from a cache view layered on
 716       * top of a base view, spending, and then checking
 717       * the resulting entry in the cache after the modification.
 718       *                  Base        Cache               Expected
 719       */
 720      for (auto base_value : {ABSENT, SPENT, VALUE1}) {
 721          CheckSpendCoins(base_value, MISSING,            base_value == VALUE1 ? SPENT_DIRTY : MISSING);
 722  
 723          CheckSpendCoins(base_value, SPENT_CLEAN,        SPENT_DIRTY);
 724          CheckSpendCoins(base_value, SPENT_FRESH,        MISSING    );
 725          CheckSpendCoins(base_value, SPENT_DIRTY,        SPENT_DIRTY);
 726          CheckSpendCoins(base_value, SPENT_DIRTY_FRESH,  MISSING    );
 727  
 728          CheckSpendCoins(base_value, VALUE2_CLEAN,       SPENT_DIRTY);
 729          CheckSpendCoins(base_value, VALUE2_FRESH,       MISSING    );
 730          CheckSpendCoins(base_value, VALUE2_DIRTY,       SPENT_DIRTY);
 731          CheckSpendCoins(base_value, VALUE2_DIRTY_FRESH, MISSING    );
 732      }
 733  }
 734  
 735  static void CheckAddCoin(const CAmount base_value, const MaybeCoin& cache_coin, const CAmount modify_value, const CoinOrError& expected, const bool coinbase)
 736  {
 737      SingleEntryCacheTest test{base_value, cache_coin};
 738      bool possible_overwrite{coinbase};
 739      auto add_coin{[&] { test.cache.AddCoin(OUTPOINT, Coin{CTxOut{modify_value, CScript{}}, 1, coinbase}, possible_overwrite); }};
 740      if (auto* expected_coin{std::get_if<MaybeCoin>(&expected)}) {
 741          add_coin();
 742          test.cache.SelfTest();
 743          BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), *expected_coin);
 744      } else {
 745          BOOST_CHECK_EXCEPTION(add_coin(), std::logic_error, HasReason(std::get<std::string>(expected)));
 746      }
 747  }
 748  
 749  BOOST_AUTO_TEST_CASE(ccoins_add)
 750  {
 751      /* Check AddCoin behavior, requesting a new coin from a cache view,
 752       * writing a modification to the coin, and then checking the resulting
 753       * entry in the cache after the modification. Verify behavior with the
 754       * AddCoin coinbase argument set to false, and to true.
 755       *               Base        Cache               Write   Expected              Coinbase
 756       */
 757      for (auto base_value : {ABSENT, SPENT, VALUE1}) {
 758          CheckAddCoin(base_value, MISSING,            VALUE3, VALUE3_DIRTY_FRESH,   false);
 759          CheckAddCoin(base_value, MISSING,            VALUE3, VALUE3_DIRTY,         true );
 760  
 761          CheckAddCoin(base_value, SPENT_CLEAN,        VALUE3, VALUE3_DIRTY_FRESH,   false);
 762          CheckAddCoin(base_value, SPENT_CLEAN,        VALUE3, VALUE3_DIRTY,         true );
 763          CheckAddCoin(base_value, SPENT_FRESH,        VALUE3, VALUE3_DIRTY_FRESH,   false);
 764          CheckAddCoin(base_value, SPENT_FRESH,        VALUE3, VALUE3_DIRTY_FRESH,   true );
 765          CheckAddCoin(base_value, SPENT_DIRTY,        VALUE3, VALUE3_DIRTY,         false);
 766          CheckAddCoin(base_value, SPENT_DIRTY,        VALUE3, VALUE3_DIRTY,         true );
 767          CheckAddCoin(base_value, SPENT_DIRTY_FRESH,  VALUE3, VALUE3_DIRTY_FRESH,   false);
 768          CheckAddCoin(base_value, SPENT_DIRTY_FRESH,  VALUE3, VALUE3_DIRTY_FRESH,   true );
 769  
 770          CheckAddCoin(base_value, VALUE2_CLEAN,       VALUE3, EX_OVERWRITE_UNSPENT, false);
 771          CheckAddCoin(base_value, VALUE2_CLEAN,       VALUE3, VALUE3_DIRTY,         true );
 772          CheckAddCoin(base_value, VALUE2_FRESH,       VALUE3, EX_OVERWRITE_UNSPENT, false);
 773          CheckAddCoin(base_value, VALUE2_FRESH,       VALUE3, VALUE3_DIRTY_FRESH,   true );
 774          CheckAddCoin(base_value, VALUE2_DIRTY,       VALUE3, EX_OVERWRITE_UNSPENT, false);
 775          CheckAddCoin(base_value, VALUE2_DIRTY,       VALUE3, VALUE3_DIRTY,         true );
 776          CheckAddCoin(base_value, VALUE2_DIRTY_FRESH, VALUE3, EX_OVERWRITE_UNSPENT, false);
 777          CheckAddCoin(base_value, VALUE2_DIRTY_FRESH, VALUE3, VALUE3_DIRTY_FRESH,   true );
 778      }
 779  }
 780  
 781  static void CheckWriteCoins(const MaybeCoin& parent, const MaybeCoin& child, const CoinOrError& expected)
 782  {
 783      SingleEntryCacheTest test{ABSENT, parent};
 784      auto write_coins{[&] { WriteCoinsViewEntry(test.cache, child); }};
 785      if (auto* expected_coin{std::get_if<MaybeCoin>(&expected)}) {
 786          write_coins();
 787          test.cache.SelfTest(/*sanity_check=*/false);
 788          BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), *expected_coin);
 789      } else {
 790          BOOST_CHECK_EXCEPTION(write_coins(), std::logic_error, HasReason(std::get<std::string>(expected)));
 791      }
 792  }
 793  
 794  BOOST_AUTO_TEST_CASE(ccoins_write)
 795  {
 796      /* Check BatchWrite behavior, flushing one entry from a child cache to a
 797       * parent cache, and checking the resulting entry in the parent cache
 798       * after the write.
 799       *              Parent              Child               Expected
 800       */
 801      CheckWriteCoins(MISSING,            MISSING,            MISSING            );
 802      CheckWriteCoins(MISSING,            SPENT_DIRTY,        SPENT_DIRTY        );
 803      CheckWriteCoins(MISSING,            SPENT_DIRTY_FRESH,  MISSING            );
 804      CheckWriteCoins(MISSING,            VALUE2_DIRTY,       VALUE2_DIRTY       );
 805      CheckWriteCoins(MISSING,            VALUE2_DIRTY_FRESH, VALUE2_DIRTY_FRESH );
 806      CheckWriteCoins(SPENT_CLEAN,        MISSING,            SPENT_CLEAN        );
 807      CheckWriteCoins(SPENT_FRESH,        MISSING,            SPENT_FRESH        );
 808      CheckWriteCoins(SPENT_DIRTY,        MISSING,            SPENT_DIRTY        );
 809      CheckWriteCoins(SPENT_DIRTY_FRESH,  MISSING,            SPENT_DIRTY_FRESH  );
 810  
 811      CheckWriteCoins(SPENT_CLEAN,        SPENT_DIRTY,        SPENT_DIRTY        );
 812      CheckWriteCoins(SPENT_CLEAN,        SPENT_DIRTY_FRESH,  SPENT_DIRTY        );
 813      CheckWriteCoins(SPENT_FRESH,        SPENT_DIRTY,        MISSING            );
 814      CheckWriteCoins(SPENT_FRESH,        SPENT_DIRTY_FRESH,  MISSING            );
 815      CheckWriteCoins(SPENT_DIRTY,        SPENT_DIRTY,        SPENT_DIRTY        );
 816      CheckWriteCoins(SPENT_DIRTY,        SPENT_DIRTY_FRESH,  SPENT_DIRTY        );
 817      CheckWriteCoins(SPENT_DIRTY_FRESH,  SPENT_DIRTY,        MISSING            );
 818      CheckWriteCoins(SPENT_DIRTY_FRESH,  SPENT_DIRTY_FRESH,  MISSING            );
 819  
 820      CheckWriteCoins(SPENT_CLEAN,        VALUE2_DIRTY,       VALUE2_DIRTY       );
 821      CheckWriteCoins(SPENT_CLEAN,        VALUE2_DIRTY_FRESH, VALUE2_DIRTY       );
 822      CheckWriteCoins(SPENT_FRESH,        VALUE2_DIRTY,       VALUE2_DIRTY_FRESH );
 823      CheckWriteCoins(SPENT_FRESH,        VALUE2_DIRTY_FRESH, VALUE2_DIRTY_FRESH );
 824      CheckWriteCoins(SPENT_DIRTY,        VALUE2_DIRTY,       VALUE2_DIRTY       );
 825      CheckWriteCoins(SPENT_DIRTY,        VALUE2_DIRTY_FRESH, VALUE2_DIRTY       );
 826      CheckWriteCoins(SPENT_DIRTY_FRESH,  VALUE2_DIRTY,       VALUE2_DIRTY_FRESH );
 827      CheckWriteCoins(SPENT_DIRTY_FRESH,  VALUE2_DIRTY_FRESH, VALUE2_DIRTY_FRESH );
 828  
 829      CheckWriteCoins(VALUE1_CLEAN,       MISSING,            VALUE1_CLEAN       );
 830      CheckWriteCoins(VALUE1_FRESH,       MISSING,            VALUE1_FRESH       );
 831      CheckWriteCoins(VALUE1_DIRTY,       MISSING,            VALUE1_DIRTY       );
 832      CheckWriteCoins(VALUE1_DIRTY_FRESH, MISSING,            VALUE1_DIRTY_FRESH );
 833      CheckWriteCoins(VALUE1_CLEAN,       SPENT_DIRTY,        SPENT_DIRTY        );
 834      CheckWriteCoins(VALUE1_CLEAN,       SPENT_DIRTY_FRESH,  EX_FRESH_MISAPPLIED);
 835      CheckWriteCoins(VALUE1_FRESH,       SPENT_DIRTY,        MISSING            );
 836      CheckWriteCoins(VALUE1_FRESH,       SPENT_DIRTY_FRESH,  EX_FRESH_MISAPPLIED);
 837      CheckWriteCoins(VALUE1_DIRTY,       SPENT_DIRTY,        SPENT_DIRTY        );
 838      CheckWriteCoins(VALUE1_DIRTY,       SPENT_DIRTY_FRESH,  EX_FRESH_MISAPPLIED);
 839      CheckWriteCoins(VALUE1_DIRTY_FRESH, SPENT_DIRTY,        MISSING            );
 840      CheckWriteCoins(VALUE1_DIRTY_FRESH, SPENT_DIRTY_FRESH,  EX_FRESH_MISAPPLIED);
 841  
 842      CheckWriteCoins(VALUE1_CLEAN,       VALUE2_DIRTY,       VALUE2_DIRTY       );
 843      CheckWriteCoins(VALUE1_CLEAN,       VALUE2_DIRTY_FRESH, EX_FRESH_MISAPPLIED);
 844      CheckWriteCoins(VALUE1_FRESH,       VALUE2_DIRTY,       VALUE2_DIRTY_FRESH );
 845      CheckWriteCoins(VALUE1_FRESH,       VALUE2_DIRTY_FRESH, EX_FRESH_MISAPPLIED);
 846      CheckWriteCoins(VALUE1_DIRTY,       VALUE2_DIRTY,       VALUE2_DIRTY       );
 847      CheckWriteCoins(VALUE1_DIRTY,       VALUE2_DIRTY_FRESH, EX_FRESH_MISAPPLIED);
 848      CheckWriteCoins(VALUE1_DIRTY_FRESH, VALUE2_DIRTY,       VALUE2_DIRTY_FRESH );
 849      CheckWriteCoins(VALUE1_DIRTY_FRESH, VALUE2_DIRTY_FRESH, EX_FRESH_MISAPPLIED);
 850  
 851      // The checks above omit cases where the child state is not DIRTY, since
 852      // they would be too repetitive (the parent cache is never updated in these
 853      // cases). The loop below covers these cases and makes sure the parent cache
 854      // is always left unchanged.
 855      for (const MaybeCoin& parent : {MISSING,
 856                                      SPENT_CLEAN, SPENT_DIRTY, SPENT_FRESH, SPENT_DIRTY_FRESH,
 857                                      VALUE1_CLEAN, VALUE1_DIRTY, VALUE1_FRESH, VALUE1_DIRTY_FRESH}) {
 858          for (const MaybeCoin& child : {MISSING,
 859                                         SPENT_CLEAN, SPENT_FRESH,
 860                                         VALUE2_CLEAN, VALUE2_FRESH}) {
 861              auto expected{CoinOrError{parent}}; // TODO test failure cases as well
 862              CheckWriteCoins(parent, child, expected);
 863          }
 864      }
 865  }
 866  
 867  struct FlushTest : BasicTestingSetup {
 868  Coin MakeCoin()
 869  {
 870      Coin coin;
 871      coin.out.nValue = m_rng.rand32();
 872      coin.nHeight = m_rng.randrange(4096);
 873      coin.fCoinBase = 0;
 874      return coin;
 875  }
 876  
 877  
 878  //! For CCoinsViewCache instances backed by either another cache instance or
 879  //! leveldb, test cache behavior and flag state (DIRTY/FRESH) by
 880  //!
 881  //! 1. Adding a random coin to the child-most cache,
 882  //! 2. Flushing all caches (without erasing),
 883  //! 3. Ensure the entry still exists in the cache and has been written to parent,
 884  //! 4. (if `do_erasing_flush`) Flushing the caches again (with erasing),
 885  //! 5. (if `do_erasing_flush`) Ensure the entry has been written to the parent and is no longer in the cache,
 886  //! 6. Spend the coin, ensure it no longer exists in the parent.
 887  //!
 888  void TestFlushBehavior(
 889      CCoinsViewCacheTest* view,
 890      CCoinsViewDB& base,
 891      std::vector<std::unique_ptr<CCoinsViewCacheTest>>& all_caches,
 892      bool do_erasing_flush)
 893  {
 894      size_t cache_usage;
 895      size_t cache_size;
 896  
 897      auto flush_all = [this, &all_caches](bool erase) {
 898          // Flush in reverse order to ensure that flushes happen from children up.
 899          for (auto i = all_caches.rbegin(); i != all_caches.rend(); ++i) {
 900              auto& cache = *i;
 901              cache->SanityCheck();
 902              // hashBlock must be filled before flushing to disk; value is
 903              // unimportant here. This is normally done during connect/disconnect block.
 904              cache->SetBestBlock(m_rng.rand256());
 905              erase ? cache->Flush() : cache->Sync();
 906          }
 907      };
 908  
 909      Txid txid = Txid::FromUint256(m_rng.rand256());
 910      COutPoint outp = COutPoint(txid, 0);
 911      Coin coin = MakeCoin();
 912      // Ensure the coins views haven't seen this coin before.
 913      BOOST_CHECK(!base.HaveCoin(outp));
 914      BOOST_CHECK(!view->HaveCoin(outp));
 915  
 916      // --- 1. Adding a random coin to the child cache
 917      //
 918      view->AddCoin(outp, Coin(coin), false);
 919  
 920      cache_usage = view->DynamicMemoryUsage();
 921      cache_size = view->map().size();
 922  
 923      // `base` shouldn't have coin (no flush yet) but `view` should have cached it.
 924      BOOST_CHECK(!base.HaveCoin(outp));
 925      BOOST_CHECK(view->HaveCoin(outp));
 926  
 927      BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), CoinEntry(coin.out.nValue, CoinEntry::State::DIRTY_FRESH));
 928  
 929      // --- 2. Flushing all caches (without erasing)
 930      //
 931      flush_all(/*erase=*/ false);
 932  
 933      // CoinsMap usage should be unchanged since we didn't erase anything.
 934      BOOST_CHECK_EQUAL(cache_usage, view->DynamicMemoryUsage());
 935      BOOST_CHECK_EQUAL(cache_size, view->map().size());
 936  
 937      // --- 3. Ensuring the entry still exists in the cache and has been written to parent
 938      //
 939      BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), CoinEntry(coin.out.nValue, CoinEntry::State::CLEAN)); // State should have been wiped.
 940  
 941      // Both views should now have the coin.
 942      BOOST_CHECK(base.HaveCoin(outp));
 943      BOOST_CHECK(view->HaveCoin(outp));
 944  
 945      if (do_erasing_flush) {
 946          // --- 4. Flushing the caches again (with erasing)
 947          //
 948          flush_all(/*erase=*/ true);
 949  
 950          // Memory does not necessarily go down due to the map using a memory pool
 951          BOOST_TEST(view->DynamicMemoryUsage() <= cache_usage);
 952          // Size of the cache must go down though
 953          BOOST_TEST(view->map().size() < cache_size);
 954  
 955          // --- 5. Ensuring the entry is no longer in the cache
 956          //
 957          BOOST_CHECK(!GetCoinsMapEntry(view->map(), outp));
 958          view->AccessCoin(outp);
 959          BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), CoinEntry(coin.out.nValue, CoinEntry::State::CLEAN));
 960      }
 961  
 962      // Can't overwrite an entry without specifying that an overwrite is
 963      // expected.
 964      BOOST_CHECK_THROW(
 965          view->AddCoin(outp, Coin(coin), /*possible_overwrite=*/ false),
 966          std::logic_error);
 967  
 968      // --- 6. Spend the coin.
 969      //
 970      BOOST_CHECK(view->SpendCoin(outp));
 971  
 972      // The coin should be in the cache, but spent and marked dirty.
 973      BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), SPENT_DIRTY);
 974      BOOST_CHECK(!view->HaveCoin(outp)); // Coin should be considered spent in `view`.
 975      BOOST_CHECK(base.HaveCoin(outp));  // But coin should still be unspent in `base`.
 976  
 977      flush_all(/*erase=*/ false);
 978  
 979      // Coin should be considered spent in both views.
 980      BOOST_CHECK(!view->HaveCoin(outp));
 981      BOOST_CHECK(!base.HaveCoin(outp));
 982  
 983      // Spent coin should not be spendable.
 984      BOOST_CHECK(!view->SpendCoin(outp));
 985  
 986      // --- Bonus check: ensure that a coin added to the base view via one cache
 987      //     can be spent by another cache which has never seen it.
 988      //
 989      txid = Txid::FromUint256(m_rng.rand256());
 990      outp = COutPoint(txid, 0);
 991      coin = MakeCoin();
 992      BOOST_CHECK(!base.HaveCoin(outp));
 993      BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
 994      BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
 995  
 996      all_caches[0]->AddCoin(outp, std::move(coin), false);
 997      all_caches[0]->Sync();
 998      BOOST_CHECK(base.HaveCoin(outp));
 999      BOOST_CHECK(all_caches[0]->HaveCoin(outp));
1000      BOOST_CHECK(!all_caches[1]->HaveCoinInCache(outp));
1001  
1002      BOOST_CHECK(all_caches[1]->SpendCoin(outp));
1003      flush_all(/*erase=*/ false);
1004      BOOST_CHECK(!base.HaveCoin(outp));
1005      BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
1006      BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
1007  
1008      flush_all(/*erase=*/ true); // Erase all cache content.
1009  
1010      // --- Bonus check 2: ensure that a FRESH, spent coin is deleted by Sync()
1011      //
1012      txid = Txid::FromUint256(m_rng.rand256());
1013      outp = COutPoint(txid, 0);
1014      coin = MakeCoin();
1015      CAmount coin_val = coin.out.nValue;
1016      BOOST_CHECK(!base.HaveCoin(outp));
1017      BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
1018      BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
1019  
1020      // Add and spend from same cache without flushing.
1021      all_caches[0]->AddCoin(outp, std::move(coin), false);
1022  
1023      // Coin should be FRESH in the cache.
1024      BOOST_CHECK_EQUAL(GetCoinsMapEntry(all_caches[0]->map(), outp), CoinEntry(coin_val, CoinEntry::State::DIRTY_FRESH));
1025      // Base shouldn't have seen coin.
1026      BOOST_CHECK(!base.HaveCoin(outp));
1027  
1028      BOOST_CHECK(all_caches[0]->SpendCoin(outp));
1029      all_caches[0]->Sync();
1030  
1031      // Ensure there is no sign of the coin after spend/flush.
1032      BOOST_CHECK(!GetCoinsMapEntry(all_caches[0]->map(), outp));
1033      BOOST_CHECK(!all_caches[0]->HaveCoinInCache(outp));
1034      BOOST_CHECK(!base.HaveCoin(outp));
1035  }
1036  }; // struct FlushTest
1037  
1038  BOOST_FIXTURE_TEST_CASE(ccoins_flush_behavior, FlushTest)
1039  {
1040      // Create two in-memory caches atop a leveldb view.
1041      CCoinsViewDB base{{.path = "test", .cache_bytes = 1 << 23, .memory_only = true}, {}};
1042      std::vector<std::unique_ptr<CCoinsViewCacheTest>> caches;
1043      caches.push_back(std::make_unique<CCoinsViewCacheTest>(&base));
1044      caches.push_back(std::make_unique<CCoinsViewCacheTest>(caches.back().get()));
1045  
1046      for (const auto& view : caches) {
1047          TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/false);
1048          TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/true);
1049      }
1050  }
1051  
1052  BOOST_AUTO_TEST_CASE(coins_resource_is_used)
1053  {
1054      CCoinsMapMemoryResource resource;
1055      PoolResourceTester::CheckAllDataAccountedFor(resource);
1056  
1057      {
1058          CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
1059          BOOST_TEST(memusage::DynamicUsage(map) >= resource.ChunkSizeBytes());
1060  
1061          map.reserve(1000);
1062  
1063          // The resource has preallocated a chunk, so we should have space for at several nodes without the need to allocate anything else.
1064          const auto usage_before = memusage::DynamicUsage(map);
1065  
1066          COutPoint out_point{};
1067          for (size_t i = 0; i < 1000; ++i) {
1068              out_point.n = i;
1069              map[out_point];
1070          }
1071          BOOST_TEST(usage_before == memusage::DynamicUsage(map));
1072      }
1073  
1074      PoolResourceTester::CheckAllDataAccountedFor(resource);
1075  }
1076  
1077  BOOST_AUTO_TEST_SUITE_END()
1078