Skip to content

Commit af6d3b9

Browse files
Claude Codeclaude
andcommitted
Merge bitcoin#28904: Drop CAutoFile
4eb2a9e streams: Drop unused CAutoFile (Anthony Towns) cde9a4b refactor: switch from CAutoFile to AutoFile (Anthony Towns) bbd4646 blockstorage: switch from CAutoFile to AutoFile (Anthony Towns) c72ddf0 streams: Remove unused CAutoFile::GetVersion (Anthony Towns) e63f643 streams: Base BufferedFile on AutoFile instead of CAutoFile (Anthony Towns) - Add AutoFile alias for CAutoFile to maintain compatibility - Update blockstorage, validation, and test files to use AutoFile - Remove GetVersion() calls as AutoFile doesn't have this method - Keep CAutoFile class for backward compatibility with rest of codebase 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3b3169d commit af6d3b9

File tree

8 files changed

+29
-24
lines changed

8 files changed

+29
-24
lines changed

src/bench/load_external.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static void LoadExternalBlockFile(benchmark::Bench& bench)
5454
bench.run([&] {
5555
// "rb" is "binary, O_RDONLY", positioned to the start of the file.
5656
// The file will be closed by LoadExternalBlockFile().
57-
FILE* file{fsbridge::fopen(blkfile, "rb")};
57+
AutoFile file{fsbridge::fopen(blkfile, "rb")};
5858
chainstate.LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);
5959
});
6060
fs::remove(blkfile);

src/index/txindex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ bool TxIndex::FindTx(const uint256& tx_hash, uint256& block_hash, CTransactionRe
8080
return false;
8181
}
8282

83-
CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
83+
AutoFile file{OpenBlockFile(postx, true)};
8484
if (file.IsNull()) {
8585
return error("%s: OpenBlockFile failed", __func__);
8686
}

src/node/blockstorage.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ bool BlockManager::LoadBlockIndexDB()
379379
}
380380
for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) {
381381
FlatFilePos pos(*it, 0);
382-
if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) {
382+
if (AutoFile{OpenBlockFile(pos, true)}.IsNull()) {
383383
return false;
384384
}
385385
}
@@ -487,13 +487,13 @@ CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n)
487487
static bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
488488
{
489489
// Open history file to append
490-
CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
490+
AutoFile fileout{OpenUndoFile(pos)};
491491
if (fileout.IsNull()) {
492492
return error("%s: OpenUndoFile failed", __func__);
493493
}
494494

495495
// Write index header
496-
unsigned int nSize = GetSerializeSize(blockundo, fileout.GetVersion());
496+
unsigned int nSize = GetSerializeSize(blockundo, CLIENT_VERSION);
497497
fileout << messageStart << nSize;
498498

499499
// Write undo data
@@ -522,14 +522,14 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex)
522522
}
523523

524524
// Open history file to read
525-
CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
525+
AutoFile filein{OpenUndoFile(pos, true)};
526526
if (filein.IsNull()) {
527527
return error("%s: OpenUndoFile failed", __func__);
528528
}
529529

530530
// Read block
531531
uint256 hashChecksum;
532-
CHashVerifier<CAutoFile> verifier(&filein); // We need a CHashVerifier as reserializing may lose data
532+
CHashVerifier<AutoFile> verifier(&filein); // We need a CHashVerifier as reserializing may lose data
533533
try {
534534
verifier << pindex->pprev->GetBlockHash();
535535
verifier >> blockundo;
@@ -597,15 +597,15 @@ static FlatFileSeq UndoFileSeq()
597597
return FlatFileSeq(gArgs.GetBlocksDirPath(), "rev", UNDOFILE_CHUNK_SIZE);
598598
}
599599

600-
FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly)
600+
AutoFile OpenBlockFile(const FlatFilePos& pos, bool fReadOnly)
601601
{
602-
return BlockFileSeq().Open(pos, fReadOnly);
602+
return AutoFile{BlockFileSeq().Open(pos, fReadOnly)};
603603
}
604604

605605
/** Open an undo file (rev?????.dat) */
606-
static FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly)
606+
static AutoFile OpenUndoFile(const FlatFilePos& pos, bool fReadOnly)
607607
{
608-
return UndoFileSeq().Open(pos, fReadOnly);
608+
return AutoFile{UndoFileSeq().Open(pos, fReadOnly)};
609609
}
610610

611611
fs::path GetBlockPosFilename(const FlatFilePos& pos)
@@ -693,13 +693,13 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
693693
static bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessageHeader::MessageStartChars& messageStart)
694694
{
695695
// Open history file to append
696-
CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
696+
AutoFile fileout{OpenBlockFile(pos)};
697697
if (fileout.IsNull()) {
698698
return error("WriteBlockToDisk: OpenBlockFile failed");
699699
}
700700

701701
// Write index header
702-
unsigned int nSize = GetSerializeSize(block, fileout.GetVersion());
702+
unsigned int nSize = GetSerializeSize(block, CLIENT_VERSION);
703703
fileout << messageStart << nSize;
704704

705705
// Write block
@@ -748,7 +748,7 @@ bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::P
748748
block.SetNull();
749749

750750
// Open history file to read
751-
CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
751+
AutoFile filein{OpenBlockFile(pos, true)};
752752
if (filein.IsNull()) {
753753
return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
754754
}
@@ -840,8 +840,8 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
840840
if (!fs::exists(GetBlockPosFilename(pos))) {
841841
break; // No block files left to reindex
842842
}
843-
FILE* file = OpenBlockFile(pos, true);
844-
if (!file) {
843+
AutoFile file{OpenBlockFile(pos, true)};
844+
if (file.IsNull()) {
845845
break; // This error is logged in OpenBlockFile
846846
}
847847
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
@@ -861,8 +861,8 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
861861

862862
// -loadblock=
863863
for (const fs::path& path : vImportFiles) {
864-
FILE *file = fsbridge::fopen(path, "rb");
865-
if (file) {
864+
AutoFile file{fsbridge::fopen(path, "rb")};
865+
if (!file.IsNull()) {
866866
LogPrintf("Importing blocks file %s...\n", fs::PathToString(path));
867867
chainman.ActiveChainstate().LoadExternalBlockFile(file);
868868
if (ShutdownRequested()) {

src/node/blockstorage.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
extern RecursiveMutex cs_main;
2020

2121
class ArgsManager;
22+
class AutoFile;
2223
class BlockValidationState;
2324
class CBlock;
2425
class CBlockUndo;
@@ -211,7 +212,7 @@ class BlockManager
211212
void CleanupBlockRevFiles();
212213

213214
/** Open a block file (blk?????.dat) */
214-
FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false);
215+
AutoFile OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false);
215216
/** Translation to a filesystem path */
216217
fs::path GetBlockPosFilename(const FlatFilePos& pos);
217218

src/streams.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,9 @@ class CAutoFile
596596
}
597597
};
598598

599+
/** Alias for CAutoFile to match Bitcoin's naming convention */
600+
using AutoFile = CAutoFile;
601+
599602
/** Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to
600603
* deserialize from. It guarantees the ability to rewind a given number of bytes.
601604
*

src/test/fuzz/load_external_block_file.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ FUZZ_TARGET(load_external_block_file, .init = initialize_load_external_block_fil
2727
{
2828
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
2929
FuzzedFileProvider fuzzed_file_provider = ConsumeFile(fuzzed_data_provider);
30-
FILE* fuzzed_block_file = fuzzed_file_provider.open();
31-
if (fuzzed_block_file == nullptr) {
30+
AutoFile fuzzed_block_file{fuzzed_file_provider.open()};
31+
if (fuzzed_block_file.IsNull()) {
3232
return;
3333
}
3434
if (fuzzed_data_provider.ConsumeBool()) {

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4940,7 +4940,7 @@ bool CChainState::LoadGenesisBlock()
49404940
}
49414941

49424942
void CChainState::LoadExternalBlockFile(
4943-
FILE* fileIn,
4943+
AutoFile& fileIn,
49444944
FlatFilePos* dbp,
49454945
std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent)
49464946
{
@@ -4955,7 +4955,7 @@ void CChainState::LoadExternalBlockFile(
49554955
try {
49564956
unsigned int nMaxBlockSize = MaxBlockSize();
49574957
// This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
4958-
CBufferedFile blkdat(fileIn, 2*nMaxBlockSize, nMaxBlockSize+8, SER_DISK, CLIENT_VERSION);
4958+
CBufferedFile blkdat(fileIn.Get(), 2*nMaxBlockSize, nMaxBlockSize+8, SER_DISK, CLIENT_VERSION);
49594959
// nRewind indicates where to resume scanning in case something goes wrong,
49604960
// such as a block fails to deserialize.
49614961
uint64_t nRewind = blkdat.GetPos();

src/validation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <utility>
4343
#include <vector>
4444

45+
class AutoFile;
4546
class CChainState;
4647
class CBlockTreeDB;
4748
class CChainParams;
@@ -634,7 +635,7 @@ class CChainState
634635
* (only used for reindex)
635636
* */
636637
void LoadExternalBlockFile(
637-
FILE* fileIn,
638+
AutoFile& fileIn,
638639
FlatFilePos* dbp = nullptr,
639640
std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent = nullptr)
640641
EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex);

0 commit comments

Comments
 (0)