Skip to content

Commit fb8fad1

Browse files
committed
Optimize ActivateBestChain for long chains
1 parent 316623f commit fb8fad1

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/main.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,10 +2811,9 @@ static void PruneBlockIndexCandidates() {
28112811
* Try to make some progress towards making pindexMostWork the active block.
28122812
* pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
28132813
*/
2814-
static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const CBlock* pblock)
2814+
static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const CBlock* pblock, bool& fInvalidFound)
28152815
{
28162816
AssertLockHeld(cs_main);
2817-
bool fInvalidFound = false;
28182817
const CBlockIndex *pindexOldTip = chainActive.Tip();
28192818
const CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork);
28202819

@@ -2902,15 +2901,22 @@ bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams,
29022901
{
29032902
LOCK(cs_main);
29042903
CBlockIndex *pindexOldTip = chainActive.Tip();
2905-
pindexMostWork = FindMostWorkChain();
2904+
if (pindexMostWork == NULL) {
2905+
pindexMostWork = FindMostWorkChain();
2906+
}
29062907

29072908
// Whether we have anything to do at all.
29082909
if (pindexMostWork == NULL || pindexMostWork == chainActive.Tip())
29092910
return true;
29102911

2911-
if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL))
2912+
bool fInvalidFound = false;
2913+
if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL, fInvalidFound))
29122914
return false;
29132915

2916+
if (fInvalidFound) {
2917+
// Wipe cache, we may need another branch now.
2918+
pindexMostWork = NULL;
2919+
}
29142920
pindexNewTip = chainActive.Tip();
29152921
pindexFork = chainActive.FindFork(pindexOldTip);
29162922
fInitialDownload = IsInitialBlockDownload();

0 commit comments

Comments
 (0)