Skip to content

Commit 24c5633

Browse files
authoredMay 31, 2017
Merge pull request ethereum-mining#35 from ethereum-mining/header
BlockHeader cleanup
2 parents c80b3a2 + 9ac93c8 commit 24c5633

File tree

7 files changed

+136
-296
lines changed

7 files changed

+136
-296
lines changed
 

‎libethcore/BlockInfo.cpp ‎libethcore/BlockHeader.cpp

+11-38
Original file line numberDiff line numberDiff line change
@@ -19,58 +19,31 @@
1919
* @date 2014
2020
*/
2121

22+
#include "BlockHeader.h"
2223
#include <libdevcore/Common.h>
2324
#include <libdevcore/Log.h>
2425
#include <libdevcore/RLP.h>
2526
#include "EthashAux.h"
26-
#include "Exceptions.h"
27-
#include "BlockInfo.h"
27+
2828
using namespace std;
2929
using namespace dev;
3030
using namespace dev::eth;
3131

32-
namespace
33-
{
34-
h256 const EmptyTrie = sha3(rlp(""));
35-
}
36-
37-
BlockInfo::BlockInfo(): m_timestamp(Invalid256)
38-
{
39-
}
40-
41-
BlockInfo::BlockInfo(bytesConstRef _block, Strictness _s, h256 const& _hashWith, BlockDataType _bdt)
42-
{
43-
RLP header = _bdt == BlockData ? extractHeader(_block) : RLP(_block);
44-
m_hash = _hashWith ? _hashWith : sha3(header.data());
45-
populateFromHeader(header, _s);
46-
}
4732

48-
void BlockInfo::clear()
33+
BlockHeader::BlockHeader(bytesConstRef _block)
4934
{
50-
m_parentHash = h256();
51-
m_sha3Uncles = EmptyListSHA3;
52-
m_coinbaseAddress = Address();
53-
m_stateRoot = EmptyTrie;
54-
m_transactionsRoot = EmptyTrie;
55-
m_receiptsRoot = EmptyTrie;
56-
m_logBloom = LogBloom();
57-
m_difficulty = 0;
58-
m_number = 0;
59-
m_gasLimit = 0;
60-
m_gasUsed = 0;
61-
m_timestamp = 0;
62-
m_extraData.clear();
63-
noteDirty();
35+
RLP header = extractHeader(_block);
36+
populateFromHeader(header);
6437
}
6538

66-
h256 const& BlockInfo::boundary() const
39+
h256 const& BlockHeader::boundary() const
6740
{
6841
if (!m_boundary && m_difficulty)
6942
m_boundary = (h256)(u256)((bigint(1) << 256) / m_difficulty);
7043
return m_boundary;
7144
}
7245

73-
h256 const& BlockInfo::hashWithout() const
46+
h256 const& BlockHeader::hashWithout() const
7447
{
7548
if (!m_hashWithout)
7649
{
@@ -81,13 +54,13 @@ h256 const& BlockInfo::hashWithout() const
8154
return m_hashWithout;
8255
}
8356

84-
void BlockInfo::streamRLPFields(RLPStream& _s) const
57+
void BlockHeader::streamRLPFields(RLPStream& _s) const
8558
{
8659
_s << m_parentHash << m_sha3Uncles << m_coinbaseAddress << m_stateRoot << m_transactionsRoot << m_receiptsRoot << m_logBloom
8760
<< m_difficulty << m_number << m_gasLimit << m_gasUsed << m_timestamp << m_extraData;
8861
}
8962

90-
RLP BlockInfo::extractHeader(bytesConstRef _block)
63+
RLP BlockHeader::extractHeader(bytesConstRef _block)
9164
{
9265
RLP root(_block);
9366
if (!root.isList())
@@ -102,7 +75,7 @@ RLP BlockInfo::extractHeader(bytesConstRef _block)
10275
return header;
10376
}
10477

105-
void BlockInfo::populateFromHeader(RLP const& _header, Strictness _s)
78+
void BlockHeader::populateFromHeader(RLP const& _header)
10679
{
10780
int field = 0;
10881
try
@@ -130,6 +103,6 @@ void BlockInfo::populateFromHeader(RLP const& _header, Strictness _s)
130103
if (m_number > ~(unsigned)0)
131104
BOOST_THROW_EXCEPTION(InvalidNumber());
132105

133-
if (_s != CheckNothing && m_gasUsed > m_gasLimit)
106+
if (m_gasUsed > m_gasLimit)
134107
BOOST_THROW_EXCEPTION(TooMuchGasUsed() << RequirementError(bigint(m_gasLimit), bigint(m_gasUsed)));
135108
}

‎libethcore/BlockHeader.h

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}

‎libethcore/BlockInfo.h

-255
This file was deleted.

‎libethcore/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
file(GLOB SOURCES "*.cpp")
22
file(GLOB HEADERS "*.h")
33

4+
45
include_directories(BEFORE ..)
56

67
add_library(ethcore ${SOURCES} ${HEADERS})

‎libethcore/EthashAux.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ using namespace chrono;
2727
using namespace dev;
2828
using namespace eth;
2929

30-
h256 const& BlockHeaderRaw::seedHash() const
30+
h256 const& BlockHeader::seedHash() const
3131
{
3232
if (!m_seedHash)
3333
m_seedHash = EthashAux::seedHash((unsigned)m_number);

‎libethcore/EthashAux.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include <libethash/ethash.h>
2626
#include <libdevcore/Log.h>
2727
#include <libdevcore/Worker.h>
28-
#include "BlockInfo.h"
28+
#include "BlockHeader.h"
2929

3030
namespace dev
3131
{

‎libethcore/Farm.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include <libdevcore/Common.h>
2828
#include <libdevcore/Worker.h>
2929
#include <libethcore/Miner.h>
30-
#include <libethcore/BlockInfo.h>
30+
#include <libethcore/BlockHeader.h>
3131

3232
namespace dev
3333
{

0 commit comments

Comments
 (0)
Please sign in to comment.