MCPcopy Create free account
hub / github.com/ElementsProject/elements / ActivateBestChain

Method ActivateBestChain

src/validation.cpp:3191–3303  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3189}
3190
3191bool CChainState::ActivateBestChain(BlockValidationState& state, std::shared_ptr<const CBlock> pblock)
3192{
3193 AssertLockNotHeld(m_chainstate_mutex);
3194
3195 // Note that while we're often called here from ProcessNewBlock, this is
3196 // far from a guarantee. Things in the P2P/RPC will often end up calling
3197 // us in the middle of ProcessNewBlock - do not assume pblock is set
3198 // sanely for performance or correctness!
3199 AssertLockNotHeld(::cs_main);
3200
3201 // ABC maintains a fair degree of expensive-to-calculate internal state
3202 // because this function periodically releases cs_main so that it does not lock up other threads for too long
3203 // during large connects - and to allow for e.g. the callback queue to drain
3204 // we use m_chainstate_mutex to enforce mutual exclusion so that only one caller may execute this function at a time
3205 LOCK(m_chainstate_mutex);
3206
3207 CBlockIndex *pindexMostWork = nullptr;
3208 CBlockIndex *pindexNewTip = nullptr;
3209 int nStopAtHeight = gArgs.GetIntArg("-stopatheight", DEFAULT_STOPATHEIGHT);
3210 bool fStall = false;
3211
3212 do {
3213 // Block until the validation queue drains. This should largely
3214 // never happen in normal operation, however may happen during
3215 // reindex, causing memory blowup if we run too far ahead.
3216 // Note that if a validationinterface callback ends up calling
3217 // ActivateBestChain this may lead to a deadlock! We should
3218 // probably have a DEBUG_LOCKORDER test for this in the future.
3219 LimitValidationInterfaceQueue();
3220
3221 {
3222 LOCK(cs_main);
3223 // Lock transaction pool for at least as long as it takes for connectTrace to be consumed
3224 LOCK(MempoolMutex());
3225 CBlockIndex* starting_tip = m_chain.Tip();
3226 bool blocks_connected = false;
3227 do {
3228 // We absolutely may not unlock cs_main until we've made forward progress
3229 // (with the exception of shutdown due to hardware issues, low disk space, etc).
3230 ConnectTrace connectTrace; // Destructed before cs_main is unlocked
3231
3232 if (pindexMostWork == nullptr) {
3233 pindexMostWork = FindMostWorkChain();
3234 }
3235
3236 // Whether we have anything to do at all.
3237 if (pindexMostWork == nullptr || pindexMostWork == m_chain.Tip()) {
3238 break;
3239 }
3240
3241 bool fInvalidFound = false;
3242 std::shared_ptr<const CBlock> nullBlockPtr;
3243 if (!ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace, fStall)) {
3244 // A system error occurred
3245 return false;
3246 }
3247 blocks_connected = true;
3248

Callers 10

ProcessGetBlockDataMethod · 0.80
ProcessMessageMethod · 0.80
AppInitMainFunction · 0.80
ProcessNewBlockMethod · 0.80
BOOST_FIXTURE_TEST_CASEFunction · 0.80
BOOST_AUTO_TEST_CASEFunction · 0.80
TestingSetupMethod · 0.80
invalidateblockFunction · 0.80
reconsiderblockFunction · 0.80
ThreadImportFunction · 0.80

Calls 12

GetSynchronizationStateFunction · 0.85
StartShutdownFunction · 0.85
ShutdownRequestedFunction · 0.85
GetIntArgMethod · 0.80
FindForkMethod · 0.80
NotifyBlockTipMethod · 0.80
TipMethod · 0.45
GetHashMethod · 0.45
GetBlockHashMethod · 0.45
BlockConnectedMethod · 0.45
UpdatedBlockTipMethod · 0.45

Tested by 3

BOOST_FIXTURE_TEST_CASEFunction · 0.64
BOOST_AUTO_TEST_CASEFunction · 0.64
TestingSetupMethod · 0.64