| 2026 | } |
| 2027 | |
| 2028 | CBlockIndex *CBlockIndex::GetAncestor(int32_t heightIn) { |
| 2029 | if (heightIn > height || heightIn < 0) |
| 2030 | return nullptr; |
| 2031 | |
| 2032 | CBlockIndex *pindexWalk = this; |
| 2033 | int32_t heightWalk = height; |
| 2034 | while (heightWalk > heightIn) { |
| 2035 | int32_t heightSkip = GetSkipHeight(heightWalk); |
| 2036 | int32_t heightSkipPrev = GetSkipHeight(heightWalk - 1); |
| 2037 | if (heightSkip == heightIn || |
| 2038 | (heightSkip > heightIn && !(heightSkipPrev < heightSkip - 2 && heightSkipPrev >= heightIn))) { |
| 2039 | // Only follow pskip if pprev->pskip isn't better than pskip->pprev. |
| 2040 | pindexWalk = pindexWalk->pskip; |
| 2041 | heightWalk = heightSkip; |
| 2042 | } else { |
| 2043 | pindexWalk = pindexWalk->pprev; |
| 2044 | heightWalk--; |
| 2045 | } |
| 2046 | } |
| 2047 | return pindexWalk; |
| 2048 | } |
| 2049 | |
| 2050 | const CBlockIndex *CBlockIndex::GetAncestor(int32_t heightIn) const { |
| 2051 | return const_cast<CBlockIndex *>(this)->GetAncestor(heightIn); |
no test coverage detected