Bitcoin Core  23.99.0
P2P Digital Currency
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
node::BlockManager Class Reference

Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-work tip is. More...

#include <blockstorage.h>

Collaboration diagram for node::BlockManager:
[legend]

Public Member Functions

BlockMap m_block_index GUARDED_BY (cs_main)
 
std::unique_ptr< CBlockTreeDB > m_block_tree_db GUARDED_BY (::cs_main)
 
CBlockIndexInsertBlockIndex (const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Create a new block index entry for a given block hash. More...
 
void PruneOneBlockFile (const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Mark one block file as pruned (modify associated database entries) More...
 
CBlockIndexLookupBlockIndex (const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
const CBlockIndexLookupBlockIndex (const uint256 &hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
CBlockFileInfoGetBlockFileInfo (size_t n)
 Get block file info entry for one block file. More...
 
uint64_t CalculateCurrentUsage ()
 Calculate the amount of disk space the block & undo files currently use. More...
 
const CBlockIndexGetLastCheckpoint (const CCheckpointData &data) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Returns last CBlockIndex* that is a checkpoint. More...
 

Public Attributes

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. More...
 
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(bool LoadBlockIndexDB(const Consensus::Params &consensus_params) EXCLUSIVE_LOCKS_REQUIRED(CBlockIndexAddToBlockIndex (const CBlockHeader &block, CBlockIndex *&best_header) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
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. More...
 
const CBlockIndex *GetFirstStoredBlock(const CBlockIndex &start_block LIFETIMEBOUND) EXCLUSIVE_LOCKS_REQUIRED(bool m_have_pruned = false
 Find the first block that is not pruned. More...
 

Private Member Functions

bool LoadBlockIndex (const Consensus::Params &consensus_params) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Load the blocktree off disk and into memory. More...
 
void FlushBlockFile (bool fFinalize=false, bool finalize_undo=false)
 
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)
 
bool FindUndoPos (BlockValidationState &state, int nFile, FlatFilePos &pos, unsigned int nAddSize)
 
void FindFilesToPruneManual (std::set< int > &setFilesToPrune, int nManualPruneHeight, int chain_tip_height)
 
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 user-defined target. More...
 
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. More...
 

Private Attributes

friend CChainState
 
friend ChainstateManager
 
RecursiveMutex cs_LastBlockFile
 
std::vector< CBlockFileInfom_blockfile_info
 
int m_last_blockfile = 0
 
bool m_check_for_pruning = false
 Global flag to indicate we should check to see if there are block/undo files that should be deleted. More...
 
std::set< CBlockIndex * > m_dirty_blockindex
 Dirty block index entries. More...
 
std::set< int > m_dirty_fileinfo
 Dirty block file entries. More...
 

Detailed Description

Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-work tip is.

This data is used mostly in CChainState - information about, e.g., candidate tips is not maintained here.

Definition at line 81 of file blockstorage.h.

Member Function Documentation

◆ CalculateCurrentUsage()

uint64_t node::BlockManager::CalculateCurrentUsage ( )

Calculate the amount of disk space the block & undo files currently use.

Definition at line 536 of file blockstorage.cpp.

Here is the caller graph for this function:

◆ FindBlockPos()

bool node::BlockManager::FindBlockPos ( FlatFilePos pos,
unsigned int  nAddSize,
unsigned int  nHeight,
CChain active_chain,
uint64_t  nTime,
bool  fKnown 
)
private

Definition at line 583 of file blockstorage.cpp.

Here is the call graph for this function:

◆ FindFilesToPrune()

void node::BlockManager::FindFilesToPrune ( std::set< int > &  setFilesToPrune,
uint64_t  nPruneAfterHeight,
int  chain_tip_height,
int  prune_height,
bool  is_ibd 
)
private

Prune block and undo files (blk???.dat and rev???.dat) so that the disk space used is less than a user-defined target.

The user sets the target (in MB) on the command line or in config file. This will be run on startup and whenever new space is allocated in a block or undo file, staying below the target. Changing back to unpruned requires a reindex (which in this case means the blockchain must be re-downloaded.)

Pruning functions are called from FlushStateToDisk when the m_check_for_pruning flag has been set. Block and undo files are deleted in lock-step (when blk00003.dat is deleted, so is rev00003.dat.) Pruning cannot take place until the longest chain is at least a certain length (CChainParams::nPruneAfterHeight). Pruning will never delete a block within a defined distance (currently 288) from the active chain's tip. The block index is updated by unsetting HAVE_DATA and HAVE_UNDO for any blocks that were stored in the deleted files. A db flag records the fact that at least some block files have been pruned.

Parameters
[out]setFilesToPruneThe set of file indices that can be unlinked will be returned

Definition at line 177 of file blockstorage.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FindFilesToPruneManual()

void node::BlockManager::FindFilesToPruneManual ( std::set< int > &  setFilesToPrune,
int  nManualPruneHeight,
int  chain_tip_height 
)
private

Definition at line 154 of file blockstorage.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FindUndoPos()

bool node::BlockManager::FindUndoPos ( BlockValidationState state,
int  nFile,
FlatFilePos pos,
unsigned int  nAddSize 
)
private

Definition at line 638 of file blockstorage.cpp.

Here is the call graph for this function:

◆ FlushBlockFile()

void node::BlockManager::FlushBlockFile ( bool  fFinalize = false,
bool  finalize_undo = false 
)
private

Definition at line 524 of file blockstorage.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FlushUndoFile()

void node::BlockManager::FlushUndoFile ( int  block_file,
bool  finalize = false 
)
private

Definition at line 516 of file blockstorage.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetBlockFileInfo()

CBlockFileInfo * node::BlockManager::GetBlockFileInfo ( size_t  n)

Get block file info entry for one block file.

Definition at line 447 of file blockstorage.cpp.

◆ GetLastCheckpoint()

const CBlockIndex * node::BlockManager::GetLastCheckpoint ( const CCheckpointData data)

Returns last CBlockIndex* that is a checkpoint.

Definition at line 374 of file blockstorage.cpp.

Here is the call graph for this function:

◆ GUARDED_BY() [1/3]

std::unordered_map<std::string, PruneLockInfo> m_prune_locks node::BlockManager::GUARDED_BY ( ::cs_main  )
private

Map from external index name to oldest block that must not be pruned.

Note
Internally, only blocks at height (height_first - PRUNE_LOCK_BUFFER - 1) and below will be pruned, but callers should avoid assuming any particular buffer size.

◆ GUARDED_BY() [2/3]

std::unique_ptr<CBlockTreeDB> m_block_tree_db node::BlockManager::GUARDED_BY ( ::cs_main  )

◆ GUARDED_BY() [3/3]

BlockMap m_block_index node::BlockManager::GUARDED_BY ( cs_main  )

◆ InsertBlockIndex()

CBlockIndex * node::BlockManager::InsertBlockIndex ( const uint256 hash)

Create a new block index entry for a given block hash.

Definition at line 241 of file blockstorage.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LoadBlockIndex()

bool node::BlockManager::LoadBlockIndex ( const Consensus::Params consensus_params)
private

Load the blocktree off disk and into memory.

Populate certain metadata per index entry (nStatus, nChainWork, nTimeMax, etc.) as well as peripheral collections like m_dirty_blockindex.

Definition at line 257 of file blockstorage.cpp.

Here is the call graph for this function:

◆ LookupBlockIndex() [1/2]

const CBlockIndex * node::BlockManager::LookupBlockIndex ( const uint256 hash) const

Definition at line 79 of file blockstorage.cpp.

Here is the call graph for this function:

◆ LookupBlockIndex() [2/2]

CBlockIndex * node::BlockManager::LookupBlockIndex ( const uint256 hash)

Definition at line 72 of file blockstorage.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PruneOneBlockFile()

void node::BlockManager::PruneOneBlockFile ( const int  fileNumber)

Mark one block file as pruned (modify associated database entries)

Definition at line 120 of file blockstorage.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ AddToBlockIndex

CBlockIndex * node::BlockManager::AddToBlockIndex

Definition at line 158 of file blockstorage.h.

◆ CChainState

friend node::BlockManager::CChainState
private

Definition at line 83 of file blockstorage.h.

◆ ChainstateManager

friend node::BlockManager::ChainstateManager
private

Definition at line 84 of file blockstorage.h.

◆ cs_LastBlockFile

RecursiveMutex node::BlockManager::cs_LastBlockFile
private

Definition at line 119 of file blockstorage.h.

◆ m_blockfile_info

std::vector<CBlockFileInfo> node::BlockManager::m_blockfile_info
private

Definition at line 120 of file blockstorage.h.

◆ m_blocks_unlinked

std::vector<CBlockIndex*> GetAllBlockIndices () EXCLUSIVE_LOCKS_REQUIRED( std::multimap<CBlockIndex*, CBlockIndex*> node::BlockManager::m_blocks_unlinked

All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.

Pruned nodes may have entries where B is missing data.

Definition at line 145 of file blockstorage.h.

◆ m_check_for_pruning

bool node::BlockManager::m_check_for_pruning = false
private

Global flag to indicate we should check to see if there are block/undo files that should be deleted.

Set on startup or if we allocate more file space when we're in prune mode

Definition at line 126 of file blockstorage.h.

◆ m_dirty_blockindex

std::set<CBlockIndex*> node::BlockManager::m_dirty_blockindex
private

Dirty block index entries.

Definition at line 129 of file blockstorage.h.

◆ m_dirty_fileinfo

std::set<int> node::BlockManager::m_dirty_fileinfo
private

Dirty block file entries.

Definition at line 132 of file blockstorage.h.

◆ m_have_pruned

const CBlockIndex* GetFirstStoredBlock (const CBlockIndex& start_block LIFETIMEBOUND) EXCLUSIVE_LOCKS_REQUIRED( bool node::BlockManager::m_have_pruned = false

Find the first block that is not pruned.

True if any block files have ever been pruned.

Definition at line 186 of file blockstorage.h.

◆ m_last_blockfile

int node::BlockManager::m_last_blockfile = 0
private

Definition at line 121 of file blockstorage.h.

◆ SaveBlockToDisk

FlatFilePos node::BlockManager::SaveBlockToDisk

Store block on disk.

If dbp is non-nullptr, the file is known to already reside on disk

Definition at line 174 of file blockstorage.h.


The documentation for this class was generated from the following files: