From 9341b5333ad54ccdb7c16802ff06c51b956948e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Wed, 28 May 2025 14:12:04 +0200 Subject: [PATCH] blockstorage: make block read hash checks explicit Dropped the default expected_hash parameter from `ReadBlock()`. In `blockmanager_flush_block_file` tests, we pass {} since the tests would already fail at PoW validation for corrupted blocks. In `ChainstateManager::LoadExternalBlockFile`, we pass {} when processing child blocks because their hashes aren't known beforehand. --- src/node/blockstorage.h | 2 +- src/test/blockmanager_tests.cpp | 6 +++--- src/validation.cpp | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h index 9405f68c6cf..0e4c13debbc 100644 --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -411,7 +411,7 @@ public: void UnlinkPrunedFiles(const std::set& setFilesToPrune) const; /** Functions for disk access for blocks */ - bool ReadBlock(CBlock& block, const FlatFilePos& pos, const std::optional& expected_hash = {}) const; + bool ReadBlock(CBlock& block, const FlatFilePos& pos, const std::optional& expected_hash) const; bool ReadBlock(CBlock& block, const CBlockIndex& index) const; bool ReadRawBlock(std::vector& block, const FlatFilePos& pos) const; diff --git a/src/test/blockmanager_tests.cpp b/src/test/blockmanager_tests.cpp index d1c64e786dc..d3b58143719 100644 --- a/src/test/blockmanager_tests.cpp +++ b/src/test/blockmanager_tests.cpp @@ -190,12 +190,12 @@ BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file) BOOST_CHECK_EQUAL(read_block.nVersion, 0); { ASSERT_DEBUG_LOG("Errors in block header"); - BOOST_CHECK(!blockman.ReadBlock(read_block, pos1)); + BOOST_CHECK(!blockman.ReadBlock(read_block, pos1, {})); BOOST_CHECK_EQUAL(read_block.nVersion, 1); } { ASSERT_DEBUG_LOG("Errors in block header"); - BOOST_CHECK(!blockman.ReadBlock(read_block, pos2)); + BOOST_CHECK(!blockman.ReadBlock(read_block, pos2, {})); BOOST_CHECK_EQUAL(read_block.nVersion, 2); } @@ -212,7 +212,7 @@ BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file) BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + STORAGE_HEADER_BYTES) * 2); // Block 2 was not overwritten: - blockman.ReadBlock(read_block, pos2); + BOOST_CHECK(!blockman.ReadBlock(read_block, pos2, {})); BOOST_CHECK_EQUAL(read_block.nVersion, 2); } diff --git a/src/validation.cpp b/src/validation.cpp index 18c775a9db9..35f66271c45 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -5168,14 +5168,14 @@ void ChainstateManager::LoadExternalBlockFile( while (range.first != range.second) { std::multimap::iterator it = range.first; std::shared_ptr pblockrecursive = std::make_shared(); - if (m_blockman.ReadBlock(*pblockrecursive, it->second)) { - LogDebug(BCLog::REINDEX, "%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(), - head.ToString()); + if (m_blockman.ReadBlock(*pblockrecursive, it->second, {})) { + const auto& block_hash{pblockrecursive->GetHash()}; + LogDebug(BCLog::REINDEX, "%s: Processing out of order child %s of %s", __func__, block_hash.ToString(), head.ToString()); LOCK(cs_main); BlockValidationState dummy; if (AcceptBlock(pblockrecursive, dummy, nullptr, true, &it->second, nullptr, true)) { nLoaded++; - queue.push_back(pblockrecursive->GetHash()); + queue.push_back(block_hash); } } range.first++;