Bitcoin Core  0.15.99
P2P Digital Currency
chain.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 
11 void CChain::SetTip(CBlockIndex *pindex) {
12  if (pindex == nullptr) {
13  vChain.clear();
14  return;
15  }
16  vChain.resize(pindex->nHeight + 1);
17  while (pindex && vChain[pindex->nHeight] != pindex) {
18  vChain[pindex->nHeight] = pindex;
19  pindex = pindex->pprev;
20  }
21 }
22 
24  int nStep = 1;
25  std::vector<uint256> vHave;
26  vHave.reserve(32);
27 
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);
43  }
44  if (vHave.size() > 10)
45  nStep *= 2;
46  }
47 
48  return CBlockLocator(vHave);
49 }
50 
51 const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
52  if (pindex == nullptr) {
53  return nullptr;
54  }
55  if (pindex->nHeight > Height())
56  pindex = pindex->GetAncestor(Height());
57  while (pindex && !Contains(pindex))
58  pindex = pindex->pprev;
59  return pindex;
60 }
61 
63 {
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);
67 }
68 
70 int static inline InvertLowestOne(int n) { return n & (n - 1); }
71 
73 int static inline GetSkipHeight(int height) {
74  if (height < 2)
75  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  return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
81 }
82 
83 const CBlockIndex* CBlockIndex::GetAncestor(int height) const
84 {
85  if (height > nHeight || height < 0) {
86  return nullptr;
87  }
88 
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--;
105  }
106  }
107  return pindexWalk;
108 }
109 
111 {
112  return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height));
113 }
114 
116 {
117  if (pprev)
118  pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
119 }
120 
122 {
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;
134 }
135 
136 int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
137 {
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;
145  }
146  r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
147  if (r.bits() > 63) {
148  return sign * std::numeric_limits<int64_t>::max();
149  }
150  return sign * r.GetLow64();
151 }
152 
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);
160  }
161 
162  while (pa != pb && pa && pb) {
163  pa = pa->pprev;
164  pb = pb->pprev;
165  }
166 
167  // Eventually all chain branches meet at the genesis block.
168  assert(pa == pb);
169  return pa;
170 }
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: chain.h:195
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
Definition: chain.h:180
std::vector< CBlockIndex * > vChain
Definition: chain.h:437
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:126
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:177
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
Definition: chain.cpp:155
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:446
int Height() const
Return the maximal height in the chain.
Definition: chain.h:477
int64_t nPowTargetSpacing
Definition: params.h:73
void BuildSkip()
Build the skiplist pointer for this entry.
Definition: chain.cpp:115
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:51
Parameters that influence chain consensus.
Definition: params.h:49
256-bit unsigned big integer.
CBlockIndex * FindEarliestAtLeast(int64_t nTime) const
Find the earliest block with timestamp equal or greater than the given.
Definition: chain.cpp:62
void SetTip(CBlockIndex *pindex)
Set/initialize a chain with a given tip.
Definition: chain.cpp:11
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:464
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:121
CBlockLocator GetLocator(const CBlockIndex *pindex=nullptr) const
Return a CBlockLocator that refers to a block in this chain (by default the tip). ...
Definition: chain.cpp:23
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:183
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:110
uint64_t GetLow64() const
uint32_t nBits
Definition: chain.h:213
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params &params)
Return the time it would take to redo the work difference between from and to, assuming the current h...
Definition: chain.cpp:136
uint256 GetBlockHash() const
Definition: chain.h:292