| 713 | } |
| 714 | |
| 715 | bool BlockManager::ReadBlockUndo(CBlockUndo& blockundo, const CBlockIndex& index) const |
| 716 | { |
| 717 | const FlatFilePos pos{WITH_LOCK(::cs_main, return index.GetUndoPos())}; |
| 718 | |
| 719 | // Open history file to read |
| 720 | AutoFile file{OpenUndoFile(pos, true)}; |
| 721 | if (file.IsNull()) { |
| 722 | LogError("OpenUndoFile failed for %s while reading block undo", pos.ToString()); |
| 723 | return false; |
| 724 | } |
| 725 | BufferedReader filein{std::move(file)}; |
| 726 | |
| 727 | try { |
| 728 | // Read block |
| 729 | HashVerifier verifier{filein}; // Use HashVerifier, as reserializing may lose data, c.f. commit d3424243 |
| 730 | |
| 731 | verifier << index.pprev->GetBlockHash(); |
| 732 | verifier >> blockundo; |
| 733 | |
| 734 | uint256 hashChecksum; |
| 735 | filein >> hashChecksum; |
| 736 | |
| 737 | // Verify checksum |
| 738 | if (hashChecksum != verifier.GetHash()) { |
| 739 | LogError("Checksum mismatch at %s while reading block undo", pos.ToString()); |
| 740 | return false; |
| 741 | } |
| 742 | } catch (const std::exception& e) { |
| 743 | LogError("Deserialize or I/O error - %s at %s while reading block undo", e.what(), pos.ToString()); |
| 744 | return false; |
| 745 | } |
| 746 | |
| 747 | return true; |
| 748 | } |
| 749 | |
| 750 | bool BlockManager::FlushUndoFile(int block_file, bool finalize) |
| 751 | { |