Fabcoin Core  0.16.2
P2P Digital Currency
chain.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 <util.h>
8 #include <chainparams.h>
9 
13 void CChain::SetTip(CBlockIndex *pindex) {
14  if (pindex == nullptr) {
15  vChain.clear();
16  return;
17  }
18  vChain.resize(pindex->nHeight + 1);
19  while (pindex && vChain[pindex->nHeight] != pindex) {
20  vChain[pindex->nHeight] = pindex;
21  pindex = pindex->pprev;
22  }
23 }
24 
26  int nStep = 1;
27  std::vector<uint256> vHave;
28  vHave.reserve(32);
29 
30  if (!pindex)
31  pindex = Tip();
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  // Exponentially larger steps back, plus the genesis block.
38  int nHeight = std::max(pindex->nHeight - nStep, 0);
39  if (Contains(pindex)) {
40  // Use O(1) CChain index if possible.
41  pindex = (*this)[nHeight];
42  } else {
43  // Otherwise, use O(log n) skiplist.
44  pindex = pindex->GetAncestor(nHeight);
45  }
46  if (vHave.size() > 10)
47  nStep *= 2;
48  }
49 
50  return CBlockLocator(vHave);
51 }
52 
53 const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
54  if (pindex == nullptr) {
55  return nullptr;
56  }
57  if (pindex->nHeight > Height())
58  pindex = pindex->GetAncestor(Height());
59  while (pindex && !Contains(pindex))
60  pindex = pindex->pprev;
61  return pindex;
62 }
63 
65 {
66  std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), nTime,
67  [](CBlockIndex* pBlock, const int64_t& time) -> bool { return pBlock->GetBlockTimeMax() < time; });
68  return (lower == vChain.end() ? nullptr : *lower);
69 }
70 
72 int static inline InvertLowestOne(int n) { return n & (n - 1); }
73 
75 int static inline GetSkipHeight(int height) {
76  if (height < 2)
77  return 0;
78 
79  // Determine which height to jump back to. Any number strictly lower than height is acceptable,
80  // but the following expression seems to perform well in simulations (max 110 steps to go back
81  // up to 2**18 blocks).
82  return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
83 }
84 
86 {
87  if (height > nHeight || height < 0)
88  return nullptr;
89 
90  CBlockIndex* pindexWalk = this;
91  int heightWalk = nHeight;
92  while (heightWalk > height) {
93  int heightSkip = GetSkipHeight(heightWalk);
94  int heightSkipPrev = GetSkipHeight(heightWalk - 1);
95  if (pindexWalk->pskip != nullptr &&
96  (heightSkip == height ||
97  (heightSkip > height && !(heightSkipPrev < heightSkip - 2 &&
98  heightSkipPrev >= height)))) {
99  // Only follow pskip if pprev->pskip isn't better than pskip->pprev.
100  pindexWalk = pindexWalk->pskip;
101  heightWalk = heightSkip;
102  } else {
103  assert(pindexWalk->pprev);
104  pindexWalk = pindexWalk->pprev;
105  heightWalk--;
106  }
107  }
108  return pindexWalk;
109 }
110 
111 const CBlockIndex* CBlockIndex::GetAncestor(int height) const
112 {
113  return const_cast<CBlockIndex*>(this)->GetAncestor(height);
114 }
115 
117 {
118  if (pprev)
119  pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
120 }
121 
122 
123 /*
124  regtest support Contract.
125  mainnet and test : only when nHeight larger than ContractHeight, support Contract
126 */
128 {
129  //genesis block
130  if ( nHeight == 0 ) {
131  if ( nVersion == 5 )
132  return true;
133  else
134  return false;
135  }
136 
138  if ( fRegTest )
139  return true;
140 
141  // when nHeight larger than consensus.ContractHeight , support Contract
142  return ((uint32_t) nHeight >= (uint32_t)Params().GetConsensus().ContractHeight );
143 }
144 
146 {
147  if ( nHeight == 0 )
148  return false;
149 
150  return (Params().NetworkIDString() == CBaseChainParams::REGTEST ) || (Params().NetworkIDString() == CBaseChainParams::UNITTEST );
151 }
152 
154 {
155  arith_uint256 bnTarget;
156  bool fNegative;
157  bool fOverflow;
158  bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
159  if (fNegative || fOverflow || bnTarget == 0)
160  return 0;
161  // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
162  // as it's too large for an arith_uint256. However, as 2**256 is at least as large
163  // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
164  // or ~bnTarget / (nTarget+1) + 1.
165  return (~bnTarget / (bnTarget + 1)) + 1;
166 }
167 
168 int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
169 {
170  arith_uint256 r;
171  int sign = 1;
172  if (to.nChainWork > from.nChainWork) {
173  r = to.nChainWork - from.nChainWork;
174  } else {
175  r = from.nChainWork - to.nChainWork;
176  sign = -1;
177  }
178  //r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
179  r = r * arith_uint256(Params().GetnPowTargetSpacing( tip.nHeight)) / GetBlockProof(tip);
180  if (r.bits() > 63) {
181  return sign * std::numeric_limits<int64_t>::max();
182  }
183  return sign * r.GetLow64();
184 }
185 
189  if (pa->nHeight > pb->nHeight) {
190  pa = pa->GetAncestor(pb->nHeight);
191  } else if (pb->nHeight > pa->nHeight) {
192  pb = pb->GetAncestor(pa->nHeight);
193  }
194 
195  while (pa != pb && pa && pb) {
196  pa = pa->pprev;
197  pb = pb->pprev;
198  }
199 
200  // Eventually all chain branches meet at the genesis block.
201  assert(pa == pb);
202  return pa;
203 }
204 
205 
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: chain.h:205
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:190
std::vector< CBlockIndex * > vChain
Definition: chain.h:503
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:251
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:184
static const std::string REGTEST
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
Definition: chain.cpp:188
static const std::string UNITTEST
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:60
assert(len-trim+(2 *lenIndices)<=WIDTH)
bool IsSupportContract() const
Definition: chain.cpp:127
std::string NetworkIDString() const
Return the BIP70 network string (main, test or regtest)
Definition: chainparams.h:78
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:512
int Height() const
Return the maximal height in the chain.
Definition: chain.h:543
ExecStats::duration max
Definition: ExecStats.cpp:36
uint32_t ContractHeight
Block height at which Fabcoin Smart Contract hard fork becomes active.
Definition: params.h:56
unsigned int nBits
Definition: chain.h:224
void BuildSkip()
Build the skiplist pointer for this entry.
Definition: chain.cpp:116
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:53
Parameters that influence chain consensus.
Definition: params.h:39
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:64
Signature sign(Secret const &_k, h256 const &_hash)
Returns siganture of message hash.
Definition: Common.cpp:233
bool IsLegacyFormat() const
Definition: chain.cpp:145
void SetTip(CBlockIndex *pindex)
Set/initialize a chain with a given tip.
Definition: chain.cpp:13
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:177
const CChainParams & Params()
Return the currently selected parameters.
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:530
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:153
dev::WithExisting max(dev::WithExisting _a, dev::WithExisting _b)
Definition: Common.h:326
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:25
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:193
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:85
uint64_t GetLow64() const
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:168
uint256 GetBlockHash() const
Definition: chain.h:324