Bitcoin Core  27.1.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>
6 #include <consensus/validation.h>
9 #include <node/utxo_snapshot.h>
10 #include <random.h>
11 #include <rpc/blockchain.h>
12 #include <sync.h>
13 #include <test/util/chainstate.h>
14 #include <test/util/logging.h>
15 #include <test/util/random.h>
16 #include <test/util/setup_common.h>
17 #include <test/util/validation.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 
28 using node::BlockManager;
31 
32 BOOST_FIXTURE_TEST_SUITE(validation_chainstatemanager_tests, TestingSetup)
33 
38 {
39  ChainstateManager& manager = *m_node.chainman;
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());
50  BOOST_CHECK(WITH_LOCK(::cs_main, return !manager.IsSnapshotValidated()));
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  {
74  LOCK(::cs_main);
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);
84  BOOST_CHECK(manager.IsSnapshotActive());
85  BOOST_CHECK(WITH_LOCK(::cs_main, return !manager.IsSnapshotValidated()));
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
106 }
107 
109 BOOST_FIXTURE_TEST_CASE(chainstatemanager_rebalance_caches, TestChain100Setup)
110 {
111  ChainstateManager& manager = *m_node.chainman;
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  {
124  LOCK(::cs_main);
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  {
150  LOCK(::cs_main);
151  c2.InitCoinsCache(1 << 23);
152  manager.MaybeRebalanceCaches();
153  }
154 
155  BOOST_CHECK_CLOSE(c1.m_coinstip_cache_size_bytes, max_cache * 0.05, 1);
156  BOOST_CHECK_CLOSE(c1.m_coinsdb_cache_size_bytes, max_cache * 0.05, 1);
157  BOOST_CHECK_CLOSE(c2.m_coinstip_cache_size_bytes, max_cache * 0.95, 1);
158  BOOST_CHECK_CLOSE(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  std::tuple<Chainstate*, Chainstate*> SetupSnapshot()
178  {
179  ChainstateManager& chainman = *Assert(m_node.chainman);
180 
181  BOOST_CHECK(!chainman.IsSnapshotActive());
182 
183  {
184  LOCK(::cs_main);
185  BOOST_CHECK(!chainman.IsSnapshotValidated());
187  }
188 
189  size_t initial_size;
190  size_t initial_total_coins{100};
191 
192  // Make some initial assertions about the contents of the chainstate.
193  {
194  LOCK(::cs_main);
195  CCoinsViewCache& ibd_coinscache = chainman.ActiveChainstate().CoinsTip();
196  initial_size = ibd_coinscache.GetCacheSize();
197  size_t total_coins{0};
198 
199  for (CTransactionRef& txn : m_coinbase_txns) {
200  COutPoint op{txn->GetHash(), 0};
201  BOOST_CHECK(ibd_coinscache.HaveCoin(op));
202  total_coins++;
203  }
204 
205  BOOST_CHECK_EQUAL(total_coins, initial_total_coins);
206  BOOST_CHECK_EQUAL(initial_size, initial_total_coins);
207  }
208 
209  Chainstate& validation_chainstate = chainman.ActiveChainstate();
210 
211  // Snapshot should refuse to load at this height.
212  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(this));
213  BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
214  BOOST_CHECK(!chainman.SnapshotBlockhash());
215 
216  // Mine 10 more blocks, putting at us height 110 where a valid assumeutxo value can
217  // be found.
218  constexpr int snapshot_height = 110;
219  mineBlocks(10);
220  initial_size += 10;
221  initial_total_coins += 10;
222 
223  // Should not load malleated snapshots
224  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
225  this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
226  // A UTXO is missing but count is correct
227  metadata.m_coins_count -= 1;
228 
229  COutPoint outpoint;
230  Coin coin;
231 
232  auto_infile >> outpoint;
233  auto_infile >> coin;
234  }));
235 
237 
238  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
239  this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
240  // Coins count is larger than coins in file
241  metadata.m_coins_count += 1;
242  }));
243  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
244  this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
245  // Coins count is smaller than coins in file
246  metadata.m_coins_count -= 1;
247  }));
248  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
249  this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
250  // Wrong hash
251  metadata.m_base_blockhash = uint256::ZERO;
252  }));
253  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
254  this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
255  // Wrong hash
256  metadata.m_base_blockhash = uint256::ONE;
257  }));
258 
259  BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(this));
261 
262  // Ensure our active chain is the snapshot chainstate.
263  BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
265  *chainman.ActiveChainstate().m_from_snapshot_blockhash,
266  *chainman.SnapshotBlockhash());
267 
268  Chainstate& snapshot_chainstate = chainman.ActiveChainstate();
269 
270  {
271  LOCK(::cs_main);
272 
274 
275  // Note: WriteSnapshotBaseBlockhash() is implicitly tested above.
278  *chainman.SnapshotBlockhash());
279 
280  // Ensure that the genesis block was not marked assumed-valid.
281  BOOST_CHECK(!chainman.ActiveChain().Genesis()->IsAssumedValid());
282  }
283 
284  const auto& au_data = ::Params().AssumeutxoForHeight(snapshot_height);
285  const CBlockIndex* tip = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip());
286 
287  BOOST_CHECK_EQUAL(tip->nChainTx, au_data->nChainTx);
288 
289  // To be checked against later when we try loading a subsequent snapshot.
290  uint256 loaded_snapshot_blockhash{*chainman.SnapshotBlockhash()};
291 
292  // Make some assertions about the both chainstates. These checks ensure the
293  // legacy chainstate hasn't changed and that the newly created chainstate
294  // reflects the expected content.
295  {
296  LOCK(::cs_main);
297  int chains_tested{0};
298 
299  for (Chainstate* chainstate : chainman.GetAll()) {
300  BOOST_TEST_MESSAGE("Checking coins in " << chainstate->ToString());
301  CCoinsViewCache& coinscache = chainstate->CoinsTip();
302 
303  // Both caches will be empty initially.
304  BOOST_CHECK_EQUAL((unsigned int)0, coinscache.GetCacheSize());
305 
306  size_t total_coins{0};
307 
308  for (CTransactionRef& txn : m_coinbase_txns) {
309  COutPoint op{txn->GetHash(), 0};
310  BOOST_CHECK(coinscache.HaveCoin(op));
311  total_coins++;
312  }
313 
314  BOOST_CHECK_EQUAL(initial_size , coinscache.GetCacheSize());
315  BOOST_CHECK_EQUAL(total_coins, initial_total_coins);
316  chains_tested++;
317  }
318 
319  BOOST_CHECK_EQUAL(chains_tested, 2);
320  }
321 
322  // Mine some new blocks on top of the activated snapshot chainstate.
323  constexpr size_t new_coins{100};
324  mineBlocks(new_coins); // Defined in TestChain100Setup.
325 
326  {
327  LOCK(::cs_main);
328  size_t coins_in_active{0};
329  size_t coins_in_background{0};
330  size_t coins_missing_from_background{0};
331 
332  for (Chainstate* chainstate : chainman.GetAll()) {
333  BOOST_TEST_MESSAGE("Checking coins in " << chainstate->ToString());
334  CCoinsViewCache& coinscache = chainstate->CoinsTip();
335  bool is_background = chainstate != &chainman.ActiveChainstate();
336 
337  for (CTransactionRef& txn : m_coinbase_txns) {
338  COutPoint op{txn->GetHash(), 0};
339  if (coinscache.HaveCoin(op)) {
340  (is_background ? coins_in_background : coins_in_active)++;
341  } else if (is_background) {
342  coins_missing_from_background++;
343  }
344  }
345  }
346 
347  BOOST_CHECK_EQUAL(coins_in_active, initial_total_coins + new_coins);
348  BOOST_CHECK_EQUAL(coins_in_background, initial_total_coins);
349  BOOST_CHECK_EQUAL(coins_missing_from_background, new_coins);
350  }
351 
352  // Snapshot should refuse to load after one has already loaded.
353  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(this));
354 
355  // Snapshot blockhash should be unchanged.
357  *chainman.ActiveChainstate().m_from_snapshot_blockhash,
358  loaded_snapshot_blockhash);
359  return std::make_tuple(&validation_chainstate, &snapshot_chainstate);
360  }
361 
362  // Simulate a restart of the node by flushing all state to disk, clearing the
363  // existing ChainstateManager, and unloading the block index.
364  //
365  // @returns a reference to the "restarted" ChainstateManager
367  {
368  ChainstateManager& chainman = *Assert(m_node.chainman);
369 
370  BOOST_TEST_MESSAGE("Simulating node restart");
371  {
372  for (Chainstate* cs : chainman.GetAll()) {
373  LOCK(::cs_main);
374  cs->ForceFlushStateToDisk();
375  }
376  // Process all callbacks referring to the old manager before wiping it.
378  LOCK(::cs_main);
379  chainman.ResetChainstates();
380  BOOST_CHECK_EQUAL(chainman.GetAll().size(), 0);
381  m_node.notifications = std::make_unique<KernelNotifications>(*Assert(m_node.shutdown), m_node.exit_status);
382  const ChainstateManager::Options chainman_opts{
383  .chainparams = ::Params(),
384  .datadir = chainman.m_options.datadir,
385  .notifications = *m_node.notifications,
386  };
387  const BlockManager::Options blockman_opts{
388  .chainparams = chainman_opts.chainparams,
389  .blocks_dir = m_args.GetBlocksDirPath(),
390  .notifications = chainman_opts.notifications,
391  };
392  // For robustness, ensure the old manager is destroyed before creating a
393  // new one.
394  m_node.chainman.reset();
395  m_node.chainman = std::make_unique<ChainstateManager>(*Assert(m_node.shutdown), chainman_opts, blockman_opts);
396  }
397  return *Assert(m_node.chainman);
398  }
399 };
400 
402 BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, SnapshotTestSetup)
403 {
404  this->SetupSnapshot();
405 }
406 
417 BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
418 {
419  ChainstateManager& chainman = *Assert(m_node.chainman);
420  Chainstate& cs1 = chainman.ActiveChainstate();
421 
422  int num_indexes{0};
423  int num_assumed_valid{0};
424  // Blocks in range [assumed_valid_start_idx, last_assumed_valid_idx) will be
425  // marked as assumed-valid and not having data.
426  const int expected_assumed_valid{20};
427  const int last_assumed_valid_idx{111};
428  const int assumed_valid_start_idx = last_assumed_valid_idx - expected_assumed_valid;
429 
430  // Mine to height 120, past the hardcoded regtest assumeutxo snapshot at
431  // height 110
432  mineBlocks(20);
433 
434  CBlockIndex* validated_tip{nullptr};
435  CBlockIndex* assumed_base{nullptr};
436  CBlockIndex* assumed_tip{WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip())};
437  BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120);
438 
439  auto reload_all_block_indexes = [&]() {
440  // For completeness, we also reset the block sequence counters to
441  // ensure that no state which affects the ranking of tip-candidates is
442  // retained (even though this isn't strictly necessary).
443  WITH_LOCK(::cs_main, return chainman.ResetBlockSequenceCounters());
444  for (Chainstate* cs : chainman.GetAll()) {
445  LOCK(::cs_main);
446  cs->ClearBlockIndexCandidates();
447  BOOST_CHECK(cs->setBlockIndexCandidates.empty());
448  }
449 
450  WITH_LOCK(::cs_main, chainman.LoadBlockIndex());
451  };
452 
453  // Ensure that without any assumed-valid BlockIndex entries, only the current tip is
454  // considered as a candidate.
455  reload_all_block_indexes();
457 
458  // Mark some region of the chain assumed-valid, and remove the HAVE_DATA flag.
459  for (int i = 0; i <= cs1.m_chain.Height(); ++i) {
460  LOCK(::cs_main);
461  auto index = cs1.m_chain[i];
462 
463  // Blocks with heights in range [91, 110] are marked ASSUMED_VALID
464  if (i < last_assumed_valid_idx && i >= assumed_valid_start_idx) {
466  }
467 
468  ++num_indexes;
469  if (index->IsAssumedValid()) ++num_assumed_valid;
470 
471  // Note the last fully-validated block as the expected validated tip.
472  if (i == (assumed_valid_start_idx - 1)) {
473  validated_tip = index;
474  BOOST_CHECK(!index->IsAssumedValid());
475  }
476  // Note the last assumed valid block as the snapshot base
477  if (i == last_assumed_valid_idx - 1) {
478  assumed_base = index;
479  BOOST_CHECK(index->IsAssumedValid());
480  } else if (i == last_assumed_valid_idx) {
481  BOOST_CHECK(!index->IsAssumedValid());
482  }
483  }
484 
485  BOOST_CHECK_EQUAL(expected_assumed_valid, num_assumed_valid);
486 
487  // Note: cs2's tip is not set when ActivateExistingSnapshot is called.
488  Chainstate& cs2 = WITH_LOCK(::cs_main,
489  return chainman.ActivateExistingSnapshot(*assumed_base->phashBlock));
490 
491  // Set tip of the fully validated chain to be the validated tip
492  cs1.m_chain.SetTip(*validated_tip);
493 
494  // Set tip of the assume-valid-based chain to the assume-valid block
495  cs2.m_chain.SetTip(*assumed_base);
496 
497  // Sanity check test variables.
498  BOOST_CHECK_EQUAL(num_indexes, 121); // 121 total blocks, including genesis
499  BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120); // original chain has height 120
500  BOOST_CHECK_EQUAL(validated_tip->nHeight, 90); // current cs1 chain has height 90
501  BOOST_CHECK_EQUAL(assumed_base->nHeight, 110); // current cs2 chain has height 110
502 
503  // Regenerate cs1.setBlockIndexCandidates and cs2.setBlockIndexCandidate and
504  // check contents below.
505  reload_all_block_indexes();
506 
507  // The fully validated chain should only have the current validated tip and
508  // the assumed valid base as candidates, blocks 90 and 110. Specifically:
509  //
510  // - It does not have blocks 0-89 because they contain less work than the
511  // chain tip.
512  //
513  // - It has block 90 because it has data and equal work to the chain tip,
514  // (since it is the chain tip).
515  //
516  // - It does not have blocks 91-109 because they do not contain data.
517  //
518  // - It has block 110 even though it does not have data, because
519  // LoadBlockIndex has a special case to always add the snapshot block as a
520  // candidate. The special case is only actually intended to apply to the
521  // snapshot chainstate cs2, not the background chainstate cs1, but it is
522  // written broadly and applies to both.
523  //
524  // - It does not have any blocks after height 110 because cs1 is a background
525  // chainstate, and only blocks where are ancestors of the snapshot block
526  // are added as candidates for the background chainstate.
528  BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(validated_tip), 1);
529  BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(assumed_base), 1);
530 
531  // The assumed-valid tolerant chain has the assumed valid base as a
532  // candidate, but otherwise has none of the assumed-valid (which do not
533  // HAVE_DATA) blocks as candidates.
534  //
535  // Specifically:
536  // - All blocks below height 110 are not candidates, because cs2 chain tip
537  // has height 110 and they have less work than it does.
538  //
539  // - Block 110 is a candidate even though it does not have data, because it
540  // is the snapshot block, which is assumed valid.
541  //
542  // - Blocks 111-120 are added because they have data.
543 
544  // Check that block 90 is absent
545  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(validated_tip), 0);
546  // Check that block 109 is absent
547  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base->pprev), 0);
548  // Check that block 110 is present
549  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base), 1);
550  // Check that block 120 is present
551  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_tip), 1);
552  // Check that 11 blocks total are present.
553  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.size(), num_indexes - last_assumed_valid_idx + 1);
554 }
555 
557 BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_init, SnapshotTestSetup)
558 {
559  ChainstateManager& chainman = *Assert(m_node.chainman);
560  Chainstate& bg_chainstate = chainman.ActiveChainstate();
561 
562  this->SetupSnapshot();
563 
564  fs::path snapshot_chainstate_dir = *node::FindSnapshotChainstateDir(chainman.m_options.datadir);
565  BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
566  BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
567 
568  BOOST_CHECK(chainman.IsSnapshotActive());
569  const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
570  return chainman.ActiveTip()->GetBlockHash());
571 
572  auto all_chainstates = chainman.GetAll();
573  BOOST_CHECK_EQUAL(all_chainstates.size(), 2);
574 
575  // "Rewind" the background chainstate so that its tip is not at the
576  // base block of the snapshot - this is so after simulating a node restart,
577  // it will initialize instead of attempting to complete validation.
578  //
579  // Note that this is not a realistic use of DisconnectTip().
581  BlockValidationState unused_state;
582  {
583  LOCK2(::cs_main, bg_chainstate.MempoolMutex());
584  BOOST_CHECK(bg_chainstate.DisconnectTip(unused_state, &unused_pool));
585  unused_pool.clear(); // to avoid queuedTx assertion errors on teardown
586  }
587  BOOST_CHECK_EQUAL(bg_chainstate.m_chain.Height(), 109);
588 
589  // Test that simulating a shutdown (resetting ChainstateManager) and then performing
590  // chainstate reinitializing successfully cleans up the background-validation
591  // chainstate data, and we end up with a single chainstate that is at tip.
592  ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
593 
594  BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
595 
596  // This call reinitializes the chainstates.
597  this->LoadVerifyActivateChainstate();
598 
599  {
600  LOCK(chainman_restarted.GetMutex());
601  BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 2);
602  BOOST_CHECK(chainman_restarted.IsSnapshotActive());
603  BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
604 
605  BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
606  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
607  }
608 
609  BOOST_TEST_MESSAGE(
610  "Ensure we can mine blocks on top of the initialized snapshot chainstate");
611  mineBlocks(10);
612  {
613  LOCK(chainman_restarted.GetMutex());
614  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
615 
616  // Background chainstate should be unaware of new blocks on the snapshot
617  // chainstate.
618  for (Chainstate* cs : chainman_restarted.GetAll()) {
619  if (cs != &chainman_restarted.ActiveChainstate()) {
620  BOOST_CHECK_EQUAL(cs->m_chain.Height(), 109);
621  }
622  }
623  }
624 }
625 
626 BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion, SnapshotTestSetup)
627 {
628  this->SetupSnapshot();
629 
630  ChainstateManager& chainman = *Assert(m_node.chainman);
631  Chainstate& active_cs = chainman.ActiveChainstate();
632  auto tip_cache_before_complete = active_cs.m_coinstip_cache_size_bytes;
633  auto db_cache_before_complete = active_cs.m_coinsdb_cache_size_bytes;
634 
636  m_node.notifications->m_shutdown_on_fatal_error = false;
637 
638  fs::path snapshot_chainstate_dir = *node::FindSnapshotChainstateDir(chainman.m_options.datadir);
639  BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
640  BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
641 
642  BOOST_CHECK(chainman.IsSnapshotActive());
643  const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
644  return chainman.ActiveTip()->GetBlockHash());
645 
646  res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
648 
650  BOOST_CHECK(chainman.IsSnapshotActive());
651 
652  // Cache should have been rebalanced and reallocated to the "only" remaining
653  // chainstate.
654  BOOST_CHECK(active_cs.m_coinstip_cache_size_bytes > tip_cache_before_complete);
655  BOOST_CHECK(active_cs.m_coinsdb_cache_size_bytes > db_cache_before_complete);
656 
657  auto all_chainstates = chainman.GetAll();
658  BOOST_CHECK_EQUAL(all_chainstates.size(), 1);
659  BOOST_CHECK_EQUAL(all_chainstates[0], &active_cs);
660 
661  // Trying completion again should return false.
662  res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
664 
665  // The invalid snapshot path should not have been used.
666  fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
667  BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
668  // chainstate_snapshot should still exist.
669  BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
670 
671  // Test that simulating a shutdown (resetting ChainstateManager) and then performing
672  // chainstate reinitializing successfully cleans up the background-validation
673  // chainstate data, and we end up with a single chainstate that is at tip.
674  ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
675 
676  BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
677 
678  // This call reinitializes the chainstates, and should clean up the now unnecessary
679  // background-validation leveldb contents.
680  this->LoadVerifyActivateChainstate();
681 
682  BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
683  // chainstate_snapshot should now *not* exist.
684  BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
685 
686  const Chainstate& active_cs2 = chainman_restarted.ActiveChainstate();
687 
688  {
689  LOCK(chainman_restarted.GetMutex());
690  BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 1);
691  BOOST_CHECK(!chainman_restarted.IsSnapshotActive());
692  BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
693  BOOST_CHECK(active_cs2.m_coinstip_cache_size_bytes > tip_cache_before_complete);
694  BOOST_CHECK(active_cs2.m_coinsdb_cache_size_bytes > db_cache_before_complete);
695 
696  BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
697  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
698  }
699 
700  BOOST_TEST_MESSAGE(
701  "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
702  mineBlocks(10);
703  {
704  LOCK(chainman_restarted.GetMutex());
705  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
706  }
707 }
708 
709 BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion_hash_mismatch, SnapshotTestSetup)
710 {
711  auto chainstates = this->SetupSnapshot();
712  Chainstate& validation_chainstate = *std::get<0>(chainstates);
713  ChainstateManager& chainman = *Assert(m_node.chainman);
715  m_node.notifications->m_shutdown_on_fatal_error = false;
716 
717  // Test tampering with the IBD UTXO set with an extra coin to ensure it causes
718  // snapshot completion to fail.
719  CCoinsViewCache& ibd_coins = WITH_LOCK(::cs_main,
720  return validation_chainstate.CoinsTip());
721  Coin badcoin;
722  badcoin.out.nValue = InsecureRand32();
723  badcoin.nHeight = 1;
724  badcoin.out.scriptPubKey.assign(InsecureRandBits(6), 0);
726  ibd_coins.AddCoin(COutPoint(txid, 0), std::move(badcoin), false);
727 
728  fs::path snapshot_chainstate_dir = gArgs.GetDataDirNet() / "chainstate_snapshot";
729  BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
730 
731  {
732  ASSERT_DEBUG_LOG("failed to validate the -assumeutxo snapshot state");
733  res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
735  }
736 
737  auto all_chainstates = chainman.GetAll();
738  BOOST_CHECK_EQUAL(all_chainstates.size(), 1);
739  BOOST_CHECK_EQUAL(all_chainstates[0], &validation_chainstate);
740  BOOST_CHECK_EQUAL(&chainman.ActiveChainstate(), &validation_chainstate);
741 
742  fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
743  BOOST_CHECK(fs::exists(snapshot_invalid_dir));
744 
745  // Test that simulating a shutdown (resetting ChainstateManager) and then performing
746  // chainstate reinitializing successfully loads only the fully-validated
747  // chainstate data, and we end up with a single chainstate that is at tip.
748  ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
749 
750  BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
751 
752  // This call reinitializes the chainstates, and should clean up the now unnecessary
753  // background-validation leveldb contents.
754  this->LoadVerifyActivateChainstate();
755 
756  BOOST_CHECK(fs::exists(snapshot_invalid_dir));
757  BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
758 
759  {
760  LOCK(::cs_main);
761  BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 1);
762  BOOST_CHECK(!chainman_restarted.IsSnapshotActive());
763  BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
764  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
765  }
766 
767  BOOST_TEST_MESSAGE(
768  "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
769  mineBlocks(10);
770  {
771  LOCK(::cs_main);
772  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
773  }
774 }
775 
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:594
CAmount nValue
Definition: transaction.h:152
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:963
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
static const uint256 ONE
Definition: uint256.h:112
const Options m_options
Definition: validation.h:959
A UTXO entry.
Definition: coins.h:31
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:846
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
static void pool cs
ArgsManager m_args
Definition: setup_common.h:57
std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir)
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
Definition: validation.h:629
std::atomic< int > exit_status
Definition: context.h:74
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1066
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
Definition: chain.h:97
util::SignalInterrupt * shutdown
Interrupt object used to track whether node shutdown was requested.
Definition: context.h:54
std::tuple< Chainstate *, Chainstate * > SetupSnapshot()
int Height() const
Return the maximal height in the chain.
Definition: chain.h:492
CTxOut out
unspent transaction output
Definition: coins.h:35
std::vector< CTransactionRef > m_coinbase_txns
Definition: setup_common.h:207
An options struct for ChainstateManager, more ergonomically referred to as ChainstateManager::Options...
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:457
std::optional< fs::path > FindSnapshotChainstateDir(const fs::path &data_dir)
Return a path to the snapshot-based chainstate dir, if one exists.
uint64_t m_coins_count
The number of coins in the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:33
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:569
static const unsigned int MAX_DISCONNECTED_TX_POOL_BYTES
Maximum bytes for transactions to store for processing during reorg.
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:388
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
Definition: chain.cpp:21
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block. ...
Definition: chain.h:191
void SetBestBlock(const uint256 &hashBlock)
Definition: coins.cpp:177
uint256 GetBlockHash() const
Definition: chain.h:258
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
Definition: coins.cpp:298
#define LOCK2(cs1, cs2)
Definition: sync.h:258
bool DisconnectTip(BlockValidationState &state, DisconnectedBlockTransactions *disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Disconnect m_chain&#39;s tip.
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:232
bool IsSnapshotActive() const
Chainstate stores and provides an API to update our local knowledge of the current best chain...
Definition: validation.h:488
BOOST_FIXTURE_TEST_CASE(chainstatemanager, TestChain100Setup)
Basic tests for ChainstateManager.
#define LOCK(cs)
Definition: sync.h:257
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:74
fs::path GetBlocksDirPath() const
Get blocks directory path.
Definition: args.cpp:280
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we&#39;re running with -reindex...
SnapshotCompletionResult
Definition: validation.h:798
BOOST_AUTO_TEST_SUITE_END()
static const uint256 ZERO
Definition: uint256.h:111
void mineBlocks(int num_blocks)
Mine a series of new blocks on the active chain.
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1068
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
Definition: blockstorage.h:136
bool LoadChainTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Update the chain tip based on database information, i.e.
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
Definition: validation.h:956
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:99
#define ASSERT_DEBUG_LOG(message)
Definition: logging.h:39
static uint256 InsecureRand256()
Definition: random.h:50
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:28
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
DisconnectedBlockTransactions.
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
Definition: validation.h:1034
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:301
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
Definition: coins.cpp:69
int64_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
Definition: validation.h:1021
ArgsManager gArgs
Definition: args.cpp:41
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:583
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:29
256-bit opaque blob.
Definition: uint256.h:106
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:149
const CChainParams & Params()
Return the currently selected parameters.
static transaction_identifier FromUint256(const uint256 &id)
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
std::unique_ptr< KernelNotifications > notifications
Definition: context.h:73
static uint32_t InsecureRand32()
Definition: random.h:45
void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:987
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:463
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1067
bool IsAssumedValid() const EXCLUSIVE_LOCKS_REQUIRED(
Definition: chain.h:323
static uint64_t InsecureRandBits(int bits)
Definition: random.h:55
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
Definition: validation.h:632
std::optional< uint256 > SnapshotBlockhash() const
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
Definition: validation.h:1025
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:228
static bool exists(const path &p)
Definition: fs.h:89
std::optional< AssumeutxoData > AssumeutxoForHeight(int height) const
Definition: chainparams.h:122
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:32
If ASSUMED_VALID is set, it means that this block has not been validated and has validity status less...
Definition: chain.h:141
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
node::NodeContext m_node
Definition: setup_common.h:51
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:1065
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:61
Testing setup that configures a complete environment.
Definition: setup_common.h:78
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
Definition: validation.h:1098
#define Assert(val)
Identity function.
Definition: check.h:77
#define BOOST_CHECK(expr)
Definition: object.cpp:17
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:24
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: coins.cpp:161