BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
CChainState Class Reference

CChainState stores and provides an API to update our local knowledge of the current best chain and header tree. More...

Collaboration diagram for CChainState:
[legend]

Public Member Functions

bool LoadBlockIndex (const Consensus::Params &consensus_params, CBlockTreeDB &blocktree) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
bool ActivateBestChain (CValidationState &state, const CChainParams &chainparams, std::shared_ptr< const CBlock > pblock)
 Make the best chain active, in multiple steps. More...
 
bool AcceptBlockHeader (const CBlockHeader &block, CValidationState &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 mapBlockIndex. More...
 
bool AcceptBlock (const std::shared_ptr< const CBlock > &pblock, CValidationState &state, const CChainParams &chainparams, CBlockIndex **ppindex, bool fRequested, const CDiskBlockPos *dbp, bool *fNewBlock) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Store block on disk. More...
 
DisconnectResult DisconnectBlock (const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &view)
 Undo the effects of this block (with given index) on the UTXO set represented by coins. More...
 
bool ConnectBlock (const CBlock &block, CValidationState &state, CBlockIndex *pindex, CCoinsViewCache &view, const CChainParams &chainparams, bool fJustCheck=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Apply the effects of this block (with given index) on the UTXO set represented by coins. More...
 
bool DisconnectTip (CValidationState &state, const CChainParams &chainparams, DisconnectedBlockTransactions *disconnectpool)
 Disconnect chainActive's tip. More...
 
bool PreciousBlock (CValidationState &state, const CChainParams &params, CBlockIndex *pindex) LOCKS_EXCLUDED(cs_main)
 
bool InvalidateBlock (CValidationState &state, const CChainParams &chainparams, CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
void ResetBlockFailureFlags (CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
bool ReplayBlocks (const CChainParams &params, CCoinsView *view)
 
bool RewindBlockIndex (const CChainParams &params)
 
bool LoadGenesisBlock (const CChainParams &chainparams)
 
void PruneBlockIndexCandidates ()
 Delete all entries in setBlockIndexCandidates that are worse than the current tip. More...
 
void UnloadBlockIndex ()
 

Public Attributes

CChain chainActive
 
BlockMap mapBlockIndex
 
std::multimap< CBlockIndex *, CBlockIndex * > mapBlocksUnlinked
 
CBlockIndexpindexBestInvalid = nullptr
 

Private Member Functions

bool ActivateBestChainStep (CValidationState &state, const CChainParams &chainparams, CBlockIndex *pindexMostWork, const std::shared_ptr< const CBlock > &pblock, bool &fInvalidFound, ConnectTrace &connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Try to make some progress towards making pindexMostWork the active block. More...
 
bool ConnectTip (CValidationState &state, const CChainParams &chainparams, CBlockIndex *pindexNew, const std::shared_ptr< const CBlock > &pblock, ConnectTrace &connectTrace, DisconnectedBlockTransactions &disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Connect a new block to chainActive. 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...
 
void CheckBlockIndex (const Consensus::Params &consensusParams)
 Make various assertions about the state of the block index. More...
 
void InvalidBlockFound (CBlockIndex *pindex, const CValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
CBlockIndexFindMostWorkChain () EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Return the tip of the chain with the most work in it, that isn't known to be invalid (it's however far from certain to be valid). More...
 
void ReceivedBlockTransactions (const CBlock &block, CBlockIndex *pindexNew, const CDiskBlockPos &pos, const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). More...
 
bool RollforwardBlock (const CBlockIndex *pindex, CCoinsViewCache &inputs, const CChainParams &params) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Apply the effects of a block on the utxo cache, ignoring that it may already have been applied. More...
 

Private Attributes

std::set< CBlockIndex *, CBlockIndexWorkComparator > setBlockIndexCandidates
 The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS (for itself and all ancestors) and as good as our current tip or better. More...
 
CCriticalSection cs_nBlockSequenceId
 Every received block is assigned a unique and increasing identifier, so we know which one to give priority in case of a fork. More...
 
int32_t nBlockSequenceId = 1
 Blocks loaded from disk are assigned id 0, so start the counter at 1. More...
 
int32_t nBlockReverseSequenceId = -1
 Decreasing counter (used by subsequent preciousblock calls). More...
 
arith_uint256 nLastPreciousChainwork = 0
 chainwork for the last block that preciousblock has been applied to. More...
 
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...
 
CCriticalSection m_cs_chainstate
 the ChainState CriticalSection A lock that must be held when modifying this ChainState - held in ActivateBestChain() More...
 

Detailed Description

CChainState stores and provides an API to update our local knowledge of the current best chain and header tree.

It generally provides access to the current block tree, as well as functions to provide new data, which it will appropriately validate and incorporate in its state as necessary.

Eventually, the API here is targeted at being exposed externally as a consumable libconsensus library, so any functions added must only call other class member functions, pure functions in other parts of the consensus library, callbacks via the validation interface, or read/write-to-disk functions (eventually this will also be via callbacks).

Definition at line 107 of file validation.cpp.

Member Function Documentation

◆ AcceptBlock()

bool CChainState::AcceptBlock ( const std::shared_ptr< const CBlock > &  pblock,
CValidationState state,
const CChainParams chainparams,
CBlockIndex **  ppindex,
bool  fRequested,
const CDiskBlockPos dbp,
bool *  fNewBlock 
)

Store block on disk.

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

Definition at line 3376 of file validation.cpp.

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

◆ AcceptBlockHeader()

bool CChainState::AcceptBlockHeader ( const CBlockHeader block,
CValidationState 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 mapBlockIndex.

Definition at line 3274 of file validation.cpp.

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

◆ ActivateBestChain()

bool CChainState::ActivateBestChain ( CValidationState state,
const CChainParams chainparams,
std::shared_ptr< const CBlock pblock 
)

Make the best chain active, in multiple steps.

The result is either failure or an activated best chain. pblock is either nullptr or a pointer to a block that is already loaded (to avoid loading it again from disk).

ActivateBestChain is split into steps (see ActivateBestChainStep) so that we avoid holding cs_main for an extended period of time; the length of this call may be quite long during reindexing or a substantial reorg.

Definition at line 2576 of file validation.cpp.

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

◆ ActivateBestChainStep()

bool CChainState::ActivateBestChainStep ( CValidationState state,
const CChainParams chainparams,
CBlockIndex pindexMostWork,
const std::shared_ptr< const CBlock > &  pblock,
bool &  fInvalidFound,
ConnectTrace connectTrace 
)
private

Try to make some progress towards making pindexMostWork the active block.

pblock is either nullptr or a pointer to a CBlock corresponding to pindexMostWork.

Definition at line 2463 of file validation.cpp.

Here is the call graph for this function:

◆ AddToBlockIndex()

CBlockIndex * CChainState::AddToBlockIndex ( const CBlockHeader block)
private

Definition at line 2818 of file validation.cpp.

Here is the call graph for this function:

◆ CheckBlockIndex()

void CChainState::CheckBlockIndex ( const Consensus::Params consensusParams)
private

Make various assertions about the state of the block index.

By default this only executes fully when using the Regtest chain; see: fCheckBlockIndex.

Definition at line 4397 of file validation.cpp.

Here is the call graph for this function:

◆ ConnectBlock()

bool CChainState::ConnectBlock ( const CBlock block,
CValidationState state,
CBlockIndex pindex,
CCoinsViewCache view,
const CChainParams chainparams,
bool  fJustCheck = false 
)

Apply the effects of this block (with given index) on the UTXO set represented by coins.

Validity checks that depend on the UTXO set are also done; ConnectBlock() can fail if those validity checks fail (among other reasons).

Definition at line 1790 of file validation.cpp.

Here is the call graph for this function:

◆ ConnectTip()

bool CChainState::ConnectTip ( CValidationState state,
const CChainParams chainparams,
CBlockIndex pindexNew,
const std::shared_ptr< const CBlock > &  pblock,
ConnectTrace connectTrace,
DisconnectedBlockTransactions disconnectpool 
)
private

Connect a new block to chainActive.

pblock is either nullptr or a pointer to a CBlock corresponding to pindexNew, to bypass loading it again from disk.

The block is added to connectTrace if connection succeeds.

Definition at line 2334 of file validation.cpp.

Here is the call graph for this function:

◆ DisconnectBlock()

DisconnectResult CChainState::DisconnectBlock ( const CBlock block,
const CBlockIndex pindex,
CCoinsViewCache view 
)

Undo the effects of this block (with given index) on the UTXO set represented by coins.

When FAILED is returned, view is left in an indeterminate state.

Definition at line 1571 of file validation.cpp.

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

◆ DisconnectTip()

bool CChainState::DisconnectTip ( CValidationState state,
const CChainParams chainparams,
DisconnectedBlockTransactions disconnectpool 
)

Disconnect chainActive's tip.

After calling, the mempool will be in an inconsistent state, with transactions from disconnected blocks being added to disconnectpool. You should make the mempool consistent again by calling UpdateMempoolForReorg. with cs_main held.

If disconnectpool is nullptr, then no disconnected transactions are added to disconnectpool (note that the caller is responsible for mempool consistency in any case).

Definition at line 2211 of file validation.cpp.

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

◆ FindMostWorkChain()

CBlockIndex * CChainState::FindMostWorkChain ( )
private

Return the tip of the chain with the most work in it, that isn't known to be invalid (it's however far from certain to be valid).

Definition at line 2393 of file validation.cpp.

Here is the call graph for this function:

◆ InsertBlockIndex()

CBlockIndex * CChainState::InsertBlockIndex ( const uint256 hash)
private

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

Definition at line 3725 of file validation.cpp.

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

◆ InvalidateBlock()

bool CChainState::InvalidateBlock ( CValidationState state,
const CChainParams chainparams,
CBlockIndex pindex 
)

Definition at line 2712 of file validation.cpp.

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

◆ InvalidBlockFound()

void CChainState::InvalidBlockFound ( CBlockIndex pindex,
const CValidationState state 
)
private

Definition at line 1301 of file validation.cpp.

Here is the call graph for this function:

◆ LoadBlockIndex()

bool CChainState::LoadBlockIndex ( const Consensus::Params consensus_params,
CBlockTreeDB blocktree 
)

Definition at line 3745 of file validation.cpp.

Here is the call graph for this function:

◆ LoadGenesisBlock()

bool CChainState::LoadGenesisBlock ( const CChainParams chainparams)

Definition at line 4245 of file validation.cpp.

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

◆ PreciousBlock()

bool CChainState::PreciousBlock ( CValidationState state,
const CChainParams params,
CBlockIndex pindex 
)

Definition at line 2680 of file validation.cpp.

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

◆ PruneBlockIndexCandidates()

void CChainState::PruneBlockIndexCandidates ( )

Delete all entries in setBlockIndexCandidates that are worse than the current tip.

Definition at line 2448 of file validation.cpp.

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

◆ ReceivedBlockTransactions()

void CChainState::ReceivedBlockTransactions ( const CBlock block,
CBlockIndex pindexNew,
const CDiskBlockPos pos,
const Consensus::Params consensusParams 
)
private

Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS).

Definition at line 2855 of file validation.cpp.

Here is the call graph for this function:

◆ ReplayBlocks()

bool CChainState::ReplayBlocks ( const CChainParams params,
CCoinsView view 
)

Definition at line 4008 of file validation.cpp.

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

◆ ResetBlockFailureFlags()

void CChainState::ResetBlockFailureFlags ( CBlockIndex pindex)

Definition at line 2780 of file validation.cpp.

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

◆ RewindBlockIndex()

bool CChainState::RewindBlockIndex ( const CChainParams params)

Definition at line 4078 of file validation.cpp.

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

◆ RollforwardBlock()

bool CChainState::RollforwardBlock ( const CBlockIndex pindex,
CCoinsViewCache inputs,
const CChainParams params 
)
private

Apply the effects of a block on the utxo cache, ignoring that it may already have been applied.

Definition at line 3988 of file validation.cpp.

Here is the call graph for this function:

◆ UnloadBlockIndex()

void CChainState::UnloadBlockIndex ( )

Definition at line 4188 of file validation.cpp.

Member Data Documentation

◆ chainActive

CChain CChainState::chainActive

Definition at line 155 of file validation.cpp.

◆ cs_nBlockSequenceId

CCriticalSection CChainState::cs_nBlockSequenceId
private

Every received block is assigned a unique and increasing identifier, so we know which one to give priority in case of a fork.

Definition at line 120 of file validation.cpp.

◆ m_cs_chainstate

CCriticalSection CChainState::m_cs_chainstate
private

the ChainState CriticalSection A lock that must be held when modifying this ChainState - held in ActivateBestChain()

Definition at line 152 of file validation.cpp.

◆ m_failed_blocks

std::set<CBlockIndex*> CChainState::m_failed_blocks
private

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 mapBlockIndex 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 mapBlockIndex 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 mapBlockIndex 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 146 of file validation.cpp.

◆ mapBlockIndex

BlockMap CChainState::mapBlockIndex

Definition at line 156 of file validation.cpp.

◆ mapBlocksUnlinked

std::multimap<CBlockIndex*, CBlockIndex*> CChainState::mapBlocksUnlinked

Definition at line 157 of file validation.cpp.

◆ nBlockReverseSequenceId

int32_t CChainState::nBlockReverseSequenceId = -1
private

Decreasing counter (used by subsequent preciousblock calls).

Definition at line 124 of file validation.cpp.

◆ nBlockSequenceId

int32_t CChainState::nBlockSequenceId = 1
private

Blocks loaded from disk are assigned id 0, so start the counter at 1.

Definition at line 122 of file validation.cpp.

◆ nLastPreciousChainwork

arith_uint256 CChainState::nLastPreciousChainwork = 0
private

chainwork for the last block that preciousblock has been applied to.

Definition at line 126 of file validation.cpp.

◆ pindexBestInvalid

CBlockIndex* CChainState::pindexBestInvalid = nullptr

Definition at line 158 of file validation.cpp.

◆ setBlockIndexCandidates

std::set<CBlockIndex*, CBlockIndexWorkComparator> CChainState::setBlockIndexCandidates
private

The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS (for itself and all ancestors) and as good as our current tip or better.

Entries may be failed, though, and pruning nodes may be missing the data for the block.

Definition at line 114 of file validation.cpp.


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