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