Skip to content

Commit 5ea3bc0

Browse files
committed
Move remaining CBlockIndex methods to chain.cpp
1 parent 9dcd524 commit 5ea3bc0

File tree

2 files changed

+49
-48
lines changed

2 files changed

+49
-48
lines changed

src/chain.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,52 @@ const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
5757
pindex = pindex->pprev;
5858
return pindex;
5959
}
60+
61+
/** Turn the lowest '1' bit in the binary representation of a number into a '0'. */
62+
int static inline InvertLowestOne(int n) { return n & (n - 1); }
63+
64+
/** Compute what height to jump back to with the CBlockIndex::pskip pointer. */
65+
int static inline GetSkipHeight(int height) {
66+
if (height < 2)
67+
return 0;
68+
69+
// Determine which height to jump back to. Any number strictly lower than height is acceptable,
70+
// but the following expression seems to perform well in simulations (max 110 steps to go back
71+
// up to 2**18 blocks).
72+
return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
73+
}
74+
75+
CBlockIndex* CBlockIndex::GetAncestor(int height)
76+
{
77+
if (height > nHeight || height < 0)
78+
return NULL;
79+
80+
CBlockIndex* pindexWalk = this;
81+
int heightWalk = nHeight;
82+
while (heightWalk > height) {
83+
int heightSkip = GetSkipHeight(heightWalk);
84+
int heightSkipPrev = GetSkipHeight(heightWalk - 1);
85+
if (heightSkip == height ||
86+
(heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
87+
heightSkipPrev >= height))) {
88+
// Only follow pskip if pprev->pskip isn't better than pskip->pprev.
89+
pindexWalk = pindexWalk->pskip;
90+
heightWalk = heightSkip;
91+
} else {
92+
pindexWalk = pindexWalk->pprev;
93+
heightWalk--;
94+
}
95+
}
96+
return pindexWalk;
97+
}
98+
99+
const CBlockIndex* CBlockIndex::GetAncestor(int height) const
100+
{
101+
return const_cast<CBlockIndex*>(this)->GetAncestor(height);
102+
}
103+
104+
void CBlockIndex::BuildSkip()
105+
{
106+
if (pprev)
107+
pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
108+
}

src/main.cpp

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,54 +2618,6 @@ static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned
26182618
return (nFound >= nRequired);
26192619
}
26202620

2621-
/** Turn the lowest '1' bit in the binary representation of a number into a '0'. */
2622-
int static inline InvertLowestOne(int n) { return n & (n - 1); }
2623-
2624-
/** Compute what height to jump back to with the CBlockIndex::pskip pointer. */
2625-
int static inline GetSkipHeight(int height) {
2626-
if (height < 2)
2627-
return 0;
2628-
2629-
// Determine which height to jump back to. Any number strictly lower than height is acceptable,
2630-
// but the following expression seems to perform well in simulations (max 110 steps to go back
2631-
// up to 2**18 blocks).
2632-
return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
2633-
}
2634-
2635-
CBlockIndex* CBlockIndex::GetAncestor(int height)
2636-
{
2637-
if (height > nHeight || height < 0)
2638-
return NULL;
2639-
2640-
CBlockIndex* pindexWalk = this;
2641-
int heightWalk = nHeight;
2642-
while (heightWalk > height) {
2643-
int heightSkip = GetSkipHeight(heightWalk);
2644-
int heightSkipPrev = GetSkipHeight(heightWalk - 1);
2645-
if (heightSkip == height ||
2646-
(heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
2647-
heightSkipPrev >= height))) {
2648-
// Only follow pskip if pprev->pskip isn't better than pskip->pprev.
2649-
pindexWalk = pindexWalk->pskip;
2650-
heightWalk = heightSkip;
2651-
} else {
2652-
pindexWalk = pindexWalk->pprev;
2653-
heightWalk--;
2654-
}
2655-
}
2656-
return pindexWalk;
2657-
}
2658-
2659-
const CBlockIndex* CBlockIndex::GetAncestor(int height) const
2660-
{
2661-
return const_cast<CBlockIndex*>(this)->GetAncestor(height);
2662-
}
2663-
2664-
void CBlockIndex::BuildSkip()
2665-
{
2666-
if (pprev)
2667-
pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
2668-
}
26692621

26702622
bool ProcessNewBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBlockPos *dbp)
26712623
{

0 commit comments

Comments
 (0)