Skip to content

Commit 6360b53

Browse files
committed
validation: Change return value of VerifyDB to enum type
This does not change behavior. It is in preparation for special handling of the case where VerifyDB doesn't finish for various reasons, but doesn't fail.
1 parent 52ddbd5 commit 6360b53

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

src/node/chainstate.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,16 @@ ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager& chainman, const C
187187
"Only rebuild the block database if you are sure that your computer's date and time are correct")};
188188
}
189189

190-
if (!CVerifyDB().VerifyDB(
191-
*chainstate, chainman.GetConsensus(), chainstate->CoinsDB(),
192-
options.check_level,
193-
options.check_blocks)) {
190+
VerifyDBResult result = CVerifyDB().VerifyDB(
191+
*chainstate, chainman.GetConsensus(), chainstate->CoinsDB(),
192+
options.check_level,
193+
options.check_blocks);
194+
switch (result) {
195+
case VerifyDBResult::SUCCESS:
196+
break;
197+
case VerifyDBResult::CORRUPTED_BLOCK_DB:
194198
return {ChainstateLoadStatus::FAILURE, _("Corrupted block database detected")};
195-
}
199+
} // no default case, so the compiler can warn about missing cases
196200
}
197201
}
198202

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ static RPCHelpMan verifychain()
11251125

11261126
Chainstate& active_chainstate = chainman.ActiveChainstate();
11271127
return CVerifyDB().VerifyDB(
1128-
active_chainstate, chainman.GetParams().GetConsensus(), active_chainstate.CoinsTip(), check_level, check_depth);
1128+
active_chainstate, chainman.GetParams().GetConsensus(), active_chainstate.CoinsTip(), check_level, check_depth) == VerifyDBResult::SUCCESS;
11291129
},
11301130
};
11311131
}

src/validation.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4057,7 +4057,7 @@ CVerifyDB::~CVerifyDB()
40574057
uiInterface.ShowProgress("", 100, false);
40584058
}
40594059

4060-
bool CVerifyDB::VerifyDB(
4060+
VerifyDBResult CVerifyDB::VerifyDB(
40614061
Chainstate& chainstate,
40624062
const Consensus::Params& consensus_params,
40634063
CCoinsView& coinsview,
@@ -4066,7 +4066,7 @@ bool CVerifyDB::VerifyDB(
40664066
AssertLockHeld(cs_main);
40674067

40684068
if (chainstate.m_chain.Tip() == nullptr || chainstate.m_chain.Tip()->pprev == nullptr) {
4069-
return true;
4069+
return VerifyDBResult::SUCCESS;
40704070
}
40714071

40724072
// Verify blocks in the best chain
@@ -4106,19 +4106,22 @@ bool CVerifyDB::VerifyDB(
41064106
CBlock block;
41074107
// check level 0: read from disk
41084108
if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
4109-
return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4109+
LogPrintf("Verification error: ReadBlockFromDisk failed at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4110+
return VerifyDBResult::CORRUPTED_BLOCK_DB;
41104111
}
41114112
// check level 1: verify block validity
41124113
if (nCheckLevel >= 1 && !CheckBlock(block, state, consensus_params)) {
4113-
return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
4114-
pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString());
4114+
LogPrintf("Verification error: found bad block at %d, hash=%s (%s)\n",
4115+
pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString());
4116+
return VerifyDBResult::CORRUPTED_BLOCK_DB;
41154117
}
41164118
// check level 2: verify undo validity
41174119
if (nCheckLevel >= 2 && pindex) {
41184120
CBlockUndo undo;
41194121
if (!pindex->GetUndoPos().IsNull()) {
41204122
if (!UndoReadFromDisk(undo, pindex)) {
4121-
return error("VerifyDB(): *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4123+
LogPrintf("Verification error: found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4124+
return VerifyDBResult::CORRUPTED_BLOCK_DB;
41224125
}
41234126
}
41244127
}
@@ -4130,7 +4133,8 @@ bool CVerifyDB::VerifyDB(
41304133
assert(coins.GetBestBlock() == pindex->GetBlockHash());
41314134
DisconnectResult res = chainstate.DisconnectBlock(block, pindex, coins);
41324135
if (res == DISCONNECT_FAILED) {
4133-
return error("VerifyDB(): *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4136+
LogPrintf("Verification error: irrecoverable inconsistency in block data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4137+
return VerifyDBResult::CORRUPTED_BLOCK_DB;
41344138
}
41354139
if (res == DISCONNECT_UNCLEAN) {
41364140
nGoodTransactions = 0;
@@ -4142,14 +4146,16 @@ bool CVerifyDB::VerifyDB(
41424146
skipped_l3_checks = true;
41434147
}
41444148
}
4145-
if (ShutdownRequested()) return true;
4149+
if (ShutdownRequested()) return VerifyDBResult::SUCCESS;
41464150
}
41474151
if (pindexFailure) {
4148-
return error("VerifyDB(): *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainstate.m_chain.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
4152+
LogPrintf("Verification error: coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainstate.m_chain.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
4153+
return VerifyDBResult::CORRUPTED_BLOCK_DB;
41494154
}
41504155
if (skipped_l3_checks) {
41514156
LogPrintf("Skipped verification of level >=3 (insufficient database cache size). Consider increasing -dbcache.\n");
41524157
}
4158+
41534159
// store block count as we move pindex at check level >= 4
41544160
int block_count = chainstate.m_chain.Height() - pindex->nHeight;
41554161

@@ -4165,18 +4171,21 @@ bool CVerifyDB::VerifyDB(
41654171
uiInterface.ShowProgress(_("Verifying blocks…").translated, percentageDone, false);
41664172
pindex = chainstate.m_chain.Next(pindex);
41674173
CBlock block;
4168-
if (!ReadBlockFromDisk(block, pindex, consensus_params))
4169-
return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4174+
if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
4175+
LogPrintf("Verification error: ReadBlockFromDisk failed at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4176+
return VerifyDBResult::CORRUPTED_BLOCK_DB;
4177+
}
41704178
if (!chainstate.ConnectBlock(block, state, pindex, coins)) {
4171-
return error("VerifyDB(): *** found unconnectable block at %d, hash=%s (%s)", pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString());
4179+
LogPrintf("Verification error: found unconnectable block at %d, hash=%s (%s)\n", pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString());
4180+
return VerifyDBResult::CORRUPTED_BLOCK_DB;
41724181
}
4173-
if (ShutdownRequested()) return true;
4182+
if (ShutdownRequested()) return VerifyDBResult::SUCCESS;
41744183
}
41754184
}
41764185

41774186
LogPrintf("Verification: No coin database inconsistencies in last %i blocks (%i transactions)\n", block_count, nGoodTransactions);
41784187

4179-
return true;
4188+
return VerifyDBResult::SUCCESS;
41804189
}
41814190

41824191
/** Apply the effects of a block on the utxo cache, ignoring that it may already have been applied. */

src/validation.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,17 @@ bool HasValidProofOfWork(const std::vector<CBlockHeader>& headers, const Consens
349349
/** Return the sum of the work on a given set of headers */
350350
arith_uint256 CalculateHeadersWork(const std::vector<CBlockHeader>& headers);
351351

352+
enum class VerifyDBResult {
353+
SUCCESS,
354+
CORRUPTED_BLOCK_DB,
355+
};
356+
352357
/** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */
353358
class CVerifyDB {
354359
public:
355360
CVerifyDB();
356361
~CVerifyDB();
357-
bool VerifyDB(
362+
[[nodiscard]] VerifyDBResult VerifyDB(
358363
Chainstate& chainstate,
359364
const Consensus::Params& consensus_params,
360365
CCoinsView& coinsview,

0 commit comments

Comments
 (0)