1 // Copyright (c) 2011-2021 The Bitcoin Core developers 2 // Distributed under the MIT software license, see the accompanying 3 // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 5 #ifndef BITCOIN_NODE_BLOCKSTORAGE_H 6 #define BITCOIN_NODE_BLOCKSTORAGE_H 7 8 #include <fs.h> 9 #include <protocol.h> // For CMessageHeader::MessageStartChars 10 11 #include <atomic> 12 #include <cstdint> 13 #include <vector> 14 15 class ArgsManager; 16 class BlockValidationState; 17 class CBlock; 18 class CBlockFileInfo; 19 class CBlockIndex; 20 class CBlockUndo; 21 class CChain; 22 class CChainParams; 23 class ChainstateManager; 24 struct FlatFilePos; 25 namespace Consensus { 26 struct Params; 27 } 28 29 static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false}; 30 31 /** The pre-allocation chunk size for blk?????.dat files (since 0.8) */ 32 static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB 33 /** The pre-allocation chunk size for rev?????.dat files (since 0.8) */ 34 static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB 35 /** The maximum size of a blk?????.dat file (since 0.8) */ 36 static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB 37 38 extern std::atomic_bool fImporting; 39 extern std::atomic_bool fReindex; 40 /** Pruning-related variables and constants */ 41 /** True if any block files have ever been pruned. */ 42 extern bool fHavePruned; 43 /** True if we're running in -prune mode. */ 44 extern bool fPruneMode; 45 /** Number of MiB of block files that we're trying to stay below. */ 46 extern uint64_t nPruneTarget; 47 48 //! Check whether the block associated with this index entry is pruned or not. 49 bool IsBlockPruned(const CBlockIndex* pblockindex); 50 51 void CleanupBlockRevFiles(); 52 53 /** Open a block file (blk?????.dat) */ 54 FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false); 55 /** Translation to a filesystem path */ 56 fs::path GetBlockPosFilename(const FlatFilePos& pos); 57 58 /** Get block file info entry for one block file */ 59 CBlockFileInfo* GetBlockFileInfo(size_t n); 60 61 /** Calculate the amount of disk space the block & undo files currently use */ 62 uint64_t CalculateCurrentUsage(); 63 64 /** 65 * Actually unlink the specified files 66 */ 67 void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune); 68 69 /** Functions for disk access for blocks */ 70 bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams); 71 bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams); 72 bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start); 73 bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const CBlockIndex* pindex, const CMessageHeader::MessageStartChars& message_start); 74 75 bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex); 76 bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams); 77 78 FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp); 79 80 void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args); 81 82 #endif // BITCOIN_NODE_BLOCKSTORAGE_H 83