@@ -945,7 +945,9 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
945
945
// Write undo information to disk
946
946
if (block.GetUndoPos ().IsNull ()) {
947
947
FlatFilePos pos;
948
- if (!FindUndoPos (state, block.nFile , pos, ::GetSerializeSize (blockundo) + 40 )) {
948
+ const unsigned int blockundo_size{static_cast <unsigned int >(GetSerializeSize (blockundo))};
949
+ static_assert (UNDO_DATA_DISK_OVERHEAD == 40 ); // TODO remove
950
+ if (!FindUndoPos (state, block.nFile , pos, blockundo_size + UNDO_DATA_DISK_OVERHEAD)) {
949
951
LogError (" %s: FindUndoPos failed\n " , __func__);
950
952
return false ;
951
953
}
@@ -957,12 +959,11 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
957
959
}
958
960
959
961
// Write index header
960
- unsigned int nSize = GetSerializeSize (blockundo);
961
- fileout << GetParams ().MessageStart () << nSize;
962
-
962
+ assert (blockundo_size == GetSerializeSize (blockundo)); // TODO remove
963
+ fileout << GetParams ().MessageStart () << blockundo_size;
963
964
// Write undo data
964
- long fileOutPos = fileout. tell () ;
965
- pos.nPos = ( unsigned int )fileOutPos;
965
+ pos. nPos += BLOCK_SERIALIZATION_HEADER_SIZE ;
966
+ assert ( pos.nPos == fileout. tell ()); // TODO remove
966
967
fileout << blockundo;
967
968
968
969
// Calculate & write checksum
@@ -1092,33 +1093,26 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatF
1092
1093
1093
1094
FlatFilePos BlockManager::SaveBlockToDisk (const CBlock& block, int nHeight)
1094
1095
{
1095
- unsigned int nBlockSize = ::GetSerializeSize (TX_WITH_WITNESS (block));
1096
- // Account for the 4 magic message start bytes + the 4 length bytes (8 bytes total,
1097
- // defined as BLOCK_SERIALIZATION_HEADER_SIZE)
1098
- nBlockSize += static_cast <unsigned int >(BLOCK_SERIALIZATION_HEADER_SIZE);
1099
- FlatFilePos pos{FindNextBlockPos (nBlockSize, nHeight, block.GetBlockTime ())};
1096
+ const unsigned int block_size{static_cast <unsigned int >(GetSerializeSize (TX_WITH_WITNESS (block)))};
1097
+ FlatFilePos pos{FindNextBlockPos (block_size + BLOCK_SERIALIZATION_HEADER_SIZE, nHeight, block.GetBlockTime ())};
1100
1098
if (pos.IsNull ()) {
1101
1099
LogError (" %s: FindNextBlockPos failed\n " , __func__);
1102
1100
return FlatFilePos ();
1103
1101
}
1104
-
1105
- // Open history file to append
1106
1102
AutoFile fileout{OpenBlockFile (pos)};
1107
1103
if (fileout.IsNull ()) {
1108
1104
LogError (" %s: OpenBlockFile failed\n " , __func__);
1109
1105
m_opts.notifications .fatalError (_ (" Failed to write block." ));
1110
1106
return FlatFilePos ();
1111
1107
}
1112
1108
1109
+ assert (block_size == GetSerializeSize (TX_WITH_WITNESS (block))); // TODO remove
1113
1110
// Write index header
1114
- unsigned int nSize = GetSerializeSize (TX_WITH_WITNESS (block));
1115
- fileout << GetParams ().MessageStart () << nSize;
1116
-
1111
+ fileout << GetParams ().MessageStart () << block_size;
1117
1112
// Write block
1118
- long fileOutPos = fileout. tell () ;
1119
- pos.nPos = ( unsigned int )fileOutPos;
1113
+ pos. nPos += BLOCK_SERIALIZATION_HEADER_SIZE ;
1114
+ assert ( pos.nPos == fileout. tell ()); // TODO remove
1120
1115
fileout << TX_WITH_WITNESS (block);
1121
-
1122
1116
return pos;
1123
1117
}
1124
1118
0 commit comments