Skip to content

Commit 1710363

Browse files
committed
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.
1 parent d9e477c commit 1710363

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/node/blockstorage.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
848848
return BlockFileSeq().FileName(pos);
849849
}
850850

851-
bool BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime)
851+
FlatFilePos BlockManager::FindNextBlockPos(unsigned int nAddSize, unsigned int nHeight, uint64_t nTime)
852852
{
853853
LOCK(cs_LastBlockFile);
854854

@@ -897,6 +897,7 @@ bool BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigne
897897
m_blockfile_info.resize(nFile + 1);
898898
}
899899
}
900+
FlatFilePos pos;
900901
pos.nFile = nFile;
901902
pos.nPos = m_blockfile_info[nFile].nSize;
902903

@@ -927,14 +928,14 @@ bool BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigne
927928
size_t bytes_allocated = BlockFileSeq().Allocate(pos, nAddSize, out_of_space);
928929
if (out_of_space) {
929930
m_opts.notifications.fatalError(_("Disk space is too low!"));
930-
return false;
931+
return {};
931932
}
932933
if (bytes_allocated != 0 && IsPruneMode()) {
933934
m_check_for_pruning = true;
934935
}
935936

936937
m_dirty_fileinfo.insert(nFile);
937-
return true;
938+
return pos;
938939
}
939940

940941
void BlockManager::UpdateBlockInfo(const CBlock& block, unsigned int nHeight, const FlatFilePos& pos)
@@ -1031,7 +1032,7 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
10311032
// we want to flush the rev (undo) file once we've written the last block, which is indicated by the last height
10321033
// in the block file info as below; note that this does not catch the case where the undo writes are keeping up
10331034
// with the block writes (usually when a synced up node is getting newly mined blocks) -- this case is caught in
1034-
// the FindBlockPos function
1035+
// the FindNextBlockPos function
10351036
if (_pos.nFile < cursor.file_num && static_cast<uint32_t>(block.nHeight) == m_blockfile_info[_pos.nFile].nHeightLast) {
10361037
// Do not propagate the return code, a failed flush here should not
10371038
// 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
11501151
FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight)
11511152
{
11521153
unsigned int nBlockSize = ::GetSerializeSize(TX_WITH_WITNESS(block));
1153-
FlatFilePos blockPos;
11541154
// Account for the 4 magic message start bytes + the 4 length bytes (8 bytes total,
11551155
// defined as BLOCK_SERIALIZATION_HEADER_SIZE)
11561156
nBlockSize += static_cast<unsigned int>(BLOCK_SERIALIZATION_HEADER_SIZE);
1157-
if (!FindBlockPos(blockPos, nBlockSize, nHeight, block.GetBlockTime())) {
1158-
LogError("%s: FindBlockPos failed\n", __func__);
1157+
FlatFilePos blockPos{FindNextBlockPos(nBlockSize, nHeight, block.GetBlockTime())};
1158+
if (blockPos.IsNull()) {
1159+
LogError("%s: FindNextBlockPos failed\n", __func__);
11591160
return FlatFilePos();
11601161
}
11611162
if (!WriteBlockToDisk(block, blockPos)) {

src/node/blockstorage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class BlockManager
164164
* The nAddSize argument passed to this function should include not just the size of the serialized CBlock, but also the size of
165165
* separator fields which are written before it by WriteBlockToDisk (BLOCK_SERIALIZATION_HEADER_SIZE).
166166
*/
167-
[[nodiscard]] bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime);
167+
[[nodiscard]] FlatFilePos FindNextBlockPos(unsigned int nAddSize, unsigned int nHeight, uint64_t nTime);
168168
[[nodiscard]] bool FlushChainstateBlockFile(int tip_height);
169169
bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize);
170170

@@ -221,7 +221,7 @@ class BlockManager
221221
//! effectively.
222222
//!
223223
//! This data structure maintains separate blockfile number cursors for each
224-
//! BlockfileType. The ASSUMED state is initialized, when necessary, in FindBlockPos().
224+
//! BlockfileType. The ASSUMED state is initialized, when necessary, in FindNextBlockPos().
225225
//!
226226
//! The first element is the NORMAL cursor, second is ASSUMED.
227227
std::array<std::optional<BlockfileCursor>, BlockfileType::NUM_TYPES>

0 commit comments

Comments
 (0)