From b43243cd0bbcde47dcae646b056b29618d5dfe53 Mon Sep 17 00:00:00 2001 From: MarkLTZ Date: Sun, 25 Aug 2024 13:54:14 +0000 Subject: [PATCH] Fix '-reindex' issues --- src/init.cpp | 21 ++++++++++----------- src/main.cpp | 2 +- src/wallet/wallet.cpp | 5 +---- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index f176a20d5..738887b8f 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -367,13 +367,8 @@ std::string HelpMessage(HelpMessageMode mode) strUsage += HelpMessageOpt("-prune=", strprintf(_("Reduce storage requirements by pruning (deleting) old blocks. This mode disables wallet support and is incompatible with -txindex. " "Warning: Reverting this setting requires re-downloading the entire blockchain. " "(default: 0 = disable pruning blocks, >%u = target size in MiB to use for block files)"), MIN_DISK_SPACE_FOR_BLOCK_FILES / 1024 / 1024)); -#ifdef ENABLE_WALLET - strUsage += HelpMessageOpt("-reindex-chainstate", _("Rebuild chain state from the currently indexed blocks (implies -rescan)")); - strUsage += HelpMessageOpt("-reindex", _("Rebuild chain state and block index from the blk*.dat files on disk (implies -rescan)")); -#else strUsage += HelpMessageOpt("-reindex-chainstate", _("Rebuild chain state from the currently indexed blocks")); strUsage += HelpMessageOpt("-reindex", _("Rebuild chain state and block index from the blk*.dat files on disk")); -#endif #ifndef WIN32 strUsage += HelpMessageOpt("-sysperms", _("Create new files with system default permissions, instead of umask 077 (only effective with disabled wallet functionality)")); #endif @@ -621,7 +616,7 @@ void ThreadStartWalletNotifier() if (pwalletMain) { std::optional walletBestBlockHash; - if (!fReindex) { + { LOCK(pwalletMain->cs_wallet); walletBestBlockHash = pwalletMain->GetPersistedBestBlock(); } @@ -928,6 +923,7 @@ void InitParameterInteraction() LogPrintf("%s: parameter interaction: -externalip set -> setting -discover=0\n", __func__); } +#ifdef ENABLE_WALLET if (GetBoolArg("-salvagewallet", false)) { // Rewrite just private keys: rescan to find transactions if (SoftSetBoolArg("-rescan", true)) @@ -939,6 +935,7 @@ void InitParameterInteraction() if (SoftSetBoolArg("-rescan", true)) LogPrintf("%s: parameter interaction: -zapwallettxes= -> setting -rescan=1\n", __func__); } +#endif } void InitLogging() @@ -1523,9 +1520,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) // Check for changed -txindex state if (fTxIndex != GetBoolArg("-txindex", DEFAULT_TXINDEX)) { - // TODO: Recommend `-reindex-chainstate` instead of - // `-reindex` after #5964 and/or #5977 are fixed. - strLoadError = _("You need to rebuild the database using -reindex to change -txindex"); + strLoadError = _("You need to rebuild the database using -reindex-chainstate to change -txindex"); break; } @@ -1593,8 +1588,6 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) if (!fReset) { bool fRet = uiInterface.ThreadSafeQuestion( strLoadError + ".\n\n" + _("Do you want to rebuild the block database now?"), - // TODO: Recommend `-reindex or -reindex-chainstate` after - // #5964 and/or #5977 are fixed. strLoadError + ".\nPlease restart with -reindex to recover.", "", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT); if (fRet) { @@ -1711,6 +1704,12 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) if (mapArgs.count("-txexpirynotify")) uiInterface.NotifyTxExpiration.connect(TxExpiryNotifyCallback); + uiInterface.InitMessage(_("Activating best chain...")); + // scan for better chains in the block chain database, that are not yet connected in the active best chain + CValidationState state; + if (!ActivateBestChain(state, chainparams)) + strErrors << "Failed to connect best block"; + std::vector vImportFiles; if (mapArgs.count("-loadblock")) { diff --git a/src/main.cpp b/src/main.cpp index 42097bdb9..dc715ff80 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4879,6 +4879,7 @@ bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskB { LogPrint("reindex", "%s: Processing out of order child %s of %s\n", __func__, block.GetHash().ToString(), head.ToString()); + LOCK(cs_main); CValidationState dummy; if (AcceptBlock(block, dummy, chainparams, NULL, true, &(range.first->second))) { @@ -5036,7 +5037,6 @@ void static CheckBlockIndex(const Consensus::Params& consensusParams) } } } - // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow // try { // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow // } catch (const runtime_error&) { diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index c0974fac8..e2c03ba26 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4762,7 +4762,7 @@ std::string CWallet::GetWalletHelpString(bool showDebug) strUsage += HelpMessageOpt("-paytxfee=", strprintf(_("Fee (in %s/kB) to add to transactions you send (default: %s)"), CURRENCY_UNIT, FormatMoney(payTxFee.GetFeePerK()))); strUsage += HelpMessageOpt("-rescan", _("Rescan the block chain for missing wallet transactions on startup")); - strUsage += HelpMessageOpt("-salvagewallet", _("Attempt to recover private keys from a corrupt wallet on startup (implies -rescan)")); + strUsage += HelpMessageOpt("-salvagewallet", _("Attempt to recover private keys from a corrupt wallet on startup")); strUsage += HelpMessageOpt("-sendfreetransactions", strprintf(_("Send transactions as zero-fee transactions if possible (default: %u)"), DEFAULT_SEND_FREE_TRANSACTIONS)); strUsage += HelpMessageOpt("-spendzeroconfchange", strprintf(_("Spend unconfirmed change when sending transactions (default: %u)"), DEFAULT_SPEND_ZEROCONF_CHANGE)); strUsage += HelpMessageOpt("-txconfirmtarget=", strprintf(_("If paytxfee is not set, include enough fee so transactions begin confirmation on average within n blocks (default: %u)"), DEFAULT_TX_CONFIRM_TARGET)); @@ -4881,9 +4881,6 @@ bool CWallet::InitLoadWallet(bool clearWitnessCaches) RegisterValidationInterface(walletInstance); - // chainActive.Genesis() may return null; in this case, we want rescanning - // to happen automatically as a consequence of the genesis block (and subsequent - // blocks) being added to the chain. CBlockIndex *pindexRescan = chainActive.Tip(); if (clearWitnessCaches || GetBoolArg("-rescan", false)) {