Bitcoin Core  29.1.0
P2P Digital Currency
chainstate.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021-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 <node/chainstate.h>
6 
7 #include <arith_uint256.h>
8 #include <chain.h>
9 #include <coins.h>
10 #include <consensus/params.h>
11 #include <kernel/caches.h>
12 #include <logging.h>
13 #include <node/blockstorage.h>
14 #include <sync.h>
15 #include <threadsafety.h>
16 #include <tinyformat.h>
17 #include <txdb.h>
18 #include <uint256.h>
19 #include <util/fs.h>
20 #include <util/signalinterrupt.h>
21 #include <util/time.h>
22 #include <util/translation.h>
23 #include <validation.h>
24 
25 #include <algorithm>
26 #include <cassert>
27 #include <vector>
28 
29 using kernel::CacheSizes;
30 
31 namespace node {
32 // Complete initialization of chainstates after the initial call has been made
33 // to ChainstateManager::InitializeChainstate().
35  ChainstateManager& chainman,
37 {
38  if (chainman.m_interrupt) return {ChainstateLoadStatus::INTERRUPTED, {}};
39 
40  // LoadBlockIndex will load m_have_pruned if we've ever removed a
41  // block file from disk.
42  // Note that it also sets m_blockfiles_indexed based on the disk flag!
43  if (!chainman.LoadBlockIndex()) {
44  if (chainman.m_interrupt) return {ChainstateLoadStatus::INTERRUPTED, {}};
45  return {ChainstateLoadStatus::FAILURE, _("Error loading block database")};
46  }
47 
48  if (!chainman.BlockIndex().empty() &&
49  !chainman.m_blockman.LookupBlockIndex(chainman.GetConsensus().hashGenesisBlock)) {
50  // If the loaded chain has a wrong genesis, bail out immediately
51  // (we're likely using a testnet datadir, or the other way around).
52  return {ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB, _("Incorrect or no genesis block found. Wrong datadir for network?")};
53  }
54 
55  // Check for changed -prune state. What we are concerned about is a user who has pruned blocks
56  // in the past, but is now trying to run unpruned.
57  if (chainman.m_blockman.m_have_pruned && !options.prune) {
58  return {ChainstateLoadStatus::FAILURE, _("You need to rebuild the database using -reindex to go back to unpruned mode. This will redownload the entire blockchain")};
59  }
60 
61  // At this point blocktree args are consistent with what's on disk.
62  // If we're not mid-reindex (based on disk + args), add a genesis block on disk
63  // (otherwise we use the one already on disk).
64  // This is called again in ImportBlocks after the reindex completes.
65  if (chainman.m_blockman.m_blockfiles_indexed && !chainman.ActiveChainstate().LoadGenesisBlock()) {
66  return {ChainstateLoadStatus::FAILURE, _("Error initializing block database")};
67  }
68 
69  auto is_coinsview_empty = [&](Chainstate* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
70  return options.wipe_chainstate_db || chainstate->CoinsTip().GetBestBlock().IsNull();
71  };
72 
73  assert(chainman.m_total_coinstip_cache > 0);
74  assert(chainman.m_total_coinsdb_cache > 0);
75 
76  // If running with multiple chainstates, limit the cache sizes with a
77  // discount factor. If discounted the actual cache size will be
78  // recalculated by `chainman.MaybeRebalanceCaches()`. The discount factor
79  // is conservatively chosen such that the sum of the caches does not exceed
80  // the allowable amount during this temporary initialization state.
81  double init_cache_fraction = chainman.GetAll().size() > 1 ? 0.2 : 1.0;
82 
83  // At this point we're either in reindex or we've loaded a useful
84  // block tree into BlockIndex()!
85 
86  for (Chainstate* chainstate : chainman.GetAll()) {
87  LogPrintf("Initializing chainstate %s\n", chainstate->ToString());
88 
89  try {
90  chainstate->InitCoinsDB(
91  /*cache_size_bytes=*/chainman.m_total_coinsdb_cache * init_cache_fraction,
92  /*in_memory=*/options.coins_db_in_memory,
93  /*should_wipe=*/options.wipe_chainstate_db);
94  } catch (dbwrapper_error& err) {
95  LogError("%s\n", err.what());
96  return {ChainstateLoadStatus::FAILURE, _("Error opening coins database")};
97  }
98 
99  if (options.coins_error_cb) {
100  chainstate->CoinsErrorCatcher().AddReadErrCallback(options.coins_error_cb);
101  }
102 
103  // Refuse to load unsupported database format.
104  // This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
105  if (chainstate->CoinsDB().NeedsUpgrade()) {
106  return {ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB, _("Unsupported chainstate database format found. "
107  "Please restart with -reindex-chainstate. This will "
108  "rebuild the chainstate database.")};
109  }
110 
111  // ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
112  if (!chainstate->ReplayBlocks()) {
113  return {ChainstateLoadStatus::FAILURE, _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.")};
114  }
115 
116  // The on-disk coinsdb is now in a good state, create the cache
117  chainstate->InitCoinsCache(chainman.m_total_coinstip_cache * init_cache_fraction);
118  assert(chainstate->CanFlushToDisk());
119 
120  if (!is_coinsview_empty(chainstate)) {
121  // LoadChainTip initializes the chain based on CoinsTip()'s best block
122  if (!chainstate->LoadChainTip()) {
123  return {ChainstateLoadStatus::FAILURE, _("Error initializing block database")};
124  }
125  assert(chainstate->m_chain.Tip() != nullptr);
126  }
127  }
128 
129  auto chainstates{chainman.GetAll()};
130  if (std::any_of(chainstates.begin(), chainstates.end(),
131  [](const Chainstate* cs) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return cs->NeedsRedownload(); })) {
132  return {ChainstateLoadStatus::FAILURE, strprintf(_("Witness data for blocks after height %d requires validation. Please restart with -reindex."),
133  chainman.GetConsensus().SegwitHeight)};
134  };
135 
136  // Now that chainstates are loaded and we're able to flush to
137  // disk, rebalance the coins caches to desired levels based
138  // on the condition of each chainstate.
139  chainman.MaybeRebalanceCaches();
140 
141  return {ChainstateLoadStatus::SUCCESS, {}};
142 }
143 
145  const ChainstateLoadOptions& options)
146 {
147  if (!chainman.AssumedValidBlock().IsNull()) {
148  LogPrintf("Assuming ancestors of block %s have valid signatures.\n", chainman.AssumedValidBlock().GetHex());
149  } else {
150  LogPrintf("Validating signatures for all blocks.\n");
151  }
152  LogPrintf("Setting nMinimumChainWork=%s\n", chainman.MinimumChainWork().GetHex());
153  if (chainman.MinimumChainWork() < UintToArith256(chainman.GetConsensus().nMinimumChainWork)) {
154  LogPrintf("Warning: nMinimumChainWork set below default value of %s\n", chainman.GetConsensus().nMinimumChainWork.GetHex());
155  }
157  LogPrintf("Block pruning enabled. Use RPC call pruneblockchain(height) to manually prune block and undo files.\n");
158  } else if (chainman.m_blockman.GetPruneTarget()) {
159  LogPrintf("Prune configured to target %u MiB on disk for block and undo files.\n", chainman.m_blockman.GetPruneTarget() / 1024 / 1024);
160  }
161 
162  LOCK(cs_main);
163 
164  chainman.m_total_coinstip_cache = cache_sizes.coins;
165  chainman.m_total_coinsdb_cache = cache_sizes.coins_db;
166 
167  // Load the fully validated chainstate.
168  chainman.InitializeChainstate(options.mempool);
169 
170  // Load a chain created from a UTXO snapshot, if any exist.
171  bool has_snapshot = chainman.DetectSnapshotChainstate();
172 
173  if (has_snapshot && options.wipe_chainstate_db) {
174  LogPrintf("[snapshot] deleting snapshot chainstate due to reindexing\n");
175  if (!chainman.DeleteSnapshotChainstate()) {
176  return {ChainstateLoadStatus::FAILURE_FATAL, Untranslated("Couldn't remove snapshot chainstate.")};
177  }
178  }
179 
180  auto [init_status, init_error] = CompleteChainstateInitialization(chainman, options);
181  if (init_status != ChainstateLoadStatus::SUCCESS) {
182  return {init_status, init_error};
183  }
184 
185  // If a snapshot chainstate was fully validated by a background chainstate during
186  // the last run, detect it here and clean up the now-unneeded background
187  // chainstate.
188  //
189  // Why is this cleanup done here (on subsequent restart) and not just when the
190  // snapshot is actually validated? Because this entails unusual
191  // filesystem operations to move leveldb data directories around, and that seems
192  // too risky to do in the middle of normal runtime.
193  auto snapshot_completion = chainman.MaybeCompleteSnapshotValidation();
194 
195  if (snapshot_completion == SnapshotCompletionResult::SKIPPED) {
196  // do nothing; expected case
197  } else if (snapshot_completion == SnapshotCompletionResult::SUCCESS) {
198  LogPrintf("[snapshot] cleaning up unneeded background chainstate, then reinitializing\n");
199  if (!chainman.ValidatedSnapshotCleanup()) {
200  return {ChainstateLoadStatus::FAILURE_FATAL, Untranslated("Background chainstate cleanup failed unexpectedly.")};
201  }
202 
203  // Because ValidatedSnapshotCleanup() has torn down chainstates with
204  // ChainstateManager::ResetChainstates(), reinitialize them here without
205  // duplicating the blockindex work above.
206  assert(chainman.GetAll().empty());
207  assert(!chainman.IsSnapshotActive());
208  assert(!chainman.IsSnapshotValidated());
209 
210  chainman.InitializeChainstate(options.mempool);
211 
212  // A reload of the block index is required to recompute setBlockIndexCandidates
213  // for the fully validated chainstate.
214  chainman.ActiveChainstate().ClearBlockIndexCandidates();
215 
216  auto [init_status, init_error] = CompleteChainstateInitialization(chainman, options);
217  if (init_status != ChainstateLoadStatus::SUCCESS) {
218  return {init_status, init_error};
219  }
220  } else {
222  "UTXO snapshot failed to validate. "
223  "Restart to resume normal initial block download, or try loading a different snapshot.")};
224  }
225 
226  return {ChainstateLoadStatus::SUCCESS, {}};
227 }
228 
230 {
231  auto is_coinsview_empty = [&](Chainstate* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
232  return options.wipe_chainstate_db || chainstate->CoinsTip().GetBestBlock().IsNull();
233  };
234 
235  LOCK(cs_main);
236 
237  for (Chainstate* chainstate : chainman.GetAll()) {
238  if (!is_coinsview_empty(chainstate)) {
239  const CBlockIndex* tip = chainstate->m_chain.Tip();
240  if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
241  return {ChainstateLoadStatus::FAILURE, _("The block database contains a block which appears to be from the future. "
242  "This may be due to your computer's date and time being set incorrectly. "
243  "Only rebuild the block database if you are sure that your computer's date and time are correct")};
244  }
245 
246  VerifyDBResult result = CVerifyDB(chainman.GetNotifications()).VerifyDB(
247  *chainstate, chainman.GetConsensus(), chainstate->CoinsDB(),
248  options.check_level,
249  options.check_blocks);
250  switch (result) {
253  break;
255  return {ChainstateLoadStatus::INTERRUPTED, _("Block verification was interrupted")};
257  return {ChainstateLoadStatus::FAILURE, _("Corrupted block database detected")};
259  if (options.require_full_verification) {
260  return {ChainstateLoadStatus::FAILURE_INSUFFICIENT_DBCACHE, _("Insufficient dbcache for block verification")};
261  }
262  break;
263  } // no default case, so the compiler can warn about missing cases
264  }
265  }
266 
267  return {ChainstateLoadStatus::SUCCESS, {}};
268 }
269 } // namespace node
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:1007
const uint256 & AssumedValidBlock() const
Definition: validation.h:980
assert(!tx.IsCoinBase())
static constexpr auto PRUNE_TARGET_MANUAL
Definition: blockstorage.h:353
ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager &chainman, const ChainstateLoadOptions &options)
Definition: chainstate.cpp:229
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:865
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:82
Fatal error which should not prompt to reindex.
size_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
Definition: validation.h:1071
uint32_t nTime
Definition: chain.h:189
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79
bool require_full_verification
Setting require_full_verification to true will require all checks at check_level (below) to succeed f...
Definition: chainstate.h:34
arith_uint256 UintToArith256(const uint256 &a)
bool IsSnapshotActive() const
Chainstate stores and provides an API to update our local knowledge of the current best chain...
Definition: validation.h:504
static void pool cs
#define LOCK(cs)
Definition: sync.h:257
kernel::Notifications & GetNotifications() const
Definition: validation.h:981
RAII wrapper for VerifyDB: Verify consistency of the block and coin databases.
Definition: validation.h:413
const Consensus::Params & GetConsensus() const
Definition: validation.h:977
size_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
Definition: validation.h:1067
constexpr bool IsNull() const
Definition: uint256.h:48
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
Definition: validation.h:1080
Definition: messages.h:20
uint256 nMinimumChainWork
The best chain should have at least this much work.
Definition: params.h:125
const arith_uint256 & MinimumChainWork() const
Definition: validation.h:979
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
auto result
Definition: common-types.h:74
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:140
#define LogError(...)
Definition: logging.h:358
VerifyDBResult
Definition: validation.h:404
std::string GetHex() const
Definition: uint256.cpp:11
Generic failure which reindexing may fix.
std::string GetHex() const
Hex encoding of the number (with the most significant digits first).
std::tuple< ChainstateLoadStatus, bilingual_str > ChainstateLoadResult
Chainstate load status code and optional error string.
Definition: chainstate.h:54
static constexpr int64_t MAX_FUTURE_BLOCK_TIME
Maximum amount of time that a block timestamp is allowed to exceed the current time before the block ...
Definition: chain.h:29
ChainstateLoadResult LoadChainstate(ChainstateManager &chainman, const CacheSizes &cache_sizes, const ChainstateLoadOptions &options)
Definition: chainstate.cpp:144
#define LogPrintf(...)
Definition: logging.h:361
int64_t GetTime()
DEPRECATED, see GetTime.
Definition: time.cpp:76
static ChainstateLoadResult CompleteChainstateInitialization(ChainstateManager &chainman, const ChainstateLoadOptions &options) EXCLUSIVE_LOCKS_REQUIRED(
Definition: chainstate.cpp:34
uint64_t GetPruneTarget() const
Attempt to stay below this number of bytes of block files.
Definition: blockstorage.h:352
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
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:1110
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
Definition: validation.h:1143