Bitcoin ABC  0.29.2
P2P Digital Currency
chainstate.cpp
Go to the documentation of this file.
1 // Copyright (c) 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 <node/chainstate.h>
6 
7 #include <chainparams.h>
8 #include <config.h>
9 #include <consensus/params.h>
10 #include <node/blockstorage.h>
11 #include <node/caches.h>
12 #include <validation.h>
13 
14 namespace node {
15 // Complete initialization of chainstates after the initial call has been made
16 // to ChainstateManager::InitializeChainstate().
18  ChainstateManager &chainman, const CacheSizes &cache_sizes,
20  auto &pblocktree{chainman.m_blockman.m_block_tree_db};
21  // new CBlockTreeDB tries to delete the existing file, which
22  // fails if it's still open from the previous loop. Close it first:
23  pblocktree.reset();
24  pblocktree.reset(new CBlockTreeDB(cache_sizes.block_tree_db,
25  options.block_tree_db_in_memory,
26  options.reindex));
27 
28  if (options.reindex) {
29  pblocktree->WriteReindexing(true);
30  // If we're reindexing in prune mode, wipe away unusable block
31  // files and all undo data files
32  if (options.prune) {
34  }
35  }
36 
37  // If necessary, upgrade from older database format.
38  // This is a no-op if we cleared the block tree db with -reindex
39  // or -reindex-chainstate
40  if (!pblocktree->Upgrade()) {
42  _("Error upgrading block index database")};
43  }
44 
45  if (options.check_interrupt && options.check_interrupt()) {
47  }
48 
49  // LoadBlockIndex will load m_have_pruned if we've ever removed a
50  // block file from disk.
51  // Note that it also sets fReindex global based on the disk flag!
52  // From here on, fReindex and options.reindex values may be different!
53  if (!chainman.LoadBlockIndex()) {
54  if (options.check_interrupt && options.check_interrupt()) {
56  }
57 
59  _("Error loading block database")};
60  }
61 
62  if (!chainman.BlockIndex().empty() &&
63  !chainman.m_blockman.LookupBlockIndex(
64  chainman.GetConsensus().hashGenesisBlock)) {
65  // If the loaded chain has a wrong genesis, bail out immediately
66  // (we're likely using a testnet datadir, or the other way around).
68  _("Incorrect or no genesis block found. Wrong datadir for "
69  "network?")};
70  }
71 
72  // Check for changed -prune state. What we are concerned about is a
73  // user who has pruned blocks in the past, but is now trying to run
74  // unpruned.
75  if (chainman.m_blockman.m_have_pruned && !options.prune) {
76  return {
78  _("You need to rebuild the database using -reindex to go back to "
79  "unpruned mode. This will redownload the entire blockchain")};
80  }
81 
82  // At this point blocktree args are consistent with what's on disk.
83  // If we're not mid-reindex (based on disk + args), add a genesis
84  // block on disk (otherwise we use the one already on disk). This is
85  // called again in ThreadImport after the reindex completes.
86  if (!fReindex && !chainman.ActiveChainstate().LoadGenesisBlock()) {
88  _("Error initializing block database")};
89  }
90 
91  auto is_coinsview_empty =
92  [&](Chainstate *chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
93  return options.reindex || options.reindex_chainstate ||
94  chainstate->CoinsTip().GetBestBlock().IsNull();
95  };
96 
97  assert(chainman.m_total_coinstip_cache > 0);
98  assert(chainman.m_total_coinsdb_cache > 0);
99 
100  // Conservative value which is arbitrarily chosen, as it will ultimately be
101  // changed by a call to `chainman.MaybeRebalanceCaches()`. We just need to
102  // make sure that the sum of the two caches (40%) does not exceed the
103  // allowable amount during this temporary initialization state.
104  double init_cache_fraction = 0.2;
105 
106  // At this point we're either in reindex or we've loaded a useful
107  // block tree into BlockIndex()!
108 
109  for (Chainstate *chainstate : chainman.GetAll()) {
110  LogPrintf("Initializing chainstate %s\n", chainstate->ToString());
111 
112  chainstate->InitCoinsDB(
113  /* cache_size_bytes */ chainman.m_total_coinsdb_cache *
114  init_cache_fraction,
115  /* in_memory */ options.coins_db_in_memory,
116  /* should_wipe */ options.reindex || options.reindex_chainstate);
117 
118  if (options.coins_error_cb) {
119  chainstate->CoinsErrorCatcher().AddReadErrCallback(
120  options.coins_error_cb);
121  }
122 
123  // If necessary, upgrade from older database format.
124  // This is a no-op if we cleared the coinsviewdb with -reindex
125  // or -reindex-chainstate
126  if (!chainstate->CoinsDB().Upgrade()) {
128  _("Error upgrading chainstate database")};
129  }
130 
131  // ReplayBlocks is a no-op if we cleared the coinsviewdb with
132  // -reindex or -reindex-chainstate
133  if (!chainstate->ReplayBlocks()) {
135  _("Unable to replay blocks. You will need to rebuild the "
136  "database using -reindex-chainstate.")};
137  }
138 
139  // The on-disk coinsdb is now in a good state, create the cache
140  chainstate->InitCoinsCache(chainman.m_total_coinstip_cache *
141  init_cache_fraction);
142  assert(chainstate->CanFlushToDisk());
143 
144  if (!is_coinsview_empty(chainstate)) {
145  // LoadChainTip initializes the chain based on CoinsTip()'s
146  // best block
147  if (!chainstate->LoadChainTip()) {
149  _("Error initializing block database")};
150  }
151  assert(chainstate->m_chain.Tip() != nullptr);
152  }
153  }
154 
155  // Now that chainstates are loaded and we're able to flush to
156  // disk, rebalance the coins caches to desired levels based
157  // on the condition of each chainstate.
158  chainman.MaybeRebalanceCaches();
159 
160  return {ChainstateLoadStatus::SUCCESS, {}};
161 }
162 
164  const CacheSizes &cache_sizes,
165  const ChainstateLoadOptions &options) {
166  if (!chainman.AssumedValidBlock().IsNull()) {
167  LogPrintf("Assuming ancestors of block %s have valid signatures.\n",
168  chainman.AssumedValidBlock().GetHex());
169  } else {
170  LogPrintf("Validating signatures for all blocks.\n");
171  }
172  LogPrintf("Setting nMinimumChainWork=%s\n",
173  chainman.MinimumChainWork().GetHex());
174  if (chainman.MinimumChainWork() <
176  LogPrintf("Warning: nMinimumChainWork set below default value of %s\n",
177  chainman.GetConsensus().nMinimumChainWork.GetHex());
178  }
179  if (chainman.m_blockman.GetPruneTarget() ==
181  LogPrintf(
182  "Block pruning enabled. Use RPC call pruneblockchain(height) to "
183  "manually prune block and undo files.\n");
184  } else if (chainman.m_blockman.GetPruneTarget()) {
185  LogPrintf("Prune configured to target %u MiB on disk for block and "
186  "undo files.\n",
187  chainman.m_blockman.GetPruneTarget() / 1024 / 1024);
188  }
189 
190  LOCK(cs_main);
191  chainman.m_total_coinstip_cache = cache_sizes.coins;
192  chainman.m_total_coinsdb_cache = cache_sizes.coins_db;
193 
194  // Load the fully validated chainstate.
195  chainman.InitializeChainstate(options.mempool);
196 
197  // Load a chain created from a UTXO snapshot, if any exist.
198  chainman.DetectSnapshotChainstate(options.mempool);
199 
200  {
201  auto [init_status, init_error] =
202  CompleteChainstateInitialization(chainman, cache_sizes, options);
203  if (init_status != ChainstateLoadStatus::SUCCESS) {
204  return {init_status, init_error};
205  }
206  }
207 
208  // If a snapshot chainstate was fully validated by a background chainstate
209  // during the last run, detect it here and clean up the now-unneeded
210  // background chainstate.
211  //
212  // Why is this cleanup done here (on subsequent restart) and not just when
213  // the snapshot is actually validated? Because this entails unusual
214  // filesystem operations to move leveldb data directories around, and that
215  // seems too risky to do in the middle of normal runtime.
216  auto snapshot_completion = chainman.MaybeCompleteSnapshotValidation();
217 
218  if (snapshot_completion == SnapshotCompletionResult::SKIPPED) {
219  // do nothing; expected case
220  } else if (snapshot_completion == SnapshotCompletionResult::SUCCESS) {
221  LogPrintf("[snapshot] cleaning up unneeded background chainstate, then "
222  "reinitializing\n");
223  if (!chainman.ValidatedSnapshotCleanup()) {
225  Untranslated(
226  "Background chainstate cleanup failed unexpectedly.")};
227  }
228 
229  // Because ValidatedSnapshotCleanup() has torn down chainstates with
230  // ChainstateManager::ResetChainstates(), reinitialize them here without
231  // duplicating the blockindex work above.
232  assert(chainman.GetAll().empty());
233  assert(!chainman.IsSnapshotActive());
234  assert(!chainman.IsSnapshotValidated());
235 
236  chainman.InitializeChainstate(options.mempool);
237 
238  // A reload of the block index is required to recompute
239  // setBlockIndexCandidates for the fully validated chainstate.
240  chainman.ActiveChainstate().UnloadBlockIndex();
241 
242  auto [init_status, init_error] =
243  CompleteChainstateInitialization(chainman, cache_sizes, options);
244  if (init_status != ChainstateLoadStatus::SUCCESS) {
245  return {init_status, init_error};
246  }
247  } else {
249  _("UTXO snapshot failed to validate. "
250  "Restart to resume normal initial block download, or try "
251  "loading a different snapshot.")};
252  }
253 
254  return {ChainstateLoadStatus::SUCCESS, {}};
255 }
256 
259  const ChainstateLoadOptions &options) {
260  auto is_coinsview_empty =
261  [&](Chainstate *chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
262  return options.reindex || options.reindex_chainstate ||
263  chainstate->CoinsTip().GetBestBlock().IsNull();
264  };
265 
266  LOCK(cs_main);
267 
268  for (Chainstate *chainstate : chainman.GetAll()) {
269  if (!is_coinsview_empty(chainstate)) {
270  const CBlockIndex *tip = chainstate->m_chain.Tip();
271  if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
273  _("The block database contains a block which appears "
274  "to be from the future. "
275  "This may be due to your computer's date and time "
276  "being set incorrectly. "
277  "Only rebuild the block database if you are sure "
278  "that your computer's date and time are correct")};
279  }
280 
281  VerifyDBResult result =
282  CVerifyDB().VerifyDB(*chainstate, chainstate->CoinsDB(),
283  options.check_level, options.check_blocks);
284  switch (result) {
287  break;
290  _("Block verification was interrupted")};
293  _("Corrupted block database detected")};
295  if (options.require_full_verification) {
296  return {
298  _("Insufficient dbcache for block verification")};
299  }
300  break;
301  } // no default case, so the compiler can warn about missing cases
302  }
303  }
304 
305  return {ChainstateLoadStatus::SUCCESS, {}};
306 }
307 } // namespace node
arith_uint256 UintToArith256(const uint256 &a)
static constexpr int64_t MAX_FUTURE_BLOCK_TIME
Maximum amount of time that a block timestamp is allowed to exceed the current network-adjusted time ...
Definition: chain.h:29
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:26
uint32_t nTime
Definition: blockindex.h:93
Access to the block database (blocks/index/)
Definition: txdb.h:112
RAII wrapper for VerifyDB: Verify consistency of the block and coin databases.
Definition: validation.h:554
VerifyDBResult VerifyDB(Chainstate &chainstate, CCoinsView &coinsview, int nCheckLevel, int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:629
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:1144
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:1304
const arith_uint256 & MinimumChainWork() const
Definition: validation.h:1247
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
Definition: validation.h:1308
const BlockHash & AssumedValidBlock() const
Definition: validation.h:1250
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
Definition: validation.h:1373
bool IsSnapshotActive() const
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
Definition: validation.h:1318
const Consensus::Params & GetConsensus() const
Definition: validation.h:1241
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:1273
bool IsNull() const
Definition: uint256.h:32
std::string GetHex() const
Definition: uint256.cpp:16
std::string GetHex() const
static constexpr auto PRUNE_TARGET_MANUAL
Definition: blockstorage.h:204
uint64_t GetPruneTarget() const
Attempt to stay below this number of bytes of block files.
Definition: blockstorage.h:201
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
#define LogPrintf(...)
Definition: logging.h:206
Definition: init.h:28
@ FAILURE_FATAL
Fatal error which should not prompt to reindex.
@ FAILURE
Generic failure which reindexing may fix.
const CBlockIndex *GetFirstStoredBlock(const CBlockIndex *start_block) EXCLUSIVE_LOCKS_REQUIRED(voi CleanupBlockRevFiles)()
Find the first block that is not pruned.
Definition: blockstorage.h:231
std::tuple< ChainstateLoadStatus, bilingual_str > ChainstateLoadResult
Chainstate load status code and optional error string.
Definition: chainstate.h:56
ChainstateLoadResult LoadChainstate(ChainstateManager &chainman, const CacheSizes &cache_sizes, const ChainstateLoadOptions &options)
This sequence can have 4 types of outcomes:
Definition: chainstate.cpp:163
static ChainstateLoadResult CompleteChainstateInitialization(ChainstateManager &chainman, const CacheSizes &cache_sizes, const ChainstateLoadOptions &options) EXCLUSIVE_LOCKS_REQUIRED(
Definition: chainstate.cpp:17
ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager &chainman, const ChainstateLoadOptions &options)
Definition: chainstate.cpp:258
std::atomic_bool fReindex
uint256 nMinimumChainWork
Definition: params.h:88
int64_t coins
Definition: caches.h:17
int64_t coins_db
Definition: caches.h:16
#define LOCK(cs)
Definition: sync.h:306
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
int64_t GetTime()
Definition: time.cpp:109
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36
assert(!tx.IsCoinBase())
VerifyDBResult
Definition: validation.h:542