Skip to content

Commit 9183d6e

Browse files
committed
validation: Extract basic block file logic into FlatFileSeq class.
1 parent 62e7add commit 9183d6e

File tree

6 files changed

+80
-6
lines changed

6 files changed

+80
-6
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ BITCOIN_CORE_H = \
128128
core_io.h \
129129
core_memusage.h \
130130
cuckoocache.h \
131+
flatfile.h \
131132
fs.h \
132133
httprpc.h \
133134
httpserver.h \
@@ -247,6 +248,7 @@ libbitcoin_server_a_SOURCES = \
247248
chain.cpp \
248249
checkpoints.cpp \
249250
consensus/tx_verify.cpp \
251+
flatfile.cpp \
250252
httprpc.cpp \
251253
httpserver.cpp \
252254
index/base.cpp \

src/flatfile.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2019 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <stdexcept>
6+
7+
#include <flatfile.h>
8+
#include <tinyformat.h>
9+
10+
FlatFileSeq::FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size) :
11+
m_dir(std::move(dir)),
12+
m_prefix(prefix),
13+
m_chunk_size(chunk_size)
14+
{
15+
if (chunk_size == 0) {
16+
throw std::invalid_argument("chunk_size must be positive");
17+
}
18+
}
19+
20+
fs::path FlatFileSeq::FileName(const CDiskBlockPos& pos) const
21+
{
22+
return m_dir / strprintf("%s%05u.dat", m_prefix, pos.nFile);
23+
}

src/flatfile.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2019 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_FLATFILE_H
6+
#define BITCOIN_FLATFILE_H
7+
8+
#include <chain.h>
9+
#include <fs.h>
10+
11+
/**
12+
* FlatFileSeq represents a sequence of numbered files storing raw data. This class facilitates
13+
* access to and efficient management of these files.
14+
*/
15+
class FlatFileSeq
16+
{
17+
private:
18+
const fs::path m_dir;
19+
const char* const m_prefix;
20+
const size_t m_chunk_size;
21+
22+
public:
23+
/**
24+
* Constructor
25+
*
26+
* @param dir The base directory that all files live in.
27+
* @param prefix A short prefix given to all file names.
28+
* @param chunk_size Disk space is pre-allocated in multiples of this amount.
29+
*/
30+
FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size);
31+
32+
/** Get the name of the file at the given position. */
33+
fs::path FileName(const CDiskBlockPos& pos) const;
34+
};
35+
36+
#endif // BITCOIN_FLATFILE_H

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ static void ThreadImport(std::vector<fs::path> vImportFiles)
673673
int nFile = 0;
674674
while (true) {
675675
CDiskBlockPos pos(nFile, 0);
676-
if (!fs::exists(GetBlockPosFilename(pos, "blk")))
676+
if (!fs::exists(GetBlockPosFilename(pos)))
677677
break; // No block files left to reindex
678678
FILE *file = OpenBlockFile(pos, true);
679679
if (!file)

src/validation.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <consensus/tx_verify.h>
1616
#include <consensus/validation.h>
1717
#include <cuckoocache.h>
18+
#include <flatfile.h>
1819
#include <hash.h>
1920
#include <index/txindex.h>
2021
#include <policy/fees.h>
@@ -318,6 +319,8 @@ static void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPr
318319
static void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight);
319320
bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks = nullptr);
320321
static FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false);
322+
static FlatFileSeq BlockFileSeq();
323+
static FlatFileSeq UndoFileSeq();
321324

322325
bool CheckFinalTx(const CTransaction &tx, int flags)
323326
{
@@ -3657,8 +3660,8 @@ void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
36573660
{
36583661
for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
36593662
CDiskBlockPos pos(*it, 0);
3660-
fs::remove(GetBlockPosFilename(pos, "blk"));
3661-
fs::remove(GetBlockPosFilename(pos, "rev"));
3663+
fs::remove(BlockFileSeq().FileName(pos));
3664+
fs::remove(UndoFileSeq().FileName(pos));
36623665
LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
36633666
}
36643667
}
@@ -3789,6 +3792,16 @@ static FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fRe
37893792
return file;
37903793
}
37913794

3795+
static FlatFileSeq BlockFileSeq()
3796+
{
3797+
return FlatFileSeq(GetBlocksDir(), "blk", BLOCKFILE_CHUNK_SIZE);
3798+
}
3799+
3800+
static FlatFileSeq UndoFileSeq()
3801+
{
3802+
return FlatFileSeq(GetBlocksDir(), "rev", UNDOFILE_CHUNK_SIZE);
3803+
}
3804+
37923805
FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
37933806
return OpenDiskFile(pos, "blk", fReadOnly);
37943807
}
@@ -3798,9 +3811,9 @@ static FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
37983811
return OpenDiskFile(pos, "rev", fReadOnly);
37993812
}
38003813

3801-
fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
3814+
fs::path GetBlockPosFilename(const CDiskBlockPos &pos)
38023815
{
3803-
return GetBlocksDir() / strprintf("%s%05u.dat", prefix, pos.nFile);
3816+
return BlockFileSeq().FileName(pos);
38043817
}
38053818

38063819
CBlockIndex * CChainState::InsertBlockIndex(const uint256& hash)

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& block, CValidationS
245245
/** Open a block file (blk?????.dat) */
246246
FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly = false);
247247
/** Translation to a filesystem path */
248-
fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix);
248+
fs::path GetBlockPosFilename(const CDiskBlockPos &pos);
249249
/** Import blocks from an external file */
250250
bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp = nullptr);
251251
/** Ensures we have a genesis block in the block tree, possibly writing one to disk. */

0 commit comments

Comments
 (0)