Skip to content

Commit b0f3249

Browse files
committed
More user-friendly error message if UTXO DB runs ahead of block DB
This gives 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. This also calls ActivateBestChain in case we just loaded the genesis block in LoadChainTip, avoiding relying on the ActivateBestChain in ThreadImport before continuing init process.
1 parent eda888e commit b0f3249

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/init.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,15 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
14421442
break;
14431443
}
14441444
pcoinsTip = new CCoinsViewCache(pcoinscatcher);
1445-
LoadChainTip(chainparams);
1445+
1446+
if (!fReindex && !fReindexChainState) {
1447+
// LoadChainTip sets chainActive based on pcoinsTip's best block
1448+
if (!LoadChainTip(chainparams)) {
1449+
strLoadError = _("Error initializing block database");
1450+
break;
1451+
}
1452+
assert(chainActive.Tip() != NULL);
1453+
}
14461454

14471455
if (!fReindex && chainActive.Tip() != NULL) {
14481456
uiInterface.InitMessage(_("Rewinding blocks..."));

src/validation.cpp

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

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

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

35513561
PruneBlockIndexCandidates();
@@ -3554,6 +3564,7 @@ void LoadChainTip(const CChainParams& chainparams)
35543564
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
35553565
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
35563566
GuessVerificationProgress(chainparams.TxData(), chainActive.Tip()));
3567+
return true;
35573568
}
35583569

35593570
CVerifyDB::CVerifyDB()

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ bool LoadGenesisBlock(const CChainParams& chainparams);
262262
* initializing state if we're running with -reindex. */
263263
bool LoadBlockIndex(const CChainParams& chainparams);
264264
/** Update the chain tip based on database information. */
265-
void LoadChainTip(const CChainParams& chainparams);
265+
bool LoadChainTip(const CChainParams& chainparams);
266266
/** Unload database information */
267267
void UnloadBlockIndex();
268268
/** Run an instance of the script checking thread */

0 commit comments

Comments
 (0)