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