Bitcoin
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 
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;
50 
52 
53  template <typename Stream, typename Operation>
54  inline void SerializationOp(Stream& s, Operation ser_action) {
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 
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 
177 
184 
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;
201  nTx = 0;
202  nChainTx = 0;
203  nStatus = 0;
204  nSequenceId = 0;
205  nTimeMax = 0;
206 
207  nVersion = 0;
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;
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();
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 
276  {
277  return (int64_t)nTime;
278  }
279 
281  {
282  return (int64_t)nTimeMax;
283  }
284 
285  static constexpr int nMedianTimeSpan = 11;
286 
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,
306  GetBlockHash().ToString());
307  }
308 
311  {
312  assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
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.
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 
371  READWRITE(VARINT(nTx));
374  if (nStatus & BLOCK_HAVE_DATA)
376  if (nStatus & BLOCK_HAVE_UNDO)
378 
379  // block header
380  READWRITE(this->nVersion);
381  READWRITE(hashPrev);
383  READWRITE(nTime);
384  READWRITE(nBits);
385  READWRITE(nNonce);
386  }
387 
389  {
390  CBlockHeader block;
391  block.nVersion = nVersion;
392  block.hashPrevBlock = hashPrev;
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)",
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
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
void SerializationOp(Stream &s, Operation ser_action)
Definition: chain.h:364
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
Definition: chain.h:186
Definition: flatfile.h:14
void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn)
Definition: chain.h:81
uint64_t nTimeLast
latest time of block in file
Definition: chain.h:49
unsigned int nTimeMax
(memory only) Maximum nTime in the chain up to and including this block.
Definition: chain.h:189
descends from failed block
Definition: chain.h:128
void SetTip(CBlockIndex *pindex)
Definition: chain.cpp:11
uint64_t nTimeFirst
earliest time of block in file
Definition: chain.h:48
Definition: block.h:126
uint32_t nTime
Definition: chain.h:181
uint256 hashMerkleRoot
Definition: block.h:26
uint256 hashPrev
Definition: chain.h:351
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Definition: chain.cpp:156
#define strprintf
Definition: tinyformat.h:1067
bool RaiseValidity(enum BlockStatus nUpTo)
Definition: chain.h:320
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
Definition: chain.h:149
int32_t nVersion
block header
Definition: chain.h:179
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
CBlockHeader GetBlockHeader() const
Definition: chain.h:248
unsigned int nHeight
Definition: mempool_eviction.cpp:15
Definition: chain.h:103
uint32_t nNonce
Definition: chain.h:183
CDiskBlockIndex()
Definition: chain.h:353
unsigned int nUndoSize
number of used bytes in the undo file
Definition: chain.h:45
unsigned int nUndoPos
Byte offset within rev?????.dat where this block's undo data is stored.
Definition: chain.h:161
int64_t GetBlockTimeMax() const
Definition: chain.h:280
std::string ToString() const
Definition: validation.cpp:4599
stage after last reached validness failed
Definition: chain.h:127
Definition: chain.h:110
Definition: params.h:49
undo data available in rev*.dat
Definition: chain.h:124
uint32_t nTime
Definition: block.h:27
CBlockIndex(const CBlockHeader &block)
Definition: chain.h:219
unsigned int nTx
Definition: chain.h:168
Unused.
Definition: chain.h:96
uint32_t nBits
Definition: chain.h:182
std::string ToString() const
Definition: chain.h:301
static constexpr int64_t TIMESTAMP_WINDOW
Definition: chain.h:30
void SetNull()
Definition: chain.h:191
unsigned int nSize
number of used bytes of block file
Definition: chain.h:44
Definition: chain.h:348
CBlockIndex * operator[](int nHeight) const
Definition: chain.h:429
bool Contains(const CBlockIndex *pindex) const
Definition: chain.h:442
uint32_t nStatus
Verification status of this block. See enum BlockStatus.
Definition: chain.h:176
std::vector< CBlockIndex * > vChain
Definition: chain.h:415
uint256 hashPrevBlock
Definition: block.h:25
FlatFilePos GetUndoPos() const
Definition: chain.h:239
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:122
Definition: chain.h:114
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
Definition: chain.h:143
ADD_SERIALIZE_METHODS
Definition: chain.h:51
Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
Definition: chain.h:117
Definition: serialize.h:177
unsigned int nHeightLast
highest height of block in file
Definition: chain.h:47
unsigned int uint32_t
Definition: stdint.h:21
uint256 hashMerkleRoot
Definition: chain.h:180
Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future.
Definition: chain.h:99
unsigned long long uint64_t
Definition: stdint.h:22
CDiskBlockIndex(const CBlockIndex *pindex)
Definition: chain.h:357
Definition: chain.h:413
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Definition: chain.cpp:51
CBlockIndex()
Definition: chain.h:214
int Height() const
Definition: chain.h:455
unsigned int nPos
Definition: flatfile.h:17
CBlockIndex * Genesis() const
Definition: chain.h:419
uint32_t nBits
Definition: block.h:28
void SerializationOp(Stream &s, Operation ser_action)
Definition: chain.h:54
int nFile
Which # file this block is stored in (blk?????.dat)
Definition: chain.h:155
std::string ToString() const
Definition: uint256.cpp:61
uint256 GetBlockHash() const
Definition: chain.h:388
CBlockFileInfo()
Definition: chain.h:74
Definition: arith_uint256.h:252
block data in blk*.data was received with a witness-enforcing client
Definition: chain.h:131
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params &)
Definition: chain.cpp:137
Definition: uint256.h:121
CBlockLocator GetLocator(const CBlockIndex *pindex=nullptr) const
Definition: chain.cpp:23
Definition: block.h:20
uint32_t nNonce
Definition: block.h:29
unsigned int nBlocks
number of blocks stored in file
Definition: chain.h:43
friend bool operator==(const CChain &a, const CChain &b)
Definition: chain.h:436
CBlockIndex * Next(const CBlockIndex *pindex) const
Definition: chain.h:447
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:111
int nFile
Definition: flatfile.h:16
CBlockIndex * FindEarliestAtLeast(int64_t nTime, int height) const
Definition: chain.cpp:62
uint256 GetBlockHash() const
Definition: chain.h:261
int32_t nVersion
Definition: block.h:24
Definition: chain.h:125
void SetNull()
Definition: chain.h:64
unsigned int nChainTx
Definition: chain.h:173
bool HaveTxsDownloaded() const
Definition: chain.h:273
CBlockIndex * Tip() const
Definition: chain.h:424
Definition: chain.h:139
signed long long int64_t
Definition: stdint.h:18
int64_t GetMedianTimePast() const
Definition: chain.h:287
Definition: chain.h:129
BlockStatus
Definition: chain.h:94
All validity bits.
Definition: chain.h:120
int64_t GetBlockTime() const
Definition: chain.h:275
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:146
static constexpr int64_t MAX_FUTURE_BLOCK_TIME
Definition: chain.h:22
static constexpr int nMedianTimeSpan
Definition: chain.h:285
signed int int32_t
Definition: stdint.h:17
unsigned int nHeightFirst
lowest height of block in file
Definition: chain.h:46
full block available in blk*.dat
Definition: chain.h:123
FlatFilePos GetBlockPos() const
Definition: chain.h:230
#define READWRITE(...)
Definition: serialize.h:184
unsigned int nDataPos
Byte offset within blk?????.dat where this block's data is stored.
Definition: chain.h:158
void BuildSkip()
Build the skiplist pointer for this entry.
Definition: chain.cpp:116
uint256 GetHash() const
Definition: block.cpp:12
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:152
ADD_SERIALIZE_METHODS
Definition: chain.h:361
static constexpr int64_t MAX_BLOCK_TIME_GAP
Definition: chain.h:38
#define VARINT(obj,...)
Definition: serialize.h:422
Definition: chain.h:40
std::string ToString() const
Definition: chain.h:401