Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
Show First 20 Lines • Show All 1,103 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
////////////////////////////////////////////////////////////////////////////// | ////////////////////////////////////////////////////////////////////////////// | ||||
// | // | ||||
// CBlock and CBlockIndex | // CBlock and CBlockIndex | ||||
// | // | ||||
bool WriteBlockToDisk(const CBlock &block, CDiskBlockPos &pos, | bool WriteBlockToDisk(const CBlock &block, CDiskBlockPos &pos, | ||||
const CMessageHeader::MessageStartChars &messageStart) { | const CMessageHeader::MessageMagic &messageStart) { | ||||
// Open history file to append | // Open history file to append | ||||
CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION); | CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION); | ||||
if (fileout.IsNull()) | if (fileout.IsNull()) | ||||
return error("WriteBlockToDisk: OpenBlockFile failed"); | return error("WriteBlockToDisk: OpenBlockFile failed"); | ||||
// Write index header | // Write index header | ||||
unsigned int nSize = GetSerializeSize(fileout, block); | unsigned int nSize = GetSerializeSize(fileout, block); | ||||
fileout << FLATDATA(messageStart) << nSize; | fileout << FLATDATA(messageStart) << nSize; | ||||
▲ Show 20 Lines • Show All 393 Lines • ▼ Show 20 Lines | bool CheckInputs(const CTransaction &tx, CValidationState &state, | ||||
return true; | return true; | ||||
} | } | ||||
namespace { | namespace { | ||||
bool UndoWriteToDisk(const CBlockUndo &blockundo, CDiskBlockPos &pos, | bool UndoWriteToDisk(const CBlockUndo &blockundo, CDiskBlockPos &pos, | ||||
const uint256 &hashBlock, | const uint256 &hashBlock, | ||||
const CMessageHeader::MessageStartChars &messageStart) { | const CMessageHeader::MessageMagic &messageStart) { | ||||
// 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()) return error("%s: OpenUndoFile failed", __func__); | if (fileout.IsNull()) return error("%s: OpenUndoFile failed", __func__); | ||||
// Write index header | // Write index header | ||||
unsigned int nSize = GetSerializeSize(fileout, blockundo); | unsigned int nSize = GetSerializeSize(fileout, blockundo); | ||||
fileout << FLATDATA(messageStart) << nSize; | fileout << FLATDATA(messageStart) << nSize; | ||||
▲ Show 20 Lines • Show All 604 Lines • ▼ Show 20 Lines | if (pindex->GetUndoPos().IsNull() || | ||||
CDiskBlockPos _pos; | CDiskBlockPos _pos; | ||||
if (!FindUndoPos( | if (!FindUndoPos( | ||||
state, pindex->nFile, _pos, | state, pindex->nFile, _pos, | ||||
::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + | ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + | ||||
40)) { | 40)) { | ||||
return error("ConnectBlock(): FindUndoPos failed"); | return error("ConnectBlock(): FindUndoPos failed"); | ||||
} | } | ||||
if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), | if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), | ||||
chainparams.MessageStart())) { | chainparams.DiskMagic())) { | ||||
return AbortNode(state, "Failed to write undo data"); | return AbortNode(state, "Failed to write undo data"); | ||||
} | } | ||||
// update nUndoPos in block index | // update nUndoPos in block index | ||||
pindex->nUndoPos = _pos.nPos; | pindex->nUndoPos = _pos.nPos; | ||||
pindex->nStatus |= BLOCK_HAVE_UNDO; | pindex->nStatus |= BLOCK_HAVE_UNDO; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 1,520 Lines • ▼ Show 20 Lines | try { | ||||
if (dbp != nullptr) { | if (dbp != nullptr) { | ||||
blockPos = *dbp; | blockPos = *dbp; | ||||
} | } | ||||
if (!FindBlockPos(state, blockPos, nBlockSize + 8, nHeight, | if (!FindBlockPos(state, blockPos, nBlockSize + 8, nHeight, | ||||
block.GetBlockTime(), dbp != nullptr)) { | block.GetBlockTime(), dbp != nullptr)) { | ||||
return error("AcceptBlock(): FindBlockPos failed"); | return error("AcceptBlock(): FindBlockPos failed"); | ||||
} | } | ||||
if (dbp == nullptr) { | if (dbp == nullptr) { | ||||
if (!WriteBlockToDisk(block, blockPos, | if (!WriteBlockToDisk(block, blockPos, chainparams.DiskMagic())) { | ||||
chainparams.MessageStart())) { | |||||
AbortNode(state, "Failed to write block"); | AbortNode(state, "Failed to write block"); | ||||
} | } | ||||
} | } | ||||
if (!ReceivedBlockTransactions(block, state, pindex, blockPos)) { | if (!ReceivedBlockTransactions(block, state, pindex, blockPos)) { | ||||
return error("AcceptBlock(): ReceivedBlockTransactions failed"); | return error("AcceptBlock(): ReceivedBlockTransactions failed"); | ||||
} | } | ||||
} catch (const std::runtime_error &e) { | } catch (const std::runtime_error &e) { | ||||
return AbortNode(state, std::string("System error: ") + e.what()); | return AbortNode(state, std::string("System error: ") + e.what()); | ||||
▲ Show 20 Lines • Show All 703 Lines • ▼ Show 20 Lines | if (!fReindex) { | ||||
unsigned int nBlockSize = | unsigned int nBlockSize = | ||||
::GetSerializeSize(block, SER_DISK, CLIENT_VERSION); | ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION); | ||||
CDiskBlockPos blockPos; | CDiskBlockPos blockPos; | ||||
CValidationState state; | CValidationState state; | ||||
if (!FindBlockPos(state, blockPos, nBlockSize + 8, 0, | if (!FindBlockPos(state, blockPos, nBlockSize + 8, 0, | ||||
block.GetBlockTime())) { | block.GetBlockTime())) { | ||||
return error("LoadBlockIndex(): FindBlockPos failed"); | return error("LoadBlockIndex(): FindBlockPos failed"); | ||||
} | } | ||||
if (!WriteBlockToDisk(block, blockPos, | if (!WriteBlockToDisk(block, blockPos, chainparams.DiskMagic())) { | ||||
chainparams.MessageStart())) { | |||||
return error( | return error( | ||||
"LoadBlockIndex(): writing genesis block to disk failed"); | "LoadBlockIndex(): writing genesis block to disk failed"); | ||||
} | } | ||||
CBlockIndex *pindex = AddToBlockIndex(block); | CBlockIndex *pindex = AddToBlockIndex(block); | ||||
if (!ReceivedBlockTransactions(block, state, pindex, blockPos)) { | if (!ReceivedBlockTransactions(block, state, pindex, blockPos)) { | ||||
return error("LoadBlockIndex(): genesis block not accepted"); | return error("LoadBlockIndex(): genesis block not accepted"); | ||||
} | } | ||||
// Force a chainstate write so that when we VerifyDB in a moment, it | // Force a chainstate write so that when we VerifyDB in a moment, it | ||||
Show All 33 Lines | try { | ||||
// Start one byte further next time, in case of failure. | // Start one byte further next time, in case of failure. | ||||
nRewind++; | nRewind++; | ||||
// Remove former limit. | // Remove former limit. | ||||
blkdat.SetLimit(); | blkdat.SetLimit(); | ||||
unsigned int nSize = 0; | unsigned int nSize = 0; | ||||
try { | try { | ||||
// Locate a header. | // Locate a header. | ||||
uint8_t buf[CMessageHeader::MESSAGE_START_SIZE]; | uint8_t buf[CMessageHeader::MESSAGE_START_SIZE]; | ||||
blkdat.FindByte(chainparams.MessageStart()[0]); | blkdat.FindByte(chainparams.DiskMagic()[0]); | ||||
nRewind = blkdat.GetPos() + 1; | nRewind = blkdat.GetPos() + 1; | ||||
blkdat >> FLATDATA(buf); | blkdat >> FLATDATA(buf); | ||||
if (memcmp(buf, chainparams.MessageStart(), | if (memcmp(buf, chainparams.DiskMagic(), | ||||
CMessageHeader::MESSAGE_START_SIZE)) { | CMessageHeader::MESSAGE_START_SIZE)) { | ||||
continue; | continue; | ||||
} | } | ||||
// Read size. | // Read size. | ||||
blkdat >> nSize; | blkdat >> nSize; | ||||
if (nSize < 80) { | if (nSize < 80) { | ||||
continue; | continue; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 599 Lines • Show Last 20 Lines |