![]() |
Bitcoin Core 22.99.0
P2P Digital Currency
|
Maintains a tree of blocks (stored in m_block_index
) which is consulted to determine where the most-work tip is.
More...
#include <validation.h>
Public Member Functions | |
BlockMap m_block_index | GUARDED_BY (cs_main) |
std::unique_ptr< CBlockTreeDB > m_block_tree_db | GUARDED_BY (::cs_main) |
void | Unload () EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Clear all data members. More... | |
CBlockIndex * | AddToBlockIndex (const CBlockHeader &block) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
CBlockIndex * | InsertBlockIndex (const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Create a new block index entry for a given block hash. More... | |
void | PruneOneBlockFile (const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Mark one block file as pruned (modify associated database entries) More... | |
bool | AcceptBlockHeader (const CBlockHeader &block, BlockValidationState &state, const CChainParams &chainparams, CBlockIndex **ppindex) 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... | |
CBlockIndex * | LookupBlockIndex (const uint256 &hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
CBlockIndex * | FindForkInGlobalIndex (const CChain &chain, const CBlockLocator &locator) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Find the last common block between the parameter chain and a locator. More... | |
CBlockIndex * | GetLastCheckpoint (const CCheckpointData &data) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Returns last CBlockIndex* that is a checkpoint. More... | |
int | GetSpendHeight (const CCoinsViewCache &inputs) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Return the spend height, which is one more than the inputs.GetBestBlock(). More... | |
~BlockManager () | |
Public Attributes | |
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... | |
std::multimap< CBlockIndex *, CBlockIndex * > | m_blocks_unlinked |
All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions. More... | |
bool LoadBlockIndexDB(std::set< CBlockIndex *, CBlockIndexWorkComparator > &setBlockIndexCandidates) EXCLUSIVE_LOCKS_REQUIRED(boo | LoadBlockIndex )(const Consensus::Params &consensus_params, std::set< CBlockIndex *, CBlockIndexWorkComparator > &block_index_candidates) EXCLUSIVE_LOCKS_REQUIRED(cs_main) |
Load the blocktree off disk and into memory. More... | |
Private Member Functions | |
void | FindFilesToPruneManual (std::set< int > &setFilesToPrune, int nManualPruneHeight, int chain_tip_height) |
void | FindFilesToPrune (std::set< int > &setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd) |
Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a user-defined target. More... | |
Private Attributes | |
friend | CChainState |
Maintains a tree of blocks (stored in m_block_index
) which is consulted to determine where the most-work tip is.
This data is used mostly in CChainState
- information about, e.g., candidate tips is not maintained here.
Definition at line 374 of file validation.h.
|
inline |
bool BlockManager::AcceptBlockHeader | ( | const CBlockHeader & | block, |
BlockValidationState & | state, | ||
const CChainParams & | chainparams, | ||
CBlockIndex ** | ppindex | ||
) |
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.
Definition at line 3188 of file validation.cpp.
CBlockIndex * BlockManager::AddToBlockIndex | ( | const CBlockHeader & | block | ) |
Definition at line 2833 of file validation.cpp.
|
private |
Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a user-defined target.
The user sets the target (in MB) on the command line or in config file. This will be run on startup and whenever new space is allocated in a block or undo file, staying below the target. Changing back to unpruned requires a reindex (which in this case means the blockchain must be re-downloaded.)
Pruning functions are called from FlushStateToDisk when the global fCheckForPruning flag has been set. Block and undo files are deleted in lock-step (when blk00003.dat is deleted, so is rev00003.dat.) Pruning cannot take place until the longest chain is at least a certain length (100000 on mainnet, 1000 on testnet, 1000 on regtest). Pruning will never delete a block within a defined distance (currently 288) from the active chain's tip. The block index is updated by unsetting HAVE_DATA and HAVE_UNDO for any blocks that were stored in the deleted files. A db flag records the fact that at least some block files have been pruned.
[out] | setFilesToPrune | The set of file indices that can be unlinked will be returned |
Definition at line 3529 of file validation.cpp.
|
private |
Definition at line 3496 of file validation.cpp.
CBlockIndex * BlockManager::FindForkInGlobalIndex | ( | const CChain & | chain, |
const CBlockLocator & | locator | ||
) |
Find the last common block between the parameter chain and a locator.
Definition at line 157 of file validation.cpp.
CBlockIndex * BlockManager::GetLastCheckpoint | ( | const CCheckpointData & | data | ) |
Returns last CBlockIndex* that is a checkpoint.
Definition at line 3035 of file validation.cpp.
int BlockManager::GetSpendHeight | ( | const CCoinsViewCache & | inputs | ) |
Return the spend height, which is one more than the inputs.GetBestBlock().
While checking, GetBestBlock() refers to the parent block. (protected by cs_main) This is also true for mempool checks.
Definition at line 1245 of file validation.cpp.
std::unique_ptr< CBlockTreeDB > m_block_tree_db BlockManager::GUARDED_BY | ( | ::cs_main | ) |
CBlockIndex * BlockManager::InsertBlockIndex | ( | const uint256 & | hash | ) |
Create a new block index entry for a given block hash.
Definition at line 3588 of file validation.cpp.
CBlockIndex * BlockManager::LookupBlockIndex | ( | const uint256 & | hash | ) | const |
Definition at line 150 of file validation.cpp.
void BlockManager::PruneOneBlockFile | ( | const int | fileNumber | ) |
Mark one block file as pruned (modify associated database entries)
BLOCK PRUNING CODE.
Definition at line 3462 of file validation.cpp.
void BlockManager::Unload | ( | ) |
Clear all data members.
Definition at line 3665 of file validation.cpp.
|
private |
Definition at line 376 of file validation.h.
bool BlockManager::LoadBlockIndex |
Load the blocktree off disk and into memory.
Populate certain metadata per index entry (nStatus, nChainWork, nTimeMax, etc.) as well as peripheral collections like setDirtyBlockIndex.
[out] | block_index_candidates | Fill this set with any valid blocks for which we've downloaded all transactions. |
Definition at line 440 of file validation.h.
std::multimap<CBlockIndex*, CBlockIndex*> BlockManager::m_blocks_unlinked |
All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
Pruned nodes may have entries where B is missing data.
Definition at line 426 of file validation.h.
std::set<CBlockIndex*> BlockManager::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 420 of file validation.h.