Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
Show First 20 Lines • Show All 3,092 Lines • ▼ Show 20 Lines | |||||
bool CChainState::ParkBlock(const Config &config, BlockValidationState &state, | bool CChainState::ParkBlock(const Config &config, BlockValidationState &state, | ||||
CBlockIndex *pindex) { | CBlockIndex *pindex) { | ||||
return UnwindBlock(config, state, pindex, false); | return UnwindBlock(config, state, pindex, false); | ||||
} | } | ||||
bool CChainState::FinalizeBlock(const Config &config, | bool CChainState::FinalizeBlock(const Config &config, | ||||
BlockValidationState &state, | BlockValidationState &state, | ||||
CBlockIndex *pindex) { | CBlockIndex *pindex) { | ||||
AssertLockHeld(cs_main); | AssertLockNotHeld(cs_main); | ||||
CBlockIndex *pindexToInvalidate = nullptr; | |||||
{ | |||||
LOCK(cs_main); | |||||
if (!MarkBlockAsFinal(config, state, pindex)) { | if (!MarkBlockAsFinal(config, state, pindex)) { | ||||
// state is set by MarkBlockAsFinal. | // state is set by MarkBlockAsFinal. | ||||
return false; | return false; | ||||
} | } | ||||
// We have a valid candidate, make sure it is not parked. | // We have a valid candidate, make sure it is not parked. | ||||
if (pindex->nStatus.isOnParkedChain()) { | if (pindex->nStatus.isOnParkedChain()) { | ||||
UnparkBlock(pindex); | UnparkBlock(pindex); | ||||
} | } | ||||
// If the finalized block is not on the active chain, we may need to rewind. | // If the finalized block is on the active chain, there is no need to | ||||
if (!::ChainActive().Contains(pindex)) { | // rewind. | ||||
const CBlockIndex *pindexFork = ::ChainActive().FindFork(pindex); | if (::ChainActive().Contains(pindex)) { | ||||
CBlockIndex *pindexToInvalidate = ::ChainActive().Next(pindexFork); | return true; | ||||
if (pindexToInvalidate) { | |||||
return UnwindBlock(config, state, pindexToInvalidate, | |||||
true /* invalidating */); | |||||
} | } | ||||
// If the finalized block is not on the active chain, that chain is | |||||
// invalid | |||||
// ... | |||||
const CBlockIndex *pindexFork = ::ChainActive().FindFork(pindex); | |||||
pindexToInvalidate = ::ChainActive().Next(pindexFork); | |||||
if (!pindexToInvalidate) { | |||||
return false; | |||||
} | } | ||||
} // end of locked cs_main scope | |||||
return true; | // ... therefore, we invalidate the block on the active chain that comes | ||||
// immediately after it | |||||
return UnwindBlock(config, state, pindexToInvalidate, | |||||
true /* invalidating */); | |||||
} | } | ||||
template <typename F> | template <typename F> | ||||
bool CChainState::UpdateFlagsForBlock(CBlockIndex *pindexBase, | bool CChainState::UpdateFlagsForBlock(CBlockIndex *pindexBase, | ||||
CBlockIndex *pindex, F f) { | CBlockIndex *pindex, F f) { | ||||
BlockStatus newStatus = f(pindex->nStatus); | BlockStatus newStatus = f(pindex->nStatus); | ||||
if (pindex->nStatus != newStatus && | if (pindex->nStatus != newStatus && | ||||
(!pindexBase || | (!pindexBase || | ||||
▲ Show 20 Lines • Show All 2,549 Lines • Show Last 20 Lines |