mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-12 12:52:35 -03:00
Decouple CBlockUndo from CDiskBlockPos
This commit is contained in:
parent
d7621ccf9d
commit
e035c6a737
2 changed files with 16 additions and 18 deletions
31
src/main.cpp
31
src/main.cpp
|
@ -1520,7 +1520,8 @@ bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsVi
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean)
|
||||||
{
|
{
|
||||||
|
@ -1535,7 +1536,7 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex
|
||||||
CDiskBlockPos pos = pindex->GetUndoPos();
|
CDiskBlockPos pos = pindex->GetUndoPos();
|
||||||
if (pos.IsNull())
|
if (pos.IsNull())
|
||||||
return error("DisconnectBlock() : no undo data available");
|
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");
|
return error("DisconnectBlock() : failure reading undo data");
|
||||||
|
|
||||||
if (blockUndo.vtxundo.size() + 1 != block.vtx.size())
|
if (blockUndo.vtxundo.size() + 1 != block.vtx.size())
|
||||||
|
@ -1777,7 +1778,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
|
||||||
CDiskBlockPos pos;
|
CDiskBlockPos pos;
|
||||||
if (!FindUndoPos(state, pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
|
if (!FindUndoPos(state, pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
|
||||||
return error("ConnectBlock() : FindUndoPos failed");
|
return error("ConnectBlock() : FindUndoPos failed");
|
||||||
if (!blockundo.WriteToDisk(pos, pindex->pprev->GetBlockHash()))
|
if (!UndoWriteToDisk(blockundo, pos, pindex->pprev->GetBlockHash()))
|
||||||
return state.Abort("Failed to write undo data");
|
return state.Abort("Failed to write undo data");
|
||||||
|
|
||||||
// update nUndoPos in block index
|
// update nUndoPos in block index
|
||||||
|
@ -2943,7 +2944,7 @@ bool CVerifyDB::VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth
|
||||||
CBlockUndo undo;
|
CBlockUndo undo;
|
||||||
CDiskBlockPos pos = pindex->GetUndoPos();
|
CDiskBlockPos pos = pindex->GetUndoPos();
|
||||||
if (!pos.IsNull()) {
|
if (!pos.IsNull()) {
|
||||||
if (!undo.ReadFromDisk(pos, pindex->pprev->GetBlockHash()))
|
if (!UndoReadFromDisk(undo, pos, pindex->pprev->GetBlockHash()))
|
||||||
return error("VerifyDB() : *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
|
return error("VerifyDB() : *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4509,44 +4510,44 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool CBlockUndo::WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock)
|
bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock)
|
||||||
{
|
{
|
||||||
// Open history file to append
|
// Open history file to append
|
||||||
CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
|
CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
|
||||||
if (fileout.IsNull())
|
if (fileout.IsNull())
|
||||||
return error("CBlockUndo::WriteToDisk : OpenUndoFile failed");
|
return error("%s : OpenUndoFile failed", __func__);
|
||||||
|
|
||||||
// Write index header
|
// Write index header
|
||||||
unsigned int nSize = fileout.GetSerializeSize(*this);
|
unsigned int nSize = fileout.GetSerializeSize(blockundo);
|
||||||
fileout << FLATDATA(Params().MessageStart()) << nSize;
|
fileout << FLATDATA(Params().MessageStart()) << nSize;
|
||||||
|
|
||||||
// Write undo data
|
// Write undo data
|
||||||
long fileOutPos = ftell(fileout.Get());
|
long fileOutPos = ftell(fileout.Get());
|
||||||
if (fileOutPos < 0)
|
if (fileOutPos < 0)
|
||||||
return error("CBlockUndo::WriteToDisk : ftell failed");
|
return error("%s : ftell failed", __func__);
|
||||||
pos.nPos = (unsigned int)fileOutPos;
|
pos.nPos = (unsigned int)fileOutPos;
|
||||||
fileout << *this;
|
fileout << blockundo;
|
||||||
|
|
||||||
// calculate & write checksum
|
// calculate & write checksum
|
||||||
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
|
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
|
||||||
hasher << hashBlock;
|
hasher << hashBlock;
|
||||||
hasher << *this;
|
hasher << blockundo;
|
||||||
fileout << hasher.GetHash();
|
fileout << hasher.GetHash();
|
||||||
|
|
||||||
return true;
|
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
|
// 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("CBlockUndo::ReadFromDisk : OpenBlockFile failed");
|
return error("%s : OpenBlockFile failed", __func__);
|
||||||
|
|
||||||
// Read block
|
// Read block
|
||||||
uint256 hashChecksum;
|
uint256 hashChecksum;
|
||||||
try {
|
try {
|
||||||
filein >> *this;
|
filein >> blockundo;
|
||||||
filein >> hashChecksum;
|
filein >> hashChecksum;
|
||||||
}
|
}
|
||||||
catch (const std::exception& e) {
|
catch (const std::exception& e) {
|
||||||
|
@ -4556,9 +4557,9 @@ bool CBlockUndo::ReadFromDisk(const CDiskBlockPos &pos, const uint256 &hashBlock
|
||||||
// Verify checksum
|
// Verify checksum
|
||||||
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
|
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
|
||||||
hasher << hashBlock;
|
hasher << hashBlock;
|
||||||
hasher << *this;
|
hasher << blockundo;
|
||||||
if (hashChecksum != hasher.GetHash())
|
if (hashChecksum != hasher.GetHash())
|
||||||
return error("CBlockUndo::ReadFromDisk : Checksum mismatch");
|
return error("%s : Checksum mismatch", __func__);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -314,9 +314,6 @@ public:
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
||||||
READWRITE(vtxundo);
|
READWRITE(vtxundo);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock);
|
|
||||||
bool ReadFromDisk(const CDiskBlockPos &pos, const uint256 &hashBlock);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue