Browse Source

Flatten BlockHeader

cl-refactor
Paweł Bylica 7 years ago
parent
commit
330bca0449
No known key found for this signature in database GPG Key ID: 7A0C037434FE77EF
  1. 42
      libethcore/BlockHeader.cpp
  2. 48
      libethcore/BlockHeader.h
  3. 1
      libethcore/CMakeLists.txt
  4. 2
      libethcore/EthashAux.cpp
  5. 2
      libethcore/EthashAux.h
  6. 2
      libethcore/Farm.h

42
libethcore/BlockInfo.cpp → libethcore/BlockHeader.cpp

@ -19,58 +19,32 @@
* @date 2014 * @date 2014
*/ */
#include "BlockHeader.h"
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
#include <libdevcore/Log.h> #include <libdevcore/Log.h>
#include <libdevcore/RLP.h> #include <libdevcore/RLP.h>
#include "EthashAux.h" #include "EthashAux.h"
#include "Exceptions.h"
#include "BlockInfo.h"
using namespace std; using namespace std;
using namespace dev; using namespace dev;
using namespace dev::eth; using namespace dev::eth;
namespace
{
h256 const EmptyTrie = sha3(rlp(""));
}
BlockInfo::BlockInfo(): m_timestamp(Invalid256) BlockHeader::BlockHeader(bytesConstRef _block, Strictness _s, h256 const& _hashWith)
{
}
BlockInfo::BlockInfo(bytesConstRef _block, Strictness _s, h256 const& _hashWith)
{ {
RLP header = extractHeader(_block); RLP header = extractHeader(_block);
m_hash = _hashWith ? _hashWith : sha3(header.data()); m_hash = _hashWith ? _hashWith : sha3(header.data());
populateFromHeader(header, _s); populateFromHeader(header, _s);
} }
void BlockInfo::clear() h256 const& BlockHeader::boundary() const
{
m_parentHash = h256();
m_sha3Uncles = EmptyListSHA3;
m_coinbaseAddress = Address();
m_stateRoot = EmptyTrie;
m_transactionsRoot = EmptyTrie;
m_receiptsRoot = EmptyTrie;
m_logBloom = LogBloom();
m_difficulty = 0;
m_number = 0;
m_gasLimit = 0;
m_gasUsed = 0;
m_timestamp = 0;
m_extraData.clear();
noteDirty();
}
h256 const& BlockInfo::boundary() const
{ {
if (!m_boundary && m_difficulty) if (!m_boundary && m_difficulty)
m_boundary = (h256)(u256)((bigint(1) << 256) / m_difficulty); m_boundary = (h256)(u256)((bigint(1) << 256) / m_difficulty);
return m_boundary; return m_boundary;
} }
h256 const& BlockInfo::hashWithout() const h256 const& BlockHeader::hashWithout() const
{ {
if (!m_hashWithout) if (!m_hashWithout)
{ {
@ -81,13 +55,13 @@ h256 const& BlockInfo::hashWithout() const
return m_hashWithout; return m_hashWithout;
} }
void BlockInfo::streamRLPFields(RLPStream& _s) const void BlockHeader::streamRLPFields(RLPStream& _s) const
{ {
_s << m_parentHash << m_sha3Uncles << m_coinbaseAddress << m_stateRoot << m_transactionsRoot << m_receiptsRoot << m_logBloom _s << m_parentHash << m_sha3Uncles << m_coinbaseAddress << m_stateRoot << m_transactionsRoot << m_receiptsRoot << m_logBloom
<< m_difficulty << m_number << m_gasLimit << m_gasUsed << m_timestamp << m_extraData; << m_difficulty << m_number << m_gasLimit << m_gasUsed << m_timestamp << m_extraData;
} }
RLP BlockInfo::extractHeader(bytesConstRef _block) RLP BlockHeader::extractHeader(bytesConstRef _block)
{ {
RLP root(_block); RLP root(_block);
if (!root.isList()) if (!root.isList())
@ -102,7 +76,7 @@ RLP BlockInfo::extractHeader(bytesConstRef _block)
return header; return header;
} }
void BlockInfo::populateFromHeader(RLP const& _header, Strictness _s) void BlockHeader::populateFromHeader(RLP const& _header, Strictness _s)
{ {
int field = 0; int field = 0;
try try

48
libethcore/BlockInfo.h → libethcore/BlockHeader.h

@ -69,20 +69,20 @@ enum Strictness
* The default constructor creates an empty object, which can be tested against with the boolean * The default constructor creates an empty object, which can be tested against with the boolean
* conversion operator. * conversion operator.
*/ */
class BlockInfo class BlockHeader
{ {
public: public:
static const unsigned BasicFields = 13; static const unsigned BasicFields = 13;
BlockInfo(); BlockHeader() = default;
explicit BlockInfo(bytesConstRef _data, Strictness _s = CheckEverything, h256 const& _hashWith = h256()); explicit BlockHeader(bytesConstRef _data, Strictness _s = CheckEverything, h256 const& _hashWith = h256());
explicit BlockInfo(bytes const& _data, Strictness _s = CheckEverything, h256 const& _hashWith = h256()): BlockInfo(&_data, _s, _hashWith) {} explicit BlockHeader(bytes const& _data, Strictness _s = CheckEverything, h256 const& _hashWith = h256()): BlockHeader(&_data, _s, _hashWith) {}
static RLP extractHeader(bytesConstRef _block); static RLP extractHeader(bytesConstRef _block);
explicit operator bool() const { return m_timestamp != Invalid256; } explicit operator bool() const { return m_timestamp != Invalid256; }
bool operator==(BlockInfo const& _cmp) const bool operator==(BlockHeader const& _cmp) const
{ {
return m_parentHash == _cmp.parentHash() && return m_parentHash == _cmp.parentHash() &&
m_sha3Uncles == _cmp.sha3Uncles() && m_sha3Uncles == _cmp.sha3Uncles() &&
@ -98,7 +98,7 @@ public:
m_timestamp == _cmp.timestamp() && m_timestamp == _cmp.timestamp() &&
m_extraData == _cmp.extraData(); m_extraData == _cmp.extraData();
} }
bool operator!=(BlockInfo const& _cmp) const { return !operator==(_cmp); } bool operator!=(BlockHeader const& _cmp) const { return !operator==(_cmp); }
h256 const& boundary() const; h256 const& boundary() const;
@ -124,10 +124,12 @@ public:
/// sha3 of the header only. /// sha3 of the header only.
h256 const& hashWithout() const; h256 const& hashWithout() const;
void clear();
void noteDirty() const { m_hashWithout = m_boundary = m_hash = h256(); } void noteDirty() const { m_hashWithout = m_boundary = m_hash = h256(); }
protected: h256 const& seedHash() const;
Nonce const& nonce() const { return m_nonce; }
private:
void populateFromHeader(RLP const& _header, Strictness _s = IgnoreSeal); void populateFromHeader(RLP const& _header, Strictness _s = IgnoreSeal);
void streamRLPFields(RLPStream& _s) const; void streamRLPFields(RLPStream& _s) const;
@ -148,42 +150,12 @@ protected:
u256 m_difficulty; // TODO: pull out into BlockHeader u256 m_difficulty; // TODO: pull out into BlockHeader
private:
mutable h256 m_hashWithout; ///< SHA3 hash of the block header! Not serialised. mutable h256 m_hashWithout; ///< SHA3 hash of the block header! Not serialised.
mutable h256 m_boundary; ///< 2^256 / difficulty mutable h256 m_boundary; ///< 2^256 / difficulty
};
class BlockHeaderRaw: public BlockInfo
{
public:
h256 const& seedHash() const;
Nonce const& nonce() const { return m_nonce; }
protected:
BlockHeaderRaw() = default;
BlockHeaderRaw(BlockInfo const& _bi): BlockInfo(_bi) {}
void clear() { m_mixHash = h256(); m_nonce = Nonce(); }
private:
Nonce m_nonce; Nonce m_nonce;
h256 m_mixHash;
mutable h256 m_seedHash; mutable h256 m_seedHash;
}; };
class BlockHeaderPolished: public BlockHeaderRaw
{
public:
BlockHeaderPolished() {}
BlockHeaderPolished(BlockInfo const& _bi): BlockHeaderRaw(_bi) {}
void clear() { BlockInfo::clear(); BlockHeaderRaw::clear(); BlockHeaderRaw::noteDirty(); }
void noteDirty() const { BlockInfo::noteDirty(); BlockHeaderRaw::noteDirty(); }
};
using BlockHeader = BlockHeaderPolished;
} }
} }

1
libethcore/CMakeLists.txt

@ -1,6 +1,7 @@
file(GLOB SOURCES "*.cpp") file(GLOB SOURCES "*.cpp")
file(GLOB HEADERS "*.h") file(GLOB HEADERS "*.h")
include_directories(BEFORE ..) include_directories(BEFORE ..)
add_library(ethcore ${SOURCES} ${HEADERS}) add_library(ethcore ${SOURCES} ${HEADERS})

2
libethcore/EthashAux.cpp

@ -27,7 +27,7 @@ using namespace chrono;
using namespace dev; using namespace dev;
using namespace eth; using namespace eth;
h256 const& BlockHeaderRaw::seedHash() const h256 const& BlockHeader::seedHash() const
{ {
if (!m_seedHash) if (!m_seedHash)
m_seedHash = EthashAux::seedHash((unsigned)m_number); m_seedHash = EthashAux::seedHash((unsigned)m_number);

2
libethcore/EthashAux.h

@ -25,7 +25,7 @@
#include <libethash/ethash.h> #include <libethash/ethash.h>
#include <libdevcore/Log.h> #include <libdevcore/Log.h>
#include <libdevcore/Worker.h> #include <libdevcore/Worker.h>
#include "BlockInfo.h" #include "BlockHeader.h"
namespace dev namespace dev
{ {

2
libethcore/Farm.h

@ -27,7 +27,7 @@
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
#include <libdevcore/Worker.h> #include <libdevcore/Worker.h>
#include <libethcore/Miner.h> #include <libethcore/Miner.h>
#include <libethcore/BlockInfo.h> #include <libethcore/BlockHeader.h>
namespace dev namespace dev
{ {

Loading…
Cancel
Save