Fabcoin Core  0.16.2
P2P Digital Currency
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 <algorithm>
25 #include <libdevcore/Common.h>
26 #include <libdevcore/RLP.h>
27 #include <libdevcore/SHA3.h>
28 #include "Common.h"
29 #include "ChainOperationParams.h"
30 #include "Exceptions.h"
31 
32 namespace dev
33 {
34 namespace eth
35 {
36 
38 {
40  WithSeal = 1,
42 };
43 
45 {
51 };
52 
53 // TODO: for implementing soon.
54 /*enum Check
55 {
56  CheckBasic,
57  CheckExtended,
58  CheckBlock,
59  CheckParent,
60  CheckSeal,
61  CheckSealQuickly,
62  CheckAll = CheckBasic | CheckExtended | CheckBlock | CheckParent | CheckSeal,
63 };
64 using Checks = FlagSet<Check>;*/
65 
67 {
70 };
71 
72 DEV_SIMPLE_EXCEPTION(NoHashRecorded);
73 DEV_SIMPLE_EXCEPTION(GenesisBlockCannotBeCalculated);
74 
96 {
97  friend class BlockChain;
98 public:
99  static const unsigned BasicFields = 13;
100 
101  BlockHeader();
102  explicit BlockHeader(bytesConstRef _data, BlockDataType _bdt = BlockData, h256 const& _hashWith = h256());
103  explicit BlockHeader(bytes const& _data, BlockDataType _bdt = BlockData, h256 const& _hashWith = h256()): BlockHeader(&_data, _bdt, _hashWith) {}
104 
105  static h256 headerHashFromBlock(bytes const& _block) { return headerHashFromBlock(&_block); }
106  static h256 headerHashFromBlock(bytesConstRef _block);
107  static RLP extractHeader(bytesConstRef _block);
108 
109  explicit operator bool() const { return m_timestamp != Invalid256; }
110 
111  bool operator==(BlockHeader const& _cmp) const
112  {
113  return m_parentHash == _cmp.parentHash() &&
114  m_sha3Uncles == _cmp.sha3Uncles() &&
115  m_author == _cmp.author() &&
116  m_stateRoot == _cmp.stateRoot() &&
118  m_receiptsRoot == _cmp.receiptsRoot() &&
119  m_logBloom == _cmp.logBloom() &&
120  m_difficulty == _cmp.difficulty() &&
121  m_number == _cmp.number() &&
122  m_gasLimit == _cmp.gasLimit() &&
123  m_gasUsed == _cmp.gasUsed() &&
124  m_timestamp == _cmp.timestamp() &&
125  m_extraData == _cmp.extraData();
126  }
127  bool operator!=(BlockHeader const& _cmp) const { return !operator==(_cmp); }
128 
129  void clear();
130  void noteDirty() const { m_hashWithout = m_hash = h256(); }
131  void populateFromParent(BlockHeader const& parent);
132 
133  // TODO: pull out into abstract class Verifier.
134  void verify(Strictness _s = CheckEverything, BlockHeader const& _parent = BlockHeader(), bytesConstRef _block = bytesConstRef()) const;
135  void verify(Strictness _s, bytesConstRef _block) const { verify(_s, BlockHeader(), _block); }
136 
137  h256 hash(IncludeSeal _i = WithSeal) const;
138  void streamRLP(RLPStream& _s, IncludeSeal _i = WithSeal) const;
139 
140  void setParentHash(h256 const& _v) { m_parentHash = _v; noteDirty(); }
141  void setSha3Uncles(h256 const& _v) { m_sha3Uncles = _v; noteDirty(); }
142  void setTimestamp(u256 const& _v) { m_timestamp = _v; noteDirty(); }
143  void setAuthor(Address const& _v) { m_author = _v; noteDirty(); }
144  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(); }
145  void setGasUsed(u256 const& _v) { m_gasUsed = _v; noteDirty(); }
146  void setNumber(u256 const& _v) { m_number = _v; noteDirty(); }
147  void setGasLimit(u256 const& _v) { m_gasLimit = _v; noteDirty(); }
148  void setExtraData(bytes const& _v) { m_extraData = _v; noteDirty(); }
149  void setLogBloom(LogBloom const& _v) { m_logBloom = _v; noteDirty(); }
150  void setDifficulty(u256 const& _v) { m_difficulty = _v; noteDirty(); }
151  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(); }
152  template <class T> void setSeal(T const& _value) { setSeal(0, _value); }
153 
154  h256 const& parentHash() const { return m_parentHash; }
155  h256 const& sha3Uncles() const { return m_sha3Uncles; }
156  u256 const& timestamp() const { return m_timestamp; }
157  Address const& author() const { return m_author; }
158  h256 const& stateRoot() const { return m_stateRoot; }
159  h256 const& transactionsRoot() const { return m_transactionsRoot; }
160  h256 const& receiptsRoot() const { return m_receiptsRoot; }
161  u256 const& gasUsed() const { return m_gasUsed; }
162  u256 const& number() const { return m_number; }
163  u256 const& gasLimit() const { return m_gasLimit; }
164  bytes const& extraData() const { return m_extraData; }
165  LogBloom const& logBloom() const { return m_logBloom; }
166  u256 const& difficulty() const { return m_difficulty; }
167  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; }
168 
169 private:
170  void populate(RLP const& _header);
171  void streamRLPFields(RLPStream& _s) const;
172 
184 
187 
188  std::vector<bytes> m_seal;
189 
190  mutable h256 m_hash;
191  mutable h256 m_hashWithout;
192 };
193 
194 inline std::ostream& operator<<(std::ostream& _out, BlockHeader const& _bi)
195 {
196  _out << _bi.hash(WithoutSeal) << " " << _bi.parentHash() << " " << _bi.sha3Uncles() << " " << _bi.author() << " " << _bi.stateRoot() << " " << _bi.transactionsRoot() << " " <<
197  _bi.receiptsRoot() << " " << _bi.logBloom() << " " << _bi.difficulty() << " " << _bi.number() << " " << _bi.gasLimit() << " " <<
198  _bi.gasUsed() << " " << _bi.timestamp();
199  return _out;
200 }
201 
202 }
203 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
h256 m_hash
(Memoised) SHA3 hash of the block header with seal.
Definition: BlockHeader.h:190
LogBloom const & logBloom() const
Definition: BlockHeader.h:165
u256 const & number() const
Definition: BlockHeader.h:162
void streamRLPFields(RLPStream &_s) const
Definition: BlockHeader.cpp:77
bool operator==(BlockHeader const &_cmp) const
Definition: BlockHeader.h:111
void streamRLP(RLPStream &_s, IncludeSeal _i=WithSeal) const
Definition: BlockHeader.cpp:83
Implements the blockchain database.
Definition: BlockChain.h:105
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
Definition: RLP.h:467
h256 const & stateRoot() const
Definition: BlockHeader.h:158
h256 hash(IncludeSeal _i=WithSeal) const
Definition: BlockHeader.cpp:64
void populateFromParent(BlockHeader const &parent)
Encapsulation of a block header.
Definition: BlockHeader.h:95
#define T(i, x)
std::ostream & operator<<(std::ostream &_out, BlockHeader const &_bi)
Definition: BlockHeader.h:194
h256 const & transactionsRoot() const
Definition: BlockHeader.h:159
void setGasUsed(u256 const &_v)
Definition: BlockHeader.h:145
static RLP extractHeader(bytesConstRef _block)
h256 m_hashWithout
(Memoised) SHA3 hash of the block header without seal.
Definition: BlockHeader.h:191
h256 const & sha3Uncles() const
Definition: BlockHeader.h:155
void noteDirty() const
Definition: BlockHeader.h:130
void setParentHash(h256 const &_v)
Definition: BlockHeader.h:140
h256 const & parentHash() const
Definition: BlockHeader.h:154
void setSha3Uncles(h256 const &_v)
Definition: BlockHeader.h:141
void setTimestamp(u256 const &_v)
Definition: BlockHeader.h:142
T convert(int _flags) const
Definition: RLP.h:378
static h256 headerHashFromBlock(bytes const &_block)
Definition: BlockHeader.h:105
bool operator!=(BlockHeader const &_cmp) const
Definition: BlockHeader.h:127
void setNumber(u256 const &_v)
Definition: BlockHeader.h:146
bytes const & extraData() const
Definition: BlockHeader.h:164
std::vector< byte > bytes
Definition: Common.h:75
vector_ref< byte const > bytesConstRef
Definition: Common.h:77
static const unsigned BasicFields
Definition: BlockHeader.h:99
const u256 Invalid256
Definition: Common.cpp:38
void setLogBloom(LogBloom const &_v)
Definition: BlockHeader.h:149
void verify(Strictness _s=CheckEverything, BlockHeader const &_parent=BlockHeader(), bytesConstRef _block=bytesConstRef()) const
FixedHash< 32 > h256
Definition: FixedHash.h:340
void setGasLimit(u256 const &_v)
Definition: BlockHeader.h:147
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
u256 const & timestamp() const
Definition: BlockHeader.h:156
void setSeal(unsigned _offset, T const &_value)
Definition: BlockHeader.h:151
h256 const & receiptsRoot() const
Definition: BlockHeader.h:160
Address const & author() const
Definition: BlockHeader.h:157
void setExtraData(bytes const &_v)
Definition: BlockHeader.h:148
void setSeal(T const &_value)
Definition: BlockHeader.h:152
u256 const & gasLimit() const
Definition: BlockHeader.h:163
void setRoots(h256 const &_t, h256 const &_r, h256 const &_u, h256 const &_s)
Definition: BlockHeader.h:144
u256 const & gasUsed() const
Definition: BlockHeader.h:161
DEV_SIMPLE_EXCEPTION(InvalidSealEngine)
Class for writing to an RLP bytestream.
Definition: RLP.h:383
std::vector< bytes > m_seal
Additional (RLP-encoded) header fields.
Definition: BlockHeader.h:188
u256 const & difficulty() const
Definition: BlockHeader.h:166
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64
void verify(Strictness _s, bytesConstRef _block) const
Definition: BlockHeader.h:135
void populate(RLP const &_header)
void setDifficulty(u256 const &_v)
Definition: BlockHeader.h:150
BlockHeader(bytes const &_data, BlockDataType _bdt=BlockData, h256 const &_hashWith=h256())
Definition: BlockHeader.h:103
void setAuthor(Address const &_v)
Definition: BlockHeader.h:143
T seal(unsigned _offset=0) const
Definition: BlockHeader.h:167