Skip to content

Commit fa39f27

Browse files
l0rincryanofsky
andcommitted
refactor,blocks: deduplicate block's serialized size calculations
For consistency `UNDO_DATA_DISK_OVERHEAD` was also extracted to avoid the constant's ambiguity. Asserts were added to help with the review - they are removed in the next commit. Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
1 parent dfb2f9d commit fa39f27

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

src/node/blockstorage.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

10931094
FlatFilePos 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

src/node/blockstorage.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
7474
/** The maximum size of a blk?????.dat file (since 0.8) */
7575
static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
7676

77-
/** Size of header written by SaveBlockToDisk before a serialized CBlock */
78-
static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = std::tuple_size_v<MessageStartChars> + sizeof(unsigned int);
77+
/** Size of header written by SaveBlockToDisk before a serialized CBlock (8 bytes) */
78+
static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE{std::tuple_size_v<MessageStartChars> + sizeof(unsigned int)};
79+
80+
/** Total overhead when writing undo data: header (8 bytes) plus checksum (32 bytes) */
81+
static constexpr size_t UNDO_DATA_DISK_OVERHEAD{BLOCK_SERIALIZATION_HEADER_SIZE + uint256::size()};
7982

8083
// Because validation code takes pointers to the map's CBlockIndex objects, if
8184
// we ever switch to another associative container, we need to either use a

0 commit comments

Comments
 (0)