Skip to content

Commit 42bc491

Browse files
l0rincryanofsky
andcommitted
refactor,blocks: inline UndoWriteToDisk
`UndoWriteToDisk` wasn't really extracting a meaningful subset of the `WriteUndoDataForBlock` functionality, it's tied closely to the only caller (needs the header size twice, recalculated undo serializes size, returns multiple branches, modifies parameter, needs documentation). The inlined code should only differ in these parts (modernization will be done in other commits): * renamed `_pos` to `pos` in `WriteUndoDataForBlock` to match the parameter name; * inlined `hashBlock` parameter usage into `hasher << block.pprev->GetBlockHash()`; * changed `return false` to `return FatalError`; * capitalize comment. Co-authored-by: Ryan Ofsky <[email protected]>
1 parent 86b85bb commit 42bc491

File tree

2 files changed

+27
-36
lines changed

2 files changed

+27
-36
lines changed

src/node/blockstorage.cpp

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -669,33 +669,6 @@ CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n)
669669
return &m_blockfile_info.at(n);
670670
}
671671

672-
bool BlockManager::UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const
673-
{
674-
// Open history file to append
675-
AutoFile fileout{OpenUndoFile(pos)};
676-
if (fileout.IsNull()) {
677-
LogError("%s: OpenUndoFile failed\n", __func__);
678-
return false;
679-
}
680-
681-
// Write index header
682-
unsigned int nSize = GetSerializeSize(blockundo);
683-
fileout << GetParams().MessageStart() << nSize;
684-
685-
// Write undo data
686-
long fileOutPos = fileout.tell();
687-
pos.nPos = (unsigned int)fileOutPos;
688-
fileout << blockundo;
689-
690-
// calculate & write checksum
691-
HashWriter hasher{};
692-
hasher << hashBlock;
693-
hasher << blockundo;
694-
fileout << hasher.GetHash();
695-
696-
return true;
697-
}
698-
699672
bool BlockManager::UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& index) const
700673
{
701674
const FlatFilePos pos{WITH_LOCK(::cs_main, return index.GetUndoPos())};
@@ -992,33 +965,52 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
992965

993966
// Write undo information to disk
994967
if (block.GetUndoPos().IsNull()) {
995-
FlatFilePos _pos;
996-
if (!FindUndoPos(state, block.nFile, _pos, ::GetSerializeSize(blockundo) + 40)) {
968+
FlatFilePos pos;
969+
if (!FindUndoPos(state, block.nFile, pos, ::GetSerializeSize(blockundo) + 40)) {
997970
LogError("%s: FindUndoPos failed\n", __func__);
998971
return false;
999972
}
1000-
if (!UndoWriteToDisk(blockundo, _pos, block.pprev->GetBlockHash())) {
973+
// Open history file to append
974+
AutoFile fileout{OpenUndoFile(pos)};
975+
if (fileout.IsNull()) {
976+
LogError("%s: OpenUndoFile failed\n", __func__);
1001977
return FatalError(m_opts.notifications, state, _("Failed to write undo data."));
1002978
}
979+
980+
// Write index header
981+
unsigned int nSize = GetSerializeSize(blockundo);
982+
fileout << GetParams().MessageStart() << nSize;
983+
984+
// Write undo data
985+
long fileOutPos = fileout.tell();
986+
pos.nPos = (unsigned int)fileOutPos;
987+
fileout << blockundo;
988+
989+
// Calculate & write checksum
990+
HashWriter hasher{};
991+
hasher << block.pprev->GetBlockHash();
992+
hasher << blockundo;
993+
fileout << hasher.GetHash();
994+
1003995
// rev files are written in block height order, whereas blk files are written as blocks come in (often out of order)
1004996
// we want to flush the rev (undo) file once we've written the last block, which is indicated by the last height
1005997
// in the block file info as below; note that this does not catch the case where the undo writes are keeping up
1006998
// with the block writes (usually when a synced up node is getting newly mined blocks) -- this case is caught in
1007999
// the FindNextBlockPos function
1008-
if (_pos.nFile < cursor.file_num && static_cast<uint32_t>(block.nHeight) == m_blockfile_info[_pos.nFile].nHeightLast) {
1000+
if (pos.nFile < cursor.file_num && static_cast<uint32_t>(block.nHeight) == m_blockfile_info[pos.nFile].nHeightLast) {
10091001
// Do not propagate the return code, a failed flush here should not
10101002
// be an indication for a failed write. If it were propagated here,
10111003
// the caller would assume the undo data not to be written, when in
10121004
// fact it is. Note though, that a failed flush might leave the data
10131005
// file untrimmed.
1014-
if (!FlushUndoFile(_pos.nFile, true)) {
1015-
LogPrintLevel(BCLog::BLOCKSTORAGE, BCLog::Level::Warning, "Failed to flush undo file %05i\n", _pos.nFile);
1006+
if (!FlushUndoFile(pos.nFile, true)) {
1007+
LogPrintLevel(BCLog::BLOCKSTORAGE, BCLog::Level::Warning, "Failed to flush undo file %05i\n", pos.nFile);
10161008
}
1017-
} else if (_pos.nFile == cursor.file_num && block.nHeight > cursor.undo_height) {
1009+
} else if (pos.nFile == cursor.file_num && block.nHeight > cursor.undo_height) {
10181010
cursor.undo_height = block.nHeight;
10191011
}
10201012
// update nUndoPos in block index
1021-
block.nUndoPos = _pos.nPos;
1013+
block.nUndoPos = pos.nPos;
10221014
block.nStatus |= BLOCK_HAVE_UNDO;
10231015
m_dirty_blockindex.insert(&block);
10241016
}

src/node/blockstorage.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ class BlockManager
176176
* (BLOCK_SERIALIZATION_HEADER_SIZE)
177177
*/
178178
bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const;
179-
bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const;
180179

181180
/* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
182181
void FindFilesToPruneManual(

0 commit comments

Comments
 (0)