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.
This commit is contained in:
Lőrinc committed 2025-06-13 12:32:56 +02:00
1 parent 2371b9f4ee
commit 9341b5333a
3 files changed
+8 -8

No files matched your search

+1 -1
View File
@@ -411,7 +411,7 @@ public:
void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const; void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const;
/** Functions for disk access for blocks */ /** Functions for disk access for blocks */
bool ReadBlock(CBlock& block, const FlatFilePos& pos, const std::optional<uint256>& expected_hash = {}) const; bool ReadBlock(CBlock& block, const FlatFilePos& pos, const std::optional<uint256>& expected_hash) const;
bool ReadBlock(CBlock& block, const CBlockIndex& index) const; bool ReadBlock(CBlock& block, const CBlockIndex& index) const;
bool ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos& pos) const; bool ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos& pos) const;
+3 -3
View File
@@ -190,12 +190,12 @@ BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
BOOST_CHECK_EQUAL(read_block.nVersion, 0); BOOST_CHECK_EQUAL(read_block.nVersion, 0);
{ {
ASSERT_DEBUG_LOG("Errors in block header"); 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); BOOST_CHECK_EQUAL(read_block.nVersion, 1);
} }
{ {
ASSERT_DEBUG_LOG("Errors in block header"); 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); 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); BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + STORAGE_HEADER_BYTES) * 2);
// Block 2 was not overwritten: // Block 2 was not overwritten:
blockman.ReadBlock(read_block, pos2); BOOST_CHECK(!blockman.ReadBlock(read_block, pos2, {}));
BOOST_CHECK_EQUAL(read_block.nVersion, 2); BOOST_CHECK_EQUAL(read_block.nVersion, 2);
} }
+4 -4
View File
@@ -5168,14 +5168,14 @@ void ChainstateManager::LoadExternalBlockFile(
while (range.first != range.second) { while (range.first != range.second) {
std::multimap<uint256, FlatFilePos>::iterator it = range.first; std::multimap<uint256, FlatFilePos>::iterator it = range.first;
std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>(); std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
if (m_blockman.ReadBlock(*pblockrecursive, it->second)) { if (m_blockman.ReadBlock(*pblockrecursive, it->second, {})) {
LogDebug(BCLog::REINDEX, "%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(), const auto& block_hash{pblockrecursive->GetHash()};
head.ToString()); LogDebug(BCLog::REINDEX, "%s: Processing out of order child %s of %s", __func__, block_hash.ToString(), head.ToString());
LOCK(cs_main); LOCK(cs_main);
BlockValidationState dummy; BlockValidationState dummy;
if (AcceptBlock(pblockrecursive, dummy, nullptr, true, &it->second, nullptr, true)) { if (AcceptBlock(pblockrecursive, dummy, nullptr, true, &it->second, nullptr, true)) {
nLoaded++; nLoaded++;
queue.push_back(pblockrecursive->GetHash()); queue.push_back(block_hash);
} }
} }
range.first++; range.first++;