| 111 | } |
| 112 | |
| 113 | const CBlockIndex* CBlockIndex::GetAncestor(int height) const |
| 114 | { |
| 115 | if (height > nHeight || height < 0) { |
| 116 | return nullptr; |
| 117 | } |
| 118 | |
| 119 | const CBlockIndex* pindexWalk = this; |
| 120 | int heightWalk = nHeight; |
| 121 | while (heightWalk > height) { |
| 122 | int heightSkip = GetSkipHeight(heightWalk); |
| 123 | int heightSkipPrev = GetSkipHeight(heightWalk - 1); |
| 124 | if (pindexWalk->pskip != nullptr && |
| 125 | (heightSkip == height || |
| 126 | (heightSkip > height && !(heightSkipPrev < heightSkip - 2 && |
| 127 | heightSkipPrev >= height)))) { |
| 128 | // Only follow pskip if pprev->pskip isn't better than pskip->pprev. |
| 129 | pindexWalk = pindexWalk->pskip; |
| 130 | heightWalk = heightSkip; |
| 131 | } else { |
| 132 | assert(pindexWalk->pprev); |
| 133 | pindexWalk = pindexWalk->pprev; |
| 134 | heightWalk--; |
| 135 | } |
| 136 | } |
| 137 | return pindexWalk; |
| 138 | } |
| 139 | |
| 140 | CBlockIndex* CBlockIndex::GetAncestor(int height) |
| 141 | { |