diff options
author | Carl Dong <contact@carldong.me> | 2021-08-18 13:54:08 -0400 |
---|---|---|
committer | Carl Dong <contact@carldong.me> | 2021-12-06 16:41:58 -0500 |
commit | aad8d597890c3707ae96fdb2b9fadc270ca574dd (patch) | |
tree | 65d313242f40e5070adbd1026ed54753dab5dd5d | |
parent | b345979a2b03b671c0984edd7e48e0baec2e2f34 (diff) |
node/chainstate: Reduce coupling of LogPrintf
...by moving the try/catch out of LoadChainstate
I strongly recommend reviewing with the following git-diff flags:
--color-moved=dimmed_zebra --color-moved-ws=allow-indentation-change
-rw-r--r-- | src/init.cpp | 54 | ||||
-rw-r--r-- | src/node/chainstate.cpp | 10 |
2 files changed, 35 insertions, 29 deletions
diff --git a/src/init.cpp b/src/init.cpp index 80f767a431..411692d88b 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1419,20 +1419,26 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) uiInterface.InitMessage(_("Loading block index…").translated); const int64_t load_block_index_start_time = GetTimeMillis(); - auto rv = LoadChainstate(fReset, - chainman, - Assert(node.mempool.get()), - fPruneMode, - chainparams, - fReindexChainState, - nBlockTreeDBCache, - nCoinDBCache, - nCoinCacheUsage, - []() { - uiInterface.ThreadSafeMessageBox( - _("Error reading from database, shutting down."), - "", CClientUIInterface::MSG_ERROR); - }); + std::optional<ChainstateLoadingError> rv; + try { + rv = LoadChainstate(fReset, + chainman, + Assert(node.mempool.get()), + fPruneMode, + chainparams, + fReindexChainState, + nBlockTreeDBCache, + nCoinDBCache, + nCoinCacheUsage, + []() { + uiInterface.ThreadSafeMessageBox( + _("Error reading from database, shutting down."), + "", CClientUIInterface::MSG_ERROR); + }); + } catch (const std::exception& e) { + LogPrintf("%s\n", e.what()); + rv = ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED; + } if (rv.has_value()) { switch (rv.value()) { case ChainstateLoadingError::ERROR_LOADING_BLOCK_DB: @@ -1468,13 +1474,19 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) break; } } else { - uiInterface.InitMessage(_("Verifying blocks…").translated); - auto rv2 = VerifyLoadedChainstate(chainman, - fReset, - fReindexChainState, - chainparams, - args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS), - args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL)); + std::optional<ChainstateLoadVerifyError> rv2; + try { + uiInterface.InitMessage(_("Verifying blocks…").translated); + rv2 = VerifyLoadedChainstate(chainman, + fReset, + fReindexChainState, + chainparams, + args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS), + args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL)); + } catch (const std::exception& e) { + LogPrintf("%s\n", e.what()); + rv2 = ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE; + } if (rv2.has_value()) { switch (rv2.value()) { case ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE: diff --git a/src/node/chainstate.cpp b/src/node/chainstate.cpp index 36d10e99d9..99bf3d8e94 100644 --- a/src/node/chainstate.cpp +++ b/src/node/chainstate.cpp @@ -26,7 +26,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset, return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull(); }; - try { + { LOCK(cs_main); chainman.InitializeChainstate(mempool); chainman.m_total_coinstip_cache = nCoinCacheUsage; @@ -113,9 +113,6 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset, assert(chainstate->m_chain.Tip() != nullptr); } } - } catch (const std::exception& e) { - LogPrintf("%s\n", e.what()); - return ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED; } if (!fReset) { @@ -141,7 +138,7 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull(); }; - try { + { LOCK(cs_main); for (CChainState* chainstate : chainman.GetAll()) { @@ -165,9 +162,6 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage } } } - } catch (const std::exception& e) { - LogPrintf("%s\n", e.what()); - return ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE; } return std::nullopt; |