Skip to content

Commit e9d60af

Browse files
refactor: Replace init retry for loop with if statement
The for loop has been a long standing source of confusion and bugs, both because its purpose is not clearly documented and because the body of the for loop contains a lot of logic. Co-Authored-By: Ryan Ofsky <[email protected]>
1 parent c1d8870 commit e9d60af

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

src/init.cpp

+25-31
Original file line numberDiff line numberDiff line change
@@ -1635,42 +1635,36 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16351635
bool do_reindex{args.GetBoolArg("-reindex", false)};
16361636
const bool do_reindex_chainstate{args.GetBoolArg("-reindex-chainstate", false)};
16371637

1638-
for (bool fLoaded = false; !fLoaded && !ShutdownRequested(node);) {
1639-
auto [status, error] = InitAndLoadChainstate(
1638+
// Chainstate initialization and loading may be retried once with reindexing by GUI users
1639+
auto [status, error] = InitAndLoadChainstate(
1640+
node,
1641+
do_reindex,
1642+
do_reindex_chainstate,
1643+
cache_sizes,
1644+
args);
1645+
if (status == ChainstateLoadStatus::FAILURE && !do_reindex && !ShutdownRequested(node)) {
1646+
// suggest a reindex
1647+
bool do_retry = uiInterface.ThreadSafeQuestion(
1648+
error + Untranslated(".\n\n") + _("Do you want to rebuild the block database now?"),
1649+
error.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
1650+
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
1651+
if (!do_retry) {
1652+
LogError("Aborted block database rebuild. Exiting.\n");
1653+
return false;
1654+
}
1655+
do_reindex = true;
1656+
if (!Assert(node.shutdown)->reset()) {
1657+
LogError("Internal error: failed to reset shutdown signal.\n");
1658+
}
1659+
std::tie(status, error) = InitAndLoadChainstate(
16401660
node,
16411661
do_reindex,
16421662
do_reindex_chainstate,
16431663
cache_sizes,
16441664
args);
1645-
1646-
if (status == node::ChainstateLoadStatus::FAILURE_FATAL || status == node::ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB || status == node::ChainstateLoadStatus::FAILURE_INSUFFICIENT_DBCACHE) {
1647-
return InitError(error);
1648-
}
1649-
1650-
if (status == ChainstateLoadStatus::SUCCESS) {
1651-
fLoaded = true;
1652-
}
1653-
1654-
if (!fLoaded && !ShutdownRequested(node)) {
1655-
// first suggest a reindex
1656-
if (!do_reindex) {
1657-
bool fRet = uiInterface.ThreadSafeQuestion(
1658-
error + Untranslated(".\n\n") + _("Do you want to rebuild the block database now?"),
1659-
error.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
1660-
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
1661-
if (fRet) {
1662-
do_reindex = true;
1663-
if (!Assert(node.shutdown)->reset()) {
1664-
LogError("Internal error: failed to reset shutdown signal.\n");
1665-
}
1666-
} else {
1667-
LogError("Aborted block database rebuild. Exiting.\n");
1668-
return false;
1669-
}
1670-
} else {
1671-
return InitError(error);
1672-
}
1673-
}
1665+
}
1666+
if (status != ChainstateLoadStatus::SUCCESS && status != ChainstateLoadStatus::INTERRUPTED) {
1667+
return InitError(error);
16741668
}
16751669

16761670
// As LoadBlockIndex can take several minutes, it's possible the user

0 commit comments

Comments
 (0)