Skip to content

Commit 719de56

Browse files
committed
lock cs_main for chainActive
ActivateBestChain uses chainActive after releasing the lock; reorder operations to move all access to synchronized object into existing LOCK(cs_main) block.
1 parent efb54ba commit 719de56

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/main.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2897,14 +2897,15 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
28972897
*/
28982898
bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, const CBlock *pblock) {
28992899
CBlockIndex *pindexMostWork = NULL;
2900+
CBlockIndex *pindexNewTip = NULL;
29002901
do {
29012902
boost::this_thread::interruption_point();
29022903
if (ShutdownRequested())
29032904
break;
29042905

2905-
CBlockIndex *pindexNewTip = NULL;
29062906
const CBlockIndex *pindexFork;
29072907
bool fInitialDownload;
2908+
int nNewHeight;
29082909
{
29092910
LOCK(cs_main);
29102911
CBlockIndex *pindexOldTip = chainActive.Tip();
@@ -2920,6 +2921,7 @@ bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams,
29202921
pindexNewTip = chainActive.Tip();
29212922
pindexFork = chainActive.FindFork(pindexOldTip);
29222923
fInitialDownload = IsInitialBlockDownload();
2924+
nNewHeight = chainActive.Height();
29232925
}
29242926
// When we reach this point, we switched to a new tip (stored in pindexNewTip).
29252927

@@ -2948,7 +2950,7 @@ bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams,
29482950
{
29492951
LOCK(cs_vNodes);
29502952
BOOST_FOREACH(CNode* pnode, vNodes) {
2951-
if (chainActive.Height() > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate)) {
2953+
if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate)) {
29522954
BOOST_REVERSE_FOREACH(const uint256& hash, vHashes) {
29532955
pnode->PushBlockHash(hash);
29542956
}
@@ -2961,7 +2963,7 @@ bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams,
29612963
}
29622964
}
29632965
}
2964-
} while(pindexMostWork != chainActive.Tip());
2966+
} while (pindexNewTip != pindexMostWork);
29652967
CheckBlockIndex(chainparams.GetConsensus());
29662968

29672969
// Write changes periodically to disk, after relay.

0 commit comments

Comments
 (0)