Skip to content

Commit dfb2f9d

Browse files
l0rincryanofsky
andcommitted
refactor,blocks: inline WriteBlockToDisk
Similarly, `WriteBlockToDisk` wasn't really extracting a meaningful subset of the `SaveBlockToDisk` functionality, it's tied closely to the only caller (needs the header size twice, recalculated block serializes size, returns multiple branches, mutates parameter). The inlined code should only differ in these parts (modernization will be done in other commits): * renamed `blockPos` to `pos` in `SaveBlockToDisk` to match the parameter name; * changed `return false` to `return FlatFilePos()`. Also removed remaining references to `SaveBlockToDisk`. Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
1 parent 42bc491 commit dfb2f9d

File tree

2 files changed

+21
-37
lines changed

2 files changed

+21
-37
lines changed

src/node/blockstorage.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -936,27 +936,6 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
936936
return true;
937937
}
938938

939-
bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const
940-
{
941-
// Open history file to append
942-
AutoFile fileout{OpenBlockFile(pos)};
943-
if (fileout.IsNull()) {
944-
LogError("%s: OpenBlockFile failed\n", __func__);
945-
return false;
946-
}
947-
948-
// Write index header
949-
unsigned int nSize = GetSerializeSize(TX_WITH_WITNESS(block));
950-
fileout << GetParams().MessageStart() << nSize;
951-
952-
// Write block
953-
long fileOutPos = fileout.tell();
954-
pos.nPos = (unsigned int)fileOutPos;
955-
fileout << TX_WITH_WITNESS(block);
956-
957-
return true;
958-
}
959-
960939
bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex& block)
961940
{
962941
AssertLockHeld(::cs_main);
@@ -1117,16 +1096,30 @@ FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight)
11171096
// Account for the 4 magic message start bytes + the 4 length bytes (8 bytes total,
11181097
// defined as BLOCK_SERIALIZATION_HEADER_SIZE)
11191098
nBlockSize += static_cast<unsigned int>(BLOCK_SERIALIZATION_HEADER_SIZE);
1120-
FlatFilePos blockPos{FindNextBlockPos(nBlockSize, nHeight, block.GetBlockTime())};
1121-
if (blockPos.IsNull()) {
1099+
FlatFilePos pos{FindNextBlockPos(nBlockSize, nHeight, block.GetBlockTime())};
1100+
if (pos.IsNull()) {
11221101
LogError("%s: FindNextBlockPos failed\n", __func__);
11231102
return FlatFilePos();
11241103
}
1125-
if (!WriteBlockToDisk(block, blockPos)) {
1104+
1105+
// Open history file to append
1106+
AutoFile fileout{OpenBlockFile(pos)};
1107+
if (fileout.IsNull()) {
1108+
LogError("%s: OpenBlockFile failed\n", __func__);
11261109
m_opts.notifications.fatalError(_("Failed to write block."));
11271110
return FlatFilePos();
11281111
}
1129-
return blockPos;
1112+
1113+
// Write index header
1114+
unsigned int nSize = GetSerializeSize(TX_WITH_WITNESS(block));
1115+
fileout << GetParams().MessageStart() << nSize;
1116+
1117+
// Write block
1118+
long fileOutPos = fileout.tell();
1119+
pos.nPos = (unsigned int)fileOutPos;
1120+
fileout << TX_WITH_WITNESS(block);
1121+
1122+
return pos;
11301123
}
11311124

11321125
static auto InitBlocksdirXorKey(const BlockManager::Options& opts)

src/node/blockstorage.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ 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 WriteBlockToDisk before a serialized CBlock */
77+
/** Size of header written by SaveBlockToDisk before a serialized CBlock */
7878
static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = std::tuple_size_v<MessageStartChars> + sizeof(unsigned int);
7979

8080
// Because validation code takes pointers to the map's CBlockIndex objects, if
@@ -161,22 +161,14 @@ class BlockManager
161161
* blockfile info, and checks if there is enough disk space to save the block.
162162
*
163163
* The nAddSize argument passed to this function should include not just the size of the serialized CBlock, but also the size of
164-
* separator fields which are written before it by WriteBlockToDisk (BLOCK_SERIALIZATION_HEADER_SIZE).
164+
* separator fields (BLOCK_SERIALIZATION_HEADER_SIZE).
165165
*/
166166
[[nodiscard]] FlatFilePos FindNextBlockPos(unsigned int nAddSize, unsigned int nHeight, uint64_t nTime);
167167
[[nodiscard]] bool FlushChainstateBlockFile(int tip_height);
168168
bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize);
169169

170170
AutoFile OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false) const;
171171

172-
/**
173-
* Write a block to disk. The pos argument passed to this function is modified by this call. Before this call, it should
174-
* point to an unused file location where separator fields will be written, followed by the serialized CBlock data.
175-
* After this call, it will point to the beginning of the serialized CBlock data, after the separator fields
176-
* (BLOCK_SERIALIZATION_HEADER_SIZE)
177-
*/
178-
bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const;
179-
180172
/* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
181173
void FindFilesToPruneManual(
182174
std::set<int>& setFilesToPrune,
@@ -346,8 +338,7 @@ class BlockManager
346338
*
347339
* @param[in] block the block being processed
348340
* @param[in] nHeight the height of the block
349-
* @param[in] pos the position of the serialized CBlock on disk. This is the position returned
350-
* by WriteBlockToDisk pointing at the CBlock, not the separator fields before it
341+
* @param[in] pos the position of the serialized CBlock on disk
351342
*/
352343
void UpdateBlockInfo(const CBlock& block, unsigned int nHeight, const FlatFilePos& pos);
353344

0 commit comments

Comments
 (0)