You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #ifndef BITCOIN_PRIMITIVES_BLOCK_H
  6. #define BITCOIN_PRIMITIVES_BLOCK_H
  7. #include "primitives/transaction.h"
  8. #include "serialize.h"
  9. #include "uint256.h"
  10. /** Nodes collect new transactions into a block, hash them into a hash tree,
  11. * and scan through nonce values to make the block's hash satisfy proof-of-work
  12. * requirements. When they solve the proof-of-work, they broadcast the block
  13. * to everyone and the block is added to the block chain. The first transaction
  14. * in the block is a special one that creates a new coin owned by the creator
  15. * of the block.
  16. */
  17. class CBlockHeader
  18. {
  19. public:
  20. // header
  21. int32_t nVersion;
  22. uint256 hashPrevBlock;
  23. uint256 hashMerkleRoot;
  24. uint32_t nTime;
  25. uint32_t nBits;
  26. uint32_t nNonce;
  27. CBlockHeader()
  28. {
  29. SetNull();
  30. }
  31. ADD_SERIALIZE_METHODS;
  32. template <typename Stream, typename Operation>
  33. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  34. READWRITE(this->nVersion);
  35. nVersion = this->nVersion;
  36. READWRITE(hashPrevBlock);
  37. READWRITE(hashMerkleRoot);
  38. READWRITE(nTime);
  39. READWRITE(nBits);
  40. READWRITE(nNonce);
  41. }
  42. void SetNull()
  43. {
  44. nVersion = 0;
  45. hashPrevBlock.SetNull();
  46. hashMerkleRoot.SetNull();
  47. nTime = 0;
  48. nBits = 0;
  49. nNonce = 0;
  50. }
  51. bool IsNull() const
  52. {
  53. return (nBits == 0);
  54. }
  55. uint256 GetHash() const;
  56. int64_t GetBlockTime() const
  57. {
  58. return (int64_t)nTime;
  59. }
  60. };
  61. class CBlock : public CBlockHeader
  62. {
  63. public:
  64. // network and disk
  65. std::vector<CTransaction> vtx;
  66. // memory only
  67. mutable bool fChecked;
  68. CBlock()
  69. {
  70. SetNull();
  71. }
  72. CBlock(const CBlockHeader &header)
  73. {
  74. SetNull();
  75. *((CBlockHeader*)this) = header;
  76. }
  77. ADD_SERIALIZE_METHODS;
  78. template <typename Stream, typename Operation>
  79. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  80. READWRITE(*(CBlockHeader*)this);
  81. READWRITE(vtx);
  82. }
  83. void SetNull()
  84. {
  85. CBlockHeader::SetNull();
  86. vtx.clear();
  87. fChecked = false;
  88. }
  89. CBlockHeader GetBlockHeader() const
  90. {
  91. CBlockHeader block;
  92. block.nVersion = nVersion;
  93. block.hashPrevBlock = hashPrevBlock;
  94. block.hashMerkleRoot = hashMerkleRoot;
  95. block.nTime = nTime;
  96. block.nBits = nBits;
  97. block.nNonce = nNonce;
  98. return block;
  99. }
  100. std::string ToString() const;
  101. };
  102. /** Describes a place in the block chain to another node such that if the
  103. * other node doesn't have the same branch, it can find a recent common trunk.
  104. * The further back it is, the further before the fork it may be.
  105. */
  106. struct CBlockLocator
  107. {
  108. std::vector<uint256> vHave;
  109. CBlockLocator() {}
  110. CBlockLocator(const std::vector<uint256>& vHaveIn)
  111. {
  112. vHave = vHaveIn;
  113. }
  114. ADD_SERIALIZE_METHODS;
  115. template <typename Stream, typename Operation>
  116. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  117. if (!(nType & SER_GETHASH))
  118. READWRITE(nVersion);
  119. READWRITE(vHave);
  120. }
  121. void SetNull()
  122. {
  123. vHave.clear();
  124. }
  125. bool IsNull() const
  126. {
  127. return vHave.empty();
  128. }
  129. };
  130. #endif // BITCOIN_PRIMITIVES_BLOCK_H