Bitcoin ABC
0.26.3
P2P Digital Currency
|
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate generated by downloading blocks, and an optional snapshot chainstate loaded from a UTXO snapshot. More...
#include <validation.h>
Public Types | |
using | Options = kernel::ChainstateManagerOpts |
Public Member Functions | |
ChainstateManager (Options options, node::BlockManager::Options blockman_options) | |
const Config & | GetConfig () const |
const CChainParams & | GetParams () const |
const Consensus::Params & | GetConsensus () const |
bool | ShouldCheckBlockIndex () const |
const arith_uint256 & | MinimumChainWork () const |
const BlockHash & | AssumedValidBlock () const |
RecursiveMutex & | GetMutex () const LOCK_RETURNED( |
Alias for cs_main. More... | |
CBlockIndex *m_best_header | GUARDED_BY (::cs_main) |
Best header we've seen so far (used for getheaders queries' starting points). More... | |
bool | ActivateSnapshot (AutoFile &coins_file, const node::SnapshotMetadata &metadata, bool in_memory) |
Construct and activate a Chainstate on the basis of UTXO snapshot data. More... | |
CChain & | ActiveChain () const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) |
int | ActiveHeight () const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) |
CBlockIndex * | ActiveTip () const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) |
node::BlockMap & | BlockIndex () EXCLUSIVE_LOCKS_REQUIRED( |
bool | IsSnapshotActive () const |
std::optional< BlockHash > | SnapshotBlockhash () const |
bool | IsSnapshotValidated () const EXCLUSIVE_LOCKS_REQUIRED( |
Is there a snapshot in use and has it been fully validated? More... | |
bool | ProcessNewBlock (const std::shared_ptr< const CBlock > &block, bool force_processing, bool min_pow_checked, bool *new_block) LOCKS_EXCLUDED(cs_main) |
Process an incoming block. More... | |
bool | ProcessNewBlockHeaders (const std::vector< CBlockHeader > &block, bool min_pow_checked, BlockValidationState &state, const CBlockIndex **ppindex=nullptr, const std::optional< CCheckpointData > &test_checkpoints=std::nullopt) LOCKS_EXCLUDED(cs_main) |
Process incoming block headers. More... | |
MempoolAcceptResult | ProcessTransaction (const CTransactionRef &tx, bool test_accept=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Try to add a transaction to the memory pool. More... | |
bool | LoadBlockIndex () EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Load the block tree and coins database from disk, initializing state if we're running with -reindex. More... | |
Public Attributes | |
const Options | m_options |
std::thread | m_load_block |
node::BlockManager | m_blockman |
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating block metadata. 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... | |
int64_t | m_total_coinstip_cache {0} |
The total number of bytes available for us to use across all in-memory coins caches. More... | |
int64_t | m_total_coinsdb_cache {0} |
The total number of bytes available for us to use across all leveldb coins databases. More... | |
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * | GetAll )() |
Instantiate a new chainstate. More... | |
Private Member Functions | |
std::unique_ptr< Chainstate > m_ibd_chainstate | GUARDED_BY (::cs_main) |
The chainstate used under normal operation (i.e. More... | |
std::unique_ptr< Chainstate > m_snapshot_chainstate | GUARDED_BY (::cs_main) |
A chainstate initialized on the basis of a UTXO snapshot. More... | |
Chainstate *m_active_chainstate | GUARDED_BY (::cs_main) |
Points to either the ibd or snapshot chainstate; indicates our most-work chain. More... | |
CBlockIndex *m_best_invalid | GUARDED_BY (::cs_main) |
CBlockIndex *m_best_parked | GUARDED_BY (::cs_main) |
bool | PopulateAndValidateSnapshot (Chainstate &snapshot_chainstate, AutoFile &coins_file, const node::SnapshotMetadata &metadata) |
Internal helper for ActivateSnapshot(). More... | |
bool | AcceptBlockHeader (const CBlockHeader &block, BlockValidationState &state, CBlockIndex **ppindex, bool min_pow_checked, const std::optional< CCheckpointData > &test_checkpoints=std::nullopt) 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 m_block_index. More... | |
SteadyMilliseconds m_last_presync_update | GUARDED_BY (::cs_main) |
Most recent headers presync progress update, for rate-limiting. More... | |
Private Attributes | |
friend | Chainstate |
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate generated by downloading blocks, and an optional snapshot chainstate loaded from a UTXO snapshot.
Managed chainstates can be maintained at different heights simultaneously.
This class provides abstractions that allow the retrieval of the current most-work chainstate ("Active") as well as chainstates which may be in background use to validate UTXO snapshots.
Definitions:
IBD chainstate: a chainstate whose current state has been "fully" validated by the initial block download process.
Snapshot chainstate: a chainstate populated by loading in an assumeutxo UTXO snapshot.
Active chainstate: the chainstate containing the current most-work chain. Consulted by most parts of the system (net_processing, wallet) as a reflection of the current chain and UTXO set. This may either be an IBD chainstate or a snapshot chainstate.
Background IBD chainstate: an IBD chainstate for which the IBD process is happening in the background while use of the active (snapshot) chainstate allows the rest of the system to function.
Definition at line 1144 of file validation.h.
Definition at line 1231 of file validation.h.
|
explicit |
Definition at line 6411 of file validation.cpp.
|
private |
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 m_block_index.
If the provided block header is valid, add it to the block index.
Caller must set min_pow_checked=true in order to add a new header to the block index (permanent memory storage), indicating that the header is known to be part of a sufficiently high-work chain (anti-dos check).
Returns true if the block is successfully added to the block index.
Definition at line 4083 of file validation.cpp.
bool ChainstateManager::ActivateSnapshot | ( | AutoFile & | coins_file, |
const node::SnapshotMetadata & | metadata, | ||
bool | in_memory | ||
) |
Construct and activate a Chainstate on the basis of UTXO snapshot data.
Steps:
CoinsViews
contents from coins_file
.m_snapshot_chainstate
and make it our ActiveChainstate(). Definition at line 5828 of file validation.cpp.
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Definition at line 1361 of file validation.h.
|
inline |
|
inline |
|
inline |
Alias for cs_main.
Should be used in new code to make it easier to make cs_main a member of this class. Generally, methods of this class should be annotated to require this mutex. This will make calling code more verbose, but also help to:
Definition at line 1265 of file validation.h.
|
inline |
|
private |
The chainstate used under normal operation (i.e.
"regular" IBD) or, if a snapshot is in use, for background validation.
Its contents (including on-disk data) will be deleted upon shutdown after background validation of the snapshot has completed. We do not free the chainstate contents immediately after it finishes validation to cautiously avoid a case where some other part of the system is still using this pointer (e.g. net_processing).
Once this pointer is set to a corresponding chainstate, it will not be reset until init.cpp:Shutdown().
This is especially important when, e.g., calling ActivateBestChain() on all chainstates because we are not able to hold cs_main going into that call.
|
private |
A chainstate initialized on the basis of a UTXO snapshot.
If this is non-null, it is always our active chainstate.
Once this pointer is set to a corresponding chainstate, it will not be reset until init.cpp:Shutdown().
This is especially important when, e.g., calling ActivateBestChain() on all chainstates because we are not able to hold cs_main going into that call.
|
inlineprivate |
Points to either the ibd or snapshot chainstate; indicates our most-work chain.
Once this pointer is set to a corresponding chainstate, it will not be reset until init.cpp:Shutdown().
This is especially important when, e.g., calling ActivateBestChain() on all chainstates because we are not able to hold cs_main going into that call.
Definition at line 1183 of file validation.h.
|
inlineprivate |
Definition at line 1185 of file validation.h.
|
inlineprivate |
Definition at line 1186 of file validation.h.
|
inlineprivate |
Most recent headers presync progress update, for rate-limiting.
Definition at line 1228 of file validation.h.
|
inline |
Best header we've seen so far (used for getheaders queries' starting points).
Definition at line 1300 of file validation.h.
bool ChainstateManager::IsSnapshotActive | ( | ) | const |
Definition at line 6338 of file validation.cpp.
|
inline |
Is there a snapshot in use and has it been fully validated?
Definition at line 1373 of file validation.h.
bool ChainstateManager::LoadBlockIndex | ( | ) |
Load the block tree and coins database from disk, initializing state if we're running with -reindex.
Definition at line 4967 of file validation.cpp.
|
inline |
|
private |
Internal helper for ActivateSnapshot().
Definition at line 5969 of file validation.cpp.
bool ChainstateManager::ProcessNewBlock | ( | const std::shared_ptr< const CBlock > & | block, |
bool | force_processing, | ||
bool | min_pow_checked, | ||
bool * | new_block | ||
) |
Process an incoming block.
This only returns after the best known valid block is made active. Note that it does not, however, guarantee that the specific block passed to it has been checked for validity!
If you want to possibly get feedback on whether block is valid, you must install a CValidationInterface (see validationinterface.h) - this will have its BlockChecked method called whenever any block completes validation.
Note that we guarantee that either the proof-of-work is valid on block, or (and possibly also) BlockChecked will have been called.
May not be called in a validationinterface callback.
[in] | block | The block we want to process. |
[in] | force_processing | Process this block even if unrequested; used for non-network block sources. |
[in] | min_pow_checked | True if proof-of-work anti-DoS checks have been done by caller for headers chain (note: only affects headers acceptance; if block header is already present in block index then this parameter has no effect) |
[out] | new_block | A boolean which is set to indicate if the block was first received via this call. |
Definition at line 4460 of file validation.cpp.
bool ChainstateManager::ProcessNewBlockHeaders | ( | const std::vector< CBlockHeader > & | block, |
bool | min_pow_checked, | ||
BlockValidationState & | state, | ||
const CBlockIndex ** | ppindex = nullptr , |
||
const std::optional< CCheckpointData > & | test_checkpoints = std::nullopt |
||
) |
Process incoming block headers.
May not be called in a validationinterface callback.
[in] | block | The block headers themselves. |
[in] | min_pow_checked | True if proof-of-work anti-DoS checks have been done by caller for headers chain |
[out] | state | This may be set to an Error state if any error occurred processing them. |
[out] | ppindex | If set, the pointer will be set to point to the last new block index object for the given headers. |
Definition at line 4213 of file validation.cpp.
MempoolAcceptResult ChainstateManager::ProcessTransaction | ( | const CTransactionRef & | tx, |
bool | test_accept = false |
||
) |
Try to add a transaction to the memory pool.
[in] | tx | The transaction to submit for mempool acceptance. |
[in] | test_accept | When true, run validation checks but don't submit to mempool. |
Definition at line 4515 of file validation.cpp.
|
inline |
std::optional< BlockHash > ChainstateManager::SnapshotBlockhash | ( | ) | const |
|
private |
Definition at line 1206 of file validation.h.
std::vector< Chainstate * > ChainstateManager::GetAll |
Instantiate a new chainstate.
[in] | mempool | The mempool to pass to the chainstate Get all chainstates currently being used. |
Definition at line 1318 of file validation.h.
node::BlockManager ChainstateManager::m_blockman |
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating block metadata.
Definition at line 1273 of file validation.h.
std::set<CBlockIndex *> ChainstateManager::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).
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 m_block_index 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 m_block_index 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 m_block_index 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 1294 of file validation.h.
std::thread ChainstateManager::m_load_block |
Definition at line 1270 of file validation.h.
const Options ChainstateManager::m_options |
Definition at line 1269 of file validation.h.
int64_t ChainstateManager::m_total_coinsdb_cache {0} |
The total number of bytes available for us to use across all leveldb coins databases.
This will be split somehow across chainstates.
Definition at line 1308 of file validation.h.
int64_t ChainstateManager::m_total_coinstip_cache {0} |
The total number of bytes available for us to use across all in-memory coins caches.
This will be split somehow across chainstates.
Definition at line 1304 of file validation.h.