Skip to content

Commit

Permalink
validation: Extract basic block file logic into FlatFileSeq class.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimpo authored and furszy committed May 14, 2021
1 parent e65acf0 commit 2619fe8
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 8 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ set(SERVER_SOURCES
./src/chain.cpp
./src/checkpoints.cpp
./src/consensus/tx_verify.cpp
./src/flatfile.cpp
./src/httprpc.cpp
./src/httpserver.cpp
./src/indirectmap.h
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ BITCOIN_CORE_H = \
pairresult.h \
addressbook.h \
wallet/db.h \
flatfile.h \
fs.h \
hash.h \
httprpc.h \
Expand Down Expand Up @@ -326,6 +327,7 @@ libbitcoin_server_a_SOURCES = \
checkpoints.cpp \
consensus/params.cpp \
consensus/tx_verify.cpp \
flatfile.cpp \
consensus/zerocoin_verify.cpp \
evo/deterministicmns.cpp \
evo/evodb.cpp \
Expand Down
48 changes: 48 additions & 0 deletions src/flatfile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 "logging.h"
#include "tinyformat.h"
#include "util.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);
}

FILE* FlatFileSeq::Open(const CDiskBlockPos& pos, bool fReadOnly)
{
if (pos.IsNull())
return nullptr;
fs::path path = FileName(pos);
fs::create_directories(path.parent_path());
FILE* file = fsbridge::fopen(path, fReadOnly ? "rb": "rb+");
if (!file && !fReadOnly)
file = fsbridge::fopen(path, "wb+");
if (!file) {
LogPrintf("Unable to open file %s\n", path.string());
return nullptr;
}
if (pos.nPos) {
if (fseek(file, pos.nPos, SEEK_SET)) {
LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
fclose(file);
return nullptr;
}
}
return file;
}
39 changes: 39 additions & 0 deletions src/flatfile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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;

/** Open a handle to the file at the given position. */
FILE* Open(const CDiskBlockPos& pos, bool fReadOnly = false);
};

#endif // BITCOIN_FLATFILE_H
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ void ThreadImport(const 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)
Expand Down
21 changes: 15 additions & 6 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "consensus/zerocoin_verify.h"
#include "evo/deterministicmns.h"
#include "evo/specialtx.h"
#include "flatfile.h"
#include "fs.h"
#include "guiinterface.h"
#include "init.h"
Expand All @@ -34,13 +35,9 @@
#include "masternode-payments.h"
#include "masternode-sync.h"
#include "masternodeman.h"
#include "messagesigner.h"
#include "netmessagemaker.h"
#include "net_processing.h"
#include "policy/policy.h"
#include "pow.h"
#include "reverse_iterate.h"
#include "sapling/sapling_validation.h"
#include "script/sigcache.h"
#include "spork.h"
#include "sporkdb.h"
Expand Down Expand Up @@ -224,6 +221,8 @@ enum FlushStateMode {

// See definition for documentation
bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode);
static FlatFileSeq BlockFileSeq();
static FlatFileSeq UndoFileSeq();

bool CheckFinalTx(const CTransactionRef& tx, int flags)
{
Expand Down Expand Up @@ -3496,6 +3495,16 @@ FILE* OpenDiskFile(const CDiskBlockPos& pos, const char* prefix, bool fReadOnly)
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);
Expand All @@ -3506,9 +3515,9 @@ 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* InsertBlockIndex(uint256 hash)
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ FILE* OpenBlockFile(const CDiskBlockPos& pos, bool fReadOnly = false);
/** Open an undo file (rev?????.dat) */
FILE* OpenUndoFile(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(FILE* fileIn, CDiskBlockPos* dbp = NULL);
/** Ensures we have a genesis block in the block tree, possibly writing one to disk. */
Expand Down

0 comments on commit 2619fe8

Please sign in to comment.