diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index 07878a5602..3ee6b5d210 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -669,33 +669,6 @@ CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n) return &m_blockfile_info.at(n); } -bool BlockManager::UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const -{ - // Open history file to append - AutoFile fileout{OpenUndoFile(pos)}; - if (fileout.IsNull()) { - LogError("%s: OpenUndoFile failed\n", __func__); - return false; - } - - // Write index header - unsigned int nSize = GetSerializeSize(blockundo); - fileout << GetParams().MessageStart() << nSize; - - // Write undo data - long fileOutPos = fileout.tell(); - pos.nPos = (unsigned int)fileOutPos; - fileout << blockundo; - - // calculate & write checksum - HashWriter hasher{}; - hasher << hashBlock; - hasher << blockundo; - fileout << hasher.GetHash(); - - return true; -} - bool BlockManager::UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& index) const { const FlatFilePos pos{WITH_LOCK(::cs_main, return index.GetUndoPos())}; @@ -992,33 +965,52 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid // Write undo information to disk if (block.GetUndoPos().IsNull()) { - FlatFilePos _pos; - if (!FindUndoPos(state, block.nFile, _pos, ::GetSerializeSize(blockundo) + 40)) { + FlatFilePos pos; + if (!FindUndoPos(state, block.nFile, pos, ::GetSerializeSize(blockundo) + 40)) { LogError("%s: FindUndoPos failed\n", __func__); return false; } - if (!UndoWriteToDisk(blockundo, _pos, block.pprev->GetBlockHash())) { + // Open history file to append + AutoFile fileout{OpenUndoFile(pos)}; + if (fileout.IsNull()) { + LogError("%s: OpenUndoFile failed\n", __func__); return FatalError(m_opts.notifications, state, _("Failed to write undo data.")); } + + // Write index header + unsigned int nSize = GetSerializeSize(blockundo); + fileout << GetParams().MessageStart() << nSize; + + // Write undo data + long fileOutPos = fileout.tell(); + pos.nPos = (unsigned int)fileOutPos; + fileout << blockundo; + + // Calculate & write checksum + HashWriter hasher{}; + hasher << block.pprev->GetBlockHash(); + hasher << blockundo; + fileout << hasher.GetHash(); + // rev files are written in block height order, whereas blk files are written as blocks come in (often out of order) // 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 // with the block writes (usually when a synced up node is getting newly mined blocks) -- this case is caught in // the FindNextBlockPos function - if (_pos.nFile < cursor.file_num && static_cast(block.nHeight) == m_blockfile_info[_pos.nFile].nHeightLast) { + if (pos.nFile < cursor.file_num && static_cast(block.nHeight) == m_blockfile_info[pos.nFile].nHeightLast) { // Do not propagate the return code, a failed flush here should not // be an indication for a failed write. If it were propagated here, // the caller would assume the undo data not to be written, when in // fact it is. Note though, that a failed flush might leave the data // file untrimmed. - if (!FlushUndoFile(_pos.nFile, true)) { - LogPrintLevel(BCLog::BLOCKSTORAGE, BCLog::Level::Warning, "Failed to flush undo file %05i\n", _pos.nFile); + if (!FlushUndoFile(pos.nFile, true)) { + LogPrintLevel(BCLog::BLOCKSTORAGE, BCLog::Level::Warning, "Failed to flush undo file %05i\n", pos.nFile); } - } else if (_pos.nFile == cursor.file_num && block.nHeight > cursor.undo_height) { + } else if (pos.nFile == cursor.file_num && block.nHeight > cursor.undo_height) { cursor.undo_height = block.nHeight; } // update nUndoPos in block index - block.nUndoPos = _pos.nPos; + block.nUndoPos = pos.nPos; block.nStatus |= BLOCK_HAVE_UNDO; m_dirty_blockindex.insert(&block); } diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h index ac6db0558d..339282227c 100644 --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -176,7 +176,6 @@ private: * (BLOCK_SERIALIZATION_HEADER_SIZE) */ bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const; - bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const; /* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */ void FindFilesToPruneManual(