Skip to content

Commit 602f4da

Browse files
committed
Add reindex=auto flag to automatically reindex corrupt data
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)
1 parent 8462cd5 commit 602f4da

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/init.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ void SetupServerArgs(NodeContext& node)
403403
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, -coinstatsindex and -rescan. "
404404
"Warning: Reverting this setting requires re-downloading the entire blockchain. "
405405
"(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);
406-
argsman.AddArg("-reindex", "Rebuild chain state and block index from the blk*.dat files on disk", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
406+
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);
407407
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);
408408
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);
409409
#if HAVE_SYSTEM
@@ -1303,6 +1303,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13031303

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

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

@@ -1519,12 +1521,20 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15191521
} while(false);
15201522

15211523
if (!fLoaded && !ShutdownRequested()) {
1522-
// first suggest a reindex
1524+
// If reindex=auto, directly start the reindex
1525+
bool fAutoReindex = (args.GetArg("-reindex", "0") == "auto");
15231526
if (!fReset) {
1524-
bool fRet = uiInterface.ThreadSafeQuestion(
1525-
strLoadError + Untranslated(".\n\n") + _("Do you want to rebuild the block database now?"),
1526-
strLoadError.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
1527-
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
1527+
bool fRet;
1528+
if(!fAutoReindex) {
1529+
// suggest a reindex to GUI users
1530+
fRet = uiInterface.ThreadSafeQuestion(
1531+
strLoadError + Untranslated(".\n\n") + _("Do you want to rebuild the block database now?"),
1532+
strLoadError.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
1533+
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
1534+
} else {
1535+
LogPrintf("Automatically running a reindex.\n");
1536+
fRet = true;
1537+
}
15281538
if (fRet) {
15291539
fReindex = true;
15301540
AbortShutdown();

0 commit comments

Comments
 (0)