Bitcoin Core  22.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 <fs.h>
9 #include <protocol.h> // For CMessageHeader::MessageStartChars
10 #include <sync.h>
11 #include <txdb.h>
12 
13 #include <atomic>
14 #include <cstdint>
15 #include <vector>
16 
17 extern RecursiveMutex cs_main;
18 
19 class ArgsManager;
21 class CBlock;
22 class CBlockFileInfo;
23 class CBlockIndex;
24 class CBlockUndo;
25 class CChain;
26 class CChainParams;
27 class CChainState;
28 class ChainstateManager;
29 struct CCheckpointData;
30 struct FlatFilePos;
31 namespace Consensus {
32 struct Params;
33 }
34 
35 namespace node {
36 static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false};
37 
39 static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
41 static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
43 static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
44 
45 extern std::atomic_bool fImporting;
46 extern std::atomic_bool fReindex;
49 extern bool fHavePruned;
51 extern bool fPruneMode;
53 extern uint64_t nPruneTarget;
54 
55 typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
56 
58  bool operator()(const CBlockIndex* pa, const CBlockIndex* pb) const;
59 };
60 
69 {
70  friend CChainState;
72 
73 private:
74  void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
75  void FlushUndoFile(int block_file, bool finalize = false);
76  bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown);
77  bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize);
78 
79  /* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
80  void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight, int chain_tip_height);
81 
97  void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd);
98 
100  std::vector<CBlockFileInfo> m_blockfile_info;
106  bool m_check_for_pruning = false;
107 
109  std::set<CBlockIndex*> m_dirty_blockindex;
110 
112  std::set<int> m_dirty_fileinfo;
113 
114 public:
115  BlockMap m_block_index GUARDED_BY(cs_main);
116 
121  std::multimap<CBlockIndex*, CBlockIndex*> m_blocks_unlinked;
122 
123  std::unique_ptr<CBlockTreeDB> m_block_tree_db GUARDED_BY(::cs_main);
124 
125  bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
126  bool LoadBlockIndexDB(ChainstateManager& chainman) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
127 
134  const Consensus::Params& consensus_params,
136 
139 
143 
145  void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
146 
148 
150  CBlockFileInfo* GetBlockFileInfo(size_t n);
151 
152  bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams)
154 
155  FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp);
156 
158  uint64_t CalculateCurrentUsage();
159 
162 
164  {
165  Unload();
166  }
167 };
168 
170 bool IsBlockPruned(const CBlockIndex* pblockindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
171 
173 
175 FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false);
178 
182 void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune);
183 
185 bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams);
186 bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams);
187 bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start);
188 
189 bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex);
190 
191 void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args);
192 } // namespace node
193 
194 #endif // BITCOIN_NODE_BLOCKSTORAGE_H
node::BlockManager::m_check_for_pruning
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:106
node::BlockManager::m_blockfile_info
std::vector< CBlockFileInfo > m_blockfile_info
Definition: blockstorage.h:100
node::fPruneMode
bool fPruneMode
True if we're running in -prune mode.
Definition: blockstorage.cpp:28
node::CleanupBlockRevFiles
bool IsBlockPruned(const CBlockIndex *pblockindex) EXCLUSIVE_LOCKS_REQUIRED(voi CleanupBlockRevFiles)()
Check whether the block associated with this index entry is pruned or not.
Definition: blockstorage.h:172
node::UNDOFILE_CHUNK_SIZE
static const unsigned int UNDOFILE_CHUNK_SIZE
The pre-allocation chunk size for rev?????.dat files (since 0.8)
Definition: blockstorage.h:41
node::BlockManager::LookupBlockIndex
CBlockIndex * LookupBlockIndex(const uint256 &hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: blockstorage.cpp:35
node::BlockManager::FlushUndoFile
void FlushUndoFile(int block_file, bool finalize=false)
Definition: blockstorage.cpp:548
node::BlockManager::GUARDED_BY
BlockMap m_block_index GUARDED_BY(cs_main)
node::CBlockIndexWorkComparator::operator()
bool operator()(const CBlockIndex *pa, const CBlockIndex *pb) const
Definition: validation.cpp:110
fs.h
nHeight
unsigned int nHeight
Definition: mempool_eviction.cpp:14
CBlockHeader
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:20
BlockValidationState
Definition: validation.h:141
node::BlockManager::CChainState
friend CChainState
Definition: blockstorage.h:70
node::BlockManager::FindBlockPos
bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int nHeight, CChain &active_chain, uint64_t nTime, bool fKnown)
Definition: blockstorage.cpp:615
node::BlockManager::CalculateCurrentUsage
uint64_t CalculateCurrentUsage()
Calculate the amount of disk space the block & undo files currently use.
Definition: blockstorage.cpp:568
node::nPruneTarget
uint64_t nPruneTarget
Number of MiB of block files that we're trying to stay below.
Definition: blockstorage.cpp:29
node::BlockManager::PruneOneBlockFile
void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark one block file as pruned (modify associated database entries)
Definition: blockstorage.cpp:78
sync.h
node::ReadRawBlockFromDisk
bool ReadRawBlockFromDisk(std::vector< uint8_t > &block, const FlatFilePos &pos, const CMessageHeader::MessageStartChars &message_start)
Definition: blockstorage.cpp:789
node::fHavePruned
bool fHavePruned
Pruning-related variables and constants.
Definition: blockstorage.cpp:27
node::BlockManager::m_dirty_fileinfo
std::set< int > m_dirty_fileinfo
Dirty block file entries.
Definition: blockstorage.h:112
node::BlockManager::FindFilesToPrune
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...
Definition: blockstorage.cpp:135
CChainParams
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:69
AnnotatedMixin< std::recursive_mutex >
node::fImporting
std::atomic_bool fImporting
node::ThreadImport
void ThreadImport(ChainstateManager &chainman, std::vector< fs::path > vImportFiles, const ArgsManager &args)
Definition: blockstorage.cpp:859
node::CBlockIndexWorkComparator
Definition: blockstorage.h:57
node::BLOCKFILE_CHUNK_SIZE
static const unsigned int BLOCKFILE_CHUNK_SIZE
The pre-allocation chunk size for blk?????.dat files (since 0.8)
Definition: blockstorage.h:39
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:138
node::ReadBlockFromDisk
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
Definition: blockstorage.cpp:745
node::IsBlockPruned
bool IsBlockPruned(const CBlockIndex *pblockindex)
Definition: blockstorage.cpp:430
node::BlockManager::FindFilesToPruneManual
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, int chain_tip_height)
Definition: blockstorage.cpp:112
node::UndoReadFromDisk
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex)
Definition: blockstorage.cpp:515
Consensus::Params
Parameters that influence chain consensus.
Definition: params.h:70
node::BlockManager::SaveBlockToDisk
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:155
node::BlockManager
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
Definition: blockstorage.h:68
node::fReindex
std::atomic_bool fReindex
Consensus
Transaction validation functions.
Definition: params.h:12
node::GetBlockPosFilename
fs::path GetBlockPosFilename(const FlatFilePos &pos)
Translation to a filesystem path.
Definition: blockstorage.cpp:610
node::BlockMap
std::unordered_map< uint256, CBlockIndex *, BlockHasher > BlockMap
Definition: blockstorage.h:55
node::BlockManager::AddToBlockIndex
CBlockIndex * AddToBlockIndex(const CBlockHeader &block) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: blockstorage.cpp:42
fs::path
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:29
FlatFilePos
Definition: flatfile.h:14
node::BlockManager::FindUndoPos
bool FindUndoPos(BlockValidationState &state, int nFile, FlatFilePos &pos, unsigned int nAddSize)
Definition: blockstorage.cpp:670
CCheckpointData
Definition: chainparams.h:22
node::OpenBlockFile
FILE * OpenBlockFile(const FlatFilePos &pos, bool fReadOnly)
Open a block file (blk?????.dat)
Definition: blockstorage.cpp:599
node::BlockManager::Unload
void Unload() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Clear all data members.
Definition: blockstorage.cpp:326
uint256
256-bit opaque blob.
Definition: uint256.h:126
node::BlockManager::LoadBlockIndex
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(bool LoadBlockIndexDB(ChainstateManager &chainman) EXCLUSIVE_LOCKS_REQUIRED(bool LoadBlockIndex(const Consensus::Params &consensus_params, ChainstateManager &chainman) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the blocktree off disk and into memory.
Definition: blockstorage.h:133
CChainState
CChainState stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:459
node::MAX_BLOCKFILE_SIZE
static const unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?????.dat file (since 0.8)
Definition: blockstorage.h:43
node::BlockManager::m_last_blockfile
int m_last_blockfile
Definition: blockstorage.h:101
ChainstateManager
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:792
CBlock
Definition: block.h:62
node::BlockManager::FlushBlockFile
void FlushBlockFile(bool fFinalize=false, bool finalize_undo=false)
Definition: blockstorage.cpp:556
node::BlockManager::InsertBlockIndex
CBlockIndex * InsertBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Create a new block index entry for a given block hash.
Definition: blockstorage.cpp:194
node::DEFAULT_STOPAFTERBLOCKIMPORT
static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT
Definition: blockstorage.h:36
CChain
An in-memory indexed chain of blocks.
Definition: chain.h:430
ArgsManager
Definition: system.h:164
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
CBlockFileInfo
Definition: chain.h:43
CMessageHeader::MessageStartChars
unsigned char MessageStartChars[MESSAGE_START_SIZE]
Definition: protocol.h:40
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:561
node
Definition: init.h:22
node::BlockManager::GetBlockFileInfo
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
Definition: blockstorage.cpp:479
node::BlockManager::cs_LastBlockFile
RecursiveMutex cs_LastBlockFile
Definition: blockstorage.h:99
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:151
node::BlockManager::m_dirty_blockindex
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
Definition: blockstorage.h:109
node::BlockManager::GetLastCheckpoint
CBlockIndex * GetLastCheckpoint(const CCheckpointData &data) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Returns last CBlockIndex* that is a checkpoint.
Definition: blockstorage.cpp:416
node::BlockManager::m_blocks_unlinked
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:121
txdb.h
CBlockUndo
Undo information for a CBlock.
Definition: undo.h:63
node::BlockManager::ChainstateManager
friend ChainstateManager
Definition: blockstorage.h:71
args
ArgsManager args
Definition: notifications.cpp:36
node::UnlinkPrunedFiles
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune)
Actually unlink the specified files.
Definition: blockstorage.cpp:579