@@ -945,7 +945,9 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
945945 // Write undo information to disk
946946 if (block.GetUndoPos ().IsNull ()) {
947947 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)) {
949951 LogError (" %s: FindUndoPos failed\n " , __func__);
950952 return false ;
951953 }
@@ -957,12 +959,11 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
957959 }
958960
959961 // 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;
963964 // 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
966967 fileout << blockundo;
967968
968969 // Calculate & write checksum
@@ -1092,33 +1093,26 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatF
10921093
10931094FlatFilePos BlockManager::SaveBlockToDisk (const CBlock& block, int nHeight)
10941095{
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 ())};
11001098 if (pos.IsNull ()) {
11011099 LogError (" %s: FindNextBlockPos failed\n " , __func__);
11021100 return FlatFilePos ();
11031101 }
1104-
1105- // Open history file to append
11061102 AutoFile fileout{OpenBlockFile (pos)};
11071103 if (fileout.IsNull ()) {
11081104 LogError (" %s: OpenBlockFile failed\n " , __func__);
11091105 m_opts.notifications .fatalError (_ (" Failed to write block." ));
11101106 return FlatFilePos ();
11111107 }
11121108
1109+ assert (block_size == GetSerializeSize (TX_WITH_WITNESS (block))); // TODO remove
11131110 // Write index header
1114- unsigned int nSize = GetSerializeSize (TX_WITH_WITNESS (block));
1115- fileout << GetParams ().MessageStart () << nSize;
1116-
1111+ fileout << GetParams ().MessageStart () << block_size;
11171112 // 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
11201115 fileout << TX_WITH_WITNESS (block);
1121-
11221116 return pos;
11231117}
11241118
0 commit comments