Get rid of the static chainMostWork (optimization)

This commit is contained in:
Pieter Wuille 2014-05-06 00:54:10 +02:00
parent 6ff35a0565
commit 77339e5aec
2 changed files with 30 additions and 32 deletions

View file

@ -40,7 +40,6 @@ CTxMemPool mempool;
map<uint256, CBlockIndex*> mapBlockIndex; map<uint256, CBlockIndex*> mapBlockIndex;
CChain chainActive; CChain chainActive;
CChain chainMostWork;
int64_t nTimeBestReceived = 0; int64_t nTimeBestReceived = 0;
int nScriptCheckThreads = 0; int nScriptCheckThreads = 0;
bool fImporting = false; bool fImporting = false;
@ -398,6 +397,12 @@ CBlockIndex *CChain::FindFork(const CBlockLocator &locator) const {
return Genesis(); return Genesis();
} }
CBlockIndex *CChain::FindFork(CBlockIndex *pindex) const {
while (pindex && !Contains(pindex))
pindex = pindex->pprev;
return pindex;
}
CCoinsViewCache *pcoinsTip = NULL; CCoinsViewCache *pcoinsTip = NULL;
CBlockTreeDB *pblocktree = NULL; CBlockTreeDB *pblocktree = NULL;
@ -2035,23 +2040,17 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew) {
return true; return true;
} }
// Make chainMostWork correspond to the chain with the most work in it, that isn't // Return the tip of the chain with the most work in it, that isn't
// known to be invalid (it's however far from certain to be valid). // known to be invalid (it's however far from certain to be valid).
void static FindMostWorkChain() { static CBlockIndex* FindMostWorkChain() {
do {
CBlockIndex *pindexNew = NULL; CBlockIndex *pindexNew = NULL;
// In case the current best is invalid, do not consider it.
while (chainMostWork.Tip() && (chainMostWork.Tip()->nStatus & BLOCK_FAILED_MASK)) {
setBlockIndexValid.erase(chainMostWork.Tip());
chainMostWork.SetTip(chainMostWork.Tip()->pprev);
}
do {
// Find the best candidate header. // Find the best candidate header.
{ {
std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexValid.rbegin(); std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexValid.rbegin();
if (it == setBlockIndexValid.rend()) if (it == setBlockIndexValid.rend())
return; return NULL;
pindexNew = *it; pindexNew = *it;
} }
@ -2075,18 +2074,9 @@ void static FindMostWorkChain() {
} }
pindexTest = pindexTest->pprev; pindexTest = pindexTest->pprev;
} }
if (fInvalidAncestor) if (!fInvalidAncestor)
continue; return pindexNew;
break;
} while(true); } while(true);
// Check whether it's actually an improvement.
if (chainMostWork.Tip() && !CBlockIndexWorkComparator()(chainMostWork.Tip(), pindexNew))
return;
// We have a new best.
chainMostWork.SetTip(pindexNew);
} }
// Try to activate to the most-work chain (thereby connecting it). // Try to activate to the most-work chain (thereby connecting it).
@ -2095,26 +2085,34 @@ bool ActivateBestChain(CValidationState &state) {
CBlockIndex *pindexOldTip = chainActive.Tip(); CBlockIndex *pindexOldTip = chainActive.Tip();
bool fComplete = false; bool fComplete = false;
while (!fComplete) { while (!fComplete) {
FindMostWorkChain(); CBlockIndex *pindexMostWork = FindMostWorkChain();
CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork);
fComplete = true; fComplete = true;
// Check whether we have something to do. // Check whether we have something to do.
if (chainMostWork.Tip() == NULL) break; 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() && !chainMostWork.Contains(chainActive.Tip())) { while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
if (!DisconnectTip(state)) if (!DisconnectTip(state))
return false; return false;
} }
// Build list of new blocks to connect.
std::vector<CBlockIndex*> vpindexToConnect;
vpindexToConnect.reserve(pindexMostWork->nHeight - (pindexFork ? pindexFork->nHeight : -1));
while (pindexMostWork && pindexMostWork != pindexFork) {
vpindexToConnect.push_back(pindexMostWork);
pindexMostWork = pindexMostWork->pprev;
}
// Connect new blocks. // Connect new blocks.
while (!chainActive.Contains(chainMostWork.Tip())) { BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) {
CBlockIndex *pindexConnect = chainMostWork[chainActive.Height() + 1];
if (!ConnectTip(state, pindexConnect)) { if (!ConnectTip(state, pindexConnect)) {
if (state.IsInvalid()) { if (state.IsInvalid()) {
// The block violates a consensus rule. // The block violates a consensus rule.
if (!state.CorruptionPossible()) if (!state.CorruptionPossible())
InvalidChainFound(chainMostWork.Tip()); InvalidChainFound(vpindexToConnect.back());
fComplete = false; fComplete = false;
state = CValidationState(); state = CValidationState();
break; break;

View file

@ -1079,14 +1079,14 @@ public:
/** Find the last common block between this chain and a locator. */ /** Find the last common block between this chain and a locator. */
CBlockIndex *FindFork(const CBlockLocator &locator) const; CBlockIndex *FindFork(const CBlockLocator &locator) const;
/** Find the last common block between this chain and a block index entry. */
CBlockIndex *FindFork(CBlockIndex *pindex) const;
}; };
/** The currently-connected chain of blocks. */ /** The currently-connected chain of blocks. */
extern CChain chainActive; extern CChain chainActive;
/** The currently best known chain of headers (some of which may be invalid). */
extern CChain chainMostWork;
/** Global variable that points to the active CCoinsView (protected by cs_main) */ /** Global variable that points to the active CCoinsView (protected by cs_main) */
extern CCoinsViewCache *pcoinsTip; extern CCoinsViewCache *pcoinsTip;