Changeset View
Changeset View
Standalone View
Standalone View
src/blockdb.cpp
Show All 30 Lines | |||||
/** Open an undo file (rev?????.dat) */ | /** Open an undo file (rev?????.dat) */ | ||||
FILE *OpenUndoFile(const FlatFilePos &pos, bool fReadOnly) { | FILE *OpenUndoFile(const FlatFilePos &pos, bool fReadOnly) { | ||||
return UndoFileSeq().Open(pos, fReadOnly); | return UndoFileSeq().Open(pos, fReadOnly); | ||||
} | } | ||||
fs::path GetBlockPosFilename(const FlatFilePos &pos) { | fs::path GetBlockPosFilename(const FlatFilePos &pos) { | ||||
return BlockFileSeq().FileName(pos); | return BlockFileSeq().FileName(pos); | ||||
} | } | ||||
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, | |||||
const Consensus::Params ¶ms) { | |||||
block.SetNull(); | |||||
// Open history file to read | |||||
CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION); | |||||
if (filein.IsNull()) { | |||||
return error("ReadBlockFromDisk: OpenBlockFile failed for %s", | |||||
pos.ToString()); | |||||
} | |||||
// Read block | |||||
try { | |||||
filein >> block; | |||||
} catch (const std::exception &e) { | |||||
return error("%s: Deserialize or I/O error - %s at %s", __func__, | |||||
e.what(), pos.ToString()); | |||||
} | |||||
// Check the header | |||||
if (!CheckProofOfWork(block.GetHash(), block.nBits, params)) { | |||||
return error("ReadBlockFromDisk: Errors in block header at %s", | |||||
pos.ToString()); | |||||
} | |||||
return true; | |||||
} | |||||
bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex, | |||||
const Consensus::Params ¶ms) { | |||||
FlatFilePos blockPos; | |||||
{ | |||||
LOCK(cs_main); | |||||
blockPos = pindex->GetBlockPos(); | |||||
} | |||||
if (!ReadBlockFromDisk(block, blockPos, params)) { | |||||
return false; | |||||
} | |||||
if (block.GetHash() != pindex->GetBlockHash()) { | |||||
return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() " | |||||
"doesn't match index for %s at %s", | |||||
pindex->ToString(), pindex->GetBlockPos().ToString()); | |||||
} | |||||
return true; | |||||
} |