2014-09-03 02:20:09 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-12-17 02:47:57 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
2014-09-29 08:22:03 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-09-03 02:20:09 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "chain.h"
|
|
|
|
|
2014-10-25 10:46:54 +02:00
|
|
|
/**
|
|
|
|
* CChain implementation
|
|
|
|
*/
|
2014-10-20 03:41:37 +02:00
|
|
|
void CChain::SetTip(CBlockIndex *pindex) {
|
2019-08-06 05:08:33 +02:00
|
|
|
if (pindex == nullptr) {
|
2014-09-03 02:20:09 +02:00
|
|
|
vChain.clear();
|
2014-10-20 03:41:37 +02:00
|
|
|
return;
|
2014-09-03 02:20:09 +02:00
|
|
|
}
|
|
|
|
vChain.resize(pindex->nHeight + 1);
|
|
|
|
while (pindex && vChain[pindex->nHeight] != pindex) {
|
|
|
|
vChain[pindex->nHeight] = pindex;
|
|
|
|
pindex = pindex->pprev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
|
|
|
|
int nStep = 1;
|
|
|
|
std::vector<uint256> vHave;
|
|
|
|
vHave.reserve(32);
|
|
|
|
|
|
|
|
if (!pindex)
|
|
|
|
pindex = Tip();
|
|
|
|
while (pindex) {
|
|
|
|
vHave.push_back(pindex->GetBlockHash());
|
|
|
|
// Stop when we have added the genesis block.
|
|
|
|
if (pindex->nHeight == 0)
|
|
|
|
break;
|
|
|
|
// Exponentially larger steps back, plus the genesis block.
|
|
|
|
int nHeight = std::max(pindex->nHeight - nStep, 0);
|
|
|
|
if (Contains(pindex)) {
|
|
|
|
// Use O(1) CChain index if possible.
|
|
|
|
pindex = (*this)[nHeight];
|
|
|
|
} else {
|
|
|
|
// Otherwise, use O(log n) skiplist.
|
|
|
|
pindex = pindex->GetAncestor(nHeight);
|
|
|
|
}
|
|
|
|
if (vHave.size() > 10)
|
|
|
|
nStep *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CBlockLocator(vHave);
|
|
|
|
}
|
|
|
|
|
|
|
|
const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
|
2019-08-06 05:08:33 +02:00
|
|
|
if (pindex == nullptr) {
|
|
|
|
return nullptr;
|
2014-11-18 22:16:32 +01:00
|
|
|
}
|
2014-09-03 02:20:09 +02:00
|
|
|
if (pindex->nHeight > Height())
|
|
|
|
pindex = pindex->GetAncestor(Height());
|
|
|
|
while (pindex && !Contains(pindex))
|
|
|
|
pindex = pindex->pprev;
|
|
|
|
return pindex;
|
|
|
|
}
|
2014-11-23 14:07:38 +01:00
|
|
|
|
2017-01-14 00:48:28 +01:00
|
|
|
CBlockIndex* CChain::FindEarliestAtLeast(int64_t nTime) const
|
2016-10-20 09:04:18 +02:00
|
|
|
{
|
|
|
|
std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), nTime,
|
2017-01-14 00:48:28 +01:00
|
|
|
[](CBlockIndex* pBlock, const int64_t& time) -> bool { return pBlock->GetBlockTimeMax() < time; });
|
2019-08-06 05:08:33 +02:00
|
|
|
return (lower == vChain.end() ? nullptr : *lower);
|
2016-10-20 09:04:18 +02:00
|
|
|
}
|
|
|
|
|
2014-11-23 14:07:38 +01:00
|
|
|
/** Turn the lowest '1' bit in the binary representation of a number into a '0'. */
|
|
|
|
int static inline InvertLowestOne(int n) { return n & (n - 1); }
|
|
|
|
|
|
|
|
/** Compute what height to jump back to with the CBlockIndex::pskip pointer. */
|
|
|
|
int static inline GetSkipHeight(int height) {
|
|
|
|
if (height < 2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Determine which height to jump back to. Any number strictly lower than height is acceptable,
|
|
|
|
// but the following expression seems to perform well in simulations (max 110 steps to go back
|
|
|
|
// up to 2**18 blocks).
|
|
|
|
return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
|
|
|
|
}
|
|
|
|
|
2017-12-01 15:17:25 +01:00
|
|
|
const CBlockIndex* CBlockIndex::GetAncestor(int height) const
|
2014-11-23 14:07:38 +01:00
|
|
|
{
|
2017-12-01 15:17:25 +01:00
|
|
|
if (height > nHeight || height < 0) {
|
2019-08-06 05:08:33 +02:00
|
|
|
return nullptr;
|
2017-12-01 15:17:25 +01:00
|
|
|
}
|
2014-11-23 14:07:38 +01:00
|
|
|
|
2017-12-01 15:17:25 +01:00
|
|
|
const CBlockIndex* pindexWalk = this;
|
2014-11-23 14:07:38 +01:00
|
|
|
int heightWalk = nHeight;
|
|
|
|
while (heightWalk > height) {
|
|
|
|
int heightSkip = GetSkipHeight(heightWalk);
|
|
|
|
int heightSkipPrev = GetSkipHeight(heightWalk - 1);
|
2019-08-06 05:08:33 +02:00
|
|
|
if (pindexWalk->pskip != nullptr &&
|
2015-03-19 13:34:06 +01:00
|
|
|
(heightSkip == height ||
|
|
|
|
(heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
|
|
|
|
heightSkipPrev >= height)))) {
|
2014-11-23 14:07:38 +01:00
|
|
|
// Only follow pskip if pprev->pskip isn't better than pskip->pprev.
|
|
|
|
pindexWalk = pindexWalk->pskip;
|
|
|
|
heightWalk = heightSkip;
|
|
|
|
} else {
|
2016-04-26 14:34:40 +02:00
|
|
|
assert(pindexWalk->pprev);
|
2014-11-23 14:07:38 +01:00
|
|
|
pindexWalk = pindexWalk->pprev;
|
|
|
|
heightWalk--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pindexWalk;
|
|
|
|
}
|
|
|
|
|
2017-12-01 15:17:25 +01:00
|
|
|
CBlockIndex* CBlockIndex::GetAncestor(int height)
|
2014-11-23 14:07:38 +01:00
|
|
|
{
|
2017-12-01 15:17:25 +01:00
|
|
|
return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height));
|
2014-11-23 14:07:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBlockIndex::BuildSkip()
|
|
|
|
{
|
|
|
|
if (pprev)
|
|
|
|
pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
|
|
|
|
}
|
2016-02-02 14:15:36 +01:00
|
|
|
|
|
|
|
arith_uint256 GetBlockProof(const CBlockIndex& block)
|
|
|
|
{
|
|
|
|
arith_uint256 bnTarget;
|
|
|
|
bool fNegative;
|
|
|
|
bool fOverflow;
|
|
|
|
bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
|
|
|
|
if (fNegative || fOverflow || bnTarget == 0)
|
|
|
|
return 0;
|
|
|
|
// We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
|
2017-06-22 20:36:16 +02:00
|
|
|
// as it's too large for an arith_uint256. However, as 2**256 is at least as large
|
2016-02-02 14:15:36 +01:00
|
|
|
// as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
|
2017-10-10 08:56:08 +02:00
|
|
|
// or ~bnTarget / (bnTarget+1) + 1.
|
2016-02-02 14:15:36 +01:00
|
|
|
return (~bnTarget / (bnTarget + 1)) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
|
|
|
|
{
|
|
|
|
arith_uint256 r;
|
|
|
|
int sign = 1;
|
|
|
|
if (to.nChainWork > from.nChainWork) {
|
|
|
|
r = to.nChainWork - from.nChainWork;
|
|
|
|
} else {
|
|
|
|
r = from.nChainWork - to.nChainWork;
|
|
|
|
sign = -1;
|
|
|
|
}
|
|
|
|
r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
|
|
|
|
if (r.bits() > 63) {
|
|
|
|
return sign * std::numeric_limits<int64_t>::max();
|
|
|
|
}
|
|
|
|
return sign * r.GetLow64();
|
|
|
|
}
|
2017-06-28 18:24:32 +02:00
|
|
|
|
|
|
|
/** Find the last common ancestor two blocks have.
|
2019-08-06 05:08:33 +02:00
|
|
|
* Both pa and pb must be non-nullptr. */
|
2017-06-28 18:24:32 +02:00
|
|
|
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;
|
|
|
|
}
|