Skip to content

Commit bd92424

Browse files
committed
Merge #10758: Fix some chainstate-init-order bugs.
c0025d0 Fix segfault when shutting down before fully loading (Matt Corallo) 1385697 Order chainstate init more logically. (Matt Corallo) ff3a219 Call RewindBlockIndex even if we're about to run -reindex-chainstate (Matt Corallo) b0f3249 More user-friendly error message if UTXO DB runs ahead of block DB (Matt Corallo) eda888e Fix some LoadChainTip-related init-order bugs. (Matt Corallo) Pull request description: This does a number of things to clean up chainstate init order, fixing some issues as it goes: * 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 the writing of fTxIndex to LoadBlockIndex - this fixes a bug introduced in d6af06d where InitBlockIndex was writing to fTxIndex which had not yet been checked (because LoadChainTip hadn't yet initialized the chainActive, which would otherwise have resulted in InitBlockIndex being a NOP), allowing you to modify -txindex without reindex, potentially corrupting your chainstate! * Rename InitBlockIndex to LoadGenesisBlock, which is now a more natural name for it. Also check mapBlockIndex instead of chainActive, fixing a bug where we'd write the genesis block out on every start. * 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. * Give LoadChainTip a return value - allowing it to indicate that the UTXO DB ran ahead of the block DB. This just provides a nicer error message instead of the previous mysterious assert(!setBlockIndexCandidates.empty()) error. * Calls ActivateBestChain in case we just loaded the genesis block in LoadChainTip, avoiding relying on the ActivateBestChain in ThreadImport before continuing init process. * 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. Tree-SHA512: 3c96ee7ed44f4130bee3479a40c5cd99a619fda5e309c26d60b54feab9f6ec60fabab8cf47a049c9cf15e88999b2edb7f16cbe6819e97273560b201a89d90762
2 parents 42307c4 + c0025d0 commit bd92424

File tree

4 files changed

+139
-76
lines changed

4 files changed

+139
-76
lines changed

src/init.cpp

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ void Shutdown()
216216
}
217217

218218
// FlushStateToDisk generates a SetBestChain callback, which we should avoid missing
219-
FlushStateToDisk();
219+
if (pcoinsTip != nullptr) {
220+
FlushStateToDisk();
221+
}
220222

221223
// After there are no more peers/RPC left to give us new data which may generate
222224
// CValidationInterface callbacks, flush them...
@@ -646,7 +648,7 @@ void ThreadImport(std::vector<fs::path> vImportFiles)
646648
fReindex = false;
647649
LogPrintf("Reindexing finished\n");
648650
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
649-
InitBlockIndex(chainparams);
651+
LoadGenesisBlock(chainparams);
650652
}
651653

652654
// hardcoded $DATADIR/bootstrap.dat
@@ -1399,23 +1401,19 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
13991401
delete pblocktree;
14001402

14011403
pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReindex);
1402-
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReindex || fReindexChainState);
1403-
pcoinscatcher = new CCoinsViewErrorCatcher(pcoinsdbview);
14041404

14051405
if (fReindex) {
14061406
pblocktree->WriteReindexing(true);
14071407
//If we're reindexing in prune mode, wipe away unusable block files and all undo data files
14081408
if (fPruneMode)
14091409
CleanupBlockRevFiles();
1410-
} else {
1411-
// If necessary, upgrade from older database format.
1412-
if (!pcoinsdbview->Upgrade()) {
1413-
strLoadError = _("Error upgrading chainstate database");
1414-
break;
1415-
}
14161410
}
1411+
14171412
if (fRequestShutdown) break;
14181413

1414+
// LoadBlockIndex will load fTxIndex from the db, or set it if
1415+
// we're reindexing. It will also load fHavePruned if we've
1416+
// ever removed a block file from disk.
14191417
if (!LoadBlockIndex(chainparams)) {
14201418
strLoadError = _("Error loading block database");
14211419
break;
@@ -1426,12 +1424,6 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
14261424
if (!mapBlockIndex.empty() && mapBlockIndex.count(chainparams.GetConsensus().hashGenesisBlock) == 0)
14271425
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
14281426

1429-
// Initialize the block index (no-op if non-empty database was already loaded)
1430-
if (!InitBlockIndex(chainparams)) {
1431-
strLoadError = _("Error initializing block database");
1432-
break;
1433-
}
1434-
14351427
// Check for changed -txindex state
14361428
if (fTxIndex != GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
14371429
strLoadError = _("You need to rebuild the database using -reindex-chainstate to change -txindex");
@@ -1445,43 +1437,80 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
14451437
break;
14461438
}
14471439

1440+
// At this point blocktree args are consistent with what's on disk.
1441+
// If we're not mid-reindex (based on disk + args), add a genesis block on disk.
1442+
// This is called again in ThreadImport in the reindex completes.
1443+
if (!fReindex && !LoadGenesisBlock(chainparams)) {
1444+
strLoadError = _("Error initializing block database");
1445+
break;
1446+
}
1447+
1448+
// At this point we're either in reindex or we've loaded a useful
1449+
// block tree into mapBlockIndex!
1450+
1451+
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReindex || fReindexChainState);
1452+
pcoinscatcher = new CCoinsViewErrorCatcher(pcoinsdbview);
1453+
1454+
// If necessary, upgrade from older database format.
1455+
// This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
1456+
if (!pcoinsdbview->Upgrade()) {
1457+
strLoadError = _("Error upgrading chainstate database");
1458+
break;
1459+
}
1460+
1461+
// ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
14481462
if (!ReplayBlocks(chainparams, pcoinsdbview)) {
14491463
strLoadError = _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.");
14501464
break;
14511465
}
1466+
1467+
// The on-disk coinsdb is now in a good state, create the cache
14521468
pcoinsTip = new CCoinsViewCache(pcoinscatcher);
1453-
LoadChainTip(chainparams);
14541469

1455-
if (!fReindex && chainActive.Tip() != NULL) {
1470+
if (!fReindex && !fReindexChainState) {
1471+
// LoadChainTip sets chainActive based on pcoinsTip's best block
1472+
if (!LoadChainTip(chainparams)) {
1473+
strLoadError = _("Error initializing block database");
1474+
break;
1475+
}
1476+
assert(chainActive.Tip() != NULL);
1477+
}
1478+
1479+
if (!fReindex) {
1480+
// Note that RewindBlockIndex MUST run even if we're about to -reindex-chainstate.
1481+
// It both disconnects blocks based on chainActive, and drops block data in
1482+
// mapBlockIndex based on lack of available witness data.
14561483
uiInterface.InitMessage(_("Rewinding blocks..."));
14571484
if (!RewindBlockIndex(chainparams)) {
14581485
strLoadError = _("Unable to rewind the database to a pre-fork state. You will need to redownload the blockchain");
14591486
break;
14601487
}
14611488
}
14621489

1463-
uiInterface.InitMessage(_("Verifying blocks..."));
1464-
if (fHavePruned && GetArg("-checkblocks", DEFAULT_CHECKBLOCKS) > MIN_BLOCKS_TO_KEEP) {
1465-
LogPrintf("Prune: pruned datadir may not have more than %d blocks; only checking available blocks",
1466-
MIN_BLOCKS_TO_KEEP);
1467-
}
1490+
if (!fReindex && !fReindexChainState) {
1491+
uiInterface.InitMessage(_("Verifying blocks..."));
1492+
if (fHavePruned && GetArg("-checkblocks", DEFAULT_CHECKBLOCKS) > MIN_BLOCKS_TO_KEEP) {
1493+
LogPrintf("Prune: pruned datadir may not have more than %d blocks; only checking available blocks",
1494+
MIN_BLOCKS_TO_KEEP);
1495+
}
14681496

1469-
{
1470-
LOCK(cs_main);
1471-
CBlockIndex* tip = chainActive.Tip();
1472-
RPCNotifyBlockChange(true, tip);
1473-
if (tip && tip->nTime > GetAdjustedTime() + 2 * 60 * 60) {
1474-
strLoadError = _("The block database contains a block which appears to be from the future. "
1475-
"This may be due to your computer's date and time being set incorrectly. "
1476-
"Only rebuild the block database if you are sure that your computer's date and time are correct");
1477-
break;
1497+
{
1498+
LOCK(cs_main);
1499+
CBlockIndex* tip = chainActive.Tip();
1500+
RPCNotifyBlockChange(true, tip);
1501+
if (tip && tip->nTime > GetAdjustedTime() + 2 * 60 * 60) {
1502+
strLoadError = _("The block database contains a block which appears to be from the future. "
1503+
"This may be due to your computer's date and time being set incorrectly. "
1504+
"Only rebuild the block database if you are sure that your computer's date and time are correct");
1505+
break;
1506+
}
14781507
}
1479-
}
14801508

1481-
if (!CVerifyDB().VerifyDB(chainparams, pcoinsdbview, GetArg("-checklevel", DEFAULT_CHECKLEVEL),
1482-
GetArg("-checkblocks", DEFAULT_CHECKBLOCKS))) {
1483-
strLoadError = _("Corrupted block database detected");
1484-
break;
1509+
if (!CVerifyDB().VerifyDB(chainparams, pcoinsdbview, GetArg("-checklevel", DEFAULT_CHECKLEVEL),
1510+
GetArg("-checkblocks", DEFAULT_CHECKBLOCKS))) {
1511+
strLoadError = _("Corrupted block database detected");
1512+
break;
1513+
}
14851514
}
14861515
} catch (const std::exception& e) {
14871516
LogPrintf("%s\n", e.what());

src/test/test_bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
7474
pblocktree = new CBlockTreeDB(1 << 20, true);
7575
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
7676
pcoinsTip = new CCoinsViewCache(pcoinsdbview);
77-
if (!InitBlockIndex(chainparams)) {
77+
if (!LoadGenesisBlock(chainparams)) {
7878
throw std::runtime_error("InitBlockIndex failed.");
7979
}
8080
{

src/validation.cpp

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,14 +3539,24 @@ bool static LoadBlockIndexDB(const CChainParams& chainparams)
35393539
return true;
35403540
}
35413541

3542-
void LoadChainTip(const CChainParams& chainparams)
3542+
bool LoadChainTip(const CChainParams& chainparams)
35433543
{
3544-
if (chainActive.Tip() && chainActive.Tip()->GetBlockHash() == pcoinsTip->GetBestBlock()) return;
3544+
if (chainActive.Tip() && chainActive.Tip()->GetBlockHash() == pcoinsTip->GetBestBlock()) return true;
3545+
3546+
if (pcoinsTip->GetBestBlock().IsNull() && mapBlockIndex.size() == 1) {
3547+
// In case we just added the genesis block, connect it now, so
3548+
// that we always have a chainActive.Tip() when we return.
3549+
LogPrintf("%s: Connecting genesis block...\n", __func__);
3550+
CValidationState state;
3551+
if (!ActivateBestChain(state, chainparams)) {
3552+
return false;
3553+
}
3554+
}
35453555

35463556
// Load pointer to end of best chain
35473557
BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
35483558
if (it == mapBlockIndex.end())
3549-
return;
3559+
return false;
35503560
chainActive.SetTip(it->second);
35513561

35523562
PruneBlockIndexCandidates();
@@ -3555,6 +3565,7 @@ void LoadChainTip(const CChainParams& chainparams)
35553565
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
35563566
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
35573567
GuessVerificationProgress(chainparams.TxData(), chainActive.Tip()));
3568+
return true;
35583569
}
35593570

35603571
CVerifyDB::CVerifyDB()
@@ -3751,6 +3762,8 @@ bool RewindBlockIndex(const CChainParams& params)
37513762
{
37523763
LOCK(cs_main);
37533764

3765+
// Note that during -reindex-chainstate we are called with an empty chainActive!
3766+
37543767
int nHeight = 1;
37553768
while (nHeight <= chainActive.Height()) {
37563769
if (IsWitnessEnabled(chainActive[nHeight - 1], params.GetConsensus()) && !(chainActive[nHeight]->nStatus & BLOCK_OPT_WITNESS)) {
@@ -3820,12 +3833,19 @@ bool RewindBlockIndex(const CChainParams& params)
38203833
}
38213834
}
38223835

3823-
PruneBlockIndexCandidates();
3836+
if (chainActive.Tip() != NULL) {
3837+
// We can't prune block index candidates based on our tip if we have
3838+
// no tip due to chainActive being empty!
3839+
PruneBlockIndexCandidates();
38243840

3825-
CheckBlockIndex(params.GetConsensus());
3841+
CheckBlockIndex(params.GetConsensus());
38263842

3827-
if (!FlushStateToDisk(params, state, FLUSH_STATE_ALWAYS)) {
3828-
return false;
3843+
// FlushStateToDisk can possibly read chainActive. Be conservative
3844+
// and skip it here, we're about to -reindex-chainstate anyway, so
3845+
// it'll get called a bunch real soon.
3846+
if (!FlushStateToDisk(params, state, FLUSH_STATE_ALWAYS)) {
3847+
return false;
3848+
}
38293849
}
38303850

38313851
return true;
@@ -3863,42 +3883,55 @@ void UnloadBlockIndex()
38633883
bool LoadBlockIndex(const CChainParams& chainparams)
38643884
{
38653885
// Load block index from databases
3866-
if (!fReindex && !LoadBlockIndexDB(chainparams))
3867-
return false;
3886+
bool needs_init = fReindex;
3887+
if (!fReindex) {
3888+
bool ret = LoadBlockIndexDB(chainparams);
3889+
if (!ret) return false;
3890+
needs_init = mapBlockIndex.empty();
3891+
}
3892+
3893+
if (needs_init) {
3894+
// Everything here is for *new* reindex/DBs. Thus, though
3895+
// LoadBlockIndexDB may have set fReindex if we shut down
3896+
// mid-reindex previously, we don't check fReindex and
3897+
// instead only check it prior to LoadBlockIndexDB to set
3898+
// needs_init.
3899+
3900+
LogPrintf("Initializing databases...\n");
3901+
// Use the provided setting for -txindex in the new database
3902+
fTxIndex = GetBoolArg("-txindex", DEFAULT_TXINDEX);
3903+
pblocktree->WriteFlag("txindex", fTxIndex);
3904+
}
38683905
return true;
38693906
}
38703907

3871-
bool InitBlockIndex(const CChainParams& chainparams)
3908+
bool LoadGenesisBlock(const CChainParams& chainparams)
38723909
{
38733910
LOCK(cs_main);
38743911

3875-
// Check whether we're already initialized
3876-
if (chainActive.Genesis() != NULL)
3912+
// Check whether we're already initialized by checking for genesis in
3913+
// mapBlockIndex. Note that we can't use chainActive here, since it is
3914+
// set based on the coins db, not the block index db, which is the only
3915+
// thing loaded at this point.
3916+
if (mapBlockIndex.count(chainparams.GenesisBlock().GetHash()))
38773917
return true;
38783918

3879-
// Use the provided setting for -txindex in the new database
3880-
fTxIndex = GetBoolArg("-txindex", DEFAULT_TXINDEX);
3881-
pblocktree->WriteFlag("txindex", fTxIndex);
3882-
LogPrintf("Initializing databases...\n");
3883-
38843919
// Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
3885-
if (!fReindex) {
3886-
try {
3887-
CBlock &block = const_cast<CBlock&>(chainparams.GenesisBlock());
3888-
// Start new block file
3889-
unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
3890-
CDiskBlockPos blockPos;
3891-
CValidationState state;
3892-
if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.GetBlockTime()))
3893-
return error("LoadBlockIndex(): FindBlockPos failed");
3894-
if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
3895-
return error("LoadBlockIndex(): writing genesis block to disk failed");
3896-
CBlockIndex *pindex = AddToBlockIndex(block);
3897-
if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
3898-
return error("LoadBlockIndex(): genesis block not accepted");
3899-
} catch (const std::runtime_error& e) {
3900-
return error("LoadBlockIndex(): failed to initialize block database: %s", e.what());
3901-
}
3920+
try {
3921+
CBlock &block = const_cast<CBlock&>(chainparams.GenesisBlock());
3922+
// Start new block file
3923+
unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
3924+
CDiskBlockPos blockPos;
3925+
CValidationState state;
3926+
if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.GetBlockTime()))
3927+
return error("%s: FindBlockPos failed", __func__);
3928+
if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
3929+
return error("%s: writing genesis block to disk failed", __func__);
3930+
CBlockIndex *pindex = AddToBlockIndex(block);
3931+
if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
3932+
return error("%s: genesis block not accepted", __func__);
3933+
} catch (const std::runtime_error& e) {
3934+
return error("%s: failed to write genesis block: %s", __func__, e.what());
39023935
}
39033936

39043937
return true;

src/validation.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,13 @@ FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly = false);
256256
fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix);
257257
/** Import blocks from an external file */
258258
bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp = NULL);
259-
/** Initialize a new block tree database + block data on disk */
260-
bool InitBlockIndex(const CChainParams& chainparams);
261-
/** Load the block tree and coins database from disk */
259+
/** Ensures we have a genesis block in the block tree, possibly writing one to disk. */
260+
bool LoadGenesisBlock(const CChainParams& chainparams);
261+
/** Load the block tree and coins database from disk,
262+
* initializing state if we're running with -reindex. */
262263
bool LoadBlockIndex(const CChainParams& chainparams);
263264
/** Update the chain tip based on database information. */
264-
void LoadChainTip(const CChainParams& chainparams);
265+
bool LoadChainTip(const CChainParams& chainparams);
265266
/** Unload database information */
266267
void UnloadBlockIndex();
267268
/** Run an instance of the script checking thread */

0 commit comments

Comments
 (0)