Skip to content

Commit

Permalink
Optimize ActivateBestChain for long chains
Browse files Browse the repository at this point in the history
Github-Pull: #2290
Rebased-From: 8d15cf5
  • Loading branch information
sipa authored and furszy committed Apr 8, 2021
1 parent c2ae5ff commit fd79bb7
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2138,12 +2138,11 @@ static void PruneBlockIndexCandidates()
* Try to make some progress towards making pindexMostWork the active block.
* pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
*/
static bool ActivateBestChainStep(CValidationState& state, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool fAlreadyChecked, ConnectTrace& connectTrace)
static bool ActivateBestChainStep(CValidationState& state, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool fAlreadyChecked, bool& fInvalidFound, ConnectTrace& connectTrace)
{
AssertLockHeld(cs_main);
if (pblock == NULL)
fAlreadyChecked = false;
bool fInvalidFound = false;
const CBlockIndex* pindexOldTip = chainActive.Tip();
const CBlockIndex* pindexFork = chainActive.FindFork(pindexMostWork);

Expand Down Expand Up @@ -2246,16 +2245,23 @@ bool ActivateBestChain(CValidationState& state, std::shared_ptr<const CBlock> pb
ConnectTrace connectTrace(mempool); // Destructed before cs_main is unlocked

CBlockIndex *pindexOldTip = chainActive.Tip();
pindexMostWork = FindMostWorkChain();
if (pindexMostWork == nullptr) {
pindexMostWork = FindMostWorkChain();
}

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

bool fInvalidFound = false;
std::shared_ptr<const CBlock> nullBlockPtr;
if (!ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fAlreadyChecked, connectTrace))
if (!ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fAlreadyChecked, fInvalidFound, connectTrace))
return false;

if (fInvalidFound) {
// Wipe cache, we may need another branch now.
pindexMostWork = nullptr;
}
pindexNewTip = chainActive.Tip();
pindexFork = chainActive.FindFork(pindexOldTip);
fInitialDownload = IsInitialBlockDownload();
Expand Down

0 comments on commit fd79bb7

Please sign in to comment.