Line | Count | Source |
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 | | std::string CBlockIndex::ToString() const |
11 | 0 | { |
12 | 0 | return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)", |
13 | 0 | pprev, nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString()); |
14 | 0 | } |
15 | | |
16 | | void CChain::SetTip(CBlockIndex& block) |
17 | 514k | { |
18 | 514k | CBlockIndex* pindex = █ |
19 | 514k | vChain.resize(pindex->nHeight + 1); |
20 | 91.6M | while (pindex && vChain[pindex->nHeight] != pindex) { Branch (20:12): [True: 91.1M, False: 450k]
Branch (20:22): [True: 91.1M, False: 64.5k]
|
21 | 91.1M | vChain[pindex->nHeight] = pindex; |
22 | 91.1M | pindex = pindex->pprev; |
23 | 91.1M | } |
24 | 514k | } |
25 | | |
26 | | std::vector<uint256> LocatorEntries(const CBlockIndex* index) |
27 | 643k | { |
28 | 643k | int step = 1; |
29 | 643k | std::vector<uint256> have; |
30 | 643k | if (index == nullptr) return have; Branch (30:9): [True: 0, False: 643k]
|
31 | | |
32 | 643k | have.reserve(32); |
33 | 12.2M | while (index) { Branch (33:12): [True: 12.2M, False: 0]
|
34 | 12.2M | have.emplace_back(index->GetBlockHash()); |
35 | 12.2M | if (index->nHeight == 0) break; Branch (35:13): [True: 643k, False: 11.5M]
|
36 | | // Exponentially larger steps back, plus the genesis block. |
37 | 11.5M | int height = std::max(index->nHeight - step, 0); |
38 | | // Use skiplist. |
39 | 11.5M | index = index->GetAncestor(height); |
40 | 11.5M | if (have.size() > 10) step *= 2; Branch (40:13): [True: 5.14M, False: 6.43M]
|
41 | 11.5M | } |
42 | 643k | return have; |
43 | 643k | } |
44 | | |
45 | | CBlockLocator GetLocator(const CBlockIndex* index) |
46 | 643k | { |
47 | 643k | return CBlockLocator{LocatorEntries(index)}; |
48 | 643k | } |
49 | | |
50 | | const CBlockIndex* CChain::FindFork(const CBlockIndex& index) const |
51 | 74.5k | { |
52 | 74.5k | const auto* pindex{&index}; |
53 | 74.5k | if (pindex->nHeight > Height()) Branch (53:9): [True: 37.8k, False: 36.6k]
|
54 | 37.8k | pindex = pindex->GetAncestor(Height()); |
55 | 105k | while (pindex && !Contains(*pindex)) Branch (55:12): [True: 105k, False: 27]
Branch (55:22): [True: 30.7k, False: 74.4k]
|
56 | 30.7k | pindex = pindex->pprev; |
57 | 74.5k | return pindex; |
58 | 74.5k | } |
59 | | |
60 | | CBlockIndex* CChain::FindEarliestAtLeast(int64_t nTime, int height) const |
61 | 0 | { |
62 | 0 | std::pair<int64_t, int> blockparams = std::make_pair(nTime, height); |
63 | 0 | std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), blockparams, |
64 | 0 | [](CBlockIndex* pBlock, const std::pair<int64_t, int>& blockparams) -> bool { return pBlock->GetBlockTimeMax() < blockparams.first || pBlock->nHeight < blockparams.second; }); Branch (64:94): [True: 0, False: 0]
Branch (64:143): [True: 0, False: 0]
|
65 | 0 | return (lower == vChain.end() ? nullptr : *lower); Branch (65:13): [True: 0, False: 0]
|
66 | 0 | } |
67 | | |
68 | | /** Turn the lowest '1' bit in the binary representation of a number into a '0'. */ |
69 | 116M | 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 | 79.2M | int static inline GetSkipHeight(int height) { |
73 | 79.2M | if (height < 2) Branch (73:9): [True: 1.40M, False: 77.8M]
|
74 | 1.40M | 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 | 77.8M | return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height); Branch (79:12): [True: 38.9M, False: 38.9M]
|
80 | 79.2M | } |
81 | | |
82 | | const CBlockIndex* CBlockIndex::GetAncestor(int height) const |
83 | 120M | { |
84 | 120M | if (height > nHeight || height < 0) { Branch (84:9): [True: 5.69M, False: 115M]
Branch (84:29): [True: 2.17k, False: 115M]
|
85 | 5.69M | return nullptr; |
86 | 5.69M | } |
87 | | |
88 | 115M | const CBlockIndex* pindexWalk = this; |
89 | 115M | int heightWalk = nHeight; |
90 | 154M | while (heightWalk > height) { Branch (90:12): [True: 39.5M, False: 115M]
|
91 | 39.5M | int heightSkip = GetSkipHeight(heightWalk); |
92 | 39.5M | int heightSkipPrev = GetSkipHeight(heightWalk - 1); |
93 | 39.5M | if (pindexWalk->pskip != nullptr && Branch (93:13): [True: 39.5M, False: 2.28k]
|
94 | 39.5M | (heightSkip == height || Branch (94:14): [True: 3.70M, False: 35.8M]
|
95 | 39.5M | (heightSkip > height && !(heightSkipPrev < heightSkip - 2 && Branch (95:15): [True: 11.0M, False: 24.8M]
Branch (95:40): [True: 1.29M, False: 9.73M]
|
96 | 14.6M | heightSkipPrev >= height)))) { Branch (96:40): [True: 91.7k, False: 1.20M]
|
97 | | // Only follow pskip if pprev->pskip isn't better than pskip->pprev. |
98 | 14.6M | pindexWalk = pindexWalk->pskip; |
99 | 14.6M | heightWalk = heightSkip; |
100 | 24.9M | } else { |
101 | 24.9M | assert(pindexWalk->pprev); Branch (101:13): [True: 24.9M, False: 0]
|
102 | 24.9M | pindexWalk = pindexWalk->pprev; |
103 | 24.9M | heightWalk--; |
104 | 24.9M | } |
105 | 39.5M | } |
106 | 115M | return pindexWalk; |
107 | 115M | } |
108 | | |
109 | | CBlockIndex* CBlockIndex::GetAncestor(int height) |
110 | 6.31M | { |
111 | 6.31M | return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height)); |
112 | 6.31M | } |
113 | | |
114 | | void CBlockIndex::BuildSkip() |
115 | 107k | { |
116 | 107k | if (pprev) Branch (116:9): [True: 107k, False: 0]
|
117 | 107k | pskip = pprev->GetAncestor(GetSkipHeight(nHeight)); |
118 | 107k | } |
119 | | |
120 | | arith_uint256 GetBitsProof(uint32_t bits) |
121 | 816k | { |
122 | 816k | arith_uint256 bnTarget; |
123 | 816k | bool fNegative; |
124 | 816k | bool fOverflow; |
125 | 816k | bnTarget.SetCompact(bits, &fNegative, &fOverflow); |
126 | 816k | if (fNegative || fOverflow || bnTarget == 0) Branch (126:9): [True: 0, False: 816k]
Branch (126:22): [True: 0, False: 816k]
Branch (126:35): [True: 0, False: 816k]
|
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 | 816k | return (~bnTarget / (bnTarget + 1)) + 1; |
133 | 816k | } |
134 | | |
135 | | int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params) |
136 | 1.13k | { |
137 | 1.13k | arith_uint256 r; |
138 | 1.13k | int sign = 1; |
139 | 1.13k | if (to.nChainWork > from.nChainWork) { Branch (139:9): [True: 629, False: 508]
|
140 | 629 | r = to.nChainWork - from.nChainWork; |
141 | 629 | } else { |
142 | 508 | r = from.nChainWork - to.nChainWork; |
143 | 508 | sign = -1; |
144 | 508 | } |
145 | 1.13k | r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip); |
146 | 1.13k | if (r.bits() > 63) { Branch (146:9): [True: 0, False: 1.13k]
|
147 | 0 | return sign * std::numeric_limits<int64_t>::max(); |
148 | 0 | } |
149 | 1.13k | return sign * int64_t(r.GetLow64()); |
150 | 1.13k | } |
151 | | |
152 | | /** Find the last common ancestor two blocks have. |
153 | | * Both pa and pb must be non-nullptr. */ |
154 | 94.4M | 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 | 94.4M | if (pa->nHeight > pb->nHeight) { Branch (156:9): [True: 2.81M, False: 91.6M]
|
157 | 2.81M | pa = pa->GetAncestor(pb->nHeight); |
158 | 91.6M | } else if (pb->nHeight > pa->nHeight) { Branch (158:16): [True: 0, False: 91.6M]
|
159 | 0 | pb = pb->GetAncestor(pa->nHeight); |
160 | 0 | } |
161 | 99.7M | while (pa != pb) { Branch (161:12): [True: 5.26M, False: 94.4M]
|
162 | | // Jump back until pa and pb have a common "skip" ancestor. |
163 | 5.47M | while (pa->pskip != pb->pskip) { Branch (163:16): [True: 210k, False: 5.26M]
|
164 | | // This logic relies on the property that equal-height blocks have equal-height skip |
165 | | // pointers. |
166 | 210k | Assume(pa->nHeight == pb->nHeight); |
167 | 210k | Assume(pa->pskip->nHeight == pb->pskip->nHeight); |
168 | 210k | pa = pa->pskip; |
169 | 210k | pb = pb->pskip; |
170 | 210k | } |
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 | 5.26M | pa = pa->pprev; |
174 | 5.26M | pb = pb->pprev; |
175 | 5.26M | } |
176 | 94.4M | return pa; |
177 | 94.4M | } |