diff --git a/src/init.cpp b/src/init.cpp index 0ca7f6703275..b4e841bb4603 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1943,6 +1943,7 @@ bool AppInitMain() LogPrintf("* Using %.1fMiB for in-memory UTXO set (plus up to %.1fMiB of unused mempool space)\n", nCoinCacheUsage * (1.0 / 1024 / 1024), nMempoolSizeMax * (1.0 / 1024 / 1024)); bool fLoaded = false; + bool fReconsiderInvalidTip{false}; int64_t nStart = GetTimeMillis(); while (!fLoaded && !fRequestShutdown) { @@ -2117,7 +2118,7 @@ bool AppInitMain() break; } - ResetBlockFailureFlags(nullptr); + fReconsiderInvalidTip = ResetBlockFailureFlags(nullptr); } } catch (const std::exception& e) { LogPrintf("%s\n", e.what()); @@ -2158,6 +2159,23 @@ bool AppInitMain() return false; } + if (fReconsiderInvalidTip) { + // Do this outside of ThreadImport to avoid exposing rpc while we are still reconsidering the chain + uiInterface.InitMessage(_("Trying to activate a better chain which was marked invalid previousely...")); + CValidationState state; + if (!ActivateBestChain(state, chainparams)) { + LogPrintf("Failed to connect best block (%s), exiting\n", FormatStateMessage(state)); + StartShutdown(); + return false; + } + + // ActivateBestChain can be a heavy one, see if the user requested to kill the GUI + if (fRequestShutdown) { + LogPrintf("Shutdown requested. Exiting.\n"); + return false; + } + } + fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME; CAutoFile est_filein(fsbridge::fopen(est_path, "rb"), SER_DISK, CLIENT_VERSION); // Allowed to fail as this file IS missing on first startup. diff --git a/src/validation.cpp b/src/validation.cpp index a1c0ec853585..abc0d817ccb7 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3379,11 +3379,13 @@ bool CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) { if (!pindex) { if (pindexBestInvalid && pindexBestInvalid->GetAncestor(chainActive.Height()) == chainActive.Tip()) { - LogPrintf("%s: the best known invalid block (%s) is ahead of our tip, reconsidering\n", - __func__, pindexBestInvalid->GetBlockHash().ToString()); + LogPrintf("%s: the best known invalid block (hash=%s, height=%d) is ahead of our tip (hash=%s, height=%d), reconsidering\n", __func__, + pindexBestInvalid->GetBlockHash().ToString(), pindexBestInvalid->nHeight, + chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height()); pindex = pindexBestInvalid; } else { - return true; + // nothing to do + return false; } }