Dogecoin Core  1.14.2
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 #include "validation.h"
8 
9 using namespace std;
10 
11 /* Moved here from the header, because we need auxpow and the logic
12  becomes more involved. */
13 CBlockHeader CBlockIndex::GetBlockHeader(const Consensus::Params& consensusParams, bool fCheckPOW) const
14 {
15  CBlockHeader block;
16 
17  block.nVersion = nVersion;
18 
19  /* The CBlockIndex object's block header is missing the auxpow.
20  So if this is an auxpow block, read it from disk instead. We only
21  have to read the actual *header*, not the full block. */
22  if (block.IsAuxpow())
23  {
24  ReadBlockHeaderFromDisk(block, this, consensusParams, fCheckPOW);
25  return block;
26  }
27 
28  if (pprev)
29  block.hashPrevBlock = pprev->GetBlockHash();
30  block.hashMerkleRoot = hashMerkleRoot;
31  block.nTime = nTime;
32  block.nBits = nBits;
33  block.nNonce = nNonce;
34  return block;
35 }
36 
40 void CChain::SetTip(CBlockIndex *pindex) {
41  if (pindex == NULL) {
42  vChain.clear();
43  return;
44  }
45  vChain.resize(pindex->nHeight + 1);
46  while (pindex && vChain[pindex->nHeight] != pindex) {
47  vChain[pindex->nHeight] = pindex;
48  pindex = pindex->pprev;
49  }
50 }
51 
53  int nStep = 1;
54  std::vector<uint256> vHave;
55  vHave.reserve(32);
56 
57  if (!pindex)
58  pindex = Tip();
59  while (pindex) {
60  vHave.push_back(pindex->GetBlockHash());
61  // Stop when we have added the genesis block.
62  if (pindex->nHeight == 0)
63  break;
64  // Exponentially larger steps back, plus the genesis block.
65  int nHeight = std::max(pindex->nHeight - nStep, 0);
66  if (Contains(pindex)) {
67  // Use O(1) CChain index if possible.
68  pindex = (*this)[nHeight];
69  } else {
70  // Otherwise, use O(log n) skiplist.
71  pindex = pindex->GetAncestor(nHeight);
72  }
73  if (vHave.size() > 10)
74  nStep *= 2;
75  }
76 
77  return CBlockLocator(vHave);
78 }
79 
80 const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
81  if (pindex == NULL) {
82  return NULL;
83  }
84  if (pindex->nHeight > Height())
85  pindex = pindex->GetAncestor(Height());
86  while (pindex && !Contains(pindex))
87  pindex = pindex->pprev;
88  return pindex;
89 }
90 
92 {
93  std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), nTime,
94  [](CBlockIndex* pBlock, const int64_t& time) -> bool { return pBlock->GetBlockTimeMax() < time; });
95  return (lower == vChain.end() ? NULL : *lower);
96 }
97 
99 int static inline InvertLowestOne(int n) { return n & (n - 1); }
100 
102 int static inline GetSkipHeight(int height) {
103  if (height < 2)
104  return 0;
105 
106  // Determine which height to jump back to. Any number strictly lower than height is acceptable,
107  // but the following expression seems to perform well in simulations (max 110 steps to go back
108  // up to 2**18 blocks).
109  return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
110 }
111 
113 {
114  if (height > nHeight || height < 0)
115  return NULL;
116 
117  CBlockIndex* pindexWalk = this;
118  int heightWalk = nHeight;
119  while (heightWalk > height) {
120  int heightSkip = GetSkipHeight(heightWalk);
121  int heightSkipPrev = GetSkipHeight(heightWalk - 1);
122  if (pindexWalk->pskip != NULL &&
123  (heightSkip == height ||
124  (heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
125  heightSkipPrev >= height)))) {
126  // Only follow pskip if pprev->pskip isn't better than pskip->pprev.
127  pindexWalk = pindexWalk->pskip;
128  heightWalk = heightSkip;
129  } else {
130  assert(pindexWalk->pprev);
131  pindexWalk = pindexWalk->pprev;
132  heightWalk--;
133  }
134  }
135  return pindexWalk;
136 }
137 
138 const CBlockIndex* CBlockIndex::GetAncestor(int height) const
139 {
140  return const_cast<CBlockIndex*>(this)->GetAncestor(height);
141 }
142 
144 {
145  if (pprev)
146  pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
147 }
148 
150 {
151  arith_uint256 bnTarget;
152  bool fNegative;
153  bool fOverflow;
154  bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
155  if (fNegative || fOverflow || bnTarget == 0)
156  return 0;
157  // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
158  // as it's too large for a arith_uint256. However, as 2**256 is at least as large
159  // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
160  // or ~bnTarget / (nTarget+1) + 1.
161  return (~bnTarget / (bnTarget + 1)) + 1;
162 }
163 
164 int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
165 {
166  arith_uint256 r;
167  int sign = 1;
168  if (to.nChainWork > from.nChainWork) {
169  r = to.nChainWork - from.nChainWork;
170  } else {
171  r = from.nChainWork - to.nChainWork;
172  sign = -1;
173  }
174  r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
175  if (r.bits() > 63) {
176  return sign * std::numeric_limits<int64_t>::max();
177  }
178  return sign * r.GetLow64();
179 }
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:149
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:164
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:25
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:158
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:164
void BuildSkip()
Build the skiplist pointer for this entry.
Definition: chain.cpp:143
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: chain.h:182
unsigned int nBits
Definition: chain.h:200
CBlockHeader GetBlockHeader(const Consensus::Params &consensusParams, bool fCheckPOW=true) const
Definition: chain.cpp:13
uint256 GetBlockHash() const
Definition: chain.h:268
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
Definition: chain.h:167
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:112
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:170
CBlockLocator GetLocator(const CBlockIndex *pindex=NULL) const
Return a CBlockLocator that refers to a block in this chain (by default the tip).
Definition: chain.cpp:52
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:80
void SetTip(CBlockIndex *pindex)
Set/initialize a chain with a given tip.
Definition: chain.cpp:40
CBlockIndex * FindEarliestAtLeast(int64_t nTime) const
Find the earliest block with timestamp equal or greater than the given.
Definition: chain.cpp:91
int32_t nVersion
Definition: pureheader.h:29
bool IsAuxpow() const
Check if the auxpow flag is set in the version.
Definition: pureheader.h:129
uint32_t nNonce
Definition: pureheader.h:34
uint256 hashPrevBlock
Definition: pureheader.h:30
uint32_t nTime
Definition: pureheader.h:32
uint256 hashMerkleRoot
Definition: pureheader.h:31
uint32_t nBits
Definition: pureheader.h:33
256-bit unsigned big integer.
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=NULL, bool *pfOverflow=NULL)
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.
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:130
Parameters that influence chain consensus.
Definition: params.h:39
int64_t nPowTargetSpacing
Definition: params.h:66
bool ReadBlockHeaderFromDisk(CBlockHeader &block, const CBlockIndex *pindex, const Consensus::Params &consensusParams, bool fCheckPOW)