force reindex first time, remove leveldb data

This commit is contained in:
Brannon King 2019-12-11 13:50:04 -07:00 committed by Anthony Fieroni
parent aae6d4e348
commit 470bb18347
2 changed files with 44 additions and 17 deletions

View file

@ -39,6 +39,7 @@
#include <rpc/register.h> #include <rpc/register.h>
#include <rpc/server.h> #include <rpc/server.h>
#include <rpc/util.h> #include <rpc/util.h>
#include <sqlite/hdr/sqlite_modern_cpp/log.h>
#include <scheduler.h> #include <scheduler.h>
#include <script/sigcache.h> #include <script/sigcache.h>
#include <script/standard.h> #include <script/standard.h>
@ -1245,6 +1246,15 @@ bool AppInitMain(InitInterfaces& interfaces)
gArgs.GetArg("-datadir", ""), fs::current_path().string()); gArgs.GetArg("-datadir", ""), fs::current_path().string());
} }
sqlite::error_log(
[](sqlite::sqlite_exception& e) {
LogPrintf("Error with Sqlite: %s\n", e.what());
},
[](sqlite::errors::misuse& e) {
// You can behave differently to specific errors
}
);
InitSignatureCache(); InitSignatureCache();
InitScriptExecutionCache(); InitScriptExecutionCache();
@ -1454,7 +1464,6 @@ bool AppInitMain(InitInterfaces& interfaces)
bool fLoaded = false; bool fLoaded = false;
while (!fLoaded && !ShutdownRequested()) { while (!fLoaded && !ShutdownRequested()) {
bool fReset = fReindex;
std::string strLoadError; std::string strLoadError;
uiInterface.InitMessage(_("Loading block index...").translated); uiInterface.InitMessage(_("Loading block index...").translated);
@ -1471,9 +1480,9 @@ bool AppInitMain(InitInterfaces& interfaces)
// new CBlockTreeDB tries to delete the existing file, which // new CBlockTreeDB tries to delete the existing file, which
// fails if it's still open from the previous loop. Close it first: // fails if it's still open from the previous loop. Close it first:
pblocktree.reset(); pblocktree.reset();
pblocktree.reset(new CBlockTreeDB(nBlockTreeDBCache, false, fReset)); pblocktree.reset(new CBlockTreeDB(nBlockTreeDBCache, false, fReindex));
if (fReset) { if (fReindex) {
pblocktree->WriteReindexing(true); pblocktree->WriteReindexing(true);
//If we're reindexing in prune mode, wipe away unusable block files and all undo data files //If we're reindexing in prune mode, wipe away unusable block files and all undo data files
if (fPruneMode) if (fPruneMode)
@ -1488,7 +1497,7 @@ bool AppInitMain(InitInterfaces& interfaces)
// From here on out fReindex and fReset mean something different! // From here on out fReindex and fReset mean something different!
if (!LoadBlockIndex(chainparams)) { if (!LoadBlockIndex(chainparams)) {
if (ShutdownRequested()) break; if (ShutdownRequested()) break;
strLoadError = _("Error loading block database").translated; strLoadError = _("Error loading block index database");
break; break;
} }
@ -1521,7 +1530,7 @@ bool AppInitMain(InitInterfaces& interfaces)
::ChainstateActive().InitCoinsDB( ::ChainstateActive().InitCoinsDB(
/* cache_size_bytes */ nCoinDBCache, /* cache_size_bytes */ nCoinDBCache,
/* in_memory */ false, /* in_memory */ false,
/* should_wipe */ fReset || fReindexChainState); /* should_wipe */ fReindex || fReindexChainState);
::ChainstateActive().CoinsErrorCatcher().AddReadErrCallback([]() { ::ChainstateActive().CoinsErrorCatcher().AddReadErrCallback([]() {
uiInterface.ThreadSafeMessageBox( uiInterface.ThreadSafeMessageBox(
@ -1553,7 +1562,7 @@ bool AppInitMain(InitInterfaces& interfaces)
::ChainstateActive().InitCoinsCache(); ::ChainstateActive().InitCoinsCache();
assert(::ChainstateActive().CanFlushToDisk()); assert(::ChainstateActive().CanFlushToDisk());
is_coinsview_empty = fReset || fReindexChainState || is_coinsview_empty = fReindex || fReindexChainState ||
::ChainstateActive().CoinsTip().GetBestBlock().IsNull(); ::ChainstateActive().CoinsTip().GetBestBlock().IsNull();
if (!is_coinsview_empty) { if (!is_coinsview_empty) {
// LoadChainTip initializes the chain based on CoinsTip()'s best block // LoadChainTip initializes the chain based on CoinsTip()'s best block
@ -1575,7 +1584,7 @@ bool AppInitMain(InitInterfaces& interfaces)
break; break;
} }
if (!fReset) { if (!fReindex) {
// Note that RewindBlockIndex MUST run even if we're about to -reindex-chainstate. // Note that RewindBlockIndex MUST run even if we're about to -reindex-chainstate.
// It both disconnects blocks based on ::ChainActive(), and drops block data in // It both disconnects blocks based on ::ChainActive(), and drops block data in
// BlockIndex() based on lack of available witness data. // BlockIndex() based on lack of available witness data.
@ -1622,10 +1631,10 @@ bool AppInitMain(InitInterfaces& interfaces)
if (!fLoaded && !ShutdownRequested()) { if (!fLoaded && !ShutdownRequested()) {
// first suggest a reindex // first suggest a reindex
if (!fReset) { if (!fReindex) {
bool fRet = uiInterface.ThreadSafeQuestion( bool fRet = uiInterface.ThreadSafeQuestion(
strLoadError + ".\n\n" + _("Do you want to rebuild the block database now?").translated, strLoadError + ".\n\n" + _("Do you want to rebuild the block database now?").translated,
strLoadError + ".\nPlease restart with -reindex or -reindex-chainstate to recover.", strLoadError + ".\nPlease restart with -reindex to recover.",
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT); "", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
if (fRet) { if (fRet) {
fReindex = true; fReindex = true;
@ -1638,6 +1647,14 @@ bool AppInitMain(InitInterfaces& interfaces)
return InitError(strLoadError); return InitError(strLoadError);
} }
} }
if (fReindex) {
// remove old LevelDB indexes
boost::system::error_code ec;
fs::remove_all(GetDataDir() / "blocks" / "index", ec);
fs::remove_all(GetDataDir() / "chainstate", ec);
fs::remove_all(GetDataDir() / "claimtrie", ec);
}
} }
// As LoadBlockIndex can take several minutes, it's possible the user // As LoadBlockIndex can take several minutes, it's possible the user

View file

@ -4753,6 +4753,16 @@ bool LoadBlockIndex(const CChainParams& chainparams)
bool ret = LoadBlockIndexDB(chainparams); bool ret = LoadBlockIndexDB(chainparams);
if (!ret) return false; if (!ret) return false;
needs_init = g_blockman.m_block_index.empty(); needs_init = g_blockman.m_block_index.empty();
if (needs_init) {
auto blockDir = GetDataDir() / "blocks";
for (auto it: fs::directory_iterator(blockDir)) {
boost::system::error_code ec;
if (fs::file_size(it, ec) > 100000000) {
return error("Block index is empty but block files exist. Please re-run with -reindex.");
}
}
}
} }
if (needs_init) { if (needs_init) {