Bitcoin Core  0.19.99
P2P Digital Currency
Public Member Functions | Public Attributes | List of all members
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 <validation.h>

Public Member Functions

BlockMap m_block_index GUARDED_BY (cs_main)
 
bool LoadBlockIndex (const Consensus::Params &consensus_params, CBlockTreeDB &blocktree, std::set< CBlockIndex *, CBlockIndexWorkComparator > &block_index_candidates) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Load the blocktree off disk and into memory. More...
 
void Unload () EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Clear all data members. More...
 
CBlockIndexAddToBlockIndex (const CBlockHeader &block) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
CBlockIndexInsertBlockIndex (const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Create a new block index entry for a given block hash. More...
 
bool AcceptBlockHeader (const CBlockHeader &block, BlockValidationState &state, const CChainParams &chainparams, CBlockIndex **ppindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 If a block header hasn't already been seen, call CheckBlockHeader on it, ensure that it doesn't descend from an invalid block, and then add it to m_block_index. More...
 

Public Attributes

std::set< CBlockIndex * > m_failed_blocks
 In order to efficiently track invalidity of headers, we keep the set of blocks which we tried to connect and found to be invalid here (ie which were set to BLOCK_FAILED_VALID since the last restart). More...
 
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...
 

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 436 of file validation.h.

Member Function Documentation

◆ AcceptBlockHeader()

bool BlockManager::AcceptBlockHeader ( const CBlockHeader block,
BlockValidationState state,
const CChainParams chainparams,
CBlockIndex **  ppindex 
)

If a block header hasn't already been seen, call CheckBlockHeader on it, ensure that it doesn't descend from an invalid block, and then add it to m_block_index.

Definition at line 3583 of file validation.cpp.

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

◆ AddToBlockIndex()

CBlockIndex * BlockManager::AddToBlockIndex ( const CBlockHeader block)

Definition at line 3137 of file validation.cpp.

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

◆ GUARDED_BY()

BlockMap m_block_index BlockManager::GUARDED_BY ( cs_main  )

◆ InsertBlockIndex()

CBlockIndex * BlockManager::InsertBlockIndex ( const uint256 hash)

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

Definition at line 4046 of file validation.cpp.

Here is the call graph for this function:

◆ LoadBlockIndex()

bool BlockManager::LoadBlockIndex ( const Consensus::Params consensus_params,
CBlockTreeDB blocktree,
std::set< CBlockIndex *, CBlockIndexWorkComparator > &  block_index_candidates 
)

Load the blocktree off disk and into memory.

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

Parameters
[out]block_index_candidatesFill this set with any valid blocks for which we've downloaded all transactions.

Definition at line 4066 of file validation.cpp.

Here is the call graph for this function:

◆ Unload()

void BlockManager::Unload ( )

Clear all data members.

Definition at line 4121 of file validation.cpp.

Member Data Documentation

◆ m_blocks_unlinked

std::multimap<CBlockIndex*, CBlockIndex*> 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 464 of file validation.h.

◆ m_failed_blocks

std::set<CBlockIndex*> BlockManager::m_failed_blocks

In order to efficiently track invalidity of headers, we keep the set of blocks which we tried to connect and found to be invalid here (ie which were set to BLOCK_FAILED_VALID since the last restart).

We can then walk this set and check if a new header is a descendant of something in this set, preventing us from having to walk m_block_index when we try to connect a bad block and fail.

While this is more complicated than marking everything which descends from an invalid block as invalid at the time we discover it to be invalid, doing so would require walking all of m_block_index to find all descendants. Since this case should be very rare, keeping track of all BLOCK_FAILED_VALID blocks in a set should be just fine and work just as well.

Because we already walk m_block_index in height-order at startup, we go ahead and mark descendants of invalid blocks as FAILED_CHILD at that time, instead of putting things in this set.

Definition at line 458 of file validation.h.


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