/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 | 795k | { |
18 | 795k | CBlockIndex* pindex = █ |
19 | 795k | vChain.resize(pindex->nHeight + 1); |
20 | 68.0M | while (pindex && vChain[pindex->nHeight] != pindex67.4M ) { |
21 | 67.2M | vChain[pindex->nHeight] = pindex; |
22 | 67.2M | pindex = pindex->pprev; |
23 | 67.2M | } |
24 | 795k | } |
25 | | |
26 | | std::vector<uint256> LocatorEntries(const CBlockIndex* index) |
27 | 2.08k | { |
28 | 2.08k | int step = 1; |
29 | 2.08k | std::vector<uint256> have; |
30 | 2.08k | if (index == nullptr) return have0 ; |
31 | | |
32 | 2.08k | have.reserve(32); |
33 | 39.6k | while (index) { |
34 | 39.6k | have.emplace_back(index->GetBlockHash()); |
35 | 39.6k | if (index->nHeight == 0) break2.08k ; |
36 | | // Exponentially larger steps back, plus the genesis block. |
37 | 37.5k | int height = std::max(index->nHeight - step, 0); |
38 | | // Use skiplist. |
39 | 37.5k | index = index->GetAncestor(height); |
40 | 37.5k | if (have.size() > 10) step *= 216.7k ; |
41 | 37.5k | } |
42 | 2.08k | return have; |
43 | 2.08k | } |
44 | | |
45 | | CBlockLocator GetLocator(const CBlockIndex* index) |
46 | 2.08k | { |
47 | 2.08k | return CBlockLocator{LocatorEntries(index)}; |
48 | 2.08k | } |
49 | | |
50 | | const CBlockIndex* CChain::FindFork(const CBlockIndex& index) const |
51 | 372k | { |
52 | 372k | const auto* pindex{&index}; |
53 | 372k | if (pindex->nHeight > Height()) |
54 | 186k | pindex = pindex->GetAncestor(Height()); |
55 | 372k | while (pindex && !Contains(*pindex)372k ) |
56 | 0 | pindex = pindex->pprev; |
57 | 372k | return pindex; |
58 | 372k | } |
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; }); |
65 | 0 | return (lower == vChain.end() ? nullptr : *lower); |
66 | 0 | } |
67 | | |
68 | | /** Turn the lowest '1' bit in the binary representation of a number into a '0'. */ |
69 | 37.0M | 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 | 24.7M | int static inline GetSkipHeight(int height) { |
73 | 24.7M | if (height < 2) |
74 | 59.5k | 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 | 24.7M | return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 112.3M : InvertLowestOne(height)12.3M ; |
80 | 24.7M | } |
81 | | |
82 | | const CBlockIndex* CBlockIndex::GetAncestor(int height) const |
83 | 6.68M | { |
84 | 6.68M | if (height > nHeight || height < 06.65M ) { |
85 | 4.02M | return nullptr; |
86 | 4.02M | } |
87 | | |
88 | 2.65M | const CBlockIndex* pindexWalk = this; |
89 | 2.65M | int heightWalk = nHeight; |
90 | 14.9M | while (heightWalk > height) { |
91 | 12.2M | int heightSkip = GetSkipHeight(heightWalk); |
92 | 12.2M | int heightSkipPrev = GetSkipHeight(heightWalk - 1); |
93 | 12.2M | if (pindexWalk->pskip != nullptr && |
94 | 12.2M | (12.2M heightSkip == height12.2M || |
95 | 12.2M | (11.7M heightSkip > height11.7M && !(5.73M heightSkipPrev < heightSkip - 25.73M && |
96 | 5.81M | heightSkipPrev >= height1.04M )))) { |
97 | | // Only follow pskip if pprev->pskip isn't better than pskip->pprev. |
98 | 5.81M | pindexWalk = pindexWalk->pskip; |
99 | 5.81M | heightWalk = heightSkip; |
100 | 6.47M | } else { |
101 | 6.47M | assert(pindexWalk->pprev); |
102 | 6.47M | pindexWalk = pindexWalk->pprev; |
103 | 6.47M | heightWalk--; |
104 | 6.47M | } |
105 | 12.2M | } |
106 | 2.65M | return pindexWalk; |
107 | 2.65M | } |
108 | | |
109 | | CBlockIndex* CBlockIndex::GetAncestor(int height) |
110 | 804k | { |
111 | 804k | return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height)); |
112 | 804k | } |
113 | | |
114 | | void CBlockIndex::BuildSkip() |
115 | 187k | { |
116 | 187k | if (pprev) |
117 | 187k | pskip = pprev->GetAncestor(GetSkipHeight(nHeight)); |
118 | 187k | } |
119 | | |
120 | | arith_uint256 GetBitsProof(uint32_t bits) |
121 | 255k | { |
122 | 255k | arith_uint256 bnTarget; |
123 | 255k | bool fNegative; |
124 | 255k | bool fOverflow; |
125 | 255k | bnTarget.SetCompact(bits, &fNegative, &fOverflow); |
126 | 255k | 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 | 255k | return (~bnTarget / (bnTarget + 1)) + 1; |
133 | 255k | } |
134 | | |
135 | | int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params) |
136 | 0 | { |
137 | 0 | arith_uint256 r; |
138 | 0 | int sign = 1; |
139 | 0 | if (to.nChainWork > from.nChainWork) { |
140 | 0 | r = to.nChainWork - from.nChainWork; |
141 | 0 | } else { |
142 | 0 | r = from.nChainWork - to.nChainWork; |
143 | 0 | sign = -1; |
144 | 0 | } |
145 | 0 | r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip); |
146 | 0 | if (r.bits() > 63) { |
147 | 0 | return sign * std::numeric_limits<int64_t>::max(); |
148 | 0 | } |
149 | 0 | return sign * int64_t(r.GetLow64()); |
150 | 0 | } |
151 | | |
152 | | /** Find the last common ancestor two blocks have. |
153 | | * Both pa and pb must be non-nullptr. */ |
154 | 6.39k | 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 | 6.39k | if (pa->nHeight > pb->nHeight) { |
157 | 5.04k | pa = pa->GetAncestor(pb->nHeight); |
158 | 5.04k | } else if (1.35k pb->nHeight > pa->nHeight1.35k ) { |
159 | 0 | pb = pb->GetAncestor(pa->nHeight); |
160 | 0 | } |
161 | 7.98k | while (pa != pb) { |
162 | | // Jump back until pa and pb have a common "skip" ancestor. |
163 | 1.58k | while (pa->pskip != pb->pskip) { |
164 | | // This logic relies on the property that equal-height blocks have equal-height skip |
165 | | // pointers. |
166 | 0 | Assume(pa->nHeight == pb->nHeight); Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
167 | 0 | Assume(pa->pskip->nHeight == pb->pskip->nHeight); Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
168 | 0 | pa = pa->pskip; |
169 | 0 | pb = pb->pskip; |
170 | 0 | } |
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 | 1.58k | pa = pa->pprev; |
174 | 1.58k | pb = pb->pprev; |
175 | 1.58k | } |
176 | 6.39k | return pa; |
177 | 6.39k | } |