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.5KB

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