Bitcoin
|
#include <validation.h>
Public Attributes | |
CChain | m_chain |
std::set< CBlockIndex *, CBlockIndexWorkComparator > | setBlockIndexCandidates |
bool ::mempool | cs |
Private Attributes | |
CCriticalSection | cs_nBlockSequenceId |
int32_t | nBlockSequenceId = 1 |
int32_t | nBlockReverseSequenceId = -1 |
arith_uint256 | nLastPreciousChainwork = 0 |
CCriticalSection | m_cs_chainstate |
std::atomic< bool > | m_cached_finished_ibd {false} |
BlockManager & | m_blockman |
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
.
|
inline |
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
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.
|
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.
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.
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).
|
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.
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.
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).
|
private |
Mark a block as not having block data.
Helper for CChainState::RewindBlockIndex.
|
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).
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.
void CChainState::ForceFlushStateToDisk | ( | ) |
Unconditionally flush all changes to disk.
bool CChainState::InvalidateBlock | ( | CValidationState & | state, |
const CChainParams & | chainparams, | ||
CBlockIndex * | pindex | ||
) |
|
private |
bool CChainState::IsInitialBlockDownload | ( | ) | const |
Check whether we are doing an initial block download (synchronizing from disk or network)
bool CChainState::LoadGenesisBlock | ( | const CChainParams & | chainparams | ) |
bool CChainState::PreciousBlock | ( | CValidationState & | state, |
const CChainParams & | params, | ||
CBlockIndex * | pindex | ||
) |
void CChainState::PruneAndFlush | ( | ) |
Prune blockfiles from the disk if necessary and then flush chainstate changes if we pruned.
void CChainState::PruneBlockIndexCandidates | ( | ) |
Delete all entries in setBlockIndexCandidates that are worse than the current tip.
|
private |
Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS).
bool CChainState::ReplayBlocks | ( | const CChainParams & | params, |
CCoinsView * | view | ||
) |
void CChainState::ResetBlockFailureFlags | ( | CBlockIndex * | pindex | ) |
bool CChainState::RewindBlockIndex | ( | const CChainParams & | params | ) |
|
private |
Apply the effects of a block on the utxo cache, ignoring that it may already have been applied.
void CChainState::UnloadBlockIndex | ( | ) |
bool::mempool CChainState::cs |
|
private |
Every received block is assigned a unique and increasing identifier, so we know which one to give priority in case of a fork.
|
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.
|
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.
CChain CChainState::m_chain |
The current chain of blockheaders we consult and build on.
|
private |
the ChainState CriticalSection A lock that must be held when modifying this ChainState - held in ActivateBestChain()
|
private |
Decreasing counter (used by subsequent preciousblock calls).
|
private |
Blocks loaded from disk are assigned id 0, so start the counter at 1.
|
private |
chainwork for the last block that preciousblock has been applied to.
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.