Bitcoin ABC  0.26.3
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 
8 void CChain::SetTip(CBlockIndex *pindex) {
9  if (pindex == nullptr) {
10  vChain.clear();
11  return;
12  }
13 
14  vChain.resize(pindex->nHeight + 1);
15  while (pindex && vChain[pindex->nHeight] != pindex) {
16  vChain[pindex->nHeight] = pindex;
17  pindex = pindex->pprev;
18  }
19 }
20 
22  int nStep = 1;
23  std::vector<BlockHash> vHave;
24  vHave.reserve(32);
25 
26  if (!pindex) {
27  pindex = Tip();
28  }
29  while (pindex) {
30  vHave.push_back(pindex->GetBlockHash());
31  // Stop when we have added the genesis block.
32  if (pindex->nHeight == 0) {
33  break;
34  }
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 
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  }
59  while (pindex && !Contains(pindex)) {
60  pindex = pindex->pprev;
61  }
62  return pindex;
63 }
64 
65 CBlockIndex *CChain::FindEarliestAtLeast(int64_t nTime, int height) const {
66  std::pair<int64_t, int> blockparams = std::make_pair(nTime, height);
67  std::vector<CBlockIndex *>::const_iterator lower = std::lower_bound(
68  vChain.begin(), vChain.end(), blockparams,
69  [](CBlockIndex *pBlock,
70  const std::pair<int64_t, int> &_blockparams) -> bool {
71  return pBlock->GetBlockTimeMax() < _blockparams.first ||
72  pBlock->nHeight < _blockparams.second;
73  });
74  return (lower == vChain.end() ? nullptr : *lower);
75 }
76 
78  arith_uint256 bnTarget;
79  bool fNegative;
80  bool fOverflow;
81  bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
82  if (fNegative || fOverflow || bnTarget == 0) {
83  return 0;
84  }
85  // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
86  // as it's too large for an arith_uint256. However, as 2**256 is at least as
87  // large as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) /
88  // (bnTarget+1)) + 1, or ~bnTarget / (bnTarget+1) + 1.
89  return (~bnTarget / (bnTarget + 1)) + 1;
90 }
91 
93  const CBlockIndex &from,
94  const CBlockIndex &tip,
95  const Consensus::Params &params) {
96  arith_uint256 r;
97  int sign = 1;
98  if (to.nChainWork > from.nChainWork) {
99  r = to.nChainWork - from.nChainWork;
100  } else {
101  r = from.nChainWork - to.nChainWork;
102  sign = -1;
103  }
104  r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
105  if (r.bits() > 63) {
106  return sign * std::numeric_limits<int64_t>::max();
107  }
108  return sign * int64_t(r.GetLow64());
109 }
110 
116  const CBlockIndex *pb) {
117  if (pa->nHeight > pb->nHeight) {
118  pa = pa->GetAncestor(pb->nHeight);
119  } else if (pb->nHeight > pa->nHeight) {
120  pb = pb->GetAncestor(pa->nHeight);
121  }
122 
123  while (pa != pb && pa && pb) {
124  if (pa->pskip && pb->pskip && pa->pskip != pb->pskip) {
125  pa = pa->pskip;
126  pb = pb->pskip;
127  assert(pa->nHeight == pb->nHeight);
128  } else {
129  pa = pa->pprev;
130  pb = pb->pprev;
131  }
132  }
133 
134  // Eventually all chain branches meet at the genesis block.
135  assert(pa == pb);
136  return pa;
137 }
138 
139 bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb) {
140  if (pa->nHeight > pb->nHeight) {
141  pa = pa->GetAncestor(pb->nHeight);
142  } else if (pb->nHeight > pa->nHeight) {
143  pb = pb->GetAncestor(pa->nHeight);
144  }
145  return pa == pb;
146 }
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:77
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
Definition: chain.cpp:115
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:92
bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb)
Check if two block index are on the same fork.
Definition: chain.cpp:139
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:26
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:33
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: blockindex.h:52
uint32_t nBits
Definition: blockindex.h:94
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
Definition: blockindex.h:36
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: blockindex.cpp:71
BlockHash GetBlockHash() const
Definition: blockindex.h:147
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:39
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:157
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:21
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:65
int Height() const
Return the maximal height in the chain.
Definition: chain.h:193
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:52
void SetTip(CBlockIndex *pindex)
Set/initialize a chain with a given tip.
Definition: chain.cpp:8
std::vector< CBlockIndex * > vChain
Definition: chain.h:143
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:173
256-bit unsigned big integer.
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...
uint64_t GetLow64() const
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.
unsigned int nHeight
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:100
Parameters that influence chain consensus.
Definition: params.h:33
int64_t nPowTargetSpacing
Definition: params.h:76
assert(!tx.IsCoinBase())