blockstorage: Rename FindBlockPos and have it return a FlatFilePos

The new name reflects that it is no longer called with existing blocks
for which the position is already known.

Returning a FlatFilePos directly simplifies the interface.
This commit is contained in:
Martin Zumsande 2024-05-10 15:08:55 -04:00
parent d9e477c4dc
commit 17103637c6
2 changed files with 10 additions and 9 deletions

View File

@ -848,7 +848,7 @@ fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
return BlockFileSeq().FileName(pos); return BlockFileSeq().FileName(pos);
} }
bool BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime) FlatFilePos BlockManager::FindNextBlockPos(unsigned int nAddSize, unsigned int nHeight, uint64_t nTime)
{ {
LOCK(cs_LastBlockFile); LOCK(cs_LastBlockFile);
@ -897,6 +897,7 @@ bool BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigne
m_blockfile_info.resize(nFile + 1); m_blockfile_info.resize(nFile + 1);
} }
} }
FlatFilePos pos;
pos.nFile = nFile; pos.nFile = nFile;
pos.nPos = m_blockfile_info[nFile].nSize; pos.nPos = m_blockfile_info[nFile].nSize;
@ -927,14 +928,14 @@ bool BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigne
size_t bytes_allocated = BlockFileSeq().Allocate(pos, nAddSize, out_of_space); size_t bytes_allocated = BlockFileSeq().Allocate(pos, nAddSize, out_of_space);
if (out_of_space) { if (out_of_space) {
m_opts.notifications.fatalError(_("Disk space is too low!")); m_opts.notifications.fatalError(_("Disk space is too low!"));
return false; return {};
} }
if (bytes_allocated != 0 && IsPruneMode()) { if (bytes_allocated != 0 && IsPruneMode()) {
m_check_for_pruning = true; m_check_for_pruning = true;
} }
m_dirty_fileinfo.insert(nFile); m_dirty_fileinfo.insert(nFile);
return true; return pos;
} }
void BlockManager::UpdateBlockInfo(const CBlock& block, unsigned int nHeight, const FlatFilePos& pos) void BlockManager::UpdateBlockInfo(const CBlock& block, unsigned int nHeight, const FlatFilePos& pos)
@ -1031,7 +1032,7 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
// we want to flush the rev (undo) file once we've written the last block, which is indicated by the last height // we want to flush the rev (undo) file once we've written the last block, which is indicated by the last height
// in the block file info as below; note that this does not catch the case where the undo writes are keeping up // in the block file info as below; note that this does not catch the case where the undo writes are keeping up
// with the block writes (usually when a synced up node is getting newly mined blocks) -- this case is caught in // with the block writes (usually when a synced up node is getting newly mined blocks) -- this case is caught in
// the FindBlockPos function // the FindNextBlockPos function
if (_pos.nFile < cursor.file_num && static_cast<uint32_t>(block.nHeight) == m_blockfile_info[_pos.nFile].nHeightLast) { if (_pos.nFile < cursor.file_num && static_cast<uint32_t>(block.nHeight) == m_blockfile_info[_pos.nFile].nHeightLast) {
// Do not propagate the return code, a failed flush here should not // Do not propagate the return code, a failed flush here should not
// be an indication for a failed write. If it were propagated here, // be an indication for a failed write. If it were propagated here,
@ -1150,12 +1151,12 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatF
FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight) FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight)
{ {
unsigned int nBlockSize = ::GetSerializeSize(TX_WITH_WITNESS(block)); unsigned int nBlockSize = ::GetSerializeSize(TX_WITH_WITNESS(block));
FlatFilePos blockPos;
// Account for the 4 magic message start bytes + the 4 length bytes (8 bytes total, // Account for the 4 magic message start bytes + the 4 length bytes (8 bytes total,
// defined as BLOCK_SERIALIZATION_HEADER_SIZE) // defined as BLOCK_SERIALIZATION_HEADER_SIZE)
nBlockSize += static_cast<unsigned int>(BLOCK_SERIALIZATION_HEADER_SIZE); nBlockSize += static_cast<unsigned int>(BLOCK_SERIALIZATION_HEADER_SIZE);
if (!FindBlockPos(blockPos, nBlockSize, nHeight, block.GetBlockTime())) { FlatFilePos blockPos{FindNextBlockPos(nBlockSize, nHeight, block.GetBlockTime())};
LogError("%s: FindBlockPos failed\n", __func__); if (blockPos.IsNull()) {
LogError("%s: FindNextBlockPos failed\n", __func__);
return FlatFilePos(); return FlatFilePos();
} }
if (!WriteBlockToDisk(block, blockPos)) { if (!WriteBlockToDisk(block, blockPos)) {

View File

@ -164,7 +164,7 @@ private:
* The nAddSize argument passed to this function should include not just the size of the serialized CBlock, but also the size of * The nAddSize argument passed to this function should include not just the size of the serialized CBlock, but also the size of
* separator fields which are written before it by WriteBlockToDisk (BLOCK_SERIALIZATION_HEADER_SIZE). * separator fields which are written before it by WriteBlockToDisk (BLOCK_SERIALIZATION_HEADER_SIZE).
*/ */
[[nodiscard]] bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime); [[nodiscard]] FlatFilePos FindNextBlockPos(unsigned int nAddSize, unsigned int nHeight, uint64_t nTime);
[[nodiscard]] bool FlushChainstateBlockFile(int tip_height); [[nodiscard]] bool FlushChainstateBlockFile(int tip_height);
bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize); bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize);
@ -221,7 +221,7 @@ private:
//! effectively. //! effectively.
//! //!
//! This data structure maintains separate blockfile number cursors for each //! This data structure maintains separate blockfile number cursors for each
//! BlockfileType. The ASSUMED state is initialized, when necessary, in FindBlockPos(). //! BlockfileType. The ASSUMED state is initialized, when necessary, in FindNextBlockPos().
//! //!
//! The first element is the NORMAL cursor, second is ASSUMED. //! The first element is the NORMAL cursor, second is ASSUMED.
std::array<std::optional<BlockfileCursor>, BlockfileType::NUM_TYPES> std::array<std::optional<BlockfileCursor>, BlockfileType::NUM_TYPES>