/root/bitcoin/src/chain.cpp
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)",Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
13 | 0 | pprev, nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString()); |
14 | 0 | } |
15 | | |
16 | | void CChain::SetTip(CBlockIndex& block) |
17 | 132M | { |
18 | 132M | CBlockIndex* pindex = █ |
19 | 132M | vChain.resize(pindex->nHeight + 1); |
20 | 11.8G | while (pindex && vChain[pindex->nHeight] != pindex11.7G ) { |
21 | 11.7G | vChain[pindex->nHeight] = pindex; |
22 | 11.7G | pindex = pindex->pprev; |
23 | 11.7G | } |
24 | 132M | } |
25 | | |
26 | | std::vector<uint256> LocatorEntries(const CBlockIndex* index) |
27 | 452k | { |
28 | 452k | int step = 1; |
29 | 452k | std::vector<uint256> have; |
30 | 452k | if (index == nullptr) return have0 ; |
31 | | |
32 | 452k | have.reserve(32); |
33 | 8.60M | while (index) { |
34 | 8.60M | have.emplace_back(index->GetBlockHash()); |
35 | 8.60M | if (index->nHeight == 0) break452k ; |
36 | | // Exponentially larger steps back, plus the genesis block. |
37 | 8.14M | int height = std::max(index->nHeight - step, 0); |
38 | | // Use skiplist. |
39 | 8.14M | index = index->GetAncestor(height); |
40 | 8.14M | if (have.size() > 10) step *= 23.62M ; |
41 | 8.14M | } |
42 | 452k | return have; |
43 | 452k | } |
44 | | |
45 | | CBlockLocator GetLocator(const CBlockIndex* index) |
46 | 452k | { |
47 | 452k | return CBlockLocator{LocatorEntries(index)}; |
48 | 452k | } |
49 | | |
50 | 60.2M | const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const { |
51 | 60.2M | if (pindex == nullptr) { |
52 | 147k | return nullptr; |
53 | 147k | } |
54 | 60.0M | if (pindex->nHeight > Height()) |
55 | 30.1M | pindex = pindex->GetAncestor(Height()); |
56 | 60.0M | while (pindex && !Contains(pindex)59.9M ) |
57 | 34.1k | pindex = pindex->pprev; |
58 | 60.0M | return pindex; |
59 | 60.2M | } |
60 | | |
61 | | CBlockIndex* CChain::FindEarliestAtLeast(int64_t nTime, int height) const |
62 | 0 | { |
63 | 0 | std::pair<int64_t, int> blockparams = std::make_pair(nTime, height); |
64 | 0 | std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), blockparams, |
65 | 0 | [](CBlockIndex* pBlock, const std::pair<int64_t, int>& blockparams) -> bool { return pBlock->GetBlockTimeMax() < blockparams.first || pBlock->nHeight < blockparams.second; }); |
66 | 0 | return (lower == vChain.end() ? nullptr : *lower); |
67 | 0 | } |
68 | | |
69 | | /** Turn the lowest '1' bit in the binary representation of a number into a '0'. */ |
70 | 6.29G | int static inline InvertLowestOne(int n) { return n & (n - 1); } |
71 | | |
72 | | /** Compute what height to jump back to with the CBlockIndex::pskip pointer. */ |
73 | 4.20G | int static inline GetSkipHeight(int height) { |
74 | 4.20G | if (height < 2) |
75 | 12.6M | return 0; |
76 | | |
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 | 4.19G | return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 12.09G : InvertLowestOne(height)2.09G ; |
81 | 4.20G | } |
82 | | |
83 | | const CBlockIndex* CBlockIndex::GetAncestor(int height) const |
84 | 1.11G | { |
85 | 1.11G | if (height > nHeight || height < 01.10G ) { |
86 | 647M | return nullptr; |
87 | 647M | } |
88 | | |
89 | 464M | const CBlockIndex* pindexWalk = this; |
90 | 464M | int heightWalk = nHeight; |
91 | 2.55G | while (heightWalk > height) { |
92 | 2.08G | int heightSkip = GetSkipHeight(heightWalk); |
93 | 2.08G | int heightSkipPrev = GetSkipHeight(heightWalk - 1); |
94 | 2.08G | if (pindexWalk->pskip != nullptr && |
95 | 2.08G | (2.08G heightSkip == height2.08G || |
96 | 2.08G | (2.00G heightSkip > height2.00G && !(972M heightSkipPrev < heightSkip - 2972M && |
97 | 985M | heightSkipPrev >= height171M )))) { |
98 | | // Only follow pskip if pprev->pskip isn't better than pskip->pprev. |
99 | 985M | pindexWalk = pindexWalk->pskip; |
100 | 985M | heightWalk = heightSkip; |
101 | 1.10G | } else { |
102 | 1.10G | assert(pindexWalk->pprev); |
103 | 1.10G | pindexWalk = pindexWalk->pprev; |
104 | 1.10G | heightWalk--; |
105 | 1.10G | } |
106 | 2.08G | } |
107 | 464M | return pindexWalk; |
108 | 464M | } |
109 | | |
110 | | CBlockIndex* CBlockIndex::GetAncestor(int height) |
111 | 136M | { |
112 | 136M | return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height)); |
113 | 136M | } |
114 | | |
115 | | void CBlockIndex::BuildSkip() |
116 | 30.4M | { |
117 | 30.4M | if (pprev) |
118 | 30.4M | pskip = pprev->GetAncestor(GetSkipHeight(nHeight)); |
119 | 30.4M | } |
120 | | |
121 | | arith_uint256 GetBitsProof(uint32_t bits) |
122 | 40.5M | { |
123 | 40.5M | arith_uint256 bnTarget; |
124 | 40.5M | bool fNegative; |
125 | 40.5M | bool fOverflow; |
126 | 40.5M | bnTarget.SetCompact(bits, &fNegative, &fOverflow); |
127 | 40.5M | if (fNegative || fOverflow || bnTarget == 0) |
128 | 0 | 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 | 40.5M | return (~bnTarget / (bnTarget + 1)) + 1; |
134 | 40.5M | } |
135 | | |
136 | | int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params) |
137 | 0 | { |
138 | 0 | arith_uint256 r; |
139 | 0 | int sign = 1; |
140 | 0 | if (to.nChainWork > from.nChainWork) { |
141 | 0 | r = to.nChainWork - from.nChainWork; |
142 | 0 | } else { |
143 | 0 | r = from.nChainWork - to.nChainWork; |
144 | 0 | sign = -1; |
145 | 0 | } |
146 | 0 | r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip); |
147 | 0 | if (r.bits() > 63) { |
148 | 0 | return sign * std::numeric_limits<int64_t>::max(); |
149 | 0 | } |
150 | 0 | return sign * int64_t(r.GetLow64()); |
151 | 0 | } |
152 | | |
153 | | /** Find the last common ancestor two blocks have. |
154 | | * Both pa and pb must be non-nullptr. */ |
155 | 15.0M | const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb) { |
156 | | // First rewind to the last common height (the forking point cannot be past one of the two). |
157 | 15.0M | if (pa->nHeight > pb->nHeight) { |
158 | 6.23M | pa = pa->GetAncestor(pb->nHeight); |
159 | 8.81M | } else if (pb->nHeight > pa->nHeight) { |
160 | 0 | pb = pb->GetAncestor(pa->nHeight); |
161 | 0 | } |
162 | 15.6M | while (pa != pb) { |
163 | | // Jump back until pa and pb have a common "skip" ancestor. |
164 | 622k | while (pa->pskip != pb->pskip) { |
165 | | // This logic relies on the property that equal-height blocks have equal-height skip |
166 | | // pointers. |
167 | 0 | Assume(pa->nHeight == pb->nHeight); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
168 | 0 | Assume(pa->pskip->nHeight == pb->pskip->nHeight); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
169 | 0 | pa = pa->pskip; |
170 | 0 | pb = pb->pskip; |
171 | 0 | } |
172 | | // At this point, pa and pb are different, but have equal pskip. The forking point lies in |
173 | | // between pa/pb on the one end, and pa->pskip/pb->pskip on the other end. |
174 | 622k | pa = pa->pprev; |
175 | 622k | pb = pb->pprev; |
176 | 622k | } |
177 | 15.0M | return pa; |
178 | 15.0M | } |