Bitcoin Core 30.99.0
P2P Digital Currency
validation_chainstatemanager_tests.cpp
Go to the documentation of this file.
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>
10#include <node/utxo_snapshot.h>
11#include <random.h>
12#include <rpc/blockchain.h>
13#include <sync.h>
15#include <test/util/logging.h>
16#include <test/util/random.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
34
35BOOST_FIXTURE_TEST_SUITE(validation_chainstatemanager_tests, TestingSetup)
36
37
41{
43
45
46 // Create a legacy (IBD) chainstate.
47 //
48 Chainstate& c1 = manager.ActiveChainstate();
49
51 {
52 LOCK(manager.GetMutex());
53 BOOST_CHECK_EQUAL(manager.m_chainstates.size(), 1);
54 BOOST_CHECK_EQUAL(manager.m_chainstates[0].get(), &c1);
55 }
56
57 auto& active_chain = WITH_LOCK(manager.GetMutex(), return manager.ActiveChain());
58 BOOST_CHECK_EQUAL(&active_chain, &c1.m_chain);
59
60 // Get to a valid assumeutxo tip (per chainparams);
61 mineBlocks(10);
62 BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 110);
63 auto active_tip = WITH_LOCK(manager.GetMutex(), return manager.ActiveTip());
64 auto exp_tip = c1.m_chain.Tip();
65 BOOST_CHECK_EQUAL(active_tip, exp_tip);
66
68
69 // Create a snapshot-based chainstate.
70 //
71 const uint256 snapshot_blockhash = active_tip->GetBlockHash();
72 Chainstate& c2{WITH_LOCK(::cs_main, return manager.AddChainstate(std::make_unique<Chainstate>(nullptr, manager.m_blockman, manager, snapshot_blockhash)))};
73 c2.InitCoinsDB(
74 /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
75 {
77 c2.InitCoinsCache(1 << 23);
78 c2.CoinsTip().SetBestBlock(active_tip->GetBlockHash());
79 c2.setBlockIndexCandidates.insert(manager.m_blockman.LookupBlockIndex(active_tip->GetBlockHash()));
80 c2.LoadChainTip();
81 }
83 BOOST_CHECK(c2.ActivateBestChain(_, nullptr));
84
87 BOOST_CHECK_EQUAL(&c2, &manager.ActiveChainstate());
88 BOOST_CHECK(&c1 != &manager.ActiveChainstate());
89 {
90 LOCK(manager.GetMutex());
91 BOOST_CHECK_EQUAL(manager.m_chainstates.size(), 2);
92 BOOST_CHECK_EQUAL(manager.m_chainstates[0].get(), &c1);
93 BOOST_CHECK_EQUAL(manager.m_chainstates[1].get(), &c2);
94 }
95
96 auto& active_chain2 = WITH_LOCK(manager.GetMutex(), return manager.ActiveChain());
97 BOOST_CHECK_EQUAL(&active_chain2, &c2.m_chain);
98
99 BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 110);
100 mineBlocks(1);
101 BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 111);
102 BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return c1.m_chain.Height()), 110);
103
104 auto active_tip2 = WITH_LOCK(manager.GetMutex(), return manager.ActiveTip());
105 BOOST_CHECK_EQUAL(active_tip, active_tip2->pprev);
106 BOOST_CHECK_EQUAL(active_tip, c1.m_chain.Tip());
107 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 m_node.validation_signals->SyncWithValidationInterfaceQueue();
111}
112
114BOOST_FIXTURE_TEST_CASE(chainstatemanager_rebalance_caches, TestChain100Setup)
115{
117
118 size_t max_cache = 10000;
119 manager.m_total_coinsdb_cache = max_cache;
120 manager.m_total_coinstip_cache = max_cache;
121
122 std::vector<Chainstate*> chainstates;
123
124 // Create a legacy (IBD) chainstate.
125 //
126 Chainstate& c1 = manager.ActiveChainstate();
127 chainstates.push_back(&c1);
128 {
130 c1.InitCoinsCache(1 << 23);
131 manager.MaybeRebalanceCaches();
132 }
133
136
137 // Create a snapshot-based chainstate.
138 //
139 CBlockIndex* snapshot_base{WITH_LOCK(manager.GetMutex(), return manager.ActiveChain()[manager.ActiveChain().Height() / 2])};
140 Chainstate& c2{WITH_LOCK(::cs_main, return manager.AddChainstate(std::make_unique<Chainstate>(nullptr, manager.m_blockman, manager, *snapshot_base->phashBlock)))};
141 chainstates.push_back(&c2);
142 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 // MaybeRebalanceCaches() to prioritize the snapshot chainstate, giving it
147 // more cache space than the snapshot chainstate. Calling ResetIbd() is
148 // necessary because m_cached_is_ibd is already latched to false 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 static_cast<TestChainstateManager&>(manager).ResetIbd();
153
154 {
156 c2.InitCoinsCache(1 << 23);
157 manager.MaybeRebalanceCaches();
158 }
159
160 BOOST_CHECK_CLOSE(double(c1.m_coinstip_cache_size_bytes), max_cache * 0.05, 1);
161 BOOST_CHECK_CLOSE(double(c1.m_coinsdb_cache_size_bytes), max_cache * 0.05, 1);
162 BOOST_CHECK_CLOSE(double(c2.m_coinstip_cache_size_bytes), max_cache * 0.95, 1);
163 BOOST_CHECK_CLOSE(double(c2.m_coinsdb_cache_size_bytes), max_cache * 0.95, 1);
164}
165
166BOOST_FIXTURE_TEST_CASE(chainstatemanager_ibd_exit_after_loading_blocks, ChainTestingSetup)
167{
168 CBlockIndex tip;
170 auto apply{[&](bool cached_is_ibd, bool loading_blocks, bool tip_exists, bool enough_work, bool tip_recent) {
172 chainman.ResetChainstates();
173 chainman.InitializeChainstate(m_node.mempool.get());
174
175 const auto recent_time{Now<NodeSeconds>() - chainman.m_options.max_tip_age};
176
177 chainman.m_cached_is_ibd.store(cached_is_ibd, std::memory_order_relaxed);
178 chainman.m_blockman.m_importing = loading_blocks;
179 if (tip_exists) {
180 tip.nChainWork = chainman.MinimumChainWork() - (enough_work ? 0 : 1);
181 tip.nTime = (recent_time - (tip_recent ? 0h : 100h)).time_since_epoch().count();
182 chainman.ActiveChain().SetTip(tip);
183 } else {
184 assert(!chainman.ActiveChain().Tip());
185 }
186 chainman.UpdateIBDStatus();
187 }};
188
189 for (const bool cached_is_ibd : {false, true}) {
190 for (const bool loading_blocks : {false, true}) {
191 for (const bool tip_exists : {false, true}) {
192 for (const bool enough_work : {false, true}) {
193 for (const bool tip_recent : {false, true}) {
194 apply(cached_is_ibd, loading_blocks, tip_exists, enough_work, tip_recent);
195 const bool expected_ibd = cached_is_ibd && (loading_blocks || !tip_exists || !enough_work || !tip_recent);
196 BOOST_CHECK_EQUAL(chainman.IsInitialBlockDownload(), expected_ibd);
197 }
198 }
199 }
200 }
201 }
202}
203
205 // Run with coinsdb on the filesystem to support, e.g., moving invalidated
206 // chainstate dirs to "*_invalid".
207 //
208 // Note that this means the tests run considerably slower than in-memory DB
209 // tests, but we can't otherwise test this functionality since it relies on
210 // destructive filesystem operations.
212 {},
213 {
214 .coins_db_in_memory = false,
215 .block_tree_db_in_memory = false,
216 },
217 }
218 {
219 }
220
221 std::tuple<Chainstate*, Chainstate*> SetupSnapshot()
222 {
224
225 {
229 }
230
231 size_t initial_size;
232 size_t initial_total_coins{100};
233
234 // Make some initial assertions about the contents of the chainstate.
235 {
237 CCoinsViewCache& ibd_coinscache = chainman.ActiveChainstate().CoinsTip();
238 initial_size = ibd_coinscache.GetCacheSize();
239 size_t total_coins{0};
240
241 for (CTransactionRef& txn : m_coinbase_txns) {
242 COutPoint op{txn->GetHash(), 0};
243 BOOST_CHECK(ibd_coinscache.HaveCoin(op));
244 total_coins++;
245 }
246
247 BOOST_CHECK_EQUAL(total_coins, initial_total_coins);
248 BOOST_CHECK_EQUAL(initial_size, initial_total_coins);
249 }
250
251 Chainstate& validation_chainstate = chainman.ActiveChainstate();
252
253 // Snapshot should refuse to load at this height.
254 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(this));
256
257 // Mine 10 more blocks, putting at us height 110 where a valid assumeutxo value can
258 // be found.
259 constexpr int snapshot_height = 110;
260 mineBlocks(10);
261 initial_size += 10;
262 initial_total_coins += 10;
263
264 // Should not load malleated snapshots
265 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
266 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
267 // A UTXO is missing but count is correct
268 metadata.m_coins_count -= 1;
269
270 Txid txid;
271 auto_infile >> txid;
272 // coins size
273 (void)ReadCompactSize(auto_infile);
274 // vout index
275 (void)ReadCompactSize(auto_infile);
276 Coin coin;
277 auto_infile >> coin;
278 }));
279
281
282 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
283 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
284 // Coins count is larger than coins in file
285 metadata.m_coins_count += 1;
286 }));
287 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
288 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
289 // Coins count is smaller than coins in file
290 metadata.m_coins_count -= 1;
291 }));
292 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
293 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
294 // Wrong hash
296 }));
297 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
298 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
299 // Wrong hash
301 }));
302
303 BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(this));
305
306 // Ensure our active chain is the snapshot chainstate.
308
309 Chainstate& snapshot_chainstate = chainman.ActiveChainstate();
310
311 {
313
314 fs::path found = *node::FindAssumeutxoChainstateDir(chainman.m_options.datadir);
315
316 // Note: WriteSnapshotBaseBlockhash() is implicitly tested above.
320 }
321
322 const auto& au_data = ::Params().AssumeutxoForHeight(snapshot_height);
323 const CBlockIndex* tip = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip());
324
325 BOOST_CHECK_EQUAL(tip->m_chain_tx_count, au_data->m_chain_tx_count);
326
327 // To be checked against later when we try loading a subsequent snapshot.
328 uint256 loaded_snapshot_blockhash{*Assert(WITH_LOCK(chainman.GetMutex(), return chainman.CurrentChainstate().m_from_snapshot_blockhash))};
329
330 // Make some assertions about the both chainstates. These checks ensure the
331 // legacy chainstate hasn't changed and that the newly created chainstate
332 // reflects the expected content.
333 {
335 int chains_tested{0};
336
337 for (const auto& chainstate : chainman.m_chainstates) {
338 BOOST_TEST_MESSAGE("Checking coins in " << chainstate->ToString());
339 CCoinsViewCache& coinscache = chainstate->CoinsTip();
340
341 // Both caches will be empty initially.
342 BOOST_CHECK_EQUAL((unsigned int)0, coinscache.GetCacheSize());
343
344 size_t total_coins{0};
345
346 for (CTransactionRef& txn : m_coinbase_txns) {
347 COutPoint op{txn->GetHash(), 0};
348 BOOST_CHECK(coinscache.HaveCoin(op));
349 total_coins++;
350 }
351
352 BOOST_CHECK_EQUAL(initial_size , coinscache.GetCacheSize());
353 BOOST_CHECK_EQUAL(total_coins, initial_total_coins);
354 chains_tested++;
355 }
356
357 BOOST_CHECK_EQUAL(chains_tested, 2);
358 }
359
360 // Mine some new blocks on top of the activated snapshot chainstate.
361 constexpr size_t new_coins{100};
362 mineBlocks(new_coins); // Defined in TestChain100Setup.
363
364 {
366 size_t coins_in_active{0};
367 size_t coins_in_background{0};
368 size_t coins_missing_from_background{0};
369
370 for (const auto& chainstate : chainman.m_chainstates) {
371 BOOST_TEST_MESSAGE("Checking coins in " << chainstate->ToString());
372 CCoinsViewCache& coinscache = chainstate->CoinsTip();
373 bool is_background = chainstate.get() != &chainman.ActiveChainstate();
374
375 for (CTransactionRef& txn : m_coinbase_txns) {
376 COutPoint op{txn->GetHash(), 0};
377 if (coinscache.HaveCoin(op)) {
378 (is_background ? coins_in_background : coins_in_active)++;
379 } else if (is_background) {
380 coins_missing_from_background++;
381 }
382 }
383 }
384
385 BOOST_CHECK_EQUAL(coins_in_active, initial_total_coins + new_coins);
386 BOOST_CHECK_EQUAL(coins_in_background, initial_total_coins);
387 BOOST_CHECK_EQUAL(coins_missing_from_background, new_coins);
388 }
389
390 // Snapshot should refuse to load after one has already loaded.
391 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(this));
392
393 // Snapshot blockhash should be unchanged.
396 loaded_snapshot_blockhash);
397 return std::make_tuple(&validation_chainstate, &snapshot_chainstate);
398 }
399
400 // Simulate a restart of the node by flushing all state to disk, clearing the
401 // existing ChainstateManager, and unloading the block index.
402 //
403 // @returns a reference to the "restarted" ChainstateManager
405 {
407
408 BOOST_TEST_MESSAGE("Simulating node restart");
409 {
410 LOCK(chainman.GetMutex());
411 for (const auto& cs : chainman.m_chainstates) {
412 if (cs->CanFlushToDisk()) cs->ForceFlushStateToDisk();
413 }
414 }
415 {
416 // Process all callbacks referring to the old manager before wiping it.
417 m_node.validation_signals->SyncWithValidationInterfaceQueue();
419 chainman.ResetChainstates();
420 BOOST_CHECK_EQUAL(chainman.m_chainstates.size(), 0);
421 m_node.notifications = std::make_unique<KernelNotifications>(Assert(m_node.shutdown_request), m_node.exit_status, *Assert(m_node.warnings));
422 const ChainstateManager::Options chainman_opts{
424 .datadir = chainman.m_options.datadir,
425 .notifications = *m_node.notifications,
426 .signals = m_node.validation_signals.get(),
427 };
428 const BlockManager::Options blockman_opts{
429 .chainparams = chainman_opts.chainparams,
430 .blocks_dir = m_args.GetBlocksDirPath(),
431 .notifications = chainman_opts.notifications,
432 .block_tree_db_params = DBParams{
433 .path = chainman.m_options.datadir / "blocks" / "index",
434 .cache_bytes = m_kernel_cache_sizes.block_tree_db,
435 .memory_only = m_block_tree_db_in_memory,
436 },
437 };
438 // For robustness, ensure the old manager is destroyed before creating a
439 // new one.
440 m_node.chainman.reset();
441 m_node.chainman = std::make_unique<ChainstateManager>(*Assert(m_node.shutdown_signal), chainman_opts, blockman_opts);
442 }
443 return *Assert(m_node.chainman);
444 }
445};
446
448BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, SnapshotTestSetup)
449{
450 this->SetupSnapshot();
451}
452
463BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
464{
466 Chainstate& cs1 = chainman.ActiveChainstate();
467
468 int num_indexes{0};
469 // Blocks in range [assumed_valid_start_idx, last_assumed_valid_idx) will be
470 // marked as assumed-valid and not having data.
471 const int expected_assumed_valid{20};
472 const int last_assumed_valid_idx{111};
473 const int assumed_valid_start_idx = last_assumed_valid_idx - expected_assumed_valid;
474
475 // Mine to height 120, past the hardcoded regtest assumeutxo snapshot at
476 // height 110
477 mineBlocks(20);
478
479 CBlockIndex* validated_tip{nullptr};
480 CBlockIndex* assumed_base{nullptr};
481 CBlockIndex* assumed_tip{WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip())};
482 BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120);
483
484 auto reload_all_block_indexes = [&]() {
485 LOCK(chainman.GetMutex());
486 // For completeness, we also reset the block sequence counters to
487 // ensure that no state which affects the ranking of tip-candidates is
488 // retained (even though this isn't strictly necessary).
490 for (const auto& cs : chainman.m_chainstates) {
491 cs->ClearBlockIndexCandidates();
492 BOOST_CHECK(cs->setBlockIndexCandidates.empty());
493 }
494 chainman.LoadBlockIndex();
495 };
496
497 // Ensure that without any assumed-valid BlockIndex entries, only the current tip is
498 // considered as a candidate.
499 reload_all_block_indexes();
501
502 // Reset some region of the chain's nStatus, removing the HAVE_DATA flag.
503 for (int i = 0; i <= cs1.m_chain.Height(); ++i) {
505 auto index = cs1.m_chain[i];
506
507 // Blocks with heights in range [91, 110] are marked as missing data.
508 if (i < last_assumed_valid_idx && i >= assumed_valid_start_idx) {
509 index->nStatus = BlockStatus::BLOCK_VALID_TREE;
510 index->nTx = 0;
511 index->m_chain_tx_count = 0;
512 }
513
514 ++num_indexes;
515
516 // Note the last fully-validated block as the expected validated tip.
517 if (i == (assumed_valid_start_idx - 1)) {
518 validated_tip = index;
519 }
520 // Note the last assumed valid block as the snapshot base
521 if (i == last_assumed_valid_idx - 1) {
522 assumed_base = index;
523 }
524 }
525
526 // Note: cs2's tip is not set when ActivateExistingSnapshot is called.
527 Chainstate& cs2{WITH_LOCK(::cs_main, return chainman.AddChainstate(std::make_unique<Chainstate>(nullptr, chainman.m_blockman, chainman, *assumed_base->phashBlock)))};
528
529 // Set tip of the fully validated chain to be the validated tip
530 cs1.m_chain.SetTip(*validated_tip);
531
532 // Set tip of the assume-valid-based chain to the assume-valid block
533 cs2.m_chain.SetTip(*assumed_base);
534
535 // Sanity check test variables.
536 BOOST_CHECK_EQUAL(num_indexes, 121); // 121 total blocks, including genesis
537 BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120); // original chain has height 120
538 BOOST_CHECK_EQUAL(validated_tip->nHeight, 90); // current cs1 chain has height 90
539 BOOST_CHECK_EQUAL(assumed_base->nHeight, 110); // current cs2 chain has height 110
540
541 // Regenerate cs1.setBlockIndexCandidates and cs2.setBlockIndexCandidate and
542 // check contents below.
543 reload_all_block_indexes();
544
545 // The fully validated chain should only have the current validated tip and
546 // the assumed valid base as candidates, blocks 90 and 110. Specifically:
547 //
548 // - It does not have blocks 0-89 because they contain less work than the
549 // chain tip.
550 //
551 // - It has block 90 because it has data and equal work to the chain tip,
552 // (since it is the chain tip).
553 //
554 // - It does not have blocks 91-109 because they do not contain data.
555 //
556 // - It has block 110 even though it does not have data, because
557 // LoadBlockIndex has a special case to always add the snapshot block as a
558 // candidate. The special case is only actually intended to apply to the
559 // snapshot chainstate cs2, not the background chainstate cs1, but it is
560 // written broadly and applies to both.
561 //
562 // - It does not have any blocks after height 110 because cs1 is a background
563 // chainstate, and only blocks where are ancestors of the snapshot block
564 // are added as candidates for the background chainstate.
566 BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(validated_tip), 1);
567 BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(assumed_base), 1);
568
569 // The assumed-valid tolerant chain has the assumed valid base as a
570 // candidate, but otherwise has none of the assumed-valid (which do not
571 // HAVE_DATA) blocks as candidates.
572 //
573 // Specifically:
574 // - All blocks below height 110 are not candidates, because cs2 chain tip
575 // has height 110 and they have less work than it does.
576 //
577 // - Block 110 is a candidate even though it does not have data, because it
578 // is the snapshot block, which is assumed valid.
579 //
580 // - Blocks 111-120 are added because they have data.
581
582 // Check that block 90 is absent
583 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(validated_tip), 0);
584 // Check that block 109 is absent
585 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base->pprev), 0);
586 // Check that block 110 is present
587 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base), 1);
588 // Check that block 120 is present
589 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_tip), 1);
590 // Check that 11 blocks total are present.
591 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.size(), num_indexes - last_assumed_valid_idx + 1);
592}
593
594BOOST_FIXTURE_TEST_CASE(loadblockindex_invalid_descendants, TestChain100Setup)
595{
596 LOCK(Assert(m_node.chainman)->GetMutex());
597 // consider the chain of blocks grand_parent <- parent <- child
598 // intentionally mark:
599 // - grand_parent: BLOCK_FAILED_VALID
600 // - parent: BLOCK_FAILED_CHILD
601 // - child: not invalid
602 // Test that when the block index is loaded, all blocks are marked as BLOCK_FAILED_VALID
603 auto* child{m_node.chainman->ActiveChain().Tip()};
604 auto* parent{child->pprev};
605 auto* grand_parent{parent->pprev};
606 grand_parent->nStatus = (grand_parent->nStatus | BLOCK_FAILED_VALID);
607 parent->nStatus = (parent->nStatus & ~BLOCK_FAILED_VALID) | BLOCK_FAILED_CHILD;
608 child->nStatus = (child->nStatus & ~BLOCK_FAILED_VALID);
609
610 // Reload block index to recompute block status validity flags.
611 m_node.chainman->LoadBlockIndex();
612
613 // check grand_parent, parent, child is marked as BLOCK_FAILED_VALID after reloading the block index
614 BOOST_CHECK(grand_parent->nStatus & BLOCK_FAILED_VALID);
615 BOOST_CHECK(parent->nStatus & BLOCK_FAILED_VALID);
616 BOOST_CHECK(child->nStatus & BLOCK_FAILED_VALID);
617}
618
621BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_init, SnapshotTestSetup)
622{
624 Chainstate& bg_chainstate = chainman.ActiveChainstate();
625
626 this->SetupSnapshot();
627
628 fs::path snapshot_chainstate_dir = *node::FindAssumeutxoChainstateDir(chainman.m_options.datadir);
629 BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
630 BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
631
633 const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
634 return chainman.ActiveTip()->GetBlockHash());
635
636 BOOST_CHECK_EQUAL(WITH_LOCK(chainman.GetMutex(), return chainman.m_chainstates.size()), 2);
637
638 // "Rewind" the background chainstate so that its tip is not at the
639 // base block of the snapshot - this is so after simulating a node restart,
640 // it will initialize instead of attempting to complete validation.
641 //
642 // Note that this is not a realistic use of DisconnectTip().
644 BlockValidationState unused_state;
645 {
646 LOCK2(::cs_main, bg_chainstate.MempoolMutex());
647 BOOST_CHECK(bg_chainstate.DisconnectTip(unused_state, &unused_pool));
648 unused_pool.clear(); // to avoid queuedTx assertion errors on teardown
649 }
650 BOOST_CHECK_EQUAL(bg_chainstate.m_chain.Height(), 109);
651
652 // Test that simulating a shutdown (resetting ChainstateManager) and then performing
653 // chainstate reinitializing successfully reloads both chainstates.
654 ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
655
656 BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
657
658 // This call reinitializes the chainstates.
659 this->LoadVerifyActivateChainstate();
660
661 {
662 LOCK(chainman_restarted.GetMutex());
663 BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates.size(), 2);
664 // Background chainstate has height of 109 not 110 here due to a quirk
665 // of the LoadVerifyActivate only calling ActivateBestChain on one
666 // chainstate. The height would be 110 after a real restart, but it's
667 // fine for this test which is focused on the snapshot chainstate.
668 BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates[0]->m_chain.Height(), 109);
669 BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates[1]->m_chain.Height(), 210);
670
672 BOOST_CHECK(chainman_restarted.CurrentChainstate().m_assumeutxo == Assumeutxo::UNVALIDATED);
673
674 BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
675 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
676 BOOST_CHECK_EQUAL(chainman_restarted.HistoricalChainstate()->m_chain.Height(), 109);
677 }
678
679 BOOST_TEST_MESSAGE(
680 "Ensure we can mine blocks on top of the initialized snapshot chainstate");
681 mineBlocks(10);
682 {
683 LOCK(chainman_restarted.GetMutex());
684 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
685
686 // Background chainstate should be unaware of new blocks on the snapshot
687 // chainstate, but the block disconnected above is now reattached.
688 BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates.size(), 2);
689 BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates[0]->m_chain.Height(), 110);
690 BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates[1]->m_chain.Height(), 220);
691 BOOST_CHECK_EQUAL(chainman_restarted.HistoricalChainstate(), nullptr);
692 }
693}
694
695BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion, SnapshotTestSetup)
696{
697 this->SetupSnapshot();
698
700 Chainstate& active_cs = chainman.ActiveChainstate();
701 Chainstate& validated_cs{*Assert(WITH_LOCK(cs_main, return chainman.HistoricalChainstate()))};
702 auto tip_cache_before_complete = active_cs.m_coinstip_cache_size_bytes;
703 auto db_cache_before_complete = active_cs.m_coinsdb_cache_size_bytes;
704
706 m_node.notifications->m_shutdown_on_fatal_error = false;
707
708 fs::path snapshot_chainstate_dir = *node::FindAssumeutxoChainstateDir(chainman.m_options.datadir);
709 BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
710 BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
711
713 const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
714 return chainman.ActiveTip()->GetBlockHash());
715
716 res = WITH_LOCK(::cs_main, return chainman.MaybeValidateSnapshot(validated_cs, active_cs));
718
719 BOOST_CHECK(WITH_LOCK(::cs_main, return chainman.CurrentChainstate().m_assumeutxo == Assumeutxo::VALIDATED));
721 BOOST_CHECK_EQUAL(WITH_LOCK(chainman.GetMutex(), return chainman.HistoricalChainstate()), nullptr);
722
723 // Cache should have been rebalanced and reallocated to the "only" remaining
724 // chainstate.
725 BOOST_CHECK(active_cs.m_coinstip_cache_size_bytes > tip_cache_before_complete);
726 BOOST_CHECK(active_cs.m_coinsdb_cache_size_bytes > db_cache_before_complete);
727
728 // Trying completion again should return false.
729 res = WITH_LOCK(::cs_main, return chainman.MaybeValidateSnapshot(validated_cs, active_cs));
731
732 // The invalid snapshot path should not have been used.
733 fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
734 BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
735 // chainstate_snapshot should still exist.
736 BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
737
738 // Test that simulating a shutdown (resetting ChainstateManager) and then performing
739 // chainstate reinitializing successfully cleans up the background-validation
740 // chainstate data, and we end up with a single chainstate that is at tip.
741 ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
742
743 BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
744
745 // This call reinitializes the chainstates, and should clean up the now unnecessary
746 // background-validation leveldb contents.
747 this->LoadVerifyActivateChainstate();
748
749 BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
750 // chainstate_snapshot should now *not* exist.
751 BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
752
753 const Chainstate& active_cs2 = chainman_restarted.ActiveChainstate();
754
755 {
756 LOCK(chainman_restarted.GetMutex());
757 BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates.size(), 1);
759 BOOST_CHECK(active_cs2.m_coinstip_cache_size_bytes > tip_cache_before_complete);
760 BOOST_CHECK(active_cs2.m_coinsdb_cache_size_bytes > db_cache_before_complete);
761
762 BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
763 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
764 }
765
766 BOOST_TEST_MESSAGE(
767 "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
768 mineBlocks(10);
769 {
770 LOCK(chainman_restarted.GetMutex());
771 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
772 }
773}
774
775BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion_hash_mismatch, SnapshotTestSetup)
776{
777 auto chainstates = this->SetupSnapshot();
778 Chainstate& validation_chainstate = *std::get<0>(chainstates);
779 Chainstate& unvalidated_cs = *std::get<1>(chainstates);
782 m_node.notifications->m_shutdown_on_fatal_error = false;
783
784 // Test tampering with the IBD UTXO set with an extra coin to ensure it causes
785 // snapshot completion to fail.
787 return validation_chainstate.CoinsTip());
788 Coin badcoin;
789 badcoin.out.nValue = m_rng.rand32();
790 badcoin.nHeight = 1;
791 badcoin.out.scriptPubKey.assign(m_rng.randbits(6), 0);
792 Txid txid = Txid::FromUint256(m_rng.rand256());
793 ibd_coins.AddCoin(COutPoint(txid, 0), std::move(badcoin), false);
794
795 fs::path snapshot_chainstate_dir = gArgs.GetDataDirNet() / "chainstate_snapshot";
796 BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
797
798 {
799 ASSERT_DEBUG_LOG("failed to validate the -assumeutxo snapshot state");
800 res = WITH_LOCK(::cs_main, return chainman.MaybeValidateSnapshot(validation_chainstate, unvalidated_cs));
802 }
803
804 {
805 LOCK(chainman.GetMutex());
806 BOOST_CHECK_EQUAL(chainman.m_chainstates.size(), 2);
807 BOOST_CHECK(chainman.m_chainstates[0]->m_assumeutxo == Assumeutxo::VALIDATED);
808 BOOST_CHECK(!chainman.m_chainstates[0]->SnapshotBase());
809 BOOST_CHECK(chainman.m_chainstates[1]->m_assumeutxo == Assumeutxo::INVALID);
810 BOOST_CHECK(chainman.m_chainstates[1]->SnapshotBase());
811 }
812
813 fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
814 BOOST_CHECK(fs::exists(snapshot_invalid_dir));
815
816 // Test that simulating a shutdown (resetting ChainstateManager) and then performing
817 // chainstate reinitializing successfully loads only the fully-validated
818 // chainstate data, and we end up with a single chainstate that is at tip.
819 ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
820
821 BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
822
823 // This call reinitializes the chainstates, and should clean up the now unnecessary
824 // background-validation leveldb contents.
825 this->LoadVerifyActivateChainstate();
826
827 BOOST_CHECK(fs::exists(snapshot_invalid_dir));
828 BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
829
830 {
832 BOOST_CHECK_EQUAL(chainman_restarted.m_chainstates.size(), 1);
834 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
835 }
836
837 BOOST_TEST_MESSAGE(
838 "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
839 mineBlocks(10);
840 {
842 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
843 }
844}
845
847template <typename Options>
849 const std::vector<const char*>& args)
850{
851 const auto argv{Cat({"ignore"}, args)};
852 std::string error{};
853 if (!args_man.ParseParameters(argv.size(), argv.data(), error)) {
854 return util::Error{Untranslated("ParseParameters failed with error: " + error)};
855 }
856 const auto result{node::ApplyArgsManOptions(args_man, opts)};
857 if (!result) return util::Error{util::ErrorString(result)};
858 return opts;
859}
860
862{
864 auto get_opts = [&](const std::vector<const char*>& args) {
865 static kernel::Notifications notifications{};
866 static const ChainstateManager::Options options{
868 .datadir = {},
869 .notifications = notifications};
870 return SetOptsFromArgs(*this->m_node.args, options, args);
871 };
873 auto get_valid_opts = [&](const std::vector<const char*>& args) {
874 const auto result{get_opts(args)};
875 BOOST_REQUIRE_MESSAGE(result, util::ErrorString(result).original);
876 return *result;
877 };
878
879 // test -assumevalid
880 BOOST_CHECK(!get_valid_opts({}).assumed_valid_block);
881 BOOST_CHECK_EQUAL(get_valid_opts({"-assumevalid="}).assumed_valid_block, uint256::ZERO);
882 BOOST_CHECK_EQUAL(get_valid_opts({"-assumevalid=0"}).assumed_valid_block, uint256::ZERO);
883 BOOST_CHECK_EQUAL(get_valid_opts({"-noassumevalid"}).assumed_valid_block, uint256::ZERO);
884 BOOST_CHECK_EQUAL(get_valid_opts({"-assumevalid=0x12"}).assumed_valid_block, uint256{0x12});
885
886 std::string assume_valid{"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"};
887 BOOST_CHECK_EQUAL(get_valid_opts({("-assumevalid=" + assume_valid).c_str()}).assumed_valid_block, uint256::FromHex(assume_valid));
888
889 BOOST_CHECK(!get_opts({"-assumevalid=xyz"})); // invalid hex characters
890 BOOST_CHECK(!get_opts({"-assumevalid=01234567890123456789012345678901234567890123456789012345678901234"})); // > 64 hex chars
891
892 // test -minimumchainwork
893 BOOST_CHECK(!get_valid_opts({}).minimum_chain_work);
894 BOOST_CHECK_EQUAL(get_valid_opts({"-minimumchainwork=0"}).minimum_chain_work, arith_uint256());
895 BOOST_CHECK_EQUAL(get_valid_opts({"-nominimumchainwork"}).minimum_chain_work, arith_uint256());
896 BOOST_CHECK_EQUAL(get_valid_opts({"-minimumchainwork=0x1234"}).minimum_chain_work, arith_uint256{0x1234});
897
898 std::string minimum_chainwork{"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"};
899 BOOST_CHECK_EQUAL(get_valid_opts({("-minimumchainwork=" + minimum_chainwork).c_str()}).minimum_chain_work, UintToArith256(uint256::FromHex(minimum_chainwork).value()));
900
901 BOOST_CHECK(!get_opts({"-minimumchainwork=xyz"})); // invalid hex characters
902 BOOST_CHECK(!get_opts({"-minimumchainwork=01234567890123456789012345678901234567890123456789012345678901234"})); // > 64 hex chars
903}
904
ArgsManager gArgs
Definition: args.cpp:40
arith_uint256 UintToArith256(const uint256 &a)
static void pool cs
node::NodeContext m_node
Definition: bitcoin-gui.cpp:43
ArgsManager & args
Definition: bitcoind.cpp:277
@ BLOCK_VALID_TREE
All parent headers found, difficulty matches, timestamp >= median previous.
Definition: chain.h:51
@ BLOCK_FAILED_CHILD
Unused flag that was previously set when descending from failed block.
Definition: chain.h:80
@ BLOCK_FAILED_VALID
stage after last reached validness failed
Definition: chain.h:79
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
Definition: check.h:113
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: args.cpp:177
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:239
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:373
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:94
uint64_t m_chain_tx_count
(memory only) Number of transactions in the chain up to and including this block.
Definition: chain.h:129
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: chain.h:118
uint32_t nTime
Definition: chain.h:142
uint256 GetBlockHash() const
Definition: chain.h:198
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:396
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
Definition: chain.cpp:16
int Height() const
Return the maximal height in the chain.
Definition: chain.h:425
std::optional< AssumeutxoData > AssumeutxoForHeight(int height) const
Definition: chainparams.h:119
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:367
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
Definition: coins.cpp:89
unsigned int GetCacheSize() const
Size of the cache (in number of transaction outputs)
Definition: coins.cpp:325
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: coins.cpp:188
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
CScript scriptPubKey
Definition: transaction.h:143
CAmount nValue
Definition: transaction.h:142
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:551
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:625
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
Definition: validation.h:721
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:686
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
Definition: validation.h:718
const std::optional< uint256 > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from.
Definition: validation.h:637
bool DisconnectTip(BlockValidationState &state, DisconnectedBlockTransactions *disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Disconnect m_chain's tip.
std::set< CBlockIndex *, node::CBlockIndexWorkComparator > setBlockIndexCandidates
The set of all CBlockIndex entries that have as much work as our current tip or more,...
Definition: validation.h:683
CoinsCacheSizeState GetCoinsCacheSizeState() EXCLUSIVE_LOCKS_REQUIRED(CoinsCacheSizeState GetCoinsCacheSizeState(size_t max_coins_cache_size_bytes, size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(RecursiveMutex * MempoolMutex() const LOCK_RETURNED(m_mempool -> cs)
Dictates whether we need to flush the cache to disk or not.
Definition: validation.h:839
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
Definition: validation.h:936
Chainstate * HistoricalChainstate() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Return historical chainstate targeting a specific block, if any.
Definition: validation.h:1124
size_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
Definition: validation.h:1078
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
Definition: validation.h:1028
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1163
Chainstate & ActiveChainstate() const
Alternatives to CurrentChainstate() used by older code to query latest chainstate information without...
SnapshotCompletionResult MaybeValidateSnapshot(Chainstate &validated_cs, Chainstate &unvalidated_cs) EXCLUSIVE_LOCKS_REQUIRED(Chainstate & CurrentChainstate() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Try to validate an assumeutxo snapshot by using a validated historical chainstate targeted at the sna...
Definition: validation.h:1115
size_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
Definition: validation.h:1082
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1162
const Options m_options
Definition: validation.h:1031
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex.
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1161
void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:1062
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:1034
A UTXO entry.
Definition: coins.h:34
CTxOut out
unspent transaction output
Definition: coins.h:37
uint32_t nHeight
at which height this containing transaction was included in the active block chain
Definition: coins.h:43
DisconnectedBlockTransactions.
256-bit unsigned big integer.
A base class defining functions for notifying about certain kernel events.
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
Definition: blockstorage.h:192
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:38
uint256 m_base_blockhash
The hash of the block that reflects the tip of the chain for the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:45
uint64_t m_coins_count
The number of coins in the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:50
void assign(size_type n, const T &val)
Definition: prevector.h:176
static transaction_identifier FromUint256(const uint256 &id)
256-bit opaque blob.
Definition: uint256.h:195
static const uint256 ONE
Definition: uint256.h:204
static const uint256 ZERO
Definition: uint256.h:203
static std::optional< uint256 > FromHex(std::string_view str)
Definition: uint256.h:197
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
static const unsigned int MAX_DISCONNECTED_TX_POOL_BYTES
Maximum bytes for transactions to store for processing during reorg.
static bool exists(const path &p)
Definition: fs.h:95
util::Result< void > ApplyArgsManOptions(const ArgsManager &args, BlockManager::Options &opts)
std::optional< fs::path > FindAssumeutxoChainstateDir(const fs::path &data_dir)
Return a path to the snapshot-based chainstate dir, if one exists.
std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir)
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:93
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
#define BOOST_CHECK(expr)
Definition: object.cpp:16
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:330
Basic testing setup.
Definition: setup_common.h:64
Testing setup that performs all steps up until right before ChainstateManager gets initialized.
Definition: setup_common.h:106
Application-specific storage settings.
Definition: dbwrapper.h:33
fs::path path
Location in the filesystem where leveldb data will be stored.
Definition: dbwrapper.h:35
std::tuple< Chainstate *, Chainstate * > SetupSnapshot()
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:146
Testing setup that configures a complete environment.
Definition: setup_common.h:121
An options struct for ChainstateManager, more ergonomically referred to as ChainstateManager::Options...
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
Definition: context.h:88
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:68
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:72
std::unique_ptr< node::Warnings > warnings
Manages all the node warnings.
Definition: context.h:91
std::function< bool()> shutdown_request
Function to request a shutdown.
Definition: context.h:63
std::unique_ptr< KernelNotifications > notifications
Issues blocking calls about sync status, errors and warnings.
Definition: context.h:86
util::SignalInterrupt * shutdown_signal
Interrupt object used to track whether node shutdown was requested.
Definition: context.h:65
ArgsManager * args
Definition: context.h:74
std::atomic< int > exit_status
Definition: context.h:89
#define LOCK2(cs1, cs2)
Definition: sync.h:259
#define LOCK(cs)
Definition: sync.h:258
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:289
static bool CreateAndActivateUTXOSnapshot(TestingSetup *fixture, F malleation=NoMalleation, bool reset_chainstate=false, bool in_memory_chainstate=false)
Create and activate a UTXO snapshot, optionally providing a function to malleate the snapshot.
Definition: chainstate.h:33
#define ASSERT_DEBUG_LOG(message)
Definition: logging.h:42
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:82
assert(!tx.IsCoinBase())
SnapshotCompletionResult
Definition: validation.h:898
@ VALIDATED
Every block in the chain has been validated.
@ UNVALIDATED
Blocks after an assumeutxo snapshot have been validated but the snapshot itself has not been validate...
@ INVALID
The assumeutxo snapshot failed validation.
BOOST_FIXTURE_TEST_CASE(chainstatemanager, TestChain100Setup)
Basic tests for ChainstateManager.
util::Result< Options > SetOptsFromArgs(ArgsManager &args_man, Options opts, const std::vector< const char * > &args)
Helper function to parse args into args_man and return the result of applying them to opts.
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
Definition: vector.h:34