Skip to content

Commit

Permalink
Add a resync recovery via GUI for corrupt block databases
Browse files Browse the repository at this point in the history
This solves the issue of users having to manually find their datadir, delete all folders, and resyng themselves. The wallet will automatically resync itself if the block database is irrecoverable, after giving the user an option via the startup GUI.
  • Loading branch information
Remapper authored Dec 16, 2019
1 parent 150e130 commit c95b4b8
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,7 @@ bool AppInit2(boost::thread_group& threadGroup)
nCoinCacheSize = nTotalCache / 300; // coins in memory require around 300 bytes

bool fLoaded = false;
bool fRepair = false;
while (!fLoaded && !ShutdownRequested()) {
bool fReset = fReindex;
std::string strLoadError;
Expand Down Expand Up @@ -1338,6 +1339,7 @@ bool AppInit2(boost::thread_group& threadGroup)
if (!LoadBlockIndex()) {
if (ShutdownRequested()) break;
strLoadError = _("Error loading block database");
fRepair = true;
break;
}

Expand All @@ -1349,6 +1351,7 @@ bool AppInit2(boost::thread_group& threadGroup)
// Initialize the block index (no-op if non-empty database was already loaded)
if (!InitBlockIndex()) {
strLoadError = _("Error initializing block database");
fRepair = true;
break;
}

Expand Down Expand Up @@ -1442,12 +1445,14 @@ bool AppInit2(boost::thread_group& threadGroup)
if (!CVerifyDB().VerifyDB(pcoinsdbview, 4, GetArg("-checkblocks", 100))) {
strLoadError = _("Corrupted block database detected");
fVerifyingBlocks = false;
fRepair = true;
break;
}
} catch (std::exception& e) {
if (fDebug) LogPrintf("%s\n", e.what());
strLoadError = _("Error opening block database");
fVerifyingBlocks = false;
fRepair = true;
break;
}

Expand All @@ -1457,7 +1462,7 @@ bool AppInit2(boost::thread_group& threadGroup)

if (!fLoaded && !ShutdownRequested()) {
// first suggest a reindex
if (!fReset) {
if (!fReset && !fRepair) {
bool fRet = uiInterface.ThreadSafeMessageBox(
strLoadError + ".\n\n" + _("Do you want to rebuild the block database now?"),
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
Expand All @@ -1468,6 +1473,50 @@ bool AppInit2(boost::thread_group& threadGroup)
LogPrintf("Aborted block database rebuild. Exiting.\n");
return false;
}
} else if (fRepair) {
bool fRet = uiInterface.ThreadSafeMessageBox(
strLoadError + ".\n\n" + _("Do you want to resync the block database?"),
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
if (fRet) {
uiInterface.InitMessage(_("Recovering..."));
filesystem::path blocksDir = GetDataDir() / "blocks";
filesystem::path chainstateDir = GetDataDir() / "chainstate";
filesystem::path sporksDir = GetDataDir() / "sporks";
filesystem::path zerocoinDir = GetDataDir() / "zerocoin";

LogPrintf("Deleting blockchain folders blocks, chainstate, sporks and zerocoin\n");
try {
if (filesystem::exists(blocksDir)){
boost::filesystem::remove_all(blocksDir);
LogPrintf("recovery: folder deleted: %s\n", blocksDir.string().c_str());
}

if (filesystem::exists(chainstateDir)){
boost::filesystem::remove_all(chainstateDir);
LogPrintf("recovery: folder deleted: %s\n", chainstateDir.string().c_str());
}

if (filesystem::exists(sporksDir)){
boost::filesystem::remove_all(sporksDir);
LogPrintf("recovery: folder deleted: %s\n", sporksDir.string().c_str());
}

if (filesystem::exists(zerocoinDir)){
boost::filesystem::remove_all(zerocoinDir);
LogPrintf("recovery: folder deleted: %s\n", zerocoinDir.string().c_str());
}
} catch (boost::filesystem::filesystem_error& error) {
LogPrintf("Failed to delete blockchain folders %s\n", error.what());
strLoadError = _("Error deleting blockchain folders");
return InitError(strLoadError);
}
uiInterface.ThreadSafeMessageBox("Recovery complete, your wallet will resync when next opened.", "", CClientUIInterface::BTN_ABORT);
return false;
} else {
LogPrintf("Aborted block database resync. Exiting.\n");
return false;
}

} else {
return InitError(strLoadError);
}
Expand Down

0 comments on commit c95b4b8

Please sign in to comment.