Bitcoin Core  0.18.99
P2P Digital Currency
chain.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 #ifndef BITCOIN_CHAIN_H
7 #define BITCOIN_CHAIN_H
8 
9 #include <arith_uint256.h>
10 #include <consensus/params.h>
11 #include <flatfile.h>
12 #include <primitives/block.h>
13 #include <tinyformat.h>
14 #include <uint256.h>
15 
16 #include <vector>
17 
22 static constexpr int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60;
23 
30 static constexpr int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME;
31 
38 static constexpr int64_t MAX_BLOCK_TIME_GAP = 90 * 60;
39 
41 {
42 public:
43  unsigned int nBlocks;
44  unsigned int nSize;
45  unsigned int nUndoSize;
46  unsigned int nHeightFirst;
47  unsigned int nHeightLast;
48  uint64_t nTimeFirst;
49  uint64_t nTimeLast;
50 
52 
53  template <typename Stream, typename Operation>
54  inline void SerializationOp(Stream& s, Operation ser_action) {
55  READWRITE(VARINT(nBlocks));
56  READWRITE(VARINT(nSize));
57  READWRITE(VARINT(nUndoSize));
58  READWRITE(VARINT(nHeightFirst));
59  READWRITE(VARINT(nHeightLast));
60  READWRITE(VARINT(nTimeFirst));
61  READWRITE(VARINT(nTimeLast));
62  }
63 
64  void SetNull() {
65  nBlocks = 0;
66  nSize = 0;
67  nUndoSize = 0;
68  nHeightFirst = 0;
69  nHeightLast = 0;
70  nTimeFirst = 0;
71  nTimeLast = 0;
72  }
73 
75  SetNull();
76  }
77 
78  std::string ToString() const;
79 
81  void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn) {
82  if (nBlocks==0 || nHeightFirst > nHeightIn)
83  nHeightFirst = nHeightIn;
84  if (nBlocks==0 || nTimeFirst > nTimeIn)
85  nTimeFirst = nTimeIn;
86  nBlocks++;
87  if (nHeightIn > nHeightLast)
88  nHeightLast = nHeightIn;
89  if (nTimeIn > nTimeLast)
90  nTimeLast = nTimeIn;
91  }
92 };
93 
94 enum BlockStatus: uint32_t {
97 
100 
104 
111 
115 
118 
122 
126 
130 
132 };
133 
140 {
141 public:
144 
147 
150 
152  int nHeight;
153 
155  int nFile;
156 
158  unsigned int nDataPos;
159 
161  unsigned int nUndoPos;
162 
165 
168  unsigned int nTx;
169 
173  unsigned int nChainTx;
174 
176  uint32_t nStatus;
177 
179  int32_t nVersion;
181  uint32_t nTime;
182  uint32_t nBits;
183  uint32_t nNonce;
184 
186  int32_t nSequenceId;
187 
189  unsigned int nTimeMax;
190 
191  void SetNull()
192  {
193  phashBlock = nullptr;
194  pprev = nullptr;
195  pskip = nullptr;
196  nHeight = 0;
197  nFile = 0;
198  nDataPos = 0;
199  nUndoPos = 0;
200  nChainWork = arith_uint256();
201  nTx = 0;
202  nChainTx = 0;
203  nStatus = 0;
204  nSequenceId = 0;
205  nTimeMax = 0;
206 
207  nVersion = 0;
208  hashMerkleRoot = uint256();
209  nTime = 0;
210  nBits = 0;
211  nNonce = 0;
212  }
213 
215  {
216  SetNull();
217  }
218 
219  explicit CBlockIndex(const CBlockHeader& block)
220  {
221  SetNull();
222 
223  nVersion = block.nVersion;
224  hashMerkleRoot = block.hashMerkleRoot;
225  nTime = block.nTime;
226  nBits = block.nBits;
227  nNonce = block.nNonce;
228  }
229 
231  FlatFilePos ret;
232  if (nStatus & BLOCK_HAVE_DATA) {
233  ret.nFile = nFile;
234  ret.nPos = nDataPos;
235  }
236  return ret;
237  }
238 
240  FlatFilePos ret;
241  if (nStatus & BLOCK_HAVE_UNDO) {
242  ret.nFile = nFile;
243  ret.nPos = nUndoPos;
244  }
245  return ret;
246  }
247 
249  {
250  CBlockHeader block;
251  block.nVersion = nVersion;
252  if (pprev)
253  block.hashPrevBlock = pprev->GetBlockHash();
254  block.hashMerkleRoot = hashMerkleRoot;
255  block.nTime = nTime;
256  block.nBits = nBits;
257  block.nNonce = nNonce;
258  return block;
259  }
260 
262  {
263  return *phashBlock;
264  }
265 
273  bool HaveTxsDownloaded() const { return nChainTx != 0; }
274 
275  int64_t GetBlockTime() const
276  {
277  return (int64_t)nTime;
278  }
279 
280  int64_t GetBlockTimeMax() const
281  {
282  return (int64_t)nTimeMax;
283  }
284 
285  static constexpr int nMedianTimeSpan = 11;
286 
287  int64_t GetMedianTimePast() const
288  {
289  int64_t pmedian[nMedianTimeSpan];
290  int64_t* pbegin = &pmedian[nMedianTimeSpan];
291  int64_t* pend = &pmedian[nMedianTimeSpan];
292 
293  const CBlockIndex* pindex = this;
294  for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev)
295  *(--pbegin) = pindex->GetBlockTime();
296 
297  std::sort(pbegin, pend);
298  return pbegin[(pend - pbegin)/2];
299  }
300 
301  std::string ToString() const
302  {
303  return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
304  pprev, nHeight,
305  hashMerkleRoot.ToString(),
306  GetBlockHash().ToString());
307  }
308 
311  {
312  assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
313  if (nStatus & BLOCK_FAILED_MASK)
314  return false;
315  return ((nStatus & BLOCK_VALID_MASK) >= nUpTo);
316  }
317 
320  bool RaiseValidity(enum BlockStatus nUpTo)
321  {
322  assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
323  if (nStatus & BLOCK_FAILED_MASK)
324  return false;
325  if ((nStatus & BLOCK_VALID_MASK) < nUpTo) {
326  nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo;
327  return true;
328  }
329  return false;
330  }
331 
333  void BuildSkip();
334 
336  CBlockIndex* GetAncestor(int height);
337  const CBlockIndex* GetAncestor(int height) const;
338 };
339 
342 int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params&);
344 const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb);
345 
346 
349 {
350 public:
352 
354  hashPrev = uint256();
355  }
356 
357  explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) {
358  hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
359  }
360 
362 
363  template <typename Stream, typename Operation>
364  inline void SerializationOp(Stream& s, Operation ser_action) {
365  int _nVersion = s.GetVersion();
366  if (!(s.GetType() & SER_GETHASH))
368 
370  READWRITE(VARINT(nStatus));
371  READWRITE(VARINT(nTx));
372  if (nStatus & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO))
374  if (nStatus & BLOCK_HAVE_DATA)
375  READWRITE(VARINT(nDataPos));
376  if (nStatus & BLOCK_HAVE_UNDO)
377  READWRITE(VARINT(nUndoPos));
378 
379  // block header
380  READWRITE(this->nVersion);
381  READWRITE(hashPrev);
382  READWRITE(hashMerkleRoot);
383  READWRITE(nTime);
384  READWRITE(nBits);
385  READWRITE(nNonce);
386  }
387 
389  {
390  CBlockHeader block;
391  block.nVersion = nVersion;
392  block.hashPrevBlock = hashPrev;
393  block.hashMerkleRoot = hashMerkleRoot;
394  block.nTime = nTime;
395  block.nBits = nBits;
396  block.nNonce = nNonce;
397  return block.GetHash();
398  }
399 
400 
401  std::string ToString() const
402  {
403  std::string str = "CDiskBlockIndex(";
404  str += CBlockIndex::ToString();
405  str += strprintf("\n hashBlock=%s, hashPrev=%s)",
406  GetBlockHash().ToString(),
407  hashPrev.ToString());
408  return str;
409  }
410 };
411 
413 class CChain {
414 private:
415  std::vector<CBlockIndex*> vChain;
416 
417 public:
419  CBlockIndex *Genesis() const {
420  return vChain.size() > 0 ? vChain[0] : nullptr;
421  }
422 
424  CBlockIndex *Tip() const {
425  return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr;
426  }
427 
430  if (nHeight < 0 || nHeight >= (int)vChain.size())
431  return nullptr;
432  return vChain[nHeight];
433  }
434 
436  friend bool operator==(const CChain &a, const CChain &b) {
437  return a.vChain.size() == b.vChain.size() &&
438  a.vChain[a.vChain.size() - 1] == b.vChain[b.vChain.size() - 1];
439  }
440 
442  bool Contains(const CBlockIndex *pindex) const {
443  return (*this)[pindex->nHeight] == pindex;
444  }
445 
447  CBlockIndex *Next(const CBlockIndex *pindex) const {
448  if (Contains(pindex))
449  return (*this)[pindex->nHeight + 1];
450  else
451  return nullptr;
452  }
453 
455  int Height() const {
456  return vChain.size() - 1;
457  }
458 
460  void SetTip(CBlockIndex *pindex);
461 
463  CBlockLocator GetLocator(const CBlockIndex *pindex = nullptr) const;
464 
466  const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
467 
469  CBlockIndex* FindEarliestAtLeast(int64_t nTime, int height) const;
470 };
471 
472 #endif // BITCOIN_CHAIN_H
uint32_t nNonce
Definition: block.h:29
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: chain.h:164
ADD_SERIALIZE_METHODS
Definition: chain.h:361
std::string ToString() const
Definition: chain.h:301
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
Definition: chain.h:186
ADD_SERIALIZE_METHODS
Definition: chain.h:51
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
Definition: chain.h:149
std::vector< CBlockIndex * > vChain
Definition: chain.h:415
int64_t GetBlockTime() const
Definition: chain.h:275
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:126
descends from failed block
Definition: chain.h:128
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:146
uint32_t nStatus
Verification status of this block. See enum BlockStatus.
Definition: chain.h:176
void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn)
update statistics (does not update nSize)
Definition: chain.h:81
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the forking point between two chain tips.
Definition: chain.cpp:156
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1067
An in-memory indexed chain of blocks.
Definition: chain.h:413
CBlockIndex()
Definition: chain.h:214
unsigned int nHeight
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
Definition: chain.h:103
CBlockHeader GetBlockHeader() const
Definition: chain.h:248
int Height() const
Return the maximal height in the chain.
Definition: chain.h:455
stage after last reached validness failed
Definition: chain.h:127
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid...
Definition: chain.h:110
unsigned int nSize
number of used bytes of block file
Definition: chain.h:44
FlatFilePos GetBlockPos() const
Definition: chain.h:230
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:419
uint32_t nTime
Definition: chain.h:181
undo data available in rev*.dat
Definition: chain.h:124
int nFile
Which # file this block is stored in (blk?????.dat)
Definition: chain.h:155
int nFile
Definition: flatfile.h:16
Unused.
Definition: chain.h:96
static constexpr int64_t TIMESTAMP_WINDOW
Timestamp window used as a grace period by code that compares external timestamps (such as timestamps...
Definition: chain.h:30
unsigned int nHeightLast
highest height of block in file
Definition: chain.h:47
uint32_t nTime
Definition: block.h:27
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block. ...
Definition: chain.h:173
unsigned int nUndoSize
number of used bytes in the undo file
Definition: chain.h:45
uint256 GetBlockHash() const
Definition: chain.h:261
void SetNull()
Definition: chain.h:64
bool IsValid(enum BlockStatus nUpTo=BLOCK_VALID_TRANSACTIONS) const
Check whether this block index entry is valid up to the passed validity level.
Definition: chain.h:310
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:122
Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends...
Definition: chain.h:114
unsigned int nTimeMax
(memory only) Maximum nTime in the chain up to and including this block.
Definition: chain.h:189
uint64_t nTimeFirst
earliest time of block in file
Definition: chain.h:48
CBlockIndex * operator[](int nHeight) const
Returns the index entry at a particular height in this chain, or nullptr if no such height exists...
Definition: chain.h:429
Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
Definition: chain.h:117
uint256 hashMerkleRoot
Definition: block.h:26
uint32_t nNonce
Definition: chain.h:183
unsigned int nDataPos
Byte offset within blk?????.dat where this block&#39;s data is stored.
Definition: chain.h:158
CBlockFileInfo()
Definition: chain.h:74
Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future...
Definition: chain.h:99
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:442
CBlockIndex(const CBlockHeader &block)
Definition: chain.h:219
CBlockIndex * Next(const CBlockIndex *pindex) const
Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip...
Definition: chain.h:447
uint256 hashPrevBlock
Definition: block.h:25
int64_t GetBlockTimeMax() const
Definition: chain.h:280
CDiskBlockIndex()
Definition: chain.h:353
uint256 hashMerkleRoot
Definition: chain.h:180
friend bool operator==(const CChain &a, const CChain &b)
Compare two chains efficiently.
Definition: chain.h:436
unsigned int nHeightFirst
lowest height of block in file
Definition: chain.h:46
Used to marshal pointers into hashes for db storage.
Definition: chain.h:348
std::string ToString() const
Definition: uint256.cpp:61
Parameters that influence chain consensus.
Definition: params.h:49
256-bit unsigned big integer.
int64_t GetMedianTimePast() const
Definition: chain.h:287
block data in blk*.data was received with a witness-enforcing client
Definition: chain.h:131
void SerializationOp(Stream &s, Operation ser_action)
Definition: chain.h:54
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params &)
Return the time it would take to redo the work difference between from and to, assuming the current h...
Definition: chain.cpp:137
unsigned int nUndoPos
Byte offset within rev?????.dat where this block&#39;s undo data is stored.
Definition: chain.h:161
uint256 GetHash() const
Definition: block.cpp:12
int32_t nVersion
block header
Definition: chain.h:179
256-bit opaque blob.
Definition: uint256.h:121
uint256 hashPrev
Definition: chain.h:351
void SerializationOp(Stream &s, Operation ser_action)
Definition: chain.h:364
CDiskBlockIndex(const CBlockIndex *pindex)
Definition: chain.h:357
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:139
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:424
unsigned int nBlocks
number of blocks stored in file
Definition: chain.h:43
std::string ToString() const
Definition: chain.h:401
bool RaiseValidity(enum BlockStatus nUpTo)
Raise the validity level of this block index entry.
Definition: chain.h:320
FlatFilePos GetUndoPos() const
Definition: chain.h:239
BlockStatus
Definition: chain.h:94
All validity bits.
Definition: chain.h:120
uint64_t nTimeLast
latest time of block in file
Definition: chain.h:49
static constexpr int64_t MAX_FUTURE_BLOCK_TIME
Maximum amount of time that a block timestamp is allowed to exceed the current network-adjusted time ...
Definition: chain.h:22
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:152
full block available in blk*.dat
Definition: chain.h:123
uint256 GetBlockHash() const
Definition: chain.h:388
#define READWRITE(...)
Definition: serialize.h:184
std::string ToString() const
unsigned int nPos
Definition: flatfile.h:17
int32_t nVersion
Definition: block.h:24
static constexpr int64_t MAX_BLOCK_TIME_GAP
Maximum gap between node time and block time used for the "Catching up..." mode in GUI...
Definition: chain.h:38
bool HaveTxsDownloaded() const
Check whether this block&#39;s and all previous blocks&#39; transactions have been downloaded (and stored to ...
Definition: chain.h:273
uint32_t nBits
Definition: chain.h:182
#define VARINT(obj,...)
Definition: serialize.h:422
unsigned int nTx
Number of transactions in this block.
Definition: chain.h:168
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:20
uint32_t nBits
Definition: block.h:28
void SetNull()
Definition: chain.h:191
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
Definition: chain.h:143