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 <txdb.h>
11 
12 #include <atomic>
13 #include <cstdint>
14 #include <vector>
15 
16 class ArgsManager;
18 class CBlock;
19 class CBlockFileInfo;
20 class CBlockIndex;
21 class CBlockUndo;
22 class CChain;
23 class CChainParams;
24 class CChainState;
25 class ChainstateManager;
26 struct CCheckpointData;
27 struct FlatFilePos;
28 namespace Consensus {
29 struct Params;
30 }
31 
32 namespace node {
33 static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false};
34 
36 static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
38 static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
40 static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
41 
42 extern std::atomic_bool fImporting;
43 extern std::atomic_bool fReindex;
46 extern bool fHavePruned;
48 extern bool fPruneMode;
50 extern uint64_t nPruneTarget;
51 
52 typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
53 
55  bool operator()(const CBlockIndex* pa, const CBlockIndex* pb) const;
56 };
57 
66 {
67  friend CChainState;
69 
70 private:
71  void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
72  void FlushUndoFile(int block_file, bool finalize = false);
73  bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown);
74  bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize);
75 
76  /* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
77  void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight, int chain_tip_height);
78 
94  void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd);
95 
97  std::vector<CBlockFileInfo> m_blockfile_info;
103  bool m_check_for_pruning = false;
104 
106  std::set<CBlockIndex*> m_dirty_blockindex;
107 
109  std::set<int> m_dirty_fileinfo;
110 
111 public:
112  BlockMap m_block_index GUARDED_BY(cs_main);
113 
118  std::multimap<CBlockIndex*, CBlockIndex*> m_blocks_unlinked;
119 
120  std::unique_ptr<CBlockTreeDB> m_block_tree_db GUARDED_BY(::cs_main);
121 
122  bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
123  bool LoadBlockIndexDB(ChainstateManager& chainman) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
124 
131  const Consensus::Params& consensus_params,
133 
136 
140 
142  void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
143 
145 
147  CBlockFileInfo* GetBlockFileInfo(size_t n);
148 
149  bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams);
150 
151  FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp);
152 
154  uint64_t CalculateCurrentUsage();
155 
158 
160  {
161  Unload();
162  }
163 };
164 
166 bool IsBlockPruned(const CBlockIndex* pblockindex);
167 
168 void CleanupBlockRevFiles();
169 
171 FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false);
174 
178 void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune);
179 
181 bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams);
182 bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams);
183 bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start);
184 bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const CBlockIndex* pindex, const CMessageHeader::MessageStartChars& message_start);
185 
186 bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex);
187 
188 void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args);
189 } // namespace node
190 
191 #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:103
node::BlockManager::m_blockfile_info
std::vector< CBlockFileInfo > m_blockfile_info
Definition: blockstorage.h:97
node::fPruneMode
bool fPruneMode
True if we're running in -prune mode.
Definition: blockstorage.cpp:28
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:38
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:546
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:67
node::BlockManager::FindBlockPos
bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int nHeight, CChain &active_chain, uint64_t nTime, bool fKnown)
Definition: blockstorage.cpp:613
node::BlockManager::CalculateCurrentUsage
uint64_t CalculateCurrentUsage()
Calculate the amount of disk space the block & undo files currently use.
Definition: blockstorage.cpp:566
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
node::ReadRawBlockFromDisk
bool ReadRawBlockFromDisk(std::vector< uint8_t > &block, const FlatFilePos &pos, const CMessageHeader::MessageStartChars &message_start)
Definition: blockstorage.cpp:786
node::fHavePruned
bool fHavePruned
Pruning-related variables and constants.
Definition: blockstorage.cpp:27
node::BlockManager::SaveBlockToDisk
FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, CChain &active_chain, const CChainParams &chainparams, const FlatFilePos *dbp)
Store block on disk.
Definition: blockstorage.cpp:833
node::BlockManager::m_dirty_fileinfo
std::set< int > m_dirty_fileinfo
Dirty block file entries.
Definition: blockstorage.h:109
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:867
node::CBlockIndexWorkComparator
Definition: blockstorage.h:54
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:36
node::ReadBlockFromDisk
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
Definition: blockstorage.cpp:742
node::IsBlockPruned
bool IsBlockPruned(const CBlockIndex *pblockindex)
Check whether the block associated with this index entry is pruned or not.
Definition: blockstorage.cpp:430
node::CleanupBlockRevFiles
void CleanupBlockRevFiles()
Definition: blockstorage.cpp:441
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:514
Consensus::Params
Parameters that influence chain consensus.
Definition: params.h:70
node::BlockManager
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
Definition: blockstorage.h:65
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:608
node::BlockMap
std::unordered_map< uint256, CBlockIndex *, BlockHasher > BlockMap
Definition: blockstorage.h:52
node::BlockManager::AddToBlockIndex
CBlockIndex * AddToBlockIndex(const CBlockHeader &block) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: blockstorage.cpp:42
fs::path
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:33
FlatFilePos
Definition: flatfile.h:14
node::BlockManager::FindUndoPos
bool FindUndoPos(BlockValidationState &state, int nFile, FlatFilePos &pos, unsigned int nAddSize)
Definition: blockstorage.cpp:668
CCheckpointData
Definition: chainparams.h:22
node::OpenBlockFile
FILE * OpenBlockFile(const FlatFilePos &pos, bool fReadOnly)
Open a block file (blk?????.dat)
Definition: blockstorage.cpp:597
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:124
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:130
CChainState
CChainState stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:443
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:40
node::BlockManager::m_last_blockfile
int m_last_blockfile
Definition: blockstorage.h:98
ChainstateManager
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:761
CBlock
Definition: block.h:62
node::BlockManager::FlushBlockFile
void FlushBlockFile(bool fFinalize=false, bool finalize_undo=false)
Definition: blockstorage.cpp:554
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:33
CChain
An in-memory indexed chain of blocks.
Definition: chain.h:417
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:138
ArgsManager
Definition: system.h:158
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
CBlockFileInfo
Definition: chain.h:40
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::BlockManager::WriteUndoDataForBlock
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex *pindex, const CChainParams &chainparams)
Definition: blockstorage.cpp:713
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:478
node::BlockManager::cs_LastBlockFile
RecursiveMutex cs_LastBlockFile
Definition: blockstorage.h:96
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:148
node::BlockManager::m_dirty_blockindex
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
Definition: blockstorage.h:106
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:118
txdb.h
CBlockUndo
Undo information for a CBlock.
Definition: undo.h:63
node::BlockManager::ChainstateManager
friend ChainstateManager
Definition: blockstorage.h:68
args
ArgsManager args
Definition: notifications.cpp:36
node::UnlinkPrunedFiles
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune)
Actually unlink the specified files.
Definition: blockstorage.cpp:577