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.
This commit is contained in:
1 parent
2bf173210f
commit
09ee8b7f27
2 files changed
+13
-12
No files matched your search
+12
-11
@@ -38,6 +38,7 @@
|
|||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <optional>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace kernel {
|
namespace kernel {
|
||||||
@@ -989,7 +990,7 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const
|
bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos, const std::optional<uint256>& expected_hash) const
|
||||||
{
|
{
|
||||||
block.SetNull();
|
block.SetNull();
|
||||||
|
|
||||||
@@ -1007,8 +1008,10 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto block_hash{block.GetHash()};
|
||||||
|
|
||||||
// Check the header
|
// Check the header
|
||||||
if (!CheckProofOfWork(block.GetHash(), block.nBits, GetConsensus())) {
|
if (!CheckProofOfWork(block_hash, block.nBits, GetConsensus())) {
|
||||||
LogError("Errors in block header at %s while reading block", pos.ToString());
|
LogError("Errors in block header at %s while reading block", pos.ToString());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -1019,21 +1022,19 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (expected_hash && block_hash != *expected_hash) {
|
||||||
|
LogError("GetHash() doesn't match index at %s while reading block (%s != %s)",
|
||||||
|
pos.ToString(), block_hash.ToString(), expected_hash->ToString());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const
|
bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const
|
||||||
{
|
{
|
||||||
const FlatFilePos block_pos{WITH_LOCK(cs_main, return index.GetBlockPos())};
|
const FlatFilePos block_pos{WITH_LOCK(cs_main, return index.GetBlockPos())};
|
||||||
|
return ReadBlock(block, block_pos, index.GetBlockHash());
|
||||||
if (!ReadBlock(block, block_pos)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (block.GetHash() != index.GetBlockHash()) {
|
|
||||||
LogError("GetHash() doesn't match index for %s at %s while reading block", index.ToString(), block_pos.ToString());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BlockManager::ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos& pos) const
|
bool BlockManager::ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos& pos) const
|
||||||
|
|||||||
@@ -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;
|
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;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user