41#include <validation.h>
53#include <system_error>
54#include <unordered_map>
91void BlockTreeDB::WriteBatchSync(
const std::vector<std::pair<int, const CBlockFileInfo*>>& fileInfo,
int nLastFile,
const std::vector<const CBlockIndex*>& blockinfo)
94 for (
const auto& [file, info] : fileInfo) {
115 fValue = ch == uint8_t{
'1'};
122 std::unique_ptr<CDBIterator> pcursor(
NewIterator());
126 while (pcursor->Valid()) {
127 if (interrupt)
return false;
128 std::pair<uint8_t, uint256> key;
131 if (pcursor->GetValue(diskindex)) {
136 pindexNew->nFile = diskindex.nFile;
137 pindexNew->nDataPos = diskindex.nDataPos;
138 pindexNew->nUndoPos = diskindex.nUndoPos;
144 pindexNew->nStatus = diskindex.nStatus;
145 pindexNew->
nTx = diskindex.
nTx;
148 LogError(
"%s: CheckProofOfWork failed: %s\n", __func__, pindexNew->
ToString());
154 LogError(
"%s: failed to read value\n", __func__);
186 if (pa < pb)
return false;
187 if (pa > pb)
return true;
198std::vector<CBlockIndex*> BlockManager::GetAllBlockIndices()
201 std::vector<CBlockIndex*> rv;
202 rv.reserve(m_block_index.size());
203 for (
auto& [
_, block_index] : m_block_index) {
204 rv.push_back(&block_index);
212 BlockMap::iterator it = m_block_index.find(hash);
213 return it == m_block_index.end() ? nullptr : &it->second;
219 BlockMap::const_iterator it = m_block_index.find(hash);
220 return it == m_block_index.end() ? nullptr : &it->second;
227 auto [mi, inserted] = m_block_index.try_emplace(block.
GetHash(), block);
239 BlockMap::iterator miPrev = m_block_index.find(block.
hashPrevBlock);
240 if (miPrev != m_block_index.end()) {
241 pindexNew->
pprev = &(*miPrev).second;
249 best_header = pindexNew;
262 for (
auto& entry : m_block_index) {
264 if (pindex->nFile == fileNumber) {
265 pindex->nStatus &= ~BLOCK_HAVE_DATA;
266 pindex->nStatus &= ~BLOCK_HAVE_UNDO;
268 pindex->nDataPos = 0;
269 pindex->nUndoPos = 0;
277 while (range.first != range.second) {
278 std::multimap<CBlockIndex*, CBlockIndex*>::iterator _it = range.first;
280 if (_it->second == pindex) {
292 std::set<int>& setFilesToPrune,
293 int nManualPruneHeight,
303 const auto [min_block_to_prune, last_block_can_prune] = chain.GetPruneRange(nManualPruneHeight);
306 for (
int fileNumber = 0; fileNumber < this->
MaxBlockfileNum(); fileNumber++) {
308 if (fileinfo.nSize == 0 || fileinfo.nHeightLast > (
unsigned)last_block_can_prune || fileinfo.nHeightFirst < (unsigned)min_block_to_prune) {
313 setFilesToPrune.insert(fileNumber);
316 LogInfo(
"[%s] Prune (Manual): prune_height=%d removed %d blk/rev pairs",
317 chain.GetRole(), last_block_can_prune,
count);
321 std::set<int>& setFilesToPrune,
335 const auto target = std::max(
337 const uint64_t target_sync_height = chainman.m_best_header->nHeight;
346 const auto [min_block_to_prune, last_block_can_prune] = chain.GetPruneRange(last_prune);
353 uint64_t nBytesToPrune;
356 if (nCurrentUsage + nBuffer >= target) {
364 static constexpr uint64_t average_block_size = 1000000;
365 const uint64_t remaining_blocks = target_sync_height - chain_tip_height;
366 nBuffer += average_block_size * remaining_blocks;
369 for (
int fileNumber = 0; fileNumber < this->
MaxBlockfileNum(); fileNumber++) {
371 nBytesToPrune = fileinfo.nSize + fileinfo.nUndoSize;
373 if (fileinfo.nSize == 0) {
377 if (nCurrentUsage + nBuffer < target) {
383 if (fileinfo.nHeightLast > (
unsigned)last_block_can_prune || fileinfo.nHeightFirst < (unsigned)min_block_to_prune) {
389 setFilesToPrune.insert(fileNumber);
390 nCurrentUsage -= nBytesToPrune;
395 LogDebug(
BCLog::PRUNE,
"[%s] target=%dMiB actual=%dMiB diff=%dMiB min_height=%d max_prune_height=%d removed %d blk/rev pairs\n",
396 chain.GetRole(), target / 1024 / 1024, nCurrentUsage / 1024 / 1024,
397 (int64_t(target) - int64_t(nCurrentUsage)) / 1024 / 1024,
398 min_block_to_prune, last_block_can_prune,
count);
401void BlockManager::UpdatePruneLock(
const std::string&
name,
const PruneLockInfo& lock_info) {
403 m_prune_locks[
name] = lock_info;
414 const auto [mi, inserted]{m_block_index.try_emplace(hash)};
424 if (!m_block_tree_db->LoadBlockIndexGuts(
429 if (snapshot_blockhash) {
431 if (!maybe_au_data) {
443 LogInfo(
"[snapshot] set m_chain_tx_count=%d for %s", au_data.
m_chain_tx_count, snapshot_blockhash->ToString());
454 std::vector<CBlockIndex*> vSortedByHeight{GetAllBlockIndices()};
455 std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
461 if (previous_index && pindex->nHeight > previous_index->nHeight + 1) {
462 LogError(
"%s: block index is non-contiguous, index of height %d missing\n", __func__, previous_index->nHeight + 1);
465 previous_index = pindex;
466 pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) +
GetBlockProof(*pindex);
467 pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime);
473 if (pindex->nTx > 0) {
476 pindex->GetBlockHash() == *snapshot_blockhash) {
478 Assert(pindex->m_chain_tx_count > 0);
479 }
else if (pindex->pprev->m_chain_tx_count > 0) {
480 pindex->m_chain_tx_count = pindex->pprev->m_chain_tx_count + pindex->nTx;
482 pindex->m_chain_tx_count = 0;
486 pindex->m_chain_tx_count = pindex->nTx;
501void BlockManager::WriteBlockIndexDB()
504 std::vector<std::pair<int, const CBlockFileInfo*>> vFiles;
510 std::vector<const CBlockIndex*> vBlocks;
513 vBlocks.push_back(*it);
517 m_block_tree_db->WriteBatchSync(vFiles, max_blockfile, vBlocks);
520bool BlockManager::LoadBlockIndexDB(
const std::optional<uint256>& snapshot_blockhash)
525 int max_blockfile_num{0};
528 m_block_tree_db->ReadLastBlockFile(max_blockfile_num);
530 LogInfo(
"Loading block index db: last block file = %i", max_blockfile_num);
531 for (
int nFile = 0; nFile <= max_blockfile_num; nFile++) {
535 for (
int nFile = max_blockfile_num + 1;
true; nFile++) {
537 if (m_block_tree_db->ReadBlockFileInfo(nFile, info)) {
545 LogInfo(
"Checking all blk files are present...");
546 std::set<int> setBlkDataFiles;
547 for (
const auto& [
_, block_index] : m_block_index) {
549 setBlkDataFiles.insert(block_index.nFile);
552 for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) {
569 m_block_tree_db->ReadFlag(
"prunedblockfiles",
m_have_pruned);
571 LogInfo(
"Loading block index db: Block files have previously been pruned");
575 bool fReindexing =
false;
576 m_block_tree_db->ReadReindexing(fReindexing);
582void BlockManager::ScanAndUnlinkAlreadyPrunedFiles()
590 std::set<int> block_files_to_prune;
591 for (
int file_number = 0; file_number < max_blockfile; file_number++) {
593 block_files_to_prune.insert(file_number);
600bool BlockManager::IsBlockPruned(
const CBlockIndex& block)
const
610 assert((last_block->nStatus & status_mask) == status_mask);
611 while (last_block->
pprev && ((last_block->
pprev->nStatus & status_mask) == status_mask)) {
614 if (last_block == lower_block)
return *lower_block;
619 last_block = last_block->
pprev;
621 assert(last_block !=
nullptr);
628 return &GetFirstBlock(upper_block,
BLOCK_HAVE_DATA, &lower_block) == &lower_block;
639 std::map<std::string, fs::path> mapBlockFiles;
644 LogInfo(
"Removing unusable blk?????.dat and rev?????.dat files for -reindex with -prune");
645 for (fs::directory_iterator it(
m_opts.
blocks_dir); it != fs::directory_iterator(); it++) {
647 if (fs::is_regular_file(*it) &&
648 path.length() == 12 &&
649 path.ends_with(
".dat"))
651 if (path.starts_with(
"blk")) {
652 mapBlockFiles[path.substr(3, 5)] = it->path();
653 }
else if (path.starts_with(
"rev")) {
663 int nContigCounter = 0;
664 for (
const std::pair<const std::string, fs::path>& item : mapBlockFiles) {
665 if (LocaleIndependentAtoi<int>(item.first) == nContigCounter) {
687 LogError(
"OpenUndoFile failed for %s while reading block undo", pos.ToString());
697 verifier >> blockundo;
700 filein >> hashChecksum;
703 if (hashChecksum != verifier.GetHash()) {
704 LogError(
"Checksum mismatch at %s while reading block undo", pos.ToString());
707 }
catch (
const std::exception& e) {
708 LogError(
"Deserialize or I/O error - %s at %s while reading block undo", e.what(), pos.ToString());
746 if (!fFinalize || finalize_undo) {
782 retval += file.nSize + file.nUndoSize;
790 for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
794 if (removed_blockfile || removed_undofile) {
822 if (!m_blockfile_cursors[chain_type]) {
826 m_blockfile_cursors[chain_type] = new_cursor;
829 const int last_blockfile = m_blockfile_cursors[chain_type]->file_num;
831 int nFile = last_blockfile;
836 bool finalize_undo =
false;
841 max_blockfile_size = 0x10000;
842 if (nAddSize >= max_blockfile_size) {
844 max_blockfile_size = nAddSize + 1;
847 assert(nAddSize < max_blockfile_size);
854 Assert(m_blockfile_cursors[chain_type])->undo_height);
869 if (nFile != last_blockfile) {
882 "Failed to flush previous block file %05i (finalize=1, finalize_undo=%i) before opening new block file %05i\n",
883 last_blockfile, finalize_undo, nFile);
912 auto& cursor{m_blockfile_cursors[chain_type]};
913 if (!cursor || cursor->file_num < pos.
nFile) {
919 const int nFile = pos.
nFile;
959 const auto blockundo_size{
static_cast<uint32_t
>(
GetSerializeSize(blockundo))};
961 LogError(
"FindUndoPos failed for %s while writing block undo", pos.
ToString());
968 LogError(
"OpenUndoFile failed for %s while writing block undo", pos.
ToString());
982 fileout << blockundo << hasher.GetHash();
988 if (file.fclose() != 0) {
1007 }
else if (pos.
nFile == cursor.file_num && block.
nHeight > cursor.undo_height) {
1008 cursor.undo_height = block.
nHeight;
1011 block.nUndoPos = pos.
nPos;
1032 }
catch (
const std::exception& e) {
1033 LogError(
"Deserialize or I/O error - %s at %s while reading block", e.what(), pos.
ToString());
1037 const auto block_hash{block.
GetHash()};
1041 LogError(
"Errors in block header at %s while reading block", pos.
ToString());
1047 LogError(
"Errors in block solution at %s while reading block", pos.
ToString());
1051 if (expected_hash && block_hash != *expected_hash) {
1052 LogError(
"GetHash() doesn't match index at %s while reading block (%s != %s)",
1053 pos.
ToString(), block_hash.ToString(), expected_hash->ToString());
1072 LogError(
"Failed for %s while reading raw block storage header", pos.
ToString());
1076 if (filein.IsNull()) {
1077 LogError(
"OpenBlockFile failed for %s while reading raw block", pos.
ToString());
1083 unsigned int blk_size;
1085 filein >> blk_start >> blk_size;
1087 if (blk_start !=
GetParams().MessageStart()) {
1088 LogError(
"Block magic mismatch for %s: %s versus expected %s while reading raw block",
1094 LogError(
"Block data is larger than maximum deserialization size for %s: %s versus %s while reading raw block",
1100 const auto [offset, size]{*block_part};
1104 filein.seek(offset, SEEK_CUR);
1108 std::vector<std::byte>
data(blk_size);
1111 }
catch (
const std::exception& e) {
1112 LogError(
"Read from block file failed: %s for %s while reading raw block", e.what(), pos.
ToString());
1122 LogError(
"FindNextBlockPos failed for %s while writing block", pos.
ToString());
1126 if (file.IsNull()) {
1127 LogError(
"OpenBlockFile failed for %s while writing block", pos.
ToString());
1141 if (file.fclose() != 0) {
1154 std::array<std::byte, Obfuscation::KEY_SIZE> obfuscation{};
1159 bool first_run =
true;
1160 for (
const auto& entry : fs::directory_iterator(opts.
blocks_dir)) {
1162 if (!entry.is_regular_file() || !path.starts_with(
'.')) {
1168 if (opts.
use_xor && first_run) {
1174 const fs::path xor_key_path{opts.
blocks_dir /
"xor.dat"};
1178 xor_key_file >> obfuscation;
1188 xor_key_file << obfuscation;
1189 if (xor_key_file.fclose() != 0) {
1190 throw std::runtime_error{
strprintf(
"Error closing XOR key file %s: %s",
1196 if (!opts.
use_xor && obfuscation !=
decltype(obfuscation){}) {
1197 throw std::runtime_error{
1198 strprintf(
"The blocksdir XOR-key can not be disabled when a random key was already stored! "
1199 "Stored key: '%s', stored path: '%s'.",
1208 : m_prune_mode{opts.prune_target > 0},
1210 m_opts{
std::move(opts)},
1213 m_interrupt{interrupt}
1218 m_block_tree_db->WriteReindexing(
true);
1257 std::multimap<uint256, FlatFilePos> blocks_with_unknown_parent;
1259 for (
int nFile{0}; nFile < total_files; ++nFile) {
1262 if (file.IsNull()) {
1265 LogInfo(
"Reindexing block file blk%05u.dat (%d%% complete)...", (
unsigned int)nFile, nFile * 100 / total_files);
1268 LogInfo(
"Interrupt requested. Exit reindexing.");
1274 LogInfo(
"Reindexing finished");
1280 for (
const fs::path& path : import_paths) {
1282 if (!file.IsNull()) {
1286 LogInfo(
"Interrupt requested. Exit block importing.");
1295 if (
auto result = chainman.ActivateBestChains(); !result) {
1305 default: os.setstate(std::ios_base::failbit);
void CheckBlockDataAvailability(BlockManager &blockman, const CBlockIndex &blockindex, bool check_for_undo)
arith_uint256 GetBlockProof(const CBlockIndex &block)
@ BLOCK_VALID_TREE
All parent headers found, difficulty matches, timestamp >= median previous.
@ BLOCK_HAVE_UNDO
undo data available in rev*.dat
@ BLOCK_HAVE_DATA
full block available in blk*.dat
@ BLOCK_FAILED_CHILD
descends from failed block
static constexpr int32_t SEQ_ID_INIT_FROM_DISK
#define Assert(val)
Identity function.
Non-refcounted RAII wrapper for FILE*.
Wrapper that buffers reads from an underlying stream.
Wrapper that buffers writes to an underlying stream.
The block chain is a tree shaped structure starting with the genesis block at the root,...
std::string ToString() const
CBlockIndex * pprev
pointer to the index of the predecessor of this block
uint64_t m_chain_tx_count
(memory only) Number of transactions in the chain up to and including this block.
void BuildSkip()
Build the skiplist pointer for this entry.
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
unsigned int nTimeMax
(memory only) Maximum nTime in the chain up to and including this block.
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
uint256 GetBlockHash() const
FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(
bool RaiseValidity(enum BlockStatus nUpTo) EXCLUSIVE_LOCKS_REQUIRED(
Raise the validity level of this block index entry.
unsigned int nTx
Number of transactions in this block.
int32_t nVersion
block header
int nHeight
height of the entry in the chain. The genesis block has height 0
FlatFilePos GetBlockPos() const EXCLUSIVE_LOCKS_REQUIRED(
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
Undo information for a CBlock.
int Height() const
Return the maximal height in the chain.
const MessageStartChars & MessageStart() const
std::optional< AssumeutxoData > AssumeutxoForBlockhash(const uint256 &blockhash) const
uint64_t PruneAfterHeight() const
Batch of changes queued to be written to a CDBWrapper.
void Write(const K &key, const V &value)
bool Read(const K &key, V &value) const
CDBIterator * NewIterator()
bool Exists(const K &key) const
void Erase(const K &key, bool fSync=false)
void WriteBatch(CDBBatch &batch, bool fSync=false)
void Write(const K &key, const V &value, bool fSync=false)
Used to marshal pointers into hashes for db storage.
uint256 ConstructBlockHash() const
Chainstate stores and provides an API to update our local knowledge of the current best chain.
CChain m_chain
The current chain of blockheaders we consult and build on.
bool LoadGenesisBlock()
Ensures we have a genesis block in the block tree, possibly writing one to disk.
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
Chainstate * HistoricalChainstate() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Return historical chainstate targeting a specific block, if any.
kernel::Notifications & GetNotifications() const
bool IsInitialBlockDownload() const
Check whether we are doing an initial block download (synchronizing from disk or network)
Chainstate & ActiveChainstate() const
Alternatives to CurrentChainstate() used by older code to query latest chainstate information without...
const util::SignalInterrupt & m_interrupt
void LoadExternalBlockFile(AutoFile &file_in, FlatFilePos *dbp=nullptr, std::multimap< uint256, FlatFilePos > *blocks_with_unknown_parent=nullptr)
Import blocks from an external file.
const CChainParams & GetParams() const
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
void fillrand(std::span< std::byte > output) noexcept
Fill a byte span with random bytes.
FlatFileSeq represents a sequence of numbered files storing raw data.
FILE * Open(const FlatFilePos &pos, bool read_only=false) const
Open a handle to the file at the given position.
fs::path FileName(const FlatFilePos &pos) const
Get the name of the file at the given position.
bool Flush(const FlatFilePos &pos, bool finalize=false) const
Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final.
size_t Allocate(const FlatFilePos &pos, size_t add_size, bool &out_of_space) const
Allocate additional space in a file after the given starting position.
Reads data from an underlying stream, while hashing the read data.
A writer stream (for serialization) that computes a 256-bit hash.
Minimal stream for reading from an existing byte array by std::span.
constexpr bool IsNull() const
void WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * > > &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
bool ReadLastBlockFile(int &nFile)
void WriteReindexing(bool fReindexing)
bool ReadFlag(const std::string &name, bool &fValue)
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
void ReadReindexing(bool &fReindexing)
void WriteFlag(const std::string &name, bool fValue)
uint32_t nSize
number of used bytes of block file
std::string ToString() const
uint64_t nTimeFirst
earliest time of block in file
uint64_t nTimeLast
latest time of block in file
uint32_t nHeightFirst
lowest height of block in file
uint32_t nBlocks
number of blocks stored in file
uint32_t nHeightLast
highest height of block in file
virtual void fatalError(const bilingual_str &message)
The fatal error notification is sent to notify the user when an error occurs in kernel code that can'...
virtual void flushError(const bilingual_str &message)
The flush error notification is sent to notify the user that an error occurred while flushing block d...
const kernel::BlockManagerOpts m_opts
std::set< int > m_dirty_fileinfo
Dirty block file entries.
const FlatFileSeq m_undo_file_seq
RecursiveMutex cs_LastBlockFile
const CChainParams & GetParams() const
bool FlushChainstateBlockFile(int tip_height)
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...
void UpdateBlockInfo(const CBlock &block, unsigned int nHeight, const FlatFilePos &pos)
Update blockfile info while processing a block during reindex.
const Obfuscation m_obfuscation
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool ReadBlockUndo(CBlockUndo &blockundo, const CBlockIndex &index) const
void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark one block file as pruned (modify associated database entries)
BlockfileType BlockfileTypeForHeight(int height)
std::atomic_bool m_blockfiles_indexed
Whether all blockfiles have been added to the block tree database.
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.
const Consensus::Params & GetConsensus() const
BlockManager(const util::SignalInterrupt &interrupt, Options opts)
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
fs::path GetBlockPosFilename(const FlatFilePos &pos) const
Translation to a filesystem path.
bool FlushBlockFile(int blockfile_num, bool fFinalize, bool finalize_undo)
Return false if block file or undo file flushing fails.
uint64_t GetPruneTarget() const
Attempt to stay below this number of bytes of block files.
int MaxBlockfileNum() const EXCLUSIVE_LOCKS_REQUIRED(cs_LastBlockFile)
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune) const
Actually unlink the specified files.
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.
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...
bool FlushUndoFile(int block_file, bool finalize=false)
Return false if undo file flushing fails.
uint64_t CalculateCurrentUsage()
Calculate the amount of disk space the block & undo files currently use.
const util::SignalInterrupt & m_interrupt
ReadRawBlockResult ReadRawBlock(const FlatFilePos &pos, std::optional< std::pair< size_t, size_t > > block_part=std::nullopt) const
const FlatFileSeq m_block_file_seq
CBlockIndex * InsertBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Create a new block index entry for a given block hash.
bool ReadBlock(CBlock &block, const FlatFilePos &pos, const std::optional< uint256 > &expected_hash) const
Functions for disk access for blocks.
bool m_check_for_pruning
Global flag to indicate we should check to see if there are block/undo files that should be deleted.
bool FindUndoPos(BlockValidationState &state, int nFile, FlatFilePos &pos, unsigned int nAddSize)
bool IsPruneMode() const
Whether running in -prune mode.
bool CheckBlockDataAvailability(const CBlockIndex &upper_block, const CBlockIndex &lower_block) 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)
Check if all blocks in the [upper_block, lower_block] range have data available.
void CleanupBlockRevFiles() const
std::atomic< bool > m_importing
bool WriteBlockUndo(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePos WriteBlock(const CBlock &block, int nHeight)
Store block on disk and update block file statistics.
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) const
Check whether the block associated with this index entry is pruned or not.
std::vector< CBlockFileInfo > m_blockfile_info
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, const Chainstate &chain)
bool LoadBlockIndex(const std::optional< uint256 > &snapshot_blockhash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the blocktree off disk and into memory.
AutoFile OpenUndoFile(const FlatFilePos &pos, bool fReadOnly=false) const
Open an undo file (rev?????.dat)
std::optional< int > m_snapshot_height
The height of the base block of an assumeutxo snapshot, if one is in use.
ImportingNow(std::atomic< bool > &importing)
std::atomic< bool > & m_importing
The util::Expected class provides a standard way for low-level functions to return either error value...
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
The util::Unexpected class represents an unexpected value stored in util::Expected.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to a byte string.
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
is a home for simple enum and struct type definitions that can be used internally by functions in the...
#define LogDebug(category,...)
std::array< uint8_t, 4 > MessageStartChars
FILE * fopen(const fs::path &p, const char *mode)
static constexpr uint8_t DB_REINDEX_FLAG
static constexpr uint8_t DB_FLAG
static constexpr uint8_t DB_BLOCK_INDEX
static constexpr uint8_t DB_LAST_BLOCK
static constexpr uint8_t DB_BLOCK_FILES
static const unsigned int UNDOFILE_CHUNK_SIZE
The pre-allocation chunk size for rev?????.dat files (since 0.8)
static auto InitBlocksdirXorKey(const BlockManager::Options &opts)
static const unsigned int BLOCKFILE_CHUNK_SIZE
The pre-allocation chunk size for blk?????.dat files (since 0.8)
static constexpr uint32_t STORAGE_HEADER_BYTES
Size of header written by WriteBlock before a serialized CBlock (8 bytes)
std::ostream & operator<<(std::ostream &os, const BlockfileType &type)
static constexpr uint32_t UNDO_DATA_DISK_OVERHEAD
Total overhead when writing undo data: header (8 bytes) plus checksum (32 bytes)
static const unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?????.dat file (since 0.8)
void ImportBlocks(ChainstateManager &chainman, std::span< const fs::path > import_paths)
bilingual_str ErrorString(const Result< T > &result)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
T SaturatingAdd(const T i, const T j) noexcept
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
static constexpr TransactionSerParams TX_WITH_WITNESS
static constexpr uint64_t MAX_SIZE
The maximum size of a serialized object in bytes or number of elements (for eg vectors) when the size...
uint64_t GetSerializeSize(const T &t)
bool CheckSignetBlockSolution(const CBlock &block, const Consensus::Params &consensusParams)
Extract signature and check whether a block has a valid solution.
Holds configuration for use during UTXO snapshot load and validation.
uint64_t m_chain_tx_count
Used to populate the m_chain_tx_count value, which is used during BlockManager::LoadBlockIndex().
Parameters that influence chain consensus.
bool wipe_data
If true, remove all existing data.
std::string ToString() const
An options struct for BlockManager, more ergonomically referred to as BlockManager::Options due to th...
Notifications & notifications
const fs::path blocks_dir
DBParams block_tree_db_params
bool operator()(const CBlockIndex *pa, const CBlockIndex *pb) const
bool operator()(const CBlockIndex *pa, const CBlockIndex *pb) const
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
std::string SysErrorString(int err)
Return system error string from errno value.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
consteval auto _(util::TranslatedLiteral str)
std::string FormatISO8601Date(int64_t nTime)
bool FatalError(Notifications ¬ifications, BlockValidationState &state, const bilingual_str &message)
static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES