Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present 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.
5 : :
6 : : #include <chain.h>
7 : : #include <tinyformat.h>
8 : : #include <util/check.h>
9 : :
10 : 0 : std::string CBlockIndex::ToString() const
11 : : {
12 : 0 : return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
13 [ # # # # ]: 0 : pprev, nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString());
14 : : }
15 : :
16 : 465662 : void CChain::SetTip(CBlockIndex& block)
17 : : {
18 : 465662 : CBlockIndex* pindex = █
19 : 465662 : vChain.resize(pindex->nHeight + 1);
20 [ + + + + ]: 3857562 : while (pindex && vChain[pindex->nHeight] != pindex) {
21 : 2926238 : vChain[pindex->nHeight] = pindex;
22 : 2926238 : pindex = pindex->pprev;
23 : : }
24 : 465662 : }
25 : :
26 : 215 : std::vector<uint256> LocatorEntries(const CBlockIndex* index)
27 : : {
28 : 215 : int step = 1;
29 : 215 : std::vector<uint256> have;
30 [ + + ]: 215 : if (index == nullptr) return have;
31 : :
32 [ + - ]: 213 : have.reserve(32);
33 [ + - ]: 4457 : while (index) {
34 [ + - ]: 4457 : have.emplace_back(index->GetBlockHash());
35 [ + + ]: 4457 : if (index->nHeight == 0) break;
36 : : // Exponentially larger steps back, plus the genesis block.
37 [ + + ]: 4244 : int height = std::max(index->nHeight - step, 0);
38 : : // Use skiplist.
39 [ + - ]: 4244 : index = index->GetAncestor(height);
40 [ - + + + ]: 4244 : if (have.size() > 10) step *= 2;
41 : : }
42 : : return have;
43 : 0 : }
44 : :
45 : 206 : CBlockLocator GetLocator(const CBlockIndex* index)
46 : : {
47 : 206 : return CBlockLocator{LocatorEntries(index)};
48 : : }
49 : :
50 : 16370 : const CBlockIndex* CChain::FindFork(const CBlockIndex& index) const
51 : : {
52 : 16370 : const auto* pindex{&index};
53 [ - + + + ]: 16370 : if (pindex->nHeight > Height())
54 : 8243 : pindex = pindex->GetAncestor(Height());
55 [ + + + + ]: 17219 : while (pindex && !Contains(*pindex))
56 : 849 : pindex = pindex->pprev;
57 : 16370 : return pindex;
58 : : }
59 : :
60 : 10025 : CBlockIndex* CChain::FindEarliestAtLeast(int64_t nTime, int height) const
61 : : {
62 : 10025 : std::pair<int64_t, int> blockparams = std::make_pair(nTime, height);
63 : 10025 : std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), blockparams,
64 [ + + + + ]: 168590 : [](CBlockIndex* pBlock, const std::pair<int64_t, int>& blockparams) -> bool { return pBlock->GetBlockTimeMax() < blockparams.first || pBlock->nHeight < blockparams.second; });
65 [ + + ]: 10025 : return (lower == vChain.end() ? nullptr : *lower);
66 : : }
67 : :
68 : : /** Turn the lowest '1' bit in the binary representation of a number into a '0'. */
69 : 54467825 : int static inline InvertLowestOne(int n) { return n & (n - 1); }
70 : :
71 : : /** Compute what height to jump back to with the CBlockIndex::pskip pointer. */
72 : 54488738 : int static inline GetSkipHeight(int height) {
73 [ + + ]: 54488738 : if (height < 2)
74 : : return 0;
75 : :
76 : : // Determine which height to jump back to. Any number strictly lower than height is acceptable,
77 : : // but the following expression seems to perform well in simulations (max 110 steps to go back
78 : : // up to 2**18 blocks).
79 [ + + ]: 54467825 : return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
80 : : }
81 : :
82 : 7263867 : const CBlockIndex* CBlockIndex::GetAncestor(int height) const
83 : : {
84 [ + + + + ]: 7263867 : if (height > nHeight || height < 0) {
85 : : return nullptr;
86 : : }
87 : :
88 : : const CBlockIndex* pindexWalk = this;
89 : : int heightWalk = nHeight;
90 [ + + ]: 30969064 : while (heightWalk > height) {
91 : 24046301 : int heightSkip = GetSkipHeight(heightWalk);
92 : 24046301 : int heightSkipPrev = GetSkipHeight(heightWalk - 1);
93 [ + + + + ]: 24046301 : if (pindexWalk->pskip != nullptr &&
94 [ + + ]: 20460251 : (heightSkip == height ||
95 [ + + + + ]: 9324515 : (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 [ - + ]: 12127521 : assert(pindexWalk->pprev);
102 : : pindexWalk = pindexWalk->pprev;
103 : : heightWalk--;
104 : : }
105 : : }
106 : : return pindexWalk;
107 : : }
108 : :
109 : 6437094 : CBlockIndex* CBlockIndex::GetAncestor(int height)
110 : : {
111 : 6437094 : return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height));
112 : : }
113 : :
114 : 6396466 : void CBlockIndex::BuildSkip()
115 : : {
116 [ + + ]: 6396466 : if (pprev)
117 : 6396136 : pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
118 : 6396466 : }
119 : :
120 : 115504 : arith_uint256 GetBitsProof(uint32_t bits)
121 : : {
122 : 115504 : arith_uint256 bnTarget;
123 : 115504 : bool fNegative;
124 : 115504 : bool fOverflow;
125 : 115504 : bnTarget.SetCompact(bits, &fNegative, &fOverflow);
126 [ + - + - : 231008 : if (fNegative || fOverflow || bnTarget == 0)
- + ]
127 : 0 : 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 : 231008 : return (~bnTarget / (bnTarget + 1)) + 1;
133 : : }
134 : :
135 : 1000 : int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
136 : : {
137 : 1000 : arith_uint256 r;
138 : 1000 : int sign = 1;
139 [ + + ]: 1000 : if (to.nChainWork > from.nChainWork) {
140 : 485 : r = to.nChainWork - from.nChainWork;
141 : : } else {
142 : 515 : r = from.nChainWork - to.nChainWork;
143 : 515 : sign = -1;
144 : : }
145 : 2000 : r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
146 [ - + ]: 1000 : if (r.bits() > 63) {
147 : 0 : return sign * std::numeric_limits<int64_t>::max();
148 : : }
149 : 1000 : return sign * int64_t(r.GetLow64());
150 : : }
151 : :
152 : : /** Find the last common ancestor two blocks have.
153 : : * Both pa and pb must be non-nullptr. */
154 : 100001 : const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) {
155 : : // First rewind to the last common height (the forking point cannot be past one of the two).
156 [ + + ]: 100001 : if (pa->nHeight > pb->nHeight) {
157 : 49760 : pa = pa->GetAncestor(pb->nHeight);
158 [ + + ]: 50241 : } else if (pb->nHeight > pa->nHeight) {
159 : 49662 : pb = pb->GetAncestor(pa->nHeight);
160 : : }
161 [ + + ]: 767211 : while (pa != pb) {
162 : : // Jump back until pa and pb have a common "skip" ancestor.
163 [ + + ]: 1147441 : while (pa->pskip != pb->pskip) {
164 : : // This logic relies on the property that equal-height blocks have equal-height skip
165 : : // pointers.
166 : 480231 : Assume(pa->nHeight == pb->nHeight);
167 : 480231 : Assume(pa->pskip->nHeight == pb->pskip->nHeight);
168 : 480231 : pa = pa->pskip;
169 : 480231 : pb = pb->pskip;
170 : : }
171 : : // At this point, pa and pb are different, but have equal pskip. The forking point lies in
172 : : // between pa/pb on the one end, and pa->pskip/pb->pskip on the other end.
173 : 667210 : pa = pa->pprev;
174 : 667210 : pb = pb->pprev;
175 : : }
176 : 100001 : return pa;
177 : : }
|