3838
3939#include < cstddef>
4040#include < map>
41+ #include < optional>
4142#include < unordered_map>
4243
4344namespace 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
10251034bool 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
10391040bool BlockManager::ReadRawBlock (std::vector<uint8_t >& block, const FlatFilePos& pos) const
0 commit comments