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