Return CAutoFile from BlockManager::Open*File()

This is a refactor.
pull/28483/head
MarcoFalke 1 year ago
parent 9999b89cd3
commit fa56c421be
No known key found for this signature in database

@ -79,7 +79,7 @@ bool TxIndex::FindTx(const uint256& tx_hash, uint256& block_hash, CTransactionRe
return false; return false;
} }
CAutoFile file{m_chainstate->m_blockman.OpenBlockFile(postx, true), CLIENT_VERSION}; CAutoFile file{m_chainstate->m_blockman.OpenBlockFile(postx, true)};
if (file.IsNull()) { if (file.IsNull()) {
return error("%s: OpenBlockFile failed", __func__); return error("%s: OpenBlockFile failed", __func__);
} }

@ -459,7 +459,7 @@ bool BlockManager::LoadBlockIndexDB()
} }
for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) { for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) {
FlatFilePos pos(*it, 0); FlatFilePos pos(*it, 0);
if (AutoFile{OpenBlockFile(pos, true)}.IsNull()) { if (OpenBlockFile(pos, true).IsNull()) {
return false; return false;
} }
} }
@ -592,7 +592,7 @@ CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n)
bool BlockManager::UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const bool BlockManager::UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const
{ {
// Open history file to append // Open history file to append
AutoFile fileout{OpenUndoFile(pos)}; CAutoFile fileout{OpenUndoFile(pos)};
if (fileout.IsNull()) { if (fileout.IsNull()) {
return error("%s: OpenUndoFile failed", __func__); return error("%s: OpenUndoFile failed", __func__);
} }
@ -627,7 +627,7 @@ bool BlockManager::UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& in
} }
// Open history file to read // Open history file to read
AutoFile filein{OpenUndoFile(pos, true)}; CAutoFile filein{OpenUndoFile(pos, true)};
if (filein.IsNull()) { if (filein.IsNull()) {
return error("%s: OpenUndoFile failed", __func__); return error("%s: OpenUndoFile failed", __func__);
} }
@ -715,15 +715,15 @@ FlatFileSeq BlockManager::UndoFileSeq() const
return FlatFileSeq(m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE); return FlatFileSeq(m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE);
} }
FILE* BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const CAutoFile BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
{ {
return BlockFileSeq().Open(pos, fReadOnly); return CAutoFile{BlockFileSeq().Open(pos, fReadOnly), CLIENT_VERSION};
} }
/** Open an undo file (rev?????.dat) */ /** Open an undo file (rev?????.dat) */
FILE* BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const CAutoFile BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
{ {
return UndoFileSeq().Open(pos, fReadOnly); return CAutoFile{UndoFileSeq().Open(pos, fReadOnly), CLIENT_VERSION};
} }
fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
@ -824,7 +824,7 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const
{ {
// Open history file to append // Open history file to append
CAutoFile fileout{OpenBlockFile(pos), CLIENT_VERSION}; CAutoFile fileout{OpenBlockFile(pos)};
if (fileout.IsNull()) { if (fileout.IsNull()) {
return error("WriteBlockToDisk: OpenBlockFile failed"); return error("WriteBlockToDisk: OpenBlockFile failed");
} }
@ -880,7 +880,7 @@ bool BlockManager::ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos) cons
block.SetNull(); block.SetNull();
// Open history file to read // Open history file to read
CAutoFile filein{OpenBlockFile(pos, true), CLIENT_VERSION}; CAutoFile filein{OpenBlockFile(pos, true)};
if (filein.IsNull()) { if (filein.IsNull()) {
return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString()); return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
} }
@ -923,7 +923,7 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatF
{ {
FlatFilePos hpos = pos; FlatFilePos hpos = pos;
hpos.nPos -= 8; // Seek back 8 bytes for meta header hpos.nPos -= 8; // Seek back 8 bytes for meta header
AutoFile filein{OpenBlockFile(hpos, true)}; CAutoFile filein{OpenBlockFile(hpos, true)};
if (filein.IsNull()) { if (filein.IsNull()) {
return error("%s: OpenBlockFile failed for %s", __func__, pos.ToString()); return error("%s: OpenBlockFile failed for %s", __func__, pos.ToString());
} }
@ -1015,7 +1015,7 @@ void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFile
if (!fs::exists(chainman.m_blockman.GetBlockPosFilename(pos))) { if (!fs::exists(chainman.m_blockman.GetBlockPosFilename(pos))) {
break; // No block files left to reindex break; // No block files left to reindex
} }
CAutoFile file{chainman.m_blockman.OpenBlockFile(pos, true), CLIENT_VERSION}; CAutoFile file{chainman.m_blockman.OpenBlockFile(pos, true)};
if (file.IsNull()) { if (file.IsNull()) {
break; // This error is logged in OpenBlockFile break; // This error is logged in OpenBlockFile
} }

@ -29,6 +29,7 @@
#include <vector> #include <vector>
class BlockValidationState; class BlockValidationState;
class CAutoFile;
class CBlock; class CBlock;
class CBlockFileInfo; class CBlockFileInfo;
class CBlockUndo; class CBlockUndo;
@ -126,7 +127,7 @@ private:
FlatFileSeq BlockFileSeq() const; FlatFileSeq BlockFileSeq() const;
FlatFileSeq UndoFileSeq() const; FlatFileSeq UndoFileSeq() const;
FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false) const; CAutoFile OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false) const;
bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const; bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const;
bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const; bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const;
@ -278,7 +279,7 @@ public:
void UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); void UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
/** Open a block file (blk?????.dat) */ /** Open a block file (blk?????.dat) */
FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false) const; CAutoFile OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false) const;
/** Translation to a filesystem path */ /** Translation to a filesystem path */
fs::path GetBlockPosFilename(const FlatFilePos& pos) const; fs::path GetBlockPosFilename(const FlatFilePos& pos) const;

@ -74,13 +74,13 @@ BOOST_FIXTURE_TEST_CASE(blockmanager_scan_unlink_already_pruned_files, TestChain
// Check that the file is not unlinked after ScanAndUnlinkAlreadyPrunedFiles // Check that the file is not unlinked after ScanAndUnlinkAlreadyPrunedFiles
// if m_have_pruned is not yet set // if m_have_pruned is not yet set
WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles()); WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
BOOST_CHECK(!AutoFile(blockman.OpenBlockFile(pos, true)).IsNull()); BOOST_CHECK(!blockman.OpenBlockFile(pos, true).IsNull());
// Check that the file is unlinked after ScanAndUnlinkAlreadyPrunedFiles // Check that the file is unlinked after ScanAndUnlinkAlreadyPrunedFiles
// once m_have_pruned is set // once m_have_pruned is set
blockman.m_have_pruned = true; blockman.m_have_pruned = true;
WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles()); WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
BOOST_CHECK(AutoFile(blockman.OpenBlockFile(pos, true)).IsNull()); BOOST_CHECK(blockman.OpenBlockFile(pos, true).IsNull());
// Check that calling with already pruned files doesn't cause an error // Check that calling with already pruned files doesn't cause an error
WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles()); WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
@ -90,7 +90,7 @@ BOOST_FIXTURE_TEST_CASE(blockmanager_scan_unlink_already_pruned_files, TestChain
BOOST_CHECK_NE(old_tip, new_tip); BOOST_CHECK_NE(old_tip, new_tip);
const int new_file_number{WITH_LOCK(chainman->GetMutex(), return new_tip->GetBlockPos().nFile)}; const int new_file_number{WITH_LOCK(chainman->GetMutex(), return new_tip->GetBlockPos().nFile)};
const FlatFilePos new_pos(new_file_number, 0); const FlatFilePos new_pos(new_file_number, 0);
BOOST_CHECK(!AutoFile(blockman.OpenBlockFile(new_pos, true)).IsNull()); BOOST_CHECK(!blockman.OpenBlockFile(new_pos, true).IsNull());
} }
BOOST_FIXTURE_TEST_CASE(blockmanager_block_data_availability, TestChain100Setup) BOOST_FIXTURE_TEST_CASE(blockmanager_block_data_availability, TestChain100Setup)

Loading…
Cancel
Save