diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -337,6 +337,7 @@ randomenv.cpp rcu.cpp rpc/request.cpp + blockdb.cpp support/cleanse.cpp support/lockedpool.cpp sync.cpp diff --git a/src/blockdb.h b/src/blockdb.h new file mode 100644 --- /dev/null +++ b/src/blockdb.h @@ -0,0 +1,42 @@ +// Copyright 2020 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_BLOCKDB_H +#define BITCOIN_BLOCKDB_H + +#include + +namespace Consensus { +struct Params; +} + +class CBlock; +class CBlockIndex; + +/** The pre-allocation chunk size for blk?????.dat files (since 0.8) */ +static constexpr unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB +/** The pre-allocation chunk size for rev?????.dat files (since 0.8) */ +static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB + +FlatFileSeq BlockFileSeq(); +FlatFileSeq UndoFileSeq(); +FILE *OpenUndoFile(const FlatFilePos &pos, bool fReadOnly = false); + +/** + * Translation to a filesystem path. + */ +fs::path GetBlockPosFilename(const FlatFilePos &pos); + +/** + * Open a block file (blk?????.dat). + */ +FILE *OpenBlockFile(const FlatFilePos &pos, bool fReadOnly = false); + +/** Functions for disk access for blocks */ +bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, + const Consensus::Params ¶ms); +bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex, + const Consensus::Params ¶ms); + +#endif // BITCOIN_BLOCKDB_H diff --git a/src/blockdb.cpp b/src/blockdb.cpp new file mode 100644 --- /dev/null +++ b/src/blockdb.cpp @@ -0,0 +1,80 @@ +#include + +#include +#include +#include +#include +#include +#include + +extern RecursiveMutex cs_main; + +FlatFileSeq BlockFileSeq() { + return FlatFileSeq(GetBlocksDir(), "blk", BLOCKFILE_CHUNK_SIZE); +} + +FlatFileSeq UndoFileSeq() { + return FlatFileSeq(GetBlocksDir(), "rev", UNDOFILE_CHUNK_SIZE); +} + +FILE *OpenBlockFile(const FlatFilePos &pos, bool fReadOnly) { + return BlockFileSeq().Open(pos, fReadOnly); +} + +/** Open an undo file (rev?????.dat) */ +FILE *OpenUndoFile(const FlatFilePos &pos, bool fReadOnly) { + return UndoFileSeq().Open(pos, fReadOnly); +} + +fs::path GetBlockPosFilename(const FlatFilePos &pos) { + return BlockFileSeq().FileName(pos); +} + +bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, + const Consensus::Params ¶ms) { + block.SetNull(); + + // Open history file to read + CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION); + if (filein.IsNull()) { + return error("ReadBlockFromDisk: OpenBlockFile failed for %s", + pos.ToString()); + } + + // Read block + try { + filein >> block; + } catch (const std::exception &e) { + return error("%s: Deserialize or I/O error - %s at %s", __func__, + e.what(), pos.ToString()); + } + + // Check the header + if (!CheckProofOfWork(block.GetHash(), block.nBits, params)) { + return error("ReadBlockFromDisk: Errors in block header at %s", + pos.ToString()); + } + + return true; +} + +bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex, + const Consensus::Params ¶ms) { + FlatFilePos blockPos; + { + LOCK(cs_main); + blockPos = pindex->GetBlockPos(); + } + + if (!ReadBlockFromDisk(block, blockPos, params)) { + return false; + } + + if (block.GetHash() != pindex->GetBlockHash()) { + return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() " + "doesn't match index for %s at %s", + pindex->ToString(), pindex->GetBlockPos().ToString()); + } + + return true; +} diff --git a/src/index/base.cpp b/src/index/base.cpp --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -2,6 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include diff --git a/src/index/txindex.cpp b/src/index/txindex.cpp --- a/src/index/txindex.cpp +++ b/src/index/txindex.cpp @@ -4,6 +4,7 @@ #include +#include #include #include #include diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -4,6 +4,7 @@ #include +#include #include #include #include diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include diff --git a/src/rest.cpp b/src/rest.cpp --- a/src/rest.cpp +++ b/src/rest.cpp @@ -3,6 +3,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -3,6 +3,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include diff --git a/src/test/util/blockfilter.cpp b/src/test/util/blockfilter.cpp --- a/src/test/util/blockfilter.cpp +++ b/src/test/util/blockfilter.cpp @@ -4,6 +4,7 @@ #include +#include #include #include diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -74,10 +74,6 @@ static const unsigned int DEFAULT_MEMPOOL_EXPIRY = 336; /** The maximum size of a blk?????.dat file (since 0.8) */ static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB -/** The pre-allocation chunk size for blk?????.dat files (since 0.8) */ -static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB -/** The pre-allocation chunk size for rev?????.dat files (since 0.8) */ -static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB /** Maximum number of dedicated script-checking threads allowed */ static const int MAX_SCRIPTCHECK_THREADS = 15; @@ -322,16 +318,6 @@ const CBlockIndex **ppindex = nullptr) LOCKS_EXCLUDED(cs_main); -/** - * Open a block file (blk?????.dat). - */ -FILE *OpenBlockFile(const FlatFilePos &pos, bool fReadOnly = false); - -/** - * Translation to a filesystem path. - */ -fs::path GetBlockPosFilename(const FlatFilePos &pos); - /** * Import blocks from an external file. */ @@ -629,12 +615,6 @@ ScriptExecutionMetrics GetScriptExecutionMetrics() const { return metrics; } }; -/** Functions for disk access for blocks */ -bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, - const Consensus::Params ¶ms); -bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex, - const Consensus::Params ¶ms); - bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex); /** Functions for validating blocks and updating the block tree */ diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -172,9 +173,6 @@ int nManualPruneHeight); static void FindFilesToPrune(std::set &setFilesToPrune, uint64_t nPruneAfterHeight); -static FILE *OpenUndoFile(const FlatFilePos &pos, bool fReadOnly = false); -static FlatFileSeq BlockFileSeq(); -static FlatFileSeq UndoFileSeq(); static uint32_t GetNextBlockScriptFlags(const Consensus::Params ¶ms, const CBlockIndex *pindex); @@ -724,55 +722,6 @@ return true; } -bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, - const Consensus::Params ¶ms) { - block.SetNull(); - - // Open history file to read - CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION); - if (filein.IsNull()) { - return error("ReadBlockFromDisk: OpenBlockFile failed for %s", - pos.ToString()); - } - - // Read block - try { - filein >> block; - } catch (const std::exception &e) { - return error("%s: Deserialize or I/O error - %s at %s", __func__, - e.what(), pos.ToString()); - } - - // Check the header - if (!CheckProofOfWork(block.GetHash(), block.nBits, params)) { - return error("ReadBlockFromDisk: Errors in block header at %s", - pos.ToString()); - } - - return true; -} - -bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex, - const Consensus::Params ¶ms) { - FlatFilePos blockPos; - { - LOCK(cs_main); - blockPos = pindex->GetBlockPos(); - } - - if (!ReadBlockFromDisk(block, blockPos, params)) { - return false; - } - - if (block.GetHash() != pindex->GetBlockHash()) { - return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() " - "doesn't match index for %s at %s", - pindex->ToString(), pindex->GetBlockPos().ToString()); - } - - return true; -} - Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams) { int halvings = nHeight / consensusParams.nSubsidyHalvingInterval; // Force block reward to zero when right shift is undefined. @@ -4478,27 +4427,6 @@ nLastBlockWeCanPrune, count); } -static FlatFileSeq BlockFileSeq() { - return FlatFileSeq(GetBlocksDir(), "blk", BLOCKFILE_CHUNK_SIZE); -} - -static FlatFileSeq UndoFileSeq() { - return FlatFileSeq(GetBlocksDir(), "rev", UNDOFILE_CHUNK_SIZE); -} - -FILE *OpenBlockFile(const FlatFilePos &pos, bool fReadOnly) { - return BlockFileSeq().Open(pos, fReadOnly); -} - -/** Open an undo file (rev?????.dat) */ -static FILE *OpenUndoFile(const FlatFilePos &pos, bool fReadOnly) { - return UndoFileSeq().Open(pos, fReadOnly); -} - -fs::path GetBlockPosFilename(const FlatFilePos &pos) { - return BlockFileSeq().FileName(pos); -} - CBlockIndex *BlockManager::InsertBlockIndex(const BlockHash &hash) { AssertLockHeld(cs_main); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include diff --git a/src/zmq/zmqpublishnotifier.cpp b/src/zmq/zmqpublishnotifier.cpp --- a/src/zmq/zmqpublishnotifier.cpp +++ b/src/zmq/zmqpublishnotifier.cpp @@ -4,6 +4,7 @@ #include +#include #include #include #include @@ -12,7 +13,6 @@ #include #include #include -#include #include