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