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 + pruning.cpp support/cleanse.cpp support/lockedpool.cpp sync.cpp diff --git a/src/index/base.cpp b/src/index/base.cpp --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -6,6 +6,7 @@ #include #include #include +#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 @@ -5,6 +5,7 @@ #include #include +#include #include #include #include diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -38,6 +38,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 @@ -18,6 +18,7 @@ #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 @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include diff --git a/src/pruning.h b/src/pruning.h new file mode 100644 --- /dev/null +++ b/src/pruning.h @@ -0,0 +1,34 @@ +// 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_PRUNING_H +#define BITCOIN_PRUNING_H + +#include +#include +#include + +namespace Consensus { +struct Params; +} + +class CBlock; + +/** The pre-allocation chunk size for blk?????.dat files (since 0.8) */ +static constexpr unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB + +FlatFileSeq BlockFileSeq(); + +/** + * 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_PRUNING_H diff --git a/src/pruning.cpp b/src/pruning.cpp new file mode 100644 --- /dev/null +++ b/src/pruning.cpp @@ -0,0 +1,65 @@ +#include + +#include +#include +#include +#include + +extern RecursiveMutex cs_main; + +FlatFileSeq BlockFileSeq() { + return FlatFileSeq(GetBlocksDir(), "blk", BLOCKFILE_CHUNK_SIZE); +} + +FILE *OpenBlockFile(const FlatFilePos &pos, bool fReadOnly) { + return BlockFileSeq().Open(pos, fReadOnly); +} + +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/rest.cpp b/src/rest.cpp --- a/src/rest.cpp +++ b/src/rest.cpp @@ -11,6 +11,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 @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include