Branch data Line data Source code
1 : : // Copyright (c) 2019-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/validation.h>
7 : : #include <kernel/disconnected_transactions.h>
8 : : #include <node/chainstatemanager_args.h>
9 : : #include <node/kernel_notifications.h>
10 : : #include <node/utxo_snapshot.h>
11 : : #include <random.h>
12 : : #include <rpc/blockchain.h>
13 : : #include <sync.h>
14 : : #include <test/util/chainstate.h>
15 : : #include <test/util/logging.h>
16 : : #include <test/util/random.h>
17 : : #include <test/util/setup_common.h>
18 : : #include <test/util/validation.h>
19 : : #include <uint256.h>
20 : : #include <util/result.h>
21 : : #include <util/vector.h>
22 : : #include <validation.h>
23 : : #include <validationinterface.h>
24 : :
25 : : #include <tinyformat.h>
26 : :
27 : : #include <vector>
28 : :
29 : : #include <boost/test/unit_test.hpp>
30 : :
31 : : using node::BlockManager;
32 : : using node::KernelNotifications;
33 : : using node::SnapshotMetadata;
34 : :
35 : : BOOST_FIXTURE_TEST_SUITE(validation_chainstatemanager_tests, TestingSetup)
36 : :
37 : : //! Basic tests for ChainstateManager.
38 : : //!
39 : : //! First create a legacy (IBD) chainstate, then create a snapshot chainstate.
40 [ + - + - : 10 : BOOST_FIXTURE_TEST_CASE(chainstatemanager, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - ]
41 : : {
42 : 1 : ChainstateManager& manager = *m_node.chainman;
43 : :
44 [ + - + - ]: 3 : BOOST_CHECK(WITH_LOCK(::cs_main, return !manager.CurrentChainstate().m_from_snapshot_blockhash));
45 : :
46 : : // Create a legacy (IBD) chainstate.
47 : : //
48 : 1 : Chainstate& c1 = manager.ActiveChainstate();
49 : :
50 [ + - + - ]: 3 : BOOST_CHECK(WITH_LOCK(::cs_main, return !manager.CurrentChainstate().m_from_snapshot_blockhash));
51 : 1 : {
52 : 1 : LOCK(manager.GetMutex());
53 [ + - - + : 1 : BOOST_CHECK_EQUAL(manager.m_chainstates.size(), 1);
+ - ]
54 [ + - + - : 1 : BOOST_CHECK_EQUAL(manager.m_chainstates[0].get(), &c1);
+ - ]
55 : 0 : }
56 : :
57 [ + - + - ]: 3 : auto& active_chain = WITH_LOCK(manager.GetMutex(), return manager.ActiveChain());
58 [ + - ]: 1 : BOOST_CHECK_EQUAL(&active_chain, &c1.m_chain);
59 : :
60 : : // Get to a valid assumeutxo tip (per chainparams);
61 : 1 : mineBlocks(10);
62 [ + - + - ]: 3 : BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 110);
63 [ + - + - ]: 3 : auto active_tip = WITH_LOCK(manager.GetMutex(), return manager.ActiveTip());
64 [ - + ]: 1 : auto exp_tip = c1.m_chain.Tip();
65 [ + - ]: 1 : BOOST_CHECK_EQUAL(active_tip, exp_tip);
66 : :
67 [ + - + - ]: 3 : BOOST_CHECK(WITH_LOCK(::cs_main, return !manager.CurrentChainstate().m_from_snapshot_blockhash));
68 : :
69 : : // Create a snapshot-based chainstate.
70 : : //
71 : 1 : const uint256 snapshot_blockhash = active_tip->GetBlockHash();
72 [ + - + - : 3 : Chainstate& c2{WITH_LOCK(::cs_main, return manager.AddChainstate(std::make_unique<Chainstate>(nullptr, manager.m_blockman, manager, snapshot_blockhash)))};
+ - ]
73 : 1 : c2.InitCoinsDB(
74 : : /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
75 : 1 : {
76 : 1 : LOCK(::cs_main);
77 [ + - ]: 1 : c2.InitCoinsCache(1 << 23);
78 [ + - + - ]: 1 : c2.CoinsTip().SetBestBlock(active_tip->GetBlockHash());
79 [ + - + - ]: 1 : c2.setBlockIndexCandidates.insert(manager.m_blockman.LookupBlockIndex(active_tip->GetBlockHash()));
80 [ + - ]: 1 : c2.LoadChainTip();
81 : 0 : }
82 [ + - ]: 1 : BlockValidationState _;
83 [ + - + - : 2 : BOOST_CHECK(c2.ActivateBestChain(_, nullptr));
+ - + - -
+ + - ]
84 : :
85 [ + - + - : 2 : BOOST_CHECK_EQUAL(WITH_LOCK(::cs_main, return *manager.CurrentChainstate().m_from_snapshot_blockhash), snapshot_blockhash);
+ - ]
86 [ + - + - : 3 : BOOST_CHECK(WITH_LOCK(::cs_main, return manager.CurrentChainstate().m_assumeutxo == Assumeutxo::UNVALIDATED));
+ - + - ]
87 [ + - + - : 1 : BOOST_CHECK_EQUAL(&c2, &manager.ActiveChainstate());
+ - ]
88 [ + - + - : 2 : BOOST_CHECK(&c1 != &manager.ActiveChainstate());
+ - + - ]
89 : 1 : {
90 [ + - ]: 1 : LOCK(manager.GetMutex());
91 [ + - - + : 1 : BOOST_CHECK_EQUAL(manager.m_chainstates.size(), 2);
+ - ]
92 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(manager.m_chainstates[0].get(), &c1);
93 [ + - + - : 1 : BOOST_CHECK_EQUAL(manager.m_chainstates[1].get(), &c2);
+ - ]
94 : 0 : }
95 : :
96 [ + - + - ]: 3 : auto& active_chain2 = WITH_LOCK(manager.GetMutex(), return manager.ActiveChain());
97 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(&active_chain2, &c2.m_chain);
98 : :
99 [ + - + - : 3 : BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 110);
+ - ]
100 [ + - ]: 1 : mineBlocks(1);
101 [ + - + - : 3 : BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 111);
+ - ]
102 [ + + + - : 2 : BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return c1.m_chain.Height()), 110);
+ - ]
103 : :
104 [ + - + - ]: 3 : auto active_tip2 = WITH_LOCK(manager.GetMutex(), return manager.ActiveTip());
105 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(active_tip, active_tip2->pprev);
106 [ + - - + : 2 : BOOST_CHECK_EQUAL(active_tip, c1.m_chain.Tip());
+ - ]
107 [ + - - + : 2 : BOOST_CHECK_EQUAL(active_tip2, c2.m_chain.Tip());
+ - ]
108 : :
109 : : // Let scheduler events finish running to avoid accessing memory that is going to be unloaded
110 [ + - ]: 1 : m_node.validation_signals->SyncWithValidationInterfaceQueue();
111 : 1 : }
112 : :
113 : : //! Test rebalancing the caches associated with each chainstate.
114 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_rebalance_caches, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
115 : : {
116 [ + - ]: 1 : ChainstateManager& manager = *m_node.chainman;
117 : :
118 : 1 : size_t max_cache = 10000;
119 : 1 : manager.m_total_coinsdb_cache = max_cache;
120 : 1 : manager.m_total_coinstip_cache = max_cache;
121 : :
122 : 1 : std::vector<Chainstate*> chainstates;
123 : :
124 : : // Create a legacy (IBD) chainstate.
125 : : //
126 [ + - ]: 1 : Chainstate& c1 = manager.ActiveChainstate();
127 [ + - ]: 1 : chainstates.push_back(&c1);
128 : 1 : {
129 [ + - ]: 1 : LOCK(::cs_main);
130 [ + - ]: 1 : c1.InitCoinsCache(1 << 23);
131 [ + - ]: 1 : manager.MaybeRebalanceCaches();
132 : 0 : }
133 : :
134 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(c1.m_coinstip_cache_size_bytes, max_cache);
135 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(c1.m_coinsdb_cache_size_bytes, max_cache);
136 : :
137 : : // Create a snapshot-based chainstate.
138 : : //
139 [ + - + - : 4 : CBlockIndex* snapshot_base{WITH_LOCK(manager.GetMutex(), return manager.ActiveChain()[manager.ActiveChain().Height() / 2])};
- + + - +
- ]
140 [ + - + - : 3 : Chainstate& c2{WITH_LOCK(::cs_main, return manager.AddChainstate(std::make_unique<Chainstate>(nullptr, manager.m_blockman, manager, *snapshot_base->phashBlock)))};
+ - ]
141 [ + - ]: 1 : chainstates.push_back(&c2);
142 [ + - ]: 1 : c2.InitCoinsDB(
143 : : /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
144 : :
145 : : // Reset IBD state so IsInitialBlockDownload() returns true and causes
146 : : // MaybeRebalancesCaches() to prioritize the snapshot chainstate, giving it
147 : : // more cache space than the snapshot chainstate. Calling ResetIbd() is
148 : : // necessary because m_cached_finished_ibd is already latched to true before
149 : : // the test starts due to the test setup. After ResetIbd() is called.
150 : : // IsInitialBlockDownload will return true because at this point the active
151 : : // chainstate has a null chain tip.
152 [ + - ]: 1 : static_cast<TestChainstateManager&>(manager).ResetIbd();
153 : :
154 : 1 : {
155 [ + - ]: 1 : LOCK(::cs_main);
156 [ + - ]: 1 : c2.InitCoinsCache(1 << 23);
157 [ + - ]: 1 : manager.MaybeRebalanceCaches();
158 : 0 : }
159 : :
160 [ + - + - ]: 1 : BOOST_CHECK_CLOSE(double(c1.m_coinstip_cache_size_bytes), max_cache * 0.05, 1);
161 [ + - + - ]: 1 : BOOST_CHECK_CLOSE(double(c1.m_coinsdb_cache_size_bytes), max_cache * 0.05, 1);
162 [ + - + - ]: 1 : BOOST_CHECK_CLOSE(double(c2.m_coinstip_cache_size_bytes), max_cache * 0.95, 1);
163 [ + - + - ]: 1 : BOOST_CHECK_CLOSE(double(c2.m_coinsdb_cache_size_bytes), max_cache * 0.95, 1);
164 : 1 : }
165 : :
166 : 8 : struct SnapshotTestSetup : TestChain100Setup {
167 : : // Run with coinsdb on the filesystem to support, e.g., moving invalidated
168 : : // chainstate dirs to "*_invalid".
169 : : //
170 : : // Note that this means the tests run considerably slower than in-memory DB
171 : : // tests, but we can't otherwise test this functionality since it relies on
172 : : // destructive filesystem operations.
173 : 4 : SnapshotTestSetup() : TestChain100Setup{
174 : : {},
175 : : {
176 : : .coins_db_in_memory = false,
177 : : .block_tree_db_in_memory = false,
178 : : },
179 [ + - ]: 4 : }
180 : : {
181 : 4 : }
182 : :
183 : 4 : std::tuple<Chainstate*, Chainstate*> SetupSnapshot()
184 : : {
185 [ - + ]: 4 : ChainstateManager& chainman = *Assert(m_node.chainman);
186 : :
187 : 4 : {
188 : 4 : LOCK(::cs_main);
189 [ + - + - : 8 : BOOST_CHECK(!chainman.CurrentChainstate().m_from_snapshot_blockhash);
+ - ]
190 [ + - + - : 12 : BOOST_CHECK(!node::FindAssumeutxoChainstateDir(chainman.m_options.datadir));
+ - - + +
- ]
191 : 0 : }
192 : :
193 : 4 : size_t initial_size;
194 : 4 : size_t initial_total_coins{100};
195 : :
196 : : // Make some initial assertions about the contents of the chainstate.
197 : 4 : {
198 : 4 : LOCK(::cs_main);
199 [ + - + - ]: 4 : CCoinsViewCache& ibd_coinscache = chainman.ActiveChainstate().CoinsTip();
200 [ + - ]: 4 : initial_size = ibd_coinscache.GetCacheSize();
201 : 4 : size_t total_coins{0};
202 : :
203 [ + + ]: 404 : for (CTransactionRef& txn : m_coinbase_txns) {
204 [ + - ]: 400 : COutPoint op{txn->GetHash(), 0};
205 [ + - + - : 800 : BOOST_CHECK(ibd_coinscache.HaveCoin(op));
+ - ]
206 : 400 : total_coins++;
207 : : }
208 : :
209 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(total_coins, initial_total_coins);
210 [ + - + - : 4 : BOOST_CHECK_EQUAL(initial_size, initial_total_coins);
+ - ]
211 : 0 : }
212 : :
213 : 4 : Chainstate& validation_chainstate = chainman.ActiveChainstate();
214 : :
215 : : // Snapshot should refuse to load at this height.
216 [ + - + - ]: 8 : BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(this));
217 [ + - + - ]: 8 : BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
218 : :
219 : : // Mine 10 more blocks, putting at us height 110 where a valid assumeutxo value can
220 : : // be found.
221 : 4 : constexpr int snapshot_height = 110;
222 : 4 : mineBlocks(10);
223 : 4 : initial_size += 10;
224 : 4 : initial_total_coins += 10;
225 : :
226 : : // Should not load malleated snapshots
227 [ + - + - : 16 : BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
+ - ]
228 : : this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
229 : : // A UTXO is missing but count is correct
230 : : metadata.m_coins_count -= 1;
231 : :
232 : : Txid txid;
233 : : auto_infile >> txid;
234 : : // coins size
235 : : (void)ReadCompactSize(auto_infile);
236 : : // vout index
237 : : (void)ReadCompactSize(auto_infile);
238 : : Coin coin;
239 : : auto_infile >> coin;
240 : : }));
241 : :
242 [ + - + - : 12 : BOOST_CHECK(!node::FindAssumeutxoChainstateDir(chainman.m_options.datadir));
- + ]
243 : :
244 [ - + + - : 12 : BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
+ - ]
245 : : this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
246 : : // Coins count is larger than coins in file
247 : : metadata.m_coins_count += 1;
248 : : }));
249 [ - + + - : 12 : BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
+ - ]
250 : : this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
251 : : // Coins count is smaller than coins in file
252 : : metadata.m_coins_count -= 1;
253 : : }));
254 [ - + + - : 12 : BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
+ - ]
255 : : this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
256 : : // Wrong hash
257 : : metadata.m_base_blockhash = uint256::ZERO;
258 : : }));
259 [ - + + - : 12 : BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
+ - ]
260 : : this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
261 : : // Wrong hash
262 : : metadata.m_base_blockhash = uint256::ONE;
263 : : }));
264 : :
265 [ + - + - ]: 8 : BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(this));
266 [ + - + - : 12 : BOOST_CHECK(fs::exists(*node::FindAssumeutxoChainstateDir(chainman.m_options.datadir)));
+ - + - ]
267 : :
268 : : // Ensure our active chain is the snapshot chainstate.
269 [ + - + - ]: 12 : BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
270 : :
271 : 4 : Chainstate& snapshot_chainstate = chainman.ActiveChainstate();
272 : :
273 : 4 : {
274 : 4 : LOCK(::cs_main);
275 : :
276 [ + - + - ]: 4 : fs::path found = *node::FindAssumeutxoChainstateDir(chainman.m_options.datadir);
277 : :
278 : : // Note: WriteSnapshotBaseBlockhash() is implicitly tested above.
279 [ + - - + : 8 : BOOST_CHECK_EQUAL(
+ - + - +
- ]
280 : : *node::ReadSnapshotBaseBlockhash(found),
281 : : *Assert(chainman.CurrentChainstate().m_from_snapshot_blockhash));
282 [ + - ]: 4 : }
283 : :
284 : 4 : const auto& au_data = ::Params().AssumeutxoForHeight(snapshot_height);
285 [ + - + - ]: 12 : const CBlockIndex* tip = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip());
286 : :
287 [ + - ]: 4 : BOOST_CHECK_EQUAL(tip->m_chain_tx_count, au_data->m_chain_tx_count);
288 : :
289 : : // To be checked against later when we try loading a subsequent snapshot.
290 [ - + + - ]: 8 : uint256 loaded_snapshot_blockhash{*Assert(WITH_LOCK(chainman.GetMutex(), return chainman.CurrentChainstate().m_from_snapshot_blockhash))};
291 : :
292 : : // Make some assertions about the both chainstates. These checks ensure the
293 : : // legacy chainstate hasn't changed and that the newly created chainstate
294 : : // reflects the expected content.
295 : 4 : {
296 : 4 : LOCK(::cs_main);
297 : 4 : int chains_tested{0};
298 : :
299 [ + + ]: 12 : for (const auto& chainstate : chainman.m_chainstates) {
300 [ + - + - : 8 : BOOST_TEST_MESSAGE("Checking coins in " << chainstate->ToString());
+ - + - ]
301 [ + - ]: 8 : CCoinsViewCache& coinscache = chainstate->CoinsTip();
302 : :
303 : : // Both caches will be empty initially.
304 [ + - + - : 8 : BOOST_CHECK_EQUAL((unsigned int)0, coinscache.GetCacheSize());
+ - ]
305 : :
306 : 8 : size_t total_coins{0};
307 : :
308 [ + + ]: 888 : for (CTransactionRef& txn : m_coinbase_txns) {
309 [ + - ]: 880 : COutPoint op{txn->GetHash(), 0};
310 [ + - + - : 1760 : BOOST_CHECK(coinscache.HaveCoin(op));
+ - ]
311 : 880 : total_coins++;
312 : : }
313 : :
314 [ + - + - : 8 : BOOST_CHECK_EQUAL(initial_size , coinscache.GetCacheSize());
+ - ]
315 [ + - + - ]: 8 : BOOST_CHECK_EQUAL(total_coins, initial_total_coins);
316 : 8 : chains_tested++;
317 : : }
318 : :
319 [ + - + - : 4 : BOOST_CHECK_EQUAL(chains_tested, 2);
+ - ]
320 : 0 : }
321 : :
322 : : // Mine some new blocks on top of the activated snapshot chainstate.
323 : 4 : constexpr size_t new_coins{100};
324 : 4 : mineBlocks(new_coins); // Defined in TestChain100Setup.
325 : :
326 : 4 : {
327 : 4 : LOCK(::cs_main);
328 : 4 : size_t coins_in_active{0};
329 : 4 : size_t coins_in_background{0};
330 : 4 : size_t coins_missing_from_background{0};
331 : :
332 [ + + ]: 12 : for (const auto& chainstate : chainman.m_chainstates) {
333 [ + - + - : 8 : BOOST_TEST_MESSAGE("Checking coins in " << chainstate->ToString());
+ - + - ]
334 [ + - ]: 8 : CCoinsViewCache& coinscache = chainstate->CoinsTip();
335 [ + - ]: 8 : bool is_background = chainstate.get() != &chainman.ActiveChainstate();
336 : :
337 [ + + ]: 1688 : for (CTransactionRef& txn : m_coinbase_txns) {
338 [ + - ]: 1680 : COutPoint op{txn->GetHash(), 0};
339 [ + - + + ]: 1680 : if (coinscache.HaveCoin(op)) {
340 [ + + ]: 1280 : (is_background ? coins_in_background : coins_in_active)++;
341 [ + - ]: 400 : } else if (is_background) {
342 : 400 : coins_missing_from_background++;
343 : : }
344 : : }
345 : : }
346 : :
347 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(coins_in_active, initial_total_coins + new_coins);
348 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(coins_in_background, initial_total_coins);
349 [ + - + - : 4 : BOOST_CHECK_EQUAL(coins_missing_from_background, new_coins);
+ - ]
350 : 0 : }
351 : :
352 : : // Snapshot should refuse to load after one has already loaded.
353 [ + - + - ]: 8 : BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(this));
354 : :
355 : : // Snapshot blockhash should be unchanged.
356 [ + - ]: 4 : BOOST_CHECK_EQUAL(
357 : : *chainman.ActiveChainstate().m_from_snapshot_blockhash,
358 : : loaded_snapshot_blockhash);
359 : 4 : return std::make_tuple(&validation_chainstate, &snapshot_chainstate);
360 : : }
361 : :
362 : : // Simulate a restart of the node by flushing all state to disk, clearing the
363 : : // existing ChainstateManager, and unloading the block index.
364 : : //
365 : : // @returns a reference to the "restarted" ChainstateManager
366 : 3 : ChainstateManager& SimulateNodeRestart()
367 : : {
368 [ - + ]: 3 : ChainstateManager& chainman = *Assert(m_node.chainman);
369 : :
370 [ + - ]: 3 : BOOST_TEST_MESSAGE("Simulating node restart");
371 : 3 : {
372 : 3 : LOCK(chainman.GetMutex());
373 [ + + ]: 9 : for (const auto& cs : chainman.m_chainstates) {
374 [ + + + - ]: 11 : if (cs->CanFlushToDisk()) cs->ForceFlushStateToDisk();
375 : : }
376 : 0 : }
377 : 3 : {
378 : : // Process all callbacks referring to the old manager before wiping it.
379 : 3 : m_node.validation_signals->SyncWithValidationInterfaceQueue();
380 : 3 : LOCK(::cs_main);
381 [ + - ]: 3 : chainman.ResetChainstates();
382 [ + - - + : 3 : BOOST_CHECK_EQUAL(chainman.m_chainstates.size(), 0);
+ - ]
383 [ - + - + : 3 : m_node.notifications = std::make_unique<KernelNotifications>(Assert(m_node.shutdown_request), m_node.exit_status, *Assert(m_node.warnings));
+ - ]
384 : 3 : const ChainstateManager::Options chainman_opts{
385 [ + - ]: 3 : .chainparams = ::Params(),
386 : 3 : .datadir = chainman.m_options.datadir,
387 : 3 : .notifications = *m_node.notifications,
388 : 3 : .signals = m_node.validation_signals.get(),
389 [ + - + - ]: 3 : };
390 : 3 : const BlockManager::Options blockman_opts{
391 : 3 : .chainparams = chainman_opts.chainparams,
392 : : .blocks_dir = m_args.GetBlocksDirPath(),
393 : 3 : .notifications = chainman_opts.notifications,
394 : : .block_tree_db_params = DBParams{
395 [ + - + - ]: 9 : .path = chainman.m_options.datadir / "blocks" / "index",
396 : 3 : .cache_bytes = m_kernel_cache_sizes.block_tree_db,
397 : 3 : .memory_only = m_block_tree_db_in_memory,
398 : : },
399 [ + - + - ]: 6 : };
400 : : // For robustness, ensure the old manager is destroyed before creating a
401 : : // new one.
402 [ + - ]: 3 : m_node.chainman.reset();
403 [ - + + - ]: 3 : m_node.chainman = std::make_unique<ChainstateManager>(*Assert(m_node.shutdown_signal), chainman_opts, blockman_opts);
404 [ + - ]: 6 : }
405 [ - + ]: 3 : return *Assert(m_node.chainman);
406 : : }
407 : : };
408 : :
409 : : //! Test basic snapshot activation.
410 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, SnapshotTestSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
411 : : {
412 : 1 : this->SetupSnapshot();
413 : 1 : }
414 : :
415 : : //! Test LoadBlockIndex behavior when multiple chainstates are in use.
416 : : //!
417 : : //! - First, verify that setBlockIndexCandidates is as expected when using a single,
418 : : //! fully-validating chainstate.
419 : : //!
420 : : //! - Then mark a region of the chain as missing data and introduce a second chainstate
421 : : //! that will tolerate assumed-valid blocks. Run LoadBlockIndex() and ensure that the first
422 : : //! chainstate only contains fully validated blocks and the other chainstate contains all blocks,
423 : : //! except those marked assume-valid, because those entries don't HAVE_DATA.
424 : : //!
425 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
426 : : {
427 [ - + ]: 1 : ChainstateManager& chainman = *Assert(m_node.chainman);
428 : 1 : Chainstate& cs1 = chainman.ActiveChainstate();
429 : :
430 : 1 : int num_indexes{0};
431 : : // Blocks in range [assumed_valid_start_idx, last_assumed_valid_idx) will be
432 : : // marked as assumed-valid and not having data.
433 : 1 : const int expected_assumed_valid{20};
434 : 1 : const int last_assumed_valid_idx{111};
435 : 1 : const int assumed_valid_start_idx = last_assumed_valid_idx - expected_assumed_valid;
436 : :
437 : : // Mine to height 120, past the hardcoded regtest assumeutxo snapshot at
438 : : // height 110
439 : 1 : mineBlocks(20);
440 : :
441 : 1 : CBlockIndex* validated_tip{nullptr};
442 : 1 : CBlockIndex* assumed_base{nullptr};
443 [ + - - + : 4 : CBlockIndex* assumed_tip{WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip())};
+ - ]
444 [ + - ]: 1 : BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120);
445 : :
446 : 3 : auto reload_all_block_indexes = [&]() {
447 : 2 : LOCK(chainman.GetMutex());
448 : : // For completeness, we also reset the block sequence counters to
449 : : // ensure that no state which affects the ranking of tip-candidates is
450 : : // retained (even though this isn't strictly necessary).
451 : 2 : chainman.ResetBlockSequenceCounters();
452 [ + + ]: 5 : for (const auto& cs : chainman.m_chainstates) {
453 [ + - ]: 3 : cs->ClearBlockIndexCandidates();
454 [ + - + - ]: 6 : BOOST_CHECK(cs->setBlockIndexCandidates.empty());
455 : : }
456 [ + - ]: 2 : chainman.LoadBlockIndex();
457 : 3 : };
458 : :
459 : : // Ensure that without any assumed-valid BlockIndex entries, only the current tip is
460 : : // considered as a candidate.
461 : 1 : reload_all_block_indexes();
462 [ + - ]: 1 : BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.size(), 1);
463 : :
464 : : // Reset some region of the chain's nStatus, removing the HAVE_DATA flag.
465 [ - + + + ]: 122 : for (int i = 0; i <= cs1.m_chain.Height(); ++i) {
466 : 121 : LOCK(::cs_main);
467 [ + - ]: 121 : auto index = cs1.m_chain[i];
468 : :
469 : : // Blocks with heights in range [91, 110] are marked as missing data.
470 [ + + ]: 121 : if (i < last_assumed_valid_idx && i >= assumed_valid_start_idx) {
471 : 20 : index->nStatus = BlockStatus::BLOCK_VALID_TREE;
472 : 20 : index->nTx = 0;
473 : 20 : index->m_chain_tx_count = 0;
474 : : }
475 : :
476 : 121 : ++num_indexes;
477 : :
478 : : // Note the last fully-validated block as the expected validated tip.
479 [ + + ]: 121 : if (i == (assumed_valid_start_idx - 1)) {
480 : 1 : validated_tip = index;
481 : : }
482 : : // Note the last assumed valid block as the snapshot base
483 [ + + ]: 121 : if (i == last_assumed_valid_idx - 1) {
484 : 1 : assumed_base = index;
485 : : }
486 : 121 : }
487 : :
488 : : // Note: cs2's tip is not set when ActivateExistingSnapshot is called.
489 [ + - + - : 3 : Chainstate& cs2{WITH_LOCK(::cs_main, return chainman.AddChainstate(std::make_unique<Chainstate>(nullptr, chainman.m_blockman, chainman, *assumed_base->phashBlock)))};
+ - ]
490 : :
491 : : // Set tip of the fully validated chain to be the validated tip
492 : 1 : cs1.m_chain.SetTip(*validated_tip);
493 : :
494 : : // Set tip of the assume-valid-based chain to the assume-valid block
495 : 1 : cs2.m_chain.SetTip(*assumed_base);
496 : :
497 : : // Sanity check test variables.
498 [ + - ]: 1 : BOOST_CHECK_EQUAL(num_indexes, 121); // 121 total blocks, including genesis
499 [ + - ]: 1 : BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120); // original chain has height 120
500 [ + - ]: 1 : BOOST_CHECK_EQUAL(validated_tip->nHeight, 90); // current cs1 chain has height 90
501 [ + - ]: 1 : BOOST_CHECK_EQUAL(assumed_base->nHeight, 110); // current cs2 chain has height 110
502 : :
503 : : // Regenerate cs1.setBlockIndexCandidates and cs2.setBlockIndexCandidate and
504 : : // check contents below.
505 : 1 : reload_all_block_indexes();
506 : :
507 : : // The fully validated chain should only have the current validated tip and
508 : : // the assumed valid base as candidates, blocks 90 and 110. Specifically:
509 : : //
510 : : // - It does not have blocks 0-89 because they contain less work than the
511 : : // chain tip.
512 : : //
513 : : // - It has block 90 because it has data and equal work to the chain tip,
514 : : // (since it is the chain tip).
515 : : //
516 : : // - It does not have blocks 91-109 because they do not contain data.
517 : : //
518 : : // - It has block 110 even though it does not have data, because
519 : : // LoadBlockIndex has a special case to always add the snapshot block as a
520 : : // candidate. The special case is only actually intended to apply to the
521 : : // snapshot chainstate cs2, not the background chainstate cs1, but it is
522 : : // written broadly and applies to both.
523 : : //
524 : : // - It does not have any blocks after height 110 because cs1 is a background
525 : : // chainstate, and only blocks where are ancestors of the snapshot block
526 : : // are added as candidates for the background chainstate.
527 [ + - ]: 1 : BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.size(), 2);
528 [ + - ]: 1 : BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(validated_tip), 1);
529 [ + - ]: 1 : BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(assumed_base), 1);
530 : :
531 : : // The assumed-valid tolerant chain has the assumed valid base as a
532 : : // candidate, but otherwise has none of the assumed-valid (which do not
533 : : // HAVE_DATA) blocks as candidates.
534 : : //
535 : : // Specifically:
536 : : // - All blocks below height 110 are not candidates, because cs2 chain tip
537 : : // has height 110 and they have less work than it does.
538 : : //
539 : : // - Block 110 is a candidate even though it does not have data, because it
540 : : // is the snapshot block, which is assumed valid.
541 : : //
542 : : // - Blocks 111-120 are added because they have data.
543 : :
544 : : // Check that block 90 is absent
545 [ + - ]: 1 : BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(validated_tip), 0);
546 : : // Check that block 109 is absent
547 [ + - ]: 1 : BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base->pprev), 0);
548 : : // Check that block 110 is present
549 [ + - ]: 1 : BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base), 1);
550 : : // Check that block 120 is present
551 [ + - ]: 1 : BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_tip), 1);
552 : : // Check that 11 blocks total are present.
553 [ + - ]: 1 : BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.size(), num_indexes - last_assumed_valid_idx + 1);
554 : 1 : }
555 : :
556 : : //! Ensure that snapshot chainstate can be loaded when found on disk after a
557 : : //! restart, and that new blocks can be connected to both chainstates.
558 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_init, SnapshotTestSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
559 : : {
560 [ - + ]: 1 : ChainstateManager& chainman = *Assert(m_node.chainman);
561 : 1 : Chainstate& bg_chainstate = chainman.ActiveChainstate();
562 : :
563 : 1 : this->SetupSnapshot();
564 : :
565 [ + - ]: 1 : fs::path snapshot_chainstate_dir = *node::FindAssumeutxoChainstateDir(chainman.m_options.datadir);
566 [ + - + - : 2 : BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
+ - + - ]
567 [ + - + - : 3 : BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
+ - + - ]
568 : :
569 [ + - + - : 3 : BOOST_CHECK(WITH_LOCK(::cs_main, return chainman.CurrentChainstate().m_from_snapshot_blockhash));
+ - + - ]
570 [ + - + - ]: 3 : const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
571 : : return chainman.ActiveTip()->GetBlockHash());
572 : :
573 [ + + + - : 2 : BOOST_CHECK_EQUAL(WITH_LOCK(chainman.GetMutex(), return chainman.m_chainstates.size()), 2);
+ - ]
574 : :
575 : : // "Rewind" the background chainstate so that its tip is not at the
576 : : // base block of the snapshot - this is so after simulating a node restart,
577 : : // it will initialize instead of attempting to complete validation.
578 : : //
579 : : // Note that this is not a realistic use of DisconnectTip().
580 [ + - ]: 1 : DisconnectedBlockTransactions unused_pool{MAX_DISCONNECTED_TX_POOL_BYTES};
581 [ + - ]: 1 : BlockValidationState unused_state;
582 : 1 : {
583 [ + - - + : 1 : LOCK2(::cs_main, bg_chainstate.MempoolMutex());
+ - ]
584 [ + - + - : 2 : BOOST_CHECK(bg_chainstate.DisconnectTip(unused_state, &unused_pool));
+ - + - ]
585 [ + - ]: 1 : unused_pool.clear(); // to avoid queuedTx assertion errors on teardown
586 [ + - ]: 1 : }
587 [ + - - + : 1 : BOOST_CHECK_EQUAL(bg_chainstate.m_chain.Height(), 109);
+ - ]
588 : :
589 : : // Test that simulating a shutdown (resetting ChainstateManager) and then performing
590 : : // chainstate reinitializing successfully reloads both chainstates.
591 [ + - ]: 1 : ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
592 : :
593 [ + - + - : 1 : BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
+ - ]
594 : :
595 : : // This call reinitializes the chainstates.
596 [ + - ]: 1 : this->LoadVerifyActivateChainstate();
597 : :
598 : 1 : {
599 [ + - ]: 1 : LOCK(chainman_restarted.GetMutex());
600 [ + - - + : 1 : BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates.size(), 2);
+ - ]
601 : : // Background chainstate has height of 109 not 110 here due to a quirk
602 : : // of the LoadVerifyActivate only calling ActivateBestChain on one
603 : : // chainstate. The height would be 110 after a real restart, but it's
604 : : // fine for this test which is focused on the snapshot chainstate.
605 [ + - - + : 1 : BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates[0]->m_chain.Height(), 109);
+ - ]
606 [ + - - + : 1 : BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates[1]->m_chain.Height(), 210);
+ - ]
607 : :
608 [ + - + - : 2 : BOOST_CHECK(chainman_restarted.CurrentChainstate().m_from_snapshot_blockhash);
+ - ]
609 [ + - + - : 2 : BOOST_CHECK(chainman_restarted.CurrentChainstate().m_assumeutxo == Assumeutxo::UNVALIDATED);
+ - ]
610 : :
611 [ + - + - : 1 : BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
+ - ]
612 [ + - + - : 1 : BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
+ - ]
613 [ + - - + : 1 : BOOST_CHECK_EQUAL(chainman_restarted.HistoricalChainstate()->m_chain.Height(), 109);
+ - + - ]
614 : 0 : }
615 : :
616 [ + - + - : 1 : BOOST_TEST_MESSAGE(
+ - ]
617 : : "Ensure we can mine blocks on top of the initialized snapshot chainstate");
618 [ + - ]: 1 : mineBlocks(10);
619 : 1 : {
620 [ + - ]: 1 : LOCK(chainman_restarted.GetMutex());
621 [ + - + - : 1 : BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
+ - ]
622 : :
623 : : // Background chainstate should be unaware of new blocks on the snapshot
624 : : // chainstate, but the block disconnected above is now reattached.
625 [ + - - + : 1 : BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates.size(), 2);
+ - ]
626 [ + - - + : 1 : BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates[0]->m_chain.Height(), 110);
+ - ]
627 [ + - - + : 1 : BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates[1]->m_chain.Height(), 220);
+ - ]
628 [ + - + - : 1 : BOOST_CHECK_EQUAL(chainman_restarted.HistoricalChainstate(), nullptr);
+ - ]
629 : 1 : }
630 : 2 : }
631 : :
632 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion, SnapshotTestSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
633 : : {
634 : 1 : this->SetupSnapshot();
635 : :
636 [ - + ]: 1 : ChainstateManager& chainman = *Assert(m_node.chainman);
637 : 1 : Chainstate& active_cs = chainman.ActiveChainstate();
638 [ + + ]: 2 : Chainstate& validated_cs{*Assert(WITH_LOCK(cs_main, return chainman.HistoricalChainstate()))};
639 : 1 : auto tip_cache_before_complete = active_cs.m_coinstip_cache_size_bytes;
640 : 1 : auto db_cache_before_complete = active_cs.m_coinsdb_cache_size_bytes;
641 : :
642 : 1 : SnapshotCompletionResult res;
643 : 1 : m_node.notifications->m_shutdown_on_fatal_error = false;
644 : :
645 [ + - ]: 1 : fs::path snapshot_chainstate_dir = *node::FindAssumeutxoChainstateDir(chainman.m_options.datadir);
646 [ + - + - : 2 : BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
+ - + - ]
647 [ + - + - : 3 : BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
+ - + - ]
648 : :
649 [ + - + - : 3 : BOOST_CHECK(WITH_LOCK(::cs_main, return chainman.CurrentChainstate().m_from_snapshot_blockhash));
+ - + - ]
650 [ + - + - ]: 3 : const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
651 : : return chainman.ActiveTip()->GetBlockHash());
652 : :
653 [ + - + - ]: 3 : res = WITH_LOCK(::cs_main, return chainman.MaybeValidateSnapshot(validated_cs, active_cs));
654 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(res, SnapshotCompletionResult::SUCCESS);
655 : :
656 [ + - + - : 3 : BOOST_CHECK(WITH_LOCK(::cs_main, return chainman.CurrentChainstate().m_assumeutxo == Assumeutxo::VALIDATED));
+ - + - ]
657 [ + - + - : 3 : BOOST_CHECK(WITH_LOCK(::cs_main, return chainman.CurrentChainstate().m_from_snapshot_blockhash));
+ - + - ]
658 [ + - + - : 2 : BOOST_CHECK_EQUAL(WITH_LOCK(chainman.GetMutex(), return chainman.HistoricalChainstate()), nullptr);
+ - ]
659 : :
660 : : // Cache should have been rebalanced and reallocated to the "only" remaining
661 : : // chainstate.
662 [ + - + - : 2 : BOOST_CHECK(active_cs.m_coinstip_cache_size_bytes > tip_cache_before_complete);
+ - ]
663 [ + - + - : 2 : BOOST_CHECK(active_cs.m_coinsdb_cache_size_bytes > db_cache_before_complete);
+ - ]
664 : :
665 : : // Trying completion again should return false.
666 [ + - + - ]: 3 : res = WITH_LOCK(::cs_main, return chainman.MaybeValidateSnapshot(validated_cs, active_cs));
667 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(res, SnapshotCompletionResult::SKIPPED);
668 : :
669 : : // The invalid snapshot path should not have been used.
670 [ + - + - ]: 2 : fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
671 [ + - + - : 2 : BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
+ - + - ]
672 : : // chainstate_snapshot should still exist.
673 [ + - + - : 2 : BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
+ - + - ]
674 : :
675 : : // Test that simulating a shutdown (resetting ChainstateManager) and then performing
676 : : // chainstate reinitializing successfully cleans up the background-validation
677 : : // chainstate data, and we end up with a single chainstate that is at tip.
678 [ + - ]: 1 : ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
679 : :
680 [ + - + - : 1 : BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
+ - ]
681 : :
682 : : // This call reinitializes the chainstates, and should clean up the now unnecessary
683 : : // background-validation leveldb contents.
684 [ + - ]: 1 : this->LoadVerifyActivateChainstate();
685 : :
686 [ + - + - : 2 : BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
+ - + - ]
687 : : // chainstate_snapshot should now *not* exist.
688 [ + - + - : 2 : BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
+ - + - ]
689 : :
690 [ + - ]: 1 : const Chainstate& active_cs2 = chainman_restarted.ActiveChainstate();
691 : :
692 : 1 : {
693 [ + - ]: 1 : LOCK(chainman_restarted.GetMutex());
694 [ + - - + : 1 : BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates.size(), 1);
+ - ]
695 [ + - + - : 2 : BOOST_CHECK(!chainman_restarted.CurrentChainstate().m_from_snapshot_blockhash);
+ - ]
696 [ + - + - : 2 : BOOST_CHECK(active_cs2.m_coinstip_cache_size_bytes > tip_cache_before_complete);
+ - ]
697 [ + - + - : 2 : BOOST_CHECK(active_cs2.m_coinsdb_cache_size_bytes > db_cache_before_complete);
+ - ]
698 : :
699 [ + - + - : 1 : BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
+ - ]
700 [ + - + - : 1 : BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
+ - + - ]
701 : 0 : }
702 : :
703 [ + - + - : 1 : BOOST_TEST_MESSAGE(
+ - ]
704 : : "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
705 [ + - ]: 1 : mineBlocks(10);
706 : 1 : {
707 [ + - ]: 1 : LOCK(chainman_restarted.GetMutex());
708 [ + - + - : 1 : BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
+ - + - ]
709 : 1 : }
710 : 2 : }
711 : :
712 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion_hash_mismatch, SnapshotTestSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
713 : : {
714 : 1 : auto chainstates = this->SetupSnapshot();
715 [ - + ]: 1 : Chainstate& validation_chainstate = *std::get<0>(chainstates);
716 [ - + ]: 1 : Chainstate& unvalidated_cs = *std::get<1>(chainstates);
717 [ - + ]: 1 : ChainstateManager& chainman = *Assert(m_node.chainman);
718 : 1 : SnapshotCompletionResult res;
719 : 1 : m_node.notifications->m_shutdown_on_fatal_error = false;
720 : :
721 : : // Test tampering with the IBD UTXO set with an extra coin to ensure it causes
722 : : // snapshot completion to fail.
723 [ + - + - ]: 3 : CCoinsViewCache& ibd_coins = WITH_LOCK(::cs_main,
724 : : return validation_chainstate.CoinsTip());
725 : 1 : Coin badcoin;
726 : 1 : badcoin.out.nValue = m_rng.rand32();
727 : 1 : badcoin.nHeight = 1;
728 : 1 : badcoin.out.scriptPubKey.assign(m_rng.randbits(6), 0);
729 [ + - ]: 1 : Txid txid = Txid::FromUint256(m_rng.rand256());
730 [ + - ]: 1 : ibd_coins.AddCoin(COutPoint(txid, 0), std::move(badcoin), false);
731 : :
732 [ + - + - ]: 2 : fs::path snapshot_chainstate_dir = gArgs.GetDataDirNet() / "chainstate_snapshot";
733 [ + - + - : 2 : BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
+ - + - ]
734 : :
735 : 1 : {
736 [ + - + - ]: 2 : ASSERT_DEBUG_LOG("failed to validate the -assumeutxo snapshot state");
737 [ + - + - ]: 3 : res = WITH_LOCK(::cs_main, return chainman.MaybeValidateSnapshot(validation_chainstate, unvalidated_cs));
738 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(res, SnapshotCompletionResult::HASH_MISMATCH);
739 : 1 : }
740 : :
741 : 1 : {
742 [ + - ]: 1 : LOCK(chainman.GetMutex());
743 [ + - - + : 1 : BOOST_CHECK_EQUAL(chainman.m_chainstates.size(), 2);
+ - ]
744 [ + - + - : 2 : BOOST_CHECK(chainman.m_chainstates[0]->m_assumeutxo == Assumeutxo::VALIDATED);
+ - ]
745 [ + - + - : 2 : BOOST_CHECK(!chainman.m_chainstates[0]->SnapshotBase());
+ - + - ]
746 [ + - + - : 2 : BOOST_CHECK(chainman.m_chainstates[1]->m_assumeutxo == Assumeutxo::INVALID);
+ - ]
747 [ + - + - : 2 : BOOST_CHECK(chainman.m_chainstates[1]->SnapshotBase());
+ - + - ]
748 : 0 : }
749 : :
750 [ + - + - ]: 2 : fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
751 [ + - + - : 2 : BOOST_CHECK(fs::exists(snapshot_invalid_dir));
+ - + - ]
752 : :
753 : : // Test that simulating a shutdown (resetting ChainstateManager) and then performing
754 : : // chainstate reinitializing successfully loads only the fully-validated
755 : : // chainstate data, and we end up with a single chainstate that is at tip.
756 [ + - ]: 1 : ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
757 : :
758 [ + - + - : 1 : BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
+ - ]
759 : :
760 : : // This call reinitializes the chainstates, and should clean up the now unnecessary
761 : : // background-validation leveldb contents.
762 [ + - ]: 1 : this->LoadVerifyActivateChainstate();
763 : :
764 [ + - + - : 2 : BOOST_CHECK(fs::exists(snapshot_invalid_dir));
+ - + - ]
765 [ + - + - : 2 : BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
+ - + - ]
766 : :
767 : 1 : {
768 [ + - ]: 1 : LOCK(::cs_main);
769 [ + - - + : 1 : BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates.size(), 1);
+ - ]
770 [ + - + - : 2 : BOOST_CHECK(!chainman_restarted.CurrentChainstate().m_from_snapshot_blockhash);
+ - ]
771 [ + - + - : 1 : BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
+ - + - ]
772 : 0 : }
773 : :
774 [ + - + - : 1 : BOOST_TEST_MESSAGE(
+ - ]
775 : : "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
776 [ + - ]: 1 : mineBlocks(10);
777 : 1 : {
778 [ + - ]: 1 : LOCK(::cs_main);
779 [ + - + - : 1 : BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
+ - + - ]
780 : 1 : }
781 : 2 : }
782 : :
783 : : /** Helper function to parse args into args_man and return the result of applying them to opts */
784 : : template <typename Options>
785 : 15 : util::Result<Options> SetOptsFromArgs(ArgsManager& args_man, Options opts,
786 : : const std::vector<const char*>& args)
787 : : {
788 [ + - - + ]: 30 : const auto argv{Cat({"ignore"}, args)};
789 [ - + ]: 15 : std::string error{};
790 [ - + + - : 15 : if (!args_man.ParseParameters(argv.size(), argv.data(), error)) {
- + ]
791 [ # # ]: 0 : return util::Error{Untranslated("ParseParameters failed with error: " + error)};
792 : : }
793 [ + - ]: 15 : const auto result{node::ApplyArgsManOptions(args_man, opts)};
794 [ + + ]: 23 : if (!result) return util::Error{util::ErrorString(result)};
795 : 22 : return opts;
796 [ - - + - ]: 34 : }
797 : :
798 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(chainstatemanager_args, BasicTestingSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
799 : : {
800 : : //! Try to apply the provided args to a ChainstateManager::Options
801 : 16 : auto get_opts = [&](const std::vector<const char*>& args) {
802 [ + + + - ]: 15 : static kernel::Notifications notifications{};
803 : 15 : static const ChainstateManager::Options options{
804 [ + - ]: 1 : .chainparams = ::Params(),
805 : : .datadir = {},
806 [ + + + - ]: 16 : .notifications = notifications};
807 [ + - ]: 30 : return SetOptsFromArgs(*this->m_node.args, options, args);
808 : 1 : };
809 : : //! Like get_opts, but requires the provided args to be valid and unwraps the result
810 : 12 : auto get_valid_opts = [&](const std::vector<const char*>& args) {
811 : 11 : const auto result{get_opts(args)};
812 [ + - + - : 22 : BOOST_REQUIRE_MESSAGE(result, util::ErrorString(result).original);
+ - ]
813 [ + - ]: 11 : return *result;
814 : 12 : };
815 : :
816 : : // test -assumevalid
817 [ + - + - ]: 3 : BOOST_CHECK(!get_valid_opts({}).assumed_valid_block);
818 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(get_valid_opts({"-assumevalid="}).assumed_valid_block, uint256::ZERO);
819 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(get_valid_opts({"-assumevalid=0"}).assumed_valid_block, uint256::ZERO);
820 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(get_valid_opts({"-noassumevalid"}).assumed_valid_block, uint256::ZERO);
821 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(get_valid_opts({"-assumevalid=0x12"}).assumed_valid_block, uint256{0x12});
822 : :
823 : 1 : std::string assume_valid{"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"};
824 [ + - - + : 2 : BOOST_CHECK_EQUAL(get_valid_opts({("-assumevalid=" + assume_valid).c_str()}).assumed_valid_block, uint256::FromHex(assume_valid));
+ - + - +
- + - +
- ]
825 : :
826 [ + - + - : 2 : BOOST_CHECK(!get_opts({"-assumevalid=xyz"})); // invalid hex characters
+ - + - +
- ]
827 [ + - + - : 2 : BOOST_CHECK(!get_opts({"-assumevalid=01234567890123456789012345678901234567890123456789012345678901234"})); // > 64 hex chars
+ - + - +
- ]
828 : :
829 : : // test -minimumchainwork
830 [ + - + - : 3 : BOOST_CHECK(!get_valid_opts({}).minimum_chain_work);
+ - + - ]
831 [ + - + - : 2 : BOOST_CHECK_EQUAL(get_valid_opts({"-minimumchainwork=0"}).minimum_chain_work, arith_uint256());
+ - + - +
- ]
832 [ + - + - : 2 : BOOST_CHECK_EQUAL(get_valid_opts({"-nominimumchainwork"}).minimum_chain_work, arith_uint256());
+ - + - +
- ]
833 [ + - + - : 2 : BOOST_CHECK_EQUAL(get_valid_opts({"-minimumchainwork=0x1234"}).minimum_chain_work, arith_uint256{0x1234});
+ - + - +
- ]
834 : :
835 [ + - ]: 1 : std::string minimum_chainwork{"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"};
836 [ + - - + : 3 : BOOST_CHECK_EQUAL(get_valid_opts({("-minimumchainwork=" + minimum_chainwork).c_str()}).minimum_chain_work, UintToArith256(uint256::FromHex(minimum_chainwork).value()));
+ - + - +
- + - + -
+ - ]
837 : :
838 [ + - + - : 2 : BOOST_CHECK(!get_opts({"-minimumchainwork=xyz"})); // invalid hex characters
+ - + - +
- ]
839 [ + - + - : 2 : BOOST_CHECK(!get_opts({"-minimumchainwork=01234567890123456789012345678901234567890123456789012345678901234"})); // > 64 hex chars
+ - + - ]
840 : 1 : }
841 : :
842 : : BOOST_AUTO_TEST_SUITE_END()
|