Skip to content

Commit 5f1f014

Browse files
committed
Order chainstate init more logically.
>>> 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.
1 parent 9b87537 commit 5f1f014

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/init.cpp

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,18 +1581,9 @@ bool AppInitMain()
15811581
pSporkDB = new CSporkDB(0, false, false);
15821582

15831583
pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReindex);
1584-
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReindex);
1585-
pcoinscatcher = new CCoinsViewErrorCatcher(pcoinsdbview);
15861584

15871585
if (fReindex) {
15881586
pblocktree->WriteReindexing(true);
1589-
} else {
1590-
uiInterface.InitMessage(_("Upgrading coins database..."));
1591-
// If necessary, upgrade from older database format.
1592-
if (!pcoinsdbview->Upgrade()) {
1593-
strLoadError = _("Error upgrading chainstate database");
1594-
break;
1595-
}
15961587
}
15971588

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

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

1622-
// Initialize the block index (no-op if non-empty database was already loaded)
1623-
if (!LoadGenesisBlock()) {
1616+
// Check for changed -txindex state
1617+
if (fTxIndex != gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
1618+
strLoadError = _("You need to rebuild the database using -reindex to change -txindex");
1619+
break;
1620+
}
1621+
1622+
// At this point blocktree args are consistent with what's on disk.
1623+
// If we're not mid-reindex (based on disk + args), add a genesis block on disk.
1624+
// This is called again in ThreadImport in the reindex completes.
1625+
if (!fReindex && !LoadGenesisBlock()) {
16241626
strLoadError = _("Error initializing block database");
16251627
break;
16261628
}
16271629

1628-
// Check for changed -txindex state
1629-
if (fTxIndex != gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
1630-
strLoadError = _("You need to rebuild the database using -reindex to change -txindex");
1630+
// At this point we're either in reindex or we've loaded a useful
1631+
// block tree into mapBlockIndex!
1632+
1633+
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReindex);
1634+
pcoinscatcher = new CCoinsViewErrorCatcher(pcoinsdbview);
1635+
1636+
// If necessary, upgrade from older database format.
1637+
// This is a no-op if we cleared the coinsviewdb with -reindex (or -reindex-chainstate !TODO)
1638+
uiInterface.InitMessage(_("Upgrading coins database if needed..."));
1639+
// If necessary, upgrade from older database format.
1640+
if (!pcoinsdbview->Upgrade()) {
1641+
strLoadError = _("Error upgrading chainstate database");
16311642
break;
16321643
}
16331644

1645+
// ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex (or -reindex-chainstate !TODO)
16341646
if (!ReplayBlocks(chainparams, pcoinsdbview)) {
16351647
strLoadError = _("Unable to replay blocks. You will need to rebuild the database using -reindex.");
16361648
break;
16371649
}
1650+
1651+
// The on-disk coinsdb is now in a good state, create the cache
16381652
pcoinsTip = new CCoinsViewCache(pcoinscatcher);
16391653

16401654
// !TODO: after enabling reindex-chainstate
@@ -1674,6 +1688,8 @@ bool AppInitMain()
16741688
}
16751689
}
16761690

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

0 commit comments

Comments
 (0)