refactor, validation: Return more errors from VerifyDB

Return ConnectBlock errors from VerifyDB.
This commit is contained in:
Ryan Ofsky 2024-03-20 17:12:09 -04:00
parent 1e29b2eaaf
commit f65fa8c911
4 changed files with 69 additions and 54 deletions

View File

@ -19,6 +19,7 @@
#include <util/fs.h>
#include <util/signalinterrupt.h>
#include <util/time.h>
#include <util/overloaded.h>
#include <util/translation.h>
#include <validation.h>
@ -282,7 +283,7 @@ FlushResult<InterruptResult, ChainstateLoadError> VerifyLoadedChainstate(Chainst
LOCK(cs_main);
util::Result<InterruptResult, ChainstateLoadError> result;
FlushResult<InterruptResult, ChainstateLoadError> result;
for (Chainstate* chainstate : chainman.GetAll()) {
if (!is_coinsview_empty(chainstate)) {
const CBlockIndex* tip = chainstate->m_chain.Tip();
@ -294,27 +295,25 @@ FlushResult<InterruptResult, ChainstateLoadError> VerifyLoadedChainstate(Chainst
return result;
}
VerifyDBResult verify_result = CVerifyDB(chainman.GetNotifications()).VerifyDB(
auto verify_result{CVerifyDB(chainman.GetNotifications()).VerifyDB(
*chainstate, chainman.GetConsensus(), chainstate->CoinsDB(),
options.check_level,
options.check_blocks);
switch (verify_result) {
case VerifyDBResult::SUCCESS:
case VerifyDBResult::SKIPPED_MISSING_BLOCKS:
break;
case VerifyDBResult::INTERRUPTED:
result.Update(Interrupted{});
return result;
case VerifyDBResult::CORRUPTED_BLOCK_DB:
options.check_blocks) >> result};
if (verify_result) {
std::visit(util::Overloaded{
[&](Success) {},
[&](SkippedMissingBlocks) {},
[&](SkippedL3Checks) {
if (options.require_full_verification) {
result.Update({util::Error{_("Insufficient dbcache for block verification")}, ChainstateLoadError::FAILURE_INSUFFICIENT_DBCACHE});
}
},
[&](kernel::Interrupted) {
result.Update(Interrupted{});
}}, *verify_result);
} else {
result.Update({util::Error{_("Corrupted block database detected")}, ChainstateLoadError::FAILURE});
return result;
case VerifyDBResult::SKIPPED_L3_CHECKS:
if (options.require_full_verification) {
result.Update({util::Error{_("Insufficient dbcache for block verification")}, ChainstateLoadError::FAILURE_INSUFFICIENT_DBCACHE});
return result;
}
break;
} // no default case, so the compiler can warn about missing cases
}
}
}

View File

@ -1144,8 +1144,8 @@ static RPCHelpMan verifychain()
LOCK(cs_main);
Chainstate& active_chainstate = chainman.ActiveChainstate();
return CVerifyDB(chainman.GetNotifications()).VerifyDB(
active_chainstate, chainman.GetParams().GetConsensus(), active_chainstate.CoinsTip(), check_level, check_depth) == VerifyDBResult::SUCCESS;
return bool{CVerifyDB(chainman.GetNotifications()).VerifyDB(
active_chainstate, chainman.GetParams().GetConsensus(), active_chainstate.CoinsTip(), check_level, check_depth)};
},
};
}

View File

@ -4642,17 +4642,17 @@ CVerifyDB::~CVerifyDB()
m_notifications.progress(bilingual_str{}, 100, false);
}
VerifyDBResult CVerifyDB::VerifyDB(
FlushResult<VerifyDBResult> CVerifyDB::VerifyDB(
Chainstate& chainstate,
const Consensus::Params& consensus_params,
CCoinsView& coinsview,
int nCheckLevel, int nCheckDepth)
{
AssertLockHeld(cs_main);
FlushResult<> result;
FlushResult<VerifyDBResult> result;
if (chainstate.m_chain.Tip() == nullptr || chainstate.m_chain.Tip()->pprev == nullptr) {
return VerifyDBResult::SUCCESS;
return result;
}
// Verify blocks in the best chain
@ -4694,22 +4694,28 @@ VerifyDBResult CVerifyDB::VerifyDB(
CBlock block;
// check level 0: read from disk
if (!chainstate.m_blockman.ReadBlockFromDisk(block, *pindex)) {
LogPrintf("Verification error: ReadBlockFromDisk failed at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
return VerifyDBResult::CORRUPTED_BLOCK_DB;
auto error{Untranslated(strprintf("Verification error: ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString()))};
LogPrintf("%s\n", error.original);
result.Update(util::Error{std::move(error)});
return result;
}
// check level 1: verify block validity
if (nCheckLevel >= 1 && !CheckBlock(block, state, consensus_params)) {
LogPrintf("Verification error: found bad block at %d, hash=%s (%s)\n",
pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString());
return VerifyDBResult::CORRUPTED_BLOCK_DB;
auto error{Untranslated(strprintf("Verification error: found bad block at %d, hash=%s (%s)",
pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString()))};
LogPrintf("%s\n", error.original);
result.Update(util::Error{std::move(error)});
return result;
}
// check level 2: verify undo validity
if (nCheckLevel >= 2 && pindex) {
CBlockUndo undo;
if (!pindex->GetUndoPos().IsNull()) {
if (!chainstate.m_blockman.UndoReadFromDisk(undo, *pindex)) {
LogPrintf("Verification error: found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
return VerifyDBResult::CORRUPTED_BLOCK_DB;
auto error{Untranslated(strprintf("Verification error: found bad undo data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString()))};
LogPrintf("%s\n", error.original);
result.Update(util::Error{std::move(error)});
return result;
}
}
}
@ -4721,8 +4727,10 @@ VerifyDBResult CVerifyDB::VerifyDB(
assert(coins.GetBestBlock() == pindex->GetBlockHash());
DisconnectResult res = chainstate.DisconnectBlock(block, pindex, coins);
if (res == DISCONNECT_FAILED) {
LogPrintf("Verification error: irrecoverable inconsistency in block data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
return VerifyDBResult::CORRUPTED_BLOCK_DB;
auto error{Untranslated(strprintf("Verification error: irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString()))};
LogPrintf("%s\n", error.original);
result.Update(util::Error{std::move(error)});
return result;
}
if (res == DISCONNECT_UNCLEAN) {
nGoodTransactions = 0;
@ -4734,11 +4742,16 @@ VerifyDBResult CVerifyDB::VerifyDB(
skipped_l3_checks = true;
}
}
if (chainstate.m_chainman.m_interrupt) return VerifyDBResult::INTERRUPTED;
if (chainstate.m_chainman.m_interrupt) {
result.Update(Interrupted{});
return result;
}
}
if (pindexFailure) {
LogPrintf("Verification error: coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainstate.m_chain.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
return VerifyDBResult::CORRUPTED_BLOCK_DB;
auto error{Untranslated(strprintf("Verification error: coin database inconsistencies found (last %i blocks, %i good transactions before that)", chainstate.m_chain.Height() - pindexFailure->nHeight + 1, nGoodTransactions))};
LogPrintf("%s\n", error.original);
result.Update(util::Error{std::move(error)});
return result;
}
if (skipped_l3_checks) {
LogPrintf("Skipped verification of level >=3 (insufficient database cache size). Consider increasing -dbcache.\n");
@ -4760,26 +4773,32 @@ VerifyDBResult CVerifyDB::VerifyDB(
pindex = chainstate.m_chain.Next(pindex);
CBlock block;
if (!chainstate.m_blockman.ReadBlockFromDisk(block, *pindex)) {
LogPrintf("Verification error: ReadBlockFromDisk failed at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
return VerifyDBResult::CORRUPTED_BLOCK_DB;
auto error{Untranslated(strprintf("Verification error: ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString()))};
LogPrintf("%s\n", error.original);
result.Update(util::Error{std::move(error)});
return result;
}
if (!(chainstate.ConnectBlock(block, state, pindex, coins) >> result)) {
LogPrintf("Verification error: found unconnectable block at %d, hash=%s (%s)\n", pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString());
return VerifyDBResult::CORRUPTED_BLOCK_DB;
auto error{Untranslated(strprintf("Verification error: found unconnectable block at %d, hash=%s (%s)", pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString()))};
LogPrintf("%s\n", error.original);
result.Update(util::Error{std::move(error)});
return result;
}
if (chainstate.m_chainman.m_interrupt) {
result.Update(Interrupted{});
return result;
}
if (chainstate.m_chainman.m_interrupt) return VerifyDBResult::INTERRUPTED;
}
}
LogPrintf("Verification: No coin database inconsistencies in last %i blocks (%i transactions)\n", block_count, nGoodTransactions);
if (skipped_l3_checks) {
return VerifyDBResult::SKIPPED_L3_CHECKS;
result.Update(SkippedL3Checks{});
} else if (skipped_no_block_data) {
result.Update(SkippedMissingBlocks{});
}
if (skipped_no_block_data) {
return VerifyDBResult::SKIPPED_MISSING_BLOCKS;
}
return VerifyDBResult::SUCCESS;
return result;
}
/** Apply the effects of a block on the utxo cache, ignoring that it may already have been applied. */

View File

@ -388,13 +388,10 @@ bool IsBlockMutated(const CBlock& block, bool check_witness_root);
/** Return the sum of the claimed work on a given set of headers. No verification of PoW is done. */
arith_uint256 CalculateClaimedHeadersWork(const std::vector<CBlockHeader>& headers);
enum class VerifyDBResult {
SUCCESS,
CORRUPTED_BLOCK_DB,
INTERRUPTED,
SKIPPED_L3_CHECKS,
SKIPPED_MISSING_BLOCKS,
};
struct Success{};
struct SkippedL3Checks{};
struct SkippedMissingBlocks{};
using VerifyDBResult = std::variant<Success, kernel::Interrupted, SkippedL3Checks, SkippedMissingBlocks>;
/** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */
class CVerifyDB
@ -405,7 +402,7 @@ private:
public:
explicit CVerifyDB(kernel::Notifications& notifications);
~CVerifyDB();
[[nodiscard]] VerifyDBResult VerifyDB(
[[nodiscard]] kernel::FlushResult<VerifyDBResult> VerifyDB(
Chainstate& chainstate,
const Consensus::Params& consensus_params,
CCoinsView& coinsview,