Remove unused GetType() from CBufferedFile and CAutoFile

GetType() is only called in tests, so it is unused and can be removed.
This commit is contained in:
MarcoFalke 2023-09-11 17:30:31 +02:00
parent 5c2b3cd4b8
commit fa2f2413b8
No known key found for this signature in database
8 changed files with 16 additions and 22 deletions

View file

@ -16,7 +16,7 @@ static void FindByte(benchmark::Bench& bench)
data[file_size-1] = 1; data[file_size-1] = 1;
fwrite(&data, sizeof(uint8_t), file_size, file); fwrite(&data, sizeof(uint8_t), file_size, file);
rewind(file); rewind(file);
CBufferedFile bf(file, /*nBufSize=*/file_size + 1, /*nRewindIn=*/file_size, 0, 0); CBufferedFile bf{file, /*nBufSize=*/file_size + 1, /*nRewindIn=*/file_size, 0};
bench.run([&] { bench.run([&] {
bf.SetPos(0); bf.SetPos(0);

View file

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

View file

@ -41,7 +41,7 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active
if (load_path.empty()) return false; if (load_path.empty()) return false;
FILE* filestr{opts.mockable_fopen_function(load_path, "rb")}; FILE* filestr{opts.mockable_fopen_function(load_path, "rb")};
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION); CAutoFile file{filestr, CLIENT_VERSION};
if (file.IsNull()) { if (file.IsNull()) {
LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n"); LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
return false; return false;
@ -157,7 +157,7 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
return false; return false;
} }
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION); CAutoFile file{filestr, CLIENT_VERSION};
uint64_t version = MEMPOOL_DUMP_VERSION; uint64_t version = MEMPOOL_DUMP_VERSION;
file << version; file << version;

View file

@ -822,7 +822,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), SER_DISK, CLIENT_VERSION); CAutoFile fileout{OpenBlockFile(pos), CLIENT_VERSION};
if (fileout.IsNull()) { if (fileout.IsNull()) {
return error("WriteBlockToDisk: OpenBlockFile failed"); return error("WriteBlockToDisk: OpenBlockFile failed");
} }
@ -878,7 +878,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), SER_DISK, CLIENT_VERSION); CAutoFile filein{OpenBlockFile(pos, true), CLIENT_VERSION};
if (filein.IsNull()) { if (filein.IsNull()) {
return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString()); return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
} }

View file

@ -550,12 +550,10 @@ public:
class CAutoFile : public AutoFile class CAutoFile : public AutoFile
{ {
private: private:
const int nType;
const int nVersion; const int nVersion;
public: 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} {} explicit CAutoFile(std::FILE* file, int version, std::vector<std::byte> data_xor = {}) : AutoFile{file, std::move(data_xor)}, nVersion{version} {}
int GetType() const { return nType; }
int GetVersion() const { return nVersion; } int GetVersion() const { return nVersion; }
template<typename T> template<typename T>
@ -582,7 +580,6 @@ public:
class CBufferedFile class CBufferedFile
{ {
private: private:
const int nType;
const int nVersion; const int nVersion;
FILE *src; //!< source file FILE *src; //!< source file
@ -632,8 +629,8 @@ private:
} }
public: public:
CBufferedFile(FILE* fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) CBufferedFile(FILE* fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nVersionIn)
: nType(nTypeIn), nVersion(nVersionIn), nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, std::byte{0}) : nVersion{nVersionIn}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0})
{ {
if (nRewindIn >= nBufSize) if (nRewindIn >= nBufSize)
throw std::ios_base::failure("Rewind limit must be less than buffer size"); throw std::ios_base::failure("Rewind limit must be less than buffer size");
@ -650,7 +647,6 @@ public:
CBufferedFile& operator=(const CBufferedFile&) = delete; CBufferedFile& operator=(const CBufferedFile&) = delete;
int GetVersion() const { return nVersion; } int GetVersion() const { return nVersion; }
int GetType() const { return nType; }
void fclose() void fclose()
{ {

View file

@ -21,7 +21,7 @@ FUZZ_TARGET(buffered_file)
std::optional<CBufferedFile> opt_buffered_file; std::optional<CBufferedFile> opt_buffered_file;
FILE* fuzzed_file = fuzzed_file_provider.open(); FILE* fuzzed_file = fuzzed_file_provider.open();
try { 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&) { } catch (const std::ios_base::failure&) {
if (fuzzed_file != nullptr) { if (fuzzed_file != nullptr) {
fclose(fuzzed_file); fclose(fuzzed_file);
@ -62,7 +62,6 @@ FUZZ_TARGET(buffered_file)
}); });
} }
opt_buffered_file->GetPos(); opt_buffered_file->GetPos();
opt_buffered_file->GetType();
opt_buffered_file->GetVersion(); opt_buffered_file->GetVersion();
} }
} }

View file

@ -260,7 +260,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
// The buffer size (second arg) must be greater than the rewind // The buffer size (second arg) must be greater than the rewind
// amount (third arg). // amount (third arg).
try { try {
CBufferedFile bfbad(file, 25, 25, 222, 333); CBufferedFile bfbad{file, 25, 25, 333};
BOOST_CHECK(false); BOOST_CHECK(false);
} catch (const std::exception& e) { } catch (const std::exception& e) {
BOOST_CHECK(strstr(e.what(), BOOST_CHECK(strstr(e.what(),
@ -268,11 +268,10 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
} }
// The buffer is 25 bytes, allow rewinding 10 bytes. // The buffer is 25 bytes, allow rewinding 10 bytes.
CBufferedFile bf(file, 25, 10, 222, 333); CBufferedFile bf{file, 25, 10, 333};
BOOST_CHECK(!bf.eof()); BOOST_CHECK(!bf.eof());
// These two members have no functional effect. // This member has no functional effect.
BOOST_CHECK_EQUAL(bf.GetType(), 222);
BOOST_CHECK_EQUAL(bf.GetVersion(), 333); BOOST_CHECK_EQUAL(bf.GetVersion(), 333);
uint8_t i; uint8_t i;
@ -392,7 +391,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_skip)
rewind(file); rewind(file);
// The buffer is 25 bytes, allow rewinding 10 bytes. // The buffer is 25 bytes, allow rewinding 10 bytes.
CBufferedFile bf(file, 25, 10, 222, 333); CBufferedFile bf{file, 25, 10, 333};
uint8_t i; uint8_t i;
// This is like bf >> (7-byte-variable), in that it will cause data // 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)
size_t bufSize = InsecureRandRange(300) + 1; size_t bufSize = InsecureRandRange(300) + 1;
size_t rewindSize = InsecureRandRange(bufSize); size_t rewindSize = InsecureRandRange(bufSize);
CBufferedFile bf(file, bufSize, rewindSize, 222, 333); CBufferedFile bf{file, bufSize, rewindSize, 333};
size_t currentPos = 0; size_t currentPos = 0;
size_t maxPos = 0; size_t maxPos = 0;
for (int step = 0; step < 100; ++step) { for (int step = 0; step < 100; ++step) {

View file

@ -4520,7 +4520,7 @@ void ChainstateManager::LoadExternalBlockFile(
int nLoaded = 0; int nLoaded = 0;
try { try {
// This takes over fileIn and calls fclose() on it in the CBufferedFile destructor // 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); CBufferedFile 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, // nRewind indicates where to resume scanning in case something goes wrong,
// such as a block fails to deserialize. // such as a block fails to deserialize.
uint64_t nRewind = blkdat.GetPos(); uint64_t nRewind = blkdat.GetPos();