|
| 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 | +*/ |
| 17 | +/** @file BlockInfo.h |
| 18 | + * @author Gav Wood <i@gavwood.com> |
| 19 | + * @date 2014 |
| 20 | + */ |
| 21 | + |
| 22 | +#pragma once |
| 23 | + |
| 24 | +#include <libdevcore/Common.h> |
| 25 | +#include <libdevcore/RLP.h> |
| 26 | +#include <libdevcore/SHA3.h> |
| 27 | +#include "Exceptions.h" |
| 28 | + |
| 29 | +namespace dev |
| 30 | +{ |
| 31 | +namespace eth |
| 32 | +{ |
| 33 | + |
| 34 | +/// An Ethereum address: 20 bytes. |
| 35 | +using Address = h160; |
| 36 | + |
| 37 | +/// The log bloom's size (2048-bit). |
| 38 | +using LogBloom = h2048; |
| 39 | + |
| 40 | +using Nonce = h64; |
| 41 | + |
| 42 | +using BlockNumber = unsigned; |
| 43 | + |
| 44 | + |
| 45 | +/** @brief Encapsulation of a block header. |
| 46 | + * Class to contain all of a block header's data. It is able to parse a block header and populate |
| 47 | + * from some given RLP block serialisation with the static fromHeader(), through the method |
| 48 | + * populateFromHeader(). This will conduct a minimal level of verification. In this case extra |
| 49 | + * verification can be performed through verifyInternals() and verifyParent(). |
| 50 | + * |
| 51 | + * The object may also be populated from an entire block through the explicit |
| 52 | + * constructor BlockInfo(bytesConstRef) and manually with the populate() method. These will |
| 53 | + * conduct verification of the header against the other information in the block. |
| 54 | + * |
| 55 | + * The object may be populated with a template given a parent BlockInfo object with the |
| 56 | + * populateFromParent() method. The genesis block info may be retrieved with genesis() and the |
| 57 | + * corresponding RLP block created with createGenesisBlock(). |
| 58 | + * |
| 59 | + * The difficulty and gas-limit derivations may be calculated with the calculateDifficulty() |
| 60 | + * and calculateGasLimit() and the object serialised to RLP with streamRLP. To determine the |
| 61 | + * header hash without the nonce (for mining), the method headerHash(WithoutNonce) is provided. |
| 62 | + * |
| 63 | + * The default constructor creates an empty object, which can be tested against with the boolean |
| 64 | + * conversion operator. |
| 65 | + */ |
| 66 | +class BlockHeader |
| 67 | +{ |
| 68 | +public: |
| 69 | + static const unsigned BasicFields = 13; |
| 70 | + |
| 71 | + BlockHeader() = default; |
| 72 | + explicit BlockHeader(bytesConstRef _data); |
| 73 | + explicit BlockHeader(bytes const& _data): BlockHeader(&_data) {} |
| 74 | + |
| 75 | + static RLP extractHeader(bytesConstRef _block); |
| 76 | + |
| 77 | + explicit operator bool() const { return m_timestamp != Invalid256; } |
| 78 | + |
| 79 | + h256 const& boundary() const; |
| 80 | + |
| 81 | + void setNumber(u256 const& _v) { m_number = _v; noteDirty(); } |
| 82 | + void setDifficulty(u256 const& _v) { m_difficulty = _v; noteDirty(); } |
| 83 | + |
| 84 | + u256 const& number() const { return m_number; } |
| 85 | + |
| 86 | + /// sha3 of the header only. |
| 87 | + h256 const& hashWithout() const; |
| 88 | + |
| 89 | + void noteDirty() const { m_hashWithout = m_boundary = h256(); } |
| 90 | + |
| 91 | + h256 const& seedHash() const; |
| 92 | + Nonce const& nonce() const { return m_nonce; } |
| 93 | + |
| 94 | +private: |
| 95 | + void populateFromHeader(RLP const& _header); |
| 96 | + void streamRLPFields(RLPStream& _s) const; |
| 97 | + |
| 98 | + h256 m_parentHash; |
| 99 | + h256 m_sha3Uncles; |
| 100 | + Address m_coinbaseAddress; |
| 101 | + h256 m_stateRoot; |
| 102 | + h256 m_transactionsRoot; |
| 103 | + h256 m_receiptsRoot; |
| 104 | + LogBloom m_logBloom; |
| 105 | + u256 m_number; |
| 106 | + u256 m_gasLimit; |
| 107 | + u256 m_gasUsed; |
| 108 | + u256 m_timestamp = Invalid256; |
| 109 | + bytes m_extraData; |
| 110 | + |
| 111 | + u256 m_difficulty; |
| 112 | + |
| 113 | + mutable h256 m_hashWithout; ///< SHA3 hash of the block header! Not serialised. |
| 114 | + mutable h256 m_boundary; ///< 2^256 / difficulty |
| 115 | + |
| 116 | + Nonce m_nonce; |
| 117 | + mutable h256 m_seedHash; |
| 118 | +}; |
| 119 | + |
| 120 | +} |
| 121 | +} |
0 commit comments