Branch data Line data Source code
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 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
32 : : {
33 [ - + - + ]: 1 : ChainstateManager& manager = *Assert(m_node.chainman);
34 [ - + ]: 1 : CTxMemPool& mempool = *Assert(m_node.mempool);
35 [ + - + - ]: 3 : Chainstate& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool));
36 : 1 : c1.InitCoinsDB(
37 : : /*cache_size_bytes=*/8_MiB, /*in_memory=*/true, /*should_wipe=*/false);
38 [ + - ]: 3 : WITH_LOCK(::cs_main, c1.InitCoinsCache(8_MiB));
39 [ + - + - ]: 1 : 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 : 1 : {
43 : 1 : LOCK(::cs_main);
44 [ + - + - ]: 1 : 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 [ + - + - ]: 1 : c1.CoinsTip().SetBestBlock(m_rng.rand256());
49 : :
50 [ + - + - : 2 : BOOST_CHECK(c1.CoinsTip().HaveCoinInCache(outpoint));
+ - + - +
- ]
51 : :
52 [ + - ]: 1 : 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 [ + - + - : 2 : BOOST_CHECK(c1.CoinsTip().HaveCoinInCache(outpoint));
+ - + - +
- ]
59 : :
60 [ + - ]: 1 : 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 [ + - + - : 2 : BOOST_CHECK(!c1.CoinsTip().HaveCoinInCache(outpoint));
+ - + - +
- ]
67 : 1 : }
68 : 1 : }
69 : :
70 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(connect_tip_does_not_cache_inputs_on_failed_connect, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
71 : : {
72 [ - + ]: 1 : Chainstate& chainstate{Assert(m_node.chainman)->ActiveChainstate()};
73 : :
74 : 1 : COutPoint outpoint;
75 : 1 : {
76 : 1 : LOCK(cs_main);
77 [ + - + - ]: 1 : outpoint = AddTestCoin(m_rng, chainstate.CoinsTip());
78 [ + - + - ]: 1 : chainstate.CoinsTip().Flush(/*reallocate_cache=*/false);
79 : 0 : }
80 : :
81 : 1 : CMutableTransaction tx;
82 [ + - ]: 1 : tx.vin.emplace_back(outpoint);
83 [ + - + - ]: 1 : tx.vout.emplace_back(MAX_MONEY, CScript{} << OP_TRUE);
84 : :
85 [ + + + - ]: 3 : const auto tip{WITH_LOCK(cs_main, return chainstate.m_chain.Tip()->GetBlockHash())};
86 [ + - + - : 3 : const CBlock block{CreateBlock({tx}, CScript{} << OP_TRUE, chainstate)};
+ - + + -
- ]
87 [ + - - + : 3 : BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(std::make_shared<CBlock>(block), true, true, nullptr));
+ - + - +
- + - - +
+ - ]
88 : :
89 [ + - ]: 1 : LOCK(cs_main);
90 [ + - - + : 2 : BOOST_CHECK_EQUAL(tip, chainstate.m_chain.Tip()->GetBlockHash()); // block rejected
+ - ]
91 [ + - + - : 2 : BOOST_CHECK(!chainstate.CoinsTip().HaveCoinInCache(outpoint)); // input not cached
+ - + - +
- ]
92 [ + - ]: 3 : }
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 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(chainstate_update_tip, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
99 : : {
100 [ - + ]: 1 : ChainstateManager& chainman = *Assert(m_node.chainman);
101 : 7 : const auto get_notify_tip{[&]() {
102 : 6 : LOCK(m_node.notifications->m_tip_block_mutex);
103 [ + - + - : 12 : BOOST_REQUIRE(m_node.notifications->TipBlock());
+ - + - ]
104 [ + - + - ]: 6 : return *m_node.notifications->TipBlock();
105 : 7 : }};
106 : 1 : 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 : 1 : mineBlocks(10);
111 : :
112 : : // After adding some blocks to the tip, best block should have changed.
113 [ + - + - ]: 2 : BOOST_CHECK(get_notify_tip() != curr_tip);
114 : :
115 : : // Grab block 1 from disk; we'll add it to the background chain later.
116 : 1 : std::shared_ptr<CBlock> pblockone = std::make_shared<CBlock>();
117 : 1 : {
118 [ + - ]: 1 : LOCK(::cs_main);
119 [ + - - + : 2 : chainman.m_blockman.ReadBlock(*pblockone, *chainman.ActiveChain()[1]);
+ - ]
120 : 0 : }
121 : :
122 [ + - + - : 2 : BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(
+ - + - ]
123 : : this, NoMalleation, /*reset_chainstate=*/ true));
124 : :
125 : : // Ensure our active chain is the snapshot chainstate.
126 [ + - + - : 3 : BOOST_CHECK(WITH_LOCK(::cs_main, return chainman.CurrentChainstate().m_from_snapshot_blockhash));
+ - + - ]
127 : :
128 [ + - ]: 1 : curr_tip = get_notify_tip();
129 : :
130 : : // Mine a new block on top of the activated snapshot chainstate.
131 [ + - ]: 1 : mineBlocks(1); // Defined in TestChain100Setup.
132 : :
133 : : // After adding some blocks to the snapshot tip, best block should have changed.
134 [ + - + - : 2 : BOOST_CHECK(get_notify_tip() != curr_tip);
+ - + - ]
135 : :
136 [ + - ]: 1 : curr_tip = get_notify_tip();
137 : :
138 [ + - - + ]: 2 : Chainstate& background_cs{*Assert(WITH_LOCK(::cs_main, return chainman.HistoricalChainstate()))};
139 : :
140 : : // Append the first block to the background chain.
141 [ + - ]: 1 : BlockValidationState state;
142 : 1 : CBlockIndex* pindex = nullptr;
143 [ + - ]: 1 : const CChainParams& chainparams = Params();
144 : 1 : 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 : 1 : {
149 [ + - ]: 1 : LOCK(::cs_main);
150 [ + - ]: 1 : bool checked = CheckBlock(*pblockone, state, chainparams.GetConsensus());
151 [ + - + - : 2 : BOOST_CHECK(checked);
+ - ]
152 [ + - + - ]: 2 : bool accepted = chainman.AcceptBlock(
153 [ + - ]: 1 : pblockone, state, &pindex, true, nullptr, &newblock, true);
154 [ + - + - : 2 : BOOST_CHECK(accepted);
+ - ]
155 : 0 : }
156 : :
157 : : // UpdateTip is called here
158 [ + - + - : 2 : bool block_added = background_cs.ActivateBestChain(state, pblockone);
+ - ]
159 : :
160 : : // Ensure tip is as expected
161 [ + - + - : 2 : 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 [ + - + - : 2 : BOOST_CHECK(block_added);
+ - ]
166 [ + - + - : 1 : BOOST_CHECK_EQUAL(curr_tip, get_notify_tip());
+ - ]
167 [ + - ]: 2 : }
168 : :
169 : : BOOST_AUTO_TEST_SUITE_END()
|