| 3189 | } |
| 3190 | |
| 3191 | bool 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 | |