Skip to content

Commit ca041fc

Browse files
committed
Merge bitcoin/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) Pull request description: Continuing the move away from `GetVersion()`, replace uses of `CAutoFile` with `AutoFile`. ACKs for top commit: maflcko: re-ACK 4eb2a9e 🖼 TheCharlatan: ACK 4eb2a9e stickies-v: ACK 4eb2a9e Tree-SHA512: 1a68c42fdb725ca4bf573e22794fe7809fea764a5f97ecb33435add3c609d40f336038fb22ab1ea72567530efd39678278c9016f92ed04891afdb310631b4e82
2 parents 3dca308 + 4eb2a9e commit ca041fc

File tree

11 files changed

+43
-65
lines changed

11 files changed

+43
-65
lines changed

src/bench/load_external.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static void LoadExternalBlockFile(benchmark::Bench& bench)
5555
bench.run([&] {
5656
// "rb" is "binary, O_RDONLY", positioned to the start of the file.
5757
// The file will be closed by LoadExternalBlockFile().
58-
CAutoFile file{fsbridge::fopen(blkfile, "rb"), CLIENT_VERSION};
58+
AutoFile file{fsbridge::fopen(blkfile, "rb")};
5959
testing_setup->m_node.chainman->LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);
6060
});
6161
fs::remove(blkfile);

src/bench/streams_findbyte.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
static void FindByte(benchmark::Bench& bench)
1515
{
1616
// Setup
17-
CAutoFile file{fsbridge::fopen("streams_tmp", "w+b"), 0};
17+
AutoFile file{fsbridge::fopen("streams_tmp", "w+b")};
1818
const size_t file_size = 200;
1919
uint8_t data[file_size] = {0};
2020
data[file_size-1] = 1;

src/index/txindex.cpp

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

82-
CAutoFile file{m_chainstate->m_blockman.OpenBlockFile(postx, true)};
82+
AutoFile file{m_chainstate->m_blockman.OpenBlockFile(postx, true)};
8383
if (file.IsNull()) {
8484
return error("%s: OpenBlockFile failed", __func__);
8585
}

src/node/blockstorage.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,32 @@
44

55
#include <node/blockstorage.h>
66

7+
#include <arith_uint256.h>
78
#include <chain.h>
8-
#include <clientversion.h>
9+
#include <consensus/params.h>
910
#include <consensus/validation.h>
1011
#include <dbwrapper.h>
1112
#include <flatfile.h>
1213
#include <hash.h>
13-
#include <kernel/chain.h>
14+
#include <kernel/blockmanager_opts.h>
1415
#include <kernel/chainparams.h>
1516
#include <kernel/messagestartchars.h>
17+
#include <kernel/notifications_interface.h>
1618
#include <logging.h>
1719
#include <pow.h>
20+
#include <primitives/block.h>
21+
#include <primitives/transaction.h>
1822
#include <reverse_iterator.h>
23+
#include <serialize.h>
1924
#include <signet.h>
25+
#include <span.h>
2026
#include <streams.h>
2127
#include <sync.h>
28+
#include <tinyformat.h>
29+
#include <uint256.h>
2230
#include <undo.h>
2331
#include <util/batchpriority.h>
32+
#include <util/check.h>
2433
#include <util/fs.h>
2534
#include <util/signalinterrupt.h>
2635
#include <util/strencodings.h>
@@ -656,7 +665,7 @@ CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n)
656665
bool BlockManager::UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const
657666
{
658667
// Open history file to append
659-
CAutoFile fileout{OpenUndoFile(pos)};
668+
AutoFile fileout{OpenUndoFile(pos)};
660669
if (fileout.IsNull()) {
661670
return error("%s: OpenUndoFile failed", __func__);
662671
}
@@ -691,7 +700,7 @@ bool BlockManager::UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& in
691700
}
692701

693702
// Open history file to read
694-
CAutoFile filein{OpenUndoFile(pos, true)};
703+
AutoFile filein{OpenUndoFile(pos, true)};
695704
if (filein.IsNull()) {
696705
return error("%s: OpenUndoFile failed", __func__);
697706
}
@@ -810,15 +819,15 @@ FlatFileSeq BlockManager::UndoFileSeq() const
810819
return FlatFileSeq(m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE);
811820
}
812821

813-
CAutoFile BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
822+
AutoFile BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
814823
{
815-
return CAutoFile{BlockFileSeq().Open(pos, fReadOnly), CLIENT_VERSION};
824+
return AutoFile{BlockFileSeq().Open(pos, fReadOnly)};
816825
}
817826

818827
/** Open an undo file (rev?????.dat) */
819-
CAutoFile BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
828+
AutoFile BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
820829
{
821-
return CAutoFile{UndoFileSeq().Open(pos, fReadOnly), CLIENT_VERSION};
830+
return AutoFile{UndoFileSeq().Open(pos, fReadOnly)};
822831
}
823832

824833
fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
@@ -950,7 +959,7 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
950959
bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const
951960
{
952961
// Open history file to append
953-
CAutoFile fileout{OpenBlockFile(pos)};
962+
AutoFile fileout{OpenBlockFile(pos)};
954963
if (fileout.IsNull()) {
955964
return error("WriteBlockToDisk: OpenBlockFile failed");
956965
}
@@ -1016,7 +1025,7 @@ bool BlockManager::ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos) cons
10161025
block.SetNull();
10171026

10181027
// Open history file to read
1019-
CAutoFile filein{OpenBlockFile(pos, true)};
1028+
AutoFile filein{OpenBlockFile(pos, true)};
10201029
if (filein.IsNull()) {
10211030
return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
10221031
}
@@ -1059,7 +1068,7 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatF
10591068
{
10601069
FlatFilePos hpos = pos;
10611070
hpos.nPos -= 8; // Seek back 8 bytes for meta header
1062-
CAutoFile filein{OpenBlockFile(hpos, true)};
1071+
AutoFile filein{OpenBlockFile(hpos, true)};
10631072
if (filein.IsNull()) {
10641073
return error("%s: OpenBlockFile failed for %s", __func__, pos.ToString());
10651074
}
@@ -1151,7 +1160,7 @@ void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFile
11511160
if (!fs::exists(chainman.m_blockman.GetBlockPosFilename(pos))) {
11521161
break; // No block files left to reindex
11531162
}
1154-
CAutoFile file{chainman.m_blockman.OpenBlockFile(pos, true)};
1163+
AutoFile file{chainman.m_blockman.OpenBlockFile(pos, true)};
11551164
if (file.IsNull()) {
11561165
break; // This error is logged in OpenBlockFile
11571166
}
@@ -1172,7 +1181,7 @@ void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFile
11721181

11731182
// -loadblock=
11741183
for (const fs::path& path : vImportFiles) {
1175-
CAutoFile file{fsbridge::fopen(path, "rb"), CLIENT_VERSION};
1184+
AutoFile file{fsbridge::fopen(path, "rb")};
11761185
if (!file.IsNull()) {
11771186
LogPrintf("Importing blocks file %s...\n", fs::PathToString(path));
11781187
chainman.LoadExternalBlockFile(file);

src/node/blockstorage.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,36 @@
88
#include <attributes.h>
99
#include <chain.h>
1010
#include <dbwrapper.h>
11+
#include <flatfile.h>
1112
#include <kernel/blockmanager_opts.h>
12-
#include <kernel/chain.h>
1313
#include <kernel/chainparams.h>
1414
#include <kernel/cs_main.h>
1515
#include <kernel/messagestartchars.h>
16+
#include <primitives/block.h>
17+
#include <streams.h>
1618
#include <sync.h>
19+
#include <uint256.h>
1720
#include <util/fs.h>
1821
#include <util/hasher.h>
1922

23+
#include <array>
2024
#include <atomic>
2125
#include <cstdint>
2226
#include <functional>
2327
#include <limits>
2428
#include <map>
2529
#include <memory>
30+
#include <optional>
2631
#include <set>
2732
#include <string>
2833
#include <unordered_map>
2934
#include <utility>
3035
#include <vector>
3136

3237
class BlockValidationState;
33-
class CAutoFile;
34-
class CBlock;
3538
class CBlockUndo;
36-
class CChainParams;
3739
class Chainstate;
3840
class ChainstateManager;
39-
struct CCheckpointData;
40-
struct FlatFilePos;
4141
namespace Consensus {
4242
struct Params;
4343
}
@@ -162,7 +162,7 @@ class BlockManager
162162
FlatFileSeq BlockFileSeq() const;
163163
FlatFileSeq UndoFileSeq() const;
164164

165-
CAutoFile OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false) const;
165+
AutoFile OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false) const;
166166

167167
bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const;
168168
bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const;
@@ -350,7 +350,7 @@ class BlockManager
350350
void UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
351351

352352
/** Open a block file (blk?????.dat) */
353-
CAutoFile OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false) const;
353+
AutoFile OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false) const;
354354

355355
/** Translation to a filesystem path */
356356
fs::path GetBlockPosFilename(const FlatFilePos& pos) const;

src/streams.h

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -505,31 +505,7 @@ class AutoFile
505505
}
506506
};
507507

508-
class CAutoFile : public AutoFile
509-
{
510-
private:
511-
const int nVersion;
512-
513-
public:
514-
explicit CAutoFile(std::FILE* file, int version, std::vector<std::byte> data_xor = {}) : AutoFile{file, std::move(data_xor)}, nVersion{version} {}
515-
int GetVersion() const { return nVersion; }
516-
517-
template<typename T>
518-
CAutoFile& operator<<(const T& obj)
519-
{
520-
::Serialize(*this, obj);
521-
return (*this);
522-
}
523-
524-
template<typename T>
525-
CAutoFile& operator>>(T&& obj)
526-
{
527-
::Unserialize(*this, obj);
528-
return (*this);
529-
}
530-
};
531-
532-
/** Wrapper around a CAutoFile& that implements a ring buffer to
508+
/** Wrapper around an AutoFile& that implements a ring buffer to
533509
* deserialize from. It guarantees the ability to rewind a given number of bytes.
534510
*
535511
* Will automatically close the file when it goes out of scope if not null.
@@ -538,7 +514,7 @@ class CAutoFile : public AutoFile
538514
class BufferedFile
539515
{
540516
private:
541-
CAutoFile& m_src;
517+
AutoFile& m_src;
542518
uint64_t nSrcPos{0}; //!< how many bytes have been read from source
543519
uint64_t m_read_pos{0}; //!< how many bytes have been read from this
544520
uint64_t nReadLimit; //!< up to which position we're allowed to read
@@ -585,15 +561,13 @@ class BufferedFile
585561
}
586562

587563
public:
588-
BufferedFile(CAutoFile& file, uint64_t nBufSize, uint64_t nRewindIn)
564+
BufferedFile(AutoFile& file, uint64_t nBufSize, uint64_t nRewindIn)
589565
: m_src{file}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0})
590566
{
591567
if (nRewindIn >= nBufSize)
592568
throw std::ios_base::failure("Rewind limit must be less than buffer size");
593569
}
594570

595-
int GetVersion() const { return m_src.GetVersion(); }
596-
597571
//! check whether we're at the end of the source file
598572
bool eof() const {
599573
return m_read_pos == nSrcPos && m_src.feof();

src/test/fuzz/buffered_file.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ FUZZ_TARGET(buffered_file)
2020
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
2121
FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
2222
std::optional<BufferedFile> opt_buffered_file;
23-
CAutoFile fuzzed_file{
23+
AutoFile fuzzed_file{
2424
fuzzed_file_provider.open(),
25-
0,
2625
ConsumeRandomLengthByteVector<std::byte>(fuzzed_data_provider),
2726
};
2827
try {
@@ -65,6 +64,5 @@ FUZZ_TARGET(buffered_file)
6564
});
6665
}
6766
opt_buffered_file->GetPos();
68-
opt_buffered_file->GetVersion();
6967
}
7068
}

src/test/fuzz/load_external_block_file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ FUZZ_TARGET(load_external_block_file, .init = initialize_load_external_block_fil
2828
{
2929
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
3030
FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
31-
CAutoFile fuzzed_block_file{fuzzed_file_provider.open(), CLIENT_VERSION};
31+
AutoFile fuzzed_block_file{fuzzed_file_provider.open()};
3232
if (fuzzed_block_file.IsNull()) {
3333
return;
3434
}

src/test/streams_tests.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
249249
BOOST_AUTO_TEST_CASE(streams_buffered_file)
250250
{
251251
fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
252-
CAutoFile file{fsbridge::fopen(streams_test_filename, "w+b"), 333};
252+
AutoFile file{fsbridge::fopen(streams_test_filename, "w+b")};
253253

254254
// The value at each offset is the offset.
255255
for (uint8_t j = 0; j < 40; ++j) {
@@ -271,9 +271,6 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
271271
BufferedFile bf{file, 25, 10};
272272
BOOST_CHECK(!bf.eof());
273273

274-
// This member has no functional effect.
275-
BOOST_CHECK_EQUAL(bf.GetVersion(), 333);
276-
277274
uint8_t i;
278275
bf >> i;
279276
BOOST_CHECK_EQUAL(i, 0);
@@ -383,7 +380,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
383380
BOOST_AUTO_TEST_CASE(streams_buffered_file_skip)
384381
{
385382
fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
386-
CAutoFile file{fsbridge::fopen(streams_test_filename, "w+b"), 333};
383+
AutoFile file{fsbridge::fopen(streams_test_filename, "w+b")};
387384
// The value at each offset is the byte offset (e.g. byte 1 in the file has the value 0x01).
388385
for (uint8_t j = 0; j < 40; ++j) {
389386
file << j;
@@ -436,7 +433,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
436433

437434
fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
438435
for (int rep = 0; rep < 50; ++rep) {
439-
CAutoFile file{fsbridge::fopen(streams_test_filename, "w+b"), 333};
436+
AutoFile file{fsbridge::fopen(streams_test_filename, "w+b")};
440437
size_t fileSize = InsecureRandRange(256);
441438
for (uint8_t i = 0; i < fileSize; ++i) {
442439
file << i;

src/validation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4649,7 +4649,7 @@ bool Chainstate::LoadGenesisBlock()
46494649
}
46504650

46514651
void ChainstateManager::LoadExternalBlockFile(
4652-
CAutoFile& file_in,
4652+
AutoFile& file_in,
46534653
FlatFilePos* dbp,
46544654
std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent)
46554655
{

0 commit comments

Comments
 (0)