Bitcoin ABC  0.29.2
P2P Digital Currency
blockstorage.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2022 The Bitcoin 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 
8 #include <chain.h>
9 #include <chainparams.h>
10 #include <clientversion.h>
11 #include <config.h>
12 #include <consensus/validation.h>
13 #include <flatfile.h>
14 #include <fs.h>
15 #include <hash.h>
16 #include <pow/pow.h>
17 #include <reverse_iterator.h>
18 #include <shutdown.h>
19 #include <streams.h>
20 #include <undo.h>
21 #include <util/system.h>
22 #include <validation.h>
23 
24 #include <map>
25 
26 namespace node {
27 std::atomic_bool fReindex(false);
28 
29 static FILE *OpenUndoFile(const FlatFilePos &pos, bool fReadOnly = false);
30 
31 static FlatFileSeq BlockFileSeq();
32 static FlatFileSeq UndoFileSeq();
33 
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);
40  }
41  return rv;
42 }
43 
46  BlockMap::iterator it = m_block_index.find(hash);
47  return it == m_block_index.end() ? nullptr : &it->second;
48 }
49 
52  BlockMap::const_iterator it = m_block_index.find(hash);
53  return it == m_block_index.end() ? nullptr : &it->second;
54 }
55 
56 CBlockIndex *BlockManager::AddToBlockIndex(const CBlockHeader &block,
57  CBlockIndex *&best_header) {
59 
60  const auto [mi, inserted] =
61  m_block_index.try_emplace(block.GetHash(), block);
62  if (!inserted) {
63  return &mi->second;
64  }
65  CBlockIndex *pindexNew = &(*mi).second;
66 
67  // We assign the sequence id to blocks only when the full data is available,
68  // to avoid miners withholding blocks but broadcasting headers, to get a
69  // competitive advantage.
70  pindexNew->nSequenceId = 0;
71 
72  pindexNew->phashBlock = &((*mi).first);
73  BlockMap::iterator miPrev = m_block_index.find(block.hashPrevBlock);
74  if (miPrev != m_block_index.end()) {
75  pindexNew->pprev = &(*miPrev).second;
76  pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
77  pindexNew->BuildSkip();
78  }
79  pindexNew->nTimeReceived = GetTime();
80  pindexNew->nTimeMax =
81  (pindexNew->pprev
82  ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime)
83  : pindexNew->nTime);
84  pindexNew->nChainWork =
85  (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) +
86  GetBlockProof(*pindexNew);
88  if (best_header == nullptr ||
89  best_header->nChainWork < pindexNew->nChainWork) {
90  best_header = pindexNew;
91  }
92 
93  m_dirty_blockindex.insert(pindexNew);
94  return pindexNew;
95 }
96 
97 void BlockManager::PruneOneBlockFile(const int fileNumber) {
100 
101  for (auto &entry : m_block_index) {
102  CBlockIndex *pindex = &entry.second;
103  if (pindex->nFile == fileNumber) {
104  pindex->nStatus = pindex->nStatus.withData(false).withUndo(false);
105  pindex->nFile = 0;
106  pindex->nDataPos = 0;
107  pindex->nUndoPos = 0;
108  m_dirty_blockindex.insert(pindex);
109 
110  // Prune from m_blocks_unlinked -- any block we prune would have
111  // to be downloaded again in order to consider its chain, at which
112  // point it would be considered as a candidate for
113  // m_blocks_unlinked or setBlockIndexCandidates.
114  auto range = m_blocks_unlinked.equal_range(pindex->pprev);
115  while (range.first != range.second) {
116  std::multimap<CBlockIndex *, CBlockIndex *>::iterator _it =
117  range.first;
118  range.first++;
119  if (_it->second == pindex) {
120  m_blocks_unlinked.erase(_it);
121  }
122  }
123  }
124  }
125 
126  m_blockfile_info[fileNumber].SetNull();
127  m_dirty_fileinfo.insert(fileNumber);
128 }
129 
130 void BlockManager::FindFilesToPruneManual(std::set<int> &setFilesToPrune,
131  int nManualPruneHeight,
132  int chain_tip_height) {
133  assert(IsPruneMode() && nManualPruneHeight > 0);
134 
136  if (chain_tip_height < 0) {
137  return;
138  }
139 
140  // last block to prune is the lesser of (user-specified height,
141  // MIN_BLOCKS_TO_KEEP from the tip)
142  unsigned int nLastBlockWeCanPrune{std::min(
143  (unsigned)nManualPruneHeight, chain_tip_height - MIN_BLOCKS_TO_KEEP)};
144  int count = 0;
145  for (int fileNumber = 0; fileNumber < m_last_blockfile; fileNumber++) {
146  if (m_blockfile_info[fileNumber].nSize == 0 ||
147  m_blockfile_info[fileNumber].nHeightLast > nLastBlockWeCanPrune) {
148  continue;
149  }
150  PruneOneBlockFile(fileNumber);
151  setFilesToPrune.insert(fileNumber);
152  count++;
153  }
154  LogPrintf("Prune (Manual): prune_height=%d removed %d blk/rev pairs\n",
155  nLastBlockWeCanPrune, count);
156 }
157 
158 void BlockManager::FindFilesToPrune(std::set<int> &setFilesToPrune,
159  uint64_t nPruneAfterHeight,
160  int chain_tip_height, int prune_height,
161  bool is_ibd) {
163  if (chain_tip_height < 0 || GetPruneTarget() == 0) {
164  return;
165  }
166  if (uint64_t(chain_tip_height) <= nPruneAfterHeight) {
167  return;
168  }
169 
170  unsigned int nLastBlockWeCanPrune = std::min(
171  prune_height, chain_tip_height - static_cast<int>(MIN_BLOCKS_TO_KEEP));
172  uint64_t nCurrentUsage = CalculateCurrentUsage();
173  // We don't check to prune until after we've allocated new space for files,
174  // so we should leave a buffer under our target to account for another
175  // allocation before the next pruning.
176  uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
177  uint64_t nBytesToPrune;
178  int count = 0;
179 
180  if (nCurrentUsage + nBuffer >= GetPruneTarget()) {
181  // On a prune event, the chainstate DB is flushed.
182  // To avoid excessive prune events negating the benefit of high dbcache
183  // values, we should not prune too rapidly.
184  // So when pruning in IBD, increase the buffer a bit to avoid a re-prune
185  // too soon.
186  if (is_ibd) {
187  // Since this is only relevant during IBD, we use a fixed 10%
188  nBuffer += GetPruneTarget() / 10;
189  }
190 
191  for (int fileNumber = 0; fileNumber < m_last_blockfile; fileNumber++) {
192  nBytesToPrune = m_blockfile_info[fileNumber].nSize +
193  m_blockfile_info[fileNumber].nUndoSize;
194 
195  if (m_blockfile_info[fileNumber].nSize == 0) {
196  continue;
197  }
198 
199  // are we below our target?
200  if (nCurrentUsage + nBuffer < GetPruneTarget()) {
201  break;
202  }
203 
204  // don't prune files that could have a block within
205  // MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
206  if (m_blockfile_info[fileNumber].nHeightLast >
207  nLastBlockWeCanPrune) {
208  continue;
209  }
210 
211  PruneOneBlockFile(fileNumber);
212  // Queue up the files for removal
213  setFilesToPrune.insert(fileNumber);
214  nCurrentUsage -= nBytesToPrune;
215  count++;
216  }
217  }
218 
220  "Prune: target=%dMiB actual=%dMiB diff=%dMiB "
221  "max_prune_height=%d removed %d blk/rev pairs\n",
222  GetPruneTarget() / 1024 / 1024, nCurrentUsage / 1024 / 1024,
223  (int64_t(GetPruneTarget()) - int64_t(nCurrentUsage)) / 1024 / 1024,
224  nLastBlockWeCanPrune, count);
225 }
226 
229 
230  if (hash.IsNull()) {
231  return nullptr;
232  }
233 
234  const auto [mi, inserted] = m_block_index.try_emplace(hash);
235  CBlockIndex *pindex = &(*mi).second;
236  if (inserted) {
237  pindex->phashBlock = &((*mi).first);
238  }
239  return pindex;
240 }
241 
244  if (!m_block_tree_db->LoadBlockIndexGuts(
245  GetConsensus(),
246  [this](const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
247  return this->InsertBlockIndex(hash);
248  })) {
249  return false;
250  }
251 
252  // Calculate nChainWork
253  std::vector<CBlockIndex *> vSortedByHeight{GetAllBlockIndices()};
254  std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
256 
257  for (CBlockIndex *pindex : vSortedByHeight) {
258  if (ShutdownRequested()) {
259  return false;
260  }
261  pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) +
262  GetBlockProof(*pindex);
263  pindex->nTimeMax =
264  (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime)
265  : pindex->nTime);
266 
267  // We can link the chain of blocks for which we've received
268  // transactions at some point, or blocks that are assumed-valid on the
269  // basis of snapshot load (see PopulateAndValidateSnapshot()).
270  // Pruned nodes may have deleted the block.
271  if (pindex->nTx > 0) {
272  if (!pindex->UpdateChainStats() && pindex->pprev) {
273  m_blocks_unlinked.insert(std::make_pair(pindex->pprev, pindex));
274  }
275  }
276 
277  if (!pindex->nStatus.hasFailed() && pindex->pprev &&
278  pindex->pprev->nStatus.hasFailed()) {
279  pindex->nStatus = pindex->nStatus.withFailedParent();
280  m_dirty_blockindex.insert(pindex);
281  }
282 
283  if (pindex->pprev) {
284  pindex->BuildSkip();
285  }
286  }
287 
288  return true;
289 }
290 
291 bool BlockManager::WriteBlockIndexDB() {
292  std::vector<std::pair<int, const CBlockFileInfo *>> vFiles;
293  vFiles.reserve(m_dirty_fileinfo.size());
294  for (int i : m_dirty_fileinfo) {
295  vFiles.push_back(std::make_pair(i, &m_blockfile_info[i]));
296  }
297 
298  m_dirty_fileinfo.clear();
299 
300  std::vector<const CBlockIndex *> vBlocks;
301  vBlocks.reserve(m_dirty_blockindex.size());
302  for (const CBlockIndex *cbi : m_dirty_blockindex) {
303  vBlocks.push_back(cbi);
304  }
305 
306  m_dirty_blockindex.clear();
307 
308  if (!m_block_tree_db->WriteBatchSync(vFiles, m_last_blockfile, vBlocks)) {
309  return false;
310  }
311  return true;
312 }
313 
314 bool BlockManager::LoadBlockIndexDB() {
315  if (!LoadBlockIndex()) {
316  return false;
317  }
318 
319  // Load block file info
320  m_block_tree_db->ReadLastBlockFile(m_last_blockfile);
322  LogPrintf("%s: last block file = %i\n", __func__, m_last_blockfile);
323  for (int nFile = 0; nFile <= m_last_blockfile; nFile++) {
324  m_block_tree_db->ReadBlockFileInfo(nFile, m_blockfile_info[nFile]);
325  }
326  LogPrintf("%s: last block file info: %s\n", __func__,
328  for (int nFile = m_last_blockfile + 1; true; nFile++) {
329  CBlockFileInfo info;
330  if (m_block_tree_db->ReadBlockFileInfo(nFile, info)) {
331  m_blockfile_info.push_back(info);
332  } else {
333  break;
334  }
335  }
336 
337  // Check presence of blk files
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);
343  }
344  }
345 
346  for (const int i : setBlkDataFiles) {
347  FlatFilePos pos(i, 0);
349  .IsNull()) {
350  return false;
351  }
352  }
353 
354  // Check whether we have ever pruned block & undo files
355  m_block_tree_db->ReadFlag("prunedblockfiles", m_have_pruned);
356  if (m_have_pruned) {
357  LogPrintf(
358  "LoadBlockIndexDB(): Block files have previously been pruned\n");
359  }
360 
361  // Check whether we need to continue reindexing
362  if (m_block_tree_db->IsReindexing()) {
363  fReindex = true;
364  }
365 
366  return true;
367 }
368 
369 const CBlockIndex *
371  const MapCheckpoints &checkpoints = data.mapCheckpoints;
372 
373  for (const MapCheckpoints::value_type &i : reverse_iterate(checkpoints)) {
374  const BlockHash &hash = i.second;
375  const CBlockIndex *pindex = LookupBlockIndex(hash);
376  if (pindex) {
377  return pindex;
378  }
379  }
380 
381  return nullptr;
382 }
383 
384 bool BlockManager::IsBlockPruned(const CBlockIndex *pblockindex) {
386  return (m_have_pruned && !pblockindex->nStatus.hasData() &&
387  pblockindex->nTx > 0);
388 }
389 
390 const CBlockIndex *GetFirstStoredBlock(const CBlockIndex *start_block) {
392  assert(start_block);
393  const CBlockIndex *last_block = start_block;
394  while (last_block->pprev && (last_block->pprev->nStatus.hasData())) {
395  last_block = last_block->pprev;
396  }
397  return last_block;
398 }
399 
400 // If we're using -prune with -reindex, then delete block files that will be
401 // ignored by the reindex. Since reindexing works by starting at block file 0
402 // and looping until a blockfile is missing, do the same here to delete any
403 // later block files after a gap. Also delete all rev files since they'll be
404 // rewritten by the reindex anyway. This ensures that m_blockfile_info is in
405 // sync with what's actually on disk by the time we start downloading, so that
406 // pruning works correctly.
407 void CleanupBlockRevFiles() {
408  std::map<std::string, fs::path> mapBlockFiles;
409 
410  // Glob all blk?????.dat and rev?????.dat files from the blocks directory.
411  // Remove the rev files immediately and insert the blk file paths into an
412  // ordered map keyed by block file index.
413  LogPrintf("Removing unusable blk?????.dat and rev?????.dat files for "
414  "-reindex with -prune\n");
415  for (const auto &file : fs::directory_iterator{gArgs.GetBlocksDirPath()}) {
416  const std::string path = fs::PathToString(file.path().filename());
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") {
422  remove(file.path());
423  }
424  }
425  }
426 
427  // Remove all block files that aren't part of a contiguous set starting at
428  // zero by walking the ordered map (keys are block file indices) by keeping
429  // a separate counter. Once we hit a gap (or if 0 doesn't exist) start
430  // removing block files.
431  int contiguousCounter = 0;
432  for (const auto &item : mapBlockFiles) {
433  if (atoi(item.first) == contiguousCounter) {
434  contiguousCounter++;
435  continue;
436  }
437  remove(item.second);
438  }
439 }
440 
443 
444  return &m_blockfile_info.at(n);
445 }
446 
447 static bool UndoWriteToDisk(const CBlockUndo &blockundo, FlatFilePos &pos,
448  const BlockHash &hashBlock,
449  const CMessageHeader::MessageMagic &messageStart) {
450  // Open history file to append
452  if (fileout.IsNull()) {
453  return error("%s: OpenUndoFile failed", __func__);
454  }
455 
456  // Write index header
457  unsigned int nSize = GetSerializeSize(blockundo, fileout.GetVersion());
458  fileout << messageStart << nSize;
459 
460  // Write undo data
461  long fileOutPos = ftell(fileout.Get());
462  if (fileOutPos < 0) {
463  return error("%s: ftell failed", __func__);
464  }
465  pos.nPos = (unsigned int)fileOutPos;
466  fileout << blockundo;
467 
468  // calculate & write checksum
470  hasher << hashBlock;
471  hasher << blockundo;
472  fileout << hasher.GetHash();
473 
474  return true;
475 }
476 
477 bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex) {
478  const FlatFilePos pos{WITH_LOCK(::cs_main, return pindex->GetUndoPos())};
479 
480  if (pos.IsNull()) {
481  return error("%s: no undo data available", __func__);
482  }
483 
484  // Open history file to read
485  CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
486  if (filein.IsNull()) {
487  return error("%s: OpenUndoFile failed", __func__);
488  }
489 
490  // Read block
491  uint256 hashChecksum;
492  // We need a CHashVerifier as reserializing may lose data
493  CHashVerifier<CAutoFile> verifier(&filein);
494  try {
495  verifier << pindex->pprev->GetBlockHash();
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());
500  }
501 
502  // Verify checksum
503  if (hashChecksum != verifier.GetHash()) {
504  return error("%s: Checksum mismatch", __func__);
505  }
506 
507  return true;
508 }
509 
510 void BlockManager::FlushUndoFile(int block_file, bool finalize) {
511  FlatFilePos undo_pos_old(block_file,
512  m_blockfile_info[block_file].nUndoSize);
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.");
516  }
517 }
518 
519 void BlockManager::FlushBlockFile(bool fFinalize, bool finalize_undo) {
521 
522  if (m_blockfile_info.empty()) {
523  // Return if we haven't loaded any blockfiles yet. This happens during
524  // chainstate init, when we call
525  // ChainstateManager::MaybeRebalanceCaches() (which then calls
526  // FlushStateToDisk()), resulting in a call to this function before we
527  // have populated `m_blockfile_info` via LoadBlockIndexDB().
528  return;
529  }
530  assert(static_cast<int>(m_blockfile_info.size()) > m_last_blockfile);
531 
532  FlatFilePos block_pos_old(m_last_blockfile,
534  if (!BlockFileSeq().Flush(block_pos_old, fFinalize)) {
535  AbortNode("Flushing block file to disk failed. This is likely the "
536  "result of an I/O error.");
537  }
538  // we do not always flush the undo file, as the chain tip may be lagging
539  // behind the incoming blocks,
540  // e.g. during IBD or a sync after a node going offline
541  if (!fFinalize || finalize_undo) {
542  FlushUndoFile(m_last_blockfile, finalize_undo);
543  }
544 }
545 
548 
549  uint64_t retval = 0;
550  for (const CBlockFileInfo &file : m_blockfile_info) {
551  retval += file.nSize + file.nUndoSize;
552  }
553 
554  return retval;
555 }
556 
557 void UnlinkPrunedFiles(const std::set<int> &setFilesToPrune) {
558  for (const int i : setFilesToPrune) {
559  FlatFilePos pos(i, 0);
560  fs::remove(BlockFileSeq().FileName(pos));
561  fs::remove(UndoFileSeq().FileName(pos));
562  LogPrint(BCLog::BLOCKSTORE, "Prune: %s deleted blk/rev (%05u)\n",
563  __func__, i);
564  }
565 }
566 
568  return FlatFileSeq(gArgs.GetBlocksDirPath(), "blk",
569  gArgs.GetBoolArg("-fastprune", false)
570  ? 0x4000 /* 16kb */
572 }
573 
576 }
577 
578 FILE *OpenBlockFile(const FlatFilePos &pos, bool fReadOnly) {
579  return BlockFileSeq().Open(pos, fReadOnly);
580 }
581 
583 static FILE *OpenUndoFile(const FlatFilePos &pos, bool fReadOnly) {
584  return UndoFileSeq().Open(pos, fReadOnly);
585 }
586 
588  return BlockFileSeq().FileName(pos);
589 }
590 
591 bool BlockManager::FindBlockPos(FlatFilePos &pos, unsigned int nAddSize,
592  unsigned int nHeight, CChain &active_chain,
593  uint64_t nTime, bool fKnown) {
595 
596  unsigned int nFile = fKnown ? pos.nFile : m_last_blockfile;
597  if (m_blockfile_info.size() <= nFile) {
598  m_blockfile_info.resize(nFile + 1);
599  }
600 
601  bool finalize_undo = false;
602  if (!fKnown) {
603  unsigned int max_blockfile_size{MAX_BLOCKFILE_SIZE};
604  // Use smaller blockfiles in test-only -fastprune mode - but avoid
605  // the possibility of having a block not fit into the block file.
606  if (gArgs.GetBoolArg("-fastprune", false)) {
607  max_blockfile_size = 0x10000; // 64kiB
608  if (nAddSize >= max_blockfile_size) {
609  // dynamically adjust the blockfile size to be larger than the
610  // added size
611  max_blockfile_size = nAddSize + 1;
612  }
613  }
614  // TODO: we will also need to dynamically adjust the blockfile size
615  // or raise MAX_BLOCKFILE_SIZE when we reach block sizes larger than
616  // 128 MiB
617  assert(nAddSize < max_blockfile_size);
618  while (m_blockfile_info[nFile].nSize + nAddSize >= max_blockfile_size) {
619  // when the undo file is keeping up with the block file, we want to
620  // flush it explicitly when it is lagging behind (more blocks arrive
621  // than are being connected), we let the undo block write case
622  // handle it
623  finalize_undo = (m_blockfile_info[nFile].nHeightLast ==
624  (unsigned int)active_chain.Tip()->nHeight);
625  nFile++;
626  if (m_blockfile_info.size() <= nFile) {
627  m_blockfile_info.resize(nFile + 1);
628  }
629  }
630  pos.nFile = nFile;
631  pos.nPos = m_blockfile_info[nFile].nSize;
632  }
633 
634  if ((int)nFile != m_last_blockfile) {
635  if (!fKnown) {
636  LogPrint(BCLog::BLOCKSTORE, "Leaving block file %i: %s\n",
639  }
640  FlushBlockFile(!fKnown, finalize_undo);
641  m_last_blockfile = nFile;
642  }
643 
644  m_blockfile_info[nFile].AddBlock(nHeight, nTime);
645  if (fKnown) {
646  m_blockfile_info[nFile].nSize =
647  std::max(pos.nPos + nAddSize, m_blockfile_info[nFile].nSize);
648  } else {
649  m_blockfile_info[nFile].nSize += nAddSize;
650  }
651 
652  if (!fKnown) {
653  bool out_of_space;
654  size_t bytes_allocated =
655  BlockFileSeq().Allocate(pos, nAddSize, out_of_space);
656  if (out_of_space) {
657  return AbortNode("Disk space is too low!",
658  _("Disk space is too low!"));
659  }
660  if (bytes_allocated != 0 && IsPruneMode()) {
661  m_check_for_pruning = true;
662  }
663  }
664 
665  m_dirty_fileinfo.insert(nFile);
666  return true;
667 }
668 
670  FlatFilePos &pos, unsigned int nAddSize) {
671  pos.nFile = nFile;
672 
674 
675  pos.nPos = m_blockfile_info[nFile].nUndoSize;
676  m_blockfile_info[nFile].nUndoSize += nAddSize;
677  m_dirty_fileinfo.insert(nFile);
678 
679  bool out_of_space;
680  size_t bytes_allocated =
681  UndoFileSeq().Allocate(pos, nAddSize, out_of_space);
682  if (out_of_space) {
683  return AbortNode(state, "Disk space is too low!",
684  _("Disk space is too low!"));
685  }
686  if (bytes_allocated != 0 && IsPruneMode()) {
687  m_check_for_pruning = true;
688  }
689 
690  return true;
691 }
692 
693 static bool WriteBlockToDisk(const CBlock &block, FlatFilePos &pos,
694  const CMessageHeader::MessageMagic &messageStart) {
695  // Open history file to append
697  if (fileout.IsNull()) {
698  return error("WriteBlockToDisk: OpenBlockFile failed");
699  }
700 
701  // Write index header
702  unsigned int nSize = GetSerializeSize(block, fileout.GetVersion());
703  fileout << messageStart << nSize;
704 
705  // Write block
706  long fileOutPos = ftell(fileout.Get());
707  if (fileOutPos < 0) {
708  return error("WriteBlockToDisk: ftell failed");
709  }
710 
711  pos.nPos = (unsigned int)fileOutPos;
712  fileout << block;
713 
714  return true;
715 }
716 
717 bool BlockManager::WriteUndoDataForBlock(const CBlockUndo &blockundo,
718  BlockValidationState &state,
719  CBlockIndex &block) {
721  // Write undo information to disk
722  if (block.GetUndoPos().IsNull()) {
723  FlatFilePos _pos;
724  if (!FindUndoPos(state, block.nFile, _pos,
725  ::GetSerializeSize(blockundo, CLIENT_VERSION) + 40)) {
726  return error("ConnectBlock(): FindUndoPos failed");
727  }
728  if (!UndoWriteToDisk(blockundo, _pos, block.pprev->GetBlockHash(),
729  GetParams().DiskMagic())) {
730  return AbortNode(state, "Failed to write undo data");
731  }
732  // rev files are written in block height order, whereas blk files are
733  // written as blocks come in (often out of order) we want to flush the
734  // rev (undo) file once we've written the last block, which is indicated
735  // by the last height in the block file info as below; note that this
736  // does not catch the case where the undo writes are keeping up with the
737  // block writes (usually when a synced up node is getting newly mined
738  // blocks) -- this case is caught in the FindBlockPos function
739  if (_pos.nFile < m_last_blockfile &&
740  static_cast<uint32_t>(block.nHeight) ==
741  m_blockfile_info[_pos.nFile].nHeightLast) {
742  FlushUndoFile(_pos.nFile, true);
743  }
744 
745  // update nUndoPos in block index
746  block.nUndoPos = _pos.nPos;
747  block.nStatus = block.nStatus.withUndo();
748  m_dirty_blockindex.insert(&block);
749  }
750 
751  return true;
752 }
753 
754 bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos,
755  const Consensus::Params &params) {
756  block.SetNull();
757 
758  // Open history file to read
759  CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
760  if (filein.IsNull()) {
761  return error("ReadBlockFromDisk: OpenBlockFile failed for %s",
762  pos.ToString());
763  }
764 
765  // Read block
766  try {
767  filein >> block;
768  } catch (const std::exception &e) {
769  return error("%s: Deserialize or I/O error - %s at %s", __func__,
770  e.what(), pos.ToString());
771  }
772 
773  // Check the header
774  if (!CheckProofOfWork(block.GetHash(), block.nBits, params)) {
775  return error("ReadBlockFromDisk: Errors in block header at %s",
776  pos.ToString());
777  }
778 
779  return true;
780 }
781 
782 bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex,
783  const Consensus::Params &params) {
784  const FlatFilePos block_pos{
785  WITH_LOCK(cs_main, return pindex->GetBlockPos())};
786 
787  if (!ReadBlockFromDisk(block, block_pos, params)) {
788  return false;
789  }
790 
791  if (block.GetHash() != pindex->GetBlockHash()) {
792  return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() "
793  "doesn't match index for %s at %s",
794  pindex->ToString(), block_pos.ToString());
795  }
796 
797  return true;
798 }
799 
801  // Open history file to read
802  CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
803  if (filein.IsNull()) {
804  return error("ReadTxFromDisk: OpenBlockFile failed for %s",
805  pos.ToString());
806  }
807 
808  // Read tx
809  try {
810  filein >> tx;
811  } catch (const std::exception &e) {
812  return error("%s: Deserialize or I/O error - %s at %s", __func__,
813  e.what(), pos.ToString());
814  }
815 
816  return true;
817 }
818 
819 bool ReadTxUndoFromDisk(CTxUndo &tx_undo, const FlatFilePos &pos) {
820  // Open undo file to read
821  CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
822  if (filein.IsNull()) {
823  return error("ReadTxUndoFromDisk: OpenUndoFile failed for %s",
824  pos.ToString());
825  }
826 
827  // Read undo data
828  try {
829  filein >> tx_undo;
830  } catch (const std::exception &e) {
831  return error("%s: Deserialize or I/O error - %s at %s", __func__,
832  e.what(), pos.ToString());
833  }
834 
835  return true;
836 }
837 
839  CChain &active_chain,
840  const FlatFilePos *dbp) {
841  unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION);
842  FlatFilePos blockPos;
843  const auto position_known{dbp != nullptr};
844  if (position_known) {
845  blockPos = *dbp;
846  } else {
847  // When known, blockPos.nPos points at the offset of the block data in
848  // the blk file. That already accounts for the serialization header
849  // present in the file (the 4 magic message start bytes + the 4 length
850  // bytes = 8 bytes = BLOCK_SERIALIZATION_HEADER_SIZE). We add
851  // BLOCK_SERIALIZATION_HEADER_SIZE only for new blocks since they will
852  // have the serialization header added when written to disk.
853  nBlockSize +=
854  static_cast<unsigned int>(BLOCK_SERIALIZATION_HEADER_SIZE);
855  }
856  if (!FindBlockPos(blockPos, nBlockSize, nHeight, active_chain,
857  block.GetBlockTime(), position_known)) {
858  error("%s: FindBlockPos failed", __func__);
859  return FlatFilePos();
860  }
861  if (!position_known) {
862  if (!WriteBlockToDisk(block, blockPos, GetParams().DiskMagic())) {
863  AbortNode("Failed to write block");
864  return FlatFilePos();
865  }
866  }
867  return blockPos;
868 }
869 
871  std::atomic<bool> &m_importing;
872 
873 public:
874  ImportingNow(std::atomic<bool> &importing) : m_importing{importing} {
875  assert(m_importing == false);
876  m_importing = true;
877  }
879  assert(m_importing == true);
880  m_importing = false;
881  }
882 };
883 
885  std::vector<fs::path> vImportFiles, const ArgsManager &args,
886  const fs::path &mempool_path) {
888 
889  {
890  ImportingNow imp{chainman.m_blockman.m_importing};
891 
892  // -reindex
893  if (fReindex) {
894  int nFile = 0;
895  // Map of disk positions for blocks with unknown parent (only used
896  // for reindex); parent hash -> child disk position, multiple
897  // children can have the same parent.
898  std::multimap<BlockHash, FlatFilePos> blocks_with_unknown_parent;
899  while (true) {
900  FlatFilePos pos(nFile, 0);
901  if (!fs::exists(GetBlockPosFilename(pos))) {
902  // No block files left to reindex
903  break;
904  }
905  FILE *file = OpenBlockFile(pos, true);
906  if (!file) {
907  // This error is logged in OpenBlockFile
908  break;
909  }
910  LogPrintf("Reindexing block file blk%05u.dat...\n",
911  (unsigned int)nFile);
912  chainman.ActiveChainstate().LoadExternalBlockFile(
913  file, &pos, &blocks_with_unknown_parent);
914  if (ShutdownRequested()) {
915  LogPrintf("Shutdown requested. Exit %s\n", __func__);
916  return;
917  }
918  nFile++;
919  }
920  WITH_LOCK(
921  ::cs_main,
922  chainman.m_blockman.m_block_tree_db->WriteReindexing(false));
923  fReindex = false;
924  LogPrintf("Reindexing finished\n");
925  // To avoid ending up in a situation without genesis block, re-try
926  // initializing (no-op if reindexing worked):
927  chainman.ActiveChainstate().LoadGenesisBlock();
928  }
929 
930  // -loadblock=
931  for (const fs::path &path : vImportFiles) {
932  FILE *file = fsbridge::fopen(path, "rb");
933  if (file) {
934  LogPrintf("Importing blocks file %s...\n",
935  fs::PathToString(path));
936  chainman.ActiveChainstate().LoadExternalBlockFile(file);
937  if (ShutdownRequested()) {
938  LogPrintf("Shutdown requested. Exit %s\n", __func__);
939  return;
940  }
941  } else {
942  LogPrintf("Warning: Could not open blocks file %s\n",
943  fs::PathToString(path));
944  }
945  }
946 
947  // Reconsider blocks we know are valid. They may have been marked
948  // invalid by, for instance, running an outdated version of the node
949  // software.
950  const MapCheckpoints &checkpoints =
951  chainman.GetParams().Checkpoints().mapCheckpoints;
952  for (const MapCheckpoints::value_type &i : checkpoints) {
953  const BlockHash &hash = i.second;
954 
955  LOCK(cs_main);
956  CBlockIndex *pblockindex =
957  chainman.m_blockman.LookupBlockIndex(hash);
958  if (pblockindex && !pblockindex->nStatus.isValid()) {
959  LogPrintf("Reconsidering checkpointed block %s ...\n",
960  hash.GetHex());
961  chainman.ActiveChainstate().ResetBlockFailureFlags(pblockindex);
962  }
963 
964  if (pblockindex && pblockindex->nStatus.isOnParkedChain()) {
965  LogPrintf("Unparking checkpointed block %s ...\n",
966  hash.GetHex());
967  chainman.ActiveChainstate().UnparkBlockAndChildren(pblockindex);
968  }
969  }
970 
971  // scan for better chains in the block chain database, that are not yet
972  // connected in the active best chain
973 
974  // We can't hold cs_main during ActivateBestChain even though we're
975  // accessing the chainman unique_ptrs since ABC requires us not to be
976  // holding cs_main, so retrieve the relevant pointers before the ABC
977  // call.
978  for (Chainstate *chainstate :
979  WITH_LOCK(::cs_main, return chainman.GetAll())) {
980  BlockValidationState state;
981  if (!chainstate->ActivateBestChain(state, nullptr)) {
982  LogPrintf("Failed to connect best block (%s)\n",
983  state.ToString());
984  StartShutdown();
985  return;
986  }
987  }
988 
989  if (args.GetBoolArg("-stopafterblockimport",
991  LogPrintf("Stopping after block import\n");
992  StartShutdown();
993  return;
994  }
995  } // End scope of ImportingNow
996  chainman.ActiveChainstate().LoadMempool(mempool_path);
997 }
998 } // namespace node
@ TREE
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:78
std::map< int, BlockHash > MapCheckpoints
Definition: chainparams.h:25
const fs::path & GetBlocksDirPath() const
Get blocks directory path.
Definition: system.cpp:410
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:665
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:570
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:567
int GetVersion() const
Definition: streams.h:640
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:23
BlockHash GetHash() const
Definition: block.cpp:11
uint32_t nBits
Definition: block.h:30
BlockHash hashPrevBlock
Definition: block.h:27
int64_t GetBlockTime() const
Definition: block.h:57
Definition: block.h:60
void SetNull()
Definition: block.h:80
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:26
uint64_t nTimeReceived
(memory only) block header metadata
Definition: blockindex.h:102
std::string ToString() const
Definition: blockindex.h:205
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:33
void BuildSkip()
Build the skiplist pointer for this entry.
Definition: blockindex.cpp:76
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: blockindex.h:52
const BlockHash * phashBlock
pointer to the hash of the block, if any.
Definition: blockindex.h:30
uint32_t nTime
Definition: blockindex.h:93
unsigned int nTimeMax
(memory only) Maximum nTime in the chain up to and including this block.
Definition: blockindex.h:105
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
Definition: blockindex.h:99
FlatFilePos GetUndoPos() const EXCLUSIVE_LOCKS_REQUIRED(
Definition: blockindex.h:124
unsigned int nTx
Number of transactions in this block.
Definition: blockindex.h:61
bool RaiseValidity(enum BlockValidity nUpTo) EXCLUSIVE_LOCKS_REQUIRED(
Raise the validity level of this block index entry.
Definition: blockindex.h:228
BlockHash GetBlockHash() const
Definition: blockindex.h:147
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:39
FlatFilePos GetBlockPos() const EXCLUSIVE_LOCKS_REQUIRED(
Definition: blockindex.h:114
Undo information for a CBlock.
Definition: undo.h:73
An in-memory indexed chain of blocks.
Definition: chain.h:140
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:156
const CCheckpointData & Checkpoints() const
Definition: chainparams.h:128
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:160
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:99
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
Definition: hash.h:122
std::array< uint8_t, MESSAGE_START_SIZE > MessageMagic
Definition: protocol.h:46
A mutable version of CTransaction.
Definition: transaction.h:274
Restore the UTXO in a Coin at a given COutPoint.
Definition: undo.h:62
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:629
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:1144
const CChainParams & GetParams() const
Definition: validation.h:1238
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
Definition: validation.h:1318
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:1273
FlatFileSeq represents a sequence of numbered files storing raw data.
Definition: flatfile.h:49
fs::path FileName(const FlatFilePos &pos) const
Get the name of the file at the given position.
Definition: flatfile.cpp:24
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:51
FILE * Open(const FlatFilePos &pos, bool read_only=false)
Open a handle to the file at the given position.
Definition: flatfile.cpp:28
std::string ToString() const
Definition: validation.h:118
bool IsNull() const
Definition: uint256.h:32
std::string GetHex() const
Definition: uint256.cpp:16
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
std::set< int > m_dirty_fileinfo
Dirty block file entries.
Definition: blockstorage.h:138
RecursiveMutex cs_LastBlockFile
Definition: blockstorage.h:122
const Consensus::Params & GetConsensus() const
Definition: blockstorage.h:73
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.
Definition: blockstorage.h:194
uint64_t GetPruneTarget() const
Attempt to stay below this number of bytes of block files.
Definition: blockstorage.h:201
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.
Definition: blockstorage.h:135
bool m_check_for_pruning
Global flag to indicate we should check to see if there are block/undo files that should be deleted.
Definition: blockstorage.h:130
const CChainParams & GetParams() const
Definition: blockstorage.h:72
bool FindUndoPos(BlockValidationState &state, int nFile, FlatFilePos &pos, unsigned int nAddSize)
bool IsPruneMode() const
Whether running in -prune mode.
Definition: blockstorage.h:198
std::atomic< bool > m_importing
Definition: blockstorage.h:148
std::vector< CBlockFileInfo > m_blockfile_info
Definition: blockstorage.h:123
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.
Definition: blockstorage.h:219
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.
Definition: blockstorage.h:152
void FlushBlockFile(bool fFinalize=false, bool finalize_undo=false)
ImportingNow(std::atomic< bool > &importing)
std::atomic< bool > & m_importing
256-bit opaque blob.
Definition: uint256.h:129
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:38
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
#define LogPrint(category,...)
Definition: logging.h:210
#define LogPrintf(...)
Definition: logging.h:206
unsigned int nHeight
@ PRUNE
Definition: logging.h:54
@ BLOCKSTORE
Definition: logging.h:68
static bool exists(const path &p)
Definition: fs.h:102
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:142
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:28
Definition: init.h:28
const CBlockIndex *GetFirstStoredBlock(const CBlockIndex *start_block) EXCLUSIVE_LOCKS_REQUIRED(voi CleanupBlockRevFiles)()
Find the first block that is not pruned.
Definition: blockstorage.h:231
static const unsigned int UNDOFILE_CHUNK_SIZE
The pre-allocation chunk size for rev?????.dat files (since 0.8)
Definition: blockstorage.h:44
const CBlockIndex * GetFirstStoredBlock(const CBlockIndex *start_block)
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &params)
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
Definition: blockstorage.h:39
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)
Definition: blockstorage.h:42
static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE
Size of header written by WriteBlockToDisk before a serialized CBlock.
Definition: blockstorage.h:49
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)
Definition: blockstorage.h:46
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 &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:91
reverse_range< T > reverse_iterate(T &x)
@ SER_DISK
Definition: serialize.h:153
@ SER_GETHASH
Definition: serialize.h:154
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1258
bool AbortNode(const std::string &strMessage, bilingual_str user_message)
Abort with a message.
Definition: shutdown.cpp:20
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
Definition: shutdown.cpp:85
void StartShutdown()
Request shutdown of the application.
Definition: shutdown.cpp:55
int atoi(const std::string &str)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:87
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
MapCheckpoints mapCheckpoints
Definition: chainparams.h:28
Parameters that influence chain consensus.
Definition: params.h:34
int nFile
Definition: flatfile.h:15
std::string ToString() const
Definition: flatfile.cpp:20
unsigned int nPos
Definition: flatfile.h:16
bool IsNull() const
Definition: flatfile.h:40
#define LOCK2(cs1, cs2)
Definition: sync.h:309
#define LOCK(cs)
Definition: sync.h:306
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:357
void ScheduleBatchPriority()
On platforms that support it, tell the kernel the calling thread is CPU-intensive and non-interactive...
Definition: system.cpp:1461
ArgsManager gArgs
Definition: system.cpp:80
bool error(const char *fmt, const Args &...args)
Definition: system.h:45
static int count
Definition: tests.c:31
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
int64_t GetTime()
Definition: time.cpp:109
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())
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...
Definition: validation.h:95
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11