![]() |
Bitcoin ABC
0.26.3
P2P Digital Currency
|
Chainstate stores and provides an API to update our local knowledge of the current best chain. More...
#include <validation.h>
Public Member Functions | |
Chainstate (CTxMemPool *mempool, node::BlockManager &blockman, ChainstateManager &chainman, std::optional< BlockHash > from_snapshot_blockhash=std::nullopt) | |
void | InitCoinsDB (size_t cache_size_bytes, bool in_memory, bool should_wipe, std::string leveldb_name="chainstate") |
Initialize the CoinsViews UTXO set database management data structures. More... | |
bool | reliesOnAssumedValid () |
Return true if this chainstate relies on blocks that are assumed-valid. More... | |
CCoinsViewCache & | CoinsTip () EXCLUSIVE_LOCKS_REQUIRED( |
CCoinsViewDB & | CoinsDB () EXCLUSIVE_LOCKS_REQUIRED( |
CTxMemPool * | GetMempool () |
CCoinsViewErrorCatcher & | CoinsErrorCatcher () EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
void | ResetCoinsViews () |
Destructs all objects related to accessing the UTXO set. More... | |
bool | FlushStateToDisk (BlockValidationState &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 (const Config &config, BlockValidationState &state, std::shared_ptr< const CBlock > pblock=nullptr) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex) LOCKS_EXCLUDED(cs_main) |
Find the best known block, and make it the tip of the block chain. More... | |
bool | AcceptBlock (const Config &config, const std::shared_ptr< const CBlock > &pblock, BlockValidationState &state, bool fRequested, const FlatFilePos *dbp, bool *fNewBlock) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Store a block on disk. More... | |
bool | DisconnectTip (BlockValidationState &state, DisconnectedBlockTransactions *disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main |
Disconnect m_chain's tip. More... | |
bool | PreciousBlock (const Config &config, BlockValidationState &state, CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex) LOCKS_EXCLUDED(cs_main) |
Mark a block as precious and reorganize. More... | |
bool | InvalidateBlock (const Config &config, BlockValidationState &state, CBlockIndex *pindex) LOCKS_EXCLUDED(cs_main) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex) |
Mark a block as invalid. More... | |
bool | ParkBlock (const Config &config, BlockValidationState &state, CBlockIndex *pindex) LOCKS_EXCLUDED(cs_main) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex) |
Park a block. More... | |
bool | AvalancheFinalizeBlock (CBlockIndex *pindex) |
Mark a block as finalized by avalanche. More... | |
void | ClearAvalancheFinalizedBlock () |
Clear avalanche finalization. More... | |
bool | IsBlockAvalancheFinalized (const CBlockIndex *pindex) const |
Checks if a block is finalized by avalanche voting. More... | |
void | ResetBlockFailureFlags (CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Remove invalidity status from a block and its descendants. More... | |
template<typename F > | |
bool | UpdateFlagsForBlock (CBlockIndex *pindexBase, CBlockIndex *pindex, F f) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
template<typename F , typename C , typename AC > | |
void | UpdateFlags (CBlockIndex *pindex, CBlockIndex *&pindexReset, F f, C fChild, AC fAncestorWasChanged) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
void | UnparkBlockAndChildren (CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Remove parked status from a block and its descendants. More... | |
void | UnparkBlock (CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Remove parked status from a block. More... | |
bool | ReplayBlocks () |
Replay blocks that aren't fully applied to the database. More... | |
bool | LoadGenesisBlock () |
Ensures we have a genesis block in the block tree, possibly writing one to disk. More... | |
void | PruneBlockIndexCandidates () |
Delete all entries in setBlockIndexCandidates that are worse than the current tip. More... | |
void | UnloadBlockIndex () EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
bool | IsInitialBlockDownload () const |
Check whether we are doing an initial block download (synchronizing from disk or network) More... | |
const CBlockIndex * | FindForkInGlobalIndex (const CBlockLocator &locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Find the last common block of this chain and a locator. More... | |
void | CheckBlockIndex () |
Make various assertions about the state of the block index. More... | |
void | LoadMempool (const Config &config, const ArgsManager &args) |
Load the persisted mempool from disk. More... | |
bool | LoadChainTip () EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Update the chain tip based on database information, i.e. More... | |
Public Attributes | |
node::BlockManager & | m_blockman |
Reference to a BlockManager instance which itself is shared across all Chainstate instances. More... | |
const CChainParams & | m_params |
Chain parameters for this chainstate. More... | |
ChainstateManager & | m_chainman |
The chainstate manager that owns this chainstate. More... | |
CChain | m_chain |
The current chain of blockheaders we consult and build on. More... | |
const std::optional< BlockHash > | m_from_snapshot_blockhash {} |
The blockhash which is the base of the snapshot this chainstate was created from. More... | |
std::set< CBlockIndex *, CBlockIndexWorkComparator > | setBlockIndexCandidates |
The set of all CBlockIndex entries with either BLOCK_VALID_TRANSACTIONS (for itself and all ancestors) or BLOCK_ASSUMED_VALID (if using background chainstates) and as good as our current tip or better. More... | |
size_t | m_coinsdb_cache_size_bytes {0} |
The cache size of the on-disk coins view. More... | |
size_t | m_coinstip_cache_size_bytes {0} |
The cache size of the in-memory coins view. More... | |
DisconnectResult DisconnectBlock(const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &view) EXCLUSIVE_LOCKS_REQUIRED(boo | ConnectBlock )(const CBlock &block, BlockValidationState &state, CBlockIndex *pindex, CCoinsViewCache &view, BlockValidationOptions options, Amount *blockFees=nullptr, 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 m_mempool | cs |
Private Member Functions | |
const CBlockIndex *m_avalancheFinalizedBlockIndex | GUARDED_BY (cs_avalancheFinalizedBlockIndex) |
The best block via avalanche voting. More... | |
bool | ActivateBestChainStep (const Config &config, BlockValidationState &state, CBlockIndex *pindexMostWork, const std::shared_ptr< const CBlock > &pblock, bool &fInvalidFound, ConnectTrace &connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main |
Dictates whether we need to flush the cache to disk or not. More... | |
bool | ConnectTip (const Config &config, BlockValidationState &state, BlockPolicyValidationState &blockPolicyState, 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 BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
CBlockIndex * | FindMostWorkChain (std::vector< const CBlockIndex * > &blocksToReconcile) 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) 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) 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 | UnparkBlockImpl (CBlockIndex *pindex, bool fClearChildren) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
bool | UnwindBlock (const Config &config, BlockValidationState &state, CBlockIndex *pindex, bool invalidate) EXCLUSIVE_LOCKS_REQUIRED(m_chainstate_mutex) |
void | CheckForkWarningConditions () EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
void | CheckForkWarningConditionsOnNewFork (CBlockIndex *pindexNewForkTip) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
void | InvalidChainFound (CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
const CBlockIndex * | FindBlockToFinalize (CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
RecursiveMutex * | MempoolMutex () const LOCK_RETURNED(m_mempool -> cs) |
Indirection necessary to make lock annotations work with an optional mempool. More... | |
Private Attributes | |
Mutex | m_chainstate_mutex |
The ChainState Mutex. More... | |
std::atomic< int32_t > | nBlockSequenceId {1} |
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 | 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::atomic< bool > | m_cached_finished_ibd {false} |
Whether this chainstate is undergoing initial block download. More... | |
CTxMemPool * | m_mempool |
Optional mempool that is kept in sync with the chain. More... | |
std::unique_ptr< CoinsViews > | m_coins_views |
Manages the UTXO set, which is a reflection of the contents of m_chain . More... | |
Mutex | cs_avalancheFinalizedBlockIndex |
CRollingBloomFilter | m_filterParkingPoliciesApplied |
Filter to prevent parking a block due to block policies more than once. More... | |
CBlockIndex const * | m_best_fork_tip = nullptr |
CBlockIndex const * | m_best_fork_base = nullptr |
void UpdateTip(const CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(frien | ChainstateManager ) |
Check warning conditions and do some notifications on new chain tip set. More... | |
Chainstate 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 BlockManager
.
Definition at line 647 of file validation.h.
|
explicit |
Definition at line 1173 of file validation.cpp.
bool Chainstate::AcceptBlock | ( | const Config & | config, |
const std::shared_ptr< const CBlock > & | pblock, | ||
BlockValidationState & | state, | ||
bool | fRequested, | ||
const FlatFilePos * | dbp, | ||
bool * | fNewBlock | ||
) |
Store a block on disk.
[in] | config | The global config. |
[in,out] | pblock | The block we want to accept. |
[in] | fRequested | A boolean to indicate if this block was requested from our peers. |
[in] | dbp | If non-null, the disk position of the block. |
[in,out] | fNewBlock | True if block was first received via this call. |
Definition at line 4225 of file validation.cpp.
bool Chainstate::ActivateBestChain | ( | const Config & | config, |
BlockValidationState & | state, | ||
std::shared_ptr< const CBlock > | pblock = nullptr |
||
) |
Find the best known block, and make it the tip of the block chain.
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.
May not be called with cs_main held. May not be called in a validationinterface callback.
Definition at line 3101 of file validation.cpp.
|
private |
Dictates whether we need to flush the cache to disk or not.
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 2925 of file validation.cpp.
bool Chainstate::AvalancheFinalizeBlock | ( | CBlockIndex * | pindex | ) |
Mark a block as finalized by avalanche.
Definition at line 3642 of file validation.cpp.
void Chainstate::CheckBlockIndex | ( | ) |
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 5172 of file validation.cpp.
|
private |
Definition at line 1250 of file validation.cpp.
|
private |
Definition at line 1301 of file validation.cpp.
void Chainstate::ClearAvalancheFinalizedBlock | ( | ) |
Clear avalanche finalization.
Definition at line 3667 of file validation.cpp.
|
inline |
Definition at line 778 of file validation.h.
|
inline |
Definition at line 788 of file validation.h.
|
inline |
Definition at line 771 of file validation.h.
|
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.
The block is valid by consensus rules so now we check if the block passes all block policy checks. If not, then park the block and bail.
We check block parking policies before flushing changes to the UTXO set. This allows us to avoid rewinding everything immediately after.
Only check block parking policies the first time the block is connected. Avalanche voting can override the parking decision made by these policies.
Definition at line 2572 of file validation.cpp.
bool Chainstate::DisconnectTip | ( | BlockValidationState & | state, |
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 2447 of file validation.cpp.
|
private |
const CBlockIndex * Chainstate::FindForkInGlobalIndex | ( | const CBlockLocator & | locator | ) | const |
Find the last common block of this chain and a locator.
Definition at line 132 of file validation.cpp.
|
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 2735 of file validation.cpp.
bool Chainstate::FlushStateToDisk | ( | BlockValidationState & | 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 2207 of file validation.cpp.
void Chainstate::ForceFlushStateToDisk | ( | ) |
Unconditionally flush all changes to disk.
Definition at line 2371 of file validation.cpp.
|
inline |
Definition at line 784 of file validation.h.
|
private |
The best block via avalanche voting.
This block cannot be reorged in any way except by explicit user action.
void Chainstate::InitCoinsDB | ( | size_t | cache_size_bytes, |
bool | in_memory, | ||
bool | should_wipe, | ||
std::string | leveldb_name = "chainstate" |
||
) |
Initialize the CoinsViews UTXO set database management data structures.
The in-memory cache is initialized separately.
All parameters forwarded to CoinsViews.
Definition at line 1180 of file validation.cpp.
bool Chainstate::InvalidateBlock | ( | const Config & | config, |
BlockValidationState & | state, | ||
CBlockIndex * | pindex | ||
) |
Mark a block as invalid.
Definition at line 3527 of file validation.cpp.
|
private |
Definition at line 1364 of file validation.cpp.
|
private |
Definition at line 1330 of file validation.cpp.
bool Chainstate::IsBlockAvalancheFinalized | ( | const CBlockIndex * | pindex | ) | const |
Checks if a block is finalized by avalanche voting.
Definition at line 3672 of file validation.cpp.
bool Chainstate::IsInitialBlockDownload | ( | ) | const |
Check whether we are doing an initial block download (synchronizing from disk or network)
Definition at line 1201 of file validation.cpp.
bool Chainstate::LoadChainTip | ( | ) |
Update the chain tip based on database information, i.e.
CoinsTip()'s best block.
Definition at line 4520 of file validation.cpp.
bool Chainstate::LoadGenesisBlock | ( | ) |
Ensures we have a genesis block in the block tree, possibly writing one to disk.
Definition at line 4984 of file validation.cpp.
void Chainstate::LoadMempool | ( | const Config & | config, |
const ArgsManager & | args | ||
) |
Load the persisted mempool from disk.
Definition at line 4510 of file validation.cpp.
|
inlineprivate |
Indirection necessary to make lock annotations work with an optional mempool.
Definition at line 1022 of file validation.h.
bool Chainstate::ParkBlock | ( | const Config & | config, |
BlockValidationState & | state, | ||
CBlockIndex * | pindex | ||
) |
Park a block.
Definition at line 3538 of file validation.cpp.
bool Chainstate::PreciousBlock | ( | const Config & | config, |
BlockValidationState & | state, | ||
CBlockIndex * | pindex | ||
) |
Mark a block as precious and reorganize.
May not be called in a validationinterface callback.
Definition at line 3248 of file validation.cpp.
void Chainstate::PruneAndFlush | ( | ) |
Prune blockfiles from the disk if necessary and then flush chainstate changes if we pruned.
Definition at line 2379 of file validation.cpp.
void Chainstate::PruneBlockIndexCandidates | ( | ) |
Delete all entries in setBlockIndexCandidates that are worse than the current tip.
Definition at line 2904 of file validation.cpp.
|
private |
Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS).
Definition at line 3683 of file validation.cpp.
|
inline |
Return true if this chainstate relies on blocks that are assumed-valid.
In practice this means it was created based on a UTXO snapshot.
Definition at line 757 of file validation.h.
bool Chainstate::ReplayBlocks | ( | ) |
Replay blocks that aren't fully applied to the database.
Definition at line 4759 of file validation.cpp.
void Chainstate::ResetBlockFailureFlags | ( | CBlockIndex * | pindex | ) |
Remove invalidity status from a block and its descendants.
Definition at line 3601 of file validation.cpp.
|
inline |
Destructs all objects related to accessing the UTXO set.
Definition at line 795 of file validation.h.
|
private |
Apply the effects of a block on the utxo cache, ignoring that it may already have been applied.
Definition at line 4731 of file validation.cpp.
void Chainstate::UnloadBlockIndex | ( | ) |
void Chainstate::UnparkBlock | ( | CBlockIndex * | pindex | ) |
Remove parked status from a block.
Definition at line 3638 of file validation.cpp.
void Chainstate::UnparkBlockAndChildren | ( | CBlockIndex * | pindex | ) |
Remove parked status from a block and its descendants.
Definition at line 3634 of file validation.cpp.
|
private |
Definition at line 3617 of file validation.cpp.
|
private |
Definition at line 3298 of file validation.cpp.
void Chainstate::UpdateFlags | ( | CBlockIndex * | pindex, |
CBlockIndex *& | pindexReset, | ||
F | f, | ||
C | fChild, | ||
AC | fAncestorWasChanged | ||
) |
Definition at line 3572 of file validation.cpp.
bool Chainstate::UpdateFlagsForBlock | ( | CBlockIndex * | pindexBase, |
CBlockIndex * | pindex, | ||
F | f | ||
) |
Definition at line 3549 of file validation.cpp.
|
private |
Check warning conditions and do some notifications on new chain tip set.
Definition at line 1032 of file validation.h.
bool Chainstate::ConnectBlock |
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 864 of file validation.h.
bool m_mempool Chainstate::cs |
Definition at line 873 of file validation.h.
|
mutableprivate |
Definition at line 682 of file validation.h.
|
private |
Definition at line 701 of file validation.h.
|
private |
Definition at line 700 of file validation.h.
node::BlockManager& Chainstate::m_blockman |
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
Definition at line 706 of file validation.h.
|
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 672 of file validation.h.
CChain Chainstate::m_chain |
The current chain of blockheaders we consult and build on.
Definition at line 745 of file validation.h.
ChainstateManager& Chainstate::m_chainman |
The chainstate manager that owns this chainstate.
The reference is necessary so that this instance can check whether it is the active chainstate within deeply nested method calls.
Definition at line 714 of file validation.h.
|
private |
The ChainState Mutex.
A lock that must be held when modifying this ChainState.
Definition at line 653 of file validation.h.
|
private |
Manages the UTXO set, which is a reflection of the contents of m_chain
.
Definition at line 680 of file validation.h.
size_t Chainstate::m_coinsdb_cache_size_bytes {0} |
The cache size of the on-disk coins view.
Definition at line 798 of file validation.h.
size_t Chainstate::m_coinstip_cache_size_bytes {0} |
The cache size of the in-memory coins view.
Definition at line 801 of file validation.h.
|
private |
Filter to prevent parking a block due to block policies more than once.
After first application of block policies, Avalanche voting will determine the final acceptance state. Rare false positives will be reconciled by the network and should not have any negative impact.
Definition at line 697 of file validation.h.
const std::optional<BlockHash> Chainstate::m_from_snapshot_blockhash {} |
The blockhash which is the base of the snapshot this chainstate was created from.
std::nullopt if this chainstate was not created from a snapshot.
Definition at line 753 of file validation.h.
|
private |
Optional mempool that is kept in sync with the chain.
Only the active chainstate has a mempool.
Definition at line 676 of file validation.h.
const CChainParams& Chainstate::m_params |
Chain parameters for this chainstate.
Definition at line 709 of file validation.h.
|
private |
Decreasing counter (used by subsequent preciousblock calls).
Definition at line 662 of file validation.h.
|
private |
Every received block is assigned a unique and increasing identifier, so we know which one to give priority in case of a fork.
Blocks loaded from disk are assigned id 0, so start the counter at 1.
Definition at line 660 of file validation.h.
|
private |
chainwork for the last block that preciousblock has been applied to.
Definition at line 664 of file validation.h.
std::set<CBlockIndex *, CBlockIndexWorkComparator> Chainstate::setBlockIndexCandidates |
The set of all CBlockIndex entries with either BLOCK_VALID_TRANSACTIONS (for itself and all ancestors) or BLOCK_ASSUMED_VALID (if using background chainstates) 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 768 of file validation.h.