Split off VerifyLoadedChainstate

This commit is contained in:
Carl Dong 2021-11-10 15:57:14 -05:00
parent adf4912d77
commit ca7c0b934d
3 changed files with 57 additions and 24 deletions

View File

@ -1427,9 +1427,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
fReindexChainState,
nBlockTreeDBCache,
nCoinDBCache,
nCoinCacheUsage,
args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS),
args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL));
nCoinCacheUsage);
if (rv.has_value()) {
switch (rv.value()) {
case ChainstateLoadingError::ERROR_LOADING_BLOCK_DB:
@ -1461,20 +1459,34 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
strLoadError = strprintf(_("Witness data for blocks after height %d requires validation. Please restart with -reindex."),
chainparams.GetConsensus().SegwitHeight);
break;
case ChainstateLoadingError::ERROR_BLOCK_FROM_FUTURE:
strLoadError = _("The block database contains a block which appears to be from the future. "
"This may be due to your computer's date and time being set incorrectly. "
"Only rebuild the block database if you are sure that your computer's date and time are correct");
break;
case ChainstateLoadingError::ERROR_CORRUPTED_BLOCK_DB:
strLoadError = _("Corrupted block database detected");
break;
case ChainstateLoadingError::SHUTDOWN_PROBED:
break;
}
} else {
fLoaded = true;
LogPrintf(" block index %15dms\n", GetTimeMillis() - load_block_index_start_time);
auto rv2 = VerifyLoadedChainstate(chainman,
fReset,
fReindexChainState,
chainparams,
args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS),
args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL));
if (rv2.has_value()) {
switch (rv2.value()) {
case ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE:
strLoadError = _("The block database contains a block which appears to be from the future. "
"This may be due to your computer's date and time being set incorrectly. "
"Only rebuild the block database if you are sure that your computer's date and time are correct");
break;
case ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB:
strLoadError = _("Corrupted block database detected");
break;
case ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE:
strLoadError = _("Error opening block database");
break;
}
} else {
fLoaded = true;
LogPrintf(" block index %15dms\n", GetTimeMillis() - load_block_index_start_time);
}
}
if (!fLoaded && !ShutdownRequested()) {

View File

@ -20,9 +20,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
bool fReindexChainState,
int64_t nBlockTreeDBCache,
int64_t nCoinDBCache,
int64_t nCoinCacheUsage,
unsigned int check_blocks,
unsigned int check_level)
int64_t nCoinCacheUsage)
{
auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
@ -131,6 +129,20 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
}
}
return std::nullopt;
}
std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManager& chainman,
bool fReset,
bool fReindexChainState,
const CChainParams& chainparams,
unsigned int check_blocks,
unsigned int check_level)
{
auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
};
try {
LOCK(cs_main);
@ -145,20 +157,20 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
const CBlockIndex* tip = chainstate->m_chain.Tip();
RPCNotifyBlockChange(tip);
if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
return ChainstateLoadingError::ERROR_BLOCK_FROM_FUTURE;
return ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE;
}
if (!CVerifyDB().VerifyDB(
*chainstate, chainparams, chainstate->CoinsDB(),
check_level,
check_blocks)) {
return ChainstateLoadingError::ERROR_CORRUPTED_BLOCK_DB;
return ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB;
}
}
}
} catch (const std::exception& e) {
LogPrintf("%s\n", e.what());
return ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
return ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE;
}
return std::nullopt;

View File

@ -22,8 +22,6 @@ enum class ChainstateLoadingError {
ERROR_LOADCHAINTIP_FAILED,
ERROR_GENERIC_BLOCKDB_OPEN_FAILED,
ERROR_BLOCKS_WITNESS_INSUFFICIENTLY_VALIDATED,
ERROR_BLOCK_FROM_FUTURE,
ERROR_CORRUPTED_BLOCK_DB,
SHUTDOWN_PROBED,
};
@ -61,8 +59,19 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
bool fReindexChainState,
int64_t nBlockTreeDBCache,
int64_t nCoinDBCache,
int64_t nCoinCacheUsage,
unsigned int check_blocks,
unsigned int check_level);
int64_t nCoinCacheUsage);
enum class ChainstateLoadVerifyError {
ERROR_BLOCK_FROM_FUTURE,
ERROR_CORRUPTED_BLOCK_DB,
ERROR_GENERIC_FAILURE,
};
std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManager& chainman,
bool fReset,
bool fReindexChainState,
const CChainParams& chainparams,
unsigned int check_blocks,
unsigned int check_level);
#endif // BITCOIN_NODE_CHAINSTATE_H