Skip to content

Commit c7da61d

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#24403: Avoid implicit-integer-sign-change in VerifyLoadedChainstate
fa79916 Fixup style of VerifyDB (MarcoFalke) fa462ea Avoid implicit-integer-sign-change in VerifyLoadedChainstate (MarcoFalke) Pull request description: This happens when checking all blocks (`-1`). To test: ``` ./configure CC=clang CXX=clang++ --with-sanitizers=undefined,integer make UBSAN_OPTIONS="suppressions=$(pwd)/test/sanitizer_suppressions/ubsan:print_stacktrace=1:halt_on_error=1:report_error_type=1" ./test/functional/rpc_blockchain.py ACKs for top commit: theStack: Code-review ACK fa79916 brunoerg: crACK fa79916 Tree-SHA512: bcbe6becf2fbedd21bbde83a544122e79465937346802039532143b2e4165784905a8852c0ccb088b964874df5e5550931fdde3629cbcee3ae237f2f63c43a8e
2 parents b71a077 + fa79916 commit c7da61d

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

src/node/chainstate.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
129129
bool fReset,
130130
bool fReindexChainState,
131131
const Consensus::Params& consensus_params,
132-
unsigned int check_blocks,
133-
unsigned int check_level,
132+
int check_blocks,
133+
int check_level,
134134
std::function<int64_t()> get_unix_time_seconds)
135135
{
136136
auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {

src/node/chainstate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
7979
bool fReset,
8080
bool fReindexChainState,
8181
const Consensus::Params& consensus_params,
82-
unsigned int check_blocks,
83-
unsigned int check_level,
82+
int check_blocks,
83+
int check_level,
8484
std::function<int64_t()> get_unix_time_seconds);
8585
} // namespace node
8686

src/validation.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3843,12 +3843,14 @@ bool CVerifyDB::VerifyDB(
38433843
{
38443844
AssertLockHeld(cs_main);
38453845

3846-
if (chainstate.m_chain.Tip() == nullptr || chainstate.m_chain.Tip()->pprev == nullptr)
3846+
if (chainstate.m_chain.Tip() == nullptr || chainstate.m_chain.Tip()->pprev == nullptr) {
38473847
return true;
3848+
}
38483849

38493850
// Verify blocks in the best chain
3850-
if (nCheckDepth <= 0 || nCheckDepth > chainstate.m_chain.Height())
3851+
if (nCheckDepth <= 0 || nCheckDepth > chainstate.m_chain.Height()) {
38513852
nCheckDepth = chainstate.m_chain.Height();
3853+
}
38523854
nCheckLevel = std::max(0, std::min(4, nCheckLevel));
38533855
LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
38543856
CCoinsViewCache coins(&coinsview);
@@ -3863,14 +3865,15 @@ bool CVerifyDB::VerifyDB(
38633865

38643866
for (pindex = chainstate.m_chain.Tip(); pindex && pindex->pprev; pindex = pindex->pprev) {
38653867
const int percentageDone = std::max(1, std::min(99, (int)(((double)(chainstate.m_chain.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
3866-
if (reportDone < percentageDone/10) {
3868+
if (reportDone < percentageDone / 10) {
38673869
// report every 10% step
38683870
LogPrintf("[%d%%]...", percentageDone); /* Continued */
3869-
reportDone = percentageDone/10;
3871+
reportDone = percentageDone / 10;
38703872
}
38713873
uiInterface.ShowProgress(_("Verifying blocks…").translated, percentageDone, false);
3872-
if (pindex->nHeight <= chainstate.m_chain.Height()-nCheckDepth)
3874+
if (pindex->nHeight <= chainstate.m_chain.Height() - nCheckDepth) {
38733875
break;
3876+
}
38743877
if ((fPruneMode || is_snapshot_cs) && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
38753878
// If pruning or running under an assumeutxo snapshot, only go
38763879
// back as far as we have data.
@@ -3879,12 +3882,14 @@ bool CVerifyDB::VerifyDB(
38793882
}
38803883
CBlock block;
38813884
// check level 0: read from disk
3882-
if (!ReadBlockFromDisk(block, pindex, consensus_params))
3885+
if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
38833886
return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3887+
}
38843888
// check level 1: verify block validity
3885-
if (nCheckLevel >= 1 && !CheckBlock(block, state, consensus_params))
3889+
if (nCheckLevel >= 1 && !CheckBlock(block, state, consensus_params)) {
38863890
return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
38873891
pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString());
3892+
}
38883893
// check level 2: verify undo validity
38893894
if (nCheckLevel >= 2 && pindex) {
38903895
CBlockUndo undo;
@@ -3912,8 +3917,9 @@ bool CVerifyDB::VerifyDB(
39123917
}
39133918
if (ShutdownRequested()) return true;
39143919
}
3915-
if (pindexFailure)
3920+
if (pindexFailure) {
39163921
return error("VerifyDB(): *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainstate.m_chain.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
3922+
}
39173923

39183924
// store block count as we move pindex at check level >= 4
39193925
int block_count = chainstate.m_chain.Height() - pindex->nHeight;
@@ -3922,10 +3928,10 @@ bool CVerifyDB::VerifyDB(
39223928
if (nCheckLevel >= 4) {
39233929
while (pindex != chainstate.m_chain.Tip()) {
39243930
const int percentageDone = std::max(1, std::min(99, 100 - (int)(((double)(chainstate.m_chain.Height() - pindex->nHeight)) / (double)nCheckDepth * 50)));
3925-
if (reportDone < percentageDone/10) {
3931+
if (reportDone < percentageDone / 10) {
39263932
// report every 10% step
39273933
LogPrintf("[%d%%]...", percentageDone); /* Continued */
3928-
reportDone = percentageDone/10;
3934+
reportDone = percentageDone / 10;
39293935
}
39303936
uiInterface.ShowProgress(_("Verifying blocks…").translated, percentageDone, false);
39313937
pindex = chainstate.m_chain.Next(pindex);

test/functional/rpc_blockchain.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ def run_test(self):
6969
self.wallet = MiniWallet(self.nodes[0])
7070
self.mine_chain()
7171
self._test_max_future_block_time()
72-
self.restart_node(0, extra_args=['-stopatheight=207', '-prune=1']) # Set extra args with pruning after rescan is complete
72+
self.restart_node(
73+
0,
74+
extra_args=[
75+
"-stopatheight=207",
76+
"-checkblocks=-1", # Check all blocks
77+
"-prune=1", # Set pruning after rescan is complete
78+
],
79+
)
7380

7481
self._test_getblockchaininfo()
7582
self._test_getchaintxstats()

0 commit comments

Comments
 (0)