Raven Core  3.0.0
P2P Digital Currency
chain.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "chain.h"
8 
12 void CChain::SetTip(CBlockIndex *pindex) {
13  if (pindex == nullptr) {
14  vChain.clear();
15  return;
16  }
17  vChain.resize(pindex->nHeight + 1);
18  while (pindex && vChain[pindex->nHeight] != pindex) {
19  vChain[pindex->nHeight] = pindex;
20  pindex = pindex->pprev;
21  }
22 }
23 
25  int nStep = 1;
26  std::vector<uint256> vHave;
27  vHave.reserve(32);
28 
29  if (!pindex)
30  pindex = Tip();
31  while (pindex) {
32  vHave.push_back(pindex->GetBlockHash());
33  // Stop when we have added the genesis block.
34  if (pindex->nHeight == 0)
35  break;
36  // Exponentially larger steps back, plus the genesis block.
37  int nHeight = std::max(pindex->nHeight - nStep, 0);
38  if (Contains(pindex)) {
39  // Use O(1) CChain index if possible.
40  pindex = (*this)[nHeight];
41  } else {
42  // Otherwise, use O(log n) skiplist.
43  pindex = pindex->GetAncestor(nHeight);
44  }
45  if (vHave.size() > 10)
46  nStep *= 2;
47  }
48 
49  return CBlockLocator(vHave);
50 }
51 
52 const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
53  if (pindex == nullptr) {
54  return nullptr;
55  }
56  if (pindex->nHeight > Height())
57  pindex = pindex->GetAncestor(Height());
58  while (pindex && !Contains(pindex))
59  pindex = pindex->pprev;
60  return pindex;
61 }
62 
64 {
65  std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), nTime,
66  [](CBlockIndex* pBlock, const int64_t& time) -> bool { return pBlock->GetBlockTimeMax() < time; });
67  return (lower == vChain.end() ? nullptr : *lower);
68 }
69 
71 int static inline InvertLowestOne(int n) { return n & (n - 1); }
72 
74 int static inline GetSkipHeight(int height) {
75  if (height < 2)
76  return 0;
77 
78  // Determine which height to jump back to. Any number strictly lower than height is acceptable,
79  // but the following expression seems to perform well in simulations (max 110 steps to go back
80  // up to 2**18 blocks).
81  return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
82 }
83 
85 {
86  if (height > nHeight || height < 0)
87  return nullptr;
88 
89  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 
110 const CBlockIndex* CBlockIndex::GetAncestor(int height) const
111 {
112  return const_cast<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:197
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
Definition: chain.h:182
std::vector< CBlockIndex * > vChain
Definition: chain.h:439
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:132
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:179
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
Definition: chain.cpp:155
int Height() const
Return the maximal height in the chain.
Definition: chain.h:479
uint256 GetBlockHash() const
Definition: chain.h:294
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:466
CBlockIndex * FindEarliestAtLeast(int64_t nTime) const
Find the earliest block with timestamp equal or greater than the given.
Definition: chain.cpp:63
int64_t nPowTargetSpacing
Definition: params.h:71
void BuildSkip()
Build the skiplist pointer for this entry.
Definition: chain.cpp:115
Parameters that influence chain consensus.
Definition: params.h:47
256-bit unsigned big integer.
void SetTip(CBlockIndex *pindex)
Set/initialize a chain with a given tip.
Definition: chain.cpp:12
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:172
uint64_t GetLow64() const
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:448
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:24
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
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:185
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:84
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:52
uint32_t nBits
Definition: chain.h:215
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
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.