Skip to content

Commit 6e138f0

Browse files
UdjinM6claude
andcommitted
review: document known_hash safety and fix test to exercise reindex path
Add comment explaining that CheckProofOfWork serves as the runtime consistency check for known_hash in release builds. Restructure test to skip standalone CheckBlock before AcceptBlock so that fChecked remains false and AcceptBlock's internal CheckBlock actually exercises the known_hash code path, mirroring the reindex flow. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 01af278 commit 6e138f0

2 files changed

Lines changed: 10 additions & 12 deletions

File tree

src/test/validation_block_tests.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,9 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
203203
}
204204

205205
/**
206-
* Test that CheckBlock and AcceptBlock work correctly when a pre-computed
207-
* known_hash is supplied (the reindex optimization path).
206+
* Test that AcceptBlock works correctly when a pre-computed known_hash is
207+
* supplied, exercising the reindex optimization path through AcceptBlockHeader
208+
* and CheckBlock.
208209
*/
209210
BOOST_AUTO_TEST_CASE(checkblock_accept_known_hash)
210211
{
@@ -214,17 +215,11 @@ BOOST_AUTO_TEST_CASE(checkblock_accept_known_hash)
214215

215216
auto good = GoodBlock(Params().GenesisBlock().GetHash());
216217
const uint256 hash{good->GetHash()};
217-
const CChainParams& chainparams = Params();
218218

219-
// CheckBlock with correct known_hash should succeed
220-
{
221-
BlockValidationState state;
222-
BOOST_CHECK(CheckBlock(*good, state, chainparams.GetConsensus(),
223-
/*fCheckPOW=*/true, /*fCheckMerkleRoot=*/true, &hash));
224-
BOOST_CHECK(state.IsValid());
225-
}
226-
227-
// AcceptBlock with correct known_hash should succeed
219+
// AcceptBlock with correct known_hash should succeed.
220+
// Do not call CheckBlock beforehand: that would set fChecked=true,
221+
// causing AcceptBlock's internal CheckBlock to short-circuit and skip
222+
// the known_hash path. Keeping fChecked=false mirrors the reindex flow.
228223
{
229224
LOCK(::cs_main);
230225
BlockValidationState state;

src/validation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4358,6 +4358,9 @@ bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, Block
43584358
CBlockIndex *pindexDummy = nullptr;
43594359
CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
43604360

4361+
// If the caller supplies a pre-computed hash, verify it in debug builds.
4362+
// In release builds a wrong hash is still caught: AcceptBlockHeader calls
4363+
// CheckBlockHeader which runs CheckProofOfWork against the header's nBits.
43614364
ASSERT_IF_DEBUG(!known_hash || *known_hash == block.GetHash());
43624365

43634366
const uint256 hash{known_hash ? *known_hash : block.GetHash()};

0 commit comments

Comments
 (0)