Skip to content

Commit 1385697

Browse files
committed
Order chainstate init more logically.
* 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 ff3a219 commit 1385697

File tree

1 file changed

+49
-33
lines changed

1 file changed

+49
-33
lines changed

src/init.cpp

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,23 +1391,19 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
13911391
delete pblocktree;
13921392

13931393
pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReindex);
1394-
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReindex || fReindexChainState);
1395-
pcoinscatcher = new CCoinsViewErrorCatcher(pcoinsdbview);
13961394

13971395
if (fReindex) {
13981396
pblocktree->WriteReindexing(true);
13991397
//If we're reindexing in prune mode, wipe away unusable block files and all undo data files
14001398
if (fPruneMode)
14011399
CleanupBlockRevFiles();
1402-
} else {
1403-
// If necessary, upgrade from older database format.
1404-
if (!pcoinsdbview->Upgrade()) {
1405-
strLoadError = _("Error upgrading chainstate database");
1406-
break;
1407-
}
14081400
}
1401+
14091402
if (fRequestShutdown) break;
14101403

1404+
// LoadBlockIndex will load fTxIndex from the db, or set it if
1405+
// we're reindexing. It will also load fHavePruned if we've
1406+
// ever removed a block file from disk.
14111407
if (!LoadBlockIndex(chainparams)) {
14121408
strLoadError = _("Error loading block database");
14131409
break;
@@ -1418,12 +1414,6 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
14181414
if (!mapBlockIndex.empty() && mapBlockIndex.count(chainparams.GetConsensus().hashGenesisBlock) == 0)
14191415
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
14201416

1421-
// Initialize the block index (no-op if non-empty database was already loaded)
1422-
if (!fReindex && !LoadGenesisBlock(chainparams)) {
1423-
strLoadError = _("Error initializing block database");
1424-
break;
1425-
}
1426-
14271417
// Check for changed -txindex state
14281418
if (fTxIndex != GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
14291419
strLoadError = _("You need to rebuild the database using -reindex-chainstate to change -txindex");
@@ -1437,10 +1427,34 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
14371427
break;
14381428
}
14391429

1430+
// At this point blocktree args are consistent with what's on disk.
1431+
// If we're not mid-reindex (based on disk + args), add a genesis block on disk.
1432+
// This is called again in ThreadImport in the reindex completes.
1433+
if (!fReindex && !LoadGenesisBlock(chainparams)) {
1434+
strLoadError = _("Error initializing block database");
1435+
break;
1436+
}
1437+
1438+
// At this point we're either in reindex or we've loaded a useful
1439+
// block tree into mapBlockIndex!
1440+
1441+
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReindex || fReindexChainState);
1442+
pcoinscatcher = new CCoinsViewErrorCatcher(pcoinsdbview);
1443+
1444+
// If necessary, upgrade from older database format.
1445+
// This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
1446+
if (!pcoinsdbview->Upgrade()) {
1447+
strLoadError = _("Error upgrading chainstate database");
1448+
break;
1449+
}
1450+
1451+
// ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
14401452
if (!ReplayBlocks(chainparams, pcoinsdbview)) {
14411453
strLoadError = _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.");
14421454
break;
14431455
}
1456+
1457+
// The on-disk coinsdb is now in a good state, create the cache
14441458
pcoinsTip = new CCoinsViewCache(pcoinscatcher);
14451459

14461460
if (!fReindex && !fReindexChainState) {
@@ -1463,28 +1477,30 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
14631477
}
14641478
}
14651479

1466-
uiInterface.InitMessage(_("Verifying blocks..."));
1467-
if (fHavePruned && GetArg("-checkblocks", DEFAULT_CHECKBLOCKS) > MIN_BLOCKS_TO_KEEP) {
1468-
LogPrintf("Prune: pruned datadir may not have more than %d blocks; only checking available blocks",
1469-
MIN_BLOCKS_TO_KEEP);
1470-
}
1480+
if (!fReindex && !fReindexChainState) {
1481+
uiInterface.InitMessage(_("Verifying blocks..."));
1482+
if (fHavePruned && GetArg("-checkblocks", DEFAULT_CHECKBLOCKS) > MIN_BLOCKS_TO_KEEP) {
1483+
LogPrintf("Prune: pruned datadir may not have more than %d blocks; only checking available blocks",
1484+
MIN_BLOCKS_TO_KEEP);
1485+
}
14711486

1472-
{
1473-
LOCK(cs_main);
1474-
CBlockIndex* tip = chainActive.Tip();
1475-
RPCNotifyBlockChange(true, tip);
1476-
if (tip && tip->nTime > GetAdjustedTime() + 2 * 60 * 60) {
1477-
strLoadError = _("The block database contains a block which appears to be from the future. "
1478-
"This may be due to your computer's date and time being set incorrectly. "
1479-
"Only rebuild the block database if you are sure that your computer's date and time are correct");
1480-
break;
1487+
{
1488+
LOCK(cs_main);
1489+
CBlockIndex* tip = chainActive.Tip();
1490+
RPCNotifyBlockChange(true, tip);
1491+
if (tip && tip->nTime > GetAdjustedTime() + 2 * 60 * 60) {
1492+
strLoadError = _("The block database contains a block which appears to be from the future. "
1493+
"This may be due to your computer's date and time being set incorrectly. "
1494+
"Only rebuild the block database if you are sure that your computer's date and time are correct");
1495+
break;
1496+
}
14811497
}
1482-
}
14831498

1484-
if (!CVerifyDB().VerifyDB(chainparams, pcoinsdbview, GetArg("-checklevel", DEFAULT_CHECKLEVEL),
1485-
GetArg("-checkblocks", DEFAULT_CHECKBLOCKS))) {
1486-
strLoadError = _("Corrupted block database detected");
1487-
break;
1499+
if (!CVerifyDB().VerifyDB(chainparams, pcoinsdbview, GetArg("-checklevel", DEFAULT_CHECKLEVEL),
1500+
GetArg("-checkblocks", DEFAULT_CHECKBLOCKS))) {
1501+
strLoadError = _("Corrupted block database detected");
1502+
break;
1503+
}
14881504
}
14891505
} catch (const std::exception& e) {
14901506
LogPrintf("%s\n", e.what());

0 commit comments

Comments
 (0)