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 <chainparams.h>
13 #include <fs.h>
15 #include <kernel/cs_main.h>
16 #include <protocol.h> // For CMessageHeader::MessageStartChars
17 #include <sync.h>
18 #include <txdb.h>
19 
20 class ArgsManager;
22 class CBlock;
23 class CBlockFileInfo;
24 class CBlockHeader;
25 class CBlockUndo;
26 class CChain;
27 class CChainParams;
28 class CTxUndo;
29 class Chainstate;
30 class ChainstateManager;
31 struct CCheckpointData;
32 class Config;
33 struct FlatFilePos;
34 namespace Consensus {
35 struct Params;
36 }
37 
38 namespace node {
39 static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false};
40 
42 static constexpr unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
44 static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
46 static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
47 
49 static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE =
50  CMessageHeader::MESSAGE_START_SIZE + sizeof(unsigned int);
51 
52 extern std::atomic_bool fReindex;
53 
54 // Because validation code takes pointers to the map's CBlockIndex objects, if
55 // we ever switch to another associative container, we need to either use a
56 // container that has stable addressing (true of all std associative
57 // containers), or make the key a `std::unique_ptr<CBlockIndex>`
58 using BlockMap = std::unordered_map<BlockHash, CBlockIndex, BlockHasher>;
59 
67 class BlockManager {
68  friend Chainstate;
70 
71 private:
72  const CChainParams &GetParams() const { return m_opts.chainparams; }
75  }
82  void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
83  void FlushUndoFile(int block_file, bool finalize = false);
84  bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize,
85  unsigned int nHeight, CChain &active_chain,
86  uint64_t nTime, bool fKnown);
87  bool FindUndoPos(BlockValidationState &state, int nFile, FlatFilePos &pos,
88  unsigned int nAddSize);
89 
94  void FindFilesToPruneManual(std::set<int> &setFilesToPrune,
95  int nManualPruneHeight, int chain_tip_height);
96 
118  void FindFilesToPrune(std::set<int> &setFilesToPrune,
119  uint64_t nPruneAfterHeight, int chain_tip_height,
120  int prune_height, bool is_ibd);
121 
130  bool m_check_for_pruning = false;
131 
132  const bool m_prune_mode;
133 
136 
139 
140  const kernel::BlockManagerOpts m_opts;
141 
142 public:
143  using Options = kernel::BlockManagerOpts;
144 
145  explicit BlockManager(Options opts)
146  : m_prune_mode{opts.prune_target > 0}, m_opts{std::move(opts)} {};
147 
148  std::atomic<bool> m_importing{false};
149 
150  BlockMap m_block_index GUARDED_BY(cs_main);
151 
152  std::vector<CBlockIndex *> GetAllBlockIndices()
154 
161 
162  std::unique_ptr<CBlockTreeDB> m_block_tree_db GUARDED_BY(::cs_main);
163 
164  bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
165  bool LoadBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
166 
167  CBlockIndex *AddToBlockIndex(const CBlockHeader &block,
168  CBlockIndex *&best_header)
173 
175  void PruneOneBlockFile(const int fileNumber)
177 
180  const CBlockIndex *LookupBlockIndex(const BlockHash &hash) const
182 
184  CBlockFileInfo *GetBlockFileInfo(size_t n);
185 
186  bool WriteUndoDataForBlock(const CBlockUndo &blockundo,
187  BlockValidationState &state, CBlockIndex &block)
189 
195  CChain &active_chain, const FlatFilePos *dbp);
196 
198  [[nodiscard]] bool IsPruneMode() const { return m_prune_mode; }
199 
201  [[nodiscard]] uint64_t GetPruneTarget() const {
202  return m_opts.prune_target;
203  }
204  static constexpr auto PRUNE_TARGET_MANUAL{
205  std::numeric_limits<uint64_t>::max()};
206 
207  [[nodiscard]] bool LoadingBlocks() const { return m_importing || fReindex; }
208 
212  uint64_t CalculateCurrentUsage();
213 
215  const CBlockIndex *GetLastCheckpoint(const CCheckpointData &data)
217 
219  bool m_have_pruned = false;
220 
223  bool IsBlockPruned(const CBlockIndex *pblockindex)
225 };
226 
228 const CBlockIndex *GetFirstStoredBlock(const CBlockIndex *start_block)
230 
232 
234 FILE *OpenBlockFile(const FlatFilePos &pos, bool fReadOnly = false);
237 
241 void UnlinkPrunedFiles(const std::set<int> &setFilesToPrune);
242 
244 bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos,
245  const Consensus::Params &consensusParams);
246 bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex,
247  const Consensus::Params &consensusParams);
248 bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex);
249 
251 bool ReadTxFromDisk(CMutableTransaction &tx, const FlatFilePos &pos);
252 bool ReadTxUndoFromDisk(CTxUndo &tx, const FlatFilePos &pos);
253 
254 void ThreadImport(ChainstateManager &chainman,
255  std::vector<fs::path> vImportFiles, const ArgsManager &args,
256  const fs::path &mempool_path);
257 } // namespace node
258 
259 #endif // BITCOIN_NODE_BLOCKSTORAGE_H
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:23
Definition: block.h:60
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:112
Undo information for a CBlock.
Definition: undo.h:73
An in-memory indexed chain of blocks.
Definition: chain.h:140
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:74
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:86
static constexpr size_t MESSAGE_START_SIZE
Definition: protocol.h:36
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:629
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:1144
Definition: config.h:17
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
Definition: blockstorage.h:67
const kernel::BlockManagerOpts m_opts
Definition: blockstorage.h:140
std::set< int > m_dirty_fileinfo
Dirty block file entries.
Definition: blockstorage.h:138
RecursiveMutex cs_LastBlockFile
Definition: blockstorage.h:122
const Consensus::Params & GetConsensus() const
Definition: blockstorage.h:73
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)
static constexpr auto PRUNE_TARGET_MANUAL
Definition: blockstorage.h:204
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the blocktree off disk and into memory.
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.
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, CChain &active_chain, const FlatFilePos *dbp)
Store block on disk.
Definition: blockstorage.h:194
bool LoadingBlocks() const
Definition: blockstorage.h:207
uint64_t GetPruneTarget() const
Attempt to stay below this number of bytes of block files.
Definition: blockstorage.h:201
const bool m_prune_mode
Definition: blockstorage.h:132
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:135
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:130
const CChainParams & GetParams() const
Definition: blockstorage.h:72
bool FindUndoPos(BlockValidationState &state, int nFile, FlatFilePos &pos, unsigned int nAddSize)
friend ChainstateManager
Definition: blockstorage.h:69
bool IsPruneMode() const
Whether running in -prune mode.
Definition: blockstorage.h:198
std::atomic< bool > m_importing
Definition: blockstorage.h:148
std::vector< CBlockFileInfo > m_blockfile_info
Definition: blockstorage.h:123
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:219
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:152
BlockMap m_block_index GUARDED_BY(cs_main)
void FlushBlockFile(bool fFinalize=false, bool finalize_undo=false)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
unsigned int nHeight
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:231
static const unsigned int UNDOFILE_CHUNK_SIZE
The pre-allocation chunk size for rev?????.dat files (since 0.8)
Definition: blockstorage.h:44
const CBlockIndex * GetFirstStoredBlock(const CBlockIndex *start_block)
std::unordered_map< BlockHash, CBlockIndex, BlockHasher > BlockMap
Definition: blockstorage.h:58
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.
static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT
Definition: blockstorage.h:39
FILE * OpenBlockFile(const FlatFilePos &pos, bool fReadOnly)
Open a block file (blk?????.dat)
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:42
static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE
Size of header written by WriteBlockToDisk before a serialized CBlock.
Definition: blockstorage.h:49
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:46
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)
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:257
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Parameters that influence chain consensus.
Definition: params.h:34
An options struct for BlockManager, more ergonomically referred to as BlockManager::Options due to th...
const CChainParams & chainparams
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56