Skip to content

Commit b3a279c

Browse files
committed
[MOVEONLY] Move LastCommonAncestor to chain
1 parent 234ffc6 commit b3a279c

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

src/chain.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,22 @@ int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& fr
148148
}
149149
return sign * r.GetLow64();
150150
}
151+
152+
/** Find the last common ancestor two blocks have.
153+
* Both pa and pb must be non-NULL. */
154+
const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
155+
if (pa->nHeight > pb->nHeight) {
156+
pa = pa->GetAncestor(pb->nHeight);
157+
} else if (pb->nHeight > pa->nHeight) {
158+
pb = pb->GetAncestor(pa->nHeight);
159+
}
160+
161+
while (pa != pb && pa && pb) {
162+
pa = pa->pprev;
163+
pb = pb->pprev;
164+
}
165+
166+
// Eventually all chain branches meet at the genesis block.
167+
assert(pa == pb);
168+
return pa;
169+
}

src/chain.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ class CBlockIndex
362362
arith_uint256 GetBlockProof(const CBlockIndex& block);
363363
/** Return the time it would take to redo the work difference between from and to, assuming the current hashrate corresponds to the difficulty at tip, in seconds. */
364364
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params&);
365+
/** Find the forking point between two chain tips. */
366+
const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb);
367+
365368

366369
/** Used to marshal pointers into hashes for db storage. */
367370
class CDiskBlockIndex : public CBlockIndex

src/net_processing.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -452,25 +452,6 @@ bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex)
452452
return false;
453453
}
454454

455-
/** Find the last common ancestor two blocks have.
456-
* Both pa and pb must be non-NULL. */
457-
const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
458-
if (pa->nHeight > pb->nHeight) {
459-
pa = pa->GetAncestor(pb->nHeight);
460-
} else if (pb->nHeight > pa->nHeight) {
461-
pb = pb->GetAncestor(pa->nHeight);
462-
}
463-
464-
while (pa != pb && pa && pb) {
465-
pa = pa->pprev;
466-
pb = pb->pprev;
467-
}
468-
469-
// Eventually all chain branches meet at the genesis block.
470-
assert(pa == pb);
471-
return pa;
472-
}
473-
474455
/** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
475456
* at most count entries. */
476457
void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) {

0 commit comments

Comments
 (0)