Skip to content

Commit 895937e

Browse files
committed
Merge bitcoin#25285: Add AutoFile without ser-type and ser-version and use it where possible
facc2fa Use AutoFile where possible (MacroFake) 6666803 streams: Add AutoFile without ser-type and ser-version (MacroFake) Pull request description: This was done in the context of bitcoin#25284 , but I think it also makes sense standalone. The basic idea is that serialization type should not be initialized when it is not needed. Same for the serialization version. So do this here for `AutoFile`. `CAutoFile` remains in places where it is not yet possible. ACKs for top commit: laanwj: Code review ACK facc2fa fanquake: ACK facc2fa Tree-SHA512: d82d024d55af57565ac53d9d1517afafc12b46964effba0332de62a6c77869356fa77f89e6d4834438fff44c45b64fccdf5a1358bfea03e28dfe55013b3c099d
2 parents 0897b18 + facc2fa commit 895937e

19 files changed

+89
-77
lines changed

src/index/blockfilterindex.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ bool BlockFilterIndex::CustomCommit(CDBBatch& batch)
133133
const FlatFilePos& pos = m_next_filter_pos;
134134

135135
// Flush current filter file to disk.
136-
CAutoFile file(m_filter_fileseq->Open(pos), SER_DISK, CLIENT_VERSION);
136+
AutoFile file{m_filter_fileseq->Open(pos)};
137137
if (file.IsNull()) {
138138
return error("%s: Failed to open filter file %d", __func__, pos.nFile);
139139
}
@@ -147,7 +147,7 @@ bool BlockFilterIndex::CustomCommit(CDBBatch& batch)
147147

148148
bool BlockFilterIndex::ReadFilterFromDisk(const FlatFilePos& pos, const uint256& hash, BlockFilter& filter) const
149149
{
150-
CAutoFile filein(m_filter_fileseq->Open(pos, true), SER_DISK, CLIENT_VERSION);
150+
AutoFile filein{m_filter_fileseq->Open(pos, true)};
151151
if (filein.IsNull()) {
152152
return false;
153153
}
@@ -179,7 +179,7 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
179179

180180
// If writing the filter would overflow the file, flush and move to the next one.
181181
if (pos.nPos + data_size > MAX_FLTR_FILE_SIZE) {
182-
CAutoFile last_file(m_filter_fileseq->Open(pos), SER_DISK, CLIENT_VERSION);
182+
AutoFile last_file{m_filter_fileseq->Open(pos)};
183183
if (last_file.IsNull()) {
184184
LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
185185
return 0;
@@ -205,7 +205,7 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
205205
return 0;
206206
}
207207

208-
CAutoFile fileout(m_filter_fileseq->Open(pos), SER_DISK, CLIENT_VERSION);
208+
AutoFile fileout{m_filter_fileseq->Open(pos)};
209209
if (fileout.IsNull()) {
210210
LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
211211
return 0;

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2825,7 +2825,7 @@ void CaptureMessageToFile(const CAddress& addr,
28252825
fs::create_directories(base_path);
28262826

28272827
fs::path path = base_path / (is_incoming ? "msgs_recv.dat" : "msgs_sent.dat");
2828-
CAutoFile f(fsbridge::fopen(path, "ab"), SER_DISK, CLIENT_VERSION);
2828+
AutoFile f{fsbridge::fopen(path, "ab")};
28292829

28302830
ser_writedata64(f, now.count());
28312831
f.write(MakeByteSpan(msg_type));

src/policy/fees.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ class TxConfirmStats
161161
unsigned int GetMaxConfirms() const { return scale * confAvg.size(); }
162162

163163
/** Write state of estimation data to a file*/
164-
void Write(CAutoFile& fileout) const;
164+
void Write(AutoFile& fileout) const;
165165

166166
/**
167167
* Read saved state of estimation data from a file and replace all internal data structures and
168168
* variables with this state.
169169
*/
170-
void Read(CAutoFile& filein, int nFileVersion, size_t numBuckets);
170+
void Read(AutoFile& filein, int nFileVersion, size_t numBuckets);
171171
};
172172

173173

@@ -390,7 +390,7 @@ double TxConfirmStats::EstimateMedianVal(int confTarget, double sufficientTxVal,
390390
return median;
391391
}
392392

393-
void TxConfirmStats::Write(CAutoFile& fileout) const
393+
void TxConfirmStats::Write(AutoFile& fileout) const
394394
{
395395
fileout << Using<EncodedDoubleFormatter>(decay);
396396
fileout << scale;
@@ -400,7 +400,7 @@ void TxConfirmStats::Write(CAutoFile& fileout) const
400400
fileout << Using<VectorFormatter<VectorFormatter<EncodedDoubleFormatter>>>(failAvg);
401401
}
402402

403-
void TxConfirmStats::Read(CAutoFile& filein, int nFileVersion, size_t numBuckets)
403+
void TxConfirmStats::Read(AutoFile& filein, int nFileVersion, size_t numBuckets)
404404
{
405405
// Read data file and do some very basic sanity checking
406406
// buckets and bucketMap are not updated yet, so don't access them
@@ -546,7 +546,7 @@ CBlockPolicyEstimator::CBlockPolicyEstimator(const fs::path& estimation_filepath
546546
longStats = std::unique_ptr<TxConfirmStats>(new TxConfirmStats(buckets, bucketMap, LONG_BLOCK_PERIODS, LONG_DECAY, LONG_SCALE));
547547

548548
// If the fee estimation file is present, read recorded estimations
549-
CAutoFile est_file(fsbridge::fopen(m_estimation_filepath, "rb"), SER_DISK, CLIENT_VERSION);
549+
AutoFile est_file{fsbridge::fopen(m_estimation_filepath, "rb")};
550550
if (est_file.IsNull() || !Read(est_file)) {
551551
LogPrintf("Failed to read fee estimates from %s. Continue anyway.\n", fs::PathToString(m_estimation_filepath));
552552
}
@@ -904,13 +904,13 @@ CFeeRate CBlockPolicyEstimator::estimateSmartFee(int confTarget, FeeCalculation
904904
void CBlockPolicyEstimator::Flush() {
905905
FlushUnconfirmed();
906906

907-
CAutoFile est_file(fsbridge::fopen(m_estimation_filepath, "wb"), SER_DISK, CLIENT_VERSION);
907+
AutoFile est_file{fsbridge::fopen(m_estimation_filepath, "wb")};
908908
if (est_file.IsNull() || !Write(est_file)) {
909909
LogPrintf("Failed to write fee estimates to %s. Continue anyway.\n", fs::PathToString(m_estimation_filepath));
910910
}
911911
}
912912

913-
bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
913+
bool CBlockPolicyEstimator::Write(AutoFile& fileout) const
914914
{
915915
try {
916916
LOCK(m_cs_fee_estimator);
@@ -935,7 +935,7 @@ bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
935935
return true;
936936
}
937937

938-
bool CBlockPolicyEstimator::Read(CAutoFile& filein)
938+
bool CBlockPolicyEstimator::Read(AutoFile& filein)
939939
{
940940
try {
941941
LOCK(m_cs_fee_estimator);

src/policy/fees.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <string>
2121
#include <vector>
2222

23-
class CAutoFile;
23+
class AutoFile;
2424
class CTxMemPoolEntry;
2525
class TxConfirmStats;
2626

@@ -220,11 +220,11 @@ class CBlockPolicyEstimator
220220
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
221221

222222
/** Write estimation data to a file */
223-
bool Write(CAutoFile& fileout) const
223+
bool Write(AutoFile& fileout) const
224224
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
225225

226226
/** Read estimation data from a file */
227-
bool Read(CAutoFile& filein)
227+
bool Read(AutoFile& filein)
228228
EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
229229

230230
/** Empty mempool transactions on shutdown to record failure to confirm for txs still in mempool */

src/rpc/blockchain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,7 +2307,7 @@ static RPCHelpMan dumptxoutset()
23072307
}
23082308

23092309
FILE* file{fsbridge::fopen(temppath, "wb")};
2310-
CAutoFile afile{file, SER_DISK, CLIENT_VERSION};
2310+
AutoFile afile{file};
23112311
if (afile.IsNull()) {
23122312
throw JSONRPCError(
23132313
RPC_INVALID_PARAMETER,
@@ -2328,7 +2328,7 @@ static RPCHelpMan dumptxoutset()
23282328
UniValue CreateUTXOSnapshot(
23292329
NodeContext& node,
23302330
CChainState& chainstate,
2331-
CAutoFile& afile,
2331+
AutoFile& afile,
23322332
const fs::path& path,
23332333
const fs::path& temppath)
23342334
{

src/rpc/blockchain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES],
5555
UniValue CreateUTXOSnapshot(
5656
node::NodeContext& node,
5757
CChainState& chainstate,
58-
CAutoFile& afile,
58+
AutoFile& afile,
5959
const fs::path& path,
6060
const fs::path& tmppath);
6161

src/streams.h

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -465,35 +465,28 @@ class BitStreamWriter
465465
};
466466

467467

468-
469468
/** Non-refcounted RAII wrapper for FILE*
470469
*
471470
* Will automatically close the file when it goes out of scope if not null.
472471
* If you're returning the file pointer, return file.release().
473472
* If you need to close the file early, use file.fclose() instead of fclose(file).
474473
*/
475-
class CAutoFile
474+
class AutoFile
476475
{
477-
private:
478-
const int nType;
479-
const int nVersion;
480-
476+
protected:
481477
FILE* file;
482478

483479
public:
484-
CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
485-
{
486-
file = filenew;
487-
}
480+
explicit AutoFile(FILE* filenew) : file{filenew} {}
488481

489-
~CAutoFile()
482+
~AutoFile()
490483
{
491484
fclose();
492485
}
493486

494487
// Disallow copies
495-
CAutoFile(const CAutoFile&) = delete;
496-
CAutoFile& operator=(const CAutoFile&) = delete;
488+
AutoFile(const AutoFile&) = delete;
489+
AutoFile& operator=(const AutoFile&) = delete;
497490

498491
void fclose()
499492
{
@@ -504,14 +497,14 @@ class CAutoFile
504497
}
505498

506499
/** Get wrapped FILE* with transfer of ownership.
507-
* @note This will invalidate the CAutoFile object, and makes it the responsibility of the caller
500+
* @note This will invalidate the AutoFile object, and makes it the responsibility of the caller
508501
* of this function to clean up the returned FILE*.
509502
*/
510503
FILE* release() { FILE* ret = file; file = nullptr; return ret; }
511504

512505
/** Get wrapped FILE* without transfer of ownership.
513506
* @note Ownership of the FILE* will remain with this class. Use this only if the scope of the
514-
* CAutoFile outlives use of the passed pointer.
507+
* AutoFile outlives use of the passed pointer.
515508
*/
516509
FILE* Get() const { return file; }
517510

@@ -522,40 +515,62 @@ class CAutoFile
522515
//
523516
// Stream subset
524517
//
525-
int GetType() const { return nType; }
526-
int GetVersion() const { return nVersion; }
527-
528518
void read(Span<std::byte> dst)
529519
{
530-
if (!file)
531-
throw std::ios_base::failure("CAutoFile::read: file handle is nullptr");
520+
if (!file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
532521
if (fread(dst.data(), 1, dst.size(), file) != dst.size()) {
533-
throw std::ios_base::failure(feof(file) ? "CAutoFile::read: end of file" : "CAutoFile::read: fread failed");
522+
throw std::ios_base::failure(feof(file) ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");
534523
}
535524
}
536525

537526
void ignore(size_t nSize)
538527
{
539-
if (!file)
540-
throw std::ios_base::failure("CAutoFile::ignore: file handle is nullptr");
528+
if (!file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");
541529
unsigned char data[4096];
542530
while (nSize > 0) {
543531
size_t nNow = std::min<size_t>(nSize, sizeof(data));
544532
if (fread(data, 1, nNow, file) != nNow)
545-
throw std::ios_base::failure(feof(file) ? "CAutoFile::ignore: end of file" : "CAutoFile::read: fread failed");
533+
throw std::ios_base::failure(feof(file) ? "AutoFile::ignore: end of file" : "AutoFile::read: fread failed");
546534
nSize -= nNow;
547535
}
548536
}
549537

550538
void write(Span<const std::byte> src)
551539
{
552-
if (!file)
553-
throw std::ios_base::failure("CAutoFile::write: file handle is nullptr");
540+
if (!file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
554541
if (fwrite(src.data(), 1, src.size(), file) != src.size()) {
555-
throw std::ios_base::failure("CAutoFile::write: write failed");
542+
throw std::ios_base::failure("AutoFile::write: write failed");
556543
}
557544
}
558545

546+
template <typename T>
547+
AutoFile& operator<<(const T& obj)
548+
{
549+
if (!file) throw std::ios_base::failure("AutoFile::operator<<: file handle is nullptr");
550+
::Serialize(*this, obj);
551+
return *this;
552+
}
553+
554+
template <typename T>
555+
AutoFile& operator>>(T&& obj)
556+
{
557+
if (!file) throw std::ios_base::failure("AutoFile::operator>>: file handle is nullptr");
558+
::Unserialize(*this, obj);
559+
return *this;
560+
}
561+
};
562+
563+
class CAutoFile : public AutoFile
564+
{
565+
private:
566+
const int nType;
567+
const int nVersion;
568+
569+
public:
570+
CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : AutoFile{filenew}, nType(nTypeIn), nVersion(nVersionIn) {}
571+
int GetType() const { return nType; }
572+
int GetVersion() const { return nVersion; }
573+
559574
template<typename T>
560575
CAutoFile& operator<<(const T& obj)
561576
{

src/test/flatfile_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,26 @@ BOOST_AUTO_TEST_CASE(flatfile_open)
4141

4242
// Write first line to file.
4343
{
44-
CAutoFile file(seq.Open(FlatFilePos(0, pos1)), SER_DISK, CLIENT_VERSION);
44+
AutoFile file{seq.Open(FlatFilePos(0, pos1))};
4545
file << LIMITED_STRING(line1, 256);
4646
}
4747

4848
// Attempt to append to file opened in read-only mode.
4949
{
50-
CAutoFile file(seq.Open(FlatFilePos(0, pos2), true), SER_DISK, CLIENT_VERSION);
50+
AutoFile file{seq.Open(FlatFilePos(0, pos2), true)};
5151
BOOST_CHECK_THROW(file << LIMITED_STRING(line2, 256), std::ios_base::failure);
5252
}
5353

5454
// Append second line to file.
5555
{
56-
CAutoFile file(seq.Open(FlatFilePos(0, pos2)), SER_DISK, CLIENT_VERSION);
56+
AutoFile file{seq.Open(FlatFilePos(0, pos2))};
5757
file << LIMITED_STRING(line2, 256);
5858
}
5959

6060
// Read text from file in read-only mode.
6161
{
6262
std::string text;
63-
CAutoFile file(seq.Open(FlatFilePos(0, pos1), true), SER_DISK, CLIENT_VERSION);
63+
AutoFile file{seq.Open(FlatFilePos(0, pos1), true)};
6464

6565
file >> LIMITED_STRING(text, 256);
6666
BOOST_CHECK_EQUAL(text, line1);
@@ -72,7 +72,7 @@ BOOST_AUTO_TEST_CASE(flatfile_open)
7272
// Read text from file with position offset.
7373
{
7474
std::string text;
75-
CAutoFile file(seq.Open(FlatFilePos(0, pos2)), SER_DISK, CLIENT_VERSION);
75+
AutoFile file{seq.Open(FlatFilePos(0, pos2))};
7676

7777
file >> LIMITED_STRING(text, 256);
7878
BOOST_CHECK_EQUAL(text, line2);
@@ -81,7 +81,7 @@ BOOST_AUTO_TEST_CASE(flatfile_open)
8181
// Ensure another file in the sequence has no data.
8282
{
8383
std::string text;
84-
CAutoFile file(seq.Open(FlatFilePos(1, pos2)), SER_DISK, CLIENT_VERSION);
84+
AutoFile file{seq.Open(FlatFilePos(1, pos2))};
8585
BOOST_CHECK_THROW(file >> LIMITED_STRING(text, 256), std::ios_base::failure);
8686
}
8787
}

src/test/fuzz/autofile.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ FUZZ_TARGET(autofile)
1818
{
1919
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
2020
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
21-
CAutoFile auto_file = fuzzed_auto_file_provider.open();
21+
AutoFile auto_file{fuzzed_auto_file_provider.open()};
2222
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
2323
CallOneOf(
2424
fuzzed_data_provider,
@@ -53,8 +53,6 @@ FUZZ_TARGET(autofile)
5353
});
5454
}
5555
(void)auto_file.Get();
56-
(void)auto_file.GetType();
57-
(void)auto_file.GetVersion();
5856
(void)auto_file.IsNull();
5957
if (fuzzed_data_provider.ConsumeBool()) {
6058
FILE* f = auto_file.release();

src/test/fuzz/policy_estimator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ FUZZ_TARGET_INIT(policy_estimator, initialize_policy_estimator)
7676
}
7777
{
7878
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
79-
CAutoFile fuzzed_auto_file = fuzzed_auto_file_provider.open();
79+
AutoFile fuzzed_auto_file{fuzzed_auto_file_provider.open()};
8080
block_policy_estimator.Write(fuzzed_auto_file);
8181
block_policy_estimator.Read(fuzzed_auto_file);
8282
}

0 commit comments

Comments
 (0)