validation_chainstate_tests.cpp raw

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