diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index 3ee6b5d2102..93dd23a5c86 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -936,27 +936,6 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP return true; } -bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const -{ - // Open history file to append - AutoFile fileout{OpenBlockFile(pos)}; - if (fileout.IsNull()) { - LogError("%s: OpenBlockFile failed\n", __func__); - return false; - } - - // Write index header - unsigned int nSize = GetSerializeSize(TX_WITH_WITNESS(block)); - fileout << GetParams().MessageStart() << nSize; - - // Write block - long fileOutPos = fileout.tell(); - pos.nPos = (unsigned int)fileOutPos; - fileout << TX_WITH_WITNESS(block); - - return true; -} - bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex& block) { AssertLockHeld(::cs_main); @@ -1117,16 +1096,30 @@ FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight) // Account for the 4 magic message start bytes + the 4 length bytes (8 bytes total, // defined as BLOCK_SERIALIZATION_HEADER_SIZE) nBlockSize += static_cast(BLOCK_SERIALIZATION_HEADER_SIZE); - FlatFilePos blockPos{FindNextBlockPos(nBlockSize, nHeight, block.GetBlockTime())}; - if (blockPos.IsNull()) { + FlatFilePos pos{FindNextBlockPos(nBlockSize, nHeight, block.GetBlockTime())}; + if (pos.IsNull()) { LogError("%s: FindNextBlockPos failed\n", __func__); return FlatFilePos(); } - if (!WriteBlockToDisk(block, blockPos)) { + + // Open history file to append + AutoFile fileout{OpenBlockFile(pos)}; + if (fileout.IsNull()) { + LogError("%s: OpenBlockFile failed\n", __func__); m_opts.notifications.fatalError(_("Failed to write block.")); return FlatFilePos(); } - return blockPos; + + // Write index header + unsigned int nSize = GetSerializeSize(TX_WITH_WITNESS(block)); + fileout << GetParams().MessageStart() << nSize; + + // Write block + long fileOutPos = fileout.tell(); + pos.nPos = (unsigned int)fileOutPos; + fileout << TX_WITH_WITNESS(block); + + return pos; } static auto InitBlocksdirXorKey(const BlockManager::Options& opts) diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h index 339282227cb..545d65a8032 100644 --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -74,7 +74,7 @@ static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB /** The maximum size of a blk?????.dat file (since 0.8) */ static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB -/** Size of header written by WriteBlockToDisk before a serialized CBlock */ +/** Size of header written by SaveBlockToDisk before a serialized CBlock */ static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = std::tuple_size_v + sizeof(unsigned int); // Because validation code takes pointers to the map's CBlockIndex objects, if @@ -161,7 +161,7 @@ private: * blockfile info, and checks if there is enough disk space to save the block. * * 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 (BLOCK_SERIALIZATION_HEADER_SIZE). */ [[nodiscard]] FlatFilePos FindNextBlockPos(unsigned int nAddSize, unsigned int nHeight, uint64_t nTime); [[nodiscard]] bool FlushChainstateBlockFile(int tip_height); @@ -169,14 +169,6 @@ private: AutoFile OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false) const; - /** - * Write a block to disk. The pos argument passed to this function is modified by this call. Before this call, it should - * point to an unused file location where separator fields will be written, followed by the serialized CBlock data. - * After this call, it will point to the beginning of the serialized CBlock data, after the separator fields - * (BLOCK_SERIALIZATION_HEADER_SIZE) - */ - bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const; - /* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */ void FindFilesToPruneManual( std::set& setFilesToPrune, @@ -346,8 +338,7 @@ public: * * @param[in] block the block being processed * @param[in] nHeight the height of the block - * @param[in] pos the position of the serialized CBlock on disk. This is the position returned - * by WriteBlockToDisk pointing at the CBlock, not the separator fields before it + * @param[in] pos the position of the serialized CBlock on disk */ void UpdateBlockInfo(const CBlock& block, unsigned int nHeight, const FlatFilePos& pos);