| 29 | } |
| 30 | |
| 31 | CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const { |
| 32 | int nStep = 1; |
| 33 | std::vector<uint256> vHave; |
| 34 | vHave.reserve(32); |
| 35 | |
| 36 | if (!pindex) |
| 37 | pindex = Tip(); |
| 38 | while (pindex) { |
| 39 | vHave.push_back(pindex->GetBlockHash()); |
| 40 | // Stop when we have added the genesis block. |
| 41 | if (pindex->nHeight == 0) |
| 42 | break; |
| 43 | // Exponentially larger steps back, plus the genesis block. |
| 44 | int nHeight = std::max(pindex->nHeight - nStep, 0); |
| 45 | if (Contains(pindex)) { |
| 46 | // Use O(1) CChain index if possible. |
| 47 | pindex = (*this)[nHeight]; |
| 48 | } else { |
| 49 | // Otherwise, use O(log n) skiplist. |
| 50 | pindex = pindex->GetAncestor(nHeight); |
| 51 | } |
| 52 | if (vHave.size() > 10) |
| 53 | nStep *= 2; |
| 54 | } |
| 55 | |
| 56 | return CBlockLocator(vHave); |
| 57 | } |
| 58 | |
| 59 | void CBlockIndex::untrim() EXCLUSIVE_LOCKS_REQUIRED(::cs_main){ |
| 60 | AssertLockHeld(::cs_main); |