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.

block.h 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. static const int32_t CURRENT_VERSION=4;
  22. int32_t nVersion;
  23. uint256 hashPrevBlock;
  24. uint256 hashMerkleRoot;
  25. uint32_t nTime;
  26. uint32_t nBits;
  27. uint32_t nNonce;
  28. CBlockHeader()
  29. {
  30. SetNull();
  31. }
  32. ADD_SERIALIZE_METHODS;
  33. template <typename Stream, typename Operation>
  34. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  35. READWRITE(this->nVersion);
  36. nVersion = this->nVersion;
  37. READWRITE(hashPrevBlock);
  38. READWRITE(hashMerkleRoot);
  39. READWRITE(nTime);
  40. READWRITE(nBits);
  41. READWRITE(nNonce);
  42. }
  43. void SetNull()
  44. {
  45. nVersion = CBlockHeader::CURRENT_VERSION;
  46. hashPrevBlock.SetNull();
  47. hashMerkleRoot.SetNull();
  48. nTime = 0;
  49. nBits = 0;
  50. nNonce = 0;
  51. }
  52. bool IsNull() const
  53. {
  54. return (nBits == 0);
  55. }
  56. uint256 GetHash() const;
  57. int64_t GetBlockTime() const
  58. {
  59. return (int64_t)nTime;
  60. }
  61. };
  62. class CBlock : public CBlockHeader
  63. {
  64. public:
  65. // network and disk
  66. std::vector<CTransaction> vtx;
  67. // memory only
  68. mutable bool fChecked;
  69. CBlock()
  70. {
  71. SetNull();
  72. }
  73. CBlock(const CBlockHeader &header)
  74. {
  75. SetNull();
  76. *((CBlockHeader*)this) = header;
  77. }
  78. ADD_SERIALIZE_METHODS;
  79. template <typename Stream, typename Operation>
  80. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  81. READWRITE(*(CBlockHeader*)this);
  82. READWRITE(vtx);
  83. }
  84. void SetNull()
  85. {
  86. CBlockHeader::SetNull();
  87. vtx.clear();
  88. fChecked = false;
  89. }
  90. CBlockHeader GetBlockHeader() const
  91. {
  92. CBlockHeader block;
  93. block.nVersion = nVersion;
  94. block.hashPrevBlock = hashPrevBlock;
  95. block.hashMerkleRoot = hashMerkleRoot;
  96. block.nTime = nTime;
  97. block.nBits = nBits;
  98. block.nNonce = nNonce;
  99. return block;
  100. }
  101. std::string ToString() const;
  102. };
  103. /** Describes a place in the block chain to another node such that if the
  104. * other node doesn't have the same branch, it can find a recent common trunk.
  105. * The further back it is, the further before the fork it may be.
  106. */
  107. struct CBlockLocator
  108. {
  109. std::vector<uint256> vHave;
  110. CBlockLocator() {}
  111. CBlockLocator(const std::vector<uint256>& vHaveIn)
  112. {
  113. vHave = vHaveIn;
  114. }
  115. ADD_SERIALIZE_METHODS;
  116. template <typename Stream, typename Operation>
  117. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  118. if (!(nType & SER_GETHASH))
  119. READWRITE(nVersion);
  120. READWRITE(vHave);
  121. }
  122. void SetNull()
  123. {
  124. vHave.clear();
  125. }
  126. bool IsNull() const
  127. {
  128. return vHave.empty();
  129. }
  130. };
  131. #endif // BITCOIN_PRIMITIVES_BLOCK_H