Bitcoin Core 28.99.0
P2P Digital Currency
Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | List of all members
CBlockIndex Class Reference

The block chain is a tree shaped structure starting with the genesis block at the root, with each block potentially having multiple candidates to be the next block. More...

#include <chain.h>

Inheritance diagram for CBlockIndex:
[legend]
Collaboration diagram for CBlockIndex:
[legend]

Public Member Functions

int nFile GUARDED_BY (::cs_main)
 Which # file this block is stored in (blk?????.dat) More...
 
unsigned int nDataPos GUARDED_BY (::cs_main)
 Byte offset within blk?????.dat where this block's data is stored. More...
 
unsigned int nUndoPos GUARDED_BY (::cs_main)
 Byte offset within rev?????.dat where this block's undo data is stored. More...
 
uint32_t nStatus GUARDED_BY (::cs_main)
 Verification status of this block. More...
 
 CBlockIndex (const CBlockHeader &block)
 
FlatFilePos GetBlockPos () const EXCLUSIVE_LOCKS_REQUIRED(
 
FlatFilePos GetUndoPos () const EXCLUSIVE_LOCKS_REQUIRED(
 
CBlockHeader GetBlockHeader () const
 
uint256 GetBlockHash () const
 
bool HaveNumChainTxs () const
 Check whether this block and all previous blocks back to the genesis block or an assumeutxo snapshot block have reached VALID_TRANSACTIONS and had transactions downloaded (and stored to disk) at some point. More...
 
NodeSeconds Time () const
 
int64_t GetBlockTime () const
 
int64_t GetBlockTimeMax () const
 
int64_t GetMedianTimePast () const
 
std::string ToString () const
 
bool IsValid (enum BlockStatus nUpTo=BLOCK_VALID_TRANSACTIONS) const EXCLUSIVE_LOCKS_REQUIRED(
 Check whether this block index entry is valid up to the passed validity level. More...
 
bool RaiseValidity (enum BlockStatus nUpTo) EXCLUSIVE_LOCKS_REQUIRED(
 Raise the validity level of this block index entry. More...
 
void BuildSkip ()
 Build the skiplist pointer for this entry. More...
 
CBlockIndexGetAncestor (int height)
 Efficiently find an ancestor of this block. More...
 
const CBlockIndexGetAncestor (int height) const
 
 CBlockIndex ()=default
 
 ~CBlockIndex ()=default
 

Public Attributes

const uint256phashBlock {nullptr}
 pointer to the hash of the block, if any. Memory is owned by this CBlockIndex More...
 
CBlockIndexpprev {nullptr}
 pointer to the index of the predecessor of this block More...
 
CBlockIndexpskip {nullptr}
 pointer to the index of some further predecessor of this block More...
 
int nHeight {0}
 height of the entry in the chain. The genesis block has height 0 More...
 
arith_uint256 nChainWork {}
 (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block More...
 
unsigned int nTx {0}
 Number of transactions in this block. More...
 
uint64_t m_chain_tx_count {0}
 (memory only) Number of transactions in the chain up to and including this block. More...
 
int32_t nVersion {0}
 block header More...
 
uint256 hashMerkleRoot {}
 
uint32_t nTime {0}
 
uint32_t nBits {0}
 
uint32_t nNonce {0}
 
int32_t nSequenceId {0}
 (memory only) Sequential id assigned to distinguish order in which blocks are received. More...
 
unsigned int nTimeMax {0}
 (memory only) Maximum nTime in the chain up to and including this block. More...
 

Static Public Attributes

static constexpr int nMedianTimeSpan = 11
 

Protected Member Functions

 CBlockIndex (const CBlockIndex &)=default
 CBlockIndex should not allow public copy construction because equality comparison via pointer is very common throughout the codebase, making use of copy a footgun. More...
 
CBlockIndexoperator= (const CBlockIndex &)=delete
 
 CBlockIndex (CBlockIndex &&)=delete
 
CBlockIndexoperator= (CBlockIndex &&)=delete
 

Detailed Description

The block chain is a tree shaped structure starting with the genesis block at the root, with each block potentially having multiple candidates to be the next block.

A blockindex may have multiple pprev pointing to it, but at most one of them can be part of the currently active branch.

Definition at line 140 of file chain.h.

Constructor & Destructor Documentation

◆ CBlockIndex() [1/4]

CBlockIndex::CBlockIndex ( const CBlockHeader block)
inlineexplicit

Definition at line 199 of file chain.h.

◆ CBlockIndex() [2/4]

CBlockIndex::CBlockIndex ( )
default

◆ ~CBlockIndex()

CBlockIndex::~CBlockIndex ( )
default

◆ CBlockIndex() [3/4]

CBlockIndex::CBlockIndex ( const CBlockIndex )
protecteddefault

CBlockIndex should not allow public copy construction because equality comparison via pointer is very common throughout the codebase, making use of copy a footgun.

Also, use of copies do not have the benefit of simplifying lifetime considerations due to attributes like pprev and pskip, which are at risk of becoming dangling pointers in a copied instance.

We declare these protected instead of simply deleting them so that CDiskBlockIndex can reuse copy construction.

◆ CBlockIndex() [4/4]

CBlockIndex::CBlockIndex ( CBlockIndex &&  )
protecteddelete

Member Function Documentation

◆ BuildSkip()

void CBlockIndex::BuildSkip ( )

Build the skiplist pointer for this entry.

Definition at line 125 of file chain.cpp.

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

◆ GetAncestor() [1/2]

CBlockIndex * CBlockIndex::GetAncestor ( int  height)

Efficiently find an ancestor of this block.

Definition at line 120 of file chain.cpp.

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

◆ GetAncestor() [2/2]

const CBlockIndex * CBlockIndex::GetAncestor ( int  height) const

Definition at line 93 of file chain.cpp.

Here is the call graph for this function:

◆ GetBlockHash()

uint256 CBlockIndex::GetBlockHash ( ) const
inline

Definition at line 243 of file chain.h.

Here is the call graph for this function:

◆ GetBlockHeader()

CBlockHeader CBlockIndex::GetBlockHeader ( ) const
inline

Definition at line 230 of file chain.h.

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

◆ GetBlockPos()

FlatFilePos CBlockIndex::GetBlockPos ( ) const
inline

Definition at line 208 of file chain.h.

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

◆ GetBlockTime()

int64_t CBlockIndex::GetBlockTime ( ) const
inline

Definition at line 266 of file chain.h.

Here is the caller graph for this function:

◆ GetBlockTimeMax()

int64_t CBlockIndex::GetBlockTimeMax ( ) const
inline

Definition at line 271 of file chain.h.

Here is the caller graph for this function:

◆ GetMedianTimePast()

int64_t CBlockIndex::GetMedianTimePast ( ) const
inline

Definition at line 278 of file chain.h.

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

◆ GetUndoPos()

FlatFilePos CBlockIndex::GetUndoPos ( ) const
inline

Definition at line 219 of file chain.h.

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

◆ GUARDED_BY() [1/4]

int nFile CBlockIndex::GUARDED_BY ( ::cs_main  )
inline

Which # file this block is stored in (blk?????.dat)

Definition at line 156 of file chain.h.

◆ GUARDED_BY() [2/4]

unsigned int nDataPos CBlockIndex::GUARDED_BY ( ::cs_main  )
inline

Byte offset within blk?????.dat where this block's data is stored.

Definition at line 159 of file chain.h.

◆ GUARDED_BY() [3/4]

unsigned int nUndoPos CBlockIndex::GUARDED_BY ( ::cs_main  )
inline

Byte offset within rev?????.dat where this block's undo data is stored.

Definition at line 162 of file chain.h.

◆ GUARDED_BY() [4/4]

uint32_t nStatus CBlockIndex::GUARDED_BY ( ::cs_main  )
inline

Verification status of this block.

See enum BlockStatus

Note: this value is modified to show BLOCK_OPT_WITNESS during UTXO snapshot load to avoid a spurious startup failure requiring -reindex.

See also
NeedsRedownload
ActivateSnapshot

Definition at line 184 of file chain.h.

◆ HaveNumChainTxs()

bool CBlockIndex::HaveNumChainTxs ( ) const
inline

Check whether this block and all previous blocks back to the genesis block or an assumeutxo snapshot block have reached VALID_TRANSACTIONS and had transactions downloaded (and stored to disk) at some point.

Does not imply the transactions are consensus-valid (ConnectTip might fail) Does not imply the transactions are still stored on disk. (IsBlockPruned might return true)

Note that this will be true for the snapshot base block, if one is loaded, since its m_chain_tx_count value will have been set manually based on the related AssumeutxoData entry.

Definition at line 259 of file chain.h.

Here is the caller graph for this function:

◆ IsValid()

bool CBlockIndex::IsValid ( enum BlockStatus  nUpTo = BLOCK_VALID_TRANSACTIONS) const
inline

Check whether this block index entry is valid up to the passed validity level.

Definition at line 295 of file chain.h.

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

◆ operator=() [1/2]

CBlockIndex & CBlockIndex::operator= ( CBlockIndex &&  )
protecteddelete

◆ operator=() [2/2]

CBlockIndex & CBlockIndex::operator= ( const CBlockIndex )
protecteddelete

◆ RaiseValidity()

bool CBlockIndex::RaiseValidity ( enum BlockStatus  nUpTo)
inline

Raise the validity level of this block index entry.

Returns true if the validity was changed.

Definition at line 307 of file chain.h.

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

◆ Time()

NodeSeconds CBlockIndex::Time ( ) const
inline

Definition at line 261 of file chain.h.

◆ ToString()

std::string CBlockIndex::ToString ( ) const

Definition at line 15 of file chain.cpp.

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

Member Data Documentation

◆ hashMerkleRoot

uint256 CBlockIndex::hashMerkleRoot {}

Definition at line 188 of file chain.h.

◆ m_chain_tx_count

uint64_t CBlockIndex::m_chain_tx_count {0}

(memory only) Number of transactions in the chain up to and including this block.

This value will be non-zero if this block and all previous blocks back to the genesis block or an assumeutxo snapshot block have reached the VALID_TRANSACTIONS level.

Definition at line 176 of file chain.h.

◆ nBits

uint32_t CBlockIndex::nBits {0}

Definition at line 190 of file chain.h.

◆ nChainWork

arith_uint256 CBlockIndex::nChainWork {}

(memory only) Total amount of work (expected number of hashes) in the chain up to and including this block

Definition at line 165 of file chain.h.

◆ nHeight

int CBlockIndex::nHeight {0}

height of the entry in the chain. The genesis block has height 0

Definition at line 153 of file chain.h.

◆ nMedianTimeSpan

constexpr int CBlockIndex::nMedianTimeSpan = 11
staticconstexpr

Definition at line 276 of file chain.h.

◆ nNonce

uint32_t CBlockIndex::nNonce {0}

Definition at line 191 of file chain.h.

◆ nSequenceId

int32_t CBlockIndex::nSequenceId {0}

(memory only) Sequential id assigned to distinguish order in which blocks are received.

Definition at line 194 of file chain.h.

◆ nTime

uint32_t CBlockIndex::nTime {0}

Definition at line 189 of file chain.h.

◆ nTimeMax

unsigned int CBlockIndex::nTimeMax {0}

(memory only) Maximum nTime in the chain up to and including this block.

Definition at line 197 of file chain.h.

◆ nTx

unsigned int CBlockIndex::nTx {0}

Number of transactions in this block.

This will be nonzero if the block reached the VALID_TRANSACTIONS level, and zero otherwise. Note: in a potential headers-first mode, this number cannot be relied upon

Definition at line 170 of file chain.h.

◆ nVersion

int32_t CBlockIndex::nVersion {0}

block header

Definition at line 187 of file chain.h.

◆ phashBlock

const uint256* CBlockIndex::phashBlock {nullptr}

pointer to the hash of the block, if any. Memory is owned by this CBlockIndex

Definition at line 144 of file chain.h.

◆ pprev

CBlockIndex* CBlockIndex::pprev {nullptr}

pointer to the index of the predecessor of this block

Definition at line 147 of file chain.h.

◆ pskip

CBlockIndex* CBlockIndex::pskip {nullptr}

pointer to the index of some further predecessor of this block

Definition at line 150 of file chain.h.


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