Bitcoin Core  26.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 <timedata.h>
19 #include <uint256.h>
20 #include <validation.h>
21 #include <validationinterface.h>
22 
23 #include <tinyformat.h>
24 
25 #include <vector>
26 
27 #include <boost/test/unit_test.hpp>
28 
29 using node::BlockManager;
32 
33 BOOST_FIXTURE_TEST_SUITE(validation_chainstatemanager_tests, TestingSetup)
34 
39 {
40  ChainstateManager& manager = *m_node.chainman;
41  std::vector<Chainstate*> chainstates;
42 
43  BOOST_CHECK(!manager.SnapshotBlockhash().has_value());
44 
45  // Create a legacy (IBD) chainstate.
46  //
47  Chainstate& c1 = manager.ActiveChainstate();
48  chainstates.push_back(&c1);
49 
50  BOOST_CHECK(!manager.IsSnapshotActive());
51  BOOST_CHECK(WITH_LOCK(::cs_main, return !manager.IsSnapshotValidated()));
52  auto all = manager.GetAll();
53  BOOST_CHECK_EQUAL_COLLECTIONS(all.begin(), all.end(), chainstates.begin(), chainstates.end());
54 
55  auto& active_chain = WITH_LOCK(manager.GetMutex(), return manager.ActiveChain());
56  BOOST_CHECK_EQUAL(&active_chain, &c1.m_chain);
57 
58  // Get to a valid assumeutxo tip (per chainparams);
59  mineBlocks(10);
60  BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 110);
61  auto active_tip = WITH_LOCK(manager.GetMutex(), return manager.ActiveTip());
62  auto exp_tip = c1.m_chain.Tip();
63  BOOST_CHECK_EQUAL(active_tip, exp_tip);
64 
65  BOOST_CHECK(!manager.SnapshotBlockhash().has_value());
66 
67  // Create a snapshot-based chainstate.
68  //
69  const uint256 snapshot_blockhash = active_tip->GetBlockHash();
70  Chainstate& c2 = WITH_LOCK(::cs_main, return manager.ActivateExistingSnapshot(snapshot_blockhash));
71  chainstates.push_back(&c2);
72  c2.InitCoinsDB(
73  /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
74  {
75  LOCK(::cs_main);
76  c2.InitCoinsCache(1 << 23);
77  c2.CoinsTip().SetBestBlock(active_tip->GetBlockHash());
78  c2.setBlockIndexCandidates.insert(manager.m_blockman.LookupBlockIndex(active_tip->GetBlockHash()));
79  c2.LoadChainTip();
80  }
82  BOOST_CHECK(c2.ActivateBestChain(_, nullptr));
83 
84  BOOST_CHECK_EQUAL(manager.SnapshotBlockhash().value(), snapshot_blockhash);
85  BOOST_CHECK(manager.IsSnapshotActive());
86  BOOST_CHECK(WITH_LOCK(::cs_main, return !manager.IsSnapshotValidated()));
87  BOOST_CHECK_EQUAL(&c2, &manager.ActiveChainstate());
88  BOOST_CHECK(&c1 != &manager.ActiveChainstate());
89  auto all2 = manager.GetAll();
90  BOOST_CHECK_EQUAL_COLLECTIONS(all2.begin(), all2.end(), chainstates.begin(), chainstates.end());
91 
92  auto& active_chain2 = WITH_LOCK(manager.GetMutex(), return manager.ActiveChain());
93  BOOST_CHECK_EQUAL(&active_chain2, &c2.m_chain);
94 
95  BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 110);
96  mineBlocks(1);
97  BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 111);
98  BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return c1.m_chain.Height()), 110);
99 
100  auto active_tip2 = WITH_LOCK(manager.GetMutex(), return manager.ActiveTip());
101  BOOST_CHECK_EQUAL(active_tip, active_tip2->pprev);
102  BOOST_CHECK_EQUAL(active_tip, c1.m_chain.Tip());
103  BOOST_CHECK_EQUAL(active_tip2, c2.m_chain.Tip());
104 
105  // Let scheduler events finish running to avoid accessing memory that is going to be unloaded
107 }
108 
110 BOOST_FIXTURE_TEST_CASE(chainstatemanager_rebalance_caches, TestChain100Setup)
111 {
112  ChainstateManager& manager = *m_node.chainman;
113 
114  size_t max_cache = 10000;
115  manager.m_total_coinsdb_cache = max_cache;
116  manager.m_total_coinstip_cache = max_cache;
117 
118  std::vector<Chainstate*> chainstates;
119 
120  // Create a legacy (IBD) chainstate.
121  //
122  Chainstate& c1 = manager.ActiveChainstate();
123  chainstates.push_back(&c1);
124  {
125  LOCK(::cs_main);
126  c1.InitCoinsCache(1 << 23);
127  manager.MaybeRebalanceCaches();
128  }
129 
132 
133  // Create a snapshot-based chainstate.
134  //
135  CBlockIndex* snapshot_base{WITH_LOCK(manager.GetMutex(), return manager.ActiveChain()[manager.ActiveChain().Height() / 2])};
136  Chainstate& c2 = WITH_LOCK(cs_main, return manager.ActivateExistingSnapshot(*snapshot_base->phashBlock));
137  chainstates.push_back(&c2);
138  c2.InitCoinsDB(
139  /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
140 
141  // Reset IBD state so IsInitialBlockDownload() returns true and causes
142  // MaybeRebalancesCaches() to prioritize the snapshot chainstate, giving it
143  // more cache space than the snapshot chainstate. Calling ResetIbd() is
144  // necessary because m_cached_finished_ibd is already latched to true before
145  // the test starts due to the test setup. After ResetIbd() is called.
146  // IsInitialBlockDownload will return true because at this point the active
147  // chainstate has a null chain tip.
148  static_cast<TestChainstateManager&>(manager).ResetIbd();
149 
150  {
151  LOCK(::cs_main);
152  c2.InitCoinsCache(1 << 23);
153  manager.MaybeRebalanceCaches();
154  }
155 
156  BOOST_CHECK_CLOSE(c1.m_coinstip_cache_size_bytes, max_cache * 0.05, 1);
157  BOOST_CHECK_CLOSE(c1.m_coinsdb_cache_size_bytes, max_cache * 0.05, 1);
158  BOOST_CHECK_CLOSE(c2.m_coinstip_cache_size_bytes, max_cache * 0.95, 1);
159  BOOST_CHECK_CLOSE(c2.m_coinsdb_cache_size_bytes, max_cache * 0.95, 1);
160 }
161 
163  // Run with coinsdb on the filesystem to support, e.g., moving invalidated
164  // chainstate dirs to "*_invalid".
165  //
166  // Note that this means the tests run considerably slower than in-memory DB
167  // tests, but we can't otherwise test this functionality since it relies on
168  // destructive filesystem operations.
170  {},
171  {},
172  /*coins_db_in_memory=*/false,
173  /*block_tree_db_in_memory=*/false,
174  }
175  {
176  }
177 
178  std::tuple<Chainstate*, Chainstate*> SetupSnapshot()
179  {
180  ChainstateManager& chainman = *Assert(m_node.chainman);
181 
182  BOOST_CHECK(!chainman.IsSnapshotActive());
183 
184  {
185  LOCK(::cs_main);
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  {
195  LOCK(::cs_main);
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  COutPoint outpoint;
231  Coin coin;
232 
233  auto_infile >> outpoint;
234  auto_infile >> coin;
235  }));
236 
238 
239  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
240  this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
241  // Coins count is larger than coins in file
242  metadata.m_coins_count += 1;
243  }));
244  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
245  this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
246  // Coins count is smaller than coins in file
247  metadata.m_coins_count -= 1;
248  }));
249  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
250  this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
251  // Wrong hash
252  metadata.m_base_blockhash = uint256::ZERO;
253  }));
254  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
255  this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
256  // Wrong hash
257  metadata.m_base_blockhash = uint256::ONE;
258  }));
259 
260  BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(this));
262 
263  // Ensure our active chain is the snapshot chainstate.
264  BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
266  *chainman.ActiveChainstate().m_from_snapshot_blockhash,
267  *chainman.SnapshotBlockhash());
268 
269  Chainstate& snapshot_chainstate = chainman.ActiveChainstate();
270 
271  {
272  LOCK(::cs_main);
273 
275 
276  // Note: WriteSnapshotBaseBlockhash() is implicitly tested above.
279  *chainman.SnapshotBlockhash());
280 
281  // Ensure that the genesis block was not marked assumed-valid.
282  BOOST_CHECK(!chainman.ActiveChain().Genesis()->IsAssumedValid());
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->nChainTx, au_data->nChainTx);
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  {
297  LOCK(::cs_main);
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  {
328  LOCK(::cs_main);
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  {
369  ChainstateManager& chainman = *Assert(m_node.chainman);
370 
371  BOOST_TEST_MESSAGE("Simulating node restart");
372  {
373  for (Chainstate* cs : chainman.GetAll()) {
374  LOCK(::cs_main);
375  cs->ForceFlushStateToDisk();
376  }
377  // Process all callbacks referring to the old manager before wiping it.
379  LOCK(::cs_main);
380  chainman.ResetChainstates();
381  BOOST_CHECK_EQUAL(chainman.GetAll().size(), 0);
382  m_node.notifications = std::make_unique<KernelNotifications>(m_node.exit_status);
383  const ChainstateManager::Options chainman_opts{
384  .chainparams = ::Params(),
385  .datadir = chainman.m_options.datadir,
386  .adjusted_time_callback = GetAdjustedTime,
387  .notifications = *m_node.notifications,
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>(m_node.kernel->interrupt, chainman_opts, blockman_opts);
398  }
399  return *Assert(m_node.chainman);
400  }
401 };
402 
404 BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, SnapshotTestSetup)
405 {
406  this->SetupSnapshot();
407 }
408 
419 BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
420 {
421  ChainstateManager& chainman = *Assert(m_node.chainman);
422  Chainstate& cs1 = chainman.ActiveChainstate();
423 
424  int num_indexes{0};
425  int num_assumed_valid{0};
426  // Blocks in range [assumed_valid_start_idx, last_assumed_valid_idx) will be
427  // marked as assumed-valid and not having data.
428  const int expected_assumed_valid{20};
429  const int last_assumed_valid_idx{111};
430  const int assumed_valid_start_idx = last_assumed_valid_idx - expected_assumed_valid;
431 
432  // Mine to height 120, past the hardcoded regtest assumeutxo snapshot at
433  // height 110
434  mineBlocks(20);
435 
436  CBlockIndex* validated_tip{nullptr};
437  CBlockIndex* assumed_base{nullptr};
438  CBlockIndex* assumed_tip{WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip())};
439  BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120);
440 
441  auto reload_all_block_indexes = [&]() {
442  // For completeness, we also reset the block sequence counters to
443  // ensure that no state which affects the ranking of tip-candidates is
444  // retained (even though this isn't strictly necessary).
445  WITH_LOCK(::cs_main, return chainman.ResetBlockSequenceCounters());
446  for (Chainstate* cs : chainman.GetAll()) {
447  LOCK(::cs_main);
448  cs->ClearBlockIndexCandidates();
449  BOOST_CHECK(cs->setBlockIndexCandidates.empty());
450  }
451 
452  WITH_LOCK(::cs_main, chainman.LoadBlockIndex());
453  };
454 
455  // Ensure that without any assumed-valid BlockIndex entries, only the current tip is
456  // considered as a candidate.
457  reload_all_block_indexes();
459 
460  // Mark some region of the chain assumed-valid, and remove the HAVE_DATA flag.
461  for (int i = 0; i <= cs1.m_chain.Height(); ++i) {
462  LOCK(::cs_main);
463  auto index = cs1.m_chain[i];
464 
465  // Blocks with heights in range [91, 110] are marked ASSUMED_VALID
466  if (i < last_assumed_valid_idx && i >= assumed_valid_start_idx) {
468  }
469 
470  ++num_indexes;
471  if (index->IsAssumedValid()) ++num_assumed_valid;
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  BOOST_CHECK(!index->IsAssumedValid());
477  }
478  // Note the last assumed valid block as the snapshot base
479  if (i == last_assumed_valid_idx - 1) {
480  assumed_base = index;
481  BOOST_CHECK(index->IsAssumedValid());
482  } else if (i == last_assumed_valid_idx) {
483  BOOST_CHECK(!index->IsAssumedValid());
484  }
485  }
486 
487  BOOST_CHECK_EQUAL(expected_assumed_valid, num_assumed_valid);
488 
489  // Note: cs2's tip is not set when ActivateExistingSnapshot is called.
490  Chainstate& cs2 = WITH_LOCK(::cs_main,
491  return chainman.ActivateExistingSnapshot(*assumed_base->phashBlock));
492 
493  // Set tip of the fully validated chain to be the validated tip
494  cs1.m_chain.SetTip(*validated_tip);
495 
496  // Set tip of the assume-valid-based chain to the assume-valid block
497  cs2.m_chain.SetTip(*assumed_base);
498 
499  // Sanity check test variables.
500  BOOST_CHECK_EQUAL(num_indexes, 121); // 121 total blocks, including genesis
501  BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120); // original chain has height 120
502  BOOST_CHECK_EQUAL(validated_tip->nHeight, 90); // current cs1 chain has height 90
503  BOOST_CHECK_EQUAL(assumed_base->nHeight, 110); // current cs2 chain has height 110
504 
505  // Regenerate cs1.setBlockIndexCandidates and cs2.setBlockIndexCandidate and
506  // check contents below.
507  reload_all_block_indexes();
508 
509  // The fully validated chain should only have the current validated tip and
510  // the assumed valid base as candidates, blocks 90 and 110. Specifically:
511  //
512  // - It does not have blocks 0-89 because they contain less work than the
513  // chain tip.
514  //
515  // - It has block 90 because it has data and equal work to the chain tip,
516  // (since it is the chain tip).
517  //
518  // - It does not have blocks 91-109 because they do not contain data.
519  //
520  // - It has block 110 even though it does not have data, because
521  // LoadBlockIndex has a special case to always add the snapshot block as a
522  // candidate. The special case is only actually intended to apply to the
523  // snapshot chainstate cs2, not the background chainstate cs1, but it is
524  // written broadly and applies to both.
525  //
526  // - It does not have any blocks after height 110 because cs1 is a background
527  // chainstate, and only blocks where are ancestors of the snapshot block
528  // are added as candidates for the background chainstate.
530  BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(validated_tip), 1);
531  BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(assumed_base), 1);
532 
533  // The assumed-valid tolerant chain has the assumed valid base as a
534  // candidate, but otherwise has none of the assumed-valid (which do not
535  // HAVE_DATA) blocks as candidates.
536  //
537  // Specifically:
538  // - All blocks below height 110 are not candidates, because cs2 chain tip
539  // has height 110 and they have less work than it does.
540  //
541  // - Block 110 is a candidate even though it does not have data, because it
542  // is the snapshot block, which is assumed valid.
543  //
544  // - Blocks 111-120 are added because they have data.
545 
546  // Check that block 90 is absent
547  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(validated_tip), 0);
548  // Check that block 109 is absent
549  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base->pprev), 0);
550  // Check that block 110 is present
551  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base), 1);
552  // Check that block 120 is present
553  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_tip), 1);
554  // Check that 11 blocks total are present.
555  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.size(), num_indexes - last_assumed_valid_idx + 1);
556 }
557 
559 BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_init, SnapshotTestSetup)
560 {
561  ChainstateManager& chainman = *Assert(m_node.chainman);
562  Chainstate& bg_chainstate = chainman.ActiveChainstate();
563 
564  this->SetupSnapshot();
565 
566  fs::path snapshot_chainstate_dir = *node::FindSnapshotChainstateDir(chainman.m_options.datadir);
567  BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
568  BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
569 
570  BOOST_CHECK(chainman.IsSnapshotActive());
571  const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
572  return chainman.ActiveTip()->GetBlockHash());
573 
574  auto all_chainstates = chainman.GetAll();
575  BOOST_CHECK_EQUAL(all_chainstates.size(), 2);
576 
577  // "Rewind" the background chainstate so that its tip is not at the
578  // base block of the snapshot - this is so after simulating a node restart,
579  // it will initialize instead of attempting to complete validation.
580  //
581  // Note that this is not a realistic use of DisconnectTip().
583  BlockValidationState unused_state;
584  {
585  LOCK2(::cs_main, bg_chainstate.MempoolMutex());
586  BOOST_CHECK(bg_chainstate.DisconnectTip(unused_state, &unused_pool));
587  unused_pool.clear(); // to avoid queuedTx assertion errors on teardown
588  }
589  BOOST_CHECK_EQUAL(bg_chainstate.m_chain.Height(), 109);
590 
591  // Test that simulating a shutdown (resetting ChainstateManager) and then performing
592  // chainstate reinitializing successfully cleans up the background-validation
593  // chainstate data, and we end up with a single chainstate that is at tip.
594  ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
595 
596  BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
597 
598  // This call reinitializes the chainstates.
599  this->LoadVerifyActivateChainstate();
600 
601  {
602  LOCK(chainman_restarted.GetMutex());
603  BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 2);
604  BOOST_CHECK(chainman_restarted.IsSnapshotActive());
605  BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
606 
607  BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
608  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
609  }
610 
611  BOOST_TEST_MESSAGE(
612  "Ensure we can mine blocks on top of the initialized snapshot chainstate");
613  mineBlocks(10);
614  {
615  LOCK(chainman_restarted.GetMutex());
616  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
617 
618  // Background chainstate should be unaware of new blocks on the snapshot
619  // chainstate.
620  for (Chainstate* cs : chainman_restarted.GetAll()) {
621  if (cs != &chainman_restarted.ActiveChainstate()) {
622  BOOST_CHECK_EQUAL(cs->m_chain.Height(), 109);
623  }
624  }
625  }
626 }
627 
628 BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion, SnapshotTestSetup)
629 {
630  this->SetupSnapshot();
631 
632  ChainstateManager& chainman = *Assert(m_node.chainman);
633  Chainstate& active_cs = chainman.ActiveChainstate();
634  auto tip_cache_before_complete = active_cs.m_coinstip_cache_size_bytes;
635  auto db_cache_before_complete = active_cs.m_coinsdb_cache_size_bytes;
636 
638  m_node.notifications->m_shutdown_on_fatal_error = false;
639 
640  fs::path snapshot_chainstate_dir = *node::FindSnapshotChainstateDir(chainman.m_options.datadir);
641  BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
642  BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
643 
644  BOOST_CHECK(chainman.IsSnapshotActive());
645  const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
646  return chainman.ActiveTip()->GetBlockHash());
647 
648  res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
650 
652  BOOST_CHECK(chainman.IsSnapshotActive());
653 
654  // Cache should have been rebalanced and reallocated to the "only" remaining
655  // chainstate.
656  BOOST_CHECK(active_cs.m_coinstip_cache_size_bytes > tip_cache_before_complete);
657  BOOST_CHECK(active_cs.m_coinsdb_cache_size_bytes > db_cache_before_complete);
658 
659  auto all_chainstates = chainman.GetAll();
660  BOOST_CHECK_EQUAL(all_chainstates.size(), 1);
661  BOOST_CHECK_EQUAL(all_chainstates[0], &active_cs);
662 
663  // Trying completion again should return false.
664  res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
666 
667  // The invalid snapshot path should not have been used.
668  fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
669  BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
670  // chainstate_snapshot should still exist.
671  BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
672 
673  // Test that simulating a shutdown (resetting ChainstateManager) and then performing
674  // chainstate reinitializing successfully cleans up the background-validation
675  // chainstate data, and we end up with a single chainstate that is at tip.
676  ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
677 
678  BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
679 
680  // This call reinitializes the chainstates, and should clean up the now unnecessary
681  // background-validation leveldb contents.
682  this->LoadVerifyActivateChainstate();
683 
684  BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
685  // chainstate_snapshot should now *not* exist.
686  BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
687 
688  const Chainstate& active_cs2 = chainman_restarted.ActiveChainstate();
689 
690  {
691  LOCK(chainman_restarted.GetMutex());
692  BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 1);
693  BOOST_CHECK(!chainman_restarted.IsSnapshotActive());
694  BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
695  BOOST_CHECK(active_cs2.m_coinstip_cache_size_bytes > tip_cache_before_complete);
696  BOOST_CHECK(active_cs2.m_coinsdb_cache_size_bytes > db_cache_before_complete);
697 
698  BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
699  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
700  }
701 
702  BOOST_TEST_MESSAGE(
703  "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
704  mineBlocks(10);
705  {
706  LOCK(chainman_restarted.GetMutex());
707  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
708  }
709 }
710 
711 BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion_hash_mismatch, SnapshotTestSetup)
712 {
713  auto chainstates = this->SetupSnapshot();
714  Chainstate& validation_chainstate = *std::get<0>(chainstates);
715  ChainstateManager& chainman = *Assert(m_node.chainman);
717  m_node.notifications->m_shutdown_on_fatal_error = false;
718 
719  // Test tampering with the IBD UTXO set with an extra coin to ensure it causes
720  // snapshot completion to fail.
721  CCoinsViewCache& ibd_coins = WITH_LOCK(::cs_main,
722  return validation_chainstate.CoinsTip());
723  Coin badcoin;
724  badcoin.out.nValue = InsecureRand32();
725  badcoin.nHeight = 1;
726  badcoin.out.scriptPubKey.assign(InsecureRandBits(6), 0);
727  uint256 txid = InsecureRand256();
728  ibd_coins.AddCoin(COutPoint(txid, 0), std::move(badcoin), false);
729 
730  fs::path snapshot_chainstate_dir = gArgs.GetDataDirNet() / "chainstate_snapshot";
731  BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
732 
733  {
734  ASSERT_DEBUG_LOG("failed to validate the -assumeutxo snapshot state");
735  res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
737  }
738 
739  auto all_chainstates = chainman.GetAll();
740  BOOST_CHECK_EQUAL(all_chainstates.size(), 1);
741  BOOST_CHECK_EQUAL(all_chainstates[0], &validation_chainstate);
742  BOOST_CHECK_EQUAL(&chainman.ActiveChainstate(), &validation_chainstate);
743 
744  fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
745  BOOST_CHECK(fs::exists(snapshot_invalid_dir));
746 
747  // Test that simulating a shutdown (resetting ChainstateManager) and then performing
748  // chainstate reinitializing successfully loads only the fully-validated
749  // chainstate data, and we end up with a single chainstate that is at tip.
750  ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
751 
752  BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
753 
754  // This call reinitializes the chainstates, and should clean up the now unnecessary
755  // background-validation leveldb contents.
756  this->LoadVerifyActivateChainstate();
757 
758  BOOST_CHECK(fs::exists(snapshot_invalid_dir));
759  BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
760 
761  {
762  LOCK(::cs_main);
763  BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 1);
764  BOOST_CHECK(!chainman_restarted.IsSnapshotActive());
765  BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
766  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
767  }
768 
769  BOOST_TEST_MESSAGE(
770  "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
771  mineBlocks(10);
772  {
773  LOCK(::cs_main);
774  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
775  }
776 }
777 
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:421
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:575
CAmount nValue
Definition: transaction.h:160
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:941
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:937
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:827
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
static void pool cs
ArgsManager m_args
Definition: setup_common.h:56
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:610
std::atomic< int > exit_status
Definition: context.h:72
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1044
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
Definition: chain.h:92
std::tuple< Chainstate *, Chainstate * > SetupSnapshot()
int Height() const
Return the maximal height in the chain.
Definition: chain.h:487
CTxOut out
unspent transaction output
Definition: coins.h:35
std::vector< CTransactionRef > m_coinbase_txns
Definition: setup_common.h:169
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:452
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:550
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:470
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:186
NodeClock::time_point GetAdjustedTime()
Definition: timedata.cpp:36
void SetBestBlock(const uint256 &hashBlock)
Definition: coins.cpp:178
uint256 GetBlockHash() const
Definition: chain.h:253
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
Definition: coins.cpp:299
#define LOCK2(cs1, cs2)
Definition: sync.h:259
bool DisconnectTip(BlockValidationState &state, DisconnectedBlockTransactions *disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Disconnect m_chain&#39;s tip.
bool IsSnapshotActive() const
Chainstate stores and provides an API to update our local knowledge of the current best chain...
Definition: validation.h:469
BOOST_FIXTURE_TEST_CASE(chainstatemanager, TestChain100Setup)
Basic tests for ChainstateManager.
#define LOCK(cs)
Definition: sync.h:258
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:74
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:779
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:1046
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:934
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:98
#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:35
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:1012
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:302
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
Definition: coins.cpp:70
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:999
ArgsManager gArgs
Definition: args.cpp:42
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:564
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:144
const CChainParams & Params()
Return the currently selected parameters.
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:71
static uint32_t InsecureRand32()
Definition: random.h:45
void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:965
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:458
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1045
std::unique_ptr< kernel::Context > kernel
libbitcoin_kernel context
Definition: context.h:50
bool IsAssumedValid() const EXCLUSIVE_LOCKS_REQUIRED(
Definition: chain.h:318
static uint64_t InsecureRandBits(int bits)
Definition: random.h:55
const fs::path & GetBlocksDirPath() const
Get blocks directory path.
Definition: args.cpp:281
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
Definition: validation.h:613
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:1003
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:228
static bool exists(const path &p)
Definition: fs.h:88
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:30
If ASSUMED_VALID is set, it means that this block has not been validated and has validity status less...
Definition: chain.h:136
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:50
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:1043
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:59
Testing setup that configures a complete environment.
Definition: setup_common.h:77
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
Definition: validation.h:1076
#define Assert(val)
Identity function.
Definition: check.h:73
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:231
static const unsigned int MAX_DISCONNECTED_TX_POOL_SIZE
Maximum kilobytes for transactions to store for processing during reorg.
#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:162