Skip to content

Commit 8209e86

Browse files
committed
Merge bitcoin/bitcoin#28458: refactor: Remove unused GetType() from CBufferedFile and CAutoFile
fa19c91 scripted-diff: Rename CBufferedFile to BufferedFile (MarcoFalke) fa2f241 Remove unused GetType() from CBufferedFile and CAutoFile (MarcoFalke) 5c2b3cd dbwrapper: Use DataStream for batch operations (TheCharlatan) Pull request description: This refactor is required for bitcoin/bitcoin#28052 and bitcoin/bitcoin#28451 Thus, split it out. ACKs for top commit: ajtowns: utACK fa19c91 TheCharlatan: ACK fa19c91 Tree-SHA512: d9c232324702512e45fd73ec3e3170f1e8a8c8f9c49cb613a6b693a9f83358914155527ace2517a2cd626a0cedcada56ef70a2a7812edafb1888fd6765eebba2
2 parents f1a9fd6 + fa19c91 commit 8209e86

10 files changed

+31
-38
lines changed

src/bench/streams_findbyte.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static void FindByte(benchmark::Bench& bench)
1616
data[file_size-1] = 1;
1717
fwrite(&data, sizeof(uint8_t), file_size, file);
1818
rewind(file);
19-
CBufferedFile bf(file, /*nBufSize=*/file_size + 1, /*nRewindIn=*/file_size, 0, 0);
19+
BufferedFile bf{file, /*nBufSize=*/file_size + 1, /*nRewindIn=*/file_size, 0};
2020

2121
bench.run([&] {
2222
bf.SetPos(0);

src/dbwrapper.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <dbwrapper.h>
66

7-
#include <clientversion.h>
87
#include <logging.h>
98
#include <random.h>
109
#include <serialize.h>
@@ -156,9 +155,9 @@ struct CDBBatch::WriteBatchImpl {
156155
leveldb::WriteBatch batch;
157156
};
158157

159-
CDBBatch::CDBBatch(const CDBWrapper& _parent) : parent(_parent),
160-
m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()},
161-
ssValue(SER_DISK, CLIENT_VERSION){};
158+
CDBBatch::CDBBatch(const CDBWrapper& _parent)
159+
: parent{_parent},
160+
m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()} {};
162161

163162
CDBBatch::~CDBBatch() = default;
164163

@@ -168,7 +167,7 @@ void CDBBatch::Clear()
168167
size_estimate = 0;
169168
}
170169

171-
void CDBBatch::WriteImpl(Span<const std::byte> key, CDataStream& ssValue)
170+
void CDBBatch::WriteImpl(Span<const std::byte> key, DataStream& ssValue)
172171
{
173172
leveldb::Slice slKey(CharCast(key.data()), key.size());
174173
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));

src/dbwrapper.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ class CDBBatch
8080
const std::unique_ptr<WriteBatchImpl> m_impl_batch;
8181

8282
DataStream ssKey{};
83-
CDataStream ssValue;
83+
DataStream ssValue{};
8484

8585
size_t size_estimate{0};
8686

87-
void WriteImpl(Span<const std::byte> key, CDataStream& ssValue);
87+
void WriteImpl(Span<const std::byte> key, DataStream& ssValue);
8888
void EraseImpl(Span<const std::byte> key);
8989

9090
public:

src/index/txindex.cpp

+1-1
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), SER_DISK, CLIENT_VERSION);
82+
CAutoFile file{m_chainstate->m_blockman.OpenBlockFile(postx, true), CLIENT_VERSION};
8383
if (file.IsNull()) {
8484
return error("%s: OpenBlockFile failed", __func__);
8585
}

src/kernel/mempool_persist.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active
4141
if (load_path.empty()) return false;
4242

4343
FILE* filestr{opts.mockable_fopen_function(load_path, "rb")};
44-
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
44+
CAutoFile file{filestr, CLIENT_VERSION};
4545
if (file.IsNull()) {
4646
LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
4747
return false;
@@ -157,7 +157,7 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
157157
return false;
158158
}
159159

160-
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
160+
CAutoFile file{filestr, CLIENT_VERSION};
161161

162162
uint64_t version = MEMPOOL_DUMP_VERSION;
163163
file << version;

src/node/blockstorage.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
822822
bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const
823823
{
824824
// Open history file to append
825-
CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
825+
CAutoFile fileout{OpenBlockFile(pos), CLIENT_VERSION};
826826
if (fileout.IsNull()) {
827827
return error("WriteBlockToDisk: OpenBlockFile failed");
828828
}
@@ -878,7 +878,7 @@ bool BlockManager::ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos) cons
878878
block.SetNull();
879879

880880
// Open history file to read
881-
CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
881+
CAutoFile filein{OpenBlockFile(pos, true), CLIENT_VERSION};
882882
if (filein.IsNull()) {
883883
return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
884884
}

src/streams.h

+9-13
Original file line numberDiff line numberDiff line change
@@ -550,12 +550,10 @@ class AutoFile
550550
class CAutoFile : public AutoFile
551551
{
552552
private:
553-
const int nType;
554553
const int nVersion;
555554

556555
public:
557-
explicit CAutoFile(std::FILE* file, int type, int version, std::vector<std::byte> data_xor = {}) : AutoFile{file, std::move(data_xor)}, nType{type}, nVersion{version} {}
558-
int GetType() const { return nType; }
556+
explicit CAutoFile(std::FILE* file, int version, std::vector<std::byte> data_xor = {}) : AutoFile{file, std::move(data_xor)}, nVersion{version} {}
559557
int GetVersion() const { return nVersion; }
560558

561559
template<typename T>
@@ -579,10 +577,9 @@ class CAutoFile : public AutoFile
579577
* Will automatically close the file when it goes out of scope if not null.
580578
* If you need to close the file early, use file.fclose() instead of fclose(file).
581579
*/
582-
class CBufferedFile
580+
class BufferedFile
583581
{
584582
private:
585-
const int nType;
586583
const int nVersion;
587584

588585
FILE *src; //!< source file
@@ -603,7 +600,7 @@ class CBufferedFile
603600
return false;
604601
size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
605602
if (nBytes == 0) {
606-
throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
603+
throw std::ios_base::failure(feof(src) ? "BufferedFile::Fill: end of file" : "BufferedFile::Fill: fread failed");
607604
}
608605
nSrcPos += nBytes;
609606
return true;
@@ -632,25 +629,24 @@ class CBufferedFile
632629
}
633630

634631
public:
635-
CBufferedFile(FILE* fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
636-
: nType(nTypeIn), nVersion(nVersionIn), nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, std::byte{0})
632+
BufferedFile(FILE* fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nVersionIn)
633+
: nVersion{nVersionIn}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0})
637634
{
638635
if (nRewindIn >= nBufSize)
639636
throw std::ios_base::failure("Rewind limit must be less than buffer size");
640637
src = fileIn;
641638
}
642639

643-
~CBufferedFile()
640+
~BufferedFile()
644641
{
645642
fclose();
646643
}
647644

648645
// Disallow copies
649-
CBufferedFile(const CBufferedFile&) = delete;
650-
CBufferedFile& operator=(const CBufferedFile&) = delete;
646+
BufferedFile(const BufferedFile&) = delete;
647+
BufferedFile& operator=(const BufferedFile&) = delete;
651648

652649
int GetVersion() const { return nVersion; }
653-
int GetType() const { return nType; }
654650

655651
void fclose()
656652
{
@@ -715,7 +711,7 @@ class CBufferedFile
715711
}
716712

717713
template<typename T>
718-
CBufferedFile& operator>>(T&& obj) {
714+
BufferedFile& operator>>(T&& obj) {
719715
::Unserialize(*this, obj);
720716
return (*this);
721717
}

src/test/fuzz/buffered_file.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ FUZZ_TARGET(buffered_file)
1818
{
1919
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
2020
FuzzedFileProvider fuzzed_file_provider = ConsumeFile(fuzzed_data_provider);
21-
std::optional<CBufferedFile> opt_buffered_file;
21+
std::optional<BufferedFile> opt_buffered_file;
2222
FILE* fuzzed_file = fuzzed_file_provider.open();
2323
try {
24-
opt_buffered_file.emplace(fuzzed_file, fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeIntegral<int>());
24+
opt_buffered_file.emplace(fuzzed_file, fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), fuzzed_data_provider.ConsumeIntegral<int>());
2525
} catch (const std::ios_base::failure&) {
2626
if (fuzzed_file != nullptr) {
2727
fclose(fuzzed_file);
@@ -62,7 +62,6 @@ FUZZ_TARGET(buffered_file)
6262
});
6363
}
6464
opt_buffered_file->GetPos();
65-
opt_buffered_file->GetType();
6665
opt_buffered_file->GetVersion();
6766
}
6867
}

src/test/streams_tests.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -260,19 +260,18 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
260260
// The buffer size (second arg) must be greater than the rewind
261261
// amount (third arg).
262262
try {
263-
CBufferedFile bfbad(file, 25, 25, 222, 333);
263+
BufferedFile bfbad{file, 25, 25, 333};
264264
BOOST_CHECK(false);
265265
} catch (const std::exception& e) {
266266
BOOST_CHECK(strstr(e.what(),
267267
"Rewind limit must be less than buffer size") != nullptr);
268268
}
269269

270270
// The buffer is 25 bytes, allow rewinding 10 bytes.
271-
CBufferedFile bf(file, 25, 10, 222, 333);
271+
BufferedFile bf{file, 25, 10, 333};
272272
BOOST_CHECK(!bf.eof());
273273

274-
// These two members have no functional effect.
275-
BOOST_CHECK_EQUAL(bf.GetType(), 222);
274+
// This member has no functional effect.
276275
BOOST_CHECK_EQUAL(bf.GetVersion(), 333);
277276

278277
uint8_t i;
@@ -357,7 +356,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
357356
BOOST_CHECK(false);
358357
} catch (const std::exception& e) {
359358
BOOST_CHECK(strstr(e.what(),
360-
"CBufferedFile::Fill: end of file") != nullptr);
359+
"BufferedFile::Fill: end of file") != nullptr);
361360
}
362361
// Attempting to read beyond the end sets the EOF indicator.
363362
BOOST_CHECK(bf.eof());
@@ -392,7 +391,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_skip)
392391
rewind(file);
393392

394393
// The buffer is 25 bytes, allow rewinding 10 bytes.
395-
CBufferedFile bf(file, 25, 10, 222, 333);
394+
BufferedFile bf{file, 25, 10, 333};
396395

397396
uint8_t i;
398397
// This is like bf >> (7-byte-variable), in that it will cause data
@@ -446,7 +445,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
446445

447446
size_t bufSize = InsecureRandRange(300) + 1;
448447
size_t rewindSize = InsecureRandRange(bufSize);
449-
CBufferedFile bf(file, bufSize, rewindSize, 222, 333);
448+
BufferedFile bf{file, bufSize, rewindSize, 333};
450449
size_t currentPos = 0;
451450
size_t maxPos = 0;
452451
for (int step = 0; step < 100; ++step) {

src/validation.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -4588,8 +4588,8 @@ void ChainstateManager::LoadExternalBlockFile(
45884588

45894589
int nLoaded = 0;
45904590
try {
4591-
// This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
4592-
CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE+8, SER_DISK, CLIENT_VERSION);
4591+
// This takes over fileIn and calls fclose() on it in the BufferedFile destructor
4592+
BufferedFile blkdat{fileIn, 2 * MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE + 8, CLIENT_VERSION};
45934593
// nRewind indicates where to resume scanning in case something goes wrong,
45944594
// such as a block fails to deserialize.
45954595
uint64_t nRewind = blkdat.GetPos();

0 commit comments

Comments
 (0)