Merge #11747: Fix: Open files read only if requested
[bitcoinplatinum.git] / src / chain.cpp
blob9f40c41fde5d5ddac690a5950c99c8605568386d
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include <chain.h>
8 /**
9 * CChain implementation
11 void CChain::SetTip(CBlockIndex *pindex) {
12 if (pindex == nullptr) {
13 vChain.clear();
14 return;
16 vChain.resize(pindex->nHeight + 1);
17 while (pindex && vChain[pindex->nHeight] != pindex) {
18 vChain[pindex->nHeight] = pindex;
19 pindex = pindex->pprev;
23 CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
24 int nStep = 1;
25 std::vector<uint256> vHave;
26 vHave.reserve(32);
28 if (!pindex)
29 pindex = Tip();
30 while (pindex) {
31 vHave.push_back(pindex->GetBlockHash());
32 // Stop when we have added the genesis block.
33 if (pindex->nHeight == 0)
34 break;
35 // Exponentially larger steps back, plus the genesis block.
36 int nHeight = std::max(pindex->nHeight - nStep, 0);
37 if (Contains(pindex)) {
38 // Use O(1) CChain index if possible.
39 pindex = (*this)[nHeight];
40 } else {
41 // Otherwise, use O(log n) skiplist.
42 pindex = pindex->GetAncestor(nHeight);
44 if (vHave.size() > 10)
45 nStep *= 2;
48 return CBlockLocator(vHave);
51 const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
52 if (pindex == nullptr) {
53 return nullptr;
55 if (pindex->nHeight > Height())
56 pindex = pindex->GetAncestor(Height());
57 while (pindex && !Contains(pindex))
58 pindex = pindex->pprev;
59 return pindex;
62 CBlockIndex* CChain::FindEarliestAtLeast(int64_t nTime) const
64 std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), nTime,
65 [](CBlockIndex* pBlock, const int64_t& time) -> bool { return pBlock->GetBlockTimeMax() < time; });
66 return (lower == vChain.end() ? nullptr : *lower);
69 /** Turn the lowest '1' bit in the binary representation of a number into a '0'. */
70 int static inline InvertLowestOne(int n) { return n & (n - 1); }
72 /** Compute what height to jump back to with the CBlockIndex::pskip pointer. */
73 int static inline GetSkipHeight(int height) {
74 if (height < 2)
75 return 0;
77 // Determine which height to jump back to. Any number strictly lower than height is acceptable,
78 // but the following expression seems to perform well in simulations (max 110 steps to go back
79 // up to 2**18 blocks).
80 return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
83 CBlockIndex* CBlockIndex::GetAncestor(int height)
85 if (height > nHeight || height < 0)
86 return nullptr;
88 CBlockIndex* pindexWalk = this;
89 int heightWalk = nHeight;
90 while (heightWalk > height) {
91 int heightSkip = GetSkipHeight(heightWalk);
92 int heightSkipPrev = GetSkipHeight(heightWalk - 1);
93 if (pindexWalk->pskip != nullptr &&
94 (heightSkip == height ||
95 (heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
96 heightSkipPrev >= height)))) {
97 // Only follow pskip if pprev->pskip isn't better than pskip->pprev.
98 pindexWalk = pindexWalk->pskip;
99 heightWalk = heightSkip;
100 } else {
101 assert(pindexWalk->pprev);
102 pindexWalk = pindexWalk->pprev;
103 heightWalk--;
106 return pindexWalk;
109 const CBlockIndex* CBlockIndex::GetAncestor(int height) const
111 return const_cast<CBlockIndex*>(this)->GetAncestor(height);
114 void CBlockIndex::BuildSkip()
116 if (pprev)
117 pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
120 arith_uint256 GetBlockProof(const CBlockIndex& block)
122 arith_uint256 bnTarget;
123 bool fNegative;
124 bool fOverflow;
125 bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
126 if (fNegative || fOverflow || bnTarget == 0)
127 return 0;
128 // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
129 // as it's too large for an arith_uint256. However, as 2**256 is at least as large
130 // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
131 // or ~bnTarget / (bnTarget+1) + 1.
132 return (~bnTarget / (bnTarget + 1)) + 1;
135 int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
137 arith_uint256 r;
138 int sign = 1;
139 if (to.nChainWork > from.nChainWork) {
140 r = to.nChainWork - from.nChainWork;
141 } else {
142 r = from.nChainWork - to.nChainWork;
143 sign = -1;
145 r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
146 if (r.bits() > 63) {
147 return sign * std::numeric_limits<int64_t>::max();
149 return sign * r.GetLow64();
152 /** Find the last common ancestor two blocks have.
153 * Both pa and pb must be non-nullptr. */
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);
161 while (pa != pb && pa && pb) {
162 pa = pa->pprev;
163 pb = pb->pprev;
166 // Eventually all chain branches meet at the genesis block.
167 assert(pa == pb);
168 return pa;