Skip to content

Commit

Permalink
Fix '-reindex' issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkLTZ committed Aug 26, 2024
1 parent 3e3d9ba commit b43243c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
21 changes: 10 additions & 11 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,8 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += HelpMessageOpt("-prune=<n>", 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
Expand Down Expand Up @@ -621,7 +616,7 @@ void ThreadStartWalletNotifier()
if (pwalletMain)
{
std::optional<uint256> walletBestBlockHash;
if (!fReindex) {
{
LOCK(pwalletMain->cs_wallet);
walletBestBlockHash = pwalletMain->GetPersistedBestBlock();
}
Expand Down Expand Up @@ -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))
Expand All @@ -939,6 +935,7 @@ void InitParameterInteraction()
if (SoftSetBoolArg("-rescan", true))
LogPrintf("%s: parameter interaction: -zapwallettxes=<mode> -> setting -rescan=1\n", __func__);
}
#endif
}

void InitLogging()
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<boost::filesystem::path> vImportFiles;
if (mapArgs.count("-loadblock"))
{
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
{
Expand Down Expand Up @@ -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&) {
Expand Down
5 changes: 1 addition & 4 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4762,7 +4762,7 @@ std::string CWallet::GetWalletHelpString(bool showDebug)
strUsage += HelpMessageOpt("-paytxfee=<amt>", 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=<n>", strprintf(_("If paytxfee is not set, include enough fee so transactions begin confirmation on average within n blocks (default: %u)"), DEFAULT_TX_CONFIRM_TARGET));
Expand Down Expand Up @@ -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))
{
Expand Down

0 comments on commit b43243c

Please sign in to comment.