aboutsummaryrefslogtreecommitdiff
path: root/src/init.cpp
diff options
context:
space:
mode:
authorCarl Dong <contact@carldong.me>2021-09-20 16:12:18 -0400
committerCarl Dong <contact@carldong.me>2021-12-06 15:55:16 -0500
commitcb64af9635a9553e335f2dc0b1cca20c6bbd0933 (patch)
tree8ce04ae564f96bda6ace11434246f1afcd98fea4 /src/init.cpp
parent786ffb3ae488061e13c02ad3fb34a5d2fc785b3d (diff)
downloadbitcoin-cb64af9635a9553e335f2dc0b1cca20c6bbd0933.tar.xz
node: Extract chainstate loading sequence
I strongly recommend reviewing with the following git-diff flags: --color-moved=dimmed_zebra --color-moved-ws=allow-indentation-change [META] This commit is intended to be as close to a move-only commit as possible, and lingering ugliness will be resolved in subsequent commits. A few variables that are passed in by value instead of by reference deserve explanation: - fReset and fReindexChainstate are both local variables in AppInitMain and are not modified in the sequence - fPruneMode, despite being a global, is only modified in AppInitParameterInteraction, long before LoadChainstate is called ---- [META] This semantic will change in a future commit named "node/chainstate: Decouple from stringy errors"
Diffstat (limited to 'src/init.cpp')
-rw-r--r--src/init.cpp187
1 files changed, 14 insertions, 173 deletions
diff --git a/src/init.cpp b/src/init.cpp
index 22c0d928b5..869c3a8f7c 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -34,6 +34,7 @@
#include <net_processing.h>
#include <netbase.h>
#include <node/blockstorage.h>
+#include <node/chainstate.h> // for LoadChainstate
#include <node/context.h>
#include <node/miner.h>
#include <node/ui_interface.h>
@@ -1414,183 +1415,23 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
bool fLoaded = false;
while (!fLoaded && !ShutdownRequested()) {
const bool fReset = fReindex;
- auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
- return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
- };
bilingual_str strLoadError;
uiInterface.InitMessage(_("Loading block index…").translated);
- do {
- const int64_t load_block_index_start_time = GetTimeMillis();
- try {
- LOCK(cs_main);
- chainman.InitializeChainstate(Assert(node.mempool.get()));
- chainman.m_total_coinstip_cache = nCoinCacheUsage;
- chainman.m_total_coinsdb_cache = nCoinDBCache;
-
- UnloadBlockIndex(node.mempool.get(), chainman);
-
- auto& pblocktree{chainman.m_blockman.m_block_tree_db};
- // new CBlockTreeDB tries to delete the existing file, which
- // fails if it's still open from the previous loop. Close it first:
- pblocktree.reset();
- pblocktree.reset(new CBlockTreeDB(nBlockTreeDBCache, false, fReset));
-
- if (fReset) {
- pblocktree->WriteReindexing(true);
- //If we're reindexing in prune mode, wipe away unusable block files and all undo data files
- if (fPruneMode)
- CleanupBlockRevFiles();
- }
-
- if (ShutdownRequested()) break;
-
- // LoadBlockIndex will load fHavePruned if we've ever removed a
- // block file from disk.
- // Note that it also sets fReindex based on the disk flag!
- // From here on out fReindex and fReset mean something different!
- if (!chainman.LoadBlockIndex()) {
- if (ShutdownRequested()) break;
- strLoadError = _("Error loading block database");
- break;
- }
-
- // If the loaded chain has a wrong genesis, bail out immediately
- // (we're likely using a testnet datadir, or the other way around).
- if (!chainman.BlockIndex().empty() &&
- !chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
- return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
- }
-
- // Check for changed -prune state. What we are concerned about is a user who has pruned blocks
- // in the past, but is now trying to run unpruned.
- if (fHavePruned && !fPruneMode) {
- strLoadError = _("You need to rebuild the database using -reindex to go back to unpruned mode. This will redownload the entire blockchain");
- break;
- }
-
- // At this point blocktree args are consistent with what's on disk.
- // If we're not mid-reindex (based on disk + args), add a genesis block on disk
- // (otherwise we use the one already on disk).
- // This is called again in ThreadImport after the reindex completes.
- if (!fReindex && !chainman.ActiveChainstate().LoadGenesisBlock()) {
- strLoadError = _("Error initializing block database");
- break;
- }
-
- // At this point we're either in reindex or we've loaded a useful
- // block tree into BlockIndex()!
-
- bool failed_chainstate_init = false;
-
- for (CChainState* chainstate : chainman.GetAll()) {
- chainstate->InitCoinsDB(
- /* cache_size_bytes */ nCoinDBCache,
- /* in_memory */ false,
- /* should_wipe */ fReset || fReindexChainState);
-
- chainstate->CoinsErrorCatcher().AddReadErrCallback([]() {
- uiInterface.ThreadSafeMessageBox(
- _("Error reading from database, shutting down."),
- "", CClientUIInterface::MSG_ERROR);
- });
-
- // If necessary, upgrade from older database format.
- // This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
- if (!chainstate->CoinsDB().Upgrade()) {
- strLoadError = _("Error upgrading chainstate database");
- failed_chainstate_init = true;
- break;
- }
-
- // ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
- if (!chainstate->ReplayBlocks()) {
- strLoadError = _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.");
- failed_chainstate_init = true;
- break;
- }
-
- // The on-disk coinsdb is now in a good state, create the cache
- chainstate->InitCoinsCache(nCoinCacheUsage);
- assert(chainstate->CanFlushToDisk());
-
- if (!is_coinsview_empty(chainstate)) {
- // LoadChainTip initializes the chain based on CoinsTip()'s best block
- if (!chainstate->LoadChainTip()) {
- strLoadError = _("Error initializing block database");
- failed_chainstate_init = true;
- break; // out of the per-chainstate loop
- }
- assert(chainstate->m_chain.Tip() != nullptr);
- }
- }
-
- if (failed_chainstate_init) {
- break; // out of the chainstate activation do-while
- }
- } catch (const std::exception& e) {
- LogPrintf("%s\n", e.what());
- strLoadError = _("Error opening block database");
- break;
- }
-
- if (!fReset) {
- LOCK(cs_main);
- auto chainstates{chainman.GetAll()};
- if (std::any_of(chainstates.begin(), chainstates.end(),
- [](const CChainState* cs) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return cs->NeedsRedownload(); })) {
- strLoadError = strprintf(_("Witness data for blocks after height %d requires validation. Please restart with -reindex."),
- chainparams.GetConsensus().SegwitHeight);
- break;
- }
- }
-
- bool failed_verification = false;
-
- try {
- LOCK(cs_main);
-
- for (CChainState* chainstate : chainman.GetAll()) {
- if (!is_coinsview_empty(chainstate)) {
- uiInterface.InitMessage(_("Verifying blocks…").translated);
- if (fHavePruned && args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS) > MIN_BLOCKS_TO_KEEP) {
- LogPrintf("Prune: pruned datadir may not have more than %d blocks; only checking available blocks\n",
- MIN_BLOCKS_TO_KEEP);
- }
-
- const CBlockIndex* tip = chainstate->m_chain.Tip();
- RPCNotifyBlockChange(tip);
- if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
- strLoadError = _("The block database contains a block which appears to be from the future. "
- "This may be due to your computer's date and time being set incorrectly. "
- "Only rebuild the block database if you are sure that your computer's date and time are correct");
- failed_verification = true;
- break;
- }
-
- if (!CVerifyDB().VerifyDB(
- *chainstate, chainparams, chainstate->CoinsDB(),
- args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL),
- args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS))) {
- strLoadError = _("Corrupted block database detected");
- failed_verification = true;
- break;
- }
- }
- }
- } catch (const std::exception& e) {
- LogPrintf("%s\n", e.what());
- strLoadError = _("Error opening block database");
- failed_verification = true;
- break;
- }
-
- if (!failed_verification) {
- fLoaded = true;
- LogPrintf(" block index %15dms\n", GetTimeMillis() - load_block_index_start_time);
- }
- } while(false);
+ bool rv = LoadChainstate(fLoaded,
+ strLoadError,
+ fReset,
+ chainman,
+ node,
+ fPruneMode,
+ chainparams,
+ args,
+ fReindexChainState,
+ nBlockTreeDBCache,
+ nCoinDBCache,
+ nCoinCacheUsage);
+ if (!rv) return false;
if (!fLoaded && !ShutdownRequested()) {
// first suggest a reindex