Bitcoin Core  24.99.0
P2P Digital Currency
blockstorage.h
Go to the documentation of this file.
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 <attributes.h>
9 #include <chain.h>
10 #include <fs.h>
11 #include <protocol.h>
12 #include <sync.h>
13 #include <txdb.h>
14 
15 #include <atomic>
16 #include <cstdint>
17 #include <unordered_map>
18 #include <vector>
19 
20 extern RecursiveMutex cs_main;
21 
22 class ArgsManager;
24 class CBlock;
25 class CBlockFileInfo;
26 class CBlockUndo;
27 class CChain;
28 class CChainParams;
29 class Chainstate;
30 class ChainstateManager;
31 struct CCheckpointData;
32 struct FlatFilePos;
33 namespace Consensus {
34 struct Params;
35 }
36 
37 namespace node {
38 static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false};
39 
41 static const 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<uint256, CBlockIndex, BlockHasher>;
60 
62  bool operator()(const CBlockIndex* pa, const CBlockIndex* pb) const;
63 };
64 
66  /* Only compares the height of two block indices, doesn't try to tie-break */
67  bool operator()(const CBlockIndex* pa, const CBlockIndex* pb) const;
68 };
69 
70 struct PruneLockInfo {
71  int height_first{std::numeric_limits<int>::max()};
72 };
73 
82 {
83  friend Chainstate;
85 
86 private:
92  bool LoadBlockIndex(const Consensus::Params& consensus_params)
94  void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
95  void FlushUndoFile(int block_file, bool finalize = false);
96  bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown);
97  bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize);
98 
99  /* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
100  void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight, int chain_tip_height);
101 
117  void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd);
118 
120  std::vector<CBlockFileInfo> m_blockfile_info;
126  bool m_check_for_pruning = false;
127 
129  std::set<CBlockIndex*> m_dirty_blockindex;
130 
132  std::set<int> m_dirty_fileinfo;
133 
140  std::unordered_map<std::string, PruneLockInfo> m_prune_locks GUARDED_BY(::cs_main);
141 
142 public:
143  BlockMap m_block_index GUARDED_BY(cs_main);
144 
145  std::vector<CBlockIndex*> GetAllBlockIndices() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
146 
152 
153  std::unique_ptr<CBlockTreeDB> m_block_tree_db GUARDED_BY(::cs_main);
154 
155  bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
156  bool LoadBlockIndexDB(const Consensus::Params& consensus_params) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
157 
161 
163  void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
164 
167 
169  CBlockFileInfo* GetBlockFileInfo(size_t n);
170 
171  bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams)
173 
174  FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp);
175 
177  uint64_t CalculateCurrentUsage();
178 
181 
183  const CBlockIndex* GetFirstStoredBlock(const CBlockIndex& start_block LIFETIMEBOUND) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
184 
186  bool m_have_pruned = false;
187 
189  bool IsBlockPruned(const CBlockIndex* pblockindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
190 
192  void UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
193 };
194 
195 void CleanupBlockRevFiles();
196 
198 FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false);
200 fs::path GetBlockPosFilename(const FlatFilePos& pos);
201 
205 void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune);
206 
208 bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams);
209 bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams);
210 bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start);
211 
212 bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex);
213 
214 void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args, const fs::path& mempool_path);
215 } // namespace node
216 
217 #endif // BITCOIN_NODE_BLOCKSTORAGE_H
#define LIFETIMEBOUND
Definition: attributes.h:16
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:121
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:69
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:152
Access to the block database (blocks/index/)
Definition: txdb.h:79
Undo information for a CBlock.
Definition: undo.h:64
An in-memory indexed chain of blocks.
Definition: chain.h:423
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:70
Message header.
Definition: protocol.h:27
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:438
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:800
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
Definition: blockstorage.h:82
std::set< int > m_dirty_fileinfo
Dirty block file entries.
Definition: blockstorage.h:132
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:174
RecursiveMutex cs_LastBlockFile
Definition: blockstorage.h:119
bool LoadBlockIndex(const Consensus::Params &consensus_params) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the blocktree off disk and into memory.
const CBlockIndex *GetFirstStoredBlock(const CBlockIndex &start_block LIFETIMEBOUND) EXCLUSIVE_LOCKS_REQUIRED(bool m_have_pruned
Find the first block that is not pruned.
Definition: blockstorage.h:186
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 rev???.dat) so that the disk space used is less than a use...
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, int chain_tip_height)
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)
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
std::unordered_map< std::string, PruneLockInfo > m_prune_locks GUARDED_BY(::cs_main)
Map from external index name to oldest block that must not be pruned.
void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark one block file as pruned (modify associated database entries)
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:145
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
Definition: blockstorage.h:129
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(bool LoadBlockIndexDB(const Consensus::Params &consensus_params) EXCLUSIVE_LOCKS_REQUIRED(CBlockIndex * AddToBlockIndex(const CBlockHeader &block, CBlockIndex *&best_header) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: blockstorage.h:158
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.
CBlockIndex * InsertBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Create a new block index entry for a given block hash.
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:126
bool FindUndoPos(BlockValidationState &state, int nFile, FlatFilePos &pos, unsigned int nAddSize)
friend ChainstateManager
Definition: blockstorage.h:84
std::vector< CBlockFileInfo > m_blockfile_info
Definition: blockstorage.h:120
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
BlockMap m_block_index GUARDED_BY(cs_main)
void FlushBlockFile(bool fFinalize=false, bool finalize_undo=false)
256-bit opaque blob.
Definition: uint256.h:119
unsigned int nHeight
Transaction validation functions.
Definition: params.h:15
Filesystem operations and types.
Definition: fs.h:20
Definition: init.h:25
static const unsigned int UNDOFILE_CHUNK_SIZE
The pre-allocation chunk size for rev?????.dat files (since 0.8)
Definition: blockstorage.h:43
bool fPruneMode
Pruning-related variables and constants.
void CleanupBlockRevFiles()
std::atomic_bool fImporting
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
static const unsigned int BLOCKFILE_CHUNK_SIZE
The pre-allocation chunk size for blk?????.dat files (since 0.8)
Definition: blockstorage.h:41
bool ReadRawBlockFromDisk(std::vector< uint8_t > &block, const FlatFilePos &pos, const CMessageHeader::MessageStartChars &message_start)
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune)
Actually unlink the specified files.
uint64_t nPruneTarget
Number of bytes of block files that we're trying to stay below.
std::unordered_map< uint256, CBlockIndex, BlockHasher > BlockMap
Definition: blockstorage.h:59
static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT
Definition: blockstorage.h:38
FILE * OpenBlockFile(const FlatFilePos &pos, bool fReadOnly)
Open a block file (blk?????.dat)
fs::path GetBlockPosFilename(const FlatFilePos &pos)
Translation to a filesystem path.
static const unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?????.dat file (since 0.8)
Definition: blockstorage.h:45
void ThreadImport(ChainstateManager &chainman, std::vector< fs::path > vImportFiles, const ArgsManager &args, const fs::path &mempool_path)
std::atomic_bool fReindex
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex)
ArgsManager args
const char * name
Definition: rest.cpp:46
Parameters that influence chain consensus.
Definition: params.h:73
bool operator()(const CBlockIndex *pa, const CBlockIndex *pb) const
bool operator()(const CBlockIndex *pa, const CBlockIndex *pb) const
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49