Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
Show First 20 Lines • Show All 216 Lines • ▼ Show 20 Lines | private: | ||||
* By default this only executes fully when using the Regtest chain; see: | * By default this only executes fully when using the Regtest chain; see: | ||||
* fCheckBlockIndex. | * fCheckBlockIndex. | ||||
*/ | */ | ||||
void CheckBlockIndex(const Consensus::Params &consensusParams); | void CheckBlockIndex(const Consensus::Params &consensusParams); | ||||
void InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state) | void InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state) | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs_main); | EXCLUSIVE_LOCKS_REQUIRED(cs_main); | ||||
CBlockIndex *FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main); | CBlockIndex *FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main); | ||||
bool ReceivedBlockTransactions(const CBlock &block, CValidationState &state, | void ReceivedBlockTransactions(const CBlock &block, CBlockIndex *pindexNew, | ||||
CBlockIndex *pindexNew, | |||||
const FlatFilePos &pos) | const FlatFilePos &pos) | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs_main); | EXCLUSIVE_LOCKS_REQUIRED(cs_main); | ||||
bool RollforwardBlock(const CBlockIndex *pindex, CCoinsViewCache &inputs, | bool RollforwardBlock(const CBlockIndex *pindex, CCoinsViewCache &inputs, | ||||
const Consensus::Params ¶ms) | const Consensus::Params ¶ms) | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs_main); | EXCLUSIVE_LOCKS_REQUIRED(cs_main); | ||||
} g_chainstate; | } g_chainstate; | ||||
▲ Show 20 Lines • Show All 3,050 Lines • ▼ Show 20 Lines | CBlockIndex *CChainState::AddToBlockIndex(const CBlockHeader &block) { | ||||
setDirtyBlockIndex.insert(pindexNew); | setDirtyBlockIndex.insert(pindexNew); | ||||
return pindexNew; | return pindexNew; | ||||
} | } | ||||
/** | /** | ||||
* Mark a block as having its data received and checked (up to | * Mark a block as having its data received and checked (up to | ||||
* BLOCK_VALID_TRANSACTIONS). | * BLOCK_VALID_TRANSACTIONS). | ||||
*/ | */ | ||||
bool CChainState::ReceivedBlockTransactions(const CBlock &block, | void CChainState::ReceivedBlockTransactions(const CBlock &block, | ||||
CValidationState &state, | |||||
CBlockIndex *pindexNew, | CBlockIndex *pindexNew, | ||||
const FlatFilePos &pos) { | const FlatFilePos &pos) { | ||||
pindexNew->nTx = block.vtx.size(); | pindexNew->nTx = block.vtx.size(); | ||||
pindexNew->nChainTx = 0; | pindexNew->nChainTx = 0; | ||||
pindexNew->nFile = pos.nFile; | pindexNew->nFile = pos.nFile; | ||||
pindexNew->nDataPos = pos.nPos; | pindexNew->nDataPos = pos.nPos; | ||||
pindexNew->nUndoPos = 0; | pindexNew->nUndoPos = 0; | ||||
pindexNew->nStatus = pindexNew->nStatus.withData(); | pindexNew->nStatus = pindexNew->nStatus.withData(); | ||||
Show All 38 Lines | if (pindexNew->pprev == nullptr || pindexNew->pprev->HaveTxsDownloaded()) { | ||||
range.first++; | range.first++; | ||||
mapBlocksUnlinked.erase(it); | mapBlocksUnlinked.erase(it); | ||||
} | } | ||||
} | } | ||||
} else if (pindexNew->pprev && | } else if (pindexNew->pprev && | ||||
pindexNew->pprev->IsValid(BlockValidity::TREE)) { | pindexNew->pprev->IsValid(BlockValidity::TREE)) { | ||||
mapBlocksUnlinked.insert(std::make_pair(pindexNew->pprev, pindexNew)); | mapBlocksUnlinked.insert(std::make_pair(pindexNew->pprev, pindexNew)); | ||||
} | } | ||||
return true; | |||||
} | } | ||||
static bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, | static bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, | ||||
unsigned int nHeight, uint64_t nTime, | unsigned int nHeight, uint64_t nTime, | ||||
bool fKnown = false) { | bool fKnown = false) { | ||||
LOCK(cs_LastBlockFile); | LOCK(cs_LastBlockFile); | ||||
unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile; | unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile; | ||||
▲ Show 20 Lines • Show All 714 Lines • ▼ Show 20 Lines | try { | ||||
FlatFilePos blockPos = | FlatFilePos blockPos = | ||||
SaveBlockToDisk(block, pindex->nHeight, chainparams, dbp); | SaveBlockToDisk(block, pindex->nHeight, chainparams, dbp); | ||||
if (blockPos.IsNull()) { | if (blockPos.IsNull()) { | ||||
state.Error(strprintf( | state.Error(strprintf( | ||||
"%s: Failed to find position to write new block to disk", | "%s: Failed to find position to write new block to disk", | ||||
__func__)); | __func__)); | ||||
return false; | return false; | ||||
} | } | ||||
if (!ReceivedBlockTransactions(block, state, pindex, blockPos)) { | ReceivedBlockTransactions(block, pindex, blockPos); | ||||
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()); | ||||
} | } | ||||
FlushStateToDisk(config.GetChainParams(), state, FlushStateMode::NONE); | FlushStateToDisk(config.GetChainParams(), state, FlushStateMode::NONE); | ||||
CheckBlockIndex(chainparams.GetConsensus()); | CheckBlockIndex(chainparams.GetConsensus()); | ||||
▲ Show 20 Lines • Show All 876 Lines • ▼ Show 20 Lines | bool CChainState::LoadGenesisBlock(const CChainParams &chainparams) { | ||||
try { | try { | ||||
const CBlock &block = chainparams.GenesisBlock(); | const CBlock &block = chainparams.GenesisBlock(); | ||||
FlatFilePos blockPos = SaveBlockToDisk(block, 0, chainparams, nullptr); | FlatFilePos blockPos = SaveBlockToDisk(block, 0, chainparams, nullptr); | ||||
if (blockPos.IsNull()) { | if (blockPos.IsNull()) { | ||||
return error("%s: writing genesis block to disk failed", __func__); | return error("%s: writing genesis block to disk failed", __func__); | ||||
} | } | ||||
CBlockIndex *pindex = AddToBlockIndex(block); | CBlockIndex *pindex = AddToBlockIndex(block); | ||||
CValidationState state; | ReceivedBlockTransactions(block, pindex, blockPos); | ||||
if (!ReceivedBlockTransactions(block, state, pindex, blockPos)) { | |||||
return error("%s: genesis block not accepted (%s)", __func__, | |||||
FormatStateMessage(state)); | |||||
} | |||||
} catch (const std::runtime_error &e) { | } catch (const std::runtime_error &e) { | ||||
return error("%s: failed to write genesis block: %s", __func__, | return error("%s: failed to write genesis block: %s", __func__, | ||||
e.what()); | e.what()); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 683 Lines • Show Last 20 Lines |