Skip to content

Commit

Permalink
Order chainstate init more logically.
Browse files Browse the repository at this point in the history
>>> adapts bitcoin/bitcoin@1385697

* Order chainstate init more logically - first all of the
  blocktree-related loading, then coinsdb, then
  pcoinsTip/chainActive. Only create objects as needed.

* More clearly document exactly what is and isn't called in
  -reindex and -reindex-chainstate both with comments noting
  calls as no-ops and by adding if guards.

* Move LoadGenesisBlock further down in init. This is a more logical
  location for it, as it is after all of the blockindex-related
  loading and checking, but before any of the UTXO-related loading
  and checking.

* Move all of the VerifyDB()-related stuff into a -reindex +
  -reindex-chainstate if guard. It couldn't do anything useful
  as chainActive.Tip() would be null at this point anyway.
  • Loading branch information
random-zebra committed Mar 2, 2021
1 parent 9b87537 commit 5f1f014
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1581,18 +1581,9 @@ bool AppInitMain()
pSporkDB = new CSporkDB(0, false, false);

pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReindex);
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReindex);
pcoinscatcher = new CCoinsViewErrorCatcher(pcoinsdbview);

if (fReindex) {
pblocktree->WriteReindexing(true);
} else {
uiInterface.InitMessage(_("Upgrading coins database..."));
// If necessary, upgrade from older database format.
if (!pcoinsdbview->Upgrade()) {
strLoadError = _("Error upgrading chainstate database");
break;
}
}

// End loop if shutdown was requested
Expand All @@ -1602,6 +1593,9 @@ bool AppInitMain()
uiInterface.InitMessage(_("Loading sporks..."));
sporkManager.LoadSporksFromDB();

// LoadBlockIndex will load fTxIndex from the db, or set it if
// we're reindexing. It will also load fHavePruned if we've
// ever removed a block file from disk.
uiInterface.InitMessage(_("Loading block index..."));
std::string strBlockIndexError;
if (!LoadBlockIndex(strBlockIndexError)) {
Expand All @@ -1619,22 +1613,42 @@ bool AppInitMain()
if (!mapBlockIndex.empty() && mapBlockIndex.count(consensus.hashGenesisBlock) == 0)
return UIError(_("Incorrect or no genesis block found. Wrong datadir for network?"));

// Initialize the block index (no-op if non-empty database was already loaded)
if (!LoadGenesisBlock()) {
// Check for changed -txindex state
if (fTxIndex != gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
strLoadError = _("You need to rebuild the database using -reindex to change -txindex");
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.
// This is called again in ThreadImport in the reindex completes.
if (!fReindex && !LoadGenesisBlock()) {
strLoadError = _("Error initializing block database");
break;
}

// Check for changed -txindex state
if (fTxIndex != gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
strLoadError = _("You need to rebuild the database using -reindex to change -txindex");
// At this point we're either in reindex or we've loaded a useful
// block tree into mapBlockIndex!

pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReindex);
pcoinscatcher = new CCoinsViewErrorCatcher(pcoinsdbview);

// If necessary, upgrade from older database format.
// This is a no-op if we cleared the coinsviewdb with -reindex (or -reindex-chainstate !TODO)
uiInterface.InitMessage(_("Upgrading coins database if needed..."));
// If necessary, upgrade from older database format.
if (!pcoinsdbview->Upgrade()) {
strLoadError = _("Error upgrading chainstate database");
break;
}

// ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex (or -reindex-chainstate !TODO)
if (!ReplayBlocks(chainparams, pcoinsdbview)) {
strLoadError = _("Unable to replay blocks. You will need to rebuild the database using -reindex.");
break;
}

// The on-disk coinsdb is now in a good state, create the cache
pcoinsTip = new CCoinsViewCache(pcoinscatcher);

// !TODO: after enabling reindex-chainstate
Expand Down Expand Up @@ -1674,6 +1688,8 @@ bool AppInitMain()
}
}

// !TODO: after enabling reindex-chainstate
// if (!fReindex && !fReindexChainState) {
if (!fReindex) {
uiInterface.InitMessage(_("Verifying blocks..."));

Expand Down

0 comments on commit 5f1f014

Please sign in to comment.