| 1048 | } |
| 1049 | |
| 1050 | bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos, const std::optional<uint256>& expected_hash) const |
| 1051 | { |
| 1052 | block.SetNull(); |
| 1053 | |
| 1054 | // Open history file to read |
| 1055 | const auto block_data{ReadRawBlock(pos)}; |
| 1056 | if (!block_data) { |
| 1057 | return false; |
| 1058 | } |
| 1059 | |
| 1060 | try { |
| 1061 | // Read block |
| 1062 | SpanReader{*block_data} >> TX_WITH_WITNESS(block); |
| 1063 | } catch (const std::exception& e) { |
| 1064 | LogError("Deserialize or I/O error - %s at %s while reading block", e.what(), pos.ToString()); |
| 1065 | return false; |
| 1066 | } |
| 1067 | |
| 1068 | const auto block_hash{block.GetHash()}; |
| 1069 | |
| 1070 | // Check the header |
| 1071 | if (!CheckProofOfWork(block_hash, block.nBits, GetConsensus())) { |
| 1072 | LogError("Errors in block header at %s while reading block", pos.ToString()); |
| 1073 | return false; |
| 1074 | } |
| 1075 | |
| 1076 | // Signet only: check block solution |
| 1077 | if (GetConsensus().signet_blocks && !CheckSignetBlockSolution(block, GetConsensus())) { |
| 1078 | LogError("Errors in block solution at %s while reading block", pos.ToString()); |
| 1079 | return false; |
| 1080 | } |
| 1081 | |
| 1082 | if (expected_hash && block_hash != *expected_hash) { |
| 1083 | LogError("GetHash() doesn't match index at %s while reading block (%s != %s)", |
| 1084 | pos.ToString(), block_hash.ToString(), expected_hash->ToString()); |
| 1085 | return false; |
| 1086 | } |
| 1087 | |
| 1088 | return true; |
| 1089 | } |
| 1090 | |
| 1091 | bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const |
| 1092 | { |