diff --git a/src/validation.cpp b/src/validation.cpp index 87647dae7..b020fa1e4 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -68,6 +68,7 @@ CCriticalSection cs_main; BlockMap mapBlockIndex; +PrevBlockMap mapPrevBlockIndex; CChain chainActive; CBlockIndex *pindexBestHeader = NULL; CWaitableCriticalSection csBestBlock; @@ -3042,6 +3043,11 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block) setDirtyBlockIndex.insert(pindexNew); + // track prevBlockHash -> pindex (multimap) + if (pindexNew->pprev) { + mapPrevBlockIndex.emplace(pindexNew->pprev->GetBlockHash(), pindexNew); + } + return pindexNew; } @@ -3827,6 +3833,11 @@ bool static LoadBlockIndexDB(const CChainParams& chainparams) { CBlockIndex* pindex = item.second; vSortedByHeight.push_back(std::make_pair(pindex->nHeight, pindex)); + + // build mapPrevBlockIndex + if (pindex->pprev) { + mapPrevBlockIndex.emplace(pindex->pprev->GetBlockHash(), pindex); + } } sort(vSortedByHeight.begin(), vSortedByHeight.end()); BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight) diff --git a/src/validation.h b/src/validation.h index 2577a8a00..5dc3c1654 100644 --- a/src/validation.h +++ b/src/validation.h @@ -157,7 +157,9 @@ extern CScript COINBASE_FLAGS; extern CCriticalSection cs_main; extern CTxMemPool mempool; typedef boost::unordered_map BlockMap; +typedef std::unordered_multimap PrevBlockMap; extern BlockMap mapBlockIndex; +extern PrevBlockMap mapPrevBlockIndex; extern uint64_t nLastBlockTx; extern uint64_t nLastBlockSize; extern const std::string strMessageMagic;