Bitcoin Core  27.99.0
P2P Digital Currency
validation_chainstatemanager_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 //
5 #include <chainparams.h>
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
105  m_node.validation_signals->SyncWithValidationInterfaceQueue();
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  Txid txid;
230  auto_infile >> txid;
231  // coins size
232  (void)ReadCompactSize(auto_infile);
233  // vout index
234  (void)ReadCompactSize(auto_infile);
235  Coin coin;
236  auto_infile >> coin;
237  }));
238 
240 
241  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
242  this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
243  // Coins count is larger than coins in file
244  metadata.m_coins_count += 1;
245  }));
246  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
247  this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
248  // Coins count is smaller than coins in file
249  metadata.m_coins_count -= 1;
250  }));
251  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
252  this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
253  // Wrong hash
254  metadata.m_base_blockhash = uint256::ZERO;
255  }));
256  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
257  this, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
258  // Wrong hash
259  metadata.m_base_blockhash = uint256::ONE;
260  }));
261 
262  BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(this));
264 
265  // Ensure our active chain is the snapshot chainstate.
266  BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
268  *chainman.ActiveChainstate().m_from_snapshot_blockhash,
269  *chainman.SnapshotBlockhash());
270 
271  Chainstate& snapshot_chainstate = chainman.ActiveChainstate();
272 
273  {
274  LOCK(::cs_main);
275 
277 
278  // Note: WriteSnapshotBaseBlockhash() is implicitly tested above.
281  *chainman.SnapshotBlockhash());
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.
377  m_node.validation_signals->SyncWithValidationInterfaceQueue();
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  .signals = m_node.validation_signals.get(),
387  };
388  const BlockManager::Options blockman_opts{
389  .chainparams = chainman_opts.chainparams,
390  .blocks_dir = m_args.GetBlocksDirPath(),
391  .notifications = chainman_opts.notifications,
392  };
393  // For robustness, ensure the old manager is destroyed before creating a
394  // new one.
395  m_node.chainman.reset();
396  m_node.chainman = std::make_unique<ChainstateManager>(*Assert(m_node.shutdown), chainman_opts, blockman_opts);
397  }
398  return *Assert(m_node.chainman);
399  }
400 };
401 
403 BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, SnapshotTestSetup)
404 {
405  this->SetupSnapshot();
406 }
407 
418 BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
419 {
420  ChainstateManager& chainman = *Assert(m_node.chainman);
421  Chainstate& cs1 = chainman.ActiveChainstate();
422 
423  int num_indexes{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  // Reset some region of the chain's nStatus, removing 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 as missing data.
464  if (i < last_assumed_valid_idx && i >= assumed_valid_start_idx) {
465  index->nStatus = BlockStatus::BLOCK_VALID_TREE;
466  index->nTx = 0;
467  index->nChainTx = 0;
468  }
469 
470  ++num_indexes;
471 
472  // Note the last fully-validated block as the expected validated tip.
473  if (i == (assumed_valid_start_idx - 1)) {
474  validated_tip = index;
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  }
480  }
481 
482  // Note: cs2's tip is not set when ActivateExistingSnapshot is called.
483  Chainstate& cs2 = WITH_LOCK(::cs_main,
484  return chainman.ActivateExistingSnapshot(*assumed_base->phashBlock));
485 
486  // Set tip of the fully validated chain to be the validated tip
487  cs1.m_chain.SetTip(*validated_tip);
488 
489  // Set tip of the assume-valid-based chain to the assume-valid block
490  cs2.m_chain.SetTip(*assumed_base);
491 
492  // Sanity check test variables.
493  BOOST_CHECK_EQUAL(num_indexes, 121); // 121 total blocks, including genesis
494  BOOST_CHECK_EQUAL(assumed_tip->nHeight, 120); // original chain has height 120
495  BOOST_CHECK_EQUAL(validated_tip->nHeight, 90); // current cs1 chain has height 90
496  BOOST_CHECK_EQUAL(assumed_base->nHeight, 110); // current cs2 chain has height 110
497 
498  // Regenerate cs1.setBlockIndexCandidates and cs2.setBlockIndexCandidate and
499  // check contents below.
500  reload_all_block_indexes();
501 
502  // The fully validated chain should only have the current validated tip and
503  // the assumed valid base as candidates, blocks 90 and 110. Specifically:
504  //
505  // - It does not have blocks 0-89 because they contain less work than the
506  // chain tip.
507  //
508  // - It has block 90 because it has data and equal work to the chain tip,
509  // (since it is the chain tip).
510  //
511  // - It does not have blocks 91-109 because they do not contain data.
512  //
513  // - It has block 110 even though it does not have data, because
514  // LoadBlockIndex has a special case to always add the snapshot block as a
515  // candidate. The special case is only actually intended to apply to the
516  // snapshot chainstate cs2, not the background chainstate cs1, but it is
517  // written broadly and applies to both.
518  //
519  // - It does not have any blocks after height 110 because cs1 is a background
520  // chainstate, and only blocks where are ancestors of the snapshot block
521  // are added as candidates for the background chainstate.
523  BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(validated_tip), 1);
524  BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(assumed_base), 1);
525 
526  // The assumed-valid tolerant chain has the assumed valid base as a
527  // candidate, but otherwise has none of the assumed-valid (which do not
528  // HAVE_DATA) blocks as candidates.
529  //
530  // Specifically:
531  // - All blocks below height 110 are not candidates, because cs2 chain tip
532  // has height 110 and they have less work than it does.
533  //
534  // - Block 110 is a candidate even though it does not have data, because it
535  // is the snapshot block, which is assumed valid.
536  //
537  // - Blocks 111-120 are added because they have data.
538 
539  // Check that block 90 is absent
540  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(validated_tip), 0);
541  // Check that block 109 is absent
542  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base->pprev), 0);
543  // Check that block 110 is present
544  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_base), 1);
545  // Check that block 120 is present
546  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_tip), 1);
547  // Check that 11 blocks total are present.
548  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.size(), num_indexes - last_assumed_valid_idx + 1);
549 }
550 
552 BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_init, SnapshotTestSetup)
553 {
554  ChainstateManager& chainman = *Assert(m_node.chainman);
555  Chainstate& bg_chainstate = chainman.ActiveChainstate();
556 
557  this->SetupSnapshot();
558 
559  fs::path snapshot_chainstate_dir = *node::FindSnapshotChainstateDir(chainman.m_options.datadir);
560  BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
561  BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
562 
563  BOOST_CHECK(chainman.IsSnapshotActive());
564  const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
565  return chainman.ActiveTip()->GetBlockHash());
566 
567  auto all_chainstates = chainman.GetAll();
568  BOOST_CHECK_EQUAL(all_chainstates.size(), 2);
569 
570  // "Rewind" the background chainstate so that its tip is not at the
571  // base block of the snapshot - this is so after simulating a node restart,
572  // it will initialize instead of attempting to complete validation.
573  //
574  // Note that this is not a realistic use of DisconnectTip().
576  BlockValidationState unused_state;
577  {
578  LOCK2(::cs_main, bg_chainstate.MempoolMutex());
579  BOOST_CHECK(bg_chainstate.DisconnectTip(unused_state, &unused_pool));
580  unused_pool.clear(); // to avoid queuedTx assertion errors on teardown
581  }
582  BOOST_CHECK_EQUAL(bg_chainstate.m_chain.Height(), 109);
583 
584  // Test that simulating a shutdown (resetting ChainstateManager) and then performing
585  // chainstate reinitializing successfully cleans up the background-validation
586  // chainstate data, and we end up with a single chainstate that is at tip.
587  ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
588 
589  BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
590 
591  // This call reinitializes the chainstates.
592  this->LoadVerifyActivateChainstate();
593 
594  {
595  LOCK(chainman_restarted.GetMutex());
596  BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 2);
597  BOOST_CHECK(chainman_restarted.IsSnapshotActive());
598  BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
599 
600  BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
601  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
602  }
603 
604  BOOST_TEST_MESSAGE(
605  "Ensure we can mine blocks on top of the initialized snapshot chainstate");
606  mineBlocks(10);
607  {
608  LOCK(chainman_restarted.GetMutex());
609  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
610 
611  // Background chainstate should be unaware of new blocks on the snapshot
612  // chainstate.
613  for (Chainstate* cs : chainman_restarted.GetAll()) {
614  if (cs != &chainman_restarted.ActiveChainstate()) {
615  BOOST_CHECK_EQUAL(cs->m_chain.Height(), 109);
616  }
617  }
618  }
619 }
620 
621 BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion, SnapshotTestSetup)
622 {
623  this->SetupSnapshot();
624 
625  ChainstateManager& chainman = *Assert(m_node.chainman);
626  Chainstate& active_cs = chainman.ActiveChainstate();
627  auto tip_cache_before_complete = active_cs.m_coinstip_cache_size_bytes;
628  auto db_cache_before_complete = active_cs.m_coinsdb_cache_size_bytes;
629 
631  m_node.notifications->m_shutdown_on_fatal_error = false;
632 
633  fs::path snapshot_chainstate_dir = *node::FindSnapshotChainstateDir(chainman.m_options.datadir);
634  BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
635  BOOST_CHECK_EQUAL(snapshot_chainstate_dir, gArgs.GetDataDirNet() / "chainstate_snapshot");
636 
637  BOOST_CHECK(chainman.IsSnapshotActive());
638  const uint256 snapshot_tip_hash = WITH_LOCK(chainman.GetMutex(),
639  return chainman.ActiveTip()->GetBlockHash());
640 
641  res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
643 
645  BOOST_CHECK(chainman.IsSnapshotActive());
646 
647  // Cache should have been rebalanced and reallocated to the "only" remaining
648  // chainstate.
649  BOOST_CHECK(active_cs.m_coinstip_cache_size_bytes > tip_cache_before_complete);
650  BOOST_CHECK(active_cs.m_coinsdb_cache_size_bytes > db_cache_before_complete);
651 
652  auto all_chainstates = chainman.GetAll();
653  BOOST_CHECK_EQUAL(all_chainstates.size(), 1);
654  BOOST_CHECK_EQUAL(all_chainstates[0], &active_cs);
655 
656  // Trying completion again should return false.
657  res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
659 
660  // The invalid snapshot path should not have been used.
661  fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
662  BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
663  // chainstate_snapshot should still exist.
664  BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
665 
666  // Test that simulating a shutdown (resetting ChainstateManager) and then performing
667  // chainstate reinitializing successfully cleans up the background-validation
668  // chainstate data, and we end up with a single chainstate that is at tip.
669  ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
670 
671  BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
672 
673  // This call reinitializes the chainstates, and should clean up the now unnecessary
674  // background-validation leveldb contents.
675  this->LoadVerifyActivateChainstate();
676 
677  BOOST_CHECK(!fs::exists(snapshot_invalid_dir));
678  // chainstate_snapshot should now *not* exist.
679  BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
680 
681  const Chainstate& active_cs2 = chainman_restarted.ActiveChainstate();
682 
683  {
684  LOCK(chainman_restarted.GetMutex());
685  BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 1);
686  BOOST_CHECK(!chainman_restarted.IsSnapshotActive());
687  BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
688  BOOST_CHECK(active_cs2.m_coinstip_cache_size_bytes > tip_cache_before_complete);
689  BOOST_CHECK(active_cs2.m_coinsdb_cache_size_bytes > db_cache_before_complete);
690 
691  BOOST_CHECK_EQUAL(chainman_restarted.ActiveTip()->GetBlockHash(), snapshot_tip_hash);
692  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
693  }
694 
695  BOOST_TEST_MESSAGE(
696  "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
697  mineBlocks(10);
698  {
699  LOCK(chainman_restarted.GetMutex());
700  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
701  }
702 }
703 
704 BOOST_FIXTURE_TEST_CASE(chainstatemanager_snapshot_completion_hash_mismatch, SnapshotTestSetup)
705 {
706  auto chainstates = this->SetupSnapshot();
707  Chainstate& validation_chainstate = *std::get<0>(chainstates);
708  ChainstateManager& chainman = *Assert(m_node.chainman);
710  m_node.notifications->m_shutdown_on_fatal_error = false;
711 
712  // Test tampering with the IBD UTXO set with an extra coin to ensure it causes
713  // snapshot completion to fail.
714  CCoinsViewCache& ibd_coins = WITH_LOCK(::cs_main,
715  return validation_chainstate.CoinsTip());
716  Coin badcoin;
717  badcoin.out.nValue = InsecureRand32();
718  badcoin.nHeight = 1;
719  badcoin.out.scriptPubKey.assign(InsecureRandBits(6), 0);
721  ibd_coins.AddCoin(COutPoint(txid, 0), std::move(badcoin), false);
722 
723  fs::path snapshot_chainstate_dir = gArgs.GetDataDirNet() / "chainstate_snapshot";
724  BOOST_CHECK(fs::exists(snapshot_chainstate_dir));
725 
726  {
727  ASSERT_DEBUG_LOG("failed to validate the -assumeutxo snapshot state");
728  res = WITH_LOCK(::cs_main, return chainman.MaybeCompleteSnapshotValidation());
730  }
731 
732  auto all_chainstates = chainman.GetAll();
733  BOOST_CHECK_EQUAL(all_chainstates.size(), 1);
734  BOOST_CHECK_EQUAL(all_chainstates[0], &validation_chainstate);
735  BOOST_CHECK_EQUAL(&chainman.ActiveChainstate(), &validation_chainstate);
736 
737  fs::path snapshot_invalid_dir = gArgs.GetDataDirNet() / "chainstate_snapshot_INVALID";
738  BOOST_CHECK(fs::exists(snapshot_invalid_dir));
739 
740  // Test that simulating a shutdown (resetting ChainstateManager) and then performing
741  // chainstate reinitializing successfully loads only the fully-validated
742  // chainstate data, and we end up with a single chainstate that is at tip.
743  ChainstateManager& chainman_restarted = this->SimulateNodeRestart();
744 
745  BOOST_TEST_MESSAGE("Performing Load/Verify/Activate of chainstate");
746 
747  // This call reinitializes the chainstates, and should clean up the now unnecessary
748  // background-validation leveldb contents.
749  this->LoadVerifyActivateChainstate();
750 
751  BOOST_CHECK(fs::exists(snapshot_invalid_dir));
752  BOOST_CHECK(!fs::exists(snapshot_chainstate_dir));
753 
754  {
755  LOCK(::cs_main);
756  BOOST_CHECK_EQUAL(chainman_restarted.GetAll().size(), 1);
757  BOOST_CHECK(!chainman_restarted.IsSnapshotActive());
758  BOOST_CHECK(!chainman_restarted.IsSnapshotValidated());
759  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 210);
760  }
761 
762  BOOST_TEST_MESSAGE(
763  "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
764  mineBlocks(10);
765  {
766  LOCK(::cs_main);
767  BOOST_CHECK_EQUAL(chainman_restarted.ActiveHeight(), 220);
768  }
769 }
770 
ArgsManager gArgs
Definition: args.cpp:41
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
@ 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
uint256 GetBlockHash() const
Definition: chain.h:244
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block.
Definition: chain.h:177
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:434
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:463
std::optional< AssumeutxoData > AssumeutxoForHeight(int height) const
Definition: chainparams.h:123
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:229
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool possible_overwrite)
Add a coin.
Definition: coins.cpp:69
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
Definition: coins.cpp:298
void SetBestBlock(const uint256 &hashBlock)
Definition: coins.cpp:177
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: coins.cpp:161
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
CScript scriptPubKey
Definition: transaction.h:153
CAmount nValue
Definition: transaction.h:152
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:490
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:584
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:570
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
Definition: validation.h:634
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:631
bool DisconnectTip(BlockValidationState &state, DisconnectedBlockTransactions *disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Disconnect m_chain's tip.
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:596
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:849
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1074
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:1073
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:1029
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
Definition: validation.h:1033
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
Definition: validation.h:1106
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
Definition: validation.h:964
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1075
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1076
bool IsSnapshotActive() const
const Options m_options
Definition: validation.h:967
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
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
Definition: validation.h:1042
void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:995
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:971
A UTXO entry.
Definition: coins.h:32
CTxOut out
unspent transaction output
Definition: coins.h:35
uint32_t nHeight
at which height this containing transaction was included in the active block chain
Definition: coins.h:41
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...
Definition: blockstorage.h:135
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:32
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:39
uint64_t m_coins_count
The number of coins in the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:45
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:106
static const uint256 ONE
Definition: uint256.h:112
static const uint256 ZERO
Definition: uint256.h:111
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
Definition: transaction.h:423
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.
Definition: setup_common.h:106
Testing setup that configures a complete environment.
Definition: setup_common.h:85
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:82
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:67
std::unique_ptr< KernelNotifications > notifications
Issues blocking calls about sync status, errors and warnings.
Definition: context.h:80
std::atomic< int > exit_status
Definition: context.h:83
util::SignalInterrupt * shutdown
Interrupt object used to track whether node shutdown was requested.
Definition: context.h:60
#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:49
static uint64_t InsecureRandBits(int bits)
Definition: random.h:54
static uint32_t InsecureRand32()
Definition: random.h:44
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:74
SnapshotCompletionResult
Definition: validation.h:800
BOOST_FIXTURE_TEST_CASE(chainstatemanager, TestChain100Setup)
Basic tests for ChainstateManager.