Skip to content

Commit 319ff58

Browse files
committed
Merge bitcoin/bitcoin#32638: blocks: force hash validations on disk read
9341b53 blockstorage: make block read hash checks explicit (Lőrinc) 2371b9f test/bench: verify hash in `ComputeFilter` reads (Lőrinc) 5d235d5 net: assert block hash in `ProcessGetBlockData` and `ProcessMessage` (Lőrinc) Pull request description: A follow-up to bitcoin/bitcoin#32487 (comment), after which validating the hash of a read block from disk doesn't incur the cost of calculating its hash anymore. ### Summary This PR adds explicit checks that the read block header's hash matches the one we were expecting. ### Context After the previous PR, validating a block's hash during read operations became essentially free. This PR leverages that by requiring callers to provide a block's expected hash (or `std::nullopt`), preventing silent failures caused by corrupted or mismatched data. Most `ReadBlock` usages were updated with expected hashes and now fail on mismatch. ### Changes * added hash assertions in `ProcessGetBlockData` and `ProcessMessage` to validate that the block read from disk matches the expected hash; * updated tests and benchmark to pass the correct block hash to `ReadBlock()`, ensuring the hash validation is tested - or none if we already expect PoW failure; * removed the default value for `expected_hash`, requiring an explicit hash for all block reads. ### Why is the hash still optional (but no longer has a default value) * for header-error tests, where the goal is to trigger failures early in the parsing process; * for out-of-order orphan blocks, where the child hash isn't available before the initial disk read. ACKs for top commit: maflcko: review ACK 9341b53 🕙 achow101: ACK 9341b53 hodlinator: ACK 9341b53 janb84: re ACK 9341b53 Tree-SHA512: cf1d4fff4c15e3f8898ec284929cb83d7e747125d4ee759e77d369f1716728e843ef98030be32c8d608956a96ae2fbefa0e801200c333b9eefd6c086ec032e1f
2 parents 689318c + 9341b53 commit 319ff58

File tree

6 files changed

+18
-16
lines changed

6 files changed

+18
-16
lines changed

src/bench/readwriteblock.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ static void ReadBlockBench(benchmark::Bench& bench)
4242
{
4343
const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
4444
auto& blockman{testing_setup->m_node.chainman->m_blockman};
45-
const auto pos{blockman.WriteBlock(CreateTestBlock(), 413'567)};
46-
CBlock block;
45+
const auto& test_block{CreateTestBlock()};
46+
const auto& expected_hash{test_block.GetHash()};
47+
const auto& pos{blockman.WriteBlock(test_block, 413'567)};
4748
bench.run([&] {
48-
const auto success{blockman.ReadBlock(block, pos)};
49+
CBlock block;
50+
const auto success{blockman.ReadBlock(block, pos, expected_hash)};
4951
assert(success);
5052
});
5153
}

src/net_processing.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,7 +2298,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
22982298
}
22992299

23002300
std::shared_ptr<const CBlock> pblock;
2301-
if (a_recent_block && a_recent_block->GetHash() == pindex->GetBlockHash()) {
2301+
if (a_recent_block && a_recent_block->GetHash() == inv.hash) {
23022302
pblock = a_recent_block;
23032303
} else if (inv.IsMsgWitnessBlk()) {
23042304
// Fast-path: in this case it is possible to serve the block directly from disk,
@@ -2318,7 +2318,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
23182318
} else {
23192319
// Send block from disk
23202320
std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
2321-
if (!m_chainman.m_blockman.ReadBlock(*pblockRead, block_pos)) {
2321+
if (!m_chainman.m_blockman.ReadBlock(*pblockRead, block_pos, inv.hash)) {
23222322
if (WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.IsBlockPruned(*pindex))) {
23232323
LogDebug(BCLog::NET, "Block was pruned before it could be read, %s\n", pfrom.DisconnectMsg(fLogIPs));
23242324
} else {
@@ -2364,7 +2364,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
23642364
// and we don't feel like constructing the object for them, so
23652365
// instead we respond with the full, non-compact block.
23662366
if (can_direct_fetch && pindex->nHeight >= tip->nHeight - MAX_CMPCTBLOCK_DEPTH) {
2367-
if (a_recent_compact_block && a_recent_compact_block->header.GetHash() == pindex->GetBlockHash()) {
2367+
if (a_recent_compact_block && a_recent_compact_block->header.GetHash() == inv.hash) {
23682368
MakeAndPushMessage(pfrom, NetMsgType::CMPCTBLOCK, *a_recent_compact_block);
23692369
} else {
23702370
CBlockHeaderAndShortTxIDs cmpctblock{*pblock, m_rng.rand64()};
@@ -4173,7 +4173,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
41734173

41744174
if (!block_pos.IsNull()) {
41754175
CBlock block;
4176-
const bool ret{m_chainman.m_blockman.ReadBlock(block, block_pos)};
4176+
const bool ret{m_chainman.m_blockman.ReadBlock(block, block_pos, req.blockhash)};
41774177
// If height is above MAX_BLOCKTXN_DEPTH then this block cannot get
41784178
// pruned after we release cs_main above, so this read should never fail.
41794179
assert(ret);

src/node/blockstorage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ class BlockManager
411411
void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const;
412412

413413
/** Functions for disk access for blocks */
414-
bool ReadBlock(CBlock& block, const FlatFilePos& pos, const std::optional<uint256>& expected_hash = {}) const;
414+
bool ReadBlock(CBlock& block, const FlatFilePos& pos, const std::optional<uint256>& expected_hash) const;
415415
bool ReadBlock(CBlock& block, const CBlockIndex& index) const;
416416
bool ReadRawBlock(std::vector<std::byte>& block, const FlatFilePos& pos) const;
417417

src/test/blockmanager_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,12 @@ BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
190190
BOOST_CHECK_EQUAL(read_block.nVersion, 0);
191191
{
192192
ASSERT_DEBUG_LOG("Errors in block header");
193-
BOOST_CHECK(!blockman.ReadBlock(read_block, pos1));
193+
BOOST_CHECK(!blockman.ReadBlock(read_block, pos1, {}));
194194
BOOST_CHECK_EQUAL(read_block.nVersion, 1);
195195
}
196196
{
197197
ASSERT_DEBUG_LOG("Errors in block header");
198-
BOOST_CHECK(!blockman.ReadBlock(read_block, pos2));
198+
BOOST_CHECK(!blockman.ReadBlock(read_block, pos2, {}));
199199
BOOST_CHECK_EQUAL(read_block.nVersion, 2);
200200
}
201201

@@ -212,7 +212,7 @@ BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
212212
BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + STORAGE_HEADER_BYTES) * 2);
213213

214214
// Block 2 was not overwritten:
215-
blockman.ReadBlock(read_block, pos2);
215+
BOOST_CHECK(!blockman.ReadBlock(read_block, pos2, {}));
216216
BOOST_CHECK_EQUAL(read_block.nVersion, 2);
217217
}
218218

src/test/util/blockfilter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ bool ComputeFilter(BlockFilterType filter_type, const CBlockIndex& block_index,
1717
LOCK(::cs_main);
1818

1919
CBlock block;
20-
if (!blockman.ReadBlock(block, block_index.GetBlockPos())) {
20+
if (!blockman.ReadBlock(block, block_index)) {
2121
return false;
2222
}
2323

src/validation.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5204,14 +5204,14 @@ void ChainstateManager::LoadExternalBlockFile(
52045204
while (range.first != range.second) {
52055205
std::multimap<uint256, FlatFilePos>::iterator it = range.first;
52065206
std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
5207-
if (m_blockman.ReadBlock(*pblockrecursive, it->second)) {
5208-
LogDebug(BCLog::REINDEX, "%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(),
5209-
head.ToString());
5207+
if (m_blockman.ReadBlock(*pblockrecursive, it->second, {})) {
5208+
const auto& block_hash{pblockrecursive->GetHash()};
5209+
LogDebug(BCLog::REINDEX, "%s: Processing out of order child %s of %s", __func__, block_hash.ToString(), head.ToString());
52105210
LOCK(cs_main);
52115211
BlockValidationState dummy;
52125212
if (AcceptBlock(pblockrecursive, dummy, nullptr, true, &it->second, nullptr, true)) {
52135213
nLoaded++;
5214-
queue.push_back(pblockrecursive->GetHash());
5214+
queue.push_back(block_hash);
52155215
}
52165216
}
52175217
range.first++;

0 commit comments

Comments
 (0)