Browse Source

Do not unlock cs_main in ABC unless we've actually made progress.

Technically, some internal datastructures may be in an inconsistent
state if we do this, though there are no known bugs there. Still,
for future safety, its much better to only unlock cs_main if we've
made progress (not just tried a reorg which may make progress).

Github-Pull: #13023
Rebased-From: ecc3c4a019e6db30e208b8554b1a3658dcb9a80a
0.16
Matt Corallo 7 years ago committed by MarcoFalke
parent
commit
0948153ea6
  1. 65
      src/validation.cpp
  2. 6
      src/validationinterface.h

65
src/validation.cpp

@ -2575,45 +2575,53 @@ bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams&
SyncWithValidationInterfaceQueue(); SyncWithValidationInterfaceQueue();
} }
const CBlockIndex *pindexFork;
bool fInitialDownload;
{ {
LOCK(cs_main); LOCK(cs_main);
ConnectTrace connectTrace(mempool); // Destructed before cs_main is unlocked CBlockIndex* starting_tip = chainActive.Tip();
bool blocks_connected = false;
do {
// We absolutely may not unlock cs_main until we've made forward progress
// (with the exception of shutdown due to hardware issues, low disk space, etc).
ConnectTrace connectTrace(mempool); // Destructed before cs_main is unlocked
if (pindexMostWork == nullptr) {
pindexMostWork = FindMostWorkChain();
}
CBlockIndex *pindexOldTip = chainActive.Tip(); // Whether we have anything to do at all.
if (pindexMostWork == nullptr) { if (pindexMostWork == nullptr || pindexMostWork == chainActive.Tip()) {
pindexMostWork = FindMostWorkChain(); break;
} }
// Whether we have anything to do at all. bool fInvalidFound = false;
if (pindexMostWork == nullptr || pindexMostWork == chainActive.Tip()) std::shared_ptr<const CBlock> nullBlockPtr;
return true; if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace))
return false;
blocks_connected = true;
bool fInvalidFound = false; if (fInvalidFound) {
std::shared_ptr<const CBlock> nullBlockPtr; // Wipe cache, we may need another branch now.
if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace)) pindexMostWork = nullptr;
return false; }
pindexNewTip = chainActive.Tip();
if (fInvalidFound) { for (const PerBlockConnectTrace& trace : connectTrace.GetBlocksConnected()) {
// Wipe cache, we may need another branch now. assert(trace.pblock && trace.pindex);
pindexMostWork = nullptr; GetMainSignals().BlockConnected(trace.pblock, trace.pindex, trace.conflictedTxs);
} }
pindexNewTip = chainActive.Tip(); } while (!chainActive.Tip() || (starting_tip && CBlockIndexWorkComparator()(chainActive.Tip(), starting_tip)));
pindexFork = chainActive.FindFork(pindexOldTip); if (!blocks_connected) return true;
fInitialDownload = IsInitialBlockDownload();
for (const PerBlockConnectTrace& trace : connectTrace.GetBlocksConnected()) { const CBlockIndex* pindexFork = chainActive.FindFork(starting_tip);
assert(trace.pblock && trace.pindex); bool fInitialDownload = IsInitialBlockDownload();
GetMainSignals().BlockConnected(trace.pblock, trace.pindex, trace.conflictedTxs);
}
// Notify external listeners about the new tip. // Notify external listeners about the new tip.
// Enqueue while holding cs_main to ensure that UpdatedBlockTip is called in the order in which blocks are connected // Enqueue while holding cs_main to ensure that UpdatedBlockTip is called in the order in which blocks are connected
GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);
// Always notify the UI if a new block tip was connected
if (pindexFork != pindexNewTip) { if (pindexFork != pindexNewTip) {
// Notify ValidationInterface subscribers
GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);
// Always notify the UI if a new block tip was connected
uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip); uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
} }
} }
@ -2637,6 +2645,7 @@ bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams&
return true; return true;
} }
bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) { bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) {
return g_chainstate.ActivateBestChain(state, chainparams, std::move(pblock)); return g_chainstate.ActivateBestChain(state, chainparams, std::move(pblock));
} }

6
src/validationinterface.h

@ -56,7 +56,11 @@ void SyncWithValidationInterfaceQueue();
class CValidationInterface { class CValidationInterface {
protected: protected:
/** /**
* Notifies listeners of updated block chain tip * Notifies listeners when the block chain tip advances.
*
* When multiple blocks are connected at once, UpdatedBlockTip will be called on the final tip
* but may not be called on every intermediate tip. If the latter behavior is desired,
* subscribe to BlockConnected() instead.
* *
* Called on a background thread. * Called on a background thread.
*/ */

Loading…
Cancel
Save