Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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 <random.h>
11#include <rpc/blockchain.h>
12#include <sync.h>
14#include <test/util/logging.h>
15#include <test/util/random.h>
18#include <uint256.h>
19#include <validation.h>
20#include <validationinterface.h>
21
22#include <tinyformat.h>
23
24#include <vector>
25
26#include <boost/test/unit_test.hpp>
27
31
32BOOST_FIXTURE_TEST_SUITE(validation_chainstatemanager_tests, TestingSetup)
33
34
38{
40 std::vector<Chainstate*> chainstates;
41
42 BOOST_CHECK(!manager.SnapshotBlockhash().has_value());
43
44 // Create a legacy (IBD) chainstate.
45 //
46 Chainstate& c1 = manager.ActiveChainstate();
47 chainstates.push_back(&c1);
48
49 BOOST_CHECK(!manager.IsSnapshotActive());
51 auto all = manager.GetAll();
52 BOOST_CHECK_EQUAL_COLLECTIONS(all.begin(), all.end(), chainstates.begin(), chainstates.end());
53
54 auto& active_chain = WITH_LOCK(manager.GetMutex(), return manager.ActiveChain());
55 BOOST_CHECK_EQUAL(&active_chain, &c1.m_chain);
56
57 // Get to a valid assumeutxo tip (per chainparams);
58 mineBlocks(10);
59 BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 110);
60 auto active_tip = WITH_LOCK(manager.GetMutex(), return manager.ActiveTip());
61 auto exp_tip = c1.m_chain.Tip();
62 BOOST_CHECK_EQUAL(active_tip, exp_tip);
63
64 BOOST_CHECK(!manager.SnapshotBlockhash().has_value());
65
66 // Create a snapshot-based chainstate.
67 //
68 const uint256 snapshot_blockhash = active_tip->GetBlockHash();
69 Chainstate& c2 = WITH_LOCK(::cs_main, return manager.ActivateExistingSnapshot(snapshot_blockhash));
70 chainstates.push_back(&c2);
71 c2.InitCoinsDB(
72 /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
73 {
75 c2.InitCoinsCache(1 << 23);
76 c2.CoinsTip().SetBestBlock(active_tip->GetBlockHash());
77 c2.setBlockIndexCandidates.insert(manager.m_blockman.LookupBlockIndex(active_tip->GetBlockHash()));
78 c2.LoadChainTip();
79 }
81 BOOST_CHECK(c2.ActivateBestChain(_, nullptr));
82
83 BOOST_CHECK_EQUAL(manager.SnapshotBlockhash().value(), snapshot_blockhash);
86 BOOST_CHECK_EQUAL(&c2, &manager.ActiveChainstate());
87 BOOST_CHECK(&c1 != &manager.ActiveChainstate());
88 auto all2 = manager.GetAll();
89 BOOST_CHECK_EQUAL_COLLECTIONS(all2.begin(), all2.end(), chainstates.begin(), chainstates.end());
90
91 auto& active_chain2 = WITH_LOCK(manager.GetMutex(), return manager.ActiveChain());
92 BOOST_CHECK_EQUAL(&active_chain2, &c2.m_chain);
93
94 BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 110);
95 mineBlocks(1);
96 BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 111);
97 BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return c1.m_chain.Height()), 110);
98
99 auto active_tip2 = WITH_LOCK(manager.GetMutex(), return manager.ActiveTip());
100 BOOST_CHECK_EQUAL(active_tip, active_tip2->pprev);
101 BOOST_CHECK_EQUAL(active_tip, c1.m_chain.Tip());
102 BOOST_CHECK_EQUAL(active_tip2, c2.m_chain.Tip());
103
104 // Let scheduler events finish running to avoid accessing memory that is going to be unloaded
105 m_node.validation_signals->SyncWithValidationInterfaceQueue();
106}
107
109BOOST_FIXTURE_TEST_CASE(chainstatemanager_rebalance_caches, TestChain100Setup)
110{
112
113 size_t max_cache = 10000;
114 manager.m_total_coinsdb_cache = max_cache;
115 manager.m_total_coinstip_cache = max_cache;
116
117 std::vector<Chainstate*> chainstates;
118
119 // Create a legacy (IBD) chainstate.
120 //
121 Chainstate& c1 = manager.ActiveChainstate();
122 chainstates.push_back(&c1);
123 {
125 c1.InitCoinsCache(1 << 23);
126 manager.MaybeRebalanceCaches();
127 }
128
131
132 // Create a snapshot-based chainstate.
133 //
134 CBlockIndex* snapshot_base{WITH_LOCK(manager.GetMutex(), return manager.ActiveChain()[manager.ActiveChain().Height() / 2])};
135 Chainstate& c2 = WITH_LOCK(cs_main, return manager.ActivateExistingSnapshot(*snapshot_base->phashBlock));
136 chainstates.push_back(&c2);
137 c2.InitCoinsDB(
138 /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
139
140 // Reset IBD state so IsInitialBlockDownload() returns true and causes
141 // MaybeRebalancesCaches() to prioritize the snapshot chainstate, giving it
142 // more cache space than the snapshot chainstate. Calling ResetIbd() is
143 // necessary because m_cached_finished_ibd is already latched to true before
144 // the test starts due to the test setup. After ResetIbd() is called.
145 // IsInitialBlockDownload will return true because at this point the active
146 // chainstate has a null chain tip.
147 static_cast<TestChainstateManager&>(manager).ResetIbd();
148
149 {
151 c2.InitCoinsCache(1 << 23);
152 manager.MaybeRebalanceCaches();
153 }
154
155 BOOST_CHECK_CLOSE(double(c1.m_coinstip_cache_size_bytes), max_cache * 0.05, 1);
156 BOOST_CHECK_CLOSE(double(c1.m_coinsdb_cache_size_bytes), max_cache * 0.05, 1);
157 BOOST_CHECK_CLOSE(double(c2.m_coinstip_cache_size_bytes), max_cache * 0.95, 1);
158 BOOST_CHECK_CLOSE(double(c2.m_coinsdb_cache_size_bytes), max_cache * 0.95, 1);
159}
160
162 // Run with coinsdb on the filesystem to support, e.g., moving invalidated
163 // chainstate dirs to "*_invalid".
164 //
165 // Note that this means the tests run considerably slower than in-memory DB
166 // tests, but we can't otherwise test this functionality since it relies on
167 // destructive filesystem operations.
169 {},
170 {
171 .coins_db_in_memory = false,
172 .block_tree_db_in_memory = false,
173 },
174 }
175 {
176 }
177
178 std::tuple<Chainstate*, Chainstate*> SetupSnapshot()
179 {
181
182 BOOST_CHECK(!chainman.IsSnapshotActive());
183
184 {
186 BOOST_CHECK(!chainman.IsSnapshotValidated());
188 }
189
190 size_t initial_size;
191 size_t initial_total_coins{100};
192
193 // Make some initial assertions about the contents of the chainstate.
194 {
196 CCoinsViewCache& ibd_coinscache = chainman.ActiveChainstate().CoinsTip();
197 initial_size = ibd_coinscache.GetCacheSize();
198 size_t total_coins{0};
199
200 for (CTransactionRef& txn : m_coinbase_txns) {
201 COutPoint op{txn->GetHash(), 0};
202 BOOST_CHECK(ibd_coinscache.HaveCoin(op));
203 total_coins++;
204 }
205
206 BOOST_CHECK_EQUAL(total_coins, initial_total_coins);
207 BOOST_CHECK_EQUAL(initial_size, initial_total_coins);
208 }
209
210 Chainstate& validation_chainstate = chainman.ActiveChainstate();
211
212 // Snapshot should refuse to load at this height.
213 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(this));
214 BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
215 BOOST_CHECK(!chainman.SnapshotBlockhash());
216
217 // Mine 10 more blocks, putting at us height 110 where a valid assumeutxo value can
218 // be found.
219 constexpr int snapshot_height = 110;
220 mineBlocks(10);
221 initial_size += 10;
222 initial_total_coins += 10;
223
224 // Should not load malleated snapshots
225 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
226 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
227 // A UTXO is missing but count is correct
228 metadata.m_coins_count -= 1;
229
230 Txid txid;
231 auto_infile >> txid;
232 // coins size
233 (void)ReadCompactSize(auto_infile);
234 // vout index
235 (void)ReadCompactSize(auto_infile);
236 Coin coin;
237 auto_infile >> coin;
238 }));
239
241
242 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
243 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
244 // Coins count is larger than coins in file
245 metadata.m_coins_count += 1;
246 }));
247 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
248 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
249 // Coins count is smaller than coins in file
250 metadata.m_coins_count -= 1;
251 }));
252 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
253 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
254 // Wrong hash
256 }));
257 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
258 this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
259 // Wrong hash
261 }));
262
263 BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(this));
265
266 // Ensure our active chain is the snapshot chainstate.
267 BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
269 *chainman.ActiveChainstate().m_from_snapshot_blockhash,
270 *chainman.SnapshotBlockhash());
271
272 Chainstate& snapshot_chainstate = chainman.ActiveChainstate();
273
274 {
276
278
279 // Note: WriteSnapshotBaseBlockhash() is implicitly tested above.
282 *chainman.SnapshotBlockhash());
283 }
284
285 const auto& au_data = ::Params().AssumeutxoForHeight(snapshot_height);
286 const CBlockIndex* tip = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip());
287
288 BOOST_CHECK_EQUAL(tip->m_chain_tx_count, au_data->m_chain_tx_count);
289
290 // To be checked against later when we try loading a subsequent snapshot.
291 uint256 loaded_snapshot_blockhash{*chainman.SnapshotBlockhash()};
292
293 // Make some assertions about the both chainstates. These checks ensure the
294 // legacy chainstate hasn't changed and that the newly created chainstate
295 // reflects the expected content.
296 {
298 int chains_tested{0};
299
300 for (Chainstate* chainstate : chainman.GetAll()) {
301 BOOST_TEST_MESSAGE("Checking coins in " << chainstate->ToString());
302 CCoinsViewCache& coinscache = chainstate->CoinsTip();
303
304 // Both caches will be empty initially.
305 BOOST_CHECK_EQUAL((unsigned int)0, coinscache.GetCacheSize());
306
307 size_t total_coins{0};
308
309 for (CTransactionRef& txn : m_coinbase_txns) {
310 COutPoint op{txn->GetHash(), 0};
311 BOOST_CHECK(coinscache.HaveCoin(op));
312 total_coins++;
313 }
314
315 BOOST_CHECK_EQUAL(initial_size , coinscache.GetCacheSize());
316 BOOST_CHECK_EQUAL(total_coins, initial_total_coins);
317 chains_tested++;
318 }
319
320 BOOST_CHECK_EQUAL(chains_tested, 2);
321 }
322
323 // Mine some new blocks on top of the activated snapshot chainstate.
324 constexpr size_t new_coins{100};
325 mineBlocks(new_coins); // Defined in TestChain100Setup.
326
327 {
329 size_t coins_in_active{0};
330 size_t coins_in_background{0};
331 size_t coins_missing_from_background{0};
332
333 for (Chainstate* chainstate : chainman.GetAll()) {
334 BOOST_TEST_MESSAGE("Checking coins in " << chainstate->ToString());
335 CCoinsViewCache& coinscache = chainstate->CoinsTip();
336 bool is_background = chainstate != &chainman.ActiveChainstate();
337
338 for (CTransactionRef& txn : m_coinbase_txns) {
339 COutPoint op{txn->GetHash(), 0};
340 if (coinscache.HaveCoin(op)) {
341 (is_background ? coins_in_background : coins_in_active)++;
342 } else if (is_background) {
343 coins_missing_from_background++;
344 }
345 }
346 }
347
348 BOOST_CHECK_EQUAL(coins_in_active, initial_total_coins + new_coins);
349 BOOST_CHECK_EQUAL(coins_in_background, initial_total_coins);
350 BOOST_CHECK_EQUAL(coins_missing_from_background, new_coins);
351 }
352
353 // Snapshot should refuse to load after one has already loaded.
354 BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(this));
355
356 // Snapshot blockhash should be unchanged.
358 *chainman.ActiveChainstate().m_from_snapshot_blockhash,
359 loaded_snapshot_blockhash);
360 return std::make_tuple(&validation_chainstate, &snapshot_chainstate);
361 }
362
363 // Simulate a restart of the node by flushing all state to disk, clearing the
364 // existing ChainstateManager, and unloading the block index.
365 //
366 // @returns a reference to the "restarted" ChainstateManager
368 {
370
371 BOOST_TEST_MESSAGE("Simulating node restart");
372 {
373 for (Chainstate* cs : chainman.GetAll()) {
375 cs->ForceFlushStateToDisk();
376 }
377 // Process all callbacks referring to the old manager before wiping it.
378 m_node.validation_signals->SyncWithValidationInterfaceQueue();
380 chainman.ResetChainstates();
381 BOOST_CHECK_EQUAL(chainman.GetAll().size(), 0);
382 m_node.notifications = std::make_unique<KernelNotifications>(*Assert(m_node.shutdown), m_node.exit_status, *Assert(m_node.warnings));
383 const ChainstateManager::Options chainman_opts{
384 .chainparams = ::Params(),
385 .datadir = chainman.m_options.datadir,
386 .notifications = *m_node.notifications,
387 .signals = m_node.validation_signals.get(),
388 };
389 const BlockManager::Options blockman_opts{
390 .chainparams = chainman_opts.chainparams,
391 .blocks_dir = m_args.GetBlocksDirPath(),
392 .notifications = chainman_opts.notifications,
393 };
394 // For robustness, ensure the old manager is destroyed before creating a
395 // new one.
396 m_node.chainman.reset();
397 m_node.chainman = std::make_unique<ChainstateManager>(*Assert(m_node.shutdown), chainman_opts, blockman_opts);
398 }
399 return *Assert(m_node.chainman);
400 }
401};
402
404BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, SnapshotTestSetup)
405{
406 this->SetupSnapshot();
407}
408
419BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
420{
422 Chainstate& cs1 = chainman.ActiveChainstate();
423
424 int num_indexes{0};
425 // Blocks in range [assumed_valid_start_idx, last_assumed_valid_idx) will be
426 // marked as assumed-valid and not having data.
427 const int expected_assumed_valid{20};
428 const int last_assumed_valid_idx{111};
429 const int assumed_valid_start_idx = last_assumed_valid_idx - expected_assumed_valid;
430
431 // Mine to height 120, past the hardcoded regtest assumeutxo snapshot at
432 // height 110
433 mineBlocks(20);
434
435 CBlockIndex* validated_tip{nullptr};
436 CBlockIndex* assumed_base{nullptr};
437 CBlockIndex* assumed_tip{WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip())};
438 BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120);
439
440 auto reload_all_block_indexes = [&]() {
441 // For completeness, we also reset the block sequence counters to
442 // ensure that no state which affects the ranking of tip-candidates is
443 // retained (even though this isn't strictly necessary).
445 for (Chainstate* cs : chainman.GetAll()) {
447 cs->ClearBlockIndexCandidates();
448 BOOST_CHECK(cs->setBlockIndexCandidates.empty());
449 }
450
452 };
453
454 // Ensure that without any assumed-valid BlockIndex entries, only the current tip is
455 // considered as a candidate.
456 reload_all_block_indexes();
458
459 // Reset some region of the chain's nStatus, removing the HAVE_DATA flag.
460 for (int i = 0; i <= cs1.m_chain.Height(); ++i) {
462 auto index = cs1.m_chain[i];
463
464 // Blocks with heights in range [91, 110] are marked as missing data.
465 if (i < last_assumed_valid_idx && i >= assumed_valid_start_idx) {
466 index->nStatus = BlockStatus::BLOCK_VALID_TREE;
467 index->nTx = 0;
468 index->m_chain_tx_count = 0;
469 }
470
471 ++num_indexes;
472
473 // Note the last fully-validated block as the expected validated tip.
474 if (i == (assumed_valid_start_idx - 1)) {
475 validated_tip = index;
476 }
477 // Note the last assumed valid block as the snapshot base
478 if (i == last_assumed_valid_idx - 1) {
479 assumed_base = index;
480 }
481 }
482
483 // Note: cs2's tip is not set when ActivateExistingSnapshot is called.
485 return chainman.ActivateExistingSnapshot(*assumed_base->phashBlock));
486
487 // Set tip of the fully validated chain to be the validated tip
488 cs1.m_chain.SetTip(*validated_tip);
489
490 // Set tip of the assume-valid-based chain to the assume-valid block
491 cs2.m_chain.SetTip(*assumed_base);
492
493 // Sanity check test variables.
494 BOOST_CHECK_EQUAL(num_indexes, 121); // 121 total blocks, including genesis
495 BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120); // original chain has height 120
496 BOOST_CHECK_EQUAL(validated_tip->nHeight, 90); // current cs1 chain has height 90
497 BOOST_CHECK_EQUAL(assumed_base->nHeight, 110); // current cs2 chain has height 110
498
499 // Regenerate cs1.setBlockIndexCandidates and cs2.setBlockIndexCandidate and
500 // check contents below.
501 reload_all_block_indexes();
502
503 // The fully validated chain should only have the current validated tip and
504 // the assumed valid base as candidates, blocks 90 and 110. Specifically:
505 //
506 // - It does not have blocks 0-89 because they contain less work than the
507 // chain tip.
508 //
509 // - It has block 90 because it has data and equal work to the chain tip,
510 // (since it is the chain tip).
511 //
512 // - It does not have blocks 91-109 because they do not contain data.
513 //
514 // - It has block 110 even though it does not have data, because
515 // LoadBlockIndex has a special case to always add the snapshot block as a
516 // candidate. The special case is only actually intended to apply to the
517 // snapshot chainstate cs2, not the background chainstate cs1, but it is
518 // written broadly and applies to both.
519 //
520 // - It does not have any blocks after height 110 because cs1 is a background
521 // chainstate, and only blocks where are ancestors of the snapshot block
522 // are added as candidates for the background chainstate.
524 BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(validated_tip), 1);
525 BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(assumed_base), 1);
526
527 // The assumed-valid tolerant chain has the assumed valid base as a
528 // candidate, but otherwise has none of the assumed-valid (which do not
529 // HAVE_DATA) blocks as candidates.
530 //
531 // Specifically:
532 // - All blocks below height 110 are not candidates, because cs2 chain tip
533 // has height 110 and they have less work than it does.
534 //
535 // - Block 110 is a candidate even though it does not have data, because it
536 // is the snapshot block, which is assumed valid.
537 //
538 // - Blocks 111-120 are added because they have data.
539
540 // Check that block 90 is absent
541 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(validated_tip), 0);
542 // Check that block 109 is absent
543 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base->pprev), 0);
544 // Check that block 110 is present
545 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base), 1);
546 // Check that block 120 is present
547 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_tip), 1);
548 // Check that 11 blocks total are present.
549 BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.size(), num_indexes - last_assumed_valid_idx + 1);
550}
551
553BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_init, SnapshotTestSetup)
554{
556 Chainstate& bg_chainstate = chainman.ActiveChainstate();
557
558 this->SetupSnapshot();
559
560 fs::path snapshot_chainstate_dir = *node::FindSnapshotChainstateDir(chainman.m_options.datadir);
561 BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
562 BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
563
564 BOOST_CHECK(chainman.IsSnapshotActive());
565 const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
566 return chainman.ActiveTip()->GetBlockHash());
567
568 auto all_chainstates = chainman.GetAll();
569 BOOST_CHECK_EQUAL(all_chainstates.size(), 2);
570
571 // "Rewind" the background chainstate so that its tip is not at the
572 // base block of the snapshot - this is so after simulating a node restart,
573 // it will initialize instead of attempting to complete validation.
574 //
575 // Note that this is not a realistic use of DisconnectTip().
577 BlockValidationState unused_state;
578 {
579 LOCK2(::cs_main, bg_chainstate.MempoolMutex());
580 BOOST_CHECK(bg_chainstate.DisconnectTip(unused_state, &unused_pool));
581 unused_pool.clear(); // to avoid queuedTx assertion errors on teardown
582 }
583 BOOST_CHECK_EQUAL(bg_chainstate.m_chain.Height(), 109);
584
585 // Test that simulating a shutdown (resetting ChainstateManager) and then performing
586 // chainstate reinitializing successfully cleans up the background-validation
587 // chainstate data, and we end up with a single chainstate that is at tip.
588 ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
589
590 BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
591
592 // This call reinitializes the chainstates.
593 this->LoadVerifyActivateChainstate();
594
595 {
596 LOCK(chainman_restarted.GetMutex());
597 BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 2);
598 BOOST_CHECK(chainman_restarted.IsSnapshotActive());
599 BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
600
601 BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
602 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
603 }
604
605 BOOST_TEST_MESSAGE(
606 "Ensure we can mine blocks on top of the initialized snapshot chainstate");
607 mineBlocks(10);
608 {
609 LOCK(chainman_restarted.GetMutex());
610 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
611
612 // Background chainstate should be unaware of new blocks on the snapshot
613 // chainstate.
614 for (Chainstate* cs : chainman_restarted.GetAll()) {
615 if (cs != &chainman_restarted.ActiveChainstate()) {
616 BOOST_CHECK_EQUAL(cs->m_chain.Height(), 109);
617 }
618 }
619 }
620}
621
622BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion, SnapshotTestSetup)
623{
624 this->SetupSnapshot();
625
627 Chainstate& active_cs = chainman.ActiveChainstate();
628 auto tip_cache_before_complete = active_cs.m_coinstip_cache_size_bytes;
629 auto db_cache_before_complete = active_cs.m_coinsdb_cache_size_bytes;
630
632 m_node.notifications->m_shutdown_on_fatal_error = false;
633
634 fs::path snapshot_chainstate_dir = *node::FindSnapshotChainstateDir(chainman.m_options.datadir);
635 BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
636 BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
637
638 BOOST_CHECK(chainman.IsSnapshotActive());
639 const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
640 return chainman.ActiveTip()->GetBlockHash());
641
642 res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
644
646 BOOST_CHECK(chainman.IsSnapshotActive());
647
648 // Cache should have been rebalanced and reallocated to the "only" remaining
649 // chainstate.
650 BOOST_CHECK(active_cs.m_coinstip_cache_size_bytes > tip_cache_before_complete);
651 BOOST_CHECK(active_cs.m_coinsdb_cache_size_bytes > db_cache_before_complete);
652
653 auto all_chainstates = chainman.GetAll();
654 BOOST_CHECK_EQUAL(all_chainstates.size(), 1);
655 BOOST_CHECK_EQUAL(all_chainstates[0], &active_cs);
656
657 // Trying completion again should return false.
658 res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
660
661 // The invalid snapshot path should not have been used.
662 fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
663 BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
664 // chainstate_snapshot should still exist.
665 BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
666
667 // Test that simulating a shutdown (resetting ChainstateManager) and then performing
668 // chainstate reinitializing successfully cleans up the background-validation
669 // chainstate data, and we end up with a single chainstate that is at tip.
670 ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
671
672 BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
673
674 // This call reinitializes the chainstates, and should clean up the now unnecessary
675 // background-validation leveldb contents.
676 this->LoadVerifyActivateChainstate();
677
678 BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
679 // chainstate_snapshot should now *not* exist.
680 BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
681
682 const Chainstate& active_cs2 = chainman_restarted.ActiveChainstate();
683
684 {
685 LOCK(chainman_restarted.GetMutex());
686 BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 1);
687 BOOST_CHECK(!chainman_restarted.IsSnapshotActive());
688 BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
689 BOOST_CHECK(active_cs2.m_coinstip_cache_size_bytes > tip_cache_before_complete);
690 BOOST_CHECK(active_cs2.m_coinsdb_cache_size_bytes > db_cache_before_complete);
691
692 BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
693 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
694 }
695
696 BOOST_TEST_MESSAGE(
697 "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
698 mineBlocks(10);
699 {
700 LOCK(chainman_restarted.GetMutex());
701 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
702 }
703}
704
705BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion_hash_mismatch, SnapshotTestSetup)
706{
707 auto chainstates = this->SetupSnapshot();
708 Chainstate& validation_chainstate = *std::get<0>(chainstates);
711 m_node.notifications->m_shutdown_on_fatal_error = false;
712
713 // Test tampering with the IBD UTXO set with an extra coin to ensure it causes
714 // snapshot completion to fail.
716 return validation_chainstate.CoinsTip());
717 Coin badcoin;
718 badcoin.out.nValue = InsecureRand32();
719 badcoin.nHeight = 1;
722 ibd_coins.AddCoin(COutPoint(txid, 0), std::move(badcoin), false);
723
724 fs::path snapshot_chainstate_dir = gArgs.GetDataDirNet() / "chainstate_snapshot";
725 BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
726
727 {
728 ASSERT_DEBUG_LOG("failed to validate the -assumeutxo snapshot state");
729 res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
731 }
732
733 auto all_chainstates = chainman.GetAll();
734 BOOST_CHECK_EQUAL(all_chainstates.size(), 1);
735 BOOST_CHECK_EQUAL(all_chainstates[0], &validation_chainstate);
736 BOOST_CHECK_EQUAL(&chainman.ActiveChainstate(), &validation_chainstate);
737
738 fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
739 BOOST_CHECK(fs::exists(snapshot_invalid_dir));
740
741 // Test that simulating a shutdown (resetting ChainstateManager) and then performing
742 // chainstate reinitializing successfully loads only the fully-validated
743 // chainstate data, and we end up with a single chainstate that is at tip.
744 ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
745
746 BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
747
748 // This call reinitializes the chainstates, and should clean up the now unnecessary
749 // background-validation leveldb contents.
750 this->LoadVerifyActivateChainstate();
751
752 BOOST_CHECK(fs::exists(snapshot_invalid_dir));
753 BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
754
755 {
757 BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 1);
758 BOOST_CHECK(!chainman_restarted.IsSnapshotActive());
759 BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
760 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
761 }
762
763 BOOST_TEST_MESSAGE(
764 "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
765 mineBlocks(10);
766 {
768 BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
769 }
770}
771
ArgsManager gArgs
Definition args.cpp:41
node::NodeContext m_node
@ 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:77
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition args.h:232
Non-refcounted RAII wrapper for FILE*.
Definition streams.h:389
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
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition coins.h:360
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
Definition coins.cpp:70
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
Definition coins.cpp:293
void SetBestBlock(const uint256 &hashBlock)
Definition coins.cpp:181
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition coins.cpp:165
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition transaction.h:29
CScript scriptPubKey
CAmount nValue
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition validation.h:513
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:607
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition validation.h:593
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
Definition validation.h:657
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition validation.h:619
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:654
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:772
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition validation.h:871
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...
int64_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
bool IsSnapshotActive() const
const Options m_options
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())
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
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.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition fs.h:33
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
kernel::BlockManagerOpts Options
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
uint256 m_base_blockhash
The hash of the block that reflects the tip of the chain for the UTXO set contained in this snapshot.
uint64_t m_coins_count
The number of coins in the UTXO set contained in this snapshot.
void assign(size_type n, const T &val)
Definition prevector.h:225
static transaction_identifier FromUint256(const uint256 &id)
256-bit opaque blob.
Definition uint256.h:178
static const uint256 ONE
Definition uint256.h:186
static const uint256 ZERO
Definition uint256.h:185
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition cs_main.cpp:8
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
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.
#define BOOST_CHECK_EQUAL(v1, v2)
Definition object.cpp:18
#define BOOST_CHECK(expr)
Definition object.cpp:17
std::shared_ptr< const CTransaction > CTransactionRef
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition serialize.h:337
std::tuple< Chainstate *, Chainstate * > SetupSnapshot()
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Testing setup that configures a complete environment.
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:85
std::unique_ptr< ChainstateManager > chainman
Definition context.h:69
std::unique_ptr< node::Warnings > warnings
Manages all the node warnings.
Definition context.h:88
std::unique_ptr< KernelNotifications > notifications
Issues blocking calls about sync status, errors and warnings.
Definition context.h:83
std::atomic< int > exit_status
Definition context.h:86
util::SignalInterrupt * shutdown
Interrupt object used to track whether node shutdown was requested.
Definition context.h:62
#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
static uint256 InsecureRand256()
Definition random.h:35
static uint64_t InsecureRandBits(int bits)
Definition random.h:40
static uint32_t InsecureRand32()
Definition random.h:30
bilingual_str _(ConstevalStringLiteral str)
Translation function.
Definition translation.h:80
BOOST_CHECK_EQUAL_COLLECTIONS(R1L.begin(), R1L.end(), R1Array, R1Array+uint256::size())
SnapshotCompletionResult
Definition validation.h:822
BOOST_FIXTURE_TEST_CASE(chainstatemanager, TestChain100Setup)
Basic tests for ChainstateManager.