Skip to content

Commit 9999b89

Browse files
author
MarcoFalke
committed
Make BufferedFile to be a CAutoFile wrapper
This refactor allows to forward some calls to the underlying CAutoFile, instead of re-implementing the logic in the buffered file.
1 parent fa389d9 commit 9999b89

File tree

9 files changed

+23
-27
lines changed

9 files changed

+23
-27
lines changed

src/bench/load_external.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static void LoadExternalBlockFile(benchmark::Bench& bench)
5656
// "rb" is "binary, O_RDONLY", positioned to the start of the file.
5757
// The file will be closed by LoadExternalBlockFile().
5858
CAutoFile file{fsbridge::fopen(blkfile, "rb"), CLIENT_VERSION};
59-
testing_setup->m_node.chainman->LoadExternalBlockFile(file.Get(), &pos, &blocks_with_unknown_parent);
59+
testing_setup->m_node.chainman->LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);
6060
});
6161
fs::remove(blkfile);
6262
}

src/bench/streams_findbyte.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static void FindByte(benchmark::Bench& bench)
2020
data[file_size-1] = 1;
2121
file << data;
2222
std::rewind(file.Get());
23-
BufferedFile bf{file.Get(), /*nBufSize=*/file_size + 1, /*nRewindIn=*/file_size, 0};
23+
BufferedFile bf{file, /*nBufSize=*/file_size + 1, /*nRewindIn=*/file_size};
2424

2525
bench.run([&] {
2626
bf.SetPos(0);

src/node/blockstorage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFile
10201020
break; // This error is logged in OpenBlockFile
10211021
}
10221022
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
1023-
chainman.LoadExternalBlockFile(file.Get(), &pos, &blocks_with_unknown_parent);
1023+
chainman.LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);
10241024
if (chainman.m_interrupt) {
10251025
LogPrintf("Interrupt requested. Exit %s\n", __func__);
10261026
return;
@@ -1039,7 +1039,7 @@ void ImportBlocks(ChainstateManager& chainman, std::vector<fs::path> vImportFile
10391039
CAutoFile file{fsbridge::fopen(path, "rb"), CLIENT_VERSION};
10401040
if (!file.IsNull()) {
10411041
LogPrintf("Importing blocks file %s...\n", fs::PathToString(path));
1042-
chainman.LoadExternalBlockFile(file.Get());
1042+
chainman.LoadExternalBlockFile(file);
10431043
if (chainman.m_interrupt) {
10441044
LogPrintf("Interrupt requested. Exit %s\n", __func__);
10451045
return;

src/streams.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ class CAutoFile : public AutoFile
571571
}
572572
};
573573

574-
/** Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to
574+
/** Wrapper around a CAutoFile& that implements a ring buffer to
575575
* deserialize from. It guarantees the ability to rewind a given number of bytes.
576576
*
577577
* Will automatically close the file when it goes out of scope if not null.
@@ -580,9 +580,7 @@ class CAutoFile : public AutoFile
580580
class BufferedFile
581581
{
582582
private:
583-
const int nVersion;
584-
585-
FILE *src; //!< source file
583+
CAutoFile& m_src;
586584
uint64_t nSrcPos{0}; //!< how many bytes have been read from source
587585
uint64_t m_read_pos{0}; //!< how many bytes have been read from this
588586
uint64_t nReadLimit; //!< up to which position we're allowed to read
@@ -598,9 +596,9 @@ class BufferedFile
598596
readNow = nAvail;
599597
if (readNow == 0)
600598
return false;
601-
size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
599+
size_t nBytes{m_src.detail_fread(Span{vchBuf}.subspan(pos, readNow))};
602600
if (nBytes == 0) {
603-
throw std::ios_base::failure(feof(src) ? "BufferedFile::Fill: end of file" : "BufferedFile::Fill: fread failed");
601+
throw std::ios_base::failure{m_src.feof() ? "BufferedFile::Fill: end of file" : "BufferedFile::Fill: fread failed"};
604602
}
605603
nSrcPos += nBytes;
606604
return true;
@@ -629,19 +627,18 @@ class BufferedFile
629627
}
630628

631629
public:
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})
630+
BufferedFile(CAutoFile& file, uint64_t nBufSize, uint64_t nRewindIn)
631+
: m_src{file}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0})
634632
{
635633
if (nRewindIn >= nBufSize)
636634
throw std::ios_base::failure("Rewind limit must be less than buffer size");
637-
src = fileIn;
638635
}
639636

640-
int GetVersion() const { return nVersion; }
637+
int GetVersion() const { return m_src.GetVersion(); }
641638

642639
//! check whether we're at the end of the source file
643640
bool eof() const {
644-
return m_read_pos == nSrcPos && feof(src);
641+
return m_read_pos == nSrcPos && m_src.feof();
645642
}
646643

647644
//! read a number of bytes

src/test/fuzz/buffered_file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ FUZZ_TARGET(buffered_file)
2121
std::optional<BufferedFile> opt_buffered_file;
2222
CAutoFile fuzzed_file{fuzzed_file_provider.open(), 0};
2323
try {
24-
opt_buffered_file.emplace(fuzzed_file.Get(), fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(0, 4096), 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));
2525
} catch (const std::ios_base::failure&) {
2626
}
2727
if (opt_buffered_file && !fuzzed_file.IsNull()) {

src/test/fuzz/load_external_block_file.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ FUZZ_TARGET(load_external_block_file, .init = initialize_load_external_block_fil
3636
// Corresponds to the -reindex case (track orphan blocks across files).
3737
FlatFilePos flat_file_pos;
3838
std::multimap<uint256, FlatFilePos> blocks_with_unknown_parent;
39-
g_setup->m_node.chainman->LoadExternalBlockFile(fuzzed_block_file.Get(), &flat_file_pos, &blocks_with_unknown_parent);
39+
g_setup->m_node.chainman->LoadExternalBlockFile(fuzzed_block_file, &flat_file_pos, &blocks_with_unknown_parent);
4040
} else {
4141
// Corresponds to the -loadblock= case (orphan blocks aren't tracked across files).
42-
g_setup->m_node.chainman->LoadExternalBlockFile(fuzzed_block_file.Get());
42+
g_setup->m_node.chainman->LoadExternalBlockFile(fuzzed_block_file);
4343
}
4444
}

src/test/streams_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,15 @@ 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-
BufferedFile bfbad{file.Get(), 25, 25, 333};
263+
BufferedFile bfbad{file, 25, 25};
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-
BufferedFile bf{file.Get(), 25, 10, 333};
271+
BufferedFile bf{file, 25, 10};
272272
BOOST_CHECK(!bf.eof());
273273

274274
// This member has no functional effect.
@@ -391,7 +391,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_skip)
391391
std::rewind(file.Get());
392392

393393
// The buffer is 25 bytes, allow rewinding 10 bytes.
394-
BufferedFile bf{file.Get(), 25, 10, 333};
394+
BufferedFile bf{file, 25, 10};
395395

396396
uint8_t i;
397397
// This is like bf >> (7-byte-variable), in that it will cause data
@@ -445,7 +445,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
445445

446446
size_t bufSize = InsecureRandRange(300) + 1;
447447
size_t rewindSize = InsecureRandRange(bufSize);
448-
BufferedFile bf{file.Get(), bufSize, rewindSize, 333};
448+
BufferedFile bf{file, bufSize, rewindSize};
449449
size_t currentPos = 0;
450450
size_t maxPos = 0;
451451
for (int step = 0; step < 100; ++step) {

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4577,7 +4577,7 @@ bool Chainstate::LoadGenesisBlock()
45774577
}
45784578

45794579
void ChainstateManager::LoadExternalBlockFile(
4580-
FILE* fileIn,
4580+
CAutoFile& file_in,
45814581
FlatFilePos* dbp,
45824582
std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent)
45834583
{
@@ -4589,7 +4589,7 @@ void ChainstateManager::LoadExternalBlockFile(
45894589

45904590
int nLoaded = 0;
45914591
try {
4592-
BufferedFile blkdat{fileIn, 2 * MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE + 8, CLIENT_VERSION};
4592+
BufferedFile blkdat{file_in, 2 * MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE + 8};
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();

src/validation.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ class Chainstate
607607
bool ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size)
608608
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
609609

610-
611610
/**
612611
* Update the on-disk chain state.
613612
* The caches and indexes are flushed depending on the mode we're called with
@@ -1087,14 +1086,14 @@ class ChainstateManager
10871086
* -loadblock= option. There's no unknown-parent tracking, so the last two arguments are omitted.
10881087
*
10891088
*
1090-
* @param[in] fileIn FILE handle to file containing blocks to read
1089+
* @param[in] file_in File containing blocks to read
10911090
* @param[in] dbp (optional) Disk block position (only for reindex)
10921091
* @param[in,out] blocks_with_unknown_parent (optional) Map of disk positions for blocks with
10931092
* unknown parent, key is parent block hash
10941093
* (only used for reindex)
10951094
* */
10961095
void LoadExternalBlockFile(
1097-
FILE* fileIn,
1096+
CAutoFile& file_in,
10981097
FlatFilePos* dbp = nullptr,
10991098
std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent = nullptr);
11001099

0 commit comments

Comments
 (0)