Dash Core  0.12.2.1
P2P Digital Currency
chain.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 "primitives/block.h"
11 #include "pow.h"
12 #include "tinyformat.h"
13 #include "uint256.h"
14 
15 #include <vector>
16 
18 {
19  int nFile;
20  unsigned int nPos;
21 
23 
24  template <typename Stream, typename Operation>
25  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
28  }
29 
31  SetNull();
32  }
33 
34  CDiskBlockPos(int nFileIn, unsigned int nPosIn) {
35  nFile = nFileIn;
36  nPos = nPosIn;
37  }
38 
39  friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b) {
40  return (a.nFile == b.nFile && a.nPos == b.nPos);
41  }
42 
43  friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b) {
44  return !(a == b);
45  }
46 
47  void SetNull() { nFile = -1; nPos = 0; }
48  bool IsNull() const { return (nFile == -1); }
49 
50  std::string ToString() const
51  {
52  return strprintf("CBlockDiskPos(nFile=%i, nPos=%i)", nFile, nPos);
53  }
54 
55 };
56 
60 
63 
67 
74 
78 
81 
85 
89 
93 };
94 
101 {
102 public:
105 
108 
111 
113  int nHeight;
114 
116  int nFile;
117 
119  unsigned int nDataPos;
120 
122  unsigned int nUndoPos;
123 
126 
129  unsigned int nTx;
130 
134  unsigned int nChainTx;
135 
137  unsigned int nStatus;
138 
140  int nVersion;
142  unsigned int nTime;
143  unsigned int nBits;
144  unsigned int nNonce;
145 
147  uint32_t nSequenceId;
148 
149  void SetNull()
150  {
151  phashBlock = NULL;
152  pprev = NULL;
153  pskip = NULL;
154  nHeight = 0;
155  nFile = 0;
156  nDataPos = 0;
157  nUndoPos = 0;
159  nTx = 0;
160  nChainTx = 0;
161  nStatus = 0;
162  nSequenceId = 0;
163 
164  nVersion = 0;
166  nTime = 0;
167  nBits = 0;
168  nNonce = 0;
169  }
170 
172  {
173  SetNull();
174  }
175 
176  CBlockIndex(const CBlockHeader& block)
177  {
178  SetNull();
179 
180  nVersion = block.nVersion;
182  nTime = block.nTime;
183  nBits = block.nBits;
184  nNonce = block.nNonce;
185  }
186 
188  CDiskBlockPos ret;
189  if (nStatus & BLOCK_HAVE_DATA) {
190  ret.nFile = nFile;
191  ret.nPos = nDataPos;
192  }
193  return ret;
194  }
195 
197  CDiskBlockPos ret;
198  if (nStatus & BLOCK_HAVE_UNDO) {
199  ret.nFile = nFile;
200  ret.nPos = nUndoPos;
201  }
202  return ret;
203  }
204 
206  {
207  CBlockHeader block;
208  block.nVersion = nVersion;
209  if (pprev)
210  block.hashPrevBlock = pprev->GetBlockHash();
212  block.nTime = nTime;
213  block.nBits = nBits;
214  block.nNonce = nNonce;
215  return block;
216  }
217 
219  {
220  return *phashBlock;
221  }
222 
223  int64_t GetBlockTime() const
224  {
225  return (int64_t)nTime;
226  }
227 
228  enum { nMedianTimeSpan=11 };
229 
230  int64_t GetMedianTimePast() const
231  {
232  int64_t pmedian[nMedianTimeSpan];
233  int64_t* pbegin = &pmedian[nMedianTimeSpan];
234  int64_t* pend = &pmedian[nMedianTimeSpan];
235 
236  const CBlockIndex* pindex = this;
237  for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev)
238  *(--pbegin) = pindex->GetBlockTime();
239 
240  std::sort(pbegin, pend);
241  return pbegin[(pend - pbegin)/2];
242  }
243 
244  std::string ToString() const
245  {
246  return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
247  pprev, nHeight,
249  GetBlockHash().ToString());
250  }
251 
254  {
255  assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
257  return false;
258  return ((nStatus & BLOCK_VALID_MASK) >= nUpTo);
259  }
260 
263  bool RaiseValidity(enum BlockStatus nUpTo)
264  {
265  assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
267  return false;
268  if ((nStatus & BLOCK_VALID_MASK) < nUpTo) {
269  nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo;
270  return true;
271  }
272  return false;
273  }
274 
276  void BuildSkip();
277 
279  CBlockIndex* GetAncestor(int height);
280  const CBlockIndex* GetAncestor(int height) const;
281 };
282 
285 {
286 public:
289 
291  hash = uint256();
292  hashPrev = uint256();
293  }
294 
295  explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) {
296  hash = (hash == uint256() ? pindex->GetBlockHash() : hash);
297  hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
298  }
299 
301 
302  template <typename Stream, typename Operation>
303  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
304  if (!(nType & SER_GETHASH))
306 
309  READWRITE(VARINT(nTx));
312  if (nStatus & BLOCK_HAVE_DATA)
314  if (nStatus & BLOCK_HAVE_UNDO)
316 
317  // block hash
318  READWRITE(hash);
319  // block header
320  READWRITE(this->nVersion);
323  READWRITE(nTime);
324  READWRITE(nBits);
325  READWRITE(nNonce);
326  }
327 
329  {
330  if(hash != uint256()) return hash;
331  // should never really get here, keeping this as a fallback
332  CBlockHeader block;
333  block.nVersion = nVersion;
334  block.hashPrevBlock = hashPrev;
336  block.nTime = nTime;
337  block.nBits = nBits;
338  block.nNonce = nNonce;
339  return block.GetHash();
340  }
341 
342 
343  std::string ToString() const
344  {
345  std::string str = "CDiskBlockIndex(";
346  str += CBlockIndex::ToString();
347  str += strprintf("\n hashBlock=%s, hashPrev=%s)",
349  hashPrev.ToString());
350  return str;
351  }
352 };
353 
355 class CChain {
356 private:
357  std::vector<CBlockIndex*> vChain;
358 
359 public:
361  CBlockIndex *Genesis() const {
362  return vChain.size() > 0 ? vChain[0] : NULL;
363  }
364 
366  CBlockIndex *Tip() const {
367  return vChain.size() > 0 ? vChain[vChain.size() - 1] : NULL;
368  }
369 
371  CBlockIndex *operator[](int nHeight) const {
372  if (nHeight < 0 || nHeight >= (int)vChain.size())
373  return NULL;
374  return vChain[nHeight];
375  }
376 
378  friend bool operator==(const CChain &a, const CChain &b) {
379  return a.vChain.size() == b.vChain.size() &&
380  a.vChain[a.vChain.size() - 1] == b.vChain[b.vChain.size() - 1];
381  }
382 
384  bool Contains(const CBlockIndex *pindex) const {
385  return (*this)[pindex->nHeight] == pindex;
386  }
387 
389  CBlockIndex *Next(const CBlockIndex *pindex) const {
390  if (Contains(pindex))
391  return (*this)[pindex->nHeight + 1];
392  else
393  return NULL;
394  }
395 
397  int Height() const {
398  return vChain.size() - 1;
399  }
400 
402  void SetTip(CBlockIndex *pindex);
403 
405  CBlockLocator GetLocator(const CBlockIndex *pindex = NULL) const;
406 
408  const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
409 };
410 
411 #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:125
#define VARINT(obj)
Definition: serialize.h:388
full block available in blk*.dat
Definition: chain.h:87
void SetNull()
Definition: chain.h:47
void SetTip(CBlockIndex *pindex)
Definition: chain.cpp:13
#define READWRITE(obj)
Definition: serialize.h:175
BlockStatus
Definition: chain.h:57
uint256 hashMerkleRoot
Definition: block.h:26
CDiskBlockPos(int nFileIn, unsigned int nPosIn)
Definition: chain.h:34
uint256 hashPrev
Definition: chain.h:288
#define strprintf
Definition: tinyformat.h:1011
bool RaiseValidity(enum BlockStatus nUpTo)
Definition: chain.h:263
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
Definition: chain.h:110
descends from failed block
Definition: chain.h:92
unsigned int nBits
Definition: chain.h:143
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:253
CBlockHeader GetBlockHeader() const
Definition: chain.h:205
CDiskBlockIndex()
Definition: chain.h:290
unsigned int nUndoPos
Byte offset within rev?????.dat where this block&#39;s undo data is stored.
Definition: chain.h:122
unsigned int nNonce
Definition: chain.h:144
CDiskBlockPos GetBlockPos() const
Definition: chain.h:187
uint32_t nTime
Definition: block.h:27
CBlockIndex(const CBlockHeader &block)
Definition: chain.h:176
unsigned int nTx
Definition: chain.h:129
std::string ToString() const
Definition: chain.h:244
void SetNull()
Definition: chain.h:149
All validity bits.
Definition: chain.h:83
CBlockIndex * operator[](int nHeight) const
Definition: chain.h:371
bool Contains(const CBlockIndex *pindex) const
Definition: chain.h:384
std::vector< CBlockIndex * > vChain
Definition: chain.h:357
uint256 hashPrevBlock
Definition: block.h:25
Unused.
Definition: chain.h:59
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
Definition: chain.h:104
CDiskBlockPos GetUndoPos() const
Definition: chain.h:196
uint256 hashMerkleRoot
Definition: chain.h:141
stage after last reached validness failed
Definition: chain.h:91
bool IsNull() const
Definition: chain.h:48
CDiskBlockIndex(const CBlockIndex *pindex)
Definition: chain.h:295
Definition: chain.h:355
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Definition: chain.cpp:53
Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future...
Definition: chain.h:62
uint32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
Definition: chain.h:147
CBlockIndex()
Definition: chain.h:171
int Height() const
Definition: chain.h:397
CBlockIndex * Genesis() const
Definition: chain.h:361
uint32_t nBits
Definition: block.h:28
unsigned int nTime
Definition: chain.h:142
int nFile
Which # file this block is stored in (blk?????.dat)
Definition: chain.h:116
std::string ToString() const
Definition: uint256.cpp:65
uint256 GetBlockHash() const
Definition: chain.h:328
int nFile
Definition: chain.h:19
std::string ToString() const
Definition: chain.h:50
uint256 hash
Definition: chain.h:287
friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b)
Definition: chain.h:39
unsigned int nPos
Definition: chain.h:20
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: chain.h:25
uint32_t nNonce
Definition: block.h:29
undo data available in rev*.dat
Definition: chain.h:88
friend bool operator==(const CChain &a, const CChain &b)
Definition: chain.h:378
CBlockIndex * Next(const CBlockIndex *pindex) const
Definition: chain.h:389
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:78
unsigned int nStatus
Verification status of this block. See enum BlockStatus.
Definition: chain.h:137
friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b)
Definition: chain.h:43
uint256 GetBlockHash() const
Definition: chain.h:218
int32_t nVersion
Definition: block.h:24
ADD_SERIALIZE_METHODS
Definition: chain.h:22
Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
Definition: chain.h:80
unsigned int nChainTx
Definition: chain.h:134
CBlockIndex * Tip() const
Definition: chain.h:366
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: chain.h:303
int64_t GetMedianTimePast() const
Definition: chain.h:230
int64_t GetBlockTime() const
Definition: chain.h:223
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:107
CBlockLocator GetLocator(const CBlockIndex *pindex=NULL) const
Definition: chain.cpp:25
unsigned int nDataPos
Byte offset within blk?????.dat where this block&#39;s data is stored.
Definition: chain.h:119
void BuildSkip()
Build the skiplist pointer for this entry.
Definition: chain.cpp:109
uint256 GetHash() const
Definition: block.cpp:13
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:113
int nVersion
block header
Definition: chain.h:140
ADD_SERIALIZE_METHODS
Definition: chain.h:300
std::string ToString() const
Definition: chain.h:343
CDiskBlockPos()
Definition: chain.h:30