/ src / test / validation_chainstate_tests.cpp
validation_chainstate_tests.cpp
  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 <chainparams.h>
  6  #include <consensus/amount.h>
  7  #include <consensus/validation.h>
  8  #include <node/kernel_notifications.h>
  9  #include <random.h>
 10  #include <rpc/blockchain.h>
 11  #include <script/script.h>
 12  #include <sync.h>
 13  #include <test/util/chainstate.h>
 14  #include <test/util/common.h>
 15  #include <test/util/coins.h>
 16  #include <test/util/random.h>
 17  #include <test/util/setup_common.h>
 18  #include <uint256.h>
 19  #include <util/check.h>
 20  #include <validation.h>
 21  
 22  #include <vector>
 23  
 24  #include <boost/test/unit_test.hpp>
 25  
 26  BOOST_FIXTURE_TEST_SUITE(validation_chainstate_tests, ChainTestingSetup)
 27  
 28  //! Test resizing coins-related Chainstate caches during runtime.
 29  //!
 30  BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
 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  BOOST_FIXTURE_TEST_CASE(connect_tip_does_not_cache_inputs_on_failed_connect, TestChain100Setup)
 70  {
 71      Chainstate& chainstate{Assert(m_node.chainman)->ActiveChainstate()};
 72  
 73      COutPoint outpoint;
 74      {
 75          LOCK(cs_main);
 76          outpoint = AddTestCoin(m_rng, chainstate.CoinsTip());
 77          chainstate.CoinsTip().Flush(/*reallocate_cache=*/false);
 78      }
 79  
 80      CMutableTransaction tx;
 81      tx.vin.emplace_back(outpoint);
 82      tx.vout.emplace_back(MAX_MONEY, CScript{} << OP_TRUE);
 83  
 84      const auto tip{WITH_LOCK(cs_main, return chainstate.m_chain.Tip()->GetBlockHash())};
 85      const CBlock block{CreateBlock({tx}, CScript{} << OP_TRUE, chainstate)};
 86      BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(std::make_shared<CBlock>(block), true, true, nullptr));
 87  
 88      LOCK(cs_main);
 89      BOOST_CHECK_EQUAL(tip, chainstate.m_chain.Tip()->GetBlockHash()); // block rejected
 90      BOOST_CHECK(!chainstate.CoinsTip().HaveCoinInCache(outpoint));    // input not cached
 91  }
 92  
 93  //! Test UpdateTip behavior for both active and background chainstates.
 94  //!
 95  //! When run on the background chainstate, UpdateTip should do a subset
 96  //! of what it does for the active chainstate.
 97  BOOST_FIXTURE_TEST_CASE(chainstate_update_tip, TestChain100Setup)
 98  {
 99      ChainstateManager& chainman = *Assert(m_node.chainman);
100      const auto get_notify_tip{[&]() {
101          LOCK(m_node.notifications->m_tip_block_mutex);
102          BOOST_REQUIRE(m_node.notifications->TipBlock());
103          return *m_node.notifications->TipBlock();
104      }};
105      uint256 curr_tip = get_notify_tip();
106  
107      // Mine 10 more blocks, putting at us height 110 where a valid assumeutxo value can
108      // be found.
109      mineBlocks(10);
110  
111      // After adding some blocks to the tip, best block should have changed.
112      BOOST_CHECK(get_notify_tip() != curr_tip);
113  
114      // Grab block 1 from disk; we'll add it to the background chain later.
115      std::shared_ptr<CBlock> pblockone = std::make_shared<CBlock>();
116      {
117          LOCK(::cs_main);
118          chainman.m_blockman.ReadBlock(*pblockone, *chainman.ActiveChain()[1]);
119      }
120  
121      BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(
122          this, NoMalleation, /*reset_chainstate=*/ true));
123  
124      // Ensure our active chain is the snapshot chainstate.
125      BOOST_CHECK(WITH_LOCK(::cs_main, return chainman.CurrentChainstate().m_from_snapshot_blockhash));
126  
127      curr_tip = get_notify_tip();
128  
129      // Mine a new block on top of the activated snapshot chainstate.
130      mineBlocks(1);  // Defined in TestChain100Setup.
131  
132      // After adding some blocks to the snapshot tip, best block should have changed.
133      BOOST_CHECK(get_notify_tip() != curr_tip);
134  
135      curr_tip = get_notify_tip();
136  
137      Chainstate& background_cs{*Assert(WITH_LOCK(::cs_main, return chainman.HistoricalChainstate()))};
138  
139      // Append the first block to the background chain.
140      BlockValidationState state;
141      CBlockIndex* pindex = nullptr;
142      const CChainParams& chainparams = Params();
143      bool newblock = false;
144  
145      // TODO: much of this is inlined from ProcessNewBlock(); just reuse PNB()
146      // once it is changed to support multiple chainstates.
147      {
148          LOCK(::cs_main);
149          bool checked = CheckBlock(*pblockone, state, chainparams.GetConsensus());
150          BOOST_CHECK(checked);
151          bool accepted = chainman.AcceptBlock(
152              pblockone, state, &pindex, true, nullptr, &newblock, true);
153          BOOST_CHECK(accepted);
154      }
155  
156      // UpdateTip is called here
157      bool block_added = background_cs.ActivateBestChain(state, pblockone);
158  
159      // Ensure tip is as expected
160      BOOST_CHECK_EQUAL(background_cs.m_chain.Tip()->GetBlockHash(), pblockone->GetHash());
161  
162      // get_notify_tip() should be unchanged after adding a block to the background
163      // validation chain.
164      BOOST_CHECK(block_added);
165      BOOST_CHECK_EQUAL(curr_tip, get_notify_tip());
166  }
167  
168  BOOST_AUTO_TEST_SUITE_END()