34 std::vector<CBlockIndex *> BlockManager::GetAllBlockIndices() {
36 std::vector<CBlockIndex *> rv;
37 rv.reserve(m_block_index.size());
38 for (
auto &[
_, block_index] : m_block_index) {
39 rv.push_back(&block_index);
46 BlockMap::iterator it = m_block_index.find(hash);
47 return it == m_block_index.end() ? nullptr : &it->second;
52 BlockMap::const_iterator it = m_block_index.find(hash);
53 return it == m_block_index.end() ? nullptr : &it->second;
60 const auto [mi, inserted] =
61 m_block_index.try_emplace(block.
GetHash(), block);
73 BlockMap::iterator miPrev = m_block_index.find(block.
hashPrevBlock);
74 if (miPrev != m_block_index.end()) {
75 pindexNew->
pprev = &(*miPrev).second;
88 if (best_header ==
nullptr ||
90 best_header = pindexNew;
101 for (
auto &entry : m_block_index) {
103 if (pindex->nFile == fileNumber) {
104 pindex->nStatus = pindex->nStatus.withData(
false).withUndo(
false);
106 pindex->nDataPos = 0;
107 pindex->nUndoPos = 0;
115 while (range.first != range.second) {
116 std::multimap<CBlockIndex *, CBlockIndex *>::iterator _it =
119 if (_it->second == pindex) {
131 int nManualPruneHeight,
132 int chain_tip_height) {
136 if (chain_tip_height < 0) {
142 unsigned int nLastBlockWeCanPrune{std::min(
151 setFilesToPrune.insert(fileNumber);
154 LogPrintf(
"Prune (Manual): prune_height=%d removed %d blk/rev pairs\n",
155 nLastBlockWeCanPrune,
count);
159 uint64_t nPruneAfterHeight,
160 int chain_tip_height,
int prune_height,
166 if (uint64_t(chain_tip_height) <= nPruneAfterHeight) {
170 unsigned int nLastBlockWeCanPrune = std::min(
177 uint64_t nBytesToPrune;
207 nLastBlockWeCanPrune) {
213 setFilesToPrune.insert(fileNumber);
214 nCurrentUsage -= nBytesToPrune;
220 "Prune: target=%dMiB actual=%dMiB diff=%dMiB "
221 "max_prune_height=%d removed %d blk/rev pairs\n",
223 (int64_t(
GetPruneTarget()) - int64_t(nCurrentUsage)) / 1024 / 1024,
224 nLastBlockWeCanPrune,
count);
234 const auto [mi, inserted] = m_block_index.try_emplace(hash);
244 if (!m_block_tree_db->LoadBlockIndexGuts(
253 std::vector<CBlockIndex *> vSortedByHeight{GetAllBlockIndices()};
254 std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
261 pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) +
264 (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime)
271 if (pindex->nTx > 0) {
272 if (!pindex->UpdateChainStats() && pindex->pprev) {
277 if (!pindex->nStatus.hasFailed() && pindex->pprev &&
278 pindex->pprev->nStatus.hasFailed()) {
279 pindex->nStatus = pindex->nStatus.withFailedParent();
291 bool BlockManager::WriteBlockIndexDB() {
292 std::vector<std::pair<int, const CBlockFileInfo *>> vFiles;
300 std::vector<const CBlockIndex *> vBlocks;
303 vBlocks.push_back(cbi);
314 bool BlockManager::LoadBlockIndexDB() {
326 LogPrintf(
"%s: last block file info: %s\n", __func__,
330 if (m_block_tree_db->ReadBlockFileInfo(nFile, info)) {
338 LogPrintf(
"Checking all blk files are present...\n");
339 std::set<int> setBlkDataFiles;
340 for (
const auto &[
_, block_index] : m_block_index) {
341 if (block_index.nStatus.hasData()) {
342 setBlkDataFiles.insert(block_index.nFile);
346 for (
const int i : setBlkDataFiles) {
355 m_block_tree_db->ReadFlag(
"prunedblockfiles",
m_have_pruned);
358 "LoadBlockIndexDB(): Block files have previously been pruned\n");
362 if (m_block_tree_db->IsReindexing()) {
373 for (
const MapCheckpoints::value_type &i :
reverse_iterate(checkpoints)) {
384 bool BlockManager::IsBlockPruned(
const CBlockIndex *pblockindex) {
387 pblockindex->
nTx > 0);
394 while (last_block->
pprev && (last_block->
pprev->nStatus.hasData())) {
395 last_block = last_block->
pprev;
408 std::map<std::string, fs::path> mapBlockFiles;
413 LogPrintf(
"Removing unusable blk?????.dat and rev?????.dat files for "
414 "-reindex with -prune\n");
417 if (fs::is_regular_file(file) && path.length() == 12 &&
418 path.substr(8, 4) ==
".dat") {
419 if (path.substr(0, 3) ==
"blk") {
420 mapBlockFiles[path.substr(3, 5)] = file.path();
421 }
else if (path.substr(0, 3) ==
"rev") {
431 int contiguousCounter = 0;
432 for (
const auto &item : mapBlockFiles) {
433 if (
atoi(item.first) == contiguousCounter) {
453 return error(
"%s: OpenUndoFile failed", __func__);
458 fileout << messageStart << nSize;
461 long fileOutPos = ftell(fileout.
Get());
462 if (fileOutPos < 0) {
463 return error(
"%s: ftell failed", __func__);
465 pos.
nPos = (
unsigned int)fileOutPos;
466 fileout << blockundo;
481 return error(
"%s: no undo data available", __func__);
487 return error(
"%s: OpenUndoFile failed", __func__);
496 verifier >> blockundo;
497 filein >> hashChecksum;
498 }
catch (
const std::exception &e) {
499 return error(
"%s: Deserialize or I/O error - %s", __func__, e.what());
503 if (hashChecksum != verifier.
GetHash()) {
504 return error(
"%s: Checksum mismatch", __func__);
513 if (!
UndoFileSeq().Flush(undo_pos_old, finalize)) {
514 AbortNode(
"Flushing undo file to disk failed. This is likely the "
515 "result of an I/O error.");
535 AbortNode(
"Flushing block file to disk failed. This is likely the "
536 "result of an I/O error.");
541 if (!fFinalize || finalize_undo) {
551 retval += file.nSize + file.nUndoSize;
558 for (
const int i : setFilesToPrune) {
593 uint64_t nTime,
bool fKnown) {
601 bool finalize_undo =
false;
607 max_blockfile_size = 0x10000;
608 if (nAddSize >= max_blockfile_size) {
611 max_blockfile_size = nAddSize + 1;
617 assert(nAddSize < max_blockfile_size);
654 size_t bytes_allocated =
657 return AbortNode(
"Disk space is too low!",
658 _(
"Disk space is too low!"));
680 size_t bytes_allocated =
683 return AbortNode(state,
"Disk space is too low!",
684 _(
"Disk space is too low!"));
698 return error(
"WriteBlockToDisk: OpenBlockFile failed");
703 fileout << messageStart << nSize;
706 long fileOutPos = ftell(fileout.
Get());
707 if (fileOutPos < 0) {
708 return error(
"WriteBlockToDisk: ftell failed");
711 pos.
nPos = (
unsigned int)fileOutPos;
717 bool BlockManager::WriteUndoDataForBlock(
const CBlockUndo &blockundo,
726 return error(
"ConnectBlock(): FindUndoPos failed");
730 return AbortNode(state,
"Failed to write undo data");
740 static_cast<uint32_t
>(block.
nHeight) ==
746 block.nUndoPos = _pos.
nPos;
747 block.nStatus = block.nStatus.withUndo();
761 return error(
"ReadBlockFromDisk: OpenBlockFile failed for %s",
768 }
catch (
const std::exception &e) {
769 return error(
"%s: Deserialize or I/O error - %s at %s", __func__,
775 return error(
"ReadBlockFromDisk: Errors in block header at %s",
792 return error(
"ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() "
793 "doesn't match index for %s at %s",
794 pindex->
ToString(), block_pos.ToString());
804 return error(
"ReadTxFromDisk: OpenBlockFile failed for %s",
811 }
catch (
const std::exception &e) {
812 return error(
"%s: Deserialize or I/O error - %s at %s", __func__,
823 return error(
"ReadTxUndoFromDisk: OpenUndoFile failed for %s",
830 }
catch (
const std::exception &e) {
831 return error(
"%s: Deserialize or I/O error - %s at %s", __func__,
843 const auto position_known{dbp !=
nullptr};
844 if (position_known) {
858 error(
"%s: FindBlockPos failed", __func__);
861 if (!position_known) {
885 std::vector<fs::path> vImportFiles,
const ArgsManager &args,
898 std::multimap<BlockHash, FlatFilePos> blocks_with_unknown_parent;
910 LogPrintf(
"Reindexing block file blk%05u.dat...\n",
911 (
unsigned int)nFile);
912 chainman.ActiveChainstate().LoadExternalBlockFile(
913 file, &pos, &blocks_with_unknown_parent);
915 LogPrintf(
"Shutdown requested. Exit %s\n", __func__);
922 chainman.
m_blockman.m_block_tree_db->WriteReindexing(
false));
927 chainman.ActiveChainstate().LoadGenesisBlock();
931 for (
const fs::path &path : vImportFiles) {
934 LogPrintf(
"Importing blocks file %s...\n",
936 chainman.ActiveChainstate().LoadExternalBlockFile(file);
938 LogPrintf(
"Shutdown requested. Exit %s\n", __func__);
942 LogPrintf(
"Warning: Could not open blocks file %s\n",
952 for (
const MapCheckpoints::value_type &i : checkpoints) {
958 if (pblockindex && !pblockindex->nStatus.isValid()) {
959 LogPrintf(
"Reconsidering checkpointed block %s ...\n",
961 chainman.ActiveChainstate().ResetBlockFailureFlags(pblockindex);
964 if (pblockindex && pblockindex->nStatus.isOnParkedChain()) {
965 LogPrintf(
"Unparking checkpointed block %s ...\n",
967 chainman.ActiveChainstate().UnparkBlockAndChildren(pblockindex);
981 if (!chainstate->ActivateBestChain(state,
nullptr)) {
982 LogPrintf(
"Failed to connect best block (%s)\n",
991 LogPrintf(
"Stopping after block import\n");
996 chainman.ActiveChainstate().LoadMempool(mempool_path);
@ TREE
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
arith_uint256 GetBlockProof(const CBlockIndex &block)
std::map< int, BlockHash > MapCheckpoints
const fs::path & GetBlocksDirPath() const
Get blocks directory path.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
The block chain is a tree shaped structure starting with the genesis block at the root,...
uint64_t nTimeReceived
(memory only) block header metadata
std::string ToString() const
CBlockIndex * pprev
pointer to the index of the predecessor of 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 ...
const BlockHash * phashBlock
pointer to the hash of the block, if any.
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.
FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(
unsigned int nTx
Number of transactions in this block.
bool RaiseValidity(enum BlockValidity nUpTo) EXCLUSIVE_LOCKS_REQUIRED(
Raise the validity level of this block index entry.
BlockHash GetBlockHash() const
int nHeight
height of the entry in the chain. The genesis block has height 0
FlatFilePos GetBlockPos() const EXCLUSIVE_LOCKS_REQUIRED(
Undo information for a CBlock.
An in-memory indexed chain of blocks.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
const CCheckpointData & Checkpoints() const
Reads data from an underlying stream, while hashing the read data.
A writer stream (for serialization) that computes a 256-bit hash.
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
A mutable version of CTransaction.
Restore the UTXO in a Coin at a given COutPoint.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
const CChainParams & GetParams() const
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
FlatFileSeq represents a sequence of numbered files storing raw data.
fs::path FileName(const FlatFilePos &pos) const
Get the name of the file at the given position.
size_t Allocate(const FlatFilePos &pos, size_t add_size, bool &out_of_space)
Allocate additional space in a file after the given starting position.
FILE * Open(const FlatFilePos &pos, bool read_only=false)
Open a handle to the file at the given position.
std::string ToString() const
std::string GetHex() const
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
std::set< int > m_dirty_fileinfo
Dirty block file entries.
RecursiveMutex cs_LastBlockFile
const Consensus::Params & GetConsensus() const
void FindFilesToPrune(std::set< int > &setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd)
Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a us...
void FindFilesToPruneManual(std::set< int > &setFilesToPrune, int nManualPruneHeight, int chain_tip_height)
Calculate the block/rev files to delete based on height specified by user with RPC command pruneblock...
void FlushUndoFile(int block_file, bool finalize=false)
bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int nHeight, CChain &active_chain, uint64_t nTime, bool fKnown)
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the blocktree off disk and into memory.
void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Mark one block file as pruned (modify associated database entries)
CBlockIndex * LookupBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
CBlockIndex * InsertBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Create a new block index entry for a given block hash.
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, CChain &active_chain, const FlatFilePos *dbp)
Store block on disk.
uint64_t GetPruneTarget() const
Attempt to stay below this number of bytes of block files.
uint64_t CalculateCurrentUsage()
Calculate the amount of disk space the block & undo files currently use.
const CBlockIndex * GetLastCheckpoint(const CCheckpointData &data) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Returns last CBlockIndex* that is a checkpoint.
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
bool m_check_for_pruning
Global flag to indicate we should check to see if there are block/undo files that should be deleted.
const CChainParams & GetParams() const
bool FindUndoPos(BlockValidationState &state, int nFile, FlatFilePos &pos, unsigned int nAddSize)
bool IsPruneMode() const
Whether running in -prune mode.
std::atomic< bool > m_importing
std::vector< CBlockFileInfo > m_blockfile_info
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
bool m_have_pruned
True if any block files have ever been pruned.
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.
void FlushBlockFile(bool fFinalize=false, bool finalize_undo=false)
ImportingNow(std::atomic< bool > &importing)
std::atomic< bool > & m_importing
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
#define LogPrint(category,...)
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to byte string.
FILE * fopen(const fs::path &p, const char *mode)
const CBlockIndex *GetFirstStoredBlock(const CBlockIndex *start_block) EXCLUSIVE_LOCKS_REQUIRED(voi CleanupBlockRevFiles)()
Find the first block that is not pruned.
static const unsigned int UNDOFILE_CHUNK_SIZE
The pre-allocation chunk size for rev?????.dat files (since 0.8)
const CBlockIndex * GetFirstStoredBlock(const CBlockIndex *start_block)
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params ¶ms)
Functions for disk access for blocks.
static FILE * OpenUndoFile(const FlatFilePos &pos, bool fReadOnly=false)
Open an undo file (rev?????.dat)
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune)
Actually unlink the specified files.
static FlatFileSeq UndoFileSeq()
static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT
FILE * OpenBlockFile(const FlatFilePos &pos, bool fReadOnly)
Open a block file (blk?????.dat)
static bool WriteBlockToDisk(const CBlock &block, FlatFilePos &pos, const CMessageHeader::MessageMagic &messageStart)
bool ReadTxFromDisk(CMutableTransaction &tx, const FlatFilePos &pos)
Functions for disk access for txs.
static constexpr unsigned int BLOCKFILE_CHUNK_SIZE
The pre-allocation chunk size for blk?????.dat files (since 0.8)
static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE
Size of header written by WriteBlockToDisk before a serialized CBlock.
fs::path GetBlockPosFilename(const FlatFilePos &pos)
Translation to a filesystem path.
bool ReadTxUndoFromDisk(CTxUndo &tx_undo, const FlatFilePos &pos)
static const unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?????.dat file (since 0.8)
void ThreadImport(ChainstateManager &chainman, std::vector< fs::path > vImportFiles, const ArgsManager &args, const fs::path &mempool_path)
std::atomic_bool fReindex
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex)
static bool UndoWriteToDisk(const CBlockUndo &blockundo, FlatFilePos &pos, const BlockHash &hashBlock, const CMessageHeader::MessageMagic &messageStart)
static FlatFileSeq BlockFileSeq()
bool CheckProofOfWork(const BlockHash &hash, uint32_t nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
reverse_range< T > reverse_iterate(T &x)
size_t GetSerializeSize(const T &t, int nVersion=0)
bool AbortNode(const std::string &strMessage, bilingual_str user_message)
Abort with a message.
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
void StartShutdown()
Request shutdown of the application.
int atoi(const std::string &str)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
A BlockHash is a unqiue identifier for a block.
MapCheckpoints mapCheckpoints
Parameters that influence chain consensus.
std::string ToString() const
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
void ScheduleBatchPriority()
On platforms that support it, tell the kernel the calling thread is CPU-intensive and non-interactive...
bool error(const char *fmt, const Args &...args)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
bilingual_str _(const char *psz)
Translation function.
static const unsigned int MIN_BLOCKS_TO_KEEP
Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pr...
static const int PROTOCOL_VERSION
network protocol versioning