mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-04 13:55:23 -05:00
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 <ryan@ofsky.org>
This commit is contained in:
parent
c1d8870ea4
commit
e9d60af988
1 changed files with 25 additions and 31 deletions
56
src/init.cpp
56
src/init.cpp
|
@ -1635,42 +1635,36 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
||||||
bool do_reindex{args.GetBoolArg("-reindex", false)};
|
bool do_reindex{args.GetBoolArg("-reindex", false)};
|
||||||
const bool do_reindex_chainstate{args.GetBoolArg("-reindex-chainstate", false)};
|
const bool do_reindex_chainstate{args.GetBoolArg("-reindex-chainstate", false)};
|
||||||
|
|
||||||
for (bool fLoaded = false; !fLoaded && !ShutdownRequested(node);) {
|
// Chainstate initialization and loading may be retried once with reindexing by GUI users
|
||||||
auto [status, error] = InitAndLoadChainstate(
|
auto [status, error] = InitAndLoadChainstate(
|
||||||
|
node,
|
||||||
|
do_reindex,
|
||||||
|
do_reindex_chainstate,
|
||||||
|
cache_sizes,
|
||||||
|
args);
|
||||||
|
if (status == ChainstateLoadStatus::FAILURE && !do_reindex && !ShutdownRequested(node)) {
|
||||||
|
// suggest a reindex
|
||||||
|
bool do_retry = uiInterface.ThreadSafeQuestion(
|
||||||
|
error + Untranslated(".\n\n") + _("Do you want to rebuild the block database now?"),
|
||||||
|
error.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
|
||||||
|
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
|
||||||
|
if (!do_retry) {
|
||||||
|
LogError("Aborted block database rebuild. Exiting.\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
do_reindex = true;
|
||||||
|
if (!Assert(node.shutdown)->reset()) {
|
||||||
|
LogError("Internal error: failed to reset shutdown signal.\n");
|
||||||
|
}
|
||||||
|
std::tie(status, error) = InitAndLoadChainstate(
|
||||||
node,
|
node,
|
||||||
do_reindex,
|
do_reindex,
|
||||||
do_reindex_chainstate,
|
do_reindex_chainstate,
|
||||||
cache_sizes,
|
cache_sizes,
|
||||||
args);
|
args);
|
||||||
|
}
|
||||||
if (status == node::ChainstateLoadStatus::FAILURE_FATAL || status == node::ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB || status == node::ChainstateLoadStatus::FAILURE_INSUFFICIENT_DBCACHE) {
|
if (status != ChainstateLoadStatus::SUCCESS && status != ChainstateLoadStatus::INTERRUPTED) {
|
||||||
return InitError(error);
|
return InitError(error);
|
||||||
}
|
|
||||||
|
|
||||||
if (status == ChainstateLoadStatus::SUCCESS) {
|
|
||||||
fLoaded = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!fLoaded && !ShutdownRequested(node)) {
|
|
||||||
// first suggest a reindex
|
|
||||||
if (!do_reindex) {
|
|
||||||
bool fRet = uiInterface.ThreadSafeQuestion(
|
|
||||||
error + Untranslated(".\n\n") + _("Do you want to rebuild the block database now?"),
|
|
||||||
error.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
|
|
||||||
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
|
|
||||||
if (fRet) {
|
|
||||||
do_reindex = true;
|
|
||||||
if (!Assert(node.shutdown)->reset()) {
|
|
||||||
LogError("Internal error: failed to reset shutdown signal.\n");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
LogError("Aborted block database rebuild. Exiting.\n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return InitError(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// As LoadBlockIndex can take several minutes, it's possible the user
|
// As LoadBlockIndex can take several minutes, it's possible the user
|
||||||
|
|
Loading…
Add table
Reference in a new issue