[#node-BlockManager]
= xref:node.adoc[node]::BlockManager
:relfileprefix: ../
:mrdocs:

Maintains a tree of blocks (stored in `m&lowbar;block&lowbar;index`) which is consulted to determine where the most&hyphen;work tip is&period;

== Synopsis

Declared in `&lt;node&sol;blockstorage&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
class BlockManager;
----

== Description

This data is used mostly in `Chainstate` &hyphen; information about, e&period;g&period;, candidate tips is not maintained here&period;

== Type Aliases

[cols="1,4"]
|===
| Name| Description
| xref:node/BlockManager/Options.adoc[`Options`] 
| Options controlling block storage behavior&period;
| xref:node/BlockManager/ReadRawBlockResult.adoc[`ReadRawBlockResult`] 
| Result of a raw block read&colon; the bytes on success, or a ReadRawError on failure&period;
|===


== Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:node/BlockManager/2constructor.adoc[`BlockManager`]         [.small]#[constructor]#
| Construct a block manager&period;
| xref:node/BlockManager/AddToBlockIndex.adoc[`AddToBlockIndex`] 
| Add a block header to the in&hyphen;memory block index, updating the best header&period;
| xref:node/BlockManager/AddUnlinkedBlock.adoc[`AddUnlinkedBlock`] 
| Record a block whose ancestor is still missing transaction data&period;
| xref:node/BlockManager/CalculateCurrentUsage.adoc[`CalculateCurrentUsage`] 
| Calculate the amount of disk space the block &amp; undo files currently use
| xref:node/BlockManager/CheckBlockDataAvailability.adoc[`CheckBlockDataAvailability`] 
| Check if all blocks in the &lsqb;upper&lowbar;block, lower&lowbar;block&rsqb;range have data available as defined by the status mask&period; The caller is responsible for ensuring that lower&lowbar;block is an ancestor of upper&lowbar;block (part of the same chain)&period;
| xref:node/BlockManager/CleanupBlockRevFiles.adoc[`CleanupBlockRevFiles`] 
| Remove leftover block and undo files that no longer belong to the block tree&period;
| xref:node/BlockManager/DeletePruneLock.adoc[`DeletePruneLock`] 
| Delete a prune lock identified by its name&period; Returns true if the lock existed&period;
| xref:node/BlockManager/GetAllBlockIndices.adoc[`GetAllBlockIndices`] 
| Return pointers to every block index entry currently held in memory&period;
| xref:node/BlockManager/GetBlockFileInfo.adoc[`GetBlockFileInfo`] 
| Get block file info entry for one block file
| xref:node/BlockManager/GetBlockPosFilename.adoc[`GetBlockPosFilename`] 
| Translation to a filesystem path
| xref:node/BlockManager/GetFirstBlock.adoc[`GetFirstBlock`] 
| Returns the earliest block with specified `status&lowbar;mask` flags set after the latest block _not_ having those flags&period;
| xref:node/BlockManager/GetPruneTarget.adoc[`GetPruneTarget`] 
| Attempt to stay below this number of bytes of block files&period;
| xref:node/BlockManager/InsertBlockIndex.adoc[`InsertBlockIndex`] 
| Create a new block index entry for a given block hash
| xref:node/BlockManager/IsBlockPruned.adoc[`IsBlockPruned`] 
| Check whether the block associated with this index entry is pruned or not&period;
| xref:node/BlockManager/IsPruneMode.adoc[`IsPruneMode`] 
| Whether running in &hyphen;prune mode&period;
| xref:node/BlockManager/LoadBlockIndexDB.adoc[`LoadBlockIndexDB`] 
| Load the block index from the database into memory&period;
| xref:node/BlockManager/LoadingBlocks.adoc[`LoadingBlocks`] 
| Report whether blocks are still being imported or indexed&period;
| xref:node/BlockManager/LookupBlockIndex-05.adoc[`LookupBlockIndex`] 
| Look up a block index entry by hash&period;
| xref:node/BlockManager/OpenBlockFile.adoc[`OpenBlockFile`] 
| Open a block file (blk?????&period;dat)
| xref:node/BlockManager/PruneOneBlockFile.adoc[`PruneOneBlockFile`] 
| Mark one block file as pruned (modify associated database entries)
| xref:node/BlockManager/ReadBlock-02.adoc[`ReadBlock`] 
| `ReadBlock` overloads
| xref:node/BlockManager/ReadBlockUndo.adoc[`ReadBlockUndo`] 
| Read a block&apos;s undo data from disk using its block index entry&period;
| xref:node/BlockManager/ReadRawBlock.adoc[`ReadRawBlock`] 
| Read the raw, still&hyphen;serialized bytes of a block (or a sub&hyphen;range) from disk&period;
| xref:node/BlockManager/ScanAndUnlinkAlreadyPrunedFiles.adoc[`ScanAndUnlinkAlreadyPrunedFiles`] 
| Remove any pruned block &amp; undo files that are still on disk&period; This could happen on some systems if the file was still being read while unlinked, or if we crash before unlinking&period;
| xref:node/BlockManager/UnlinkPrunedFiles.adoc[`UnlinkPrunedFiles`] 
| Actually unlink the specified files
| xref:node/BlockManager/UpdateBlockInfo.adoc[`UpdateBlockInfo`] 
| Update blockfile info while processing a block during reindex&period; The block must be available on disk&period;
| xref:node/BlockManager/UpdatePruneLock.adoc[`UpdatePruneLock`] 
| Create or update a prune lock identified by its name
| xref:node/BlockManager/WriteBlock.adoc[`WriteBlock`] 
| Store block on disk and update block file statistics&period;
| xref:node/BlockManager/WriteBlockIndexDB.adoc[`WriteBlockIndexDB`] 
| Write the dirty block index and block&hyphen;file entries to the database&period;
| xref:node/BlockManager/WriteBlockUndo.adoc[`WriteBlockUndo`] 
| Write a block&apos;s undo data to disk and record it in the block index&period;
|===


== Data Members

[cols="1,4"]
|===
| Name| Description
| xref:node/BlockManager/m_block_index.adoc[`m&lowbar;block&lowbar;index`] 
| The in&hyphen;memory block tree, mapping each block hash to its index entry&period;
| xref:node/BlockManager/m_block_tree_db.adoc[`m&lowbar;block&lowbar;tree&lowbar;db`] 
| Handle to the on&hyphen;disk block index database (blocks&sol;index&sol;)&period;
| xref:node/BlockManager/m_blockfiles_indexed.adoc[`m&lowbar;blockfiles&lowbar;indexed`] 
| Whether all blockfiles have been added to the block tree database&period; Normally true, but set to false when a reindex is requested and the database is wiped&period; The value is persisted in the database across restarts and will be false until reindexing completes&period;
| xref:node/BlockManager/m_blocks_unlinked.adoc[`m&lowbar;blocks&lowbar;unlinked`] 
| All pairs A&hyphen;&gt;B, where A (or one of its ancestors) misses transactions, but B has transactions&period;
| xref:node/BlockManager/m_have_pruned.adoc[`m&lowbar;have&lowbar;pruned`] 
| True if any block files have ever been pruned&period;
| xref:node/BlockManager/m_importing.adoc[`m&lowbar;importing`] 
| Whether blocks are currently being imported from disk&period;
| xref:node/BlockManager/m_interrupt.adoc[`m&lowbar;interrupt`] 
| Interrupt used to abort long&hyphen;running block&hyphen;loading operations&period;
| xref:node/BlockManager/m_snapshot_height.adoc[`m&lowbar;snapshot&lowbar;height`] 
| The height of the base block of an assumeutxo snapshot, if one is in use&period;
|===


== Static Data Members

[cols="1,4"]
|===
| Name| Description
| xref:node/BlockManager/PRUNE_TARGET_MANUAL.adoc[`PRUNE&lowbar;TARGET&lowbar;MANUAL`] 
| Sentinel prune target meaning pruning is manual rather than size&hyphen;based&period;
|===


== Protected Data Members

[cols="1,4"]
|===
| Name| Description
| xref:node/BlockManager/m_blockfile_info.adoc[`m&lowbar;blockfile&lowbar;info`] 
| In&hyphen;memory statistics for each block file, indexed by file number&period;
| xref:node/BlockManager/m_dirty_blockindex.adoc[`m&lowbar;dirty&lowbar;blockindex`] 
| Dirty block index entries&period;
| xref:node/BlockManager/m_dirty_fileinfo.adoc[`m&lowbar;dirty&lowbar;fileinfo`] 
| Dirty block file entries&period;
|===


== Friends

[cols="1,4"]
|===
| Name| Description
| `xref:ChainstateManager.adoc[ChainstateManager]`
| Interface for managing multiple Chainstate objects, where each chainstate is associated with chainstate&ast; subdirectory in the data directory and contains a database of UTXOs existing at a different point in history&period; (See the Chainstate class for more information&period;)
| `xref:Chainstate.adoc[Chainstate]`
| Chainstate stores and provides an API to update our local knowledge of the current best chain&period;
|===

== Non-Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:CheckBlockDataAvailability.adoc[`&colon;&colon;CheckBlockDataAvailability`]
| Check that a block&apos;s data (and optionally undo data) is available on disk&period;
| xref:GetPruneHeight.adoc[`&colon;&colon;GetPruneHeight`]
| Return height of highest block that has been pruned, or std&colon;&colon;nullopt if no blocks have been pruned
| xref:blockToJSON.adoc[`&colon;&colon;blockToJSON`]
| Block description to JSON
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
