Skip to content

Commit

Permalink
doc: Improve documentation of FindBlockPos and SaveBlockToDisk
Browse files Browse the repository at this point in the history
  • Loading branch information
mzumsande committed May 8, 2024
1 parent 6915091 commit 6a22eed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/node/blockstorage.cpp
Expand Up @@ -1152,9 +1152,8 @@ FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight)
{
unsigned int nBlockSize = ::GetSerializeSize(TX_WITH_WITNESS(block));
FlatFilePos blockPos;
// when known, blockPos.nPos points at the offset of the block data in the blk file. that already accounts for
// the serialization header present in the file (the 4 magic message start bytes + the 4 length bytes = 8 bytes = BLOCK_SERIALIZATION_HEADER_SIZE).
// we add BLOCK_SERIALIZATION_HEADER_SIZE only for new blocks since they will have the serialization header added when written to disk.
// Must also account for the serialization header
// (the 4 magic message start bytes + the 4 length bytes = 8 bytes = BLOCK_SERIALIZATION_HEADER_SIZE)
nBlockSize += static_cast<unsigned int>(BLOCK_SERIALIZATION_HEADER_SIZE);
if (!FindBlockPos(blockPos, nBlockSize, nHeight, block.GetBlockTime())) {
LogError("%s: FindBlockPos failed\n", __func__);
Expand Down
15 changes: 14 additions & 1 deletion src/node/blockstorage.h
Expand Up @@ -155,6 +155,12 @@ class BlockManager
/** Return false if undo file flushing fails. */
[[nodiscard]] bool FlushUndoFile(int block_file, bool finalize = false);

/**
* Helper function performing various preparations before a block can be saved to disk:
* Locates the correct position for the block to be saved, which may be in the current or a new
* block file depending on nAddSize. May flush the previous blockfile to disk if full, updates
* blockfile info, and checks if there is enough disk space to save the block.
*/
[[nodiscard]] bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime);
[[nodiscard]] bool FlushChainstateBlockFile(int tip_height);
bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize);
Expand Down Expand Up @@ -312,7 +318,14 @@ class BlockManager
bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex& block)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);

/** Store block on disk. If dbp is not nullptr, then it provides the known position of the block within a block file on disk. */
/** Store block on disk and update block file statistics.
*
* @param[in] block the block to be stored
* @param[in] nHeight the height of the block
*
* @returns in case of success, the position to which the block was written to
* in case of an error, an empty FlatFilePos
*/
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight);

/** Update blockfile info while processing a block during reindex. The block must be available on disk.
Expand Down

0 comments on commit 6a22eed

Please sign in to comment.