Bitcoin Core  21.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 <cstdint>
9 #include <vector>
10 
11 #include <fs.h>
12 #include <protocol.h> // For CMessageHeader::MessageStartChars
13 
14 class ArgsManager;
16 class CBlock;
17 class CBlockFileInfo;
18 class CBlockIndex;
19 class CBlockUndo;
20 class CChain;
21 class CChainParams;
22 class ChainstateManager;
23 struct FlatFilePos;
24 namespace Consensus {
25 struct Params;
26 }
27 
28 static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false};
29 
31 static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
33 static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
35 static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
36 
37 extern std::atomic_bool fImporting;
38 extern std::atomic_bool fReindex;
41 extern bool fHavePruned;
43 extern bool fPruneMode;
45 extern uint64_t nPruneTarget;
46 
48 bool IsBlockPruned(const CBlockIndex* pblockindex);
49 
51 
53 FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly = false);
55 fs::path GetBlockPosFilename(const FlatFilePos& pos);
56 
59 
61 uint64_t CalculateCurrentUsage();
62 
66 void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune);
67 
69 bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams);
70 bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams);
71 bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start);
72 bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const CBlockIndex* pindex, const CMessageHeader::MessageStartChars& message_start);
73 
74 bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex);
75 bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams);
76 
77 FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp);
78 
79 void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args);
80 
81 #endif // BITCOIN_NODE_BLOCKSTORAGE_H
fImporting
std::atomic_bool fImporting
CleanupBlockRevFiles
void CleanupBlockRevFiles()
Definition: blockstorage.cpp:60
fs.h
nHeight
unsigned int nHeight
Definition: mempool_eviction.cpp:14
BlockValidationState
Definition: validation.h:140
MAX_BLOCKFILE_SIZE
static const unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?????.dat file (since 0.8)
Definition: blockstorage.h:35
ReadBlockFromDisk
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
Definition: blockstorage.cpp:366
CChainParams
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:67
UnlinkPrunedFiles
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune)
Actually unlink the specified files.
Definition: blockstorage.cpp:200
DEFAULT_STOPAFTERBLOCKIMPORT
static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT
Definition: blockstorage.h:28
UNDOFILE_CHUNK_SIZE
static const unsigned int UNDOFILE_CHUNK_SIZE
The pre-allocation chunk size for rev?????.dat files (since 0.8)
Definition: blockstorage.h:33
fReindex
std::atomic_bool fReindex
GetBlockFileInfo
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
Definition: blockstorage.cpp:101
Consensus::Params
Parameters that influence chain consensus.
Definition: params.h:56
Consensus
Transaction validation functions.
Definition: params.h:12
FlatFilePos
Definition: flatfile.h:14
UndoReadFromDisk
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex)
Definition: blockstorage.cpp:137
ReadRawBlockFromDisk
bool ReadRawBlockFromDisk(std::vector< uint8_t > &block, const FlatFilePos &pos, const CMessageHeader::MessageStartChars &message_start)
Definition: blockstorage.cpp:414
ThreadImport
void ThreadImport(ChainstateManager &chainman, std::vector< fs::path > vImportFiles, const ArgsManager &args)
Definition: blockstorage.cpp:495
nPruneTarget
uint64_t nPruneTarget
Number of MiB of block files that we're trying to stay below.
Definition: blockstorage.cpp:26
ChainstateManager
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:792
CBlock
Definition: block.h:62
IsBlockPruned
bool IsBlockPruned(const CBlockIndex *pblockindex)
Check whether the block associated with this index entry is pruned or not.
Definition: blockstorage.cpp:49
CChain
An in-memory indexed chain of blocks.
Definition: chain.h:392
ArgsManager
Definition: system.h:161
fHavePruned
bool fHavePruned
Pruning-related variables and constants.
Definition: blockstorage.cpp:24
BLOCKFILE_CHUNK_SIZE
static const unsigned int BLOCKFILE_CHUNK_SIZE
The pre-allocation chunk size for blk?????.dat files (since 0.8)
Definition: blockstorage.h:31
CBlockFileInfo
Definition: chain.h:40
OpenBlockFile
FILE * OpenBlockFile(const FlatFilePos &pos, bool fReadOnly=false)
Open a block file (blk?????.dat)
Definition: blockstorage.cpp:220
CalculateCurrentUsage
uint64_t CalculateCurrentUsage()
Calculate the amount of disk space the block & undo files currently use.
Definition: blockstorage.cpp:189
CMessageHeader::MessageStartChars
unsigned char MessageStartChars[MESSAGE_START_SIZE]
Definition: protocol.h:39
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:538
SaveBlockToDisk
FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, CChain &active_chain, const CChainParams &chainparams, const FlatFilePos *dbp)
Store block on disk.
Definition: blockstorage.cpp:461
GetBlockPosFilename
fs::path GetBlockPosFilename(const FlatFilePos &pos)
Translation to a filesystem path.
Definition: blockstorage.cpp:231
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:137
fPruneMode
bool fPruneMode
True if we're running in -prune mode.
Definition: blockstorage.cpp:25
WriteUndoDataForBlock
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex *pindex, const CChainParams &chainparams)
Definition: blockstorage.cpp:337
CBlockUndo
Undo information for a CBlock.
Definition: undo.h:63