Bitcoin Core  21.99.0
P2P Digital Currency
blockstorage.cpp
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 #include <node/blockstorage.h>
6 
7 #include <chain.h>
8 #include <chainparams.h>
9 #include <clientversion.h>
10 #include <consensus/validation.h>
11 #include <flatfile.h>
12 #include <fs.h>
13 #include <hash.h>
14 #include <pow.h>
15 #include <shutdown.h>
16 #include <signet.h>
17 #include <streams.h>
18 #include <undo.h>
19 #include <util/system.h>
20 #include <validation.h>
21 
22 std::atomic_bool fImporting(false);
23 std::atomic_bool fReindex(false);
24 bool fHavePruned = false;
25 bool fPruneMode = false;
26 uint64_t nPruneTarget = 0;
27 
28 // TODO make namespace {
30 std::vector<CBlockFileInfo> vinfoBlockFile;
36 bool fCheckForPruning = false;
37 
39 std::set<CBlockIndex*> setDirtyBlockIndex;
40 
42 std::set<int> setDirtyFileInfo;
43 // } // namespace
44 
45 static FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false);
46 static FlatFileSeq BlockFileSeq();
47 static FlatFileSeq UndoFileSeq();
48 
49 bool IsBlockPruned(const CBlockIndex* pblockindex)
50 {
51  return (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0);
52 }
53 
54 // If we're using -prune with -reindex, then delete block files that will be ignored by the
55 // reindex. Since reindexing works by starting at block file 0 and looping until a blockfile
56 // is missing, do the same here to delete any later block files after a gap. Also delete all
57 // rev files since they'll be rewritten by the reindex anyway. This ensures that vinfoBlockFile
58 // is in sync with what's actually on disk by the time we start downloading, so that pruning
59 // works correctly.
61 {
62  std::map<std::string, fs::path> mapBlockFiles;
63 
64  // Glob all blk?????.dat and rev?????.dat files from the blocks directory.
65  // Remove the rev files immediately and insert the blk file paths into an
66  // ordered map keyed by block file index.
67  LogPrintf("Removing unusable blk?????.dat and rev?????.dat files for -reindex with -prune\n");
68  fs::path blocksdir = gArgs.GetBlocksDirPath();
69  for (fs::directory_iterator it(blocksdir); it != fs::directory_iterator(); it++) {
70  if (fs::is_regular_file(*it) &&
71  it->path().filename().string().length() == 12 &&
72  it->path().filename().string().substr(8,4) == ".dat")
73  {
74  if (it->path().filename().string().substr(0, 3) == "blk") {
75  mapBlockFiles[it->path().filename().string().substr(3, 5)] = it->path();
76  } else if (it->path().filename().string().substr(0, 3) == "rev") {
77  remove(it->path());
78  }
79  }
80  }
81 
82  // Remove all block files that aren't part of a contiguous set starting at
83  // zero by walking the ordered map (keys are block file indices) by
84  // keeping a separate counter. Once we hit a gap (or if 0 doesn't exist)
85  // start removing block files.
86  int nContigCounter = 0;
87  for (const std::pair<const std::string, fs::path>& item : mapBlockFiles) {
88  if (atoi(item.first) == nContigCounter) {
89  nContigCounter++;
90  continue;
91  }
92  remove(item.second);
93  }
94 }
95 
96 std::string CBlockFileInfo::ToString() const
97 {
98  return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, FormatISO8601Date(nTimeFirst), FormatISO8601Date(nTimeLast));
99 }
100 
102 {
104 
105  return &vinfoBlockFile.at(n);
106 }
107 
108 static bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
109 {
110  // Open history file to append
112  if (fileout.IsNull()) {
113  return error("%s: OpenUndoFile failed", __func__);
114  }
115 
116  // Write index header
117  unsigned int nSize = GetSerializeSize(blockundo, fileout.GetVersion());
118  fileout << messageStart << nSize;
119 
120  // Write undo data
121  long fileOutPos = ftell(fileout.Get());
122  if (fileOutPos < 0) {
123  return error("%s: ftell failed", __func__);
124  }
125  pos.nPos = (unsigned int)fileOutPos;
126  fileout << blockundo;
127 
128  // calculate & write checksum
130  hasher << hashBlock;
131  hasher << blockundo;
132  fileout << hasher.GetHash();
133 
134  return true;
135 }
136 
137 bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex)
138 {
139  FlatFilePos pos = pindex->GetUndoPos();
140  if (pos.IsNull()) {
141  return error("%s: no undo data available", __func__);
142  }
143 
144  // Open history file to read
145  CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
146  if (filein.IsNull()) {
147  return error("%s: OpenUndoFile failed", __func__);
148  }
149 
150  // Read block
151  uint256 hashChecksum;
152  CHashVerifier<CAutoFile> verifier(&filein); // We need a CHashVerifier as reserializing may lose data
153  try {
154  verifier << pindex->pprev->GetBlockHash();
155  verifier >> blockundo;
156  filein >> hashChecksum;
157  } catch (const std::exception& e) {
158  return error("%s: Deserialize or I/O error - %s", __func__, e.what());
159  }
160 
161  // Verify checksum
162  if (hashChecksum != verifier.GetHash()) {
163  return error("%s: Checksum mismatch", __func__);
164  }
165 
166  return true;
167 }
168 
169 static void FlushUndoFile(int block_file, bool finalize = false)
170 {
171  FlatFilePos undo_pos_old(block_file, vinfoBlockFile[block_file].nUndoSize);
172  if (!UndoFileSeq().Flush(undo_pos_old, finalize)) {
173  AbortNode("Flushing undo file to disk failed. This is likely the result of an I/O error.");
174  }
175 }
176 
177 void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false)
178 {
181  if (!BlockFileSeq().Flush(block_pos_old, fFinalize)) {
182  AbortNode("Flushing block file to disk failed. This is likely the result of an I/O error.");
183  }
184  // we do not always flush the undo file, as the chain tip may be lagging behind the incoming blocks,
185  // e.g. during IBD or a sync after a node going offline
186  if (!fFinalize || finalize_undo) FlushUndoFile(nLastBlockFile, finalize_undo);
187 }
188 
190 {
192 
193  uint64_t retval = 0;
194  for (const CBlockFileInfo& file : vinfoBlockFile) {
195  retval += file.nSize + file.nUndoSize;
196  }
197  return retval;
198 }
199 
200 void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
201 {
202  for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
203  FlatFilePos pos(*it, 0);
204  fs::remove(BlockFileSeq().FileName(pos));
205  fs::remove(UndoFileSeq().FileName(pos));
206  LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
207  }
208 }
209 
211 {
212  return FlatFileSeq(gArgs.GetBlocksDirPath(), "blk", gArgs.GetBoolArg("-fastprune", false) ? 0x4000 /* 16kb */ : BLOCKFILE_CHUNK_SIZE);
213 }
214 
216 {
218 }
219 
220 FILE* OpenBlockFile(const FlatFilePos& pos, bool fReadOnly)
221 {
222  return BlockFileSeq().Open(pos, fReadOnly);
223 }
224 
226 static FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly)
227 {
228  return UndoFileSeq().Open(pos, fReadOnly);
229 }
230 
231 fs::path GetBlockPosFilename(const FlatFilePos& pos)
232 {
233  return BlockFileSeq().FileName(pos);
234 }
235 
236 bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown = false)
237 {
239 
240  unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile;
241  if (vinfoBlockFile.size() <= nFile) {
242  vinfoBlockFile.resize(nFile + 1);
243  }
244 
245  bool finalize_undo = false;
246  if (!fKnown) {
247  while (vinfoBlockFile[nFile].nSize + nAddSize >= (gArgs.GetBoolArg("-fastprune", false) ? 0x10000 /* 64kb */ : MAX_BLOCKFILE_SIZE)) {
248  // when the undo file is keeping up with the block file, we want to flush it explicitly
249  // when it is lagging behind (more blocks arrive than are being connected), we let the
250  // undo block write case handle it
251  assert(std::addressof(::ChainActive()) == std::addressof(active_chain));
252  finalize_undo = (vinfoBlockFile[nFile].nHeightLast == (unsigned int)active_chain.Tip()->nHeight);
253  nFile++;
254  if (vinfoBlockFile.size() <= nFile) {
255  vinfoBlockFile.resize(nFile + 1);
256  }
257  }
258  pos.nFile = nFile;
259  pos.nPos = vinfoBlockFile[nFile].nSize;
260  }
261 
262  if ((int)nFile != nLastBlockFile) {
263  if (!fKnown) {
264  LogPrint(BCLog::VALIDATION, "Leaving block file %i: %s\n", nLastBlockFile, vinfoBlockFile[nLastBlockFile].ToString());
265  }
266  FlushBlockFile(!fKnown, finalize_undo);
267  nLastBlockFile = nFile;
268  }
269 
270  vinfoBlockFile[nFile].AddBlock(nHeight, nTime);
271  if (fKnown) {
272  vinfoBlockFile[nFile].nSize = std::max(pos.nPos + nAddSize, vinfoBlockFile[nFile].nSize);
273  } else {
274  vinfoBlockFile[nFile].nSize += nAddSize;
275  }
276 
277  if (!fKnown) {
278  bool out_of_space;
279  size_t bytes_allocated = BlockFileSeq().Allocate(pos, nAddSize, out_of_space);
280  if (out_of_space) {
281  return AbortNode("Disk space is too low!", _("Disk space is too low!"));
282  }
283  if (bytes_allocated != 0 && fPruneMode) {
284  fCheckForPruning = true;
285  }
286  }
287 
288  setDirtyFileInfo.insert(nFile);
289  return true;
290 }
291 
292 static bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize)
293 {
294  pos.nFile = nFile;
295 
297 
298  pos.nPos = vinfoBlockFile[nFile].nUndoSize;
299  vinfoBlockFile[nFile].nUndoSize += nAddSize;
300  setDirtyFileInfo.insert(nFile);
301 
302  bool out_of_space;
303  size_t bytes_allocated = UndoFileSeq().Allocate(pos, nAddSize, out_of_space);
304  if (out_of_space) {
305  return AbortNode(state, "Disk space is too low!", _("Disk space is too low!"));
306  }
307  if (bytes_allocated != 0 && fPruneMode) {
308  fCheckForPruning = true;
309  }
310 
311  return true;
312 }
313 
314 static bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessageHeader::MessageStartChars& messageStart)
315 {
316  // Open history file to append
318  if (fileout.IsNull()) {
319  return error("WriteBlockToDisk: OpenBlockFile failed");
320  }
321 
322  // Write index header
323  unsigned int nSize = GetSerializeSize(block, fileout.GetVersion());
324  fileout << messageStart << nSize;
325 
326  // Write block
327  long fileOutPos = ftell(fileout.Get());
328  if (fileOutPos < 0) {
329  return error("WriteBlockToDisk: ftell failed");
330  }
331  pos.nPos = (unsigned int)fileOutPos;
332  fileout << block;
333 
334  return true;
335 }
336 
337 bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams)
338 {
339  // Write undo information to disk
340  if (pindex->GetUndoPos().IsNull()) {
341  FlatFilePos _pos;
342  if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo, CLIENT_VERSION) + 40)) {
343  return error("ConnectBlock(): FindUndoPos failed");
344  }
345  if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart())) {
346  return AbortNode(state, "Failed to write undo data");
347  }
348  // rev files are written in block height order, whereas blk files are written as blocks come in (often out of order)
349  // we want to flush the rev (undo) file once we've written the last block, which is indicated by the last height
350  // in the block file info as below; note that this does not catch the case where the undo writes are keeping up
351  // with the block writes (usually when a synced up node is getting newly mined blocks) -- this case is caught in
352  // the FindBlockPos function
353  if (_pos.nFile < nLastBlockFile && static_cast<uint32_t>(pindex->nHeight) == vinfoBlockFile[_pos.nFile].nHeightLast) {
354  FlushUndoFile(_pos.nFile, true);
355  }
356 
357  // update nUndoPos in block index
358  pindex->nUndoPos = _pos.nPos;
359  pindex->nStatus |= BLOCK_HAVE_UNDO;
360  setDirtyBlockIndex.insert(pindex);
361  }
362 
363  return true;
364 }
365 
366 bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams)
367 {
368  block.SetNull();
369 
370  // Open history file to read
371  CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
372  if (filein.IsNull()) {
373  return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
374  }
375 
376  // Read block
377  try {
378  filein >> block;
379  } catch (const std::exception& e) {
380  return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
381  }
382 
383  // Check the header
384  if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams)) {
385  return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
386  }
387 
388  // Signet only: check block solution
389  if (consensusParams.signet_blocks && !CheckSignetBlockSolution(block, consensusParams)) {
390  return error("ReadBlockFromDisk: Errors in block solution at %s", pos.ToString());
391  }
392 
393  return true;
394 }
395 
396 bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
397 {
398  FlatFilePos blockPos;
399  {
400  LOCK(cs_main);
401  blockPos = pindex->GetBlockPos();
402  }
403 
404  if (!ReadBlockFromDisk(block, blockPos, consensusParams)) {
405  return false;
406  }
407  if (block.GetHash() != pindex->GetBlockHash()) {
408  return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
409  pindex->ToString(), pindex->GetBlockPos().ToString());
410  }
411  return true;
412 }
413 
414 bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start)
415 {
416  FlatFilePos hpos = pos;
417  hpos.nPos -= 8; // Seek back 8 bytes for meta header
418  CAutoFile filein(OpenBlockFile(hpos, true), SER_DISK, CLIENT_VERSION);
419  if (filein.IsNull()) {
420  return error("%s: OpenBlockFile failed for %s", __func__, pos.ToString());
421  }
422 
423  try {
425  unsigned int blk_size;
426 
427  filein >> blk_start >> blk_size;
428 
429  if (memcmp(blk_start, message_start, CMessageHeader::MESSAGE_START_SIZE)) {
430  return error("%s: Block magic mismatch for %s: %s versus expected %s", __func__, pos.ToString(),
431  HexStr(blk_start),
432  HexStr(message_start));
433  }
434 
435  if (blk_size > MAX_SIZE) {
436  return error("%s: Block data is larger than maximum deserialization size for %s: %s versus %s", __func__, pos.ToString(),
437  blk_size, MAX_SIZE);
438  }
439 
440  block.resize(blk_size); // Zeroing of memory is intentional here
441  filein.read((char*)block.data(), blk_size);
442  } catch (const std::exception& e) {
443  return error("%s: Read from block file failed: %s for %s", __func__, e.what(), pos.ToString());
444  }
445 
446  return true;
447 }
448 
449 bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const CBlockIndex* pindex, const CMessageHeader::MessageStartChars& message_start)
450 {
451  FlatFilePos block_pos;
452  {
453  LOCK(cs_main);
454  block_pos = pindex->GetBlockPos();
455  }
456 
457  return ReadRawBlockFromDisk(block, block_pos, message_start);
458 }
459 
461 FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp)
462 {
463  unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION);
464  FlatFilePos blockPos;
465  if (dbp != nullptr) {
466  blockPos = *dbp;
467  }
468  if (!FindBlockPos(blockPos, nBlockSize + 8, nHeight, active_chain, block.GetBlockTime(), dbp != nullptr)) {
469  error("%s: FindBlockPos failed", __func__);
470  return FlatFilePos();
471  }
472  if (dbp == nullptr) {
473  if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart())) {
474  AbortNode("Failed to write block");
475  return FlatFilePos();
476  }
477  }
478  return blockPos;
479 }
480 
483  {
484  assert(fImporting == false);
485  fImporting = true;
486  }
487 
489  {
490  assert(fImporting == true);
491  fImporting = false;
492  }
493 };
494 
495 void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args)
496 {
497  const CChainParams& chainparams = Params();
499 
500  {
501  CImportingNow imp;
502 
503  // -reindex
504  if (fReindex) {
505  int nFile = 0;
506  while (true) {
507  FlatFilePos pos(nFile, 0);
508  if (!fs::exists(GetBlockPosFilename(pos))) {
509  break; // No block files left to reindex
510  }
511  FILE* file = OpenBlockFile(pos, true);
512  if (!file) {
513  break; // This error is logged in OpenBlockFile
514  }
515  LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
516  chainman.ActiveChainstate().LoadExternalBlockFile(chainparams, file, &pos);
517  if (ShutdownRequested()) {
518  LogPrintf("Shutdown requested. Exit %s\n", __func__);
519  return;
520  }
521  nFile++;
522  }
523  pblocktree->WriteReindexing(false);
524  fReindex = false;
525  LogPrintf("Reindexing finished\n");
526  // To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
527  chainman.ActiveChainstate().LoadGenesisBlock(chainparams);
528  }
529 
530  // -loadblock=
531  for (const fs::path& path : vImportFiles) {
532  FILE* file = fsbridge::fopen(path, "rb");
533  if (file) {
534  LogPrintf("Importing blocks file %s...\n", path.string());
535  chainman.ActiveChainstate().LoadExternalBlockFile(chainparams, file);
536  if (ShutdownRequested()) {
537  LogPrintf("Shutdown requested. Exit %s\n", __func__);
538  return;
539  }
540  } else {
541  LogPrintf("Warning: Could not open blocks file %s\n", path.string());
542  }
543  }
544 
545  // scan for better chains in the block chain database, that are not yet connected in the active best chain
546 
547  // We can't hold cs_main during ActivateBestChain even though we're accessing
548  // the chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve
549  // the relevant pointers before the ABC call.
550  for (CChainState* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) {
551  BlockValidationState state;
552  if (!chainstate->ActivateBestChain(state, chainparams, nullptr)) {
553  LogPrintf("Failed to connect best block (%s)\n", state.ToString());
554  StartShutdown();
555  return;
556  }
557  }
558 
559  if (args.GetBoolArg("-stopafterblockimport", DEFAULT_STOPAFTERBLOCKIMPORT)) {
560  LogPrintf("Stopping after block import\n");
561  StartShutdown();
562  return;
563  }
564  } // End scope of CImportingNow
565  chainman.ActiveChainstate().LoadMempool(args);
566 }
UndoFileSeq
static FlatFileSeq UndoFileSeq()
Definition: blockstorage.cpp:215
GetSerializeSize
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1116
FlatFilePos::nFile
int nFile
Definition: flatfile.h:16
ShutdownRequested
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
Definition: shutdown.cpp:87
FormatISO8601Date
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:145
ArgsManager::GetBoolArg
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:594
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:57
CBlockFileInfo::nTimeFirst
uint64_t nTimeFirst
earliest time of block in file
Definition: chain.h:48
CBlockIndex::nFile
int nFile
Which # file this block is stored in (blk?????.dat)
Definition: chain.h:153
ToString
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:79
SER_DISK
@ SER_DISK
Definition: serialize.h:167
IsBlockPruned
bool IsBlockPruned(const CBlockIndex *pblockindex)
Check whether the block associated with this index entry is pruned or not.
Definition: blockstorage.cpp:49
pow.h
fPruneMode
bool fPruneMode
True if we're running in -prune mode.
Definition: blockstorage.cpp:25
CImportingNow::~CImportingNow
~CImportingNow()
Definition: blockstorage.cpp:488
CBlockHeader::nBits
uint32_t nBits
Definition: block.h:28
fs.h
nHeight
unsigned int nHeight
Definition: mempool_eviction.cpp:14
ChainActive
CChain & ChainActive()
Please prefer the identical ChainstateManager::ActiveChain.
Definition: validation.cpp:111
CHashVerifier
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:157
BlockValidationState
Definition: validation.h:140
fsbridge::fopen
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:24
streams.h
FlatFilePos::ToString
std::string ToString() const
Definition: flatfile.cpp:23
CBlockIndex::nTx
unsigned int nTx
Number of transactions in this block.
Definition: chain.h:169
ChainstateManager::GetAll
CChainState &InitializeChainstate(CTxMemPool &mempool, const std::optional< uint256 > &snapshot_blockhash=std::nullopt) EXCLUSIVE_LOCKS_REQUIRED(std::vector< CChainState * > GetAll()
Instantiate a new chainstate and assign it based upon whether it is from a snapshot.
Definition: validation.h:873
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
atoi
int atoi(const std::string &str)
Definition: strencodings.cpp:449
CAutoFile::Get
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:605
CleanupBlockRevFiles
void CleanupBlockRevFiles()
Definition: blockstorage.cpp:60
flatfile.h
CBlockIndex::pprev
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:144
FlatFileSeq::Allocate
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.
Definition: flatfile.cpp:55
CBlockIndex::nHeight
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:150
FlatFilePos::IsNull
bool IsNull() const
Definition: flatfile.h:37
validation.h
CChainParams
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:67
CheckSignetBlockSolution
bool CheckSignetBlockSolution(const CBlock &block, const Consensus::Params &consensusParams)
Extract signature and check whether a block has a valid solution.
Definition: signet.cpp:124
FlushUndoFile
static void FlushUndoFile(int block_file, bool finalize=false)
Definition: blockstorage.cpp:169
CBlockIndex::ToString
std::string ToString() const
Definition: chain.h:286
CAutoFile::read
void read(char *pch, size_t nSize)
Definition: streams.h:617
WITH_LOCK
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:276
CheckProofOfWork
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:74
AnnotatedMixin< std::recursive_mutex >
clientversion.h
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
vinfoBlockFile
std::vector< CBlockFileInfo > vinfoBlockFile
Definition: blockstorage.cpp:30
chainparams.h
ChainstateManager::ActiveChainstate
CChainState & ActiveChainstate() const
The most-work chain.
Definition: validation.cpp:4985
ScheduleBatchPriority
void ScheduleBatchPriority()
On platforms that support it, tell the kernel the calling thread is CPU-intensive and non-interactive...
Definition: system.cpp:1367
WriteBlockToDisk
static bool WriteBlockToDisk(const CBlock &block, FlatFilePos &pos, const CMessageHeader::MessageStartChars &messageStart)
Definition: blockstorage.cpp:314
CChain::Tip
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:403
ArgsManager::GetBlocksDirPath
const fs::path & GetBlocksDirPath()
Get blocks directory path.
Definition: system.cpp:391
CBlockHeader::GetHash
uint256 GetHash() const
Definition: block.cpp:11
FindBlockPos
bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int nHeight, CChain &active_chain, uint64_t nTime, bool fKnown=false)
Definition: blockstorage.cpp:236
CBlockIndex::GetBlockPos
FlatFilePos GetBlockPos() const
Definition: chain.h:215
FlushBlockFile
void FlushBlockFile(bool fFinalize=false, bool finalize_undo=false)
Definition: blockstorage.cpp:177
fReindex
std::atomic_bool fReindex(false)
ValidationState::ToString
std::string ToString() const
Definition: validation.h:125
shutdown.h
CAutoFile::GetVersion
int GetVersion() const
Definition: streams.h:615
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:564
FlatFileSeq
FlatFileSeq represents a sequence of numbered files storing raw data.
Definition: flatfile.h:46
Consensus::Params
Parameters that influence chain consensus.
Definition: params.h:56
CBlockFileInfo::nTimeLast
uint64_t nTimeLast
latest time of block in file
Definition: chain.h:49
MAX_SIZE
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...
Definition: serialize.h:31
FlatFileSeq::Open
FILE * Open(const FlatFilePos &pos, bool read_only=false)
Open a handle to the file at the given position.
Definition: flatfile.cpp:33
CBlockIndex::nStatus
uint32_t nStatus
Verification status of this block.
Definition: chain.h:187
CBlockFileInfo::nBlocks
unsigned int nBlocks
number of blocks stored in file
Definition: chain.h:43
CalculateCurrentUsage
uint64_t CalculateCurrentUsage()
Calculate the amount of disk space the block & undo files currently use.
Definition: blockstorage.cpp:189
CImportingNow::CImportingNow
CImportingNow()
Definition: blockstorage.cpp:482
CBlockHeader::GetBlockTime
int64_t GetBlockTime() const
Definition: block.h:55
FlatFilePos
Definition: flatfile.h:14
CChainState::LoadGenesisBlock
bool LoadGenesisBlock(const CChainParams &chainparams)
Ensures we have a genesis block in the block tree, possibly writing one to disk.
Definition: validation.cpp:4121
CBlockFileInfo::nHeightFirst
unsigned int nHeightFirst
lowest height of block in file
Definition: chain.h:46
undo.h
CImportingNow
Definition: blockstorage.cpp:481
CChainState::LoadMempool
void LoadMempool(const ArgsManager &args)
Load the persisted mempool from disk.
Definition: validation.cpp:3808
fHavePruned
bool fHavePruned
Pruning-related variables and constants.
Definition: blockstorage.cpp:24
LogPrintf
#define LogPrintf(...)
Definition: logging.h:184
ReadBlockFromDisk
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
Definition: blockstorage.cpp:366
signet.h
CAutoFile::IsNull
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:609
BCLog::VALIDATION
@ VALIDATION
Definition: logging.h:59
uint256
256-bit opaque blob.
Definition: uint256.h:124
OpenUndoFile
static FILE * OpenUndoFile(const FlatFilePos &pos, bool fReadOnly=false)
Open an undo file (rev?????.dat)
Definition: blockstorage.cpp:226
GetBlockFileInfo
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
Definition: blockstorage.cpp:101
CChainState
CChainState stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:516
CBlockFileInfo::nHeightLast
unsigned int nHeightLast
highest height of block in file
Definition: chain.h:47
LogPrint
#define LogPrint(category,...)
Definition: logging.h:188
UndoReadFromDisk
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex)
Definition: blockstorage.cpp:137
CMessageHeader::MESSAGE_START_SIZE
static constexpr size_t MESSAGE_START_SIZE
Definition: protocol.h:32
CBlockIndex::GetBlockHash
uint256 GetBlockHash() const
Definition: chain.h:246
UndoWriteToDisk
static bool UndoWriteToDisk(const CBlockUndo &blockundo, FlatFilePos &pos, const uint256 &hashBlock, const CMessageHeader::MessageStartChars &messageStart)
Definition: blockstorage.cpp:108
CChainParams::MessageStart
const CMessageHeader::MessageStartChars & MessageStart() const
Definition: chainparams.h:81
ChainstateManager
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:792
system.h
CBlock
Definition: block.h:62
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
FlatFileSeq::FileName
fs::path FileName(const FlatFilePos &pos) const
Get the name of the file at the given position.
Definition: flatfile.cpp:28
CChain
An in-memory indexed chain of blocks.
Definition: chain.h:392
setDirtyBlockIndex
std::set< CBlockIndex * > setDirtyBlockIndex
Dirty block index entries.
Definition: blockstorage.cpp:39
FindUndoPos
static bool FindUndoPos(BlockValidationState &state, int nFile, FlatFilePos &pos, unsigned int nAddSize)
Definition: blockstorage.cpp:292
WriteUndoDataForBlock
bool WriteUndoDataForBlock(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex *pindex, const CChainParams &chainparams)
Definition: blockstorage.cpp:337
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:127
OpenBlockFile
FILE * OpenBlockFile(const FlatFilePos &pos, bool fReadOnly)
Open a block file (blk?????.dat)
Definition: blockstorage.cpp:220
ArgsManager
Definition: system.h:161
GetBlockPosFilename
fs::path GetBlockPosFilename(const FlatFilePos &pos)
Translation to a filesystem path.
Definition: blockstorage.cpp:231
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
pblocktree
std::unique_ptr< CBlockTreeDB > pblocktree
Global variable that points to the active block tree (protected by cs_main)
Definition: validation.cpp:187
LOCK
#define LOCK(cs)
Definition: sync.h:232
CBlockIndex::nUndoPos
unsigned int nUndoPos
Byte offset within rev?????.dat where this block's undo data is stored.
Definition: chain.h:159
gArgs
ArgsManager gArgs
Definition: system.cpp:79
CBlockFileInfo
Definition: chain.h:40
blockstorage.h
CLIENT_VERSION
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:33
BlockFileSeq
static FlatFileSeq BlockFileSeq()
Definition: blockstorage.cpp:210
CHashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:100
BLOCK_HAVE_UNDO
@ BLOCK_HAVE_UNDO
undo data available in rev*.dat
Definition: chain.h:122
fImporting
std::atomic_bool fImporting(false)
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
nLastBlockFile
int nLastBlockFile
Definition: blockstorage.cpp:31
ReadRawBlockFromDisk
bool ReadRawBlockFromDisk(std::vector< uint8_t > &block, const FlatFilePos &pos, const CMessageHeader::MessageStartChars &message_start)
Definition: blockstorage.cpp:414
AbortNode
bool AbortNode(const std::string &strMessage, bilingual_str user_message)
Abort with a message.
Definition: shutdown.cpp:21
FlatFilePos::nPos
unsigned int nPos
Definition: flatfile.h:17
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
CBlockFileInfo::ToString
std::string ToString() const
Definition: blockstorage.cpp:96
cs_LastBlockFile
RecursiveMutex cs_LastBlockFile
Definition: blockstorage.cpp:29
nPruneTarget
uint64_t nPruneTarget
Number of MiB of block files that we're trying to stay below.
Definition: blockstorage.cpp:26
CBlock::SetNull
void SetNull()
Definition: block.h:88
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
BLOCK_HAVE_DATA
@ BLOCK_HAVE_DATA
full block available in blk*.dat
Definition: chain.h:121
CHashWriter::GetHash
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
Definition: hash.h:122
ThreadImport
void ThreadImport(ChainstateManager &chainman, std::vector< fs::path > vImportFiles, const ArgsManager &args)
Definition: blockstorage.cpp:495
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:137
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:594
CBlockIndex::GetUndoPos
FlatFilePos GetUndoPos() const
Definition: chain.h:224
CBlockFileInfo::nSize
unsigned int nSize
number of used bytes of block file
Definition: chain.h:44
assert
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:168
StartShutdown
void StartShutdown()
Request shutdown of the application.
Definition: shutdown.cpp:56
fCheckForPruning
bool fCheckForPruning
Global flag to indicate we should check to see if there are block/undo files that should be deleted.
Definition: blockstorage.cpp:36
CBlockUndo
Undo information for a CBlock.
Definition: undo.h:63
UnlinkPrunedFiles
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune)
Actually unlink the specified files.
Definition: blockstorage.cpp:200
it
auto it
Definition: validation.cpp:382
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
Consensus::Params::signet_blocks
bool signet_blocks
If true, witness commitments contain a payload equal to a Bitcoin Script solution to the signet chall...
Definition: params.h:101
setDirtyFileInfo
std::set< int > setDirtyFileInfo
Dirty block file entries.
Definition: blockstorage.cpp:42