Allow ActivateBestChain to release its lock on cs_main

This commit is contained in:
Pieter Wuille 2014-05-06 01:23:13 +02:00
parent 77339e5aec
commit 4e0eed88ac
1 changed files with 55 additions and 37 deletions

View File

@ -2079,18 +2079,11 @@ static CBlockIndex* FindMostWorkChain() {
} while(true); } while(true);
} }
// Try to activate to the most-work chain (thereby connecting it). // Try to make some progress towards making pindexMostWork the active block.
bool ActivateBestChain(CValidationState &state) { static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork) {
LOCK(cs_main); AssertLockHeld(cs_main);
CBlockIndex *pindexOldTip = chainActive.Tip(); CBlockIndex *pindexOldTip = chainActive.Tip();
bool fComplete = false;
while (!fComplete) {
CBlockIndex *pindexMostWork = FindMostWorkChain();
CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork); CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork);
fComplete = true;
// Check whether we have something to do.
if (pindexMostWork == NULL) break;
// Disconnect active blocks which are no longer in the best chain. // Disconnect active blocks which are no longer in the best chain.
while (chainActive.Tip() && chainActive.Tip() != pindexFork) { while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
@ -2113,13 +2106,16 @@ bool ActivateBestChain(CValidationState &state) {
// The block violates a consensus rule. // The block violates a consensus rule.
if (!state.CorruptionPossible()) if (!state.CorruptionPossible())
InvalidChainFound(vpindexToConnect.back()); InvalidChainFound(vpindexToConnect.back());
fComplete = false;
state = CValidationState(); state = CValidationState();
break; break;
} else { } else {
// A system error occurred (disk space, database error, ...). // A system error occurred (disk space, database error, ...).
return false; return false;
} }
} else {
if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
// We're in a better position than we were. Return temporarily to release the lock.
break;
} }
} }
} }
@ -2136,6 +2132,28 @@ bool ActivateBestChain(CValidationState &state) {
return true; return true;
} }
bool ActivateBestChain(CValidationState &state) {
do {
boost::this_thread::interruption_point();
LOCK(cs_main);
// Check whether we're done (this could be avoided after the first run,
// but that's not worth optimizing.
CBlockIndex *pindexMostWork = FindMostWorkChain();
if (pindexMostWork == NULL || pindexMostWork == chainActive.Tip())
return true;
if (!ActivateBestChainStep(state, pindexMostWork))
return false;
// Check whether we're done now.
if (pindexMostWork == chainActive.Tip())
return true;
} while(true);
return true;
}
CBlockIndex* AddToBlockIndex(CBlockHeader& block) CBlockIndex* AddToBlockIndex(CBlockHeader& block)
{ {