Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
BlockManager Class Reference

Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-work tip is. More...

#include <blockstorage.h>

Inheritance diagram for BlockManager:
[legend]
Collaboration diagram for BlockManager:
[legend]

Public Types

using Options = kernel::BlockManagerOpts
using ReadRawBlockResult = util::Expected<std::vector<std::byte>, ReadRawError>

Public Member Functions

BlockMap m_block_index GUARDED_BY (cs_main)
std::unique_ptr< BlockTreeDB > m_block_tree_db GUARDED_BY (::cs_main)
 BlockManager (const util::SignalInterrupt &interrupt, Options opts)
CBlockIndexInsertBlockIndex (const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Create a new block index entry for a given block hash.
void PruneOneBlockFile (int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Mark one block file as pruned (modify associated database entries).
CBlockIndexLookupBlockIndex (const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
const CBlockIndexLookupBlockIndex (const uint256 &hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
CBlockFileInfoGetBlockFileInfo (size_t n)
 Get block file info entry for one block file.
void UpdateBlockInfo (const CBlock &block, unsigned int nHeight, const FlatFilePos &pos)
 Update blockfile info while processing a block during reindex.
bool IsPruneMode () const
 Whether running in -prune mode.
uint64_t GetPruneTarget () const
 Attempt to stay below this number of bytes of block files.
bool LoadingBlocks () const
uint64_t CalculateCurrentUsage ()
 Calculate the amount of disk space the block & undo files currently use.
fs::path GetBlockPosFilename (const FlatFilePos &pos) const
 Translation to a filesystem path.
void UnlinkPrunedFiles (const std::set< int > &setFilesToPrune) const
 Actually unlink the specified files.
bool ReadBlock (CBlock &block, const FlatFilePos &pos, const std::optional< uint256 > &expected_hash) const
 Functions for disk access for blocks.
bool ReadBlock (CBlock &block, const CBlockIndex &index) const
ReadRawBlockResult ReadRawBlock (const FlatFilePos &pos, std::optional< std::pair< size_t, size_t > > block_part=std::nullopt) const
bool ReadBlockUndo (CBlockUndo &blockundo, const CBlockIndex &index) const
void CleanupBlockRevFiles () const

Public Attributes

const util::SignalInterruptm_interrupt
std::atomic< bool > m_importing {false}
std::atomic_bool m_blockfiles_indexed {true}
 Whether all blockfiles have been added to the block tree database.
std::optional< int > m_snapshot_height
 The height of the base block of an assumeutxo snapshot, if one is in use.
std::vector< CBlockIndex * > GetAllBlockIndices() EXCLUSIVE_LOCKS_REQUIRED(std::multimap< CBlockIndex *, CBlockIndex * > m_blocks_unlinked
 All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
void WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(bool LoadBlockIndexDB(const std::optional< uint256 > &snapshot_blockhash) EXCLUSIVE_LOCKS_REQUIRED(void ScanAndUnlinkAlreadyPrunedFiles() EXCLUSIVE_LOCKS_REQUIRED(CBlockIndex AddToBlockIndex )(const CBlockHeader &block, CBlockIndex *&best_header) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Remove any pruned block & undo files that are still on disk.
bool WriteBlockUndo(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePo WriteBlock )(const CBlock &block, int nHeight)
 Store block on disk and update block file statistics.
bool CheckBlockDataAvailability(const CBlockIndex &upper_block, const CBlockIndex &lower_block, BlockStatus block_status=BLOCK_HAVE_DATA) EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex &GetFirstBlock(const CBlockIndex &upper_block LIFETIMEBOUND, uint32_t status_mask, const CBlockIndex *lower_block LIFETIMEBOUND=nullptr) const EXCLUSIVE_LOCKS_REQUIRED(boo m_have_pruned ) = false
 Returns the earliest block with specified status_mask flags set after the latest block not having those flags.
bool IsBlockPruned(const CBlockIndex &block) const EXCLUSIVE_LOCKS_REQUIRED(void UpdatePruneLock(const std::string &name, const PruneLockInfo &lock_info) EXCLUSIVE_LOCKS_REQUIRED(AutoFil OpenBlockFile )(const FlatFilePos &pos, bool fReadOnly) const
 Check whether the block associated with this index entry is pruned or not.

Static Public Attributes

static constexpr auto PRUNE_TARGET_MANUAL {std::numeric_limits<uint64_t>::max()}

Protected Attributes

std::vector< CBlockFileInfom_blockfile_info
std::set< CBlockIndex * > m_dirty_blockindex
 Dirty block index entries.
std::set< int > m_dirty_fileinfo
 Dirty block file entries.

Private Member Functions

const CChainParamsGetParams () const
const Consensus::ParamsGetConsensus () const
bool LoadBlockIndex (const std::optional< uint256 > &snapshot_blockhash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
 Load the blocktree off disk and into memory.
bool FlushBlockFile (int blockfile_num, bool fFinalize, bool finalize_undo)
 Return false if block file or undo file flushing fails.
bool FlushUndoFile (int block_file, bool finalize=false)
 Return false if undo file flushing fails.
FlatFilePos FindNextBlockPos (unsigned int nAddSize, unsigned int nHeight, uint64_t nTime)
 Helper function performing various preparations before a block can be saved to disk: Returns the correct position for the block to be saved, which may be in the current or a new block file depending on nAddSize.
bool FlushChainstateBlockFile (int tip_height)
bool FindUndoPos (BlockValidationState &state, int nFile, FlatFilePos &pos, unsigned int nAddSize)
AutoFile OpenUndoFile (const FlatFilePos &pos, bool fReadOnly=false) const
 Open an undo file (rev?
void FindFilesToPruneManual (std::set< int > &setFilesToPrune, int nManualPruneHeight, const Chainstate &chain)
void FindFilesToPrune (std::set< int > &setFilesToPrune, int last_prune, const Chainstate &chain, ChainstateManager &chainman)
 Prune block and undo files (blk?
std::array< std::optional< BlockfileCursor >, BlockfileType::NUM_TYPES > m_blockfile_cursors GUARDED_BY (cs_LastBlockFile)
std::unordered_map< std::string, PruneLockInfo > m_prune_locks GUARDED_BY (::cs_main)
 Map from external index name to oldest block that must not be pruned.
int MaxBlockfileNum () const EXCLUSIVE_LOCKS_REQUIRED(cs_LastBlockFile)
BlockfileType BlockfileTypeForHeight (int height)

Private Attributes

friend Chainstate
friend ChainstateManager
RecursiveMutex cs_LastBlockFile
bool m_check_for_pruning = false
 Global flag to indicate we should check to see if there are block/undo files that should be deleted.
const bool m_prune_mode
const Obfuscation m_obfuscation
const kernel::BlockManagerOpts m_opts
const FlatFileSeq m_block_file_seq
const FlatFileSeq m_undo_file_seq

Detailed Description

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 Chainstate - information about, e.g., candidate tips is not maintained here.

Definition at line 191 of file blockstorage.h.

Member Typedef Documentation

◆ Options

◆ ReadRawBlockResult

using node::BlockManager::ReadRawBlockResult = util::Expected<std::vector<std::byte>, ReadRawError>

Definition at line 317 of file blockstorage.h.

Constructor & Destructor Documentation

◆ BlockManager()

node::BlockManager::BlockManager ( const util::SignalInterrupt & interrupt,
Options opts )
explicit

Definition at line 1224 of file blockstorage.cpp.

Member Function Documentation

◆ BlockfileTypeForHeight()

BlockfileType node::BlockManager::BlockfileTypeForHeight ( int height)
private

Definition at line 771 of file blockstorage.cpp.

◆ CalculateCurrentUsage()

Calculate the amount of disk space the block & undo files currently use.

Definition at line 793 of file blockstorage.cpp.

◆ CleanupBlockRevFiles()

Definition at line 654 of file blockstorage.cpp.

◆ FindFilesToPrune()

void node::BlockManager::FindFilesToPrune ( std::set< int > & setFilesToPrune,
int last_prune,
const Chainstate & chain,
ChainstateManager & chainman )
private

Prune block and undo files (blk?

??.dat and rev???.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 m_check_for_pruning 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 (CChainParams::nPruneAfterHeight). 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.

Parameters
[out]setFilesToPruneThe set of file indices that can be unlinked will be returned
last_pruneThe last height we're able to prune, according to the prune locks

Definition at line 321 of file blockstorage.cpp.

◆ FindFilesToPruneManual()

void node::BlockManager::FindFilesToPruneManual ( std::set< int > & setFilesToPrune,
int nManualPruneHeight,
const Chainstate & chain )
private

Definition at line 292 of file blockstorage.cpp.

◆ FindNextBlockPos()

FlatFilePos node::BlockManager::FindNextBlockPos ( unsigned int nAddSize,
unsigned int nHeight,
uint64_t nTime )
nodiscardprivate

Helper function performing various preparations before a block can be saved to disk: Returns the correct position for the block to be saved, which may be in the current or a new block file depending on nAddSize.

May flush the previous blockfile to disk if full, updates blockfile info, and checks if there is enough disk space to save the block.

The nAddSize argument passed to this function should include not just the size of the serialized CBlock, but also the size of separator fields (STORAGE_HEADER_BYTES).

Definition at line 833 of file blockstorage.cpp.

◆ FindUndoPos()

bool node::BlockManager::FindUndoPos ( BlockValidationState & state,
int nFile,
FlatFilePos & pos,
unsigned int nAddSize )
private

Definition at line 945 of file blockstorage.cpp.

◆ FlushBlockFile()

bool node::BlockManager::FlushBlockFile ( int blockfile_num,
bool fFinalize,
bool finalize_undo )
nodiscardprivate

Return false if block file or undo file flushing fails.

Definition at line 742 of file blockstorage.cpp.

◆ FlushChainstateBlockFile()

bool node::BlockManager::FlushChainstateBlockFile ( int tip_height)
nodiscardprivate

Definition at line 779 of file blockstorage.cpp.

◆ FlushUndoFile()

bool node::BlockManager::FlushUndoFile ( int block_file,
bool finalize = false )
nodiscardprivate

Return false if undo file flushing fails.

Definition at line 732 of file blockstorage.cpp.

◆ GetBlockFileInfo()

Get block file info entry for one block file.

Definition at line 690 of file blockstorage.cpp.

◆ GetBlockPosFilename()

Translation to a filesystem path.

Definition at line 828 of file blockstorage.cpp.

◆ GetConsensus()

const Consensus::Params & node::BlockManager::GetConsensus ( ) const
inlineprivate

Definition at line 198 of file blockstorage.h.

◆ GetParams()

const CChainParams & node::BlockManager::GetParams ( ) const
inlineprivate

Definition at line 197 of file blockstorage.h.

◆ GetPruneTarget()

uint64_t node::BlockManager::GetPruneTarget ( ) const
inlinenodiscard

Attempt to stay below this number of bytes of block files.

Definition at line 407 of file blockstorage.h.

◆ GUARDED_BY() [1/4]

std::unique_ptr< BlockTreeDB > m_block_tree_db node::BlockManager::GUARDED_BY ( ::cs_main )

◆ GUARDED_BY() [2/4]

std::unordered_map< std::string, PruneLockInfo > m_prune_locks node::BlockManager::GUARDED_BY ( ::cs_main )
private

Map from external index name to oldest block that must not be pruned.

Note
Internally, only blocks at height (height_first - PRUNE_LOCK_BUFFER - 1) and below will be pruned, but callers should avoid assuming any particular buffer size.

◆ GUARDED_BY() [3/4]

std::array< std::optional< BlockfileCursor >, BlockfileType::NUM_TYPES > m_blockfile_cursors node::BlockManager::GUARDED_BY ( cs_LastBlockFile )
private

Since assumedvalid chainstates may be syncing a range of the chain that is very far away from the normal/background validation process, we should segment blockfiles for assumed chainstates. Otherwise, we might have wildly different height ranges mixed into the same block files, which would impair our ability to prune effectively.

This data structure maintains separate blockfile number cursors for each BlockfileType. The ASSUMED state is initialized, when necessary, in FindNextBlockPos().

The first element is the NORMAL cursor, second is ASSUMED.

◆ GUARDED_BY() [4/4]

◆ InsertBlockIndex()

Create a new block index entry for a given block hash.

Definition at line 407 of file blockstorage.cpp.

◆ IsPruneMode()

bool node::BlockManager::IsPruneMode ( ) const
inlinenodiscard

Whether running in -prune mode.

Definition at line 404 of file blockstorage.h.

◆ LoadBlockIndex()

bool node::BlockManager::LoadBlockIndex ( const std::optional< uint256 > & snapshot_blockhash)
private

Load the blocktree off disk and into memory.

Populate certain metadata per index entry (nStatus, nChainWork, nTimeMax, etc.) as well as peripheral collections like m_dirty_blockindex.

Definition at line 423 of file blockstorage.cpp.

◆ LoadingBlocks()

bool node::BlockManager::LoadingBlocks ( ) const
inlinenodiscard

Definition at line 410 of file blockstorage.h.

◆ LookupBlockIndex() [1/2]

Definition at line 217 of file blockstorage.cpp.

◆ LookupBlockIndex() [2/2]

Definition at line 210 of file blockstorage.cpp.

◆ MaxBlockfileNum()

int node::BlockManager::MaxBlockfileNum ( ) const
inlineprivate

Definition at line 273 of file blockstorage.h.

◆ OpenUndoFile()

AutoFile node::BlockManager::OpenUndoFile ( const FlatFilePos & pos,
bool fReadOnly = false ) const
private

Open an undo file (rev?

????.dat)

Definition at line 823 of file blockstorage.cpp.

◆ PruneOneBlockFile()

void node::BlockManager::PruneOneBlockFile ( int fileNumber)

Mark one block file as pruned (modify associated database entries).

Definition at line 258 of file blockstorage.cpp.

◆ ReadBlock() [1/2]

bool node::BlockManager::ReadBlock ( CBlock & block,
const CBlockIndex & index ) const

Definition at line 1077 of file blockstorage.cpp.

◆ ReadBlock() [2/2]

bool node::BlockManager::ReadBlock ( CBlock & block,
const FlatFilePos & pos,
const std::optional< uint256 > & expected_hash ) const

Functions for disk access for blocks.

Definition at line 1036 of file blockstorage.cpp.

◆ ReadBlockUndo()

bool node::BlockManager::ReadBlockUndo ( CBlockUndo & blockundo,
const CBlockIndex & index ) const

Definition at line 697 of file blockstorage.cpp.

◆ ReadRawBlock()

BlockManager::ReadRawBlockResult node::BlockManager::ReadRawBlock ( const FlatFilePos & pos,
std::optional< std::pair< size_t, size_t > > block_part = std::nullopt ) const

Definition at line 1083 of file blockstorage.cpp.

◆ UnlinkPrunedFiles()

void node::BlockManager::UnlinkPrunedFiles ( const std::set< int > & setFilesToPrune) const

Actually unlink the specified files.

Definition at line 804 of file blockstorage.cpp.

◆ UpdateBlockInfo()

void node::BlockManager::UpdateBlockInfo ( const CBlock & block,
unsigned int nHeight,
const FlatFilePos & pos )

Update blockfile info while processing a block during reindex.

The block must be available on disk.

Parameters
[in]blockthe block being processed
[in]nHeightthe height of the block
[in]posthe position of the serialized CBlock on disk

Definition at line 923 of file blockstorage.cpp.

Member Data Documentation

◆ AddToBlockIndex

Remove any pruned block & undo files that are still on disk.

This could happen on some systems if the file was still being read while unlinked, or if we crash before unlinking.

Definition at line 369 of file blockstorage.h.

◆ Chainstate

Definition at line 193 of file blockstorage.h.

◆ ChainstateManager

Definition at line 194 of file blockstorage.h.

◆ cs_LastBlockFile

◆ m_block_file_seq

Definition at line 303 of file blockstorage.h.

◆ m_blockfile_info

Definition at line 307 of file blockstorage.h.

◆ m_blockfiles_indexed

Whether all blockfiles have been added to the block tree database.

Normally true, but set to false when a reindex is requested and the database is wiped. The value is persisted in the database across restarts and will be false until reindexing completes.

Definition at line 330 of file blockstorage.h.

◆ 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 354 of file blockstorage.h.

◆ m_check_for_pruning

Global flag to indicate we should check to see if there are block/undo files that should be deleted.

Set on startup or if we allocate more file space when we're in prune mode

Definition at line 285 of file blockstorage.h.

◆ m_dirty_blockindex

Dirty block index entries.

Definition at line 310 of file blockstorage.h.

◆ m_dirty_fileinfo

std::set<int> node::BlockManager::m_dirty_fileinfo
protected

Dirty block file entries.

Definition at line 313 of file blockstorage.h.

◆ m_have_pruned

bool CheckBlockDataAvailability (const CBlockIndex& upper_block, const CBlockIndex& lower_block, BlockStatus block_status = BLOCK_HAVE_DATA) EXCLUSIVE_LOCKS_REQUIRED( const CBlockIndex& GetFirstBlock ( const CBlockIndex& upper_block LIFETIMEBOUND, uint32_t status_mask, const CBlockIndex* lower_block LIFETIMEBOUND = nullptr ) const EXCLUSIVE_LOCKS_REQUIRED( boo node::BlockManager::m_have_pruned) = false

Returns the earliest block with specified status_mask flags set after the latest block not having those flags.

Check if all blocks in the [upper_block, lower_block] range have data available as defined by the status mask. The caller is responsible for ensuring that lower_block is an ancestor of upper_block (part of the same chain).

This function starts from upper_block, which must have all status_mask flags set, and iterates backwards through its ancestors. It continues as long as each block has all status_mask flags set, until reaching the oldest ancestor or lower_block.

Precondition
upper_block must have all status_mask flags set.
lower_block must be null or an ancestor of upper_block
Parameters
upper_blockThe starting block for the search, which must have all status_mask flags set.
status_maskBitmask specifying required status flags.
lower_blockThe earliest possible block to return. If null, the search can extend to the genesis block.
Returns
A reference to the earliest block between upper_block and lower_block, inclusive, such that every block between the returned block and upper_block has status_mask flags set. True if any block files have ever been pruned.

Definition at line 450 of file blockstorage.h.

◆ m_importing

std::atomic<bool> node::BlockManager::m_importing {false}

Definition at line 322 of file blockstorage.h.

◆ m_interrupt

◆ m_obfuscation

Definition at line 289 of file blockstorage.h.

◆ m_opts

Definition at line 301 of file blockstorage.h.

◆ m_prune_mode

Definition at line 287 of file blockstorage.h.

◆ m_snapshot_height

The height of the base block of an assumeutxo snapshot, if one is in use.

This controls how blockfiles are segmented by chainstate type to avoid comingling different height regions of the chain when an assumedvalid chainstate is in use. If heights are drastically different in the same blockfile, pruning suffers.

This is set during ActivateSnapshot() or upon LoadBlockIndex() if a snapshot had been previously loaded. After the snapshot is validated, this is unset to restore normal LoadBlockIndex behavior.

Definition at line 346 of file blockstorage.h.

◆ m_undo_file_seq

Definition at line 304 of file blockstorage.h.

◆ OpenBlockFile

Check whether the block associated with this index entry is pruned or not.

Create or update a prune lock identified by its name Open a block file (blk?????.dat)

Definition at line 459 of file blockstorage.h.

◆ PRUNE_TARGET_MANUAL

auto node::BlockManager::PRUNE_TARGET_MANUAL {std::numeric_limits<uint64_t>::max()}
staticconstexpr

Definition at line 408 of file blockstorage.h.

◆ WriteBlock

Store block on disk and update block file statistics.

Parameters
[in]blockthe block to be stored
[in]nHeightthe height of the block
Returns
in case of success, the position to which the block was written to in case of an error, an empty FlatFilePos

Definition at line 393 of file blockstorage.h.


The documentation for this class was generated from the following files: