Merge #12001: [RPC] Adding ::minRelayTxFee amount to getmempoolinfo and updating...
[bitcoinplatinum.git] / src / chain.cpp
blob79e8bdfa445961d089614339d5e580664fa11ca9
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 const CBlockIndex* CBlockIndex::GetAncestor(int height) const
85 if (height > nHeight || height < 0) {
86 return nullptr;
89 const CBlockIndex* pindexWalk = this;
90 int heightWalk = nHeight;
91 while (heightWalk > height) {
92 int heightSkip = GetSkipHeight(heightWalk);
93 int heightSkipPrev = GetSkipHeight(heightWalk - 1);
94 if (pindexWalk->pskip != nullptr &&
95 (heightSkip == height ||
96 (heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
97 heightSkipPrev >= height)))) {
98 // Only follow pskip if pprev->pskip isn't better than pskip->pprev.
99 pindexWalk = pindexWalk->pskip;
100 heightWalk = heightSkip;
101 } else {
102 assert(pindexWalk->pprev);
103 pindexWalk = pindexWalk->pprev;
104 heightWalk--;
107 return pindexWalk;
110 CBlockIndex* CBlockIndex::GetAncestor(int height)
112 return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height));
115 void CBlockIndex::BuildSkip()
117 if (pprev)
118 pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
121 arith_uint256 GetBlockProof(const CBlockIndex& block)
123 arith_uint256 bnTarget;
124 bool fNegative;
125 bool fOverflow;
126 bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
127 if (fNegative || fOverflow || bnTarget == 0)
128 return 0;
129 // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
130 // as it's too large for an arith_uint256. However, as 2**256 is at least as large
131 // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
132 // or ~bnTarget / (bnTarget+1) + 1.
133 return (~bnTarget / (bnTarget + 1)) + 1;
136 int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
138 arith_uint256 r;
139 int sign = 1;
140 if (to.nChainWork > from.nChainWork) {
141 r = to.nChainWork - from.nChainWork;
142 } else {
143 r = from.nChainWork - to.nChainWork;
144 sign = -1;
146 r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
147 if (r.bits() > 63) {
148 return sign * std::numeric_limits<int64_t>::max();
150 return sign * r.GetLow64();
153 /** Find the last common ancestor two blocks have.
154 * Both pa and pb must be non-nullptr. */
155 const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
156 if (pa->nHeight > pb->nHeight) {
157 pa = pa->GetAncestor(pb->nHeight);
158 } else if (pb->nHeight > pa->nHeight) {
159 pb = pb->GetAncestor(pa->nHeight);
162 while (pa != pb && pa && pb) {
163 pa = pa->pprev;
164 pb = pb->pprev;
167 // Eventually all chain branches meet at the genesis block.
168 assert(pa == pb);
169 return pa;