38
38
39
39
#include < cstddef>
40
40
#include < map>
41
+ #include < optional>
41
42
#include < unordered_map>
42
43
43
44
namespace kernel {
@@ -989,7 +990,7 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
989
990
return true ;
990
991
}
991
992
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
993
994
{
994
995
block.SetNull ();
995
996
@@ -1007,8 +1008,10 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const
1007
1008
return false ;
1008
1009
}
1009
1010
1011
+ const auto block_hash{block.GetHash ()};
1012
+
1010
1013
// Check the header
1011
- if (!CheckProofOfWork (block. GetHash () , block.nBits , GetConsensus ())) {
1014
+ if (!CheckProofOfWork (block_hash , block.nBits , GetConsensus ())) {
1012
1015
LogError (" Errors in block header at %s while reading block" , pos.ToString ());
1013
1016
return false ;
1014
1017
}
@@ -1019,21 +1022,19 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const
1019
1022
return false ;
1020
1023
}
1021
1024
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
+
1022
1031
return true ;
1023
1032
}
1024
1033
1025
1034
bool BlockManager::ReadBlock (CBlock& block, const CBlockIndex& index) const
1026
1035
{
1027
1036
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 ());
1037
1038
}
1038
1039
1039
1040
bool BlockManager::ReadRawBlock (std::vector<uint8_t >& block, const FlatFilePos& pos) const
0 commit comments