diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2017-04-16 06:57:11 -0700 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2017-06-26 10:45:48 -0700 |
commit | b3a279cd58d9ae0e107c7fee81f598635e53f9e1 (patch) | |
tree | 4ca31d34f211a7fb926e81c8fef27c5cd5f96ccc /src/chain.cpp | |
parent | 234ffc677ee58591b139695bf92bbd6f504ee91a (diff) |
[MOVEONLY] Move LastCommonAncestor to chain
Diffstat (limited to 'src/chain.cpp')
-rw-r--r-- | src/chain.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/chain.cpp b/src/chain.cpp index 8d4c4e7dea..ffd58d471d 100644 --- a/src/chain.cpp +++ b/src/chain.cpp @@ -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; +} |