From d3345eeccc5fd972635eef6051bbf81cc7e0406e Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Thu, 3 Oct 2024 08:57:52 +0000 Subject: [PATCH] node/chainstate: Reduce coupling of LogPrintf --- src/init.cpp | 94 +++++++++++++++++++++++------------------ src/node/chainstate.cpp | 10 +---- 2 files changed, 55 insertions(+), 49 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index d58ef908ca74b..007b64113b6d4 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1865,39 +1865,45 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) uiInterface.InitMessage(_("Loading block index…").translated); const auto load_block_index_start_time{SteadyClock::now()}; - auto rv = LoadChainstate(fReset, - chainman, - *node.govman, - *node.mn_metaman, - *node.mn_sync, - *node.sporkman, - node.mn_activeman, - node.chain_helper, - node.cpoolman, - node.dmnman, - node.evodb, - node.mnhf_manager, - llmq::chainLocksHandler, - llmq::quorumInstantSendManager, - llmq::quorumSnapshotManager, - node.llmq_ctx, - Assert(node.mempool.get()), - fPruneMode, - args.GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX), - is_governance_enabled, - args.GetBoolArg("-spentindex", DEFAULT_SPENTINDEX), - args.GetBoolArg("-timestampindex", DEFAULT_TIMESTAMPINDEX), - args.GetBoolArg("-txindex", DEFAULT_TXINDEX), - chainparams, - fReindexChainState, - nBlockTreeDBCache, - nCoinDBCache, - nCoinCacheUsage, - []() { - uiInterface.ThreadSafeMessageBox( - _("Error reading from database, shutting down."), - "", CClientUIInterface::MSG_ERROR); - }); + std::optional rv; + try { + rv = LoadChainstate(fReset, + chainman, + *node.govman, + *node.mn_metaman, + *node.mn_sync, + *node.sporkman, + node.mn_activeman, + node.chain_helper, + node.cpoolman, + node.dmnman, + node.evodb, + node.mnhf_manager, + llmq::chainLocksHandler, + llmq::quorumInstantSendManager, + llmq::quorumSnapshotManager, + node.llmq_ctx, + Assert(node.mempool.get()), + fPruneMode, + args.GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX), + is_governance_enabled, + args.GetBoolArg("-spentindex", DEFAULT_SPENTINDEX), + args.GetBoolArg("-timestampindex", DEFAULT_TIMESTAMPINDEX), + args.GetBoolArg("-txindex", DEFAULT_TXINDEX), + 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: @@ -1951,14 +1957,20 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) break; } } else { - uiInterface.InitMessage(_("Verifying blocks…").translated); - auto rv2 = VerifyLoadedChainstate(chainman, - *Assert(node.evodb.get()), - fReset, - fReindexChainState, - chainparams, - args.GetArg("-checkblocks", DEFAULT_CHECKBLOCKS), - args.GetArg("-checklevel", DEFAULT_CHECKLEVEL)); + std::optional rv2; + try { + uiInterface.InitMessage(_("Verifying blocks…").translated); + rv2 = VerifyLoadedChainstate(chainman, + *Assert(node.evodb.get()), + fReset, + fReindexChainState, + chainparams, + args.GetArg("-checkblocks", DEFAULT_CHECKBLOCKS), + args.GetArg("-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 4562d750b5098..35165200faeae 100644 --- a/src/node/chainstate.cpp +++ b/src/node/chainstate.cpp @@ -56,7 +56,7 @@ std::optional LoadChainstate(bool fReset, return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull(); }; - try { + { LOCK(cs_main); int64_t nEvoDbCache{64 * 1024 * 1024}; // TODO @@ -223,9 +223,6 @@ std::optional LoadChainstate(bool fReset, if (!mnhf_manager->ForceSignalDBUpdate()) { return ChainstateLoadingError::ERROR_UPGRADING_SIGNALS_DB; } - } catch (const std::exception& e) { - LogPrintf("%s\n", e.what()); - return ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED; } return std::nullopt; @@ -243,7 +240,7 @@ std::optional VerifyLoadedChainstate(ChainstateManage return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull(); }; - try { + { LOCK(cs_main); for (CChainState* chainstate : chainman.GetAll()) { @@ -293,9 +290,6 @@ std::optional VerifyLoadedChainstate(ChainstateManage } } } - } catch (const std::exception& e) { - LogPrintf("%s\n", e.what()); - return ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE; } return std::nullopt;