Bitcoin Core  0.18.99
P2P Digital Currency
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. More...

#include <validation.h>

Collaboration diagram for CChainState:
[legend]

Public Member Functions

 CChainState (BlockManager &blockman)
 
bool FlushStateToDisk (const CChainParams &chainparams, CValidationState &state, FlushStateMode mode, int nManualPruneHeight=0)
 Update the on-disk chain state. More...
 
void ForceFlushStateToDisk ()
 Unconditionally flush all changes to disk. More...
 
void PruneAndFlush ()
 Prune blockfiles from the disk if necessary and then flush chainstate changes if we pruned. More...
 
bool ActivateBestChain (CValidationState &state, const CChainParams &chainparams, std::shared_ptr< const CBlock > pblock) LOCKS_EXCLUDED(cs_main)
 Make the best chain active, in multiple steps. More...
 
bool AcceptBlock (const std::shared_ptr< const CBlock > &pblock, CValidationState &state, const CChainParams &chainparams, CBlockIndex **ppindex, bool fRequested, const FlatFilePos *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) EXCLUSIVE_LOCKS_REQUIRED(cs_main
 Disconnect m_chain'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) LOCKS_EXCLUDED(cs_main)
 
void ResetBlockFailureFlags (CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 
bool ReplayBlocks (const CChainParams &params, CCoinsView *view)
 
bool RewindBlockIndex (const CChainParams &params) LOCKS_EXCLUDED(cs_main)
 
bool LoadGenesisBlock (const CChainParams &chainparams)
 
void PruneBlockIndexCandidates ()
 Delete all entries in setBlockIndexCandidates that are worse than the current tip. More...
 
void UnloadBlockIndex ()
 
bool IsInitialBlockDownload () const
 Check whether we are doing an initial block download (synchronizing from disk or network) More...
 
void CheckBlockIndex (const Consensus::Params &consensusParams)
 Make various assertions about the state of the block index. More...
 

Public Attributes

CChain m_chain
 The current chain of blockheaders we consult and build on. More...
 
std::set< CBlockIndex *, CBlockIndexWorkComparatorsetBlockIndexCandidates
 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...
 
bool ::mempool cs
 

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 m_chain. 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 FlatFilePos &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...
 
void EraseBlockData (CBlockIndex *index) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Mark a block as not having block data. More...
 

Private Attributes

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...
 
CCriticalSection m_cs_chainstate
 the ChainState CriticalSection A lock that must be held when modifying this ChainState - held in ActivateBestChain() More...
 
std::atomic< bool > m_cached_finished_ibd {false}
 Whether this chainstate is undergoing initial block download. More...
 
BlockManagerm_blockman
 Reference to a BlockManager instance which itself is shared across all CChainState instances. More...
 

Detailed Description

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

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).

Anything that is contingent on the current tip of the chain is stored here, whereas block information and metadata independent of the current tip is kept in BlockMetadataManager.

Definition at line 516 of file validation.h.

Constructor & Destructor Documentation

◆ CChainState()

CChainState::CChainState ( BlockManager blockman)
inline

Definition at line 551 of file validation.h.

Member Function Documentation

◆ AcceptBlock()

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

Store block on disk.

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

Definition at line 3367 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 2536 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 2415 of file validation.cpp.

Here is the call graph for this function:

◆ CheckBlockIndex()

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

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

Here is the call graph for this function:
Here is the caller 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 1656 of file validation.cpp.

Here is the call graph for this function:
Here is the caller 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 m_chain.

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 2285 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 1445 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 m_chain'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 2165 of file validation.cpp.

Here is the call graph for this function:

◆ EraseBlockData()

void CChainState::EraseBlockData ( CBlockIndex index)
private

Mark a block as not having block data.

Helper for CChainState::RewindBlockIndex.

Definition at line 4064 of file validation.cpp.

Here is the call 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 2344 of file validation.cpp.

Here is the call graph for this function:

◆ FlushStateToDisk()

bool CChainState::FlushStateToDisk ( const CChainParams chainparams,
CValidationState state,
FlushStateMode  mode,
int  nManualPruneHeight = 0 
)

Update the on-disk chain state.

The caches and indexes are flushed depending on the mode we're called with if they're too large, if it's been a while since the last write, or always and in all cases if we're in prune mode and are deleting files.

If FlushStateMode::NONE is used, then FlushStateToDisk(...) won't do anything besides checking if we need to prune.

Definition at line 1959 of file validation.cpp.

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

◆ ForceFlushStateToDisk()

void CChainState::ForceFlushStateToDisk ( )

Unconditionally flush all changes to disk.

Definition at line 2070 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 2670 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 1170 of file validation.cpp.

Here is the call graph for this function:

◆ IsInitialBlockDownload()

bool CChainState::IsInitialBlockDownload ( ) const

Check whether we are doing an initial block download (synchronizing from disk or network)

Definition at line 1031 of file validation.cpp.

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

◆ LoadGenesisBlock()

bool CChainState::LoadGenesisBlock ( const CChainParams chainparams)

Definition at line 4263 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 2638 of file validation.cpp.

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

◆ PruneAndFlush()

void CChainState::PruneAndFlush ( )

Prune blockfiles from the disk if necessary and then flush chainstate changes if we pruned.

Definition at line 2078 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 2400 of file validation.cpp.

Here is the caller graph for this function:

◆ ReceivedBlockTransactions()

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

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

Definition at line 2835 of file validation.cpp.

Here is the call graph for this function:

◆ ReplayBlocks()

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

Definition at line 3993 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 2760 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 4099 of file validation.cpp.

Here is the call 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 3973 of file validation.cpp.

Here is the call graph for this function:

◆ UnloadBlockIndex()

void CChainState::UnloadBlockIndex ( )

Definition at line 4212 of file validation.cpp.

Here is the caller graph for this function:

Member Data Documentation

◆ cs

bool::mempool CChainState::cs

Definition at line 595 of file validation.h.

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

◆ m_blockman

BlockManager& CChainState::m_blockman
private

Reference to a BlockManager instance which itself is shared across all CChainState instances.

Keeping a local reference allows us to test more easily as opposed to referencing a global.

Definition at line 548 of file validation.h.

◆ m_cached_finished_ibd

std::atomic<bool> CChainState::m_cached_finished_ibd {false}
mutableprivate

Whether this chainstate is undergoing initial block download.

Mutable because we need to be able to mark IsInitialBlockDownload() const, which latches this for caching purposes.

Definition at line 543 of file validation.h.

◆ m_chain

CChain CChainState::m_chain

The current chain of blockheaders we consult and build on.

See also
CChain, CBlockIndex.

Definition at line 555 of file validation.h.

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

◆ nBlockReverseSequenceId

int32_t CChainState::nBlockReverseSequenceId = -1
private

Decreasing counter (used by subsequent preciousblock calls).

Definition at line 527 of file validation.h.

◆ nBlockSequenceId

int32_t CChainState::nBlockSequenceId = 1
private

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

Definition at line 525 of file validation.h.

◆ nLastPreciousChainwork

arith_uint256 CChainState::nLastPreciousChainwork = 0
private

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

Definition at line 529 of file validation.h.

◆ setBlockIndexCandidates

std::set<CBlockIndex*, CBlockIndexWorkComparator> CChainState::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.

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

Definition at line 561 of file validation.h.


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