| 80 | } |
| 81 | |
| 82 | const CBlockIndex* CBlockIndex::GetAncestor(int height) const |
| 83 | { |
| 84 | if (height > nHeight || height < 0) { |
| 85 | return nullptr; |
| 86 | } |
| 87 | |
| 88 | const CBlockIndex* pindexWalk = this; |
| 89 | int heightWalk = nHeight; |
| 90 | while (heightWalk > height) { |
| 91 | int heightSkip = GetSkipHeight(heightWalk); |
| 92 | int heightSkipPrev = GetSkipHeight(heightWalk - 1); |
| 93 | if (pindexWalk->pskip != nullptr && |
| 94 | (heightSkip == height || |
| 95 | (heightSkip > height && !(heightSkipPrev < heightSkip - 2 && |
| 96 | heightSkipPrev >= height)))) { |
| 97 | // Only follow pskip if pprev->pskip isn't better than pskip->pprev. |
| 98 | pindexWalk = pindexWalk->pskip; |
| 99 | heightWalk = heightSkip; |
| 100 | } else { |
| 101 | assert(pindexWalk->pprev); |
| 102 | pindexWalk = pindexWalk->pprev; |
| 103 | heightWalk--; |
| 104 | } |
| 105 | } |
| 106 | return pindexWalk; |
| 107 | } |
| 108 | |
| 109 | CBlockIndex* CBlockIndex::GetAncestor(int height) |
| 110 | { |
no test coverage detected