Ethereum  PoC-8
The C++ Implementation of Ethereum
BlockHeader.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #pragma once
23 
24 #include "ChainOperationParams.h"
25 #include "Common.h"
26 #include "Exceptions.h"
27 #include <libdevcore/Common.h>
28 #include <libdevcore/Guards.h>
29 #include <libdevcore/Log.h>
30 #include <libdevcore/RLP.h>
31 #include <libdevcore/SHA3.h>
32 #include <algorithm>
33 
34 namespace dev
35 {
36 namespace eth
37 {
38 
40 {
42  WithSeal = 1,
43  OnlySeal = 2
44 };
45 
47 {
52 };
53 
54 // TODO: for implementing soon.
55 /*enum Check
56 {
57  CheckBasic,
58  CheckExtended,
59  CheckBlock,
60  CheckParent,
61  CheckSeal,
62  CheckSealQuickly,
63  CheckAll = CheckBasic | CheckExtended | CheckBlock | CheckParent | CheckSeal,
64 };
65 using Checks = FlagSet<Check>;*/
66 
68 {
70  BlockData
71 };
72 
73 DEV_SIMPLE_EXCEPTION(NoHashRecorded);
74 DEV_SIMPLE_EXCEPTION(GenesisBlockCannotBeCalculated);
75 
97 {
98  friend class BlockChain;
99 public:
100  static const unsigned BasicFields = 13;
101 
102  BlockHeader();
103  explicit BlockHeader(bytesConstRef _data, BlockDataType _bdt = BlockData, h256 const& _hashWith = h256());
104  explicit BlockHeader(bytes const& _data, BlockDataType _bdt = BlockData, h256 const& _hashWith = h256()): BlockHeader(&_data, _bdt, _hashWith) {}
105  BlockHeader(BlockHeader const& _other);
106  BlockHeader& operator=(BlockHeader const& _other);
107 
108  static h256 headerHashFromBlock(bytes const& _block) { return headerHashFromBlock(&_block); }
109  static h256 headerHashFromBlock(bytesConstRef _block);
110  static RLP extractHeader(bytesConstRef _block);
111 
112  explicit operator bool() const { return m_timestamp >= 0; }
113 
114  bool operator==(BlockHeader const& _cmp) const
115  {
116  return m_parentHash == _cmp.parentHash() &&
117  m_sha3Uncles == _cmp.sha3Uncles() &&
118  m_author == _cmp.author() &&
119  m_stateRoot == _cmp.stateRoot() &&
120  m_transactionsRoot == _cmp.transactionsRoot() &&
121  m_receiptsRoot == _cmp.receiptsRoot() &&
122  m_logBloom == _cmp.logBloom() &&
123  m_difficulty == _cmp.difficulty() &&
124  m_number == _cmp.number() &&
125  m_gasLimit == _cmp.gasLimit() &&
126  m_gasUsed == _cmp.gasUsed() &&
127  m_timestamp == _cmp.timestamp() &&
128  m_extraData == _cmp.extraData();
129  }
130  bool operator!=(BlockHeader const& _cmp) const { return !operator==(_cmp); }
131 
132  void clear();
133  void noteDirty() const { Guard l(m_hashLock); m_hashWithout = m_hash = h256(); }
134  void populateFromParent(BlockHeader const& parent);
135 
136  // TODO: pull out into abstract class Verifier.
137  void verify(Strictness _s = CheckEverything, BlockHeader const& _parent = BlockHeader(), bytesConstRef _block = bytesConstRef()) const;
138  void verify(Strictness _s, bytesConstRef _block) const { verify(_s, BlockHeader(), _block); }
139 
140  h256 hash(IncludeSeal _i = WithSeal) const;
141  void streamRLP(RLPStream& _s, IncludeSeal _i = WithSeal) const;
142 
143  void setParentHash(h256 const& _v) { m_parentHash = _v; noteDirty(); }
144  void setSha3Uncles(h256 const& _v) { m_sha3Uncles = _v; noteDirty(); }
145  void setTimestamp(int64_t _v) { m_timestamp = _v; noteDirty(); }
146  void setAuthor(Address const& _v) { m_author = _v; noteDirty(); }
147  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(); }
148  void setGasUsed(u256 const& _v) { m_gasUsed = _v; noteDirty(); }
149  void setNumber(int64_t _v) { m_number = _v; noteDirty(); }
150  void setGasLimit(u256 const& _v) { m_gasLimit = _v; noteDirty(); }
151  void setExtraData(bytes const& _v) { m_extraData = _v; noteDirty(); }
152  void setLogBloom(LogBloom const& _v) { m_logBloom = _v; noteDirty(); }
153  void setDifficulty(u256 const& _v) { m_difficulty = _v; noteDirty(); }
154  template <class T> void setSeal(unsigned _offset, T const& _value) { Guard l(m_sealLock); if (m_seal.size() <= _offset) m_seal.resize(_offset + 1); m_seal[_offset] = rlp(_value); noteDirty(); }
155  template <class T> void setSeal(T const& _value) { setSeal(0, _value); }
156 
157  h256 const& parentHash() const { return m_parentHash; }
158  h256 const& sha3Uncles() const { return m_sha3Uncles; }
159  bool hasUncles() const { return m_sha3Uncles != EmptyListSHA3; }
160  int64_t timestamp() const { return m_timestamp; }
161  Address const& author() const { return m_author; }
162  h256 const& stateRoot() const { return m_stateRoot; }
163  h256 const& transactionsRoot() const { return m_transactionsRoot; }
164  h256 const& receiptsRoot() const { return m_receiptsRoot; }
165  u256 const& gasUsed() const { return m_gasUsed; }
166  int64_t number() const { return m_number; }
167  u256 const& gasLimit() const { return m_gasLimit; }
168  bytes const& extraData() const { return m_extraData; }
169  LogBloom const& logBloom() const { return m_logBloom; }
170  u256 const& difficulty() const { return m_difficulty; }
171  template <class T> T seal(unsigned _offset = 0) const { T ret; Guard l(m_sealLock); if (_offset < m_seal.size()) ret = RLP(m_seal[_offset]).convert<T>(RLP::VeryStrict); return ret; }
172 
173 private:
174  void populate(RLP const& _header);
175  void streamRLPFields(RLPStream& _s) const;
176  std::vector<bytes> seal() const
177  {
178  Guard l(m_sealLock);
179  return m_seal;
180  }
181  h256 hashRawRead() const
182  {
183  Guard l(m_hashLock);
184  return m_hash;
185  }
186  h256 hashWithoutRawRead() const
187  {
188  Guard l(m_hashLock);
189  return m_hashWithout;
190  }
191 
192  h256 m_parentHash;
193  h256 m_sha3Uncles;
194  h256 m_stateRoot;
195  h256 m_transactionsRoot;
196  h256 m_receiptsRoot;
197  LogBloom m_logBloom;
198  int64_t m_number = 0;
199  u256 m_gasLimit;
200  u256 m_gasUsed;
201  bytes m_extraData;
202  int64_t m_timestamp = -1;
203 
204  Address m_author;
205  u256 m_difficulty;
206 
207  std::vector<bytes> m_seal;
208  mutable Mutex m_sealLock;
209 
210  mutable h256 m_hash;
211  mutable h256 m_hashWithout;
212  mutable Mutex m_hashLock;
213 
214  mutable Logger m_logger{createLogger(VerbosityDebug, "blockhdr")};
215 };
216 
217 inline std::ostream& operator<<(std::ostream& _out, BlockHeader const& _bi)
218 {
219  _out << _bi.hash(WithoutSeal) << " " << _bi.parentHash() << " " << _bi.sha3Uncles() << " " << _bi.author() << " " << _bi.stateRoot() << " " << _bi.transactionsRoot() << " " <<
220  _bi.receiptsRoot() << " " << _bi.logBloom() << " " << _bi.difficulty() << " " << _bi.number() << " " << _bi.gasLimit() << " " <<
221  _bi.gasUsed() << " " << _bi.timestamp();
222  return _out;
223 }
224 
225 }
226 }
dev::eth::BlockHeader::setGasLimit
void setGasLimit(u256 const &_v)
Definition: BlockHeader.h:150
dev::eth::BlockHeader::setSeal
void setSeal(T const &_value)
Definition: BlockHeader.h:155
dev::RLP::VeryStrict
@ VeryStrict
Definition: RLP.h:58
dev::eth::BlockHeader::hash
h256 hash(IncludeSeal _i=WithSeal) const
Definition: BlockHeader.cpp:119
dev::eth::BlockHeader::verify
void verify(Strictness _s, bytesConstRef _block) const
Definition: BlockHeader.h:138
dev::eth::HeaderData
@ HeaderData
Definition: BlockHeader.h:69
dev::vector_ref< byte const >
dev::eth::BlockHeader
Encapsulation of a block header. Class to contain all of a block header's data. It is able to parse a...
Definition: BlockHeader.h:97
dev::eth::BlockHeader::setLogBloom
void setLogBloom(LogBloom const &_v)
Definition: BlockHeader.h:152
dev::eth::BlockHeader::setSeal
void setSeal(unsigned _offset, T const &_value)
Definition: BlockHeader.h:154
dev::eth::BlockHeader::sha3Uncles
h256 const & sha3Uncles() const
Definition: BlockHeader.h:158
dev::eth::BlockHeader::extraData
bytes const & extraData() const
Definition: BlockHeader.h:168
dev::Guard
std::lock_guard< std::mutex > Guard
Definition: Guards.h:41
dev::eth::BlockHeader::setGasUsed
void setGasUsed(u256 const &_v)
Definition: BlockHeader.h:148
dev::eth::BlockHeader::stateRoot
h256 const & stateRoot() const
Definition: BlockHeader.h:162
dev::eth::BlockHeader::logBloom
LogBloom const & logBloom() const
Definition: BlockHeader.h:169
dev::eth::BlockHeader::BlockHeader
BlockHeader(bytes const &_data, BlockDataType _bdt=BlockData, h256 const &_hashWith=h256())
Definition: BlockHeader.h:104
dev::h256
FixedHash< 32 > h256
Definition: FixedHash.h:356
Exceptions.h
dev::eth::QuickNonce
@ QuickNonce
Definition: BlockHeader.h:49
dev::eth::BlockHeader::BasicFields
static const unsigned BasicFields
Definition: BlockHeader.h:100
dev::eth::BlockData
@ BlockData
Definition: BlockHeader.h:70
dev::eth::BlockHeader::clear
void clear()
Definition: BlockHeader.cpp:100
dev::eth::BlockHeader::verify
void verify(Strictness _s=CheckEverything, BlockHeader const &_parent=BlockHeader(), bytesConstRef _block=bytesConstRef()) const
Definition: BlockHeader.cpp:210
dev::rlp
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
Definition: RLP.h:453
ChainOperationParams.h
dev::FixedHash< 32 >
dev::eth::BlockHeader::seal
T seal(unsigned _offset=0) const
Definition: BlockHeader.h:171
dev::eth::BlockHeader::setTimestamp
void setTimestamp(int64_t _v)
Definition: BlockHeader.h:145
Common.h
dev::eth::BlockHeader::setSha3Uncles
void setSha3Uncles(h256 const &_v)
Definition: BlockHeader.h:144
dev::eth::BlockHeader::setRoots
void setRoots(h256 const &_t, h256 const &_r, h256 const &_u, h256 const &_s)
Definition: BlockHeader.h:147
dev::eth::LogBloom
h2048 LogBloom
The log bloom's size (2048-bit).
Definition: Common.h:50
dev::Logger
boost::log::sources::severity_channel_logger<> Logger
Definition: Log.h:124
Common.h
dev::eth::BlockHeader::parentHash
h256 const & parentHash() const
Definition: BlockHeader.h:157
dev::eth::BlockHeader::extractHeader
static RLP extractHeader(bytesConstRef _block)
Definition: BlockHeader.cpp:156
dev::eth::BlockHeader::hasUncles
bool hasUncles() const
Definition: BlockHeader.h:159
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::createLogger
Logger createLogger(int _severity, std::string const &_channel)
Definition: Log.h:125
SHA3.h
dev::eth::BlockHeader::setNumber
void setNumber(int64_t _v)
Definition: BlockHeader.h:149
dev::bytesConstRef
vector_ref< byte const > bytesConstRef
Definition: Common.h:74
dev::eth::BlockChain
Implements the blockchain database. All data this gives is disk-backed. @threadsafe.
Definition: BlockChain.h:105
dev::eth::CheckEverything
@ CheckEverything
Definition: BlockHeader.h:48
dev::eth::BlockHeader::number
int64_t number() const
Definition: BlockHeader.h:166
dev::eth::BlockHeader::noteDirty
void noteDirty() const
Definition: BlockHeader.h:133
dev::eth::BlockHeader::streamRLP
void streamRLP(RLPStream &_s, IncludeSeal _i=WithSeal) const
Definition: BlockHeader.cpp:139
dev::RLPStream
Class for writing to an RLP bytestream.
Definition: RLP.h:370
dev::eth::IgnoreSeal
@ IgnoreSeal
Definition: BlockHeader.h:50
dev::VerbosityDebug
@ VerbosityDebug
Definition: Log.h:71
dev::Mutex
std::mutex Mutex
Definition: Guards.h:37
dev::eth::BlockHeader::transactionsRoot
h256 const & transactionsRoot() const
Definition: BlockHeader.h:163
dev::eth::BlockHeader::populateFromParent
void populateFromParent(BlockHeader const &parent)
Definition: BlockHeader.cpp:200
dev::RLP::convert
T convert(int _flags) const
Definition: RLP.h:364
dev::eth::BlockHeader::operator=
BlockHeader & operator=(BlockHeader const &_other)
Definition: BlockHeader.cpp:67
dev::eth::BlockHeader::setExtraData
void setExtraData(bytes const &_v)
Definition: BlockHeader.h:151
dev::eth::BlockHeader::setParentHash
void setParentHash(h256 const &_v)
Definition: BlockHeader.h:143
dev::u256
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
dev::eth::operator<<
std::ostream & operator<<(std::ostream &_out, BlockHeader const &_bi)
Definition: BlockHeader.h:217
dev::eth::BlockHeader::BlockHeader
BlockHeader()
Definition: BlockHeader.cpp:35
dev::eth::DEV_SIMPLE_EXCEPTION
DEV_SIMPLE_EXCEPTION(NoHashRecorded)
dev::eth::BlockHeader::author
Address const & author() const
Definition: BlockHeader.h:161
dev::eth::BlockHeader::setAuthor
void setAuthor(Address const &_v)
Definition: BlockHeader.h:146
dev
Definition: Address.cpp:21
dev::eth::BlockHeader::operator==
bool operator==(BlockHeader const &_cmp) const
Definition: BlockHeader.h:114
dev::eth::WithSeal
@ WithSeal
Definition: BlockHeader.h:42
dev::eth::CheckNothingNew
@ CheckNothingNew
Definition: BlockHeader.h:51
dev::EmptyListSHA3
h256 const EmptyListSHA3
Definition: SHA3.cpp:26
dev::eth::IncludeSeal
IncludeSeal
Definition: BlockHeader.h:40
dev::eth::BlockHeader::gasLimit
u256 const & gasLimit() const
Definition: BlockHeader.h:167
dev::eth::BlockHeader::receiptsRoot
h256 const & receiptsRoot() const
Definition: BlockHeader.h:164
Guards.h
dev::eth::Strictness
Strictness
Definition: BlockHeader.h:47
dev::eth::BlockHeader::headerHashFromBlock
static h256 headerHashFromBlock(bytes const &_block)
Definition: BlockHeader.h:108
dev::RLP
Definition: RLP.h:48
dev::eth::BlockHeader::setDifficulty
void setDifficulty(u256 const &_v)
Definition: BlockHeader.h:153
dev::eth::OnlySeal
@ OnlySeal
Definition: BlockHeader.h:43
dev::Address
h160 Address
Definition: Address.h:30
dev::eth::BlockHeader::difficulty
u256 const & difficulty() const
Definition: BlockHeader.h:170
Log.h
RLP.h
dev::eth::BlockDataType
BlockDataType
Definition: BlockHeader.h:68
dev::eth::BlockHeader::gasUsed
u256 const & gasUsed() const
Definition: BlockHeader.h:165
dev::eth::WithoutSeal
@ WithoutSeal
Definition: BlockHeader.h:41
dev::eth::BlockHeader::timestamp
int64_t timestamp() const
Definition: BlockHeader.h:160
dev::eth::BlockHeader::operator!=
bool operator!=(BlockHeader const &_cmp) const
Definition: BlockHeader.h:130