Skip to content

Commit

Permalink
Add reindex=auto flag to automatically reindex corrupt data
Browse files Browse the repository at this point in the history
This PR allows the reindex flag to be set to auto, which automatically starts a reindex if the chain state or block index are corrupt.
This can be especially useful for  Raspberry Pi based full nodes, which often experience power outages or similar issues which can corrupt data.
It allows full node operators to make Bitcoin Core reindex automatically, without having to worry about removing the reindex flag again. (Which isn't much effort, but can be annoying to forget)

Github-Pull: bitcoin#22072
Rebased-From: 602f4da
  • Loading branch information
AaronDewes authored and luke-jr committed Jun 25, 2021
1 parent 831675c commit 291ac87
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ void SetupServerArgs(NodeContext& node)
argsman.AddArg("-prune=<n>", strprintf("Reduce storage requirements by enabling pruning (deleting) of old blocks. This allows the pruneblockchain RPC to be called to delete specific blocks, and enables automatic pruning of old blocks if a target size in MiB is provided. This mode is incompatible with -txindex and -rescan. "
"Warning: Reverting this setting requires re-downloading the entire blockchain. "
"(default: 0 = disable pruning blocks, 1 = allow manual pruning via RPC, >=%u = automatically prune block files to stay under the specified target size in MiB)", MIN_DISK_SPACE_FOR_BLOCK_FILES / 1024 / 1024), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-reindex", "Rebuild chain state and block index from the blk*.dat files on disk", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-reindex", "Rebuild chain state and block index from the blk*.dat files on disk. Setting this to auto automatically reindexes the block database if it is corrupted.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-reindex-chainstate", "Rebuild chain state from the currently indexed blocks. When in pruning mode or if blocks on disk might be corrupted, use full -reindex instead.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-settings=<file>", strprintf("Specify path to dynamic settings data file. Can be disabled with -nosettings. File is written at runtime and not meant to be edited by users (use %s instead for custom settings). Relative paths will be prefixed by datadir location. (default: %s)", BITCOIN_CONF_FILENAME, BITCOIN_SETTINGS_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
#if HAVE_SYSTEM
Expand Down Expand Up @@ -1534,6 +1534,8 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA

// ********************************************************* Step 7: load block chain

// If reindex=auto, then this returns false, which is intentional,
// because we check for auto only if corruption is detected
fReindex = args.GetBoolArg("-reindex", false);
bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false);

Expand Down Expand Up @@ -1765,12 +1767,20 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
} while(false);

if (!fLoaded && !ShutdownRequested()) {
// first suggest a reindex
// If reindex=auto, directly start the reindex
bool fAutoReindex = (args.GetArg("-reindex", "0") == "auto");
if (!fReset) {
bool fRet = uiInterface.ThreadSafeQuestion(
strLoadError + Untranslated(".\n\n") + _("Do you want to rebuild the block database now?"),
strLoadError.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
bool fRet;
if(!fAutoReindex) {
// suggest a reindex to GUI users
fRet = uiInterface.ThreadSafeQuestion(
strLoadError + Untranslated(".\n\n") + _("Do you want to rebuild the block database now?"),
strLoadError.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
} else {
LogPrintf("Automatically running a reindex.\n");
fRet = true;
}
if (fRet) {
fReindex = true;
AbortShutdown();
Expand Down

0 comments on commit 291ac87

Please sign in to comment.