Skip to content

Commit

Permalink
More user-friendly error message if UTXO DB runs ahead of block DB
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TheBlueMatt authored and random-zebra committed Mar 2, 2021
1 parent 9bcc942 commit 9b87537
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,17 @@ bool AppInitMain()
break;
}
pcoinsTip = new CCoinsViewCache(pcoinscatcher);
LoadChainTip(chainparams);

// !TODO: after enabling reindex-chainstate
// if (!fReindex && !fReindexChainState) {
if (!fReindex) {
// LoadChainTip sets chainActive based on pcoinsTip's best block
if (!LoadChainTip(chainparams)) {
strLoadError = _("Error initializing block database");
break;
}
assert(chainActive.Tip() != NULL);
}

// Populate list of invalid/fraudulent outpoints that are banned from the chain
invalid_out::LoadOutpoints();
Expand Down
20 changes: 16 additions & 4 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3517,14 +3517,25 @@ bool static LoadBlockIndexDB(std::string& strError)
return true;
}

void LoadChainTip(const CChainParams& chainparams)
bool LoadChainTip(const CChainParams& chainparams)
{
if (chainActive.Tip() && chainActive.Tip()->GetBlockHash() == pcoinsTip->GetBestBlock()) return;
if (chainActive.Tip() && chainActive.Tip()->GetBlockHash() == pcoinsTip->GetBestBlock()) return true;

if (pcoinsTip->GetBestBlock().IsNull() && mapBlockIndex.size() == 1) {
// In case we just added the genesis block, connect it now, so
// that we always have a chainActive.Tip() when we return.
LogPrintf("%s: Connecting genesis block...\n", __func__);
CValidationState state;
if (!ActivateBestChain(state)) {
return false;
}
}

// Load pointer to end of best chain
BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
if (it == mapBlockIndex.end())
return;
if (it == mapBlockIndex.end()) {
return false;
}
chainActive.SetTip(it->second);

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

CVerifyDB::CVerifyDB()
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ bool LoadGenesisBlock();
* initializing state if we're running with -reindex. */
bool LoadBlockIndex(std::string& strError);
/** Update the chain tip based on database information. */
void LoadChainTip(const CChainParams& chainparams);
bool LoadChainTip(const CChainParams& chainparams);
/** Unload database information */
void UnloadBlockIndex();
/** See whether the protocol update is enforced for connected nodes */
Expand Down

0 comments on commit 9b87537

Please sign in to comment.