Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
Show First 20 Lines • Show All 1,288 Lines • ▼ Show 20 Lines | bool UndoWriteToDisk(const CBlockUndo &blockundo, CDiskBlockPos &pos, | ||||
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION); | CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION); | ||||
hasher << hashBlock; | hasher << hashBlock; | ||||
hasher << blockundo; | hasher << blockundo; | ||||
fileout << hasher.GetHash(); | fileout << hasher.GetHash(); | ||||
return true; | return true; | ||||
} | } | ||||
bool UndoReadFromDisk(CBlockUndo &blockundo, const CDiskBlockPos &pos, | static bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex) { | ||||
const uint256 &hashBlock) { | CDiskBlockPos pos = pindex->GetUndoPos(); | ||||
if (pos.IsNull()) { | |||||
return error("%s: no undo data available", __func__); | |||||
} | |||||
// Open history file to read | // Open history file to read | ||||
CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION); | CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION); | ||||
if (filein.IsNull()) { | if (filein.IsNull()) { | ||||
return error("%s: OpenUndoFile failed", __func__); | return error("%s: OpenUndoFile failed", __func__); | ||||
} | } | ||||
// Read block | // Read block | ||||
uint256 hashChecksum; | uint256 hashChecksum; | ||||
// We need a CHashVerifier as reserializing may lose data | // We need a CHashVerifier as reserializing may lose data | ||||
CHashVerifier<CAutoFile> verifier(&filein); | CHashVerifier<CAutoFile> verifier(&filein); | ||||
try { | try { | ||||
verifier << hashBlock; | verifier << pindex->pprev->GetBlockHash(); | ||||
verifier >> blockundo; | verifier >> blockundo; | ||||
filein >> hashChecksum; | filein >> hashChecksum; | ||||
} catch (const std::exception &e) { | } catch (const std::exception &e) { | ||||
return error("%s: Deserialize or I/O error - %s", __func__, e.what()); | return error("%s: Deserialize or I/O error - %s", __func__, e.what()); | ||||
} | } | ||||
// Verify checksum | // Verify checksum | ||||
if (hashChecksum != verifier.GetHash()) { | if (hashChecksum != verifier.GetHash()) { | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Undo the effects of this block (with given index) on the UTXO set represented | * Undo the effects of this block (with given index) on the UTXO set represented | ||||
* by coins. When FAILED is returned, view is left in an indeterminate state. | * by coins. When FAILED is returned, view is left in an indeterminate state. | ||||
*/ | */ | ||||
static DisconnectResult DisconnectBlock(const CBlock &block, | static DisconnectResult DisconnectBlock(const CBlock &block, | ||||
const CBlockIndex *pindex, | const CBlockIndex *pindex, | ||||
CCoinsViewCache &view) { | CCoinsViewCache &view) { | ||||
CBlockUndo blockUndo; | CBlockUndo blockUndo; | ||||
CDiskBlockPos pos = pindex->GetUndoPos(); | if (!UndoReadFromDisk(blockUndo, pindex)) { | ||||
if (pos.IsNull()) { | |||||
error("DisconnectBlock(): no undo data available"); | |||||
return DISCONNECT_FAILED; | |||||
} | |||||
if (!UndoReadFromDisk(blockUndo, pos, pindex->pprev->GetBlockHash())) { | |||||
error("DisconnectBlock(): failure reading undo data"); | error("DisconnectBlock(): failure reading undo data"); | ||||
return DISCONNECT_FAILED; | return DISCONNECT_FAILED; | ||||
} | } | ||||
return ApplyBlockUndo(blockUndo, block, pindex, view); | return ApplyBlockUndo(blockUndo, block, pindex, view); | ||||
} | } | ||||
DisconnectResult ApplyBlockUndo(const CBlockUndo &blockUndo, | DisconnectResult ApplyBlockUndo(const CBlockUndo &blockUndo, | ||||
▲ Show 20 Lines • Show All 3,162 Lines • ▼ Show 20 Lines | for (CBlockIndex *pindex = chainActive.Tip(); pindex && pindex->pprev; | ||||
__func__, pindex->nHeight, | __func__, pindex->nHeight, | ||||
pindex->GetBlockHash().ToString(), | pindex->GetBlockHash().ToString(), | ||||
FormatStateMessage(state)); | FormatStateMessage(state)); | ||||
} | } | ||||
// check level 2: verify undo validity | // check level 2: verify undo validity | ||||
if (nCheckLevel >= 2 && pindex) { | if (nCheckLevel >= 2 && pindex) { | ||||
CBlockUndo undo; | CBlockUndo undo; | ||||
CDiskBlockPos pos = pindex->GetUndoPos(); | if (!pindex->GetUndoPos().IsNull()) { | ||||
if (!pos.IsNull()) { | if (!UndoReadFromDisk(undo, pindex)) { | ||||
if (!UndoReadFromDisk(undo, pos, | |||||
pindex->pprev->GetBlockHash())) { | |||||
return error( | return error( | ||||
"VerifyDB(): *** found bad undo data at %d, hash=%s\n", | "VerifyDB(): *** found bad undo data at %d, hash=%s\n", | ||||
pindex->nHeight, pindex->GetBlockHash().ToString()); | pindex->nHeight, pindex->GetBlockHash().ToString()); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
// check level 3: check for inconsistencies during memory-only | // check level 3: check for inconsistencies during memory-only | ||||
▲ Show 20 Lines • Show All 991 Lines • Show Last 20 Lines |