@@ -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-
699672bool 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 }
0 commit comments