diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -1998,7 +1998,7 @@ if (!fReindex && chainActive.Tip() != nullptr) { uiInterface.InitMessage(_("Rewinding blocks...")); - if (!RewindBlockIndex(config, chainparams)) { + if (!RewindBlockIndex(config)) { strLoadError = _("Unable to rewind the database to a " "pre-fork state. You will need to " "redownload the blockchain"); @@ -2032,7 +2032,7 @@ } if (!CVerifyDB().VerifyDB( - config, chainparams, pcoinsdbview, + config, pcoinsdbview, GetArg("-checklevel", DEFAULT_CHECKLEVEL), GetArg("-checkblocks", DEFAULT_CHECKBLOCKS))) { strLoadError = _("Corrupted block database detected"); diff --git a/src/miner.cpp b/src/miner.cpp --- a/src/miner.cpp +++ b/src/miner.cpp @@ -217,8 +217,7 @@ GetSigOpCountWithoutP2SH(*pblock->vtx[0]); CValidationState state; - if (!TestBlockValidity(*config, state, chainparams, *pblock, pindexPrev, - false, false)) { + if (!TestBlockValidity(*config, state, *pblock, pindexPrev, false, false)) { throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state))); diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1153,8 +1153,7 @@ nCheckDepth = request.params[1].get_int(); } - return CVerifyDB().VerifyDB(config, Params(), pcoinsTip, nCheckLevel, - nCheckDepth); + return CVerifyDB().VerifyDB(config, pcoinsTip, nCheckLevel, nCheckDepth); } /** Implementation of IsSuperMajority with better feedback */ diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -552,8 +552,7 @@ return "inconclusive-not-best-prevblk"; } CValidationState state; - TestBlockValidity(config, state, Params(), block, pindexPrev, false, - true); + TestBlockValidity(config, state, block, pindexPrev, false, true); return BIP22ValidationResult(config, state); } diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -593,25 +593,30 @@ const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev); -/** Check a block is completely valid from start to finish (only works on top of - * our current best block, with cs_main held) */ +/** + * Check a block is completely valid from start to finish (only works on top of + * our current best block, with cs_main held) + */ bool TestBlockValidity(const Config &config, CValidationState &state, - const CChainParams &chainparams, const CBlock &block, - CBlockIndex *pindexPrev, bool fCheckPOW = true, - bool fCheckMerkleRoot = true); + const CBlock &block, CBlockIndex *pindexPrev, + bool fCheckPOW = true, bool fCheckMerkleRoot = true); -/** When there are blocks in the active chain with missing data, rewind the - * chainstate and remove them from the block index */ -bool RewindBlockIndex(const Config &config, const CChainParams ¶ms); +/** + * When there are blocks in the active chain with missing data, rewind the + * chainstate and remove them from the block index. + */ +bool RewindBlockIndex(const Config &config); -/** RAII wrapper for VerifyDB: Verify consistency of the block and coin - * databases */ +/** + * RAII wrapper for VerifyDB: Verify consistency of the block and coin + * databases. + */ class CVerifyDB { public: CVerifyDB(); ~CVerifyDB(); - bool VerifyDB(const Config &config, const CChainParams &chainparams, - CCoinsView *coinsview, int nCheckLevel, int nCheckDepth); + bool VerifyDB(const Config &config, CCoinsView *coinsview, int nCheckLevel, + int nCheckDepth); }; /** Find the last common block between the parameter chain and a locator. */ diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3747,10 +3747,11 @@ } bool TestBlockValidity(const Config &config, CValidationState &state, - const CChainParams &chainparams, const CBlock &block, - CBlockIndex *pindexPrev, bool fCheckPOW, - bool fCheckMerkleRoot) { + const CBlock &block, CBlockIndex *pindexPrev, + bool fCheckPOW, bool fCheckMerkleRoot) { AssertLockHeld(cs_main); + const CChainParams &chainparams = config.GetChainParams(); + assert(pindexPrev && pindexPrev == chainActive.Tip()); if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, @@ -4138,9 +4139,8 @@ uiInterface.ShowProgress("", 100); } -bool CVerifyDB::VerifyDB(const Config &config, const CChainParams &chainparams, - CCoinsView *coinsview, int nCheckLevel, - int nCheckDepth) { +bool CVerifyDB::VerifyDB(const Config &config, CCoinsView *coinsview, + int nCheckLevel, int nCheckDepth) { LOCK(cs_main); if (chainActive.Tip() == nullptr || chainActive.Tip()->pprev == nullptr) { return true; @@ -4151,12 +4151,17 @@ // suffices until the year 19000 nCheckDepth = 1000000000; } + if (nCheckDepth > chainActive.Height()) { nCheckDepth = chainActive.Height(); } + nCheckLevel = std::max(0, std::min(4, nCheckLevel)); LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel); + + const CChainParams &chainparams = config.GetChainParams(); + CCoinsViewCache coins(coinsview); CBlockIndex *pindexState = chainActive.Tip(); CBlockIndex *pindexFailure = nullptr; @@ -4191,6 +4196,7 @@ pindex->nHeight); break; } + CBlock block; // check level 0: read from disk @@ -4290,7 +4296,7 @@ return true; } -bool RewindBlockIndex(const Config &config, const CChainParams ¶ms) { +bool RewindBlockIndex(const Config &config) { LOCK(cs_main); int nHeight = chainActive.Height() + 1; @@ -4334,7 +4340,7 @@ PruneBlockIndexCandidates(); - CheckBlockIndex(params.GetConsensus()); + CheckBlockIndex(config.GetChainParams().GetConsensus()); if (!FlushStateToDisk(state, FLUSH_STATE_ALWAYS)) { return false;