Skip to content

Commit 9b604c0

Browse files
committed
validation: prepare VerifyDB for assumeutxo
Removes assumptions of use only on the active chainstate.
1 parent 7901647 commit 9b604c0

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

src/init.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,11 +1550,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15501550
break;
15511551
}
15521552

1553-
// Only verify the DB of the active chainstate. This is fixed in later
1554-
// work when we allow VerifyDB to be parameterized by chainstate.
1555-
if (&::ChainstateActive() == chainstate &&
1556-
!CVerifyDB().VerifyDB(
1557-
*chainstate, chainparams, &chainstate->CoinsDB(),
1553+
if (!CVerifyDB().VerifyDB(
1554+
*chainstate, chainparams, chainstate->CoinsDB(),
15581555
args.GetArg("-checklevel", DEFAULT_CHECKLEVEL),
15591556
args.GetArg("-checkblocks", DEFAULT_CHECKBLOCKS))) {
15601557
strLoadError = _("Corrupted block database detected");

src/rpc/blockchain.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,8 @@ static RPCHelpMan verifychain()
12391239
LOCK(cs_main);
12401240

12411241
CChainState& active_chainstate = chainman.ActiveChainstate();
1242-
return CVerifyDB().VerifyDB(active_chainstate, Params(), &active_chainstate.CoinsTip(), check_level, check_depth);
1242+
return CVerifyDB().VerifyDB(
1243+
active_chainstate, Params(), active_chainstate.CoinsTip(), check_level, check_depth);
12431244
},
12441245
};
12451246
}

src/validation.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4104,7 +4104,7 @@ CVerifyDB::~CVerifyDB()
41044104
bool CVerifyDB::VerifyDB(
41054105
CChainState& chainstate,
41064106
const CChainParams& chainparams,
4107-
CCoinsView* coinsview,
4107+
CCoinsView& coinsview,
41084108
int nCheckLevel, int nCheckDepth)
41094109
{
41104110
AssertLockHeld(cs_main);
@@ -4118,13 +4118,16 @@ bool CVerifyDB::VerifyDB(
41184118
nCheckDepth = chainstate.m_chain.Height();
41194119
nCheckLevel = std::max(0, std::min(4, nCheckLevel));
41204120
LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
4121-
CCoinsViewCache coins(coinsview);
4121+
CCoinsViewCache coins(&coinsview);
41224122
CBlockIndex* pindex;
41234123
CBlockIndex* pindexFailure = nullptr;
41244124
int nGoodTransactions = 0;
41254125
BlockValidationState state;
41264126
int reportDone = 0;
41274127
LogPrintf("[0%%]..."); /* Continued */
4128+
4129+
bool is_snapshot_cs = !chainstate.m_from_snapshot_blockhash.IsNull();
4130+
41284131
for (pindex = chainstate.m_chain.Tip(); pindex && pindex->pprev; pindex = pindex->pprev) {
41294132
const int percentageDone = std::max(1, std::min(99, (int)(((double)(chainstate.m_chain.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
41304133
if (reportDone < percentageDone/10) {
@@ -4135,8 +4138,9 @@ bool CVerifyDB::VerifyDB(
41354138
uiInterface.ShowProgress(_("Verifying blocks...").translated, percentageDone, false);
41364139
if (pindex->nHeight <= chainstate.m_chain.Height()-nCheckDepth)
41374140
break;
4138-
if (fPruneMode && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
4139-
// If pruning, only go back as far as we have data.
4141+
if ((fPruneMode || is_snapshot_cs) && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
4142+
// If pruning or running under an assumeutxo snapshot, only go
4143+
// back as far as we have data.
41404144
LogPrintf("VerifyDB(): block verification stopping at height %d (pruning, no data)\n", pindex->nHeight);
41414145
break;
41424146
}
@@ -4158,9 +4162,9 @@ bool CVerifyDB::VerifyDB(
41584162
}
41594163
}
41604164
// check level 3: check for inconsistencies during memory-only disconnect of tip blocks
4161-
int64_t curr_coins_usage = coins.DynamicMemoryUsage() + chainstate.CoinsTip().DynamicMemoryUsage();
4165+
size_t curr_coins_usage = coins.DynamicMemoryUsage() + chainstate.CoinsTip().DynamicMemoryUsage();
41624166

4163-
if (nCheckLevel >= 3 && static_cast<unsigned long>(curr_coins_usage) <= chainstate.m_coinstip_cache_size_bytes) {
4167+
if (nCheckLevel >= 3 && curr_coins_usage <= chainstate.m_coinstip_cache_size_bytes) {
41644168
assert(coins.GetBestBlock() == pindex->GetBlockHash());
41654169
DisconnectResult res = chainstate.DisconnectBlock(block, pindex, coins);
41664170
if (res == DISCONNECT_FAILED) {

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ class CVerifyDB {
332332
bool VerifyDB(
333333
CChainState& chainstate,
334334
const CChainParams& chainparams,
335-
CCoinsView* coinsview,
335+
CCoinsView& coinsview,
336336
int nCheckLevel,
337337
int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
338338
};

0 commit comments

Comments
 (0)