Fabcoin Core  0.16.2
P2P Digital Currency
Ethash.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 */
24 #pragma once
25 
26 #include <libethcore/SealEngine.h>
28 #include "EthashProofOfWork.h"
29 
30 namespace dev
31 {
32 
33 namespace eth
34 {
35 
36 class Ethash: public SealEngineBase
37 {
38 public:
39  Ethash();
40 
41  std::string name() const override { return "Ethash"; }
42  unsigned revision() const override { return 1; }
43  unsigned sealFields() const override { return 2; }
44  bytes sealRLP() const override { return rlp(h256()) + rlp(Nonce()); }
45 
46  StringHashMap jsInfo(BlockHeader const& _bi) const override;
47  void verify(Strictness _s, BlockHeader const& _bi, BlockHeader const& _parent, bytesConstRef _block) const override;
48  void verifyTransaction(ImportRequirements::value _ir, TransactionBase const& _t, BlockHeader const& _bi) const override;
49  void populateFromParent(BlockHeader& _bi, BlockHeader const& _parent) const override;
50 
51  strings sealers() const override;
52  std::string sealer() const override { return m_sealer; }
53  void setSealer(std::string const& _sealer) override { m_sealer = _sealer; }
54  void cancelGeneration() override { m_farm.stop(); }
55  void generateSeal(BlockHeader const& _bi) override;
56  bool shouldSeal(Interface* _i) override;
57 
59 
60  enum { MixHashField = 0, NonceField = 1 };
61  static h256 seedHash(BlockHeader const& _bi);
62  static Nonce nonce(BlockHeader const& _bi) { return _bi.seal<Nonce>(NonceField); }
63  static h256 mixHash(BlockHeader const& _bi) { return _bi.seal<h256>(MixHashField); }
64  static h256 boundary(BlockHeader const& _bi) { auto d = _bi.difficulty(); return d ? (h256)u256((bigint(1) << 256) / d) : h256(); }
65  static BlockHeader& setNonce(BlockHeader& _bi, Nonce _v) { _bi.setSeal(NonceField, _v); return _bi; }
66  static BlockHeader& setMixHash(BlockHeader& _bi, h256 const& _v) { _bi.setSeal(MixHashField, _v); return _bi; }
67 
68  u256 calculateDifficulty(BlockHeader const& _bi, BlockHeader const& _parent) const;
69  u256 childGasLimit(BlockHeader const& _bi, u256 const& _gasFloorTarget = Invalid256) const;
70 
71  void manuallySetWork(BlockHeader const& _work) { m_sealing = _work; }
72  void manuallySubmitWork(h256 const& _mixHash, Nonce _nonce);
73 
74  static void ensurePrecomputed(unsigned _number);
75  static void init();
76 
77 private:
78  bool verifySeal(BlockHeader const& _bi) const;
79  bool quickVerifySeal(BlockHeader const& _bi) const;
80 
82  std::string m_sealer = "cpu";
84 };
85 
86 }
87 }
bool verifySeal(BlockHeader const &_bi) const
Definition: Ethash.cpp:233
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
static BlockHeader & setNonce(BlockHeader &_bi, Nonce _v)
Definition: Ethash.h:65
strings sealers() const override
Definition: Ethash.cpp:61
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
Definition: RLP.h:467
unsigned sealFields() const override
Definition: Ethash.h:43
StringHashMap jsInfo(BlockHeader const &_bi) const override
Definition: Ethash.cpp:71
void populateFromParent(BlockHeader &_bi, BlockHeader const &_parent) const override
Don&#39;t forget to call Super::populateFromParent when subclassing & overriding.
Definition: Ethash.cpp:209
Encapsulation of a block header.
Definition: BlockHeader.h:95
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
Definition: Common.h:121
unsigned revision() const override
Definition: Ethash.h:42
std::vector< std::string > strings
Definition: Common.h:147
static h256 seedHash(BlockHeader const &_bi)
Definition: Ethash.cpp:66
A collective of Miners.
Definition: GenericFarm.h:45
std::string name() const override
Definition: Ethash.h:41
void manuallySubmitWork(h256 const &_mixHash, Nonce _nonce)
Definition: Ethash.cpp:177
void manuallySetWork(BlockHeader const &_work)
Definition: Ethash.h:71
static Nonce nonce(BlockHeader const &_bi)
Definition: Ethash.h:62
eth::GenericFarm< EthashProofOfWork > m_farm
Definition: Ethash.h:81
void verifyTransaction(ImportRequirements::value _ir, TransactionBase const &_t, BlockHeader const &_bi) const override
Additional verification for transactions in blocks.
Definition: Ethash.cpp:147
static h256 boundary(BlockHeader const &_bi)
Definition: Ethash.h:64
std::string sealer() const override
Definition: Ethash.h:52
static h256 mixHash(BlockHeader const &_bi)
Definition: Ethash.h:63
std::vector< byte > bytes
Definition: Common.h:75
bool quickVerifySeal(BlockHeader const &_bi) const
Definition: Ethash.cpp:216
const u256 Invalid256
Definition: Common.cpp:38
u256 childGasLimit(BlockHeader const &_bi, u256 const &_gasFloorTarget=Invalid256) const
Definition: Ethash.cpp:166
FixedHash< 32 > h256
Definition: FixedHash.h:340
bytes sealRLP() const override
Definition: Ethash.h:44
bool shouldSeal(Interface *_i) override
Definition: Ethash.cpp:275
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
static void init()
Definition: Ethash.cpp:33
u256 calculateDifficulty(BlockHeader const &_bi, BlockHeader const &_parent) const
Definition: Ethash.cpp:182
static void ensurePrecomputed(unsigned _number)
Definition: Ethash.cpp:280
std::unordered_map< std::string, std::string > StringHashMap
Definition: Common.h:143
Main API hub for interfacing with Ethereum.
Definition: Interface.h:67
void cancelGeneration() override
Definition: Ethash.h:54
void generateSeal(BlockHeader const &_bi) override
Definition: Ethash.cpp:264
BlockHeader m_sealing
Definition: Ethash.h:83
void setSeal(unsigned _offset, T const &_value)
Definition: BlockHeader.h:151
void setSealer(std::string const &_sealer) override
Definition: Ethash.h:53
void verify(Strictness _s, BlockHeader const &_bi, BlockHeader const &_parent, bytesConstRef _block) const override
Don&#39;t forget to call Super::verify when subclassing & overriding.
Definition: Ethash.cpp:76
#define d(i)
Definition: sha.cpp:732
eth::GenericFarm< EthashProofOfWork > & farm()
Definition: Ethash.h:58
u256 const & difficulty() const
Definition: BlockHeader.h:166
static BlockHeader & setMixHash(BlockHeader &_bi, h256 const &_v)
Definition: Ethash.h:66
h64 Nonce
Definition: Common.h:70
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Definition: Transaction.h:50
std::string m_sealer
Definition: Ethash.h:82
T seal(unsigned _offset=0) const
Definition: BlockHeader.h:167