Bitcoin ABC  0.24.7
P2P Digital Currency
blockdb.cpp
Go to the documentation of this file.
1 #include <blockdb.h>
2 
3 #include <blockindex.h>
4 #include <clientversion.h>
5 #include <pow/pow.h>
6 #include <primitives/block.h>
7 #include <streams.h>
8 #include <util/system.h>
9 
10 extern RecursiveMutex cs_main;
11 
14 }
15 
18 }
19 
20 FILE *OpenBlockFile(const FlatFilePos &pos, bool fReadOnly) {
21  return BlockFileSeq().Open(pos, fReadOnly);
22 }
23 
25 FILE *OpenUndoFile(const FlatFilePos &pos, bool fReadOnly) {
26  return UndoFileSeq().Open(pos, fReadOnly);
27 }
28 
29 fs::path GetBlockPosFilename(const FlatFilePos &pos) {
30  return BlockFileSeq().FileName(pos);
31 }
32 
33 bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos,
34  const Consensus::Params &params) {
35  block.SetNull();
36 
37  // Open history file to read
38  CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
39  if (filein.IsNull()) {
40  return error("ReadBlockFromDisk: OpenBlockFile failed for %s",
41  pos.ToString());
42  }
43 
44  // Read block
45  try {
46  filein >> block;
47  } catch (const std::exception &e) {
48  return error("%s: Deserialize or I/O error - %s at %s", __func__,
49  e.what(), pos.ToString());
50  }
51 
52  // Check the header
53  if (!CheckProofOfWork(block.GetHash(), block.nBits, params)) {
54  return error("ReadBlockFromDisk: Errors in block header at %s",
55  pos.ToString());
56  }
57 
58  return true;
59 }
60 
61 bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex,
62  const Consensus::Params &params) {
63  FlatFilePos blockPos;
64  {
65  LOCK(cs_main);
66  blockPos = pindex->GetBlockPos();
67  }
68 
69  if (!ReadBlockFromDisk(block, blockPos, params)) {
70  return false;
71  }
72 
73  if (block.GetHash() != pindex->GetBlockHash()) {
74  return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() "
75  "doesn't match index for %s at %s",
76  pindex->ToString(), pindex->GetBlockPos().ToString());
77  }
78 
79  return true;
80 }
OpenBlockFile
FILE * OpenBlockFile(const FlatFilePos &pos, bool fReadOnly)
Open a block file (blk?????.dat).
Definition: blockdb.cpp:20
block.h
UndoFileSeq
FlatFileSeq UndoFileSeq()
Definition: blockdb.cpp:16
pow.h
CBlockHeader::nBits
uint32_t nBits
Definition: block.h:29
OpenUndoFile
FILE * OpenUndoFile(const FlatFilePos &pos, bool fReadOnly)
Open an undo file (rev?????.dat)
Definition: blockdb.cpp:25
streams.h
FlatFilePos::ToString
std::string ToString() const
Definition: flatfile.cpp:20
CBlockIndex::ToString
std::string ToString() const
Definition: blockindex.h:187
AnnotatedMixin< std::recursive_mutex >
clientversion.h
BLOCKFILE_CHUNK_SIZE
static constexpr unsigned int BLOCKFILE_CHUNK_SIZE
The pre-allocation chunk size for blk?????.dat files (since 0.8)
Definition: blockdb.h:18
CBlockIndex::GetBlockHash
BlockHash GetBlockHash() const
Definition: blockindex.h:133
CBlockIndex::GetBlockPos
FlatFilePos GetBlockPos() const
Definition: blockindex.h:102
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:581
FlatFileSeq
FlatFileSeq represents a sequence of numbered files storing raw data.
Definition: flatfile.h:49
Consensus::Params
Parameters that influence chain consensus.
Definition: params.h:59
blockdb.h
CheckProofOfWork
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:45
FlatFileSeq::Open
FILE * Open(const FlatFilePos &pos, bool read_only=false)
Open a handle to the file at the given position.
Definition: flatfile.cpp:28
cs_main
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:103
FlatFilePos
Definition: flatfile.h:14
BlockFileSeq
FlatFileSeq BlockFileSeq()
Definition: blockdb.cpp:12
GetBlockPosFilename
fs::path GetBlockPosFilename(const FlatFilePos &pos)
Translation to a filesystem path.
Definition: blockdb.cpp:29
ReadBlockFromDisk
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &params)
Functions for disk access for blocks.
Definition: blockdb.cpp:33
CAutoFile::IsNull
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:627
system.h
CBlock
Definition: block.h:55
CLIENT_VERSION
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:44
FlatFileSeq::FileName
fs::path FileName(const FlatFilePos &pos) const
Get the name of the file at the given position.
Definition: flatfile.cpp:24
LOCK
#define LOCK(cs)
Definition: sync.h:241
blockindex.h
GetBlocksDir
const fs::path & GetBlocksDir()
Definition: system.cpp:753
SER_DISK
@ SER_DISK
Definition: serialize.h:166
UNDOFILE_CHUNK_SIZE
static const unsigned int UNDOFILE_CHUNK_SIZE
The pre-allocation chunk size for rev?????.dat files (since 0.8)
Definition: blockdb.h:20
CBlock::SetNull
void SetNull()
Definition: block.h:75
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:23
CBlockHeader::GetHash
BlockHash GetHash() const
Definition: block.cpp:11