Bitcoin Core 28.99.0
P2P Digital Currency
validation_chainstatemanager_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2019-2022 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 std::vector<Chainstate*> chainstates;
44
45 BOOST_CHECK(!manager.SnapshotBlockhash().has_value());
46
47 // Create a legacy (IBD) chainstate.
48 //
49 Chainstate& c1 = manager.ActiveChainstate();
50 chainstates.push_back(&c1);
51
52 BOOST_CHECK(!manager.IsSnapshotActive());
54 auto all = manager.GetAll();
55 BOOST_CHECK_EQUAL_COLLECTIONS(all.begin(), all.end(), chainstates.begin(), chainstates.end());
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
67 BOOST_CHECK(!manager.SnapshotBlockhash().has_value());
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.ActivateExistingSnapshot(snapshot_blockhash));
73 chainstates.push_back(&c2);
74 c2.InitCoinsDB(
75 /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
76 {
78 c2.InitCoinsCache(1 << 23);
79 c2.CoinsTip().SetBestBlock(active_tip->GetBlockHash());
80 c2.setBlockIndexCandidates.insert(manager.m_blockman.LookupBlockIndex(active_tip->GetBlockHash()));
81 c2.LoadChainTip();
82 }
84 BOOST_CHECK(c2.ActivateBestChain(_, nullptr));
85
86 BOOST_CHECK_EQUAL(manager.SnapshotBlockhash().value(), snapshot_blockhash);
89 BOOST_CHECK_EQUAL(&c2, &manager.ActiveChainstate());
90 BOOST_CHECK(&c1 != &manager.ActiveChainstate());
91 auto all2 = manager.GetAll();
92 BOOST_CHECK_EQUAL_COLLECTIONS(all2.begin(), all2.end(), chainstates.begin(), chainstates.end());
93
94 auto& active_chain2 = WITH_LOCK(manager.GetMutex(), return manager.ActiveChain());
95 BOOST_CHECK_EQUAL(&active_chain2, &c2.m_chain);
96
97 BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 110);
98 mineBlocks(1);
99 BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 111);
100 BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return c1.m_chain.Height()), 110);
101
102 auto active_tip2 = WITH_LOCK(manager.GetMutex(), return manager.ActiveTip());
103 BOOST_CHECK_EQUAL(active_tip, active_tip2->pprev);
104 BOOST_CHECK_EQUAL(active_tip, c1.m_chain.Tip());
105 BOOST_CHECK_EQUAL(active_tip2, c2.m_chain.Tip());
106
107 // Let scheduler events finish running to avoid accessing memory that is going to be unloaded
108 m_node.validation_signals->SyncWithValidationInterfaceQueue();
109}
110
112BOOST_FIXTURE_TEST_CASE(chainstatemanager_rebalance_caches, TestChain100Setup)
113{
115
116 size_t max_cache = 10000;
117 manager.m_total_coinsdb_cache = max_cache;
118 manager.m_total_coinstip_cache = max_cache;
119
120 std::vector<Chainstate*> chainstates;
121
122 // Create a legacy (IBD) chainstate.
123 //
124 Chainstate& c1 = manager.ActiveChainstate();
125 chainstates.push_back(&c1);
126 {
128 c1.InitCoinsCache(1 << 23);
129 manager.MaybeRebalanceCaches();
130 }
131
134
135 // Create a snapshot-based chainstate.
136 //
137 CBlockIndex* snapshot_base{WITH_LOCK(manager.GetMutex(), return manager.ActiveChain()[manager.ActiveChain().Height() / 2])};
138 Chainstate& c2 = WITH_LOCK(cs_main, return manager.ActivateExistingSnapshot(*snapshot_base->phashBlock));
139 chainstates.push_back(&c2);
140 c2.InitCoinsDB(
141 /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
142
143 // Reset IBD state so IsInitialBlockDownload() returns true and causes
144 // MaybeRebalancesCaches() to prioritize the snapshot chainstate, giving it
145 // more cache space than the snapshot chainstate. Calling ResetIbd() is
146 // necessary because m_cached_finished_ibd is already latched to true before
147 // the test starts due to the test setup. After ResetIbd() is called.
148 // IsInitialBlockDownload will return true because at this point the active
149 // chainstate has a null chain tip.
150 static_cast<TestChainstateManager&>(manager).ResetIbd();
151
152 {
154 c2.InitCoinsCache(1 << 23);
155 manager.MaybeRebalanceCaches();
156 }
157
158 BOOST_CHECK_CLOSE(double(c1.m_coinstip_cache_size_bytes), max_cache * 0.05, 1);
159 BOOST_CHECK_CLOSE(double(c1.m_coinsdb_cache_size_bytes), max_cache * 0.05, 1);
160 BOOST_CHECK_CLOSE(double(c2.m_coinstip_cache_size_bytes), max_cache * 0.95, 1);
161 BOOST_CHECK_CLOSE(double(c2.m_coinsdb_cache_size_bytes), max_cache * 0.95, 1);
162}
163
165 // Run with coinsdb on the filesystem to support, e.g., moving invalidated
166 // chainstate dirs to "*_invalid".
167 //
168 // Note that this means the tests run considerably slower than in-memory DB
169 // tests, but we can't otherwise test this functionality since it relies on
170 // destructive filesystem operations.
172 {},
173 {
174 .coins_db_in_memory = false,
175 .block_tree_db_in_memory = false,
176 },
177 }
178 {
179 }
180
181 std::tuple<Chainstate*, Chainstate*> SetupSnapshot()
182 {
184
185 BOOST_CHECK(!chainman.IsSnapshotActive());
186
187 {
189 BOOST_CHECK(!chainman.IsSnapshotValidated());
191 }
192
193 size_t initial_size;
194 size_t initial_total_coins{100};
195
196 // Make some initial assertions about the contents of the chainstate.
197 {
199 CCoinsViewCache& ibd_coinscache = chainman.ActiveChainstate().CoinsTip();
200 initial_size = ibd_coinscache.GetCacheSize();
201 size_t total_coins{0};
202
203 for (CTransactionRef& txn : m_coinbase_txns) {
204 COutPoint op{txn->GetHash(), 0};
205 BOOST_CHECK(ibd_coinscache.HaveCoin(op));
206 total_coins++;
207 }
208
209 BOOST_CHECK_EQUAL(total_coins, initial_total_coins);
210 BOOST_CHECK_EQUAL(initial_size, initial_total_coins);
211 }
212
213 Chainstate& validation_chainstate = chainman.ActiveChainstate();
214
215 // Snapshot should refuse to load at this height.
216 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(this));
217 BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
218 BOOST_CHECK(!chainman.SnapshotBlockhash());
219
220 // Mine 10 more blocks, putting at us height 110 where a valid assumeutxo value can
221 // be found.
222 constexpr int snapshot_height = 110;
223 mineBlocks(10);
224 initial_size += 10;
225 initial_total_coins += 10;
226
227 // Should not load malleated snapshots
228 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
229 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
230 // A UTXO is missing but count is correct
231 metadata.m_coins_count -= 1;
232
233 Txid txid;
234 auto_infile >> txid;
235 // coins size
236 (void)ReadCompactSize(auto_infile);
237 // vout index
238 (void)ReadCompactSize(auto_infile);
239 Coin coin;
240 auto_infile >> coin;
241 }));
242
244
245 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
246 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
247 // Coins count is larger than coins in file
248 metadata.m_coins_count += 1;
249 }));
250 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
251 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
252 // Coins count is smaller than coins in file
253 metadata.m_coins_count -= 1;
254 }));
255 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
256 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
257 // Wrong hash
259 }));
260 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
261 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
262 // Wrong hash
264 }));
265
266 BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(this));
268
269 // Ensure our active chain is the snapshot chainstate.
270 BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
272 *chainman.ActiveChainstate().m_from_snapshot_blockhash,
273 *chainman.SnapshotBlockhash());
274
275 Chainstate& snapshot_chainstate = chainman.ActiveChainstate();
276
277 {
279
281
282 // Note: WriteSnapshotBaseBlockhash() is implicitly tested above.
285 *chainman.SnapshotBlockhash());
286 }
287
288 const auto& au_data = ::Params().AssumeutxoForHeight(snapshot_height);
289 const CBlockIndex* tip = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip());
290
291 BOOST_CHECK_EQUAL(tip->m_chain_tx_count, au_data->m_chain_tx_count);
292
293 // To be checked against later when we try loading a subsequent snapshot.
294 uint256 loaded_snapshot_blockhash{*chainman.SnapshotBlockhash()};
295
296 // Make some assertions about the both chainstates. These checks ensure the
297 // legacy chainstate hasn't changed and that the newly created chainstate
298 // reflects the expected content.
299 {
301 int chains_tested{0};
302
303 for (Chainstate* chainstate : chainman.GetAll()) {
304 BOOST_TEST_MESSAGE("Checking coins in " << chainstate->ToString());
305 CCoinsViewCache& coinscache = chainstate->CoinsTip();
306
307 // Both caches will be empty initially.
308 BOOST_CHECK_EQUAL((unsigned int)0, coinscache.GetCacheSize());
309
310 size_t total_coins{0};
311
312 for (CTransactionRef& txn : m_coinbase_txns) {
313 COutPoint op{txn->GetHash(), 0};
314 BOOST_CHECK(coinscache.HaveCoin(op));
315 total_coins++;
316 }
317
318 BOOST_CHECK_EQUAL(initial_size , coinscache.GetCacheSize());
319 BOOST_CHECK_EQUAL(total_coins, initial_total_coins);
320 chains_tested++;
321 }
322
323 BOOST_CHECK_EQUAL(chains_tested, 2);
324 }
325
326 // Mine some new blocks on top of the activated snapshot chainstate.
327 constexpr size_t new_coins{100};
328 mineBlocks(new_coins); // Defined in TestChain100Setup.
329
330 {
332 size_t coins_in_active{0};
333 size_t coins_in_background{0};
334 size_t coins_missing_from_background{0};
335
336 for (Chainstate* chainstate : chainman.GetAll()) {
337 BOOST_TEST_MESSAGE("Checking coins in " << chainstate->ToString());
338 CCoinsViewCache& coinscache = chainstate->CoinsTip();
339 bool is_background = chainstate != &chainman.ActiveChainstate();
340
341 for (CTransactionRef& txn : m_coinbase_txns) {
342 COutPoint op{txn->GetHash(), 0};
343 if (coinscache.HaveCoin(op)) {
344 (is_background ? coins_in_background : coins_in_active)++;
345 } else if (is_background) {
346 coins_missing_from_background++;
347 }
348 }
349 }
350
351 BOOST_CHECK_EQUAL(coins_in_active, initial_total_coins + new_coins);
352 BOOST_CHECK_EQUAL(coins_in_background, initial_total_coins);
353 BOOST_CHECK_EQUAL(coins_missing_from_background, new_coins);
354 }
355
356 // Snapshot should refuse to load after one has already loaded.
357 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(this));
358
359 // Snapshot blockhash should be unchanged.
361 *chainman.ActiveChainstate().m_from_snapshot_blockhash,
362 loaded_snapshot_blockhash);
363 return std::make_tuple(&validation_chainstate, &snapshot_chainstate);
364 }
365
366 // Simulate a restart of the node by flushing all state to disk, clearing the
367 // existing ChainstateManager, and unloading the block index.
368 //
369 // @returns a reference to the "restarted" ChainstateManager
371 {
373
374 BOOST_TEST_MESSAGE("Simulating node restart");
375 {
376 for (Chainstate* cs : chainman.GetAll()) {
378 cs->ForceFlushStateToDisk();
379 }
380 // Process all callbacks referring to the old manager before wiping it.
381 m_node.validation_signals->SyncWithValidationInterfaceQueue();
383 chainman.ResetChainstates();
384 BOOST_CHECK_EQUAL(chainman.GetAll().size(), 0);
385 m_node.notifications = std::make_unique<KernelNotifications>(Assert(m_node.shutdown_request), m_node.exit_status, *Assert(m_node.warnings));
386 const ChainstateManager::Options chainman_opts{
388 .datadir = chainman.m_options.datadir,
389 .notifications = *m_node.notifications,
390 .signals = m_node.validation_signals.get(),
391 };
392 const BlockManager::Options blockman_opts{
393 .chainparams = chainman_opts.chainparams,
394 .blocks_dir = m_args.GetBlocksDirPath(),
395 .notifications = chainman_opts.notifications,
396 };
397 // For robustness, ensure the old manager is destroyed before creating a
398 // new one.
399 m_node.chainman.reset();
400 m_node.chainman = std::make_unique<ChainstateManager>(*Assert(m_node.shutdown_signal), chainman_opts, blockman_opts);
401 }
402 return *Assert(m_node.chainman);
403 }
404};
405
407BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, SnapshotTestSetup)
408{
409 this->SetupSnapshot();
410}
411
422BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
423{
425 Chainstate& cs1 = chainman.ActiveChainstate();
426
427 int num_indexes{0};
428 // Blocks in range [assumed_valid_start_idx, last_assumed_valid_idx) will be
429 // marked as assumed-valid and not having data.
430 const int expected_assumed_valid{20};
431 const int last_assumed_valid_idx{111};
432 const int assumed_valid_start_idx = last_assumed_valid_idx - expected_assumed_valid;
433
434 // Mine to height 120, past the hardcoded regtest assumeutxo snapshot at
435 // height 110
436 mineBlocks(20);
437
438 CBlockIndex* validated_tip{nullptr};
439 CBlockIndex* assumed_base{nullptr};
440 CBlockIndex* assumed_tip{WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip())};
441 BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120);
442
443 auto reload_all_block_indexes = [&]() {
444 // For completeness, we also reset the block sequence counters to
445 // ensure that no state which affects the ranking of tip-candidates is
446 // retained (even though this isn't strictly necessary).
448 for (Chainstate* cs : chainman.GetAll()) {
450 cs->ClearBlockIndexCandidates();
451 BOOST_CHECK(cs->setBlockIndexCandidates.empty());
452 }
453
455 };
456
457 // Ensure that without any assumed-valid BlockIndex entries, only the current tip is
458 // considered as a candidate.
459 reload_all_block_indexes();
461
462 // Reset some region of the chain's nStatus, removing the HAVE_DATA flag.
463 for (int i = 0; i <= cs1.m_chain.Height(); ++i) {
465 auto index = cs1.m_chain[i];
466
467 // Blocks with heights in range [91, 110] are marked as missing data.
468 if (i < last_assumed_valid_idx && i >= assumed_valid_start_idx) {
469 index->nStatus = BlockStatus::BLOCK_VALID_TREE;
470 index->nTx = 0;
471 index->m_chain_tx_count = 0;
472 }
473
474 ++num_indexes;
475
476 // Note the last fully-validated block as the expected validated tip.
477 if (i == (assumed_valid_start_idx - 1)) {
478 validated_tip = index;
479 }
480 // Note the last assumed valid block as the snapshot base
481 if (i == last_assumed_valid_idx - 1) {
482 assumed_base = index;
483 }
484 }
485
486 // Note: cs2's tip is not set when ActivateExistingSnapshot is called.
488 return chainman.ActivateExistingSnapshot(*assumed_base->phashBlock));
489
490 // Set tip of the fully validated chain to be the validated tip
491 cs1.m_chain.SetTip(*validated_tip);
492
493 // Set tip of the assume-valid-based chain to the assume-valid block
494 cs2.m_chain.SetTip(*assumed_base);
495
496 // Sanity check test variables.
497 BOOST_CHECK_EQUAL(num_indexes, 121); // 121 total blocks, including genesis
498 BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120); // original chain has height 120
499 BOOST_CHECK_EQUAL(validated_tip->nHeight, 90); // current cs1 chain has height 90
500 BOOST_CHECK_EQUAL(assumed_base->nHeight, 110); // current cs2 chain has height 110
501
502 // Regenerate cs1.setBlockIndexCandidates and cs2.setBlockIndexCandidate and
503 // check contents below.
504 reload_all_block_indexes();
505
506 // The fully validated chain should only have the current validated tip and
507 // the assumed valid base as candidates, blocks 90 and 110. Specifically:
508 //
509 // - It does not have blocks 0-89 because they contain less work than the
510 // chain tip.
511 //
512 // - It has block 90 because it has data and equal work to the chain tip,
513 // (since it is the chain tip).
514 //
515 // - It does not have blocks 91-109 because they do not contain data.
516 //
517 // - It has block 110 even though it does not have data, because
518 // LoadBlockIndex has a special case to always add the snapshot block as a
519 // candidate. The special case is only actually intended to apply to the
520 // snapshot chainstate cs2, not the background chainstate cs1, but it is
521 // written broadly and applies to both.
522 //
523 // - It does not have any blocks after height 110 because cs1 is a background
524 // chainstate, and only blocks where are ancestors of the snapshot block
525 // are added as candidates for the background chainstate.
527 BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(validated_tip), 1);
528 BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(assumed_base), 1);
529
530 // The assumed-valid tolerant chain has the assumed valid base as a
531 // candidate, but otherwise has none of the assumed-valid (which do not
532 // HAVE_DATA) blocks as candidates.
533 //
534 // Specifically:
535 // - All blocks below height 110 are not candidates, because cs2 chain tip
536 // has height 110 and they have less work than it does.
537 //
538 // - Block 110 is a candidate even though it does not have data, because it
539 // is the snapshot block, which is assumed valid.
540 //
541 // - Blocks 111-120 are added because they have data.
542
543 // Check that block 90 is absent
544 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(validated_tip), 0);
545 // Check that block 109 is absent
546 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base->pprev), 0);
547 // Check that block 110 is present
548 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base), 1);
549 // Check that block 120 is present
550 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_tip), 1);
551 // Check that 11 blocks total are present.
552 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.size(), num_indexes - last_assumed_valid_idx + 1);
553}
554
556BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_init, SnapshotTestSetup)
557{
559 Chainstate& bg_chainstate = chainman.ActiveChainstate();
560
561 this->SetupSnapshot();
562
563 fs::path snapshot_chainstate_dir = *node::FindSnapshotChainstateDir(chainman.m_options.datadir);
564 BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
565 BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
566
567 BOOST_CHECK(chainman.IsSnapshotActive());
568 const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
569 return chainman.ActiveTip()->GetBlockHash());
570
571 auto all_chainstates = chainman.GetAll();
572 BOOST_CHECK_EQUAL(all_chainstates.size(), 2);
573
574 // "Rewind" the background chainstate so that its tip is not at the
575 // base block of the snapshot - this is so after simulating a node restart,
576 // it will initialize instead of attempting to complete validation.
577 //
578 // Note that this is not a realistic use of DisconnectTip().
580 BlockValidationState unused_state;
581 {
582 LOCK2(::cs_main, bg_chainstate.MempoolMutex());
583 BOOST_CHECK(bg_chainstate.DisconnectTip(unused_state, &unused_pool));
584 unused_pool.clear(); // to avoid queuedTx assertion errors on teardown
585 }
586 BOOST_CHECK_EQUAL(bg_chainstate.m_chain.Height(), 109);
587
588 // Test that simulating a shutdown (resetting ChainstateManager) and then performing
589 // chainstate reinitializing successfully cleans up the background-validation
590 // chainstate data, and we end up with a single chainstate that is at tip.
591 ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
592
593 BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
594
595 // This call reinitializes the chainstates.
596 this->LoadVerifyActivateChainstate();
597
598 {
599 LOCK(chainman_restarted.GetMutex());
600 BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 2);
601 BOOST_CHECK(chainman_restarted.IsSnapshotActive());
602 BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
603
604 BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
605 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
606 }
607
608 BOOST_TEST_MESSAGE(
609 "Ensure we can mine blocks on top of the initialized snapshot chainstate");
610 mineBlocks(10);
611 {
612 LOCK(chainman_restarted.GetMutex());
613 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
614
615 // Background chainstate should be unaware of new blocks on the snapshot
616 // chainstate.
617 for (Chainstate* cs : chainman_restarted.GetAll()) {
618 if (cs != &chainman_restarted.ActiveChainstate()) {
619 BOOST_CHECK_EQUAL(cs->m_chain.Height(), 109);
620 }
621 }
622 }
623}
624
625BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion, SnapshotTestSetup)
626{
627 this->SetupSnapshot();
628
630 Chainstate& active_cs = chainman.ActiveChainstate();
631 auto tip_cache_before_complete = active_cs.m_coinstip_cache_size_bytes;
632 auto db_cache_before_complete = active_cs.m_coinsdb_cache_size_bytes;
633
635 m_node.notifications->m_shutdown_on_fatal_error = false;
636
637 fs::path snapshot_chainstate_dir = *node::FindSnapshotChainstateDir(chainman.m_options.datadir);
638 BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
639 BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
640
641 BOOST_CHECK(chainman.IsSnapshotActive());
642 const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
643 return chainman.ActiveTip()->GetBlockHash());
644
645 res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
647
649 BOOST_CHECK(chainman.IsSnapshotActive());
650
651 // Cache should have been rebalanced and reallocated to the "only" remaining
652 // chainstate.
653 BOOST_CHECK(active_cs.m_coinstip_cache_size_bytes > tip_cache_before_complete);
654 BOOST_CHECK(active_cs.m_coinsdb_cache_size_bytes > db_cache_before_complete);
655
656 auto all_chainstates = chainman.GetAll();
657 BOOST_CHECK_EQUAL(all_chainstates.size(), 1);
658 BOOST_CHECK_EQUAL(all_chainstates[0], &active_cs);
659
660 // Trying completion again should return false.
661 res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
663
664 // The invalid snapshot path should not have been used.
665 fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
666 BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
667 // chainstate_snapshot should still exist.
668 BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
669
670 // Test that simulating a shutdown (resetting ChainstateManager) and then performing
671 // chainstate reinitializing successfully cleans up the background-validation
672 // chainstate data, and we end up with a single chainstate that is at tip.
673 ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
674
675 BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
676
677 // This call reinitializes the chainstates, and should clean up the now unnecessary
678 // background-validation leveldb contents.
679 this->LoadVerifyActivateChainstate();
680
681 BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
682 // chainstate_snapshot should now *not* exist.
683 BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
684
685 const Chainstate& active_cs2 = chainman_restarted.ActiveChainstate();
686
687 {
688 LOCK(chainman_restarted.GetMutex());
689 BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 1);
690 BOOST_CHECK(!chainman_restarted.IsSnapshotActive());
691 BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
692 BOOST_CHECK(active_cs2.m_coinstip_cache_size_bytes > tip_cache_before_complete);
693 BOOST_CHECK(active_cs2.m_coinsdb_cache_size_bytes > db_cache_before_complete);
694
695 BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
696 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
697 }
698
699 BOOST_TEST_MESSAGE(
700 "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
701 mineBlocks(10);
702 {
703 LOCK(chainman_restarted.GetMutex());
704 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
705 }
706}
707
708BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion_hash_mismatch, SnapshotTestSetup)
709{
710 auto chainstates = this->SetupSnapshot();
711 Chainstate& validation_chainstate = *std::get<0>(chainstates);
714 m_node.notifications->m_shutdown_on_fatal_error = false;
715
716 // Test tampering with the IBD UTXO set with an extra coin to ensure it causes
717 // snapshot completion to fail.
719 return validation_chainstate.CoinsTip());
720 Coin badcoin;
721 badcoin.out.nValue = m_rng.rand32();
722 badcoin.nHeight = 1;
723 badcoin.out.scriptPubKey.assign(m_rng.randbits(6), 0);
724 Txid txid = Txid::FromUint256(m_rng.rand256());
725 ibd_coins.AddCoin(COutPoint(txid, 0), std::move(badcoin), false);
726
727 fs::path snapshot_chainstate_dir = gArgs.GetDataDirNet() / "chainstate_snapshot";
728 BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
729
730 {
731 ASSERT_DEBUG_LOG("failed to validate the -assumeutxo snapshot state");
732 res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
734 }
735
736 auto all_chainstates = chainman.GetAll();
737 BOOST_CHECK_EQUAL(all_chainstates.size(), 1);
738 BOOST_CHECK_EQUAL(all_chainstates[0], &validation_chainstate);
739 BOOST_CHECK_EQUAL(&chainman.ActiveChainstate(), &validation_chainstate);
740
741 fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
742 BOOST_CHECK(fs::exists(snapshot_invalid_dir));
743
744 // Test that simulating a shutdown (resetting ChainstateManager) and then performing
745 // chainstate reinitializing successfully loads only the fully-validated
746 // chainstate data, and we end up with a single chainstate that is at tip.
747 ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
748
749 BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
750
751 // This call reinitializes the chainstates, and should clean up the now unnecessary
752 // background-validation leveldb contents.
753 this->LoadVerifyActivateChainstate();
754
755 BOOST_CHECK(fs::exists(snapshot_invalid_dir));
756 BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
757
758 {
760 BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 1);
761 BOOST_CHECK(!chainman_restarted.IsSnapshotActive());
762 BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
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 {
771 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
772 }
773}
774
776template <typename Options>
778 const std::vector<const char*>& args)
779{
780 const auto argv{Cat({"ignore"}, args)};
781 std::string error{};
782 if (!args_man.ParseParameters(argv.size(), argv.data(), error)) {
783 return util::Error{Untranslated("ParseParameters failed with error: " + error)};
784 }
785 const auto result{node::ApplyArgsManOptions(args_man, opts)};
786 if (!result) return util::Error{util::ErrorString(result)};
787 return opts;
788}
789
791{
793 auto get_opts = [&](const std::vector<const char*>& args) {
794 static kernel::Notifications notifications{};
795 static const ChainstateManager::Options options{
797 .datadir = {},
798 .notifications = notifications};
799 return SetOptsFromArgs(*this->m_node.args, options, args);
800 };
802 auto get_valid_opts = [&](const std::vector<const char*>& args) {
803 const auto result{get_opts(args)};
804 BOOST_REQUIRE_MESSAGE(result, util::ErrorString(result).original);
805 return *result;
806 };
807
808 // test -assumevalid
809 BOOST_CHECK(!get_valid_opts({}).assumed_valid_block);
810 BOOST_CHECK_EQUAL(get_valid_opts({"-assumevalid="}).assumed_valid_block, uint256::ZERO);
811 BOOST_CHECK_EQUAL(get_valid_opts({"-assumevalid=0"}).assumed_valid_block, uint256::ZERO);
812 BOOST_CHECK_EQUAL(get_valid_opts({"-noassumevalid"}).assumed_valid_block, uint256::ZERO);
813 BOOST_CHECK_EQUAL(get_valid_opts({"-assumevalid=0x12"}).assumed_valid_block, uint256{0x12});
814
815 std::string assume_valid{"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"};
816 BOOST_CHECK_EQUAL(get_valid_opts({("-assumevalid=" + assume_valid).c_str()}).assumed_valid_block, uint256::FromHex(assume_valid));
817
818 BOOST_CHECK(!get_opts({"-assumevalid=xyz"})); // invalid hex characters
819 BOOST_CHECK(!get_opts({"-assumevalid=01234567890123456789012345678901234567890123456789012345678901234"})); // > 64 hex chars
820
821 // test -minimumchainwork
822 BOOST_CHECK(!get_valid_opts({}).minimum_chain_work);
823 BOOST_CHECK_EQUAL(get_valid_opts({"-minimumchainwork=0"}).minimum_chain_work, arith_uint256());
824 BOOST_CHECK_EQUAL(get_valid_opts({"-nominimumchainwork"}).minimum_chain_work, arith_uint256());
825 BOOST_CHECK_EQUAL(get_valid_opts({"-minimumchainwork=0x1234"}).minimum_chain_work, arith_uint256{0x1234});
826
827 std::string minimum_chainwork{"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"};
828 BOOST_CHECK_EQUAL(get_valid_opts({("-minimumchainwork=" + minimum_chainwork).c_str()}).minimum_chain_work, UintToArith256(uint256::FromHex(minimum_chainwork).value()));
829
830 BOOST_CHECK(!get_opts({"-minimumchainwork=xyz"})); // invalid hex characters
831 BOOST_CHECK(!get_opts({"-minimumchainwork=01234567890123456789012345678901234567890123456789012345678901234"})); // > 64 hex chars
832}
833
ArgsManager gArgs
Definition: args.cpp:42
arith_uint256 UintToArith256(const uint256 &a)
node::NodeContext m_node
Definition: bitcoin-gui.cpp:42
ArgsManager & args
Definition: bitcoind.cpp:277
@ BLOCK_VALID_TREE
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
Definition: chain.h:97
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
Definition: check.h:85
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: args.cpp:179
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:234
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:392
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:141
uint64_t m_chain_tx_count
(memory only) Number of transactions in the chain up to and including this block.
Definition: chain.h:176
uint256 GetBlockHash() const
Definition: chain.h:243
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:433
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
Definition: chain.cpp:21
int Height() const
Return the maximal height in the chain.
Definition: chain.h:462
std::optional< AssumeutxoData > AssumeutxoForHeight(int height) const
Definition: chainparams.h:123
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:363
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
Definition: coins.cpp:72
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
Definition: coins.cpp:289
void SetBestBlock(const uint256 &hashBlock)
Definition: coins.cpp:179
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: coins.cpp:163
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
CScript scriptPubKey
Definition: transaction.h:153
CAmount nValue
Definition: transaction.h:152
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:505
const CBlockIndex *SnapshotBase() EXCLUSIVE_LOCKS_REQUIRED(std::set< CBlockIndex *, node::CBlockIndexWorkComparator > setBlockIndexCandidates
The base of the snapshot this chainstate was created from.
Definition: validation.h:599
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:585
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
Definition: validation.h:649
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:611
bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Update the chain tip based on database information, i.e.
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
Definition: validation.h:646
bool DisconnectTip(BlockValidationState &state, DisconnectedBlockTransactions *disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Disconnect m_chain's tip.
ChainstateRole GetRole() const EXCLUSIVE_LOCKS_REQUIRED(void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe, fs::path leveldb_name="chainstate")
Return the current role of the chainstate.
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:767
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:866
SnapshotCompletionResult MaybeCompleteSnapshotValidation() EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetSnapshotBaseBlock() const EXCLUSIVE_LOCKS_REQUIRED(Chainstate ActiveChainstate)() const
Once the background validation chainstate has reached the height which is the base of the UTXO snapsh...
Definition: validation.h:1110
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:1067
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
Definition: validation.h:1001
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
Definition: validation.h:1143
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1113
size_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
Definition: validation.h:1071
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1112
bool IsSnapshotActive() const
const Options m_options
Definition: validation.h:1004
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex.
std::optional< uint256 > SnapshotBlockhash() const
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1111
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
Definition: validation.h:1080
void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:1033
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:1007
A UTXO entry.
Definition: coins.h:33
CTxOut out
unspent transaction output
Definition: coins.h:36
uint32_t nHeight
at which height this containing transaction was included in the active block chain
Definition: coins.h:42
DisconnectedBlockTransactions.
256-bit unsigned big integer.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
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:136
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:34
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:41
uint64_t m_coins_count
The number of coins in the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:46
void assign(size_type n, const T &val)
Definition: prevector.h:223
static transaction_identifier FromUint256(const uint256 &id)
256-bit opaque blob.
Definition: uint256.h:201
static const uint256 ONE
Definition: uint256.h:210
static const uint256 ZERO
Definition: uint256.h:209
static std::optional< uint256 > FromHex(std::string_view str)
Definition: uint256.h:203
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 void pool cs
static bool exists(const path &p)
Definition: fs.h:89
util::Result< void > ApplyArgsManOptions(const ArgsManager &args, BlockManager::Options &opts)
std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir)
std::optional< fs::path > FindSnapshotChainstateDir(const fs::path &data_dir)
Return a path to the snapshot-based chainstate dir, if one exists.
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:93
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:339
Basic testing setup.
Definition: setup_common.h:64
std::tuple< Chainstate *, Chainstate * > SetupSnapshot()
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:140
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< 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:258
#define LOCK(cs)
Definition: sync.h:257
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:301
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:39
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
SnapshotCompletionResult
Definition: validation.h:817
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