Changeset View
Changeset View
Standalone View
Standalone View
src/chain.cpp
Show All 19 Lines | void CChain::SetTip(CBlockIndex *pindex) { | ||||
} | } | ||||
} | } | ||||
CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const { | CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const { | ||||
int nStep = 1; | int nStep = 1; | ||||
std::vector<uint256> vHave; | std::vector<uint256> vHave; | ||||
vHave.reserve(32); | vHave.reserve(32); | ||||
if (!pindex) | if (!pindex) pindex = Tip(); | ||||
pindex = Tip(); | |||||
while (pindex) { | while (pindex) { | ||||
vHave.push_back(pindex->GetBlockHash()); | vHave.push_back(pindex->GetBlockHash()); | ||||
// Stop when we have added the genesis block. | // Stop when we have added the genesis block. | ||||
if (pindex->nHeight == 0) | if (pindex->nHeight == 0) break; | ||||
break; | |||||
// Exponentially larger steps back, plus the genesis block. | // Exponentially larger steps back, plus the genesis block. | ||||
int nHeight = std::max(pindex->nHeight - nStep, 0); | int nHeight = std::max(pindex->nHeight - nStep, 0); | ||||
if (Contains(pindex)) { | if (Contains(pindex)) { | ||||
// Use O(1) CChain index if possible. | // Use O(1) CChain index if possible. | ||||
pindex = (*this)[nHeight]; | pindex = (*this)[nHeight]; | ||||
} else { | } else { | ||||
// Otherwise, use O(log n) skiplist. | // Otherwise, use O(log n) skiplist. | ||||
pindex = pindex->GetAncestor(nHeight); | pindex = pindex->GetAncestor(nHeight); | ||||
} | } | ||||
if (vHave.size() > 10) | if (vHave.size() > 10) nStep *= 2; | ||||
nStep *= 2; | |||||
} | } | ||||
return CBlockLocator(vHave); | return CBlockLocator(vHave); | ||||
} | } | ||||
const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const { | const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const { | ||||
if (pindex == NULL) { | if (pindex == NULL) { | ||||
return NULL; | return NULL; | ||||
} | } | ||||
if (pindex->nHeight > Height()) | if (pindex->nHeight > Height()) pindex = pindex->GetAncestor(Height()); | ||||
pindex = pindex->GetAncestor(Height()); | |||||
while (pindex && !Contains(pindex)) | while (pindex && !Contains(pindex)) | ||||
pindex = pindex->pprev; | pindex = pindex->pprev; | ||||
return pindex; | return pindex; | ||||
} | } | ||||
CBlockIndex* CChain::FindEarliestAtLeast(int64_t nTime) const | CBlockIndex *CChain::FindEarliestAtLeast(int64_t nTime) const { | ||||
{ | std::vector<CBlockIndex *>::const_iterator lower = | ||||
std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), nTime, | std::lower_bound(vChain.begin(), vChain.end(), nTime, | ||||
[](CBlockIndex* pBlock, const int64_t& time) -> bool { return pBlock->GetBlockTimeMax() < time; }); | [](CBlockIndex *pBlock, const int64_t &time) -> bool { | ||||
return pBlock->GetBlockTimeMax() < time; | |||||
}); | |||||
return (lower == vChain.end() ? NULL : *lower); | return (lower == vChain.end() ? NULL : *lower); | ||||
} | } | ||||
/** Turn the lowest '1' bit in the binary representation of a number into a '0'. */ | /** 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); } | */ | ||||
int static inline InvertLowestOne(int n) { | |||||
return n & (n - 1); | |||||
} | |||||
/** Compute what height to jump back to with the CBlockIndex::pskip pointer. */ | /** Compute what height to jump back to with the CBlockIndex::pskip pointer. */ | ||||
int static inline GetSkipHeight(int height) { | int static inline GetSkipHeight(int height) { | ||||
if (height < 2) | if (height < 2) return 0; | ||||
return 0; | |||||
// Determine which height to jump back to. Any number strictly lower than height is acceptable, | // Determine which height to jump back to. Any number strictly lower than | ||||
// but the following expression seems to perform well in simulations (max 110 steps to go back | // height is acceptable, but the following expression seems to perform well | ||||
// up to 2**18 blocks). | // in simulations (max 110 steps to go back up to 2**18 blocks). | ||||
return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height); | return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 | ||||
: InvertLowestOne(height); | |||||
} | } | ||||
CBlockIndex* CBlockIndex::GetAncestor(int height) | CBlockIndex *CBlockIndex::GetAncestor(int height) { | ||||
{ | if (height > nHeight || height < 0) return NULL; | ||||
if (height > nHeight || height < 0) | |||||
return NULL; | |||||
CBlockIndex* pindexWalk = this; | CBlockIndex *pindexWalk = this; | ||||
int heightWalk = nHeight; | int heightWalk = nHeight; | ||||
while (heightWalk > height) { | while (heightWalk > height) { | ||||
int heightSkip = GetSkipHeight(heightWalk); | int heightSkip = GetSkipHeight(heightWalk); | ||||
int heightSkipPrev = GetSkipHeight(heightWalk - 1); | int heightSkipPrev = GetSkipHeight(heightWalk - 1); | ||||
if (pindexWalk->pskip != NULL && | if (pindexWalk->pskip != NULL && | ||||
(heightSkip == height || | (heightSkip == height || (heightSkip > height && | ||||
(heightSkip > height && !(heightSkipPrev < heightSkip - 2 && | !(heightSkipPrev < heightSkip - 2 && | ||||
heightSkipPrev >= height)))) { | heightSkipPrev >= height)))) { | ||||
// Only follow pskip if pprev->pskip isn't better than pskip->pprev. | // Only follow pskip if pprev->pskip isn't better than pskip->pprev. | ||||
pindexWalk = pindexWalk->pskip; | pindexWalk = pindexWalk->pskip; | ||||
heightWalk = heightSkip; | heightWalk = heightSkip; | ||||
} else { | } else { | ||||
assert(pindexWalk->pprev); | assert(pindexWalk->pprev); | ||||
pindexWalk = pindexWalk->pprev; | pindexWalk = pindexWalk->pprev; | ||||
heightWalk--; | heightWalk--; | ||||
} | } | ||||
} | } | ||||
return pindexWalk; | return pindexWalk; | ||||
} | } | ||||
const CBlockIndex* CBlockIndex::GetAncestor(int height) const | const CBlockIndex *CBlockIndex::GetAncestor(int height) const { | ||||
{ | |||||
return const_cast<CBlockIndex*>(this)->GetAncestor(height); | return const_cast<CBlockIndex *>(this)->GetAncestor(height); | ||||
} | } | ||||
void CBlockIndex::BuildSkip() | void CBlockIndex::BuildSkip() { | ||||
{ | if (pprev) pskip = pprev->GetAncestor(GetSkipHeight(nHeight)); | ||||
if (pprev) | |||||
pskip = pprev->GetAncestor(GetSkipHeight(nHeight)); | |||||
} | } | ||||
arith_uint256 GetBlockProof(const CBlockIndex& block) | arith_uint256 GetBlockProof(const CBlockIndex &block) { | ||||
{ | |||||
arith_uint256 bnTarget; | arith_uint256 bnTarget; | ||||
bool fNegative; | bool fNegative; | ||||
bool fOverflow; | bool fOverflow; | ||||
bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow); | bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow); | ||||
if (fNegative || fOverflow || bnTarget == 0) | if (fNegative || fOverflow || bnTarget == 0) return 0; | ||||
return 0; | |||||
// We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256 | // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256 | ||||
// as it's too large for a arith_uint256. However, as 2**256 is at least as large | // as it's too large for a arith_uint256. However, as 2**256 is at least as | ||||
// as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1, | // large as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / | ||||
// or ~bnTarget / (nTarget+1) + 1. | // (bnTarget+1)) + 1, or ~bnTarget / (nTarget+1) + 1. | ||||
return (~bnTarget / (bnTarget + 1)) + 1; | return (~bnTarget / (bnTarget + 1)) + 1; | ||||
} | } | ||||
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params) | int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, | ||||
{ | const CBlockIndex &from, | ||||
const CBlockIndex &tip, | |||||
const Consensus::Params ¶ms) { | |||||
arith_uint256 r; | arith_uint256 r; | ||||
int sign = 1; | int sign = 1; | ||||
if (to.nChainWork > from.nChainWork) { | if (to.nChainWork > from.nChainWork) { | ||||
r = to.nChainWork - from.nChainWork; | r = to.nChainWork - from.nChainWork; | ||||
} else { | } else { | ||||
r = from.nChainWork - to.nChainWork; | r = from.nChainWork - to.nChainWork; | ||||
sign = -1; | sign = -1; | ||||
} | } | ||||
r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip); | r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip); | ||||
if (r.bits() > 63) { | if (r.bits() > 63) { | ||||
return sign * std::numeric_limits<int64_t>::max(); | return sign * std::numeric_limits<int64_t>::max(); | ||||
} | } | ||||
return sign * r.GetLow64(); | return sign * r.GetLow64(); | ||||
} | } |