Bitcoin ABC  0.26.3
P2P Digital Currency
blockstorage.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2021 The Bitcoin 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 <cstdint>
9 #include <vector>
10 
11 #include <chain.h>
12 #include <fs.h>
13 #include <protocol.h> // For CMessageHeader::MessageStartChars
14 #include <sync.h>
15 #include <txdb.h>
16 
17 extern RecursiveMutex cs_main;
18 
19 class ArgsManager;
21 class CBlock;
22 class CBlockFileInfo;
23 class CBlockHeader;
24 class CBlockUndo;
25 class CChain;
26 class CChainParams;
27 class CTxUndo;
28 class Chainstate;
29 class ChainstateManager;
30 struct CCheckpointData;
31 class Config;
32 struct FlatFilePos;
33 namespace Consensus {
34 struct Params;
35 }
36 
37 namespace node {
38 static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false};
39 
41 static constexpr unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
43 static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
45 static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
46 
47 extern std::atomic_bool fImporting;
48 extern std::atomic_bool fReindex;
51 extern bool fPruneMode;
53 extern uint64_t nPruneTarget;
54 
55 // Because validation code takes pointers to the map's CBlockIndex objects, if
56 // we ever switch to another associative container, we need to either use a
57 // container that has stable addressing (true of all std associative
58 // containers), or make the key a `std::unique_ptr<CBlockIndex>`
59 using BlockMap = std::unordered_map<BlockHash, CBlockIndex, BlockHasher>;
60 
68 class BlockManager {
69  friend Chainstate;
71 
72 private:
78  bool LoadBlockIndex(const Consensus::Params &consensus_params)
80  void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
81  void FlushUndoFile(int block_file, bool finalize = false);
82  bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize,
83  unsigned int nHeight, CChain &active_chain,
84  uint64_t nTime, bool fKnown);
85  bool FindUndoPos(BlockValidationState &state, int nFile, FlatFilePos &pos,
86  unsigned int nAddSize);
87 
92  void FindFilesToPruneManual(std::set<int> &setFilesToPrune,
93  int nManualPruneHeight, int chain_tip_height);
94 
116  void FindFilesToPrune(std::set<int> &setFilesToPrune,
117  uint64_t nPruneAfterHeight, int chain_tip_height,
118  int prune_height, bool is_ibd);
119 
121  std::vector<CBlockFileInfo> m_blockfile_info;
128  bool m_check_for_pruning = false;
129 
131  std::set<CBlockIndex *> m_dirty_blockindex;
132 
134  std::set<int> m_dirty_fileinfo;
135 
136 public:
137  BlockMap m_block_index GUARDED_BY(cs_main);
138 
139  std::vector<CBlockIndex *> GetAllBlockIndices()
141 
148 
149  std::unique_ptr<CBlockTreeDB> m_block_tree_db GUARDED_BY(::cs_main);
150 
151  bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
152  bool LoadBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
153 
154  CBlockIndex *AddToBlockIndex(const CBlockHeader &block,
155  CBlockIndex *&best_header)
160 
162  void PruneOneBlockFile(const int fileNumber)
164 
167  const CBlockIndex *LookupBlockIndex(const BlockHash &hash) const
169 
171  CBlockFileInfo *GetBlockFileInfo(size_t n);
172 
173  bool WriteUndoDataForBlock(const CBlockUndo &blockundo,
174  BlockValidationState &state, CBlockIndex *pindex,
175  const CChainParams &chainparams)
177 
179  CChain &active_chain,
180  const CChainParams &chainparams,
181  const FlatFilePos *dbp);
182 
186  uint64_t CalculateCurrentUsage();
187 
189  const CBlockIndex *GetLastCheckpoint(const CCheckpointData &data)
191 
193  bool m_have_pruned = false;
194 
197  bool IsBlockPruned(const CBlockIndex *pblockindex)
199 };
200 
202 const CBlockIndex *GetFirstStoredBlock(const CBlockIndex *start_block)
204 
206 
208 FILE *OpenBlockFile(const FlatFilePos &pos, bool fReadOnly = false);
210 fs::path GetBlockPosFilename(const FlatFilePos &pos);
211 
215 void UnlinkPrunedFiles(const std::set<int> &setFilesToPrune);
216 
218 bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos,
219  const Consensus::Params &consensusParams);
220 bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex,
221  const Consensus::Params &consensusParams);
222 bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex);
223 
225 bool ReadTxFromDisk(CMutableTransaction &tx, const FlatFilePos &pos);
226 bool ReadTxUndoFromDisk(CTxUndo &tx, const FlatFilePos &pos);
227 
228 void ThreadImport(const Config &config, ChainstateManager &chainman,
229  std::vector<fs::path> vImportFiles, const ArgsManager &args);
230 } // namespace node
231 
232 #endif // BITCOIN_NODE_BLOCKSTORAGE_H
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:113
const CChainParams & Params()
Return the currently selected parameters.
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
Definition: block.h:55
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:26
Access to the block database (blocks/index/)
Definition: txdb.h:106
Undo information for a CBlock.
Definition: undo.h:73
An in-memory indexed chain of blocks.
Definition: chain.h:141
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:74
A mutable version of CTransaction.
Definition: transaction.h:274
Restore the UTXO in a Coin at a given COutPoint.
Definition: undo.h:62
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:647
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:1062
Definition: config.h:17
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
Definition: blockstorage.h:68
std::set< int > m_dirty_fileinfo
Dirty block file entries.
Definition: blockstorage.h:134
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex *pindex, const CChainParams &chainparams) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePo SaveBlockToDisk)(const CBlock &block, int nHeight, CChain &active_chain, const CChainParams &chainparams, const FlatFilePos *dbp)
Store block on disk.
Definition: blockstorage.h:178
RecursiveMutex cs_LastBlockFile
Definition: blockstorage.h:120
bool LoadBlockIndex(const Consensus::Params &consensus_params) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the blocktree off disk and into memory.
void FindFilesToPrune(std::set< int > &setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd)
Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a us...
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, int chain_tip_height)
Calculate the block/rev files to delete based on height specified by user with RPC command pruneblock...
void FlushUndoFile(int block_file, bool finalize=false)
bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int nHeight, CChain &active_chain, uint64_t nTime, bool fKnown)
void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark one block file as pruned (modify associated database entries)
CBlockIndex * LookupBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
CBlockIndex * InsertBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Create a new block index entry for a given block hash.
uint64_t CalculateCurrentUsage()
Calculate the amount of disk space the block & undo files currently use.
const CBlockIndex * GetLastCheckpoint(const CCheckpointData &data) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Returns last CBlockIndex* that is a checkpoint.
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
Definition: blockstorage.h:131
bool m_check_for_pruning
Global flag to indicate we should check to see if there are block/undo files that should be deleted.
Definition: blockstorage.h:128
bool FindUndoPos(BlockValidationState &state, int nFile, FlatFilePos &pos, unsigned int nAddSize)
friend ChainstateManager
Definition: blockstorage.h:70
std::vector< CBlockFileInfo > m_blockfile_info
Definition: blockstorage.h:121
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
bool m_have_pruned
True if any block files have ever been pruned.
Definition: blockstorage.h:193
std::vector< CBlockIndex * > GetAllBlockIndices() EXCLUSIVE_LOCKS_REQUIRED(std::multimap< CBlockIndex *, CBlockIndex * > m_blocks_unlinked
All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
Definition: blockstorage.h:139
BlockMap m_block_index GUARDED_BY(cs_main)
void FlushBlockFile(bool fFinalize=false, bool finalize_undo=false)
unsigned int nHeight
Filesystem operations and types.
Definition: fs.h:19
Definition: init.h:28
const CBlockIndex *GetFirstStoredBlock(const CBlockIndex *start_block) EXCLUSIVE_LOCKS_REQUIRED(voi CleanupBlockRevFiles)()
Find the first block that is not pruned.
Definition: blockstorage.h:205
static const unsigned int UNDOFILE_CHUNK_SIZE
The pre-allocation chunk size for rev?????.dat files (since 0.8)
Definition: blockstorage.h:43
const CBlockIndex * GetFirstStoredBlock(const CBlockIndex *start_block)
bool fPruneMode
Pruning-related variables and constants.
std::atomic_bool fImporting
std::unordered_map< BlockHash, CBlockIndex, BlockHasher > BlockMap
Definition: blockstorage.h:59
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &params)
Functions for disk access for blocks.
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune)
Actually unlink the specified files.
uint64_t nPruneTarget
Number of MiB of block files that we're trying to stay below.
static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT
Definition: blockstorage.h:38
FILE * OpenBlockFile(const FlatFilePos &pos, bool fReadOnly)
Open a block file (blk?????.dat)
void ThreadImport(const Config &config, ChainstateManager &chainman, std::vector< fs::path > vImportFiles, const ArgsManager &args)
bool ReadTxFromDisk(CMutableTransaction &tx, const FlatFilePos &pos)
Functions for disk access for txs.
static constexpr unsigned int BLOCKFILE_CHUNK_SIZE
The pre-allocation chunk size for blk?????.dat files (since 0.8)
Definition: blockstorage.h:41
fs::path GetBlockPosFilename(const FlatFilePos &pos)
Translation to a filesystem path.
bool ReadTxUndoFromDisk(CTxUndo &tx_undo, const FlatFilePos &pos)
static const unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?????.dat file (since 0.8)
Definition: blockstorage.h:45
std::atomic_bool fReindex
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex)
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Parameters that influence chain consensus.
Definition: params.h:33
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56