Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit facc2fa

Browse files
author
MacroFake
committedJun 29, 2022
Use AutoFile where possible
1 parent 6666803 commit facc2fa

17 files changed

+46
-49
lines changed
 

‎src/index/blockfilterindex.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ bool BlockFilterIndex::CommitInternal(CDBBatch& batch)
131131
const FlatFilePos& pos = m_next_filter_pos;
132132

133133
// Flush current filter file to disk.
134-
CAutoFile file(m_filter_fileseq->Open(pos), SER_DISK, CLIENT_VERSION);
134+
AutoFile file{m_filter_fileseq->Open(pos)};
135135
if (file.IsNull()) {
136136
return error("%s: Failed to open filter file %d", __func__, pos.nFile);
137137
}
@@ -145,7 +145,7 @@ bool BlockFilterIndex::CommitInternal(CDBBatch& batch)
145145

146146
bool BlockFilterIndex::ReadFilterFromDisk(const FlatFilePos& pos, BlockFilter& filter) const
147147
{
148-
CAutoFile filein(m_filter_fileseq->Open(pos, true), SER_DISK, CLIENT_VERSION);
148+
AutoFile filein{m_filter_fileseq->Open(pos, true)};
149149
if (filein.IsNull()) {
150150
return false;
151151
}
@@ -173,7 +173,7 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
173173

174174
// If writing the filter would overflow the file, flush and move to the next one.
175175
if (pos.nPos + data_size > MAX_FLTR_FILE_SIZE) {
176-
CAutoFile last_file(m_filter_fileseq->Open(pos), SER_DISK, CLIENT_VERSION);
176+
AutoFile last_file{m_filter_fileseq->Open(pos)};
177177
if (last_file.IsNull()) {
178178
LogPrintf("%s: Failed to open filter file %d\n", __func__, pos.nFile);
179179
return 0;
@@ -199,7 +199,7 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
199199
return 0;
200200
}
201201

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

‎src/net.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3054,7 +3054,7 @@ void CaptureMessageToFile(const CAddress& addr,
30543054
fs::create_directories(base_path);
30553055

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

30593059
ser_writedata64(f, now.count());
30603060
f.write(MakeByteSpan(msg_type));

‎src/policy/fees.cpp

+8-8
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

+3-3
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2293,7 +2293,7 @@ static RPCHelpMan dumptxoutset()
22932293
}
22942294

22952295
FILE* file{fsbridge::fopen(temppath, "wb")};
2296-
CAutoFile afile{file, SER_DISK, CLIENT_VERSION};
2296+
AutoFile afile{file};
22972297
if (afile.IsNull()) {
22982298
throw JSONRPCError(
22992299
RPC_INVALID_PARAMETER,
@@ -2314,7 +2314,7 @@ static RPCHelpMan dumptxoutset()
23142314
UniValue CreateUTXOSnapshot(
23152315
NodeContext& node,
23162316
CChainState& chainstate,
2317-
CAutoFile& afile,
2317+
AutoFile& afile,
23182318
const fs::path& path,
23192319
const fs::path& temppath)
23202320
{

‎src/rpc/blockchain.h

+1-1
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/test/flatfile_tests.cpp

+6-6
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

+1-3
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

+1-1
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
}

‎src/test/fuzz/policy_estimator_io.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ FUZZ_TARGET_INIT(policy_estimator_io, initialize_policy_estimator_io)
2626
{
2727
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
2828
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
29-
CAutoFile fuzzed_auto_file = fuzzed_auto_file_provider.open();
29+
AutoFile fuzzed_auto_file{fuzzed_auto_file_provider.open()};
3030
// Re-using block_policy_estimator across runs to avoid costly creation of CBlockPolicyEstimator object.
3131
static CBlockPolicyEstimator block_policy_estimator{FeeestPath(*g_setup->m_node.args)};
3232
if (block_policy_estimator.Read(fuzzed_auto_file)) {

‎src/test/fuzz/util.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -361,17 +361,16 @@ class FuzzedFileProvider
361361

362362
class FuzzedAutoFileProvider
363363
{
364-
FuzzedDataProvider& m_fuzzed_data_provider;
365364
FuzzedFileProvider m_fuzzed_file_provider;
366365

367366
public:
368-
FuzzedAutoFileProvider(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider{fuzzed_data_provider}, m_fuzzed_file_provider{fuzzed_data_provider}
367+
FuzzedAutoFileProvider(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_file_provider{fuzzed_data_provider}
369368
{
370369
}
371370

372-
CAutoFile open()
371+
AutoFile open()
373372
{
374-
return {m_fuzzed_file_provider.open(), m_fuzzed_data_provider.ConsumeIntegral<int>(), m_fuzzed_data_provider.ConsumeIntegral<int>()};
373+
return AutoFile{m_fuzzed_file_provider.open()};
375374
}
376375
};
377376

‎src/test/fuzz/utxo_snapshot.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ FUZZ_TARGET_INIT(utxo_snapshot, initialize_chain)
3939
Assert(!chainman.SnapshotBlockhash());
4040

4141
{
42-
CAutoFile outfile{fsbridge::fopen(snapshot_path, "wb"), SER_DISK, CLIENT_VERSION};
42+
AutoFile outfile{fsbridge::fopen(snapshot_path, "wb")};
4343
const auto file_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
4444
outfile << Span{file_data};
4545
}
4646

4747
const auto ActivateFuzzedSnapshot{[&] {
48-
CAutoFile infile{fsbridge::fopen(snapshot_path, "rb"), SER_DISK, CLIENT_VERSION};
48+
AutoFile infile{fsbridge::fopen(snapshot_path, "rb")};
4949
SnapshotMetadata metadata;
5050
try {
5151
infile >> metadata;

‎src/test/util/chainstate.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include <boost/test/unit_test.hpp>
1818

19-
const auto NoMalleation = [](CAutoFile& file, node::SnapshotMetadata& meta){};
19+
const auto NoMalleation = [](AutoFile& file, node::SnapshotMetadata& meta){};
2020

2121
/**
2222
* Create and activate a UTXO snapshot, optionally providing a function to
@@ -32,7 +32,7 @@ CreateAndActivateUTXOSnapshot(node::NodeContext& node, const fs::path root, F ma
3232
WITH_LOCK(::cs_main, height = node.chainman->ActiveHeight());
3333
fs::path snapshot_path = root / fs::u8path(tfm::format("test_snapshot.%d.dat", height));
3434
FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
35-
CAutoFile auto_outfile{outfile, SER_DISK, CLIENT_VERSION};
35+
AutoFile auto_outfile{outfile};
3636

3737
UniValue result = CreateUTXOSnapshot(
3838
node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path);
@@ -42,7 +42,7 @@ CreateAndActivateUTXOSnapshot(node::NodeContext& node, const fs::path root, F ma
4242
// Read the written snapshot in and then activate it.
4343
//
4444
FILE* infile{fsbridge::fopen(snapshot_path, "rb")};
45-
CAutoFile auto_infile{infile, SER_DISK, CLIENT_VERSION};
45+
AutoFile auto_infile{infile};
4646
node::SnapshotMetadata metadata;
4747
auto_infile >> metadata;
4848

‎src/test/validation_chainstatemanager_tests.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
193193

194194
// Should not load malleated snapshots
195195
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
196-
m_node, m_path_root, [](CAutoFile& auto_infile, SnapshotMetadata& metadata) {
196+
m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
197197
// A UTXO is missing but count is correct
198198
metadata.m_coins_count -= 1;
199199

@@ -204,22 +204,22 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
204204
auto_infile >> coin;
205205
}));
206206
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
207-
m_node, m_path_root, [](CAutoFile& auto_infile, SnapshotMetadata& metadata) {
207+
m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
208208
// Coins count is larger than coins in file
209209
metadata.m_coins_count += 1;
210210
}));
211211
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
212-
m_node, m_path_root, [](CAutoFile& auto_infile, SnapshotMetadata& metadata) {
212+
m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
213213
// Coins count is smaller than coins in file
214214
metadata.m_coins_count -= 1;
215215
}));
216216
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
217-
m_node, m_path_root, [](CAutoFile& auto_infile, SnapshotMetadata& metadata) {
217+
m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
218218
// Wrong hash
219219
metadata.m_base_blockhash = uint256::ZERO;
220220
}));
221221
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
222-
m_node, m_path_root, [](CAutoFile& auto_infile, SnapshotMetadata& metadata) {
222+
m_node, m_path_root, [](AutoFile& auto_infile, SnapshotMetadata& metadata) {
223223
// Wrong hash
224224
metadata.m_base_blockhash = uint256::ONE;
225225
}));

‎src/util/asmap.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ std::vector<bool> DecodeAsmap(fs::path path)
195195
{
196196
std::vector<bool> bits;
197197
FILE *filestr = fsbridge::fopen(path, "rb");
198-
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
198+
AutoFile file{filestr};
199199
if (file.IsNull()) {
200200
LogPrintf("Failed to open asmap file from disk\n");
201201
return bits;

‎src/validation.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -4864,7 +4864,7 @@ const AssumeutxoData* ExpectedAssumeutxo(
48644864
}
48654865

48664866
bool ChainstateManager::ActivateSnapshot(
4867-
CAutoFile& coins_file,
4867+
AutoFile& coins_file,
48684868
const SnapshotMetadata& metadata,
48694869
bool in_memory)
48704870
{
@@ -4959,7 +4959,7 @@ static void FlushSnapshotToDisk(CCoinsViewCache& coins_cache, bool snapshot_load
49594959

49604960
bool ChainstateManager::PopulateAndValidateSnapshot(
49614961
CChainState& snapshot_chainstate,
4962-
CAutoFile& coins_file,
4962+
AutoFile& coins_file,
49634963
const SnapshotMetadata& metadata)
49644964
{
49654965
// It's okay to release cs_main before we're done using `coins_cache` because we know

‎src/validation.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ class ChainstateManager
820820
//! Internal helper for ActivateSnapshot().
821821
[[nodiscard]] bool PopulateAndValidateSnapshot(
822822
CChainState& snapshot_chainstate,
823-
CAutoFile& coins_file,
823+
AutoFile& coins_file,
824824
const node::SnapshotMetadata& metadata);
825825

826826
/**
@@ -909,7 +909,7 @@ class ChainstateManager
909909
//! - Move the new chainstate to `m_snapshot_chainstate` and make it our
910910
//! ChainstateActive().
911911
[[nodiscard]] bool ActivateSnapshot(
912-
CAutoFile& coins_file, const node::SnapshotMetadata& metadata, bool in_memory);
912+
AutoFile& coins_file, const node::SnapshotMetadata& metadata, bool in_memory);
913913

914914
//! The most-work chain.
915915
CChainState& ActiveChainstate() const;

0 commit comments

Comments
 (0)
Please sign in to comment.