Fabcoin Core  0.16.2
P2P Digital Currency
chain.h
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 #ifndef FABCOIN_CHAIN_H
7 #define FABCOIN_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 #include <util.h>
15 
17 #include <libdevcore/SHA3.h>
18 #include <libdevcore/RLP.h>
19 #include <arith_uint256.h>
21 
22 #include <vector>
23 
28 static const int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60;
29 
36 static const int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME;
37 
39 {
40 public:
41  unsigned int nBlocks;
42  unsigned int nSize;
43  unsigned int nUndoSize;
44  unsigned int nHeightFirst;
45  unsigned int nHeightLast;
46  uint64_t nTimeFirst;
47  uint64_t nTimeLast;
48 
50 
51  template <typename Stream, typename Operation>
52  inline void SerializationOp(Stream& s, Operation ser_action) {
53  READWRITE(VARINT(nBlocks));
54  READWRITE(VARINT(nSize));
55  READWRITE(VARINT(nUndoSize));
56  READWRITE(VARINT(nHeightFirst));
57  READWRITE(VARINT(nHeightLast));
58  READWRITE(VARINT(nTimeFirst));
59  READWRITE(VARINT(nTimeLast));
60  }
61 
62  void SetNull() {
63  nBlocks = 0;
64  nSize = 0;
65  nUndoSize = 0;
66  nHeightFirst = 0;
67  nHeightLast = 0;
68  nTimeFirst = 0;
69  nTimeLast = 0;
70  }
71 
73  SetNull();
74  }
75 
76  std::string ToString() const;
77 
79  void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn) {
80  if (nBlocks==0 || nHeightFirst > nHeightIn)
81  nHeightFirst = nHeightIn;
82  if (nBlocks==0 || nTimeFirst > nTimeIn)
83  nTimeFirst = nTimeIn;
84  nBlocks++;
85  if (nHeightIn > nHeightLast)
86  nHeightLast = nHeightIn;
87  if (nTimeIn > nTimeLast)
88  nTimeLast = nTimeIn;
89  }
90 };
91 
93 {
94  int nFile;
95  unsigned int nPos;
96 
98 
99  template <typename Stream, typename Operation>
100  inline void SerializationOp(Stream& s, Operation ser_action) {
101  READWRITE(VARINT(nFile));
102  READWRITE(VARINT(nPos));
103  }
104 
106  SetNull();
107  }
108 
109  CDiskBlockPos(int nFileIn, unsigned int nPosIn) {
110  nFile = nFileIn;
111  nPos = nPosIn;
112  }
113 
114  friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b) {
115  return (a.nFile == b.nFile && a.nPos == b.nPos);
116  }
117 
118  friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b) {
119  return !(a == b);
120  }
121 
122  void SetNull() { nFile = -1; nPos = 0; }
123  bool IsNull() const { return (nFile == -1); }
124 
125  std::string ToString() const
126  {
127  return strprintf("CBlockDiskPos(nFile=%i, nPos=%i)", nFile, nPos);
128  }
129 
130 };
131 
132 enum BlockStatus: uint32_t {
135 
138 
142 
149 
153 
156 
160 
164 
168 
170 };
171 
178 {
179 public:
182 
185 
188 
191 
193  int nHeight;
194 
196  int nFile;
197 
199  unsigned int nDataPos;
200 
202  unsigned int nUndoPos;
203 
206 
209  unsigned int nTx;
210 
214  unsigned int nChainTx;
215 
217  unsigned int nStatus;
218 
220  int nVersion;
222  uint32_t nReserved[7];
223  unsigned int nTime;
224  unsigned int nBits;
229  std::vector<unsigned char> nSolution;
230 
232  int32_t nSequenceId;
233 
235  unsigned int nTimeMax;
236 
237  void SetNull()
238  {
239  phashBlock = nullptr;
240  pprev = nullptr;
241  pnext = nullptr;
242  pskip = nullptr;
243  nHeight = 0;
244  nFile = 0;
245  nDataPos = 0;
246  nUndoPos = 0;
247  nChainWork = arith_uint256();
248  nTx = 0;
249  nChainTx = 0;
250  nStatus = 0;
251  nSequenceId = 0;
252  nTimeMax = 0;
253 
254  nVersion = 0;
255  hashMerkleRoot = uint256();
256  memset(nReserved, 0, sizeof(nReserved));
257  nTime = 0;
258  nBits = 0;
259  nNonce = uint256();
260  hashStateRoot = uint256(h256Touint(dev::h256("e965ffd002cd6ad0e2dc402b8044de833e06b23127ea8c3d80aec91410771495"))); // fasc
261  hashUTXORoot = uint256(h256Touint(dev::sha3(dev::rlp("")))); // fasc
262 
263  nSolution.clear();
264  }
265 
267  {
268  SetNull();
269  }
270 
271  CBlockIndex(const CBlockHeader& block)
272  {
273  SetNull();
274 
275  nVersion = block.nVersion;
276  hashMerkleRoot = block.hashMerkleRoot;
277  nHeight = block.nHeight;
278  memcpy(nReserved, block.nReserved, sizeof(nReserved));
279  nTime = block.nTime;
280  nBits = block.nBits;
281  nNonce = block.nNonce;
282  //nMoneySupply = 0;
283  hashStateRoot = block.hashStateRoot; // fasc
284  hashUTXORoot = block.hashUTXORoot; // fasc
285  nSolution = block.nSolution;
286  }
287 
289  CDiskBlockPos ret;
290  if (nStatus & BLOCK_HAVE_DATA) {
291  ret.nFile = nFile;
292  ret.nPos = nDataPos;
293  }
294  return ret;
295  }
296 
298  CDiskBlockPos ret;
299  if (nStatus & BLOCK_HAVE_UNDO) {
300  ret.nFile = nFile;
301  ret.nPos = nUndoPos;
302  }
303  return ret;
304  }
305 
307  {
308  CBlockHeader block;
309  block.nVersion = nVersion;
310  if (pprev)
311  block.hashPrevBlock = pprev->GetBlockHash();
312  block.hashMerkleRoot = hashMerkleRoot;
313  block.nHeight = nHeight;
314  memcpy(block.nReserved, nReserved, sizeof(block.nReserved));
315  block.nTime = nTime;
316  block.nBits = nBits;
317  block.nNonce = nNonce;
318  block.hashStateRoot = hashStateRoot; // fasc
319  block.hashUTXORoot = hashUTXORoot; // fasc
320  block.nSolution = nSolution;
321  return block;
322  }
323 
325  {
326  return *phashBlock;
327  }
328 
329  int64_t GetBlockTime() const
330  {
331  return (int64_t)nTime;
332  }
333 
334  int64_t GetBlockTimeMax() const
335  {
336  return (int64_t)nTimeMax;
337  }
338 
339  enum { nMedianTimeSpan=11 };
340 
341  int64_t GetMedianTimePast() const
342  {
343  int64_t pmedian[nMedianTimeSpan];
344  int64_t* pbegin = &pmedian[nMedianTimeSpan];
345  int64_t* pend = &pmedian[nMedianTimeSpan];
346 
347  const CBlockIndex* pindex = this;
348  for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev)
349  *(--pbegin) = pindex->GetBlockTime();
350 
351  std::sort(pbegin, pend);
352  return pbegin[(pend - pbegin)/2];
353  }
354 
355  bool IsProofOfWork() const // fasc
356  {
357  return true;
358  }
359 
360  bool IsProofOfStake() const
361  {
362  return false;
363  }
364 
365  bool IsSupportContract() const;
366 
367  bool IsLegacyFormat() const;
368 
369 
370  std::string ToString() const
371  {
372  return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
373  pprev, nHeight,
374  hashMerkleRoot.ToString(),
375  GetBlockHash().ToString());
376  }
377 
380  {
381  assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
382  if (nStatus & BLOCK_FAILED_MASK)
383  return false;
384  return ((nStatus & BLOCK_VALID_MASK) >= nUpTo);
385  }
386 
389  bool RaiseValidity(enum BlockStatus nUpTo)
390  {
391  assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
392  if (nStatus & BLOCK_FAILED_MASK)
393  return false;
394  if ((nStatus & BLOCK_VALID_MASK) < nUpTo) {
395  nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo;
396  return true;
397  }
398  return false;
399  }
400 
402  void BuildSkip();
403 
405  CBlockIndex* GetAncestor(int height);
406  const CBlockIndex* GetAncestor(int height) const;
407 
408 };
409 
412 int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params&);
414 const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex* pb);
415 
416 
419 {
420 public:
422 
424  hashPrev = uint256();
425  }
426 
427  explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) {
428  hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
429  }
430 
432 
433  template <typename Stream, typename Operation>
434  inline void SerializationOp(Stream& s, Operation ser_action) {
435  int _nVersion = s.GetVersion();
436  if (!(s.GetType() & SER_GETHASH))
437  READWRITE(VARINT(_nVersion));
438 
439  READWRITE(VARINT(nHeight));
440  READWRITE(VARINT(nStatus));
441  READWRITE(VARINT(nTx));
442  if (nStatus & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO))
443  READWRITE(VARINT(nFile));
444  if (nStatus & BLOCK_HAVE_DATA)
445  READWRITE(VARINT(nDataPos));
446  if (nStatus & BLOCK_HAVE_UNDO)
447  READWRITE(VARINT(nUndoPos));
448 
449  // block header
450  READWRITE(this->nVersion);
451  READWRITE(hashPrev);
452  READWRITE(hashMerkleRoot);
453  for(size_t i = 0; i < (sizeof(nReserved) / sizeof(nReserved[0])); i++) {
454  READWRITE(nReserved[i]);
455  }
456  READWRITE(nTime);
457  READWRITE(nBits);
458  READWRITE(nNonce);
459 
460  bool hascontract = IsSupportContract();
461  if( hascontract )
462  {
463  READWRITE(hashStateRoot); // fasc
464  READWRITE(hashUTXORoot); // fasc
465  }
466 
467  READWRITE(nSolution);
468  }
469 
471  {
472  CBlockHeader block;
473  block.nVersion = nVersion;
474  block.hashPrevBlock = hashPrev;
475  block.hashMerkleRoot = hashMerkleRoot;
476  block.hashStateRoot = hashStateRoot; // fasc
477  block.hashUTXORoot = hashUTXORoot; // fasc
478  block.nHeight = nHeight;
479  memcpy(block.nReserved, nReserved, sizeof(block.nReserved));
480  block.nTime = nTime;
481  block.nBits = nBits;
482  block.nNonce = nNonce;
483 
484  block.nSolution = nSolution;
485  return block.GetHash();
486  }
487 
488 
489  std::string ToString() const
490  {
491  std::string str = "CDiskBlockIndex(";
492  str += CBlockIndex::ToString();
493  str += strprintf("\n hashBlock=%s, hashPrev=%s)",
494  GetBlockHash().ToString(),
495  hashPrev.ToString());
496  return str;
497  }
498 };
499 
501 class CChain {
502 private:
503  std::vector<CBlockIndex*> vChain;
504 
505 public:
507  CBlockIndex *Genesis() const {
508  return vChain.size() > 0 ? vChain[0] : nullptr;
509  }
510 
512  CBlockIndex *Tip() const {
513  return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr;
514  }
515 
517  CBlockIndex *operator[](int nHeight) const {
518  if (nHeight < 0 || nHeight >= (int)vChain.size())
519  return nullptr;
520  return vChain[nHeight];
521  }
522 
524  friend bool operator==(const CChain &a, const CChain &b) {
525  return a.vChain.size() == b.vChain.size() &&
526  a.vChain[a.vChain.size() - 1] == b.vChain[b.vChain.size() - 1];
527  }
528 
530  bool Contains(const CBlockIndex *pindex) const {
531  return (*this)[pindex->nHeight] == pindex;
532  }
533 
535  CBlockIndex *Next(const CBlockIndex *pindex) const {
536  if (Contains(pindex))
537  return (*this)[pindex->nHeight + 1];
538  else
539  return nullptr;
540  }
541 
543  int Height() const {
544  return vChain.size() - 1;
545  }
546 
548  void SetTip(CBlockIndex *pindex);
549 
551  CBlockLocator GetLocator(const CBlockIndex *pindex = nullptr) const;
552 
554  const CBlockIndex *FindFork(const CBlockIndex *pindex) const;
555 
557  CBlockIndex* FindEarliestAtLeast(int64_t nTime) const;
558 };
559 
560 #endif // FABCOIN_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:205
#define VARINT(obj)
Definition: serialize.h:389
ADD_SERIALIZE_METHODS
Definition: chain.h:431
CDiskBlockPos GetBlockPos() const
Definition: chain.h:288
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
Definition: chain.h:232
ADD_SERIALIZE_METHODS
Definition: chain.h:49
bool IsProofOfStake() const
Definition: chain.h:360
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
CDiskBlockPos(int nFileIn, unsigned int nPosIn)
Definition: chain.h:109
uint256 nNonce
Definition: block.h:53
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
descends from failed block
Definition: chain.h:166
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:184
#define READWRITE(obj)
Definition: serialize.h:179
void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn)
update statistics (does not update nSize)
Definition: chain.h:79
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
Definition: RLP.h:467
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the forking point between two chain tips.
Definition: chain.cpp:188
#define strprintf
Definition: tinyformat.h:1054
An in-memory indexed chain of blocks.
Definition: chain.h:501
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:517
CBlockIndex()
Definition: chain.h:266
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
Definition: chain.h:141
uint32_t nHeight
Definition: block.h:46
std::string ToString() const
Definition: chain.h:370
stage after last reached validness failed
Definition: chain.h:165
uint32_t nReserved[7]
Definition: block.h:47
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid...
Definition: chain.h:148
unsigned int nSize
number of used bytes of block file
Definition: chain.h:42
friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b)
Definition: chain.h:114
assert(len-trim+(2 *lenIndices)<=WIDTH)
undo data available in rev*.dat
Definition: chain.h:162
int nFile
Which # file this block is stored in (blk?????.dat)
Definition: chain.h:196
bool IsProofOfWork() const
Definition: chain.h:355
Unused.
Definition: chain.h:134
unsigned int nHeightLast
highest height of block in file
Definition: chain.h:45
uint32_t nTime
Definition: block.h:48
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block. ...
Definition: chain.h:214
unsigned int nUndoSize
number of used bytes in the undo file
Definition: chain.h:43
std::string ToString() const
Definition: uint256.cpp:95
friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b)
Definition: chain.h:118
int64_t GetBlockTimeMax() const
Definition: chain.h:334
void SetNull()
Definition: chain.h:62
#define a(i)
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:153
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:512
Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends...
Definition: chain.h:152
int Height() const
Return the maximal height in the chain.
Definition: chain.h:543
unsigned int nTimeMax
(memory only) Maximum nTime in the chain upto and including this block.
Definition: chain.h:235
unsigned int nTime
Definition: chain.h:223
uint64_t nTimeFirst
earliest time of block in file
Definition: chain.h:46
ADD_SERIALIZE_METHODS
Definition: chain.h:97
unsigned int nStatus
Verification status of this block. See enum BlockStatus.
Definition: chain.h:217
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:535
Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
Definition: chain.h:155
CBlockHeader GetBlockHeader() const
Definition: chain.h:306
uint256 hashMerkleRoot
Definition: block.h:45
void SerializationOp(Stream &s, Operation ser_action)
Definition: chain.h:100
unsigned int nDataPos
Byte offset within blk?????.dat where this block&#39;s data is stored.
Definition: chain.h:199
CBlockFileInfo()
Definition: chain.h:72
uint256 GetBlockHash() const
Definition: chain.h:470
Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future...
Definition: chain.h:137
CBlockIndex(const CBlockHeader &block)
Definition: chain.h:271
uint256 hashPrevBlock
Definition: block.h:44
unsigned int nBits
Definition: chain.h:224
CDiskBlockIndex()
Definition: chain.h:423
CDiskBlockPos()
Definition: chain.h:105
uint256 hashMerkleRoot
Definition: chain.h:221
friend bool operator==(const CChain &a, const CChain &b)
Compare two chains efficiently.
Definition: chain.h:524
unsigned int nHeightFirst
lowest height of block in file
Definition: chain.h:44
std::string ToString() const
Definition: chain.h:125
Used to marshal pointers into hashes for db storage.
Definition: chain.h:418
bool IsNull() const
Definition: chain.h:123
Parameters that influence chain consensus.
Definition: params.h:39
std::vector< unsigned char > nSolution
Definition: block.h:54
std::string ToString() const
Definition: chain.h:489
256-bit unsigned big integer.
block data in blk*.data was received with a witness-enforcing client
Definition: chain.h:169
unsigned int nPos
Definition: chain.h:95
#define b(i, j)
void SerializationOp(Stream &s, Operation ser_action)
Definition: chain.h:52
int nVersion
block header
Definition: chain.h:220
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:168
unsigned int nUndoPos
Byte offset within rev?????.dat where this block&#39;s undo data is stored.
Definition: chain.h:202
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:507
256-bit opaque blob.
Definition: uint256.h:132
uint256 hashPrev
Definition: chain.h:421
uint256 GetHash() const
Definition: block.cpp:38
CBlockIndex * pnext
pointer to the index of the successor of this block
Definition: chain.h:187
uint256 hashProof
Definition: chain.h:228
void SerializationOp(Stream &s, Operation ser_action)
Definition: chain.h:434
CDiskBlockIndex(const CBlockIndex *pindex)
Definition: chain.h:427
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:177
void * memcpy(void *a, const void *b, size_t c)
void SetNull()
Definition: chain.h:122
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:530
unsigned int nBlocks
number of blocks stored in file
Definition: chain.h:41
uint256 hashUTXORoot
Definition: chain.h:227
std::string ToString() const
bool RaiseValidity(enum BlockStatus nUpTo)
Raise the validity level of this block index entry.
Definition: chain.h:389
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:379
BlockStatus
Definition: chain.h:132
uint256 hashStateRoot
Definition: block.h:50
All validity bits.
Definition: chain.h:158
uint64_t nTimeLast
latest time of block in file
Definition: chain.h:47
CDiskBlockPos GetUndoPos() const
Definition: chain.h:297
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:193
uint256 hashStateRoot
Definition: chain.h:226
full block available in blk*.dat
Definition: chain.h:161
int64_t GetBlockTime() const
Definition: chain.h:329
uint256 hashUTXORoot
Definition: block.h:51
int nFile
Definition: chain.h:94
int64_t GetMedianTimePast() const
Definition: chain.h:341
uint256 nNonce
Definition: chain.h:225
int32_t nVersion
Definition: block.h:43
unsigned int nTx
Number of transactions in this block.
Definition: chain.h:209
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:35
std::vector< unsigned char > nSolution
Definition: chain.h:229
uint256 h256Touint(const dev::h256 &in)
Definition: uint256.h:178
uint256 GetBlockHash() const
Definition: chain.h:324
uint32_t nBits
Definition: block.h:49
void SetNull()
Definition: chain.h:237
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
Definition: chain.h:181