Skip to content

Commit

Permalink
Decouple CBlockUndo from CDiskBlockPos
Browse files Browse the repository at this point in the history
  • Loading branch information
jtimon authored and random-zebra committed Jul 15, 2020
1 parent 9703b0e commit 4fe609f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
31 changes: 17 additions & 14 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,9 @@ static bool AbortNode(CValidationState& state, const std::string& strMessage, co
return state.Error(strMessage);
}

bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock);
bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock);

bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean)
{
AssertLockHeld(cs_main);
Expand All @@ -1952,7 +1955,7 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex
CDiskBlockPos pos = pindex->GetUndoPos();
if (pos.IsNull())
return error("DisconnectBlock() : no undo data available");
if (!blockUndo.ReadFromDisk(pos, pindex->pprev->GetBlockHash()))
if (!UndoReadFromDisk(blockUndo, pos, pindex->pprev->GetBlockHash()))
return error("DisconnectBlock() : failure reading undo data");

if (blockUndo.vtxundo.size() + 1 != block.vtx.size())
Expand Down Expand Up @@ -2320,7 +2323,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
CDiskBlockPos diskPosBlock;
if (!FindUndoPos(state, pindex->nFile, diskPosBlock, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
return error("ConnectBlock() : FindUndoPos failed");
if (!blockundo.WriteToDisk(diskPosBlock, pindex->pprev->GetBlockHash()))
if (!UndoWriteToDisk(blockundo, diskPosBlock, pindex->pprev->GetBlockHash()))
return AbortNode(state, "Failed to write undo data");

// update nUndoPos in block index
Expand Down Expand Up @@ -4351,7 +4354,7 @@ bool CVerifyDB::VerifyDB(CCoinsView* coinsview, int nCheckLevel, int nCheckDepth
CBlockUndo undo;
CDiskBlockPos pos = pindex->GetUndoPos();
if (!pos.IsNull()) {
if (!undo.ReadFromDisk(pos, pindex->pprev->GetBlockHash()))
if (!UndoReadFromDisk(undo, pos, pindex->pprev->GetBlockHash()))
return error("%s: *** found bad undo data at %d, hash=%s\n", __func__, pindex->nHeight, pindex->GetBlockHash().ToString());
}
}
Expand Down Expand Up @@ -6218,44 +6221,44 @@ bool SendMessages(CNode* pto)
}


bool CBlockUndo::WriteToDisk(CDiskBlockPos& pos, const uint256& hashBlock)
bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock)
{
// Open history file to append
CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
if (fileout.IsNull())
return error("CBlockUndo::WriteToDisk : OpenUndoFile failed");
return error("%s : OpenUndoFile failed", __func__);

// Write index header
unsigned int nSize = GetSerializeSize(fileout, *this);
unsigned int nSize = GetSerializeSize(fileout, blockundo);
fileout << FLATDATA(Params().MessageStart()) << nSize;

// Write undo data
long fileOutPos = ftell(fileout.Get());
if (fileOutPos < 0)
return error("CBlockUndo::WriteToDisk : ftell failed");
return error("%s : ftell failed", __func__);
pos.nPos = (unsigned int)fileOutPos;
fileout << *this;
fileout << blockundo;

// calculate & write checksum
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
hasher << hashBlock;
hasher << *this;
hasher << blockundo;
fileout << hasher.GetHash();

return true;
}

bool CBlockUndo::ReadFromDisk(const CDiskBlockPos& pos, const uint256& hashBlock)
bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock)
{
// Open history file to read
CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
if (filein.IsNull())
return error("CBlockUndo::ReadFromDisk : OpenBlockFile failed");
return error("%s : OpenBlockFile failed", __func__);

// Read block
uint256 hashChecksum;
try {
filein >> *this;
filein >> blockundo;
filein >> hashChecksum;
} catch (const std::exception& e) {
return error("%s : Deserialize or I/O error - %s", __func__, e.what());
Expand All @@ -6264,9 +6267,9 @@ bool CBlockUndo::ReadFromDisk(const CDiskBlockPos& pos, const uint256& hashBlock
// Verify checksum
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
hasher << hashBlock;
hasher << *this;
hasher << blockundo;
if (hashChecksum != hasher.GetHash())
return error("CBlockUndo::ReadFromDisk : Checksum mismatch");
return error("%s : Checksum mismatch", __func__);

return true;
}
Expand Down
3 changes: 0 additions & 3 deletions src/undo.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ class CBlockUndo
{
READWRITE(vtxundo);
}

bool WriteToDisk(CDiskBlockPos& pos, const uint256& hashBlock);
bool ReadFromDisk(const CDiskBlockPos& pos, const uint256& hashBlock);
};

#endif // BITCOIN_UNDO_H

0 comments on commit 4fe609f

Please sign in to comment.