| 3331 | } |
| 3332 | |
| 3333 | bool Chainstate::ActivateBestChain(BlockValidationState& state, std::shared_ptr<const CBlock> pblock) |
| 3334 | { |
| 3335 | AssertLockNotHeld(m_chainstate_mutex); |
| 3336 | |
| 3337 | // Note that while we're often called here from ProcessNewBlock, this is |
| 3338 | // far from a guarantee. Things in the P2P/RPC will often end up calling |
| 3339 | // us in the middle of ProcessNewBlock - do not assume pblock is set |
| 3340 | // sanely for performance or correctness! |
| 3341 | AssertLockNotHeld(::cs_main); |
| 3342 | |
| 3343 | // ABC maintains a fair degree of expensive-to-calculate internal state |
| 3344 | // because this function periodically releases cs_main so that it does not lock up other threads for too long |
| 3345 | // during large connects - and to allow for e.g. the callback queue to drain |
| 3346 | // we use m_chainstate_mutex to enforce mutual exclusion so that only one caller may execute this function at a time |
| 3347 | LOCK(m_chainstate_mutex); |
| 3348 | |
| 3349 | // Belt-and-suspenders check that we aren't attempting to advance the |
| 3350 | // chainstate past the target block. |
| 3351 | if (WITH_LOCK(::cs_main, return m_target_utxohash)) { |
| 3352 | LogError("%s", STR_INTERNAL_BUG("m_target_utxohash is set - this chainstate should not be in operation.")); |
| 3353 | return Assume(false); |
| 3354 | } |
| 3355 | |
| 3356 | CBlockIndex *pindexMostWork = nullptr; |
| 3357 | CBlockIndex *pindexNewTip = nullptr; |
| 3358 | bool exited_ibd{false}; |
| 3359 | do { |
| 3360 | // Block until the validation queue drains. This should largely |
| 3361 | // never happen in normal operation, however may happen during |
| 3362 | // reindex, causing memory blowup if we run too far ahead. |
| 3363 | // Note that if a validationinterface callback ends up calling |
| 3364 | // ActivateBestChain this may lead to a deadlock! We should |
| 3365 | // probably have a DEBUG_LOCKORDER test for this in the future. |
| 3366 | if (m_chainman.m_options.signals) LimitValidationInterfaceQueue(*m_chainman.m_options.signals); |
| 3367 | |
| 3368 | { |
| 3369 | LOCK(cs_main); |
| 3370 | { |
| 3371 | // Lock transaction pool for at least as long as it takes for connected_blocks to be consumed |
| 3372 | LOCK(MempoolMutex()); |
| 3373 | const bool was_in_ibd = m_chainman.IsInitialBlockDownload(); |
| 3374 | CBlockIndex* starting_tip = m_chain.Tip(); |
| 3375 | bool blocks_connected = false; |
| 3376 | do { |
| 3377 | // We absolutely may not unlock cs_main until we've made forward progress |
| 3378 | // (with the exception of shutdown due to hardware issues, low disk space, etc). |
| 3379 | std::vector<ConnectedBlock> connected_blocks; // Destructed before cs_main is unlocked |
| 3380 | |
| 3381 | if (pindexMostWork == nullptr) { |
| 3382 | pindexMostWork = FindMostWorkChain(); |
| 3383 | } |
| 3384 | |
| 3385 | // Whether we have anything to do at all. |
| 3386 | if (pindexMostWork == nullptr || pindexMostWork == m_chain.Tip()) { |
| 3387 | break; |
| 3388 | } |
| 3389 | |
| 3390 | bool fInvalidFound = false; |