Skip to content

Commit a4de160

Browse files
committed
scripted-diff: shorten BLOCK_SERIALIZATION_HEADER_SIZE constant
Renames the constant to be less verbose and better reflect its purpose: it represents the size of the storage header that precedes serialized block data on disk, not to be confused with a block's own header. -BEGIN VERIFY SCRIPT- git grep -q "STORAGE_HEADER_BYTES" $(git ls-files) && echo "Error: Target name STORAGE_HEADER_BYTES already exists in the codebase" && exit 1 sed -i 's/BLOCK_SERIALIZATION_HEADER_SIZE/STORAGE_HEADER_BYTES/g' $(git grep -l 'BLOCK_SERIALIZATION_HEADER_SIZE') -END VERIFY SCRIPT-
1 parent 6640dd5 commit a4de160

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/node/blockstorage.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
949949

950950
// Write index header
951951
fileout << GetParams().MessageStart() << blockundo_size;
952-
pos.nPos += BLOCK_SERIALIZATION_HEADER_SIZE;
952+
pos.nPos += STORAGE_HEADER_BYTES;
953953
{
954954
// Calculate checksum
955955
HashWriter hasher{};
@@ -1087,7 +1087,7 @@ bool BlockManager::ReadRawBlock(std::vector<uint8_t>& block, const FlatFilePos&
10871087
FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
10881088
{
10891089
const unsigned int block_size{static_cast<unsigned int>(GetSerializeSize(TX_WITH_WITNESS(block)))};
1090-
FlatFilePos pos{FindNextBlockPos(block_size + BLOCK_SERIALIZATION_HEADER_SIZE, nHeight, block.GetBlockTime())};
1090+
FlatFilePos pos{FindNextBlockPos(block_size + STORAGE_HEADER_BYTES, nHeight, block.GetBlockTime())};
10911091
if (pos.IsNull()) {
10921092
LogError("FindNextBlockPos failed");
10931093
return FlatFilePos();
@@ -1101,7 +1101,7 @@ FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
11011101

11021102
// Write index header
11031103
fileout << GetParams().MessageStart() << block_size;
1104-
pos.nPos += BLOCK_SERIALIZATION_HEADER_SIZE;
1104+
pos.nPos += STORAGE_HEADER_BYTES;
11051105
// Write block
11061106
fileout << TX_WITH_WITNESS(block);
11071107
return pos;

src/node/blockstorage.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
7575
static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
7676

7777
/** Size of header written by WriteBlock before a serialized CBlock (8 bytes) */
78-
static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE{std::tuple_size_v<MessageStartChars> + sizeof(unsigned int)};
78+
static constexpr size_t STORAGE_HEADER_BYTES{std::tuple_size_v<MessageStartChars> + sizeof(unsigned int)};
7979

8080
/** 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()};
81+
static constexpr size_t UNDO_DATA_DISK_OVERHEAD{STORAGE_HEADER_BYTES + uint256::size()};
8282

8383
// Because validation code takes pointers to the map's CBlockIndex objects, if
8484
// we ever switch to another associative container, we need to either use a
@@ -164,7 +164,7 @@ class BlockManager
164164
* blockfile info, and checks if there is enough disk space to save the block.
165165
*
166166
* The nAddSize argument passed to this function should include not just the size of the serialized CBlock, but also the size of
167-
* separator fields (BLOCK_SERIALIZATION_HEADER_SIZE).
167+
* separator fields (STORAGE_HEADER_BYTES).
168168
*/
169169
[[nodiscard]] FlatFilePos FindNextBlockPos(unsigned int nAddSize, unsigned int nHeight, uint64_t nTime);
170170
[[nodiscard]] bool FlushChainstateBlockFile(int tip_height);

src/test/blockmanager_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <test/util/logging.h>
1818
#include <test/util/setup_common.h>
1919

20-
using node::BLOCK_SERIALIZATION_HEADER_SIZE;
20+
using node::STORAGE_HEADER_BYTES;
2121
using node::BlockManager;
2222
using node::KernelNotifications;
2323
using node::MAX_BLOCKFILE_SIZE;
@@ -40,12 +40,12 @@ BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
4040
};
4141
BlockManager blockman{*Assert(m_node.shutdown_signal), blockman_opts};
4242
// simulate adding a genesis block normally
43-
BOOST_CHECK_EQUAL(blockman.WriteBlock(params->GenesisBlock(), 0).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
43+
BOOST_CHECK_EQUAL(blockman.WriteBlock(params->GenesisBlock(), 0).nPos, STORAGE_HEADER_BYTES);
4444
// simulate what happens during reindex
4545
// simulate a well-formed genesis block being found at offset 8 in the blk00000.dat file
4646
// the block is found at offset 8 because there is an 8 byte serialization header
4747
// consisting of 4 magic bytes + 4 length bytes before each block in a well-formed blk file.
48-
const FlatFilePos pos{0, BLOCK_SERIALIZATION_HEADER_SIZE};
48+
const FlatFilePos pos{0, STORAGE_HEADER_BYTES};
4949
blockman.UpdateBlockInfo(params->GenesisBlock(), 0, pos);
5050
// now simulate what happens after reindex for the first new block processed
5151
// the actual block contents don't matter, just that it's a block.
@@ -54,7 +54,7 @@ BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
5454
// 8 bytes (for serialization header) + 285 (for serialized genesis block) = 293
5555
// add another 8 bytes for the second block's serialization header and we get 293 + 8 = 301
5656
FlatFilePos actual{blockman.WriteBlock(params->GenesisBlock(), 1)};
57-
BOOST_CHECK_EQUAL(actual.nPos, BLOCK_SERIALIZATION_HEADER_SIZE + ::GetSerializeSize(TX_WITH_WITNESS(params->GenesisBlock())) + BLOCK_SERIALIZATION_HEADER_SIZE);
57+
BOOST_CHECK_EQUAL(actual.nPos, STORAGE_HEADER_BYTES + ::GetSerializeSize(TX_WITH_WITNESS(params->GenesisBlock())) + STORAGE_HEADER_BYTES);
5858
}
5959

6060
BOOST_FIXTURE_TEST_CASE(blockmanager_scan_unlink_already_pruned_files, TestChain100Setup)
@@ -172,7 +172,7 @@ BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
172172
FlatFilePos pos2{blockman.WriteBlock(block2, /*nHeight=*/2)};
173173

174174
// Two blocks in the file
175-
BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + BLOCK_SERIALIZATION_HEADER_SIZE) * 2);
175+
BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + STORAGE_HEADER_BYTES) * 2);
176176

177177
// First two blocks are written as expected
178178
// Errors are expected because block data is junk, thrown AFTER successful read
@@ -199,7 +199,7 @@ BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
199199
// Metadata is updated...
200200
BOOST_CHECK_EQUAL(block_data->nBlocks, 3);
201201
// ...but there are still only two blocks in the file
202-
BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + BLOCK_SERIALIZATION_HEADER_SIZE) * 2);
202+
BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + STORAGE_HEADER_BYTES) * 2);
203203

204204
// Block 2 was not overwritten:
205205
blockman.ReadBlock(read_block, pos2);

0 commit comments

Comments
 (0)