Skip to content

Commit aad8d59

Browse files
committed
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
1 parent b345979 commit aad8d59

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

src/init.cpp

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,20 +1419,26 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14191419

14201420
uiInterface.InitMessage(_("Loading block index…").translated);
14211421
const int64_t load_block_index_start_time = GetTimeMillis();
1422-
auto rv = LoadChainstate(fReset,
1423-
chainman,
1424-
Assert(node.mempool.get()),
1425-
fPruneMode,
1426-
chainparams,
1427-
fReindexChainState,
1428-
nBlockTreeDBCache,
1429-
nCoinDBCache,
1430-
nCoinCacheUsage,
1431-
[]() {
1432-
uiInterface.ThreadSafeMessageBox(
1433-
_("Error reading from database, shutting down."),
1434-
"", CClientUIInterface::MSG_ERROR);
1435-
});
1422+
std::optional<ChainstateLoadingError> rv;
1423+
try {
1424+
rv = LoadChainstate(fReset,
1425+
chainman,
1426+
Assert(node.mempool.get()),
1427+
fPruneMode,
1428+
chainparams,
1429+
fReindexChainState,
1430+
nBlockTreeDBCache,
1431+
nCoinDBCache,
1432+
nCoinCacheUsage,
1433+
[]() {
1434+
uiInterface.ThreadSafeMessageBox(
1435+
_("Error reading from database, shutting down."),
1436+
"", CClientUIInterface::MSG_ERROR);
1437+
});
1438+
} catch (const std::exception& e) {
1439+
LogPrintf("%s\n", e.what());
1440+
rv = ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
1441+
}
14361442
if (rv.has_value()) {
14371443
switch (rv.value()) {
14381444
case ChainstateLoadingError::ERROR_LOADING_BLOCK_DB:
@@ -1468,13 +1474,19 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14681474
break;
14691475
}
14701476
} else {
1471-
uiInterface.InitMessage(_("Verifying blocks…").translated);
1472-
auto rv2 = VerifyLoadedChainstate(chainman,
1473-
fReset,
1474-
fReindexChainState,
1475-
chainparams,
1476-
args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS),
1477-
args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL));
1477+
std::optional<ChainstateLoadVerifyError> rv2;
1478+
try {
1479+
uiInterface.InitMessage(_("Verifying blocks…").translated);
1480+
rv2 = VerifyLoadedChainstate(chainman,
1481+
fReset,
1482+
fReindexChainState,
1483+
chainparams,
1484+
args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS),
1485+
args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL));
1486+
} catch (const std::exception& e) {
1487+
LogPrintf("%s\n", e.what());
1488+
rv2 = ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE;
1489+
}
14781490
if (rv2.has_value()) {
14791491
switch (rv2.value()) {
14801492
case ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE:

src/node/chainstate.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
2626
return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
2727
};
2828

29-
try {
29+
{
3030
LOCK(cs_main);
3131
chainman.InitializeChainstate(mempool);
3232
chainman.m_total_coinstip_cache = nCoinCacheUsage;
@@ -113,9 +113,6 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
113113
assert(chainstate->m_chain.Tip() != nullptr);
114114
}
115115
}
116-
} catch (const std::exception& e) {
117-
LogPrintf("%s\n", e.what());
118-
return ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
119116
}
120117

121118
if (!fReset) {
@@ -141,7 +138,7 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
141138
return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
142139
};
143140

144-
try {
141+
{
145142
LOCK(cs_main);
146143

147144
for (CChainState* chainstate : chainman.GetAll()) {
@@ -165,9 +162,6 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
165162
}
166163
}
167164
}
168-
} catch (const std::exception& e) {
169-
LogPrintf("%s\n", e.what());
170-
return ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE;
171165
}
172166

173167
return std::nullopt;

0 commit comments

Comments
 (0)