refactor: collect block read operations into try block

Reorganized error handling in block-related operations by grouping related operations together within the same scope.

In `ReadBlockUndo()` and `ReadBlock()`, moved all deserialization operations, comments and checksum verification inside a single try/catch block for cleaner error handling.
In `WriteBlockUndo()`, consolidated hash calculation and data writing operations within a common block to better express their logical relationship.
This commit is contained in:
Lőrinc 2025-03-14 20:11:52 +01:00
parent c77e3107b8
commit 3197155f91

View File

@ -666,24 +666,26 @@ bool BlockManager::ReadBlockUndo(CBlockUndo& blockundo, const CBlockIndex& index
return false; return false;
} }
// Read block
uint256 hashChecksum;
HashVerifier verifier{filein}; // Use HashVerifier as reserializing may lose data, c.f. commit d342424301013ec47dc146a4beb49d5c9319d80a
try { try {
// Read block
HashVerifier verifier{filein}; // Use HashVerifier, as reserializing may lose data, c.f. commit d3424243
verifier << index.pprev->GetBlockHash(); verifier << index.pprev->GetBlockHash();
verifier >> blockundo; verifier >> blockundo;
uint256 hashChecksum;
filein >> hashChecksum; filein >> hashChecksum;
// Verify checksum
if (hashChecksum != verifier.GetHash()) {
LogError("%s: Checksum mismatch at %s\n", __func__, pos.ToString());
return false;
}
} catch (const std::exception& e) { } catch (const std::exception& e) {
LogError("%s: Deserialize or I/O error - %s at %s\n", __func__, e.what(), pos.ToString()); LogError("%s: Deserialize or I/O error - %s at %s\n", __func__, e.what(), pos.ToString());
return false; return false;
} }
// Verify checksum
if (hashChecksum != verifier.GetHash()) {
LogError("%s: Checksum mismatch at %s\n", __func__, pos.ToString());
return false;
}
return true; return true;
} }
@ -945,15 +947,14 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
// Write index header // Write index header
fileout << GetParams().MessageStart() << blockundo_size; fileout << GetParams().MessageStart() << blockundo_size;
// Write undo data
pos.nPos += BLOCK_SERIALIZATION_HEADER_SIZE; pos.nPos += BLOCK_SERIALIZATION_HEADER_SIZE;
fileout << blockundo; {
// Calculate checksum
// Calculate & write checksum HashWriter hasher{};
HashWriter hasher{}; hasher << block.pprev->GetBlockHash() << blockundo;
hasher << block.pprev->GetBlockHash(); // Write undo data & checksum
hasher << blockundo; fileout << blockundo << hasher.GetHash();
fileout << hasher.GetHash(); }
// rev files are written in block height order, whereas blk files are written as blocks come in (often out of order) // rev files are written in block height order, whereas blk files are written as blocks come in (often out of order)
// we want to flush the rev (undo) file once we've written the last block, which is indicated by the last height // we want to flush the rev (undo) file once we've written the last block, which is indicated by the last height
@ -992,8 +993,8 @@ bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos) const
return false; return false;
} }
// Read block
try { try {
// Read block
filein >> TX_WITH_WITNESS(block); filein >> TX_WITH_WITNESS(block);
} catch (const std::exception& e) { } catch (const std::exception& e) {
LogError("%s: Deserialize or I/O error - %s at %s\n", __func__, e.what(), pos.ToString()); LogError("%s: Deserialize or I/O error - %s at %s\n", __func__, e.what(), pos.ToString());
@ -1091,8 +1092,8 @@ FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
// Write index header // Write index header
fileout << GetParams().MessageStart() << block_size; fileout << GetParams().MessageStart() << block_size;
// Write block
pos.nPos += BLOCK_SERIALIZATION_HEADER_SIZE; pos.nPos += BLOCK_SERIALIZATION_HEADER_SIZE;
// Write block
fileout << TX_WITH_WITNESS(block); fileout << TX_WITH_WITNESS(block);
return pos; return pos;
} }