Skip to content

Commit fdae638

Browse files
mzumsanderyanofsky
andcommitted
doc: Improve doc for functions involved in saving blocks to disk
In particular, document the flat file positions expected and returned by functions better. Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
1 parent 0d114e3 commit fdae638

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/node/blockstorage.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ class BlockManager
155155
/** Return false if undo file flushing fails. */
156156
[[nodiscard]] bool FlushUndoFile(int block_file, bool finalize = false);
157157

158+
/**
159+
* Helper function performing various preparations before a block can be saved to disk:
160+
* Returns the correct position for the block to be saved, which may be in the current or a new
161+
* block file depending on nAddSize. May flush the previous blockfile to disk if full, updates
162+
* blockfile info, and checks if there is enough disk space to save the block.
163+
*
164+
* If fKnown is false, the nAddSize argument passed to this function should include not just the size of the serialized CBlock, but also the size of
165+
* separator fields which are written before it by WriteBlockToDisk (BLOCK_SERIALIZATION_HEADER_SIZE).
166+
* If fKnown is true, nAddSize should be just the size of the serialized CBlock.
167+
*/
158168
[[nodiscard]] bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown);
159169
[[nodiscard]] bool FlushChainstateBlockFile(int tip_height);
160170
bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize);
@@ -164,6 +174,12 @@ class BlockManager
164174

165175
AutoFile OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false) const;
166176

177+
/**
178+
* Write a block to disk. The pos argument passed to this function is modified by this call. Before this call, it should
179+
* point to an unused file location where separator fields will be written, followed by the serialized CBlock data.
180+
* After this call, it will point to the beginning of the serialized CBlock data, after the separator fields
181+
* (BLOCK_SERIALIZATION_HEADER_SIZE)
182+
*/
167183
bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const;
168184
bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const;
169185

@@ -312,7 +328,16 @@ class BlockManager
312328
bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex& block)
313329
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
314330

315-
/** Store block on disk. If dbp is not nullptr, then it provides the known position of the block within a block file on disk. */
331+
/** Store block on disk and update block file statistics.
332+
* If dbp is non-null, it means the block data is already stored, and dbp contains the file position.
333+
* In this case, the block data will not be written, only the block file statistics will be updated. This case should only happen during reindexing.
334+
*
335+
* @param[in] block the block to be stored
336+
* @param[in] nHeight the height of the block
337+
*
338+
* @returns in case of success, the position to which the block was written to
339+
* in case of an error, an empty FlatFilePos
340+
*/
316341
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, const FlatFilePos* dbp);
317342

318343
/** Whether running in -prune mode. */

0 commit comments

Comments
 (0)