Skip to content

Commit

Permalink
Track parent->child relations for blocks
Browse files Browse the repository at this point in the history
Allows to cheaply find all possible children of a block.
  • Loading branch information
codablock committed Jan 28, 2019
1 parent 04a51c9 commit 2bf6eb1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
CCriticalSection cs_main;

BlockMap mapBlockIndex;
PrevBlockMap mapPrevBlockIndex;
CChain chainActive;
CBlockIndex *pindexBestHeader = NULL;
CWaitableCriticalSection csBestBlock;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ extern CScript COINBASE_FLAGS;
extern CCriticalSection cs_main;
extern CTxMemPool mempool;
typedef boost::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
typedef std::unordered_multimap<uint256, CBlockIndex*, BlockHasher> PrevBlockMap;
extern BlockMap mapBlockIndex;
extern PrevBlockMap mapPrevBlockIndex;
extern uint64_t nLastBlockTx;
extern uint64_t nLastBlockSize;
extern const std::string strMessageMagic;
Expand Down

0 comments on commit 2bf6eb1

Please sign in to comment.