| 58 | } |
| 59 | |
| 60 | CBlockLocator CChain::GetLocator(const CBlockIndex *pIndex) const { |
| 61 | int32_t nStep = 1; |
| 62 | vector<uint256> vHave; |
| 63 | vHave.reserve(32); |
| 64 | |
| 65 | if (!pIndex) |
| 66 | pIndex = Tip(); |
| 67 | while (pIndex) { |
| 68 | vHave.push_back(pIndex->GetBlockHash()); |
| 69 | // Stop when we have added the genesis block. |
| 70 | if (pIndex->height == 0) |
| 71 | break; |
| 72 | // Exponentially larger steps back, plus the genesis block. |
| 73 | int32_t height = max(pIndex->height - nStep, 0); |
| 74 | // Jump back quickly to the same height as the chain. |
| 75 | if (pIndex->height > height) |
| 76 | pIndex = pIndex->GetAncestor(height); |
| 77 | // In case pIndex is not in this chain, iterate pIndex->pprev to find blocks. |
| 78 | while (!Contains(pIndex)) |
| 79 | pIndex = pIndex->pprev; |
| 80 | |
| 81 | // If pIndex is in this chain, use direct height-based access. |
| 82 | if (pIndex->height > height) |
| 83 | pIndex = (*this)[height]; |
| 84 | |
| 85 | if (vHave.size() > 10) |
| 86 | nStep *= 2; |
| 87 | } |
| 88 | |
| 89 | return CBlockLocator(vHave); |
| 90 | } |
| 91 | |
| 92 | CBlockIndex* CChain::FindFork(map<uint256, CBlockIndex *> &mapBlockIndex, const CBlockLocator &locator) const { |
| 93 | // Find the first block the caller has in the main chain |
no test coverage detected