Skip to content

Commit 09ee8b7

Browse files
committed
node: avoid recomputing block hash in ReadBlock
Eliminate one SHA‑256 double‑hash computation of the header per block read by reusing the hash for: * proof‑of‑work verification; * (optional) integrity check against the supplied hash.
1 parent 2bf1732 commit 09ee8b7

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/node/blockstorage.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include <cstddef>
4040
#include <map>
41+
#include <optional>
4142
#include <unordered_map>
4243

4344
namespace kernel {
@@ -989,7 +990,7 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
989990
return true;
990991
}
991992

992-
bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const
993+
bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos, const std::optional<uint256>& expected_hash) const
993994
{
994995
block.SetNull();
995996

@@ -1007,8 +1008,10 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const
10071008
return false;
10081009
}
10091010

1011+
const auto block_hash{block.GetHash()};
1012+
10101013
// Check the header
1011-
if (!CheckProofOfWork(block.GetHash(), block.nBits, GetConsensus())) {
1014+
if (!CheckProofOfWork(block_hash, block.nBits, GetConsensus())) {
10121015
LogError("Errors in block header at %s while reading block", pos.ToString());
10131016
return false;
10141017
}
@@ -1019,21 +1022,19 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const
10191022
return false;
10201023
}
10211024

1025+
if (expected_hash && block_hash != *expected_hash) {
1026+
LogError("GetHash() doesn't match index at %s while reading block (%s != %s)",
1027+
pos.ToString(), block_hash.ToString(), expected_hash->ToString());
1028+
return false;
1029+
}
1030+
10221031
return true;
10231032
}
10241033

10251034
bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const
10261035
{
10271036
const FlatFilePos block_pos{WITH_LOCK(cs_main, return index.GetBlockPos())};
1028-
1029-
if (!ReadBlock(block, block_pos)) {
1030-
return false;
1031-
}
1032-
if (block.GetHash() != index.GetBlockHash()) {
1033-
LogError("GetHash() doesn't match index for %s at %s while reading block", index.ToString(), block_pos.ToString());
1034-
return false;
1035-
}
1036-
return true;
1037+
return ReadBlock(block, block_pos, index.GetBlockHash());
10371038
}
10381039

10391040
bool BlockManager::ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos& pos) const

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;
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<uint8_t>& block, const FlatFilePos& pos) const;
417417

0 commit comments

Comments
 (0)