Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#28458: refactor: Remove unused GetType() from C…
Browse files Browse the repository at this point in the history
…BufferedFile 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
  • Loading branch information
fanquake committed Sep 14, 2023
2 parents f1a9fd6 + fa19c91 commit 8209e86
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/bench/streams_findbyte.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static void FindByte(benchmark::Bench& bench)
data[file_size-1] = 1;
fwrite(&data, sizeof(uint8_t), file_size, file);
rewind(file);
CBufferedFile bf(file, /*nBufSize=*/file_size + 1, /*nRewindIn=*/file_size, 0, 0);
BufferedFile bf{file, /*nBufSize=*/file_size + 1, /*nRewindIn=*/file_size, 0};

bench.run([&] {
bf.SetPos(0);
Expand Down
9 changes: 4 additions & 5 deletions src/dbwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <dbwrapper.h>

#include <clientversion.h>
#include <logging.h>
#include <random.h>
#include <serialize.h>
Expand Down Expand Up @@ -156,9 +155,9 @@ struct CDBBatch::WriteBatchImpl {
leveldb::WriteBatch batch;
};

CDBBatch::CDBBatch(const CDBWrapper& _parent) : parent(_parent),
m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()},
ssValue(SER_DISK, CLIENT_VERSION){};
CDBBatch::CDBBatch(const CDBWrapper& _parent)
: parent{_parent},
m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()} {};

CDBBatch::~CDBBatch() = default;

Expand All @@ -168,7 +167,7 @@ void CDBBatch::Clear()
size_estimate = 0;
}

void CDBBatch::WriteImpl(Span<const std::byte> key, CDataStream& ssValue)
void CDBBatch::WriteImpl(Span<const std::byte> key, DataStream& ssValue)
{
leveldb::Slice slKey(CharCast(key.data()), key.size());
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
Expand Down
4 changes: 2 additions & 2 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ class CDBBatch
const std::unique_ptr<WriteBatchImpl> m_impl_batch;

DataStream ssKey{};
CDataStream ssValue;
DataStream ssValue{};

size_t size_estimate{0};

void WriteImpl(Span<const std::byte> key, CDataStream& ssValue);
void WriteImpl(Span<const std::byte> key, DataStream& ssValue);
void EraseImpl(Span<const std::byte> key);

public:
Expand Down
2 changes: 1 addition & 1 deletion src/index/txindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bool TxIndex::FindTx(const uint256& tx_hash, uint256& block_hash, CTransactionRe
return false;
}

CAutoFile file(m_chainstate->m_blockman.OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
CAutoFile file{m_chainstate->m_blockman.OpenBlockFile(postx, true), CLIENT_VERSION};
if (file.IsNull()) {
return error("%s: OpenBlockFile failed", __func__);
}
Expand Down
4 changes: 2 additions & 2 deletions src/kernel/mempool_persist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active
if (load_path.empty()) return false;

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

CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
CAutoFile file{filestr, CLIENT_VERSION};

uint64_t version = MEMPOOL_DUMP_VERSION;
file << version;
Expand Down
4 changes: 2 additions & 2 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const
{
// Open history file to append
CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
CAutoFile fileout{OpenBlockFile(pos), CLIENT_VERSION};
if (fileout.IsNull()) {
return error("WriteBlockToDisk: OpenBlockFile failed");
}
Expand Down Expand Up @@ -878,7 +878,7 @@ bool BlockManager::ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos) cons
block.SetNull();

// Open history file to read
CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
CAutoFile filein{OpenBlockFile(pos, true), CLIENT_VERSION};
if (filein.IsNull()) {
return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
}
Expand Down
22 changes: 9 additions & 13 deletions src/streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,12 +550,10 @@ class AutoFile
class CAutoFile : public AutoFile
{
private:
const int nType;
const int nVersion;

public:
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} {}
int GetType() const { return nType; }
explicit CAutoFile(std::FILE* file, int version, std::vector<std::byte> data_xor = {}) : AutoFile{file, std::move(data_xor)}, nVersion{version} {}
int GetVersion() const { return nVersion; }

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

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

public:
CBufferedFile(FILE* fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
: nType(nTypeIn), nVersion(nVersionIn), nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, std::byte{0})
BufferedFile(FILE* fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nVersionIn)
: nVersion{nVersionIn}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0})
{
if (nRewindIn >= nBufSize)
throw std::ios_base::failure("Rewind limit must be less than buffer size");
src = fileIn;
}

~CBufferedFile()
~BufferedFile()
{
fclose();
}

// Disallow copies
CBufferedFile(const CBufferedFile&) = delete;
CBufferedFile& operator=(const CBufferedFile&) = delete;
BufferedFile(const BufferedFile&) = delete;
BufferedFile& operator=(const BufferedFile&) = delete;

int GetVersion() const { return nVersion; }
int GetType() const { return nType; }

void fclose()
{
Expand Down Expand Up @@ -715,7 +711,7 @@ class CBufferedFile
}

template<typename T>
CBufferedFile& operator>>(T&& obj) {
BufferedFile& operator>>(T&& obj) {
::Unserialize(*this, obj);
return (*this);
}
Expand Down
5 changes: 2 additions & 3 deletions src/test/fuzz/buffered_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ FUZZ_TARGET(buffered_file)
{
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
FuzzedFileProvider fuzzed_file_provider = ConsumeFile(fuzzed_data_provider);
std::optional<CBufferedFile> opt_buffered_file;
std::optional<BufferedFile> opt_buffered_file;
FILE* fuzzed_file = fuzzed_file_provider.open();
try {
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>());
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>());
} catch (const std::ios_base::failure&) {
if (fuzzed_file != nullptr) {
fclose(fuzzed_file);
Expand Down Expand Up @@ -62,7 +62,6 @@ FUZZ_TARGET(buffered_file)
});
}
opt_buffered_file->GetPos();
opt_buffered_file->GetType();
opt_buffered_file->GetVersion();
}
}
13 changes: 6 additions & 7 deletions src/test/streams_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,18 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
// The buffer size (second arg) must be greater than the rewind
// amount (third arg).
try {
CBufferedFile bfbad(file, 25, 25, 222, 333);
BufferedFile bfbad{file, 25, 25, 333};
BOOST_CHECK(false);
} catch (const std::exception& e) {
BOOST_CHECK(strstr(e.what(),
"Rewind limit must be less than buffer size") != nullptr);
}

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

// These two members have no functional effect.
BOOST_CHECK_EQUAL(bf.GetType(), 222);
// This member has no functional effect.
BOOST_CHECK_EQUAL(bf.GetVersion(), 333);

uint8_t i;
Expand Down Expand Up @@ -357,7 +356,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
BOOST_CHECK(false);
} catch (const std::exception& e) {
BOOST_CHECK(strstr(e.what(),
"CBufferedFile::Fill: end of file") != nullptr);
"BufferedFile::Fill: end of file") != nullptr);
}
// Attempting to read beyond the end sets the EOF indicator.
BOOST_CHECK(bf.eof());
Expand Down Expand Up @@ -392,7 +391,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_skip)
rewind(file);

// The buffer is 25 bytes, allow rewinding 10 bytes.
CBufferedFile bf(file, 25, 10, 222, 333);
BufferedFile bf{file, 25, 10, 333};

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

size_t bufSize = InsecureRandRange(300) + 1;
size_t rewindSize = InsecureRandRange(bufSize);
CBufferedFile bf(file, bufSize, rewindSize, 222, 333);
BufferedFile bf{file, bufSize, rewindSize, 333};
size_t currentPos = 0;
size_t maxPos = 0;
for (int step = 0; step < 100; ++step) {
Expand Down
4 changes: 2 additions & 2 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4588,8 +4588,8 @@ void ChainstateManager::LoadExternalBlockFile(

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

0 comments on commit 8209e86

Please sign in to comment.