diff --git a/src/blockdb.h b/src/blockdb.h --- a/src/blockdb.h +++ b/src/blockdb.h @@ -33,10 +33,4 @@ */ 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 --- a/src/blockdb.cpp +++ b/src/blockdb.cpp @@ -36,52 +36,3 @@ 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 @@ -7,12 +7,13 @@ #include #include #include +#include #include #include #include #include #include -#include +#include // For g_chainman #include #include diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp --- a/src/index/blockfilterindex.cpp +++ b/src/index/blockfilterindex.cpp @@ -4,9 +4,9 @@ #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,13 +4,13 @@ #include -#include #include #include #include #include #include #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 @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -5,16 +5,39 @@ #ifndef BITCOIN_NODE_BLOCKSTORAGE_H #define BITCOIN_NODE_BLOCKSTORAGE_H +#include #include #include +#include // For CMessageHeader::MessageStartChars class ArgsManager; +class CBlock; +class CBlockIndex; +class CBlockUndo; +class CChain; +class CChainParams; class ChainstateManager; class Config; +struct FlatFilePos; +namespace Consensus { +struct Params; +} static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false}; +/** Functions for disk access for blocks */ +bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, + const Consensus::Params &consensusParams); +bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex, + const Consensus::Params &consensusParams); +bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex); + +FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, + CChain &active_chain, + const CChainParams &chainparams, + const FlatFilePos *dbp); + void ThreadImport(const Config &config, ChainstateManager &chainman, std::vector vImportFiles, const ArgsManager &args); diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -5,14 +5,122 @@ #include #include +#include #include #include #include +#include #include +#include #include +#include #include #include +// From validation. TODO move here +bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int nHeight, + CChain &active_chain, uint64_t nTime, bool fKnown = false); + +static bool WriteBlockToDisk(const CBlock &block, FlatFilePos &pos, + const CMessageHeader::MessageMagic &messageStart) { + // Open history file to append + CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION); + if (fileout.IsNull()) { + return error("WriteBlockToDisk: OpenBlockFile failed"); + } + + // Write index header + unsigned int nSize = GetSerializeSize(block, fileout.GetVersion()); + fileout << messageStart << nSize; + + // Write block + long fileOutPos = ftell(fileout.Get()); + if (fileOutPos < 0) { + return error("WriteBlockToDisk: ftell failed"); + } + + pos.nPos = (unsigned int)fileOutPos; + fileout << block; + + 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; +} + +/** + * Store block on disk. If dbp is non-nullptr, the file is known to already + * reside on disk. + */ +FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, + CChain &active_chain, + const CChainParams &chainparams, + const FlatFilePos *dbp) { + unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION); + FlatFilePos blockPos; + if (dbp != nullptr) { + blockPos = *dbp; + } + if (!FindBlockPos(blockPos, nBlockSize + 8, nHeight, active_chain, + block.GetBlockTime(), dbp != nullptr)) { + error("%s: FindBlockPos failed", __func__); + return FlatFilePos(); + } + if (dbp == nullptr) { + if (!WriteBlockToDisk(block, blockPos, chainparams.DiskMagic())) { + AbortNode("Failed to write block"); + return FlatFilePos(); + } + } + return blockPos; +} + struct CImportingNow { CImportingNow() { assert(fImporting == false); diff --git a/src/rest.cpp b/src/rest.cpp --- a/src/rest.cpp +++ b/src/rest.cpp @@ -10,6 +10,7 @@ #include #include #include +#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 @@ -17,6 +17,7 @@ #include #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 @@ -14,6 +14,7 @@ #include #include #include +#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 @@ -6,6 +6,7 @@ #include #include +#include #include bool ComputeFilter(BlockFilterType filter_type, const CBlockIndex *block_index, diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -472,8 +472,6 @@ ScriptExecutionMetrics GetScriptExecutionMetrics() const { return metrics; } }; -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 @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -854,35 +855,6 @@ return nullptr; } -////////////////////////////////////////////////////////////////////////////// -// -// CBlock and CBlockIndex -// - -static bool WriteBlockToDisk(const CBlock &block, FlatFilePos &pos, - const CMessageHeader::MessageMagic &messageStart) { - // Open history file to append - CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION); - if (fileout.IsNull()) { - return error("WriteBlockToDisk: OpenBlockFile failed"); - } - - // Write index header - unsigned int nSize = GetSerializeSize(block, fileout.GetVersion()); - fileout << messageStart << nSize; - - // Write block - long fileOutPos = ftell(fileout.Get()); - if (fileOutPos < 0) { - return error("WriteBlockToDisk: ftell failed"); - } - - pos.nPos = (unsigned int)fileOutPos; - fileout << block; - - 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. @@ -3675,9 +3647,9 @@ } } -static bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, - unsigned int nHeight, CChain &active_chain, - uint64_t nTime, bool fKnown = false) { +// TODO move to blockstorage +bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int nHeight, + CChain &active_chain, uint64_t nTime, bool fKnown = false) { LOCK(cs_LastBlockFile); unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile; @@ -4259,33 +4231,6 @@ return true; } -/** - * Store block on disk. If dbp is non-nullptr, the file is known to already - * reside on disk. - */ -static FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, - CChain &active_chain, - const CChainParams &chainparams, - const FlatFilePos *dbp) { - unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION); - FlatFilePos blockPos; - if (dbp != nullptr) { - blockPos = *dbp; - } - if (!FindBlockPos(blockPos, nBlockSize + 8, nHeight, active_chain, - block.GetBlockTime(), dbp != nullptr)) { - error("%s: FindBlockPos failed", __func__); - return FlatFilePos(); - } - if (dbp == nullptr) { - if (!WriteBlockToDisk(block, blockPos, chainparams.DiskMagic())) { - AbortNode("Failed to write block"); - return FlatFilePos(); - } - } - return blockPos; -} - /** * Store a block on disk. * diff --git a/src/zmq/zmqpublishnotifier.cpp b/src/zmq/zmqpublishnotifier.cpp --- a/src/zmq/zmqpublishnotifier.cpp +++ b/src/zmq/zmqpublishnotifier.cpp @@ -4,10 +4,10 @@ #include -#include #include #include #include +#include #include #include #include diff --git a/test/lint/lint-circular-dependencies.sh b/test/lint/lint-circular-dependencies.sh --- a/test/lint/lint-circular-dependencies.sh +++ b/test/lint/lint-circular-dependencies.sh @@ -14,7 +14,9 @@ EXPECTED_CIRCULAR_DEPENDENCIES=( "index/txindex -> validation -> index/txindex" - "index/blockfilterindex -> validation -> index/blockfilterindex" + "node/blockstorage -> validation -> node/blockstorage" + "index/blockfilterindex -> node/blockstorage -> validation -> index/blockfilterindex" + "index/base -> validation -> index/blockfilterindex -> index/base" "qt/addresstablemodel -> qt/walletmodel -> qt/addresstablemodel" "qt/bitcoingui -> qt/walletframe -> qt/bitcoingui" "qt/recentrequeststablemodel -> qt/walletmodel -> qt/recentrequeststablemodel"