BlockHeader.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file BlockHeader.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <algorithm>
  20. #include <libdevcore/Common.h>
  21. #include <libdevcore/RLP.h>
  22. #include <libdevcore/SHA3.h>
  23. #include "Common.h"
  24. #include "ChainOperationParams.h"
  25. #include "Exceptions.h"
  26. namespace dev
  27. {
  28. namespace eth
  29. {
  30. enum IncludeSeal
  31. {
  32. WithoutSeal = 0,
  33. WithSeal = 1,
  34. OnlySeal = 2
  35. };
  36. enum Strictness
  37. {
  38. CheckEverything,
  39. JustSeal,
  40. QuickNonce,
  41. IgnoreSeal,
  42. CheckNothingNew
  43. };
  44. // TODO: for implementing soon.
  45. /*enum Check
  46. {
  47. CheckBasic,
  48. CheckExtended,
  49. CheckBlock,
  50. CheckParent,
  51. CheckSeal,
  52. CheckSealQuickly,
  53. CheckAll = CheckBasic | CheckExtended | CheckBlock | CheckParent | CheckSeal,
  54. };
  55. using Checks = FlagSet<Check>;*/
  56. enum BlockDataType
  57. {
  58. HeaderData,
  59. BlockData
  60. };
  61. DEV_SIMPLE_EXCEPTION(NoHashRecorded);
  62. DEV_SIMPLE_EXCEPTION(GenesisBlockCannotBeCalculated);
  63. /** @brief Encapsulation of a block header.
  64. * Class to contain all of a block header's data. It is able to parse a block header and populate
  65. * from some given RLP block serialisation with the static fromHeader(), through the method
  66. * populate(). This will not conduct any verification above basic formating. In this case extra
  67. * verification can be performed through verify().
  68. *
  69. * The object may also be populated from an entire block through the explicit
  70. * constructor BlockHeader(bytesConstRef) and manually with the populate() method. These will
  71. * conduct verification of the header against the other information in the block.
  72. *
  73. * The object may be populated with a template given a parent BlockHeader object with the
  74. * populateFromParent() method. The genesis block info may be retrieved with genesis() and the
  75. * corresponding RLP block created with createGenesisBlock().
  76. *
  77. * To determine the header hash without the nonce (for sealing), the method hash(WithoutNonce) is
  78. * provided.
  79. *
  80. * The default constructor creates an empty object, which can be tested against with the boolean
  81. * conversion operator.
  82. */
  83. class BlockHeader
  84. {
  85. friend class BlockChain;
  86. public:
  87. static const unsigned BasicFields = 13;
  88. BlockHeader();
  89. explicit BlockHeader(bytesConstRef _data, BlockDataType _bdt = BlockData, h256 const& _hashWith = h256());
  90. explicit BlockHeader(bytes const& _data, BlockDataType _bdt = BlockData, h256 const& _hashWith = h256()): BlockHeader(&_data, _bdt, _hashWith) {}
  91. static h256 headerHashFromBlock(bytes const& _block) { return headerHashFromBlock(&_block); }
  92. static h256 headerHashFromBlock(bytesConstRef _block);
  93. static RLP extractHeader(bytesConstRef _block);
  94. explicit operator bool() const { return m_timestamp != Invalid256; }
  95. bool operator==(BlockHeader const& _cmp) const
  96. {
  97. return m_parentHash == _cmp.parentHash() &&
  98. m_sha3Uncles == _cmp.sha3Uncles() &&
  99. m_author == _cmp.author() &&
  100. m_stateRoot == _cmp.stateRoot() &&
  101. m_transactionsRoot == _cmp.transactionsRoot() &&
  102. m_receiptsRoot == _cmp.receiptsRoot() &&
  103. m_logBloom == _cmp.logBloom() &&
  104. m_difficulty == _cmp.difficulty() &&
  105. m_number == _cmp.number() &&
  106. m_gasLimit == _cmp.gasLimit() &&
  107. m_gasUsed == _cmp.gasUsed() &&
  108. m_timestamp == _cmp.timestamp() &&
  109. m_extraData == _cmp.extraData();
  110. }
  111. bool operator!=(BlockHeader const& _cmp) const { return !operator==(_cmp); }
  112. void clear();
  113. void noteDirty() const { m_hashWithout = m_hash = h256(); }
  114. void populateFromParent(BlockHeader const& parent);
  115. // TODO: pull out into abstract class Verifier.
  116. void verify(Strictness _s = CheckEverything, BlockHeader const& _parent = BlockHeader(), bytesConstRef _block = bytesConstRef()) const;
  117. void verify(Strictness _s, bytesConstRef _block) const { verify(_s, BlockHeader(), _block); }
  118. h256 hash(IncludeSeal _i = WithSeal) const;
  119. void streamRLP(RLPStream& _s, IncludeSeal _i = WithSeal) const;
  120. void setParentHash(h256 const& _v) { m_parentHash = _v; noteDirty(); }
  121. void setSha3Uncles(h256 const& _v) { m_sha3Uncles = _v; noteDirty(); }
  122. void setTimestamp(u256 const& _v) { m_timestamp = _v; noteDirty(); }
  123. void setAuthor(Address const& _v) { m_author = _v; noteDirty(); }
  124. void setRoots(h256 const& _t, h256 const& _r, h256 const& _u, h256 const& _s) { m_transactionsRoot = _t; m_receiptsRoot = _r; m_stateRoot = _s; m_sha3Uncles = _u; noteDirty(); }
  125. void setGasUsed(u256 const& _v) { m_gasUsed = _v; noteDirty(); }
  126. void setNumber(u256 const& _v) { m_number = _v; noteDirty(); }
  127. void setGasLimit(u256 const& _v) { m_gasLimit = _v; noteDirty(); }
  128. void setExtraData(bytes const& _v) { m_extraData = _v; noteDirty(); }
  129. void setLogBloom(LogBloom const& _v) { m_logBloom = _v; noteDirty(); }
  130. void setDifficulty(u256 const& _v) { m_difficulty = _v; noteDirty(); }
  131. template <class T> void setSeal(unsigned _offset, T const& _value) { if (m_seal.size() <= _offset) m_seal.resize(_offset + 1); m_seal[_offset] = rlp(_value); noteDirty(); }
  132. template <class T> void setSeal(T const& _value) { setSeal(0, _value); }
  133. h256 const& parentHash() const { return m_parentHash; }
  134. h256 const& sha3Uncles() const { return m_sha3Uncles; }
  135. u256 const& timestamp() const { return m_timestamp; }
  136. Address const& author() const { return m_author; }
  137. h256 const& stateRoot() const { return m_stateRoot; }
  138. h256 const& transactionsRoot() const { return m_transactionsRoot; }
  139. h256 const& receiptsRoot() const { return m_receiptsRoot; }
  140. u256 const& gasUsed() const { return m_gasUsed; }
  141. u256 const& number() const { return m_number; }
  142. u256 const& gasLimit() const { return m_gasLimit; }
  143. bytes const& extraData() const { return m_extraData; }
  144. LogBloom const& logBloom() const { return m_logBloom; }
  145. u256 const& difficulty() const { return m_difficulty; }
  146. template <class T> T seal(unsigned _offset = 0) const { T ret; if (_offset < m_seal.size()) ret = RLP(m_seal[_offset]).convert<T>(RLP::VeryStrict); return ret; }
  147. private:
  148. void populate(RLP const& _header);
  149. void streamRLPFields(RLPStream& _s) const;
  150. h256 m_parentHash;
  151. h256 m_sha3Uncles;
  152. h256 m_stateRoot;
  153. h256 m_transactionsRoot;
  154. h256 m_receiptsRoot;
  155. LogBloom m_logBloom;
  156. u256 m_number;
  157. u256 m_gasLimit;
  158. u256 m_gasUsed;
  159. bytes m_extraData;
  160. u256 m_timestamp = Invalid256;
  161. Address m_author;
  162. u256 m_difficulty;
  163. std::vector<bytes> m_seal; ///< Additional (RLP-encoded) header fields.
  164. mutable h256 m_hash; ///< (Memoised) SHA3 hash of the block header with seal.
  165. mutable h256 m_hashWithout; ///< (Memoised) SHA3 hash of the block header without seal.
  166. };
  167. inline std::ostream& operator<<(std::ostream& _out, BlockHeader const& _bi)
  168. {
  169. _out << _bi.hash(WithoutSeal) << " " << _bi.parentHash() << " " << _bi.sha3Uncles() << " " << _bi.author() << " " << _bi.stateRoot() << " " << _bi.transactionsRoot() << " " <<
  170. _bi.receiptsRoot() << " " << _bi.logBloom() << " " << _bi.difficulty() << " " << _bi.number() << " " << _bi.gasLimit() << " " <<
  171. _bi.gasUsed() << " " << _bi.timestamp();
  172. return _out;
  173. }
  174. }
  175. }