MCPcopy Create free account
hub / github.com/BTCGPU/BTCGPU / ActivateBestChain

Method ActivateBestChain

src/validation.cpp:2827–2925  ·  view source on GitHub ↗

* Make the best chain active, in multiple steps. The result is either failure * or an activated best chain. pblock is either nullptr or a pointer to a block * that is already loaded (to avoid loading it again from disk). * * ActivateBestChain is split into steps (see ActivateBestChainStep) so that * we avoid holding cs_main for an extended period of time; the length of this * call may be qui

Source from the content-addressed store, hash-verified

2825 * call may be quite long during reindexing or a substantial reorg.
2826 */
2827bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) {
2828 // Note that while we're often called here from ProcessNewBlock, this is
2829 // far from a guarantee. Things in the P2P/RPC will often end up calling
2830 // us in the middle of ProcessNewBlock - do not assume pblock is set
2831 // sanely for performance or correctness!
2832 AssertLockNotHeld(cs_main);
2833
2834 // ABC maintains a fair degree of expensive-to-calculate internal state
2835 // because this function periodically releases cs_main so that it does not lock up other threads for too long
2836 // during large connects - and to allow for e.g. the callback queue to drain
2837 // we use m_cs_chainstate to enforce mutual exclusion so that only one caller may execute this function at a time
2838 LOCK(m_cs_chainstate);
2839
2840 CBlockIndex *pindexMostWork = nullptr;
2841 CBlockIndex *pindexNewTip = nullptr;
2842 int nStopAtHeight = gArgs.GetArg("-stopatheight", DEFAULT_STOPATHEIGHT);
2843 do {
2844 boost::this_thread::interruption_point();
2845
2846 if (GetMainSignals().CallbacksPending() > 10) {
2847 // Block until the validation queue drains. This should largely
2848 // never happen in normal operation, however may happen during
2849 // reindex, causing memory blowup if we run too far ahead.
2850 // Note that if a validationinterface callback ends up calling
2851 // ActivateBestChain this may lead to a deadlock! We should
2852 // probably have a DEBUG_LOCKORDER test for this in the future.
2853 SyncWithValidationInterfaceQueue();
2854 }
2855
2856 {
2857 LOCK(cs_main);
2858 CBlockIndex* starting_tip = chainActive.Tip();
2859 bool blocks_connected = false;
2860 do {
2861 // We absolutely may not unlock cs_main until we've made forward progress
2862 // (with the exception of shutdown due to hardware issues, low disk space, etc).
2863 ConnectTrace connectTrace(mempool); // Destructed before cs_main is unlocked
2864
2865 if (pindexMostWork == nullptr) {
2866 pindexMostWork = FindMostWorkChain();
2867 }
2868
2869 // Whether we have anything to do at all.
2870 if (pindexMostWork == nullptr || pindexMostWork == chainActive.Tip()) {
2871 break;
2872 }
2873
2874 bool fInvalidFound = false;
2875 std::shared_ptr<const CBlock> nullBlockPtr;
2876 if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace))
2877 return false;
2878 blocks_connected = true;
2879
2880 if (fInvalidFound) {
2881 // Wipe cache, we may need another branch now.
2882 pindexMostWork = nullptr;
2883 }
2884 pindexNewTip = chainActive.Tip();

Callers 2

ActivateBestChainFunction · 0.80
ProcessNewBlockFunction · 0.80

Calls 14

IsInitialBlockDownloadFunction · 0.85
FlushStateToDiskFunction · 0.85
FindForkMethod · 0.80
StartShutdownFunction · 0.70
ShutdownRequestedFunction · 0.70
GetArgMethod · 0.45
CallbacksPendingMethod · 0.45
TipMethod · 0.45
GetHashMethod · 0.45
GetBlockHashMethod · 0.45

Tested by

no test coverage detected