Skip to content

Commit 954f4a9

Browse files
author
MarcoFalke
committed
Merge #13431: validation: count blocks correctly for check level < 3
f618ebc validation: count blocks correctly for check level < 3 (Karl-Johan Alm) Pull request description: As noted in bitcoin/bitcoin#13428 (comment) there is a bug where if check level < 3, the resulting count for blocks is wrong, because `pindexState` is never updated. Post-commit `./bitcoin-cli verifychain 1 3`: ``` 2018-06-11T07:12:28Z Verifying last 3 blocks at level 1 2018-06-11T07:12:28Z [0%]...[33%]...[66%]...[99%]...[DONE]. 2018-06-11T07:12:28Z No coin database inconsistencies in last 3 blocks (0 transactions) ``` Pre-commit `./bitcoin-cli verifychain 1 3`: ``` 2018-06-11T07:13:34Z Verifying last 3 blocks at level 1 2018-06-11T07:13:34Z [0%]...[33%]...[66%]...[99%]...[DONE]. 2018-06-11T07:13:34Z No coin database inconsistencies in last 0 blocks (0 transactions) ``` Tree-SHA512: 3d82ed26665162c9615fb0e6e91a46ed4d229a5e6797c6c420e6b0bf1be6e5e02401c6e9a93b7a5aec503a2650d8c20d1b45fe300a922379e4cef8ee26e18d96
2 parents 10ffca7 + f618ebc commit 954f4a9

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/validation.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3984,14 +3984,13 @@ bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview,
39843984
nCheckLevel = std::max(0, std::min(4, nCheckLevel));
39853985
LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
39863986
CCoinsViewCache coins(coinsview);
3987-
CBlockIndex* pindexState = chainActive.Tip();
3987+
CBlockIndex* pindex;
39883988
CBlockIndex* pindexFailure = nullptr;
39893989
int nGoodTransactions = 0;
39903990
CValidationState state;
39913991
int reportDone = 0;
39923992
LogPrintf("[0%%]..."); /* Continued */
3993-
for (CBlockIndex* pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev)
3994-
{
3993+
for (pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev) {
39953994
boost::this_thread::interruption_point();
39963995
int percentageDone = std::max(1, std::min(99, (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
39973996
if (reportDone < percentageDone/10) {
@@ -4025,13 +4024,12 @@ bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview,
40254024
}
40264025
}
40274026
// check level 3: check for inconsistencies during memory-only disconnect of tip blocks
4028-
if (nCheckLevel >= 3 && pindex == pindexState && (coins.DynamicMemoryUsage() + pcoinsTip->DynamicMemoryUsage()) <= nCoinCacheUsage) {
4027+
if (nCheckLevel >= 3 && (coins.DynamicMemoryUsage() + pcoinsTip->DynamicMemoryUsage()) <= nCoinCacheUsage) {
40294028
assert(coins.GetBestBlock() == pindex->GetBlockHash());
40304029
DisconnectResult res = g_chainstate.DisconnectBlock(block, pindex, coins);
40314030
if (res == DISCONNECT_FAILED) {
40324031
return error("VerifyDB(): *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
40334032
}
4034-
pindexState = pindex->pprev;
40354033
if (res == DISCONNECT_UNCLEAN) {
40364034
nGoodTransactions = 0;
40374035
pindexFailure = pindex;
@@ -4045,9 +4043,11 @@ bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview,
40454043
if (pindexFailure)
40464044
return error("VerifyDB(): *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainActive.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
40474045

4046+
// store block count as we move pindex at check level >= 4
4047+
int block_count = chainActive.Height() - pindex->nHeight;
4048+
40484049
// check level 4: try reconnecting blocks
40494050
if (nCheckLevel >= 4) {
4050-
CBlockIndex *pindex = pindexState;
40514051
while (pindex != chainActive.Tip()) {
40524052
boost::this_thread::interruption_point();
40534053
uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, 100 - (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * 50))), false);
@@ -4061,7 +4061,7 @@ bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview,
40614061
}
40624062

40634063
LogPrintf("[DONE].\n");
4064-
LogPrintf("No coin database inconsistencies in last %i blocks (%i transactions)\n", chainActive.Height() - pindexState->nHeight, nGoodTransactions);
4064+
LogPrintf("No coin database inconsistencies in last %i blocks (%i transactions)\n", block_count, nGoodTransactions);
40654065

40664066
return true;
40674067
}

0 commit comments

Comments
 (0)