5 #ifndef BITCOIN_NODE_BLOCKSTORAGE_H 6 #define BITCOIN_NODE_BLOCKSTORAGE_H 33 #include <unordered_map> 45 class SignalInterrupt;
54 bool WriteBatchSync(
const std::vector<std::pair<int, const CBlockFileInfo*>>& fileInfo,
int nLastFile,
const std::vector<const CBlockIndex*>& blockinfo);
83 using BlockMap = std::unordered_map<uint256, CBlockIndex, BlockHasher>;
124 std::ostream&
operator<<(std::ostream& os,
const BlockfileCursor& cursor);
147 bool LoadBlockIndex(
const std::optional<uint256>& snapshot_blockhash)
151 [[nodiscard]]
bool FlushBlockFile(
int blockfile_num,
bool fFinalize,
bool finalize_undo);
154 [[nodiscard]]
bool FlushUndoFile(
int block_file,
bool finalize =
false);
182 std::set<int>& setFilesToPrune,
183 int nManualPruneHeight,
204 std::set<int>& setFilesToPrune,
230 const auto& normal = m_blockfile_cursors[BlockfileType::NORMAL].value_or(empty_cursor);
232 return std::max(normal.file_num, assumed.file_num);
257 std::unordered_map<std::string, PruneLockInfo> m_prune_locks
GUARDED_BY(::
cs_main);
309 bool LoadBlockIndexDB(const
std::optional<
uint256>& snapshot_blockhash)
398 uint32_t status_mask,
435 #endif // BITCOIN_NODE_BLOCKSTORAGE_H
std::optional< int > m_snapshot_height
The height of the base block of an assumeutxo snapshot, if one is in use.
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo *>> &fileInfo, int nLastFile, const std::vector< const CBlockIndex *> &blockinfo)
std::set< int > m_dirty_fileinfo
Dirty block file entries.
const util::SignalInterrupt & m_interrupt
void ImportBlocks(ChainstateManager &chainman, std::vector< fs::path > vImportFiles)
bool m_check_for_pruning
Global flag to indicate we should check to see if there are block/undo files that should be deleted...
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.
void CleanupBlockRevFiles() const
static constexpr auto PRUNE_TARGET_MANUAL
uint64_t CalculateCurrentUsage()
Calculate the amount of disk space the block & undo files currently use.
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
RecursiveMutex cs_LastBlockFile
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Access to the block database (blocks/index/)
bool CheckBlockDataAvailability(const CBlockIndex &upper_block LIFETIMEBOUND, const CBlockIndex &lower_block LIFETIMEBOUND) EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetFirstBlock(const CBlockIndex &upper_block LIFETIMEBOUND, uint32_t status_mask, const CBlockIndex *lower_block=nullptr) const EXCLUSIVE_LOCKS_REQUIRED(boo m_have_pruned)
Check if all blocks in the [upper_block, lower_block] range have data available.
const Consensus::Params & GetConsensus() const
bool WriteReindexing(bool fReindexing)
std::unordered_map< uint256, CBlockIndex, BlockHasher > BlockMap
AutoFile OpenUndoFile(const FlatFilePos &pos, bool fReadOnly=false) const
Open an undo file (rev?????.dat)
bool FlushBlockFile(int blockfile_num, bool fFinalize, bool finalize_undo)
Return false if block file or undo file flushing fails.
std::array< std::optional< BlockfileCursor >, BlockfileType::NUM_TYPES > m_blockfile_cursors GUARDED_BY(cs_LastBlockFile)
Since assumedvalid chainstates may be syncing a range of the chain that is very far away from the nor...
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight)
Store block on disk and update block file statistics.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos) const
Functions for disk access for blocks.
BlockManager(const util::SignalInterrupt &interrupt, Options opts)
bool ReadFlag(const std::string &name, bool &fValue)
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune) const
Actually unlink the specified files.
Non-refcounted RAII wrapper for FILE*.
Filesystem operations and types.
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.
bool ReadLastBlockFile(int &nFile)
Transaction validation functions.
bool UndoWriteToDisk(const CBlockUndo &blockundo, FlatFilePos &pos, const uint256 &hashBlock) const
void FindFilesToPrune(std::set< int > &setFilesToPrune, int last_prune, const Chainstate &chain, ChainstateManager &chainman)
Prune block and undo files (blk???.dat and rev???.dat) so that the disk space used is less than a use...
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 corr...
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, const Chainstate &chain, ChainstateManager &chainman)
int MaxBlockfileNum() const EXCLUSIVE_LOCKS_REQUIRED(cs_LastBlockFile)
static const unsigned int BLOCKFILE_CHUNK_SIZE
The pre-allocation chunk size for blk?????.dat files (since 0.8)
bool IsBlockPruned(const CBlockIndex &block) const EXCLUSIVE_LOCKS_REQUIRED(void UpdatePruneLock(const std::string &name, const PruneLockInfo &lock_info) EXCLUSIVE_LOCKS_REQUIRED(AutoFile OpenBlockFile(const FlatFilePos &pos, bool fReadOnly=false) const
Check whether the block associated with this index entry is pruned or not.
bool 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.
An options struct for BlockManager, more ergonomically referred to as BlockManager::Options due to th...
Chainstate stores and provides an API to update our local knowledge of the current best chain...
static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE
Size of header written by WriteBlockToDisk before a serialized CBlock.
bool FindUndoPos(BlockValidationState &state, int nFile, FlatFilePos &pos, unsigned int nAddSize)
CDBWrapper(const DBParams ¶ms)
const kernel::BlockManagerOpts m_opts
const FlatFileSeq m_block_file_seq
bool LoadingBlocks() const
BlockfileType BlockfileTypeForHeight(int height)
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex &index) const
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
const CChainParams & chainparams
std::atomic_bool m_blockfiles_indexed
Whether all blockfiles have been added to the block tree database.
bool LoadBlockIndex(const std::optional< uint256 > &snapshot_blockhash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the blocktree off disk and into memory.
Parameters that influence chain consensus.
bool WriteFlag(const std::string &name, bool fValue)
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool operator()(const CBlockIndex *pa, const CBlockIndex *pb) const
const CBlockIndex * GetLastCheckpoint(const CCheckpointData &data) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Returns last CBlockIndex* that is a checkpoint.
const FlatFileSeq m_undo_file_seq
const CChainParams & GetParams() const
const std::vector< std::byte > m_xor_key
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
#define EXCLUSIVE_LOCKS_REQUIRED(...)
bool WriteBlockToDisk(const CBlock &block, FlatFilePos &pos) const
Write a block to disk.
fs::path GetBlockPosFilename(const FlatFilePos &pos) const
Translation to a filesystem path.
bool FlushUndoFile(int block_file, bool finalize=false)
Return false if undo file flushing fails.
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
The block chain is a tree shaped structure starting with the genesis block at the root...
const CChainParams & Params()
Return the currently selected parameters.
Undo information for a CBlock.
bool ReadRawBlockFromDisk(std::vector< uint8_t > &block, const FlatFilePos &pos) const
static const unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?????.dat file (since 0.8)
CBlockIndex * InsertBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Create a new block index entry for a given block hash.
bool FlushChainstateBlockFile(int tip_height)
static const unsigned int UNDOFILE_CHUNK_SIZE
The pre-allocation chunk size for rev?????.dat files (since 0.8)
void ReadReindexing(bool &fReindexing)
const Consensus::Params & GetConsensus() const
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark one block file as pruned (modify associated database entries)
FlatFileSeq represents a sequence of numbered files storing raw data.
std::atomic< bool > m_importing
uint64_t GetPruneTarget() const
Attempt to stay below this number of bytes of block files.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
std::ostream & operator<<(std::ostream &os, const BlockfileType &type)
bool operator()(const CBlockIndex *pa, const CBlockIndex *pb) const
std::vector< CBlockFileInfo > m_blockfile_info