Skip to content

Commit

Permalink
[MOVEONLY] Move LastCommonAncestor to chain
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Jun 26, 2017
1 parent 234ffc6 commit b3a279c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
19 changes: 19 additions & 0 deletions src/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,22 @@ int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& fr
}
return sign * r.GetLow64();
}

/** Find the last common ancestor two blocks have.
* Both pa and pb must be non-NULL. */
const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
if (pa->nHeight > pb->nHeight) {
pa = pa->GetAncestor(pb->nHeight);
} else if (pb->nHeight > pa->nHeight) {
pb = pb->GetAncestor(pa->nHeight);
}

while (pa != pb && pa && pb) {
pa = pa->pprev;
pb = pb->pprev;
}

// Eventually all chain branches meet at the genesis block.
assert(pa == pb);
return pa;
}
3 changes: 3 additions & 0 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ class CBlockIndex
arith_uint256 GetBlockProof(const CBlockIndex& block);
/** 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. */
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params&);
/** Find the forking point between two chain tips. */
const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb);


/** Used to marshal pointers into hashes for db storage. */
class CDiskBlockIndex : public CBlockIndex
Expand Down
19 changes: 0 additions & 19 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,25 +452,6 @@ bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex)
return false;
}

/** Find the last common ancestor two blocks have.
* Both pa and pb must be non-NULL. */
const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
if (pa->nHeight > pb->nHeight) {
pa = pa->GetAncestor(pb->nHeight);
} else if (pb->nHeight > pa->nHeight) {
pb = pb->GetAncestor(pa->nHeight);
}

while (pa != pb && pa && pb) {
pa = pa->pprev;
pb = pb->pprev;
}

// Eventually all chain branches meet at the genesis block.
assert(pa == pb);
return pa;
}

/** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
* at most count entries. */
void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) {
Expand Down

0 comments on commit b3a279c

Please sign in to comment.