Skip to content

Commit

Permalink
Split off VerifyLoadedChainstate
Browse files Browse the repository at this point in the history
  • Loading branch information
dongcarl committed Dec 6, 2021
1 parent adf4912 commit ca7c0b9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
38 changes: 25 additions & 13 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()) {
Expand Down
24 changes: 18 additions & 6 deletions src/node/chainstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down
19 changes: 14 additions & 5 deletions src/node/chainstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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

0 comments on commit ca7c0b9

Please sign in to comment.