Bitcoin Core 22.99.0
P2P Digital Currency
blockstorage.h
Go to the documentation of this file.
1// Copyright (c) 2011-2021 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_NODE_BLOCKSTORAGE_H
6#define BITCOIN_NODE_BLOCKSTORAGE_H
7
8#include <fs.h>
9#include <protocol.h> // For CMessageHeader::MessageStartChars
10
11#include <atomic>
12#include <cstdint>
13#include <vector>
14
15class ArgsManager;
17class CBlock;
18class CBlockFileInfo;
19class CBlockIndex;
20class CBlockUndo;
21class CChain;
22class CChainParams;
24struct FlatFilePos;
25namespace Consensus {
26struct Params;
27}
28
29static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false};
30
32static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
34static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
36static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
37
38extern std::atomic_bool fImporting;
39extern std::atomic_bool fReindex;
42extern bool fHavePruned;
44extern bool fPruneMode;
46extern uint64_t nPruneTarget;
47
49bool IsBlockPruned(const CBlockIndex* pblockindex);
50
52
54FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false);
57
60
62uint64_t CalculateCurrentUsage();
63
67void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune);
68
70bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams);
71bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams);
72bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start);
73bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const CBlockIndex* pindex, const CMessageHeader::MessageStartChars& message_start);
74
75bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex);
76bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams);
77
78FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp);
79
80void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args);
81
82#endif // BITCOIN_NODE_BLOCKSTORAGE_H
static const unsigned int UNDOFILE_CHUNK_SIZE
The pre-allocation chunk size for rev?????.dat files (since 0.8)
Definition: blockstorage.h:34
std::atomic_bool fReindex
uint64_t nPruneTarget
Number of MiB of block files that we're trying to stay below.
static const unsigned int BLOCKFILE_CHUNK_SIZE
The pre-allocation chunk size for blk?????.dat files (since 0.8)
Definition: blockstorage.h:32
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex)
bool fHavePruned
Pruning-related variables and constants.
void ThreadImport(ChainstateManager &chainman, std::vector< fs::path > vImportFiles, const ArgsManager &args)
std::atomic_bool fImporting
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
uint64_t CalculateCurrentUsage()
Calculate the amount of disk space the block & undo files currently use.
void CleanupBlockRevFiles()
bool ReadRawBlockFromDisk(std::vector< uint8_t > &block, const FlatFilePos &pos, const CMessageHeader::MessageStartChars &message_start)
bool IsBlockPruned(const CBlockIndex *pblockindex)
Check whether the block associated with this index entry is pruned or not.
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
fs::path GetBlockPosFilename(const FlatFilePos &pos)
Translation to a filesystem path.
FILE * OpenBlockFile(const FlatFilePos &pos, bool fReadOnly=false)
Open a block file (blk?????.dat)
static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT
Definition: blockstorage.h:29
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex *pindex, const CChainParams &chainparams)
bool fPruneMode
True if we're running in -prune mode.
static const unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?????.dat file (since 0.8)
Definition: blockstorage.h:36
FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, CChain &active_chain, const CChainParams &chainparams, const FlatFilePos *dbp)
Store block on disk.
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune)
Actually unlink the specified files.
const CChainParams & Params()
Return the currently selected parameters.
Definition: block.h:63
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:146
Undo information for a CBlock.
Definition: undo.h:64
An in-memory indexed chain of blocks.
Definition: chain.h:410
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:70
unsigned char MessageStartChars[MESSAGE_START_SIZE]
Definition: protocol.h:40
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:847
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:34
unsigned int nHeight
Transaction validation functions.
Definition: params.h:12
Parameters that influence chain consensus.
Definition: params.h:70