Bitcoin Core  24.99.0
P2P Digital Currency
validation_chainstatemanager_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2021 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>
7 #include <node/utxo_snapshot.h>
8 #include <random.h>
9 #include <rpc/blockchain.h>
10 #include <sync.h>
11 #include <test/util/chainstate.h>
12 #include <test/util/setup_common.h>
13 #include <uint256.h>
14 #include <validation.h>
15 #include <validationinterface.h>
16 
17 #include <tinyformat.h>
18 
19 #include <vector>
20 
21 #include <boost/test/unit_test.hpp>
22 
24 
25 BOOST_FIXTURE_TEST_SUITE(validation_chainstatemanager_tests, ChainTestingSetup)
26 
27 BOOST_AUTO_TEST_CASE(chainstatemanager)
31 {
32  ChainstateManager& manager = *m_node.chainman;
33  CTxMemPool& mempool = *m_node.mempool;
34 
35  std::vector<Chainstate*> chainstates;
36 
37  BOOST_CHECK(!manager.SnapshotBlockhash().has_value());
38 
39  // Create a legacy (IBD) chainstate.
40  //
41  Chainstate& c1 = WITH_LOCK(::cs_main, return manager.InitializeChainstate(&mempool));
42  chainstates.push_back(&c1);
43  c1.InitCoinsDB(
44  /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
45  WITH_LOCK(::cs_main, c1.InitCoinsCache(1 << 23));
46 
47  BOOST_CHECK(!manager.IsSnapshotActive());
48  BOOST_CHECK(WITH_LOCK(::cs_main, return !manager.IsSnapshotValidated()));
49  auto all = manager.GetAll();
50  BOOST_CHECK_EQUAL_COLLECTIONS(all.begin(), all.end(), chainstates.begin(), chainstates.end());
51 
52  auto& active_chain = WITH_LOCK(manager.GetMutex(), return manager.ActiveChain());
53  BOOST_CHECK_EQUAL(&active_chain, &c1.m_chain);
54 
55  BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), -1);
56 
57  auto active_tip = WITH_LOCK(manager.GetMutex(), return manager.ActiveTip());
58  auto exp_tip = c1.m_chain.Tip();
59  BOOST_CHECK_EQUAL(active_tip, exp_tip);
60 
61  BOOST_CHECK(!manager.SnapshotBlockhash().has_value());
62 
63  // Create a snapshot-based chainstate.
64  //
65  const uint256 snapshot_blockhash = GetRandHash();
66  Chainstate& c2 = WITH_LOCK(::cs_main, return manager.InitializeChainstate(
67  &mempool, snapshot_blockhash));
68  chainstates.push_back(&c2);
69 
70  BOOST_CHECK_EQUAL(manager.SnapshotBlockhash().value(), snapshot_blockhash);
71 
72  c2.InitCoinsDB(
73  /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
74  WITH_LOCK(::cs_main, c2.InitCoinsCache(1 << 23));
75  // Unlike c1, which doesn't have any blocks. Gets us different tip, height.
76  c2.LoadGenesisBlock();
78  BOOST_CHECK(c2.ActivateBestChain(_, nullptr));
79 
80  BOOST_CHECK(manager.IsSnapshotActive());
81  BOOST_CHECK(WITH_LOCK(::cs_main, return !manager.IsSnapshotValidated()));
82  BOOST_CHECK_EQUAL(&c2, &manager.ActiveChainstate());
83  BOOST_CHECK(&c1 != &manager.ActiveChainstate());
84  auto all2 = manager.GetAll();
85  BOOST_CHECK_EQUAL_COLLECTIONS(all2.begin(), all2.end(), chainstates.begin(), chainstates.end());
86 
87  auto& active_chain2 = WITH_LOCK(manager.GetMutex(), return manager.ActiveChain());
88  BOOST_CHECK_EQUAL(&active_chain2, &c2.m_chain);
89 
90  BOOST_CHECK_EQUAL(WITH_LOCK(manager.GetMutex(), return manager.ActiveHeight()), 0);
91 
92  auto active_tip2 = WITH_LOCK(manager.GetMutex(), return manager.ActiveTip());
93  auto exp_tip2 = c2.m_chain.Tip();
94  BOOST_CHECK_EQUAL(active_tip2, exp_tip2);
95 
96  // Ensure that these pointers actually correspond to different
97  // CCoinsViewCache instances.
98  BOOST_CHECK(exp_tip != exp_tip2);
99 
100  // Let scheduler events finish running to avoid accessing memory that is going to be unloaded
102 }
103 
105 BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
106 {
107  ChainstateManager& manager = *m_node.chainman;
108  CTxMemPool& mempool = *m_node.mempool;
109 
110  size_t max_cache = 10000;
111  manager.m_total_coinsdb_cache = max_cache;
112  manager.m_total_coinstip_cache = max_cache;
113 
114  std::vector<Chainstate*> chainstates;
115 
116  // Create a legacy (IBD) chainstate.
117  //
118  Chainstate& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool));
119  chainstates.push_back(&c1);
120  c1.InitCoinsDB(
121  /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
122 
123  {
124  LOCK(::cs_main);
125  c1.InitCoinsCache(1 << 23);
126  BOOST_REQUIRE(c1.LoadGenesisBlock());
128  manager.MaybeRebalanceCaches();
129  }
130 
133 
134  // Create a snapshot-based chainstate.
135  //
136  Chainstate& c2 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool, GetRandHash()));
137  chainstates.push_back(&c2);
138  c2.InitCoinsDB(
139  /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
140 
141  {
142  LOCK(::cs_main);
143  c2.InitCoinsCache(1 << 23);
144  BOOST_REQUIRE(c2.LoadGenesisBlock());
146  manager.MaybeRebalanceCaches();
147  }
148 
149  // Since both chainstates are considered to be in initial block download,
150  // the snapshot chainstate should take priority.
151  BOOST_CHECK_CLOSE(c1.m_coinstip_cache_size_bytes, max_cache * 0.05, 1);
152  BOOST_CHECK_CLOSE(c1.m_coinsdb_cache_size_bytes, max_cache * 0.05, 1);
153  BOOST_CHECK_CLOSE(c2.m_coinstip_cache_size_bytes, max_cache * 0.95, 1);
154  BOOST_CHECK_CLOSE(c2.m_coinsdb_cache_size_bytes, max_cache * 0.95, 1);
155 }
156 
158 BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
159 {
160  ChainstateManager& chainman = *Assert(m_node.chainman);
161 
162  size_t initial_size;
163  size_t initial_total_coins{100};
164 
165  // Make some initial assertions about the contents of the chainstate.
166  {
167  LOCK(::cs_main);
168  CCoinsViewCache& ibd_coinscache = chainman.ActiveChainstate().CoinsTip();
169  initial_size = ibd_coinscache.GetCacheSize();
170  size_t total_coins{0};
171 
172  for (CTransactionRef& txn : m_coinbase_txns) {
173  COutPoint op{txn->GetHash(), 0};
174  BOOST_CHECK(ibd_coinscache.HaveCoin(op));
175  total_coins++;
176  }
177 
178  BOOST_CHECK_EQUAL(total_coins, initial_total_coins);
179  BOOST_CHECK_EQUAL(initial_size, initial_total_coins);
180  }
181 
182  // Snapshot should refuse to load at this height.
183  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(m_node, m_path_root));
185  BOOST_CHECK(!chainman.SnapshotBlockhash());
186 
187  // Mine 10 more blocks, putting at us height 110 where a valid assumeutxo value can
188  // be found.
189  constexpr int snapshot_height = 110;
190  mineBlocks(10);
191  initial_size += 10;
192  initial_total_coins += 10;
193 
194  // Should not load malleated snapshots
195  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
196  m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
197  // A UTXO is missing but count is correct
198  metadata.m_coins_count -= 1;
199 
200  COutPoint outpoint;
201  Coin coin;
202 
203  auto_infile >> outpoint;
204  auto_infile >> coin;
205  }));
206  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
207  m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
208  // Coins count is larger than coins in file
209  metadata.m_coins_count += 1;
210  }));
211  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
212  m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
213  // Coins count is smaller than coins in file
214  metadata.m_coins_count -= 1;
215  }));
216  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
217  m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
218  // Wrong hash
219  metadata.m_base_blockhash = uint256::ZERO;
220  }));
221  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
222  m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
223  // Wrong hash
224  metadata.m_base_blockhash = uint256::ONE;
225  }));
226 
227  BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(m_node, m_path_root));
228 
229  // Ensure our active chain is the snapshot chainstate.
233  *chainman.SnapshotBlockhash());
234 
235  // Ensure that the genesis block was not marked assumed-valid.
236  BOOST_CHECK(WITH_LOCK(::cs_main, return !chainman.ActiveChain().Genesis()->IsAssumedValid()));
237 
238  const AssumeutxoData& au_data = *ExpectedAssumeutxo(snapshot_height, ::Params());
239  const CBlockIndex* tip = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip());
240 
241  BOOST_CHECK_EQUAL(tip->nChainTx, au_data.nChainTx);
242 
243  // To be checked against later when we try loading a subsequent snapshot.
244  uint256 loaded_snapshot_blockhash{*chainman.SnapshotBlockhash()};
245 
246  // Make some assertions about the both chainstates. These checks ensure the
247  // legacy chainstate hasn't changed and that the newly created chainstate
248  // reflects the expected content.
249  {
250  LOCK(::cs_main);
251  int chains_tested{0};
252 
253  for (Chainstate* chainstate : chainman.GetAll()) {
254  BOOST_TEST_MESSAGE("Checking coins in " << chainstate->ToString());
255  CCoinsViewCache& coinscache = chainstate->CoinsTip();
256 
257  // Both caches will be empty initially.
258  BOOST_CHECK_EQUAL((unsigned int)0, coinscache.GetCacheSize());
259 
260  size_t total_coins{0};
261 
262  for (CTransactionRef& txn : m_coinbase_txns) {
263  COutPoint op{txn->GetHash(), 0};
264  BOOST_CHECK(coinscache.HaveCoin(op));
265  total_coins++;
266  }
267 
268  BOOST_CHECK_EQUAL(initial_size , coinscache.GetCacheSize());
269  BOOST_CHECK_EQUAL(total_coins, initial_total_coins);
270  chains_tested++;
271  }
272 
273  BOOST_CHECK_EQUAL(chains_tested, 2);
274  }
275 
276  // Mine some new blocks on top of the activated snapshot chainstate.
277  constexpr size_t new_coins{100};
278  mineBlocks(new_coins); // Defined in TestChain100Setup.
279 
280  {
281  LOCK(::cs_main);
282  size_t coins_in_active{0};
283  size_t coins_in_background{0};
284  size_t coins_missing_from_background{0};
285 
286  for (Chainstate* chainstate : chainman.GetAll()) {
287  BOOST_TEST_MESSAGE("Checking coins in " << chainstate->ToString());
288  CCoinsViewCache& coinscache = chainstate->CoinsTip();
289  bool is_background = chainstate != &chainman.ActiveChainstate();
290 
291  for (CTransactionRef& txn : m_coinbase_txns) {
292  COutPoint op{txn->GetHash(), 0};
293  if (coinscache.HaveCoin(op)) {
294  (is_background ? coins_in_background : coins_in_active)++;
295  } else if (is_background) {
296  coins_missing_from_background++;
297  }
298  }
299  }
300 
301  BOOST_CHECK_EQUAL(coins_in_active, initial_total_coins + new_coins);
302  BOOST_CHECK_EQUAL(coins_in_background, initial_total_coins);
303  BOOST_CHECK_EQUAL(coins_missing_from_background, new_coins);
304  }
305 
306  // Snapshot should refuse to load after one has already loaded.
307  BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(m_node, m_path_root));
308 
309  // Snapshot blockhash should be unchanged.
312  loaded_snapshot_blockhash);
313 }
314 
325 BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
326 {
327  ChainstateManager& chainman = *Assert(m_node.chainman);
328  CTxMemPool& mempool = *m_node.mempool;
329  Chainstate& cs1 = chainman.ActiveChainstate();
330 
331  int num_indexes{0};
332  int num_assumed_valid{0};
333  const int expected_assumed_valid{20};
334  const int last_assumed_valid_idx{40};
335  const int assumed_valid_start_idx = last_assumed_valid_idx - expected_assumed_valid;
336 
337  CBlockIndex* validated_tip{nullptr};
338  CBlockIndex* assumed_tip{WITH_LOCK(chainman.GetMutex(), return chainman.ActiveChain().Tip())};
339 
340  auto reload_all_block_indexes = [&]() {
341  for (Chainstate* cs : chainman.GetAll()) {
342  LOCK(::cs_main);
343  cs->UnloadBlockIndex();
344  BOOST_CHECK(cs->setBlockIndexCandidates.empty());
345  }
346 
347  WITH_LOCK(::cs_main, chainman.LoadBlockIndex());
348  };
349 
350  // Ensure that without any assumed-valid BlockIndex entries, all entries are considered
351  // tip candidates.
352  reload_all_block_indexes();
354 
355  // Mark some region of the chain assumed-valid.
356  for (int i = 0; i <= cs1.m_chain.Height(); ++i) {
357  LOCK(::cs_main);
358  auto index = cs1.m_chain[i];
359 
360  if (i < last_assumed_valid_idx && i >= assumed_valid_start_idx) {
362  }
363 
364  ++num_indexes;
365  if (index->IsAssumedValid()) ++num_assumed_valid;
366 
367  // Note the last fully-validated block as the expected validated tip.
368  if (i == (assumed_valid_start_idx - 1)) {
369  validated_tip = index;
370  BOOST_CHECK(!index->IsAssumedValid());
371  }
372  }
373 
374  BOOST_CHECK_EQUAL(expected_assumed_valid, num_assumed_valid);
375 
376  Chainstate& cs2 = WITH_LOCK(::cs_main,
377  return chainman.InitializeChainstate(&mempool, GetRandHash()));
378 
379  reload_all_block_indexes();
380 
381  // The fully validated chain only has candidates up to the start of the assumed-valid
382  // blocks.
383  BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(validated_tip), 1);
384  BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.count(assumed_tip), 0);
385  BOOST_CHECK_EQUAL(cs1.setBlockIndexCandidates.size(), assumed_valid_start_idx);
386 
387  // The assumed-valid tolerant chain has all blocks as candidates.
388  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(validated_tip), 1);
389  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.count(assumed_tip), 1);
390  BOOST_CHECK_EQUAL(cs2.setBlockIndexCandidates.size(), num_indexes);
391 }
392 
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
@ BLOCK_ASSUMED_VALID
If set, this indicates that the block index entry is assumed-valid.
Definition: chain.h:143
@ BLOCK_VALID_TREE
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
Definition: chain.h:107
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:121
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
Definition: check.h:74
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:475
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:152
bool IsAssumedValid() const EXCLUSIVE_LOCKS_REQUIRED(
Definition: chain.h:325
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block.
Definition: chain.h:193
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:433
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:439
int Height() const
Return the maximal height in the chain.
Definition: chain.h:468
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:213
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
Definition: coins.cpp:257
void SetBestBlock(const uint256 &hashBlock)
Definition: coins.cpp:175
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: coins.cpp:159
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:35
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:432
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:438
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:519
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
Definition: validation.h:576
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
Definition: validation.h:573
bool LoadGenesisBlock()
Ensures we have a genesis block in the block tree, possibly writing one to disk.
const std::optional< uint256 > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from.
Definition: validation.h:526
std::set< CBlockIndex *, node::CBlockIndexWorkComparator > setBlockIndexCandidates
The set of all CBlockIndex entries with either BLOCK_VALID_TRANSACTIONS (for itself and all ancestors...
Definition: validation.h:538
void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe, fs::path leveldb_name="chainstate")
Initialize the CoinsViews UTXO set database management data structures.
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:541
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:800
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:965
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:926
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
Definition: validation.h:930
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
Definition: validation.h:987
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
Definition: validation.h:892
Chainstate & ActiveChainstate() const
The most-work chain.
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:966
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:967
bool IsSnapshotActive() const
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, const std::optional< uint256 > &snapshot_blockhash=std::nullopt) LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate and assign it based upon whether it is from a snapshot.
Definition: validation.h:945
A UTXO entry.
Definition: coins.h:31
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:16
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:20
uint64_t m_coins_count
The number of coins in the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:24
256-bit opaque blob.
Definition: uint256.h:119
static const uint256 ONE
Definition: uint256.h:125
static const uint256 ZERO
Definition: uint256.h:124
BOOST_AUTO_TEST_SUITE_END()
static void pool cs
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
#define BOOST_CHECK(expr)
Definition: object.cpp:16
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:414
uint256 GetRandHash() noexcept
Definition: random.cpp:592
static uint256 InsecureRand256()
Definition: setup_common.h:73
Holds configuration for use during UTXO snapshot load and validation.
Definition: chainparams.h:40
const unsigned int nChainTx
Used to populate the nChainTx value, which is used during BlockManager::LoadBlockIndex().
Definition: chainparams.h:48
Testing setup that performs all steps up until right before ChainstateManager gets initialized.
Definition: setup_common.h:100
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:126
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:50
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:54
#define LOCK(cs)
Definition: sync.h:261
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:305
static bool CreateAndActivateUTXOSnapshot(node::NodeContext &node, const fs::path root, F malleation=NoMalleation)
Create and activate a UTXO snapshot, optionally providing a function to malleate the snapshot.
Definition: chainstate.h:26
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:65
const AssumeutxoData * ExpectedAssumeutxo(const int height, const CChainParams &chainparams)
Return the expected assumeutxo value for a given height, if one exists.
BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
Test basic snapshot activation.
BOOST_AUTO_TEST_CASE(chainstatemanager)
Basic tests for ChainstateManager.
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...