Skip to content

Commit 3197155

Browse files
committed
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.
1 parent c77e310 commit 3197155

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/node/blockstorage.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -666,24 +666,26 @@ bool BlockManager::ReadBlockUndo(CBlockUndo& blockundo, const CBlockIndex& index
666666
return false;
667667
}
668668

669-
// Read block
670-
uint256 hashChecksum;
671-
HashVerifier verifier{filein}; // Use HashVerifier as reserializing may lose data, c.f. commit d342424301013ec47dc146a4beb49d5c9319d80a
672669
try {
670+
// Read block
671+
HashVerifier verifier{filein}; // Use HashVerifier, as reserializing may lose data, c.f. commit d3424243
672+
673673
verifier << index.pprev->GetBlockHash();
674674
verifier >> blockundo;
675+
676+
uint256 hashChecksum;
675677
filein >> hashChecksum;
678+
679+
// Verify checksum
680+
if (hashChecksum != verifier.GetHash()) {
681+
LogError("%s: Checksum mismatch at %s\n", __func__, pos.ToString());
682+
return false;
683+
}
676684
} catch (const std::exception& e) {
677685
LogError("%s: Deserialize or I/O error - %s at %s\n", __func__, e.what(), pos.ToString());
678686
return false;
679687
}
680688

681-
// Verify checksum
682-
if (hashChecksum != verifier.GetHash()) {
683-
LogError("%s: Checksum mismatch at %s\n", __func__, pos.ToString());
684-
return false;
685-
}
686-
687689
return true;
688690
}
689691

@@ -945,15 +947,14 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
945947

946948
// Write index header
947949
fileout << GetParams().MessageStart() << blockundo_size;
948-
// Write undo data
949950
pos.nPos += BLOCK_SERIALIZATION_HEADER_SIZE;
950-
fileout << blockundo;
951-
952-
// Calculate & write checksum
953-
HashWriter hasher{};
954-
hasher << block.pprev->GetBlockHash();
955-
hasher << blockundo;
956-
fileout << hasher.GetHash();
951+
{
952+
// Calculate checksum
953+
HashWriter hasher{};
954+
hasher << block.pprev->GetBlockHash() << blockundo;
955+
// Write undo data & checksum
956+
fileout << blockundo << hasher.GetHash();
957+
}
957958

958959
// rev files are written in block height order, whereas blk files are written as blocks come in (often out of order)
959960
// 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
992993
return false;
993994
}
994995

995-
// Read block
996996
try {
997+
// Read block
997998
filein >> TX_WITH_WITNESS(block);
998999
} catch (const std::exception& e) {
9991000
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)
10911092

10921093
// Write index header
10931094
fileout << GetParams().MessageStart() << block_size;
1094-
// Write block
10951095
pos.nPos += BLOCK_SERIALIZATION_HEADER_SIZE;
1096+
// Write block
10961097
fileout << TX_WITH_WITNESS(block);
10971098
return pos;
10981099
}

0 commit comments

Comments
 (0)