From 168ba993921c2c1a21cad1f03a331f7c01c67079 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Wed, 19 Jun 2013 11:32:49 -0400 Subject: [PATCH 1/2] Pass check level, check depth to VerifyDB() --- src/init.cpp | 3 ++- src/main.cpp | 5 ++--- src/main.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index fe74cd6963..ec10e627f5 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -757,7 +757,8 @@ bool AppInit2(boost::thread_group& threadGroup) } uiInterface.InitMessage(_("Verifying blocks...")); - if (!VerifyDB()) { + if (!VerifyDB(GetArg("-checklevel", 3), + GetArg( "-checkblocks", 288))) { strLoadError = _("Corrupted block database detected"); break; } diff --git a/src/main.cpp b/src/main.cpp index f0c08d273f..58c0c93635 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2616,13 +2616,12 @@ bool static LoadBlockIndexDB() return true; } -bool VerifyDB() { +bool VerifyDB(int nCheckLevel, int nCheckDepth) +{ if (pindexBest == NULL || pindexBest->pprev == NULL) return true; // Verify blocks in the best chain - int nCheckLevel = GetArg("-checklevel", 3); - int nCheckDepth = GetArg( "-checkblocks", 288); if (nCheckDepth == 0) nCheckDepth = 1000000000; // suffices until the year 19000 if (nCheckDepth > nBestHeight) diff --git a/src/main.h b/src/main.h index 87f1dbfa8f..19d4a87b39 100644 --- a/src/main.h +++ b/src/main.h @@ -146,7 +146,7 @@ bool LoadBlockIndex(); /** Unload database information */ void UnloadBlockIndex(); /** Verify consistency of the block and coin databases */ -bool VerifyDB(); +bool VerifyDB(int nCheckLevel, int nCheckDepth); /** Print the loaded block tree */ void PrintBlockTree(); /** Find a block by height in the currently-connected chain */ From f590653377d5ee18c05894a58d226b5d2531077b Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Wed, 19 Jun 2013 11:53:02 -0400 Subject: [PATCH 2/2] RPC: add 'verifychain', to verify chain database at runtime --- src/bitcoinrpc.cpp | 3 +++ src/bitcoinrpc.h | 1 + src/main.cpp | 2 +- src/rpcblockchain.cpp | 16 ++++++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 285a88c8d8..d939f5f7b4 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -258,6 +258,7 @@ static const CRPCCommand vRPCCommands[] = { "gettxout", &gettxout, true, false }, { "lockunspent", &lockunspent, false, false }, { "listlockunspent", &listlockunspent, false, false }, + { "verifychain", &verifychain, true, false }, }; CRPCTable::CRPCTable() @@ -1198,6 +1199,8 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector 0) ConvertTo(params[0]); if (strMethod == "lockunspent" && n > 1) ConvertTo(params[1]); if (strMethod == "importprivkey" && n > 2) ConvertTo(params[2]); + if (strMethod == "verifychain" && n > 0) ConvertTo(params[0]); + if (strMethod == "verifychain" && n > 1) ConvertTo(params[1]); return params; } diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h index 44c657f8dc..aff759a5c8 100644 --- a/src/bitcoinrpc.h +++ b/src/bitcoinrpc.h @@ -205,5 +205,6 @@ extern json_spirit::Value getblockhash(const json_spirit::Array& params, bool fH extern json_spirit::Value getblock(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value gettxoutsetinfo(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value gettxout(const json_spirit::Array& params, bool fHelp); +extern json_spirit::Value verifychain(const json_spirit::Array& params, bool fHelp); #endif diff --git a/src/main.cpp b/src/main.cpp index 58c0c93635..4f29f77112 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2622,7 +2622,7 @@ bool VerifyDB(int nCheckLevel, int nCheckDepth) return true; // Verify blocks in the best chain - if (nCheckDepth == 0) + if (nCheckDepth <= 0) nCheckDepth = 1000000000; // suffices until the year 19000 if (nCheckDepth > nBestHeight) nCheckDepth = nBestHeight; diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index a0ddc4d9ee..6b2ef8315f 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -243,4 +243,20 @@ Value gettxout(const Array& params, bool fHelp) return ret; } +Value verifychain(const Array& params, bool fHelp) +{ + if (fHelp || params.size() > 2) + throw runtime_error( + "verifychain [check level] [num blocks]\n" + "Verifies blockchain database."); + + int nCheckLevel = GetArg("-checklevel", 3); + int nCheckDepth = GetArg("-checkblocks", 288); + if (params.size() > 0) + nCheckLevel = params[0].get_int(); + if (params.size() > 1) + nCheckDepth = params[1].get_int(); + + return VerifyDB(nCheckLevel, nCheckDepth); +}