validation: Extract basic block file logic into FlatFileSeq class.

This commit is contained in:
Jim Posen 2019-01-06 11:06:31 -08:00
parent 62e7addb63
commit 9183d6ef65
6 changed files with 80 additions and 6 deletions

View file

@ -128,6 +128,7 @@ BITCOIN_CORE_H = \
core_io.h \
core_memusage.h \
cuckoocache.h \
flatfile.h \
fs.h \
httprpc.h \
httpserver.h \
@ -247,6 +248,7 @@ libbitcoin_server_a_SOURCES = \
chain.cpp \
checkpoints.cpp \
consensus/tx_verify.cpp \
flatfile.cpp \
httprpc.cpp \
httpserver.cpp \
index/base.cpp \

23
src/flatfile.cpp Normal file
View file

@ -0,0 +1,23 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <stdexcept>
#include <flatfile.h>
#include <tinyformat.h>
FlatFileSeq::FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size) :
m_dir(std::move(dir)),
m_prefix(prefix),
m_chunk_size(chunk_size)
{
if (chunk_size == 0) {
throw std::invalid_argument("chunk_size must be positive");
}
}
fs::path FlatFileSeq::FileName(const CDiskBlockPos& pos) const
{
return m_dir / strprintf("%s%05u.dat", m_prefix, pos.nFile);
}

36
src/flatfile.h Normal file
View file

@ -0,0 +1,36 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_FLATFILE_H
#define BITCOIN_FLATFILE_H
#include <chain.h>
#include <fs.h>
/**
* FlatFileSeq represents a sequence of numbered files storing raw data. This class facilitates
* access to and efficient management of these files.
*/
class FlatFileSeq
{
private:
const fs::path m_dir;
const char* const m_prefix;
const size_t m_chunk_size;
public:
/**
* Constructor
*
* @param dir The base directory that all files live in.
* @param prefix A short prefix given to all file names.
* @param chunk_size Disk space is pre-allocated in multiples of this amount.
*/
FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size);
/** Get the name of the file at the given position. */
fs::path FileName(const CDiskBlockPos& pos) const;
};
#endif // BITCOIN_FLATFILE_H

View file

@ -673,7 +673,7 @@ static void ThreadImport(std::vector<fs::path> vImportFiles)
int nFile = 0;
while (true) {
CDiskBlockPos pos(nFile, 0);
if (!fs::exists(GetBlockPosFilename(pos, "blk")))
if (!fs::exists(GetBlockPosFilename(pos)))
break; // No block files left to reindex
FILE *file = OpenBlockFile(pos, true);
if (!file)

View file

@ -15,6 +15,7 @@
#include <consensus/tx_verify.h>
#include <consensus/validation.h>
#include <cuckoocache.h>
#include <flatfile.h>
#include <hash.h>
#include <index/txindex.h>
#include <policy/fees.h>
@ -318,6 +319,8 @@ static void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPr
static void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight);
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);
static FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false);
static FlatFileSeq BlockFileSeq();
static FlatFileSeq UndoFileSeq();
bool CheckFinalTx(const CTransaction &tx, int flags)
{
@ -3657,8 +3660,8 @@ void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
{
for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
CDiskBlockPos pos(*it, 0);
fs::remove(GetBlockPosFilename(pos, "blk"));
fs::remove(GetBlockPosFilename(pos, "rev"));
fs::remove(BlockFileSeq().FileName(pos));
fs::remove(UndoFileSeq().FileName(pos));
LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
}
}
@ -3789,6 +3792,16 @@ static FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fRe
return file;
}
static FlatFileSeq BlockFileSeq()
{
return FlatFileSeq(GetBlocksDir(), "blk", BLOCKFILE_CHUNK_SIZE);
}
static FlatFileSeq UndoFileSeq()
{
return FlatFileSeq(GetBlocksDir(), "rev", UNDOFILE_CHUNK_SIZE);
}
FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
return OpenDiskFile(pos, "blk", fReadOnly);
}
@ -3798,9 +3811,9 @@ static FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
return OpenDiskFile(pos, "rev", fReadOnly);
}
fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
fs::path GetBlockPosFilename(const CDiskBlockPos &pos)
{
return GetBlocksDir() / strprintf("%s%05u.dat", prefix, pos.nFile);
return BlockFileSeq().FileName(pos);
}
CBlockIndex * CChainState::InsertBlockIndex(const uint256& hash)

View file

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