diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2014-05-06 00:54:10 +0200 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2014-06-09 02:21:15 +0200 |
commit | 77339e5aec4da99f727b80829f9697357b4cec45 (patch) | |
tree | 13009d65f4d6f14aee63667ae23aaaf3e267b9f5 /src | |
parent | 6ff35a0565a6903464d1dfaaf02360d5c5e70c0f (diff) |
Get rid of the static chainMostWork (optimization)
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 56 | ||||
-rw-r--r-- | src/main.h | 6 |
2 files changed, 30 insertions, 32 deletions
diff --git a/src/main.cpp b/src/main.cpp index 429473d8f8..806f1c20db 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -40,7 +40,6 @@ CTxMemPool mempool; map<uint256, CBlockIndex*> mapBlockIndex; CChain chainActive; -CChain chainMostWork; int64_t nTimeBestReceived = 0; int nScriptCheckThreads = 0; bool fImporting = false; @@ -398,6 +397,12 @@ CBlockIndex *CChain::FindFork(const CBlockLocator &locator) const { return Genesis(); } +CBlockIndex *CChain::FindFork(CBlockIndex *pindex) const { + while (pindex && !Contains(pindex)) + pindex = pindex->pprev; + return pindex; +} + CCoinsViewCache *pcoinsTip = NULL; CBlockTreeDB *pblocktree = NULL; @@ -2035,23 +2040,17 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew) { 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). -void static FindMostWorkChain() { - 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); - } - +static CBlockIndex* FindMostWorkChain() { do { + CBlockIndex *pindexNew = NULL; + // Find the best candidate header. { std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexValid.rbegin(); if (it == setBlockIndexValid.rend()) - return; + return NULL; pindexNew = *it; } @@ -2075,18 +2074,9 @@ void static FindMostWorkChain() { } pindexTest = pindexTest->pprev; } - if (fInvalidAncestor) - continue; - - break; + if (!fInvalidAncestor) + return pindexNew; } 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). @@ -2095,26 +2085,34 @@ bool ActivateBestChain(CValidationState &state) { CBlockIndex *pindexOldTip = chainActive.Tip(); bool fComplete = false; while (!fComplete) { - FindMostWorkChain(); + CBlockIndex *pindexMostWork = FindMostWorkChain(); + CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork); fComplete = true; // 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. - while (chainActive.Tip() && !chainMostWork.Contains(chainActive.Tip())) { + while (chainActive.Tip() && chainActive.Tip() != pindexFork) { if (!DisconnectTip(state)) 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. - while (!chainActive.Contains(chainMostWork.Tip())) { - CBlockIndex *pindexConnect = chainMostWork[chainActive.Height() + 1]; + BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) { if (!ConnectTip(state, pindexConnect)) { if (state.IsInvalid()) { // The block violates a consensus rule. if (!state.CorruptionPossible()) - InvalidChainFound(chainMostWork.Tip()); + InvalidChainFound(vpindexToConnect.back()); fComplete = false; state = CValidationState(); break; diff --git a/src/main.h b/src/main.h index 23c8660376..98155989a3 100644 --- a/src/main.h +++ b/src/main.h @@ -1079,14 +1079,14 @@ public: /** Find the last common block between this chain and a locator. */ 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. */ 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) */ extern CCoinsViewCache *pcoinsTip; |