validation_chainstate_tests.cpp raw

   1  // Copyright (c) 2020-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 <chainparams.h>
   6  #include <consensus/validation.h>
   7  #include <node/kernel_notifications.h>
   8  #include <random.h>
   9  #include <rpc/blockchain.h>
  10  #include <sync.h>
  11  #include <test/util/chainstate.h>
  12  #include <test/util/coins.h>
  13  #include <test/util/random.h>
  14  #include <test/util/setup_common.h>
  15  #include <uint256.h>
  16  #include <util/check.h>
  17  #include <util/mempressure.h>
  18  #include <validation.h>
  19  
  20  #include <vector>
  21  
  22  #include <boost/test/unit_test.hpp>
  23  
  24  BOOST_FIXTURE_TEST_SUITE(validation_chainstate_tests, ChainTestingSetup)
  25  
  26  //! Test resizing coins-related Chainstate caches during runtime.
  27  //!
  28  BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
  29  {
  30      g_low_memory_threshold = 0;  // disable to get deterministic flushing
  31  
  32      ChainstateManager& manager = *Assert(m_node.chainman);
  33      CTxMemPool& mempool = *Assert(m_node.mempool);
  34      Chainstate& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool));
  35      c1.InitCoinsDB(
  36          /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
  37      WITH_LOCK(::cs_main, c1.InitCoinsCache(1 << 23));
  38      BOOST_REQUIRE(c1.LoadGenesisBlock()); // Need at least one block loaded to be able to flush caches
  39  
  40      // Add a coin to the in-memory cache, upsize once, then downsize.
  41      {
  42          LOCK(::cs_main);
  43          const auto outpoint = AddTestCoin(m_rng, c1.CoinsTip());
  44  
  45          // Set a meaningless bestblock value in the coinsview cache - otherwise we won't
  46          // flush during ResizecoinsCaches() and will subsequently hit an assertion.
  47          c1.CoinsTip().SetBestBlock(m_rng.rand256());
  48  
  49          BOOST_CHECK(c1.CoinsTip().HaveCoinInCache(outpoint));
  50  
  51          c1.ResizeCoinsCaches(
  52              1 << 24,  // upsizing the coinsview cache
  53              1 << 22  // downsizing the coinsdb cache
  54          );
  55  
  56          // View should still have the coin cached, since we haven't destructed the cache on upsize.
  57          BOOST_CHECK(c1.CoinsTip().HaveCoinInCache(outpoint));
  58  
  59          c1.ResizeCoinsCaches(
  60              1 << 22,  // downsizing the coinsview cache
  61              1 << 23  // upsizing the coinsdb cache
  62          );
  63  
  64          // The view cache should be empty since we had to destruct to downsize.
  65          BOOST_CHECK(!c1.CoinsTip().HaveCoinInCache(outpoint));
  66      }
  67  }
  68  
  69  //! Test UpdateTip behavior for both active and background chainstates.
  70  //!
  71  //! When run on the background chainstate, UpdateTip should do a subset
  72  //! of what it does for the active chainstate.
  73  BOOST_FIXTURE_TEST_CASE(chainstate_update_tip, TestChain100Setup)
  74  {
  75      ChainstateManager& chainman = *Assert(m_node.chainman);
  76      const auto get_notify_tip{[&]() {
  77          LOCK(m_node.notifications->m_tip_block_mutex);
  78          BOOST_REQUIRE(m_node.notifications->TipBlock());
  79          return *m_node.notifications->TipBlock();
  80      }};
  81      uint256 curr_tip = get_notify_tip();
  82  
  83      // Mine 10 more blocks, putting at us height 110 where a valid assumeutxo value can
  84      // be found.
  85      mineBlocks(10);
  86  
  87      // After adding some blocks to the tip, best block should have changed.
  88      BOOST_CHECK(get_notify_tip() != curr_tip);
  89  
  90      // Grab block 1 from disk; we'll add it to the background chain later.
  91      std::shared_ptr<CBlock> pblockone = std::make_shared<CBlock>();
  92      {
  93          LOCK(::cs_main);
  94          chainman.m_blockman.ReadBlock(*pblockone, *chainman.ActiveChain()[1]);
  95      }
  96  
  97      BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(
  98          this, NoMalleation, /*reset_chainstate=*/ true));
  99  
 100      // Ensure our active chain is the snapshot chainstate.
 101      BOOST_CHECK(WITH_LOCK(::cs_main, return chainman.IsSnapshotActive()));
 102  
 103      curr_tip = get_notify_tip();
 104  
 105      // Mine a new block on top of the activated snapshot chainstate.
 106      mineBlocks(1);  // Defined in TestChain100Setup.
 107  
 108      // After adding some blocks to the snapshot tip, best block should have changed.
 109      BOOST_CHECK(get_notify_tip() != curr_tip);
 110  
 111      curr_tip = get_notify_tip();
 112  
 113      BOOST_CHECK_EQUAL(chainman.GetAll().size(), 2);
 114  
 115      Chainstate& background_cs{*Assert([&]() -> Chainstate* {
 116          for (Chainstate* cs : chainman.GetAll()) {
 117              if (cs != &chainman.ActiveChainstate()) {
 118                  return cs;
 119              }
 120          }
 121          return nullptr;
 122      }())};
 123  
 124      // Append the first block to the background chain.
 125      BlockValidationState state;
 126      CBlockIndex* pindex = nullptr;
 127      const CChainParams& chainparams = Params();
 128      bool newblock = false;
 129  
 130      // TODO: much of this is inlined from ProcessNewBlock(); just reuse PNB()
 131      // once it is changed to support multiple chainstates.
 132      {
 133          LOCK(::cs_main);
 134          bool checked = CheckBlock(*pblockone, state, chainparams.GetConsensus());
 135          BOOST_CHECK(checked);
 136          bool accepted = chainman.AcceptBlock(
 137              pblockone, state, &pindex, true, nullptr, &newblock, true);
 138          BOOST_CHECK(accepted);
 139      }
 140  
 141      // UpdateTip is called here
 142      bool block_added = background_cs.ActivateBestChain(state, pblockone);
 143  
 144      // Ensure tip is as expected
 145      BOOST_CHECK_EQUAL(background_cs.m_chain.Tip()->GetBlockHash(), pblockone->GetHash());
 146  
 147      // get_notify_tip() should be unchanged after adding a block to the background
 148      // validation chain.
 149      BOOST_CHECK(block_added);
 150      BOOST_CHECK_EQUAL(curr_tip, get_notify_tip());
 151  }
 152  
 153  BOOST_AUTO_TEST_SUITE_END()
 154