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 <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 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
39 : : {
40 [ - + - + ]: 1 : ChainstateManager& manager = *Assert(m_node.chainman);
41 [ - + ]: 1 : CTxMemPool& mempool = *Assert(m_node.mempool);
42 [ + - + - ]: 3 : Chainstate& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool));
43 : 1 : c1.InitCoinsDB(
44 : : /*cache_size_bytes=*/8_MiB, /*in_memory=*/true, /*should_wipe=*/false);
45 [ + - ]: 3 : WITH_LOCK(::cs_main, c1.InitCoinsCache(8_MiB));
46 [ + - + - ]: 2 : BOOST_REQUIRE(c1.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 : 1 : {
50 : 1 : LOCK(::cs_main);
51 [ + - + - ]: 1 : 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 [ + - + - ]: 1 : c1.CoinsTip().SetBestBlock(m_rng.rand256());
56 : :
57 [ + - + - : 2 : BOOST_CHECK(c1.CoinsTip().HaveCoinInCache(outpoint));
+ - + - +
- ]
58 : :
59 [ + - ]: 1 : 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 [ + - + - : 2 : BOOST_CHECK(c1.CoinsTip().HaveCoinInCache(outpoint));
+ - + - +
- ]
66 : :
67 [ + - ]: 1 : 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 [ + - + - : 2 : BOOST_CHECK(!c1.CoinsTip().HaveCoinInCache(outpoint));
+ - + - +
- ]
74 : 1 : }
75 : 1 : }
76 : :
77 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(connect_tip_does_not_cache_inputs_on_failed_connect, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
78 : : {
79 [ - + ]: 1 : Chainstate& chainstate{Assert(m_node.chainman)->ActiveChainstate()};
80 : :
81 : 1 : COutPoint outpoint;
82 : 1 : {
83 : 1 : LOCK(cs_main);
84 [ + - + - ]: 1 : outpoint = AddTestCoin(m_rng, chainstate.CoinsTip());
85 [ + - + - ]: 1 : chainstate.CoinsTip().Flush(/*reallocate_cache=*/false);
86 : 0 : }
87 : :
88 : 1 : CMutableTransaction tx;
89 [ + - ]: 1 : tx.vin.emplace_back(outpoint);
90 [ + - + - ]: 1 : tx.vout.emplace_back(MAX_MONEY, CScript{} << OP_TRUE);
91 : :
92 [ + + + - ]: 3 : const auto tip{WITH_LOCK(cs_main, return chainstate.m_chain.Tip()->GetBlockHash())};
93 [ + - + - : 3 : const CBlock block{CreateBlock({tx}, CScript{} << OP_TRUE)};
+ - + + -
- ]
94 [ + - - + : 3 : BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(std::make_shared<CBlock>(block), true, true, nullptr));
+ - + - +
- + - - +
+ - ]
95 : :
96 [ + - ]: 1 : LOCK(cs_main);
97 [ + - - + : 2 : BOOST_CHECK_EQUAL(tip, chainstate.m_chain.Tip()->GetBlockHash()); // block rejected
+ - ]
98 [ + - + - : 2 : BOOST_CHECK(!chainstate.CoinsTip().HaveCoinInCache(outpoint)); // input not cached
+ - + - +
- ]
99 [ + - ]: 3 : }
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 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(chainstate_update_tip, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
106 : : {
107 [ - + ]: 1 : ChainstateManager& chainman = *Assert(m_node.chainman);
108 : 7 : const auto get_notify_tip{[&]() {
109 : 6 : LOCK(m_node.notifications->m_tip_block_mutex);
110 [ + - + - : 12 : BOOST_REQUIRE(m_node.notifications->TipBlock());
+ - + - ]
111 [ + - + - ]: 6 : return *m_node.notifications->TipBlock();
112 : 7 : }};
113 : 1 : 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 : 1 : mineBlocks(10);
118 : :
119 : : // After adding some blocks to the tip, best block should have changed.
120 [ + - + - ]: 2 : BOOST_CHECK(get_notify_tip() != curr_tip);
121 : :
122 : : // Grab block 1 from disk; we'll add it to the background chain later.
123 : 1 : std::shared_ptr<CBlock> pblockone = std::make_shared<CBlock>();
124 : 1 : {
125 [ + - ]: 1 : LOCK(::cs_main);
126 [ + - - + : 2 : chainman.m_blockman.ReadBlock(*pblockone, *chainman.ActiveChain()[1]);
+ - ]
127 : 0 : }
128 : :
129 [ + - + - : 2 : BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(
+ - + - ]
130 : : this, NoMalleation, /*reset_chainstate=*/ true));
131 : :
132 : : // Ensure our active chain is the snapshot chainstate.
133 [ + - + - : 3 : BOOST_CHECK(WITH_LOCK(::cs_main, return chainman.CurrentChainstate().m_from_snapshot_blockhash));
+ - + - ]
134 : :
135 [ + - ]: 1 : curr_tip = get_notify_tip();
136 : :
137 : : // Mine a new block on top of the activated snapshot chainstate.
138 [ + - ]: 1 : mineBlocks(1); // Defined in TestChain100Setup.
139 : :
140 : : // After adding some blocks to the snapshot tip, best block should have changed.
141 [ + - + - : 2 : BOOST_CHECK(get_notify_tip() != curr_tip);
+ - + - ]
142 : :
143 [ + - ]: 1 : curr_tip = get_notify_tip();
144 : :
145 [ + - - + ]: 2 : Chainstate& background_cs{*Assert(WITH_LOCK(::cs_main, return chainman.HistoricalChainstate()))};
146 : :
147 : : // Append the first block to the background chain.
148 [ + - ]: 1 : BlockValidationState state;
149 : 1 : CBlockIndex* pindex = nullptr;
150 [ + - ]: 1 : const CChainParams& chainparams = Params();
151 : 1 : 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 : 1 : {
156 [ + - ]: 1 : LOCK(::cs_main);
157 [ + - ]: 1 : bool checked = CheckBlock(*pblockone, state, chainparams.GetConsensus());
158 [ + - + - : 2 : BOOST_CHECK(checked);
+ - ]
159 [ + - + - ]: 2 : bool accepted = chainman.AcceptBlock(
160 [ + - ]: 1 : pblockone, state, &pindex, true, nullptr, &newblock, true);
161 [ + - + - : 2 : BOOST_CHECK(accepted);
+ - ]
162 : 0 : }
163 : :
164 : : // UpdateTip is called here
165 [ + - + - : 2 : bool block_added = background_cs.ActivateBestChain(state, pblockone);
+ - ]
166 : :
167 : : // Ensure tip is as expected
168 [ + - + - : 2 : 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 [ + - + - : 2 : BOOST_CHECK(block_added);
+ - ]
173 [ + - + - : 1 : BOOST_CHECK_EQUAL(curr_tip, get_notify_tip());
+ - ]
174 [ + - ]: 2 : }
175 : :
176 : : BOOST_AUTO_TEST_SUITE_END()
|