diff options
author | Carl Dong <contact@carldong.me> | 2021-11-10 15:57:14 -0500 |
---|---|---|
committer | Carl Dong <contact@carldong.me> | 2021-12-06 15:58:10 -0500 |
commit | ca7c0b934db68acdc410e3a82f1ed898382da2e5 (patch) | |
tree | f270e09b1d92fd1e3ac37e85e68a7f3918add8f0 | |
parent | adf4912d77496b9a243476c5944528f95641f14d (diff) | |
download | bitcoin-ca7c0b934db68acdc410e3a82f1ed898382da2e5.tar.xz |
Split off VerifyLoadedChainstate
-rw-r--r-- | src/init.cpp | 38 | ||||
-rw-r--r-- | src/node/chainstate.cpp | 24 | ||||
-rw-r--r-- | src/node/chainstate.h | 19 |
3 files changed, 57 insertions, 24 deletions
diff --git a/src/init.cpp b/src/init.cpp index 0db5464fbc..e52ea57bdb 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -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()) { diff --git a/src/node/chainstate.cpp b/src/node/chainstate.cpp index 8cc523fdd5..7dbaedb483 100644 --- a/src/node/chainstate.cpp +++ b/src/node/chainstate.cpp @@ -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; diff --git a/src/node/chainstate.h b/src/node/chainstate.h index 39621a31c5..c122391a41 100644 --- a/src/node/chainstate.h +++ b/src/node/chainstate.h @@ -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 |