Bitcoin ABC  0.22.13
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 // 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 
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<BlockHash> vHave;
27  vHave.reserve(32);
28 
29  if (!pindex) {
30  pindex = Tip();
31  }
32  while (pindex) {
33  vHave.push_back(pindex->GetBlockHash());
34  // Stop when we have added the genesis block.
35  if (pindex->nHeight == 0) {
36  break;
37  }
38  // Exponentially larger steps back, plus the genesis block.
39  int nHeight = std::max(pindex->nHeight - nStep, 0);
40  if (Contains(pindex)) {
41  // Use O(1) CChain index if possible.
42  pindex = (*this)[nHeight];
43  } else {
44  // Otherwise, use O(log n) skiplist.
45  pindex = pindex->GetAncestor(nHeight);
46  }
47  if (vHave.size() > 10) {
48  nStep *= 2;
49  }
50  }
51 
52  return CBlockLocator(vHave);
53 }
54 
55 const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
56  if (pindex == nullptr) {
57  return nullptr;
58  }
59  if (pindex->nHeight > Height()) {
60  pindex = pindex->GetAncestor(Height());
61  }
62  while (pindex && !Contains(pindex)) {
63  pindex = pindex->pprev;
64  }
65  return pindex;
66 }
67 
68 CBlockIndex *CChain::FindEarliestAtLeast(int64_t nTime, int height) const {
69  std::pair<int64_t, int> blockparams = std::make_pair(nTime, height);
70  std::vector<CBlockIndex *>::const_iterator lower = std::lower_bound(
71  vChain.begin(), vChain.end(), blockparams,
72  [](CBlockIndex *pBlock,
73  const std::pair<int64_t, int> &_blockparams) -> bool {
74  return pBlock->GetBlockTimeMax() < _blockparams.first ||
75  pBlock->nHeight < _blockparams.second;
76  });
77  return (lower == vChain.end() ? nullptr : *lower);
78 }
79 
81  arith_uint256 bnTarget;
82  bool fNegative;
83  bool fOverflow;
84  bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
85  if (fNegative || fOverflow || bnTarget == 0) {
86  return 0;
87  }
88  // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
89  // as it's too large for an arith_uint256. However, as 2**256 is at least as
90  // large as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) /
91  // (bnTarget+1)) + 1, or ~bnTarget / (bnTarget+1) + 1.
92  return (~bnTarget / (bnTarget + 1)) + 1;
93 }
94 
96  const CBlockIndex &from,
97  const CBlockIndex &tip,
98  const Consensus::Params &params) {
99  arith_uint256 r;
100  int sign = 1;
101  if (to.nChainWork > from.nChainWork) {
102  r = to.nChainWork - from.nChainWork;
103  } else {
104  r = from.nChainWork - to.nChainWork;
105  sign = -1;
106  }
107  r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
108  if (r.bits() > 63) {
109  return sign * std::numeric_limits<int64_t>::max();
110  }
111  return sign * r.GetLow64();
112 }
113 
119  const CBlockIndex *pb) {
120  if (pa->nHeight > pb->nHeight) {
121  pa = pa->GetAncestor(pb->nHeight);
122  } else if (pb->nHeight > pa->nHeight) {
123  pb = pb->GetAncestor(pa->nHeight);
124  }
125 
126  while (pa != pb && pa && pb) {
127  if (pa->pskip && pb->pskip && pa->pskip != pb->pskip) {
128  pa = pa->pskip;
129  pb = pb->pskip;
130  assert(pa->nHeight == pb->nHeight);
131  } else {
132  pa = pa->pprev;
133  pb = pb->pprev;
134  }
135  }
136 
137  // Eventually all chain branches meet at the genesis block.
138  assert(pa == pb);
139  return pa;
140 }
141 
142 bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb) {
143  if (pa->nHeight > pb->nHeight) {
144  pa = pa->GetAncestor(pb->nHeight);
145  } else if (pb->nHeight > pa->nHeight) {
146  pb = pb->GetAncestor(pa->nHeight);
147  }
148  return pa == pb;
149 }
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: blockindex.h:49
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
Definition: blockindex.h:33
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:110
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:30
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
Definition: chain.cpp:118
unsigned int nHeight
int Height() const
Return the maximal height in the chain.
Definition: chain.h:210
std::vector< CBlockIndex * > vChain
Definition: chain.h:154
BlockHash GetBlockHash() const
Definition: blockindex.h:133
CBlockIndex * FindEarliestAtLeast(int64_t nTime, int height) const
Find the earliest block with timestamp equal or greater than the given time and height equal or great...
Definition: chain.cpp:68
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:190
int64_t nPowTargetSpacing
Definition: params.h:104
Parameters that influence chain consensus.
Definition: params.h:59
256-bit unsigned big integer.
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: blockindex.h:23
uint64_t GetLow64() const
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:168
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...
bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb)
Check if two block index are on the same fork.
Definition: chain.cpp:142
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:80
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:36
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: blockindex.cpp:71
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:55
uint32_t nBits
Definition: blockindex.h:82
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:95
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.