![]() |
Bitcoin Core 30.99.0
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< uint256 > from_snapshot_blockhash=std::nullopt) | |
| ChainstateRole GetRole() const EXCLUSIVE_LOCKS_REQUIRED(void | InitCoinsDB (size_t cache_size_bytes, bool in_memory, bool should_wipe, fs::path leveldb_name="chainstate") |
| Return the current role of the chainstate. More... | |
| void InitCoinsCache(size_t cache_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(bool | CanFlushToDisk () const EXCLUSIVE_LOCKS_REQUIRED( |
| Initialize the in-memory coins cache (to be done after the health of the on-disk database is verified). More... | |
| CCoinsViewCache & | CoinsTip () EXCLUSIVE_LOCKS_REQUIRED( |
| CCoinsViewDB & | CoinsDB () EXCLUSIVE_LOCKS_REQUIRED( |
| CTxMemPool * | GetMempool () |
| CCoinsViewErrorCatcher & | CoinsErrorCatcher () EXCLUSIVE_LOCKS_REQUIRED( |
| void | ResetCoinsViews () |
| Destructs all objects related to accessing the UTXO set. More... | |
| bool | HasCoinsViews () const |
| Does this chainstate have a UTXO set attached? More... | |
| bool ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size) EXCLUSIVE_LOCKS_REQUIRED(bool | FlushStateToDisk (BlockValidationState &state, FlushStateMode mode, int nManualPruneHeight=0) |
| Resize the CoinsViews caches dynamically and flush state to disk. 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 | DisconnectTip (BlockValidationState &state, DisconnectedBlockTransactions *disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main |
| Disconnect m_chain's tip. More... | |
| bool | ReplayBlocks () |
| Replay blocks that aren't fully applied to the database. More... | |
| bool | NeedsRedownload () const EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
| Whether the chain state needs to be redownloaded due to lack of witness data. More... | |
| bool | LoadGenesisBlock () |
| Ensures we have a genesis block in the block tree, possibly writing one to disk. More... | |
| void | TryAddBlockIndexCandidate (CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
| void | PruneBlockIndexCandidates () |
| Delete all entries in setBlockIndexCandidates that are worse than the current tip. More... | |
| void ClearBlockIndexCandidates() EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex * | FindForkInGlobalIndex (const CBlockLocator &locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
| Find the last common block of this chain and a locator. More... | |
| bool | LoadChainTip () EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
| Update the chain tip based on database information, i.e. More... | |
| CoinsCacheSizeState GetCoinsCacheSizeState() EXCLUSIVE_LOCKS_REQUIRED(CoinsCacheSizeState GetCoinsCacheSizeState(size_t max_coins_cache_size_bytes, size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(RecursiveMutex * | MempoolMutex () const LOCK_RETURNED(m_mempool -> cs) |
| Dictates whether we need to flush the cache to disk or not. More... | |
Public Attributes | |
| node::BlockManager & | m_blockman |
| Reference to a BlockManager instance which itself is shared across all Chainstate instances. 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< uint256 > | m_from_snapshot_blockhash |
| The blockhash which is the base of the snapshot this chainstate was created from. More... | |
| const CBlockIndex *SnapshotBase() const EXCLUSIVE_LOCKS_REQUIRED(std::set< CBlockIndex *, node::CBlockIndexWorkComparator > | setBlockIndexCandidates |
| The base of the snapshot this chainstate was created from. 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... | |
| bool ActivateBestChain(BlockValidationState &state, std::shared_ptr< const CBlock > pblock=nullptr) LOCKS_EXCLUDED(DisconnectResult DisconnectBlock(const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &view) EXCLUSIVE_LOCKS_REQUIRED(boo | ConnectBlock )(const CBlock &block, BlockValidationState &state, CBlockIndex *pindex, CCoinsViewCache &view, bool fJustCheck=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
| Find the best known block, and make it the tip of the block chain. More... | |
| bool m_mempool | cs |
| bool PreciousBlock(BlockValidationState &state, CBlockIndex *pindex) LOCKS_EXCLUDED(bool InvalidateBlock(BlockValidationState &state, CBlockIndex *pindex) LOCKS_EXCLUDED(void SetBlockFailureFlags(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(voi | ResetBlockFailureFlags )(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
| Mark a block as precious and reorganize. More... | |
Protected Member Functions | |
| bool m_disabled | GUARDED_BY (::cs_main) |
| This toggle exists for use when doing background validation for UTXO snapshots. More... | |
| const CBlockIndex *m_cached_snapshot_base | GUARDED_BY (::cs_main) |
| Cached result of LookupBlockIndex(*m_from_snapshot_blockhash) More... | |
| std::optional< const char * > m_last_script_check_reason_logged | GUARDED_BY (::cs_main) |
| bool | ActivateBestChainStep (BlockValidationState &state, 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 (BlockValidationState &state, CBlockIndex *pindexNew, std::shared_ptr< const CBlock > block_to_connect, 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 () 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... | |
| 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 | CheckForkWarningConditions () EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
| void | InvalidChainFound (CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
| void | MaybeUpdateMempoolForReorg (DisconnectedBlockTransactions &disconnectpool, bool fAddToMempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main |
| Make mempool consistent after a reorg, by re-adding or recursively erasing disconnected block transactions from the mempool, and also removing any other transactions from the mempool that are no longer valid given the new tip/height. More... | |
Protected Attributes | |
| Mutex | m_chainstate_mutex |
| The ChainState Mutex A lock that must be held when modifying this ChainState - held in ActivateBestChain() and InvalidateBlock() 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... | |
| void m_mempool | cs |
| void UpdateTip(const CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(NodeClock::time_poin | m_next_write ) {NodeClock::time_point::max()} |
| Check warning conditions and do some notifications on new chain tip set. More... | |
| util::Result< void > InvalidateCoinsDBOnDisk() EXCLUSIVE_LOCKS_REQUIRED(friend | ChainstateManager |
| In case of an invalid snapshot, rename the coins leveldb directory so that it can be examined for issue diagnosis. 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 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 531 of file validation.h.
|
explicit |
Definition at line 1942 of file validation.cpp.
|
protected |
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 3307 of file validation.cpp.
|
inline |
Initialize the in-memory coins cache (to be done after the health of the on-disk database is verified).
Definition at line 606 of file validation.h.
|
protected |
Definition at line 2022 of file validation.cpp.
|
inline |
Definition at line 648 of file validation.h.
|
inline |
Definition at line 662 of file validation.h.
|
inline |
Definition at line 640 of file validation.h.
|
protected |
Connect a new block to m_chain.
block_to_connect 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 3126 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 MaybeUpdateMempoolForReorg. 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 3015 of file validation.cpp.
| const CBlockIndex * Chainstate::FindForkInGlobalIndex | ( | const CBlockLocator & | locator | ) | const |
Find the last common block of this chain and a locator.
Definition at line 121 of file validation.cpp.
|
protected |
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 3230 of file validation.cpp.
| bool Chainstate::FlushStateToDisk | ( | BlockValidationState & | state, |
| FlushStateMode | mode, | ||
| int | nManualPruneHeight = 0 |
||
| ) |
Resize the CoinsViews caches dynamically and flush state to disk.
If FlushStateMode::NONE is used, then FlushStateToDisk(...) won't do anything besides checking if we need to prune.
Definition at line 2781 of file validation.cpp.
| void Chainstate::ForceFlushStateToDisk | ( | ) |
Unconditionally flush all changes to disk.
Definition at line 2928 of file validation.cpp.
|
inline |
Definition at line 655 of file validation.h.
|
inlineprotected |
This toggle exists for use when doing background validation for UTXO snapshots.
In the expected case, it is set once the background validation chain reaches the same height as the base of the snapshot and its UTXO set is found to hash to the expected assumeutxo value. It signals that we should no longer connect blocks to the background chainstate. When set on the background validation chainstate, it signifies that we have fully validated the snapshot chainstate.
In the unlikely case that the snapshot chainstate is found to be invalid, this is set to true on the snapshot chainstate.
Definition at line 559 of file validation.h.
|
inlinemutableprotected |
Cached result of LookupBlockIndex(*m_from_snapshot_blockhash)
Definition at line 562 of file validation.h.
|
inlineprotected |
Definition at line 564 of file validation.h.
|
inline |
Does this chainstate have a UTXO set attached?
Definition at line 672 of file validation.h.
| void Chainstate::InitCoinsDB | ( | size_t | cache_size_bytes, |
| bool | in_memory, | ||
| bool | should_wipe, | ||
| fs::path | leveldb_name = "chainstate" |
||
| ) |
Return the current role of the chainstate.
See ChainstateManager documentation for a description of the different types of chainstates.
All parameters forwarded to CoinsViews.
Definition at line 1959 of file validation.cpp.
|
protected |
Definition at line 2068 of file validation.cpp.
|
protected |
Definition at line 2044 of file validation.cpp.
| bool Chainstate::LoadChainTip | ( | ) |
Update the chain tip based on database information, i.e.
CoinsTip()'s best block.
Definition at line 4658 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 5021 of file validation.cpp.
|
protected |
Make mempool consistent after a reorg, by re-adding or recursively erasing disconnected block transactions from the mempool, and also removing any other transactions from the mempool that are no longer valid given the new tip/height.
Note: we assume that disconnectpool only contains transactions that are NOT confirmed in the current chain nor already in the mempool (otherwise, in-mempool descendants of such transactions would be removed).
Passing fAddToMempool=false will skip trying to add the transactions back, and instead just erase from the mempool as needed.
Definition at line 295 of file validation.cpp.
|
inline |
Dictates whether we need to flush the cache to disk or not.
Definition at line 796 of file validation.h.
| bool Chainstate::NeedsRedownload | ( | ) | const |
Whether the chain state needs to be redownloaded due to lack of witness data.
Definition at line 4958 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 2936 of file validation.cpp.
| void Chainstate::PruneBlockIndexCandidates | ( | ) |
Delete all entries in setBlockIndexCandidates that are worse than the current tip.
Definition at line 3290 of file validation.cpp.
| bool Chainstate::ReplayBlocks | ( | ) |
Replay blocks that aren't fully applied to the database.
Definition at line 4883 of file validation.cpp.
|
inline |
Destructs all objects related to accessing the UTXO set.
Definition at line 669 of file validation.h.
|
protected |
Apply the effects of a block on the utxo cache, ignoring that it may already have been applied.
Definition at line 4861 of file validation.cpp.
| void Chainstate::TryAddBlockIndexCandidate | ( | CBlockIndex * | pindex | ) |
Definition at line 3848 of file validation.cpp.
|
protected |
In case of an invalid snapshot, rename the coins leveldb directory so that it can be examined for issue diagnosis.
Definition at line 847 of file validation.h.
| bool Chainstate::ConnectBlock |
Find the best known block, and make it the tip of the block chain.
Apply the effects of this block (with given index) on the UTXO set represented by coins.
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.
Note that if this is called while a snapshot chainstate is active, and if it is called on a background chainstate whose tip has reached the base block of the snapshot, its execution will take MINUTES while it hashes the background UTXO set to verify the assumeutxo value the snapshot was activated with. cs_main will be held during this time.
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 738 of file validation.h.
| bool m_mempool Chainstate::cs |
Definition at line 742 of file validation.h.
|
protected |
Definition at line 833 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 569 of file validation.h.
| CChain Chainstate::m_chain |
The current chain of blockheaders we consult and build on.
Definition at line 614 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 574 of file validation.h.
|
protected |
The ChainState Mutex A lock that must be held when modifying this ChainState - held in ActivateBestChain() and InvalidateBlock()
Definition at line 539 of file validation.h.
|
protected |
Manages the UTXO set, which is a reflection of the contents of m_chain.
Definition at line 546 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 675 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 678 of file validation.h.
| const std::optional<uint256> 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 621 of file validation.h.
|
protected |
Optional mempool that is kept in sync with the chain.
Only the active chainstate has a mempool.
Definition at line 543 of file validation.h.
|
protected |
Check warning conditions and do some notifications on new chain tip set.
Definition at line 839 of file validation.h.
| void Chainstate::ResetBlockFailureFlags |
Mark a block as precious and reorganize.
May not be called in a validationinterface callback. Mark a block as invalid. Set invalidity status to all descendants of a block Remove invalidity status from a block, its descendants and ancestors and reconsider them for activation
Definition at line 762 of file validation.h.
| const CBlockIndex* SnapshotBase () const EXCLUSIVE_LOCKS_REQUIRED( std::set<CBlockIndex*, node::CBlockIndexWorkComparator> Chainstate::setBlockIndexCandidates |
The base of the snapshot this chainstate was created from.
nullptr if this chainstate was not created from a snapshot. The set of all CBlockIndex entries that have as much work as our current tip or more, and transaction data needed to be validated (with BLOCK_VALID_TRANSACTIONS for each block and its parents back to the genesis block or an assumeutxo snapshot block). Entries may be failed, though, and pruning nodes may be missing the data for the block.
Definition at line 637 of file validation.h.