mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-23 23:27:21 +01:00
refactor,blocks: inline WriteBlockToDisk
Similarly, `WriteBlockToDisk` wasn't really extracting a meaningful subset of the `SaveBlockToDisk` functionality, it's tied closely to the only caller (needs the header size twice, recalculated block serializes size, returns multiple branches, mutates parameter). The inlined code should only differ in these parts (modernization will be done in other commits): * renamed `blockPos` to `pos` in `SaveBlockToDisk` to match the parameter name; * changed `return false` to `return FlatFilePos()`. Also removed remaining references to `SaveBlockToDisk`. Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
This commit is contained in:
parent
42bc491465
commit
dfb2f9d004
2 changed files with 21 additions and 37 deletions
|
@ -936,27 +936,6 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
|
||||||
return true;
|
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)
|
bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex& block)
|
||||||
{
|
{
|
||||||
AssertLockHeld(::cs_main);
|
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,
|
// 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);
|
||||||
FlatFilePos blockPos{FindNextBlockPos(nBlockSize, nHeight, block.GetBlockTime())};
|
FlatFilePos pos{FindNextBlockPos(nBlockSize, nHeight, block.GetBlockTime())};
|
||||||
if (blockPos.IsNull()) {
|
if (pos.IsNull()) {
|
||||||
LogError("%s: FindNextBlockPos failed\n", __func__);
|
LogError("%s: FindNextBlockPos failed\n", __func__);
|
||||||
return FlatFilePos();
|
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."));
|
m_opts.notifications.fatalError(_("Failed to write block."));
|
||||||
return FlatFilePos();
|
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)
|
static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
|
||||||
|
|
|
@ -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) */
|
/** The maximum size of a blk?????.dat file (since 0.8) */
|
||||||
static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
|
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<MessageStartChars> + sizeof(unsigned int);
|
static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = std::tuple_size_v<MessageStartChars> + sizeof(unsigned int);
|
||||||
|
|
||||||
// Because validation code takes pointers to the map's CBlockIndex objects, if
|
// 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.
|
* 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
|
* 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]] FlatFilePos FindNextBlockPos(unsigned int nAddSize, unsigned int nHeight, uint64_t nTime);
|
||||||
[[nodiscard]] bool FlushChainstateBlockFile(int tip_height);
|
[[nodiscard]] bool FlushChainstateBlockFile(int tip_height);
|
||||||
|
@ -169,14 +169,6 @@ private:
|
||||||
|
|
||||||
AutoFile OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false) const;
|
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 */
|
/* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
|
||||||
void FindFilesToPruneManual(
|
void FindFilesToPruneManual(
|
||||||
std::set<int>& setFilesToPrune,
|
std::set<int>& setFilesToPrune,
|
||||||
|
@ -346,8 +338,7 @@ public:
|
||||||
*
|
*
|
||||||
* @param[in] block the block being processed
|
* @param[in] block the block being processed
|
||||||
* @param[in] nHeight the height of the block
|
* @param[in] nHeight the height of the block
|
||||||
* @param[in] pos the position of the serialized CBlock on disk. This is the position returned
|
* @param[in] pos the position of the serialized CBlock on disk
|
||||||
* by WriteBlockToDisk pointing at the CBlock, not the separator fields before it
|
|
||||||
*/
|
*/
|
||||||
void UpdateBlockInfo(const CBlock& block, unsigned int nHeight, const FlatFilePos& pos);
|
void UpdateBlockInfo(const CBlock& block, unsigned int nHeight, const FlatFilePos& pos);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue