Skip to content

Commit 9b87537

Browse files
TheBlueMattrandom-zebra
authored andcommitted
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 9bcc942 commit 9b87537

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/init.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,17 @@ bool AppInitMain()
16361636
break;
16371637
}
16381638
pcoinsTip = new CCoinsViewCache(pcoinscatcher);
1639-
LoadChainTip(chainparams);
1639+
1640+
// !TODO: after enabling reindex-chainstate
1641+
// if (!fReindex && !fReindexChainState) {
1642+
if (!fReindex) {
1643+
// LoadChainTip sets chainActive based on pcoinsTip's best block
1644+
if (!LoadChainTip(chainparams)) {
1645+
strLoadError = _("Error initializing block database");
1646+
break;
1647+
}
1648+
assert(chainActive.Tip() != NULL);
1649+
}
16401650

16411651
// Populate list of invalid/fraudulent outpoints that are banned from the chain
16421652
invalid_out::LoadOutpoints();

src/validation.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3517,14 +3517,25 @@ bool static LoadBlockIndexDB(std::string& strError)
35173517
return true;
35183518
}
35193519

3520-
void LoadChainTip(const CChainParams& chainparams)
3520+
bool LoadChainTip(const CChainParams& chainparams)
35213521
{
3522-
if (chainActive.Tip() && chainActive.Tip()->GetBlockHash() == pcoinsTip->GetBestBlock()) return;
3522+
if (chainActive.Tip() && chainActive.Tip()->GetBlockHash() == pcoinsTip->GetBestBlock()) return true;
3523+
3524+
if (pcoinsTip->GetBestBlock().IsNull() && mapBlockIndex.size() == 1) {
3525+
// In case we just added the genesis block, connect it now, so
3526+
// that we always have a chainActive.Tip() when we return.
3527+
LogPrintf("%s: Connecting genesis block...\n", __func__);
3528+
CValidationState state;
3529+
if (!ActivateBestChain(state)) {
3530+
return false;
3531+
}
3532+
}
35233533

35243534
// Load pointer to end of best chain
35253535
BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
3526-
if (it == mapBlockIndex.end())
3527-
return;
3536+
if (it == mapBlockIndex.end()) {
3537+
return false;
3538+
}
35283539
chainActive.SetTip(it->second);
35293540

35303541
PruneBlockIndexCandidates();
@@ -3535,6 +3546,7 @@ void LoadChainTip(const CChainParams& chainparams)
35353546
pChainTip->GetBlockHash().GetHex(), pChainTip->nHeight,
35363547
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", pChainTip->GetBlockTime()),
35373548
Checkpoints::GuessVerificationProgress(pChainTip));
3549+
return true;
35383550
}
35393551

35403552
CVerifyDB::CVerifyDB()

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ bool LoadGenesisBlock();
189189
* initializing state if we're running with -reindex. */
190190
bool LoadBlockIndex(std::string& strError);
191191
/** Update the chain tip based on database information. */
192-
void LoadChainTip(const CChainParams& chainparams);
192+
bool LoadChainTip(const CChainParams& chainparams);
193193
/** Unload database information */
194194
void UnloadBlockIndex();
195195
/** See whether the protocol update is enforced for connected nodes */

0 commit comments

Comments
 (0)