-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ethash.h
91 lines (71 loc) · 3.22 KB
/
Ethash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file Ethash.h
* @author Gav Wood <[email protected]>
* @date 2014
*
* A proof of work algorithm.
*/
#pragma once
#include <libethcore/SealEngine.h>
#include <libethereum/GenericFarm.h>
#include "EthashProofOfWork.h"
namespace dev
{
namespace eth
{
class Ethash: public SealEngineBase
{
public:
Ethash();
~Ethash();
std::string name() const override { return "Ethash"; }
unsigned revision() const override { return 1; }
unsigned sealFields() const override { return 2; }
bytes sealRLP() const override { return rlp(h256()) + rlp(Nonce()); }
StringHashMap jsInfo(BlockHeader const& _bi) const override;
void verify(Strictness _s, BlockHeader const& _bi, BlockHeader const& _parent, bytesConstRef _block) const override;
void verifyTransaction(ImportRequirements::value _ir, TransactionBase const& _t, BlockHeader const& _header, u256 const& _startGasUsed) const override;
void populateFromParent(BlockHeader& _bi, BlockHeader const& _parent) const override;
strings sealers() const override;
std::string sealer() const override { return m_sealer; }
void setSealer(std::string const& _sealer) override { m_sealer = _sealer; }
void cancelGeneration() override { m_farm.stop(); }
void generateSeal(BlockHeader const& _bi) override;
bool shouldSeal(Interface* _i) override;
eth::GenericFarm<EthashProofOfWork>& farm() { return m_farm; }
enum { MixHashField = 0, NonceField = 1 };
static h256 seedHash(BlockHeader const& _bi);
static Nonce nonce(BlockHeader const& _bi) { return _bi.seal<Nonce>(NonceField); }
static h256 mixHash(BlockHeader const& _bi) { return _bi.seal<h256>(MixHashField); }
static h256 boundary(BlockHeader const& _bi) { auto d = _bi.difficulty(); return d ? (h256)u256((bigint(1) << 256) / d) : h256(); }
static BlockHeader& setNonce(BlockHeader& _bi, Nonce _v) { _bi.setSeal(NonceField, _v); return _bi; }
static BlockHeader& setMixHash(BlockHeader& _bi, h256 const& _v) { _bi.setSeal(MixHashField, _v); return _bi; }
u256 calculateDifficulty(BlockHeader const& _bi, BlockHeader const& _parent) const;
u256 childGasLimit(BlockHeader const& _bi, u256 const& _gasFloorTarget = Invalid256) const;
void manuallySetWork(BlockHeader const& _work) { m_sealing = _work; }
void manuallySubmitWork(h256 const& _mixHash, Nonce _nonce);
static void ensurePrecomputed(unsigned _number);
static void init();
private:
bool verifySeal(BlockHeader const& _bi) const;
bool quickVerifySeal(BlockHeader const& _bi) const;
eth::GenericFarm<EthashProofOfWork> m_farm;
std::string m_sealer = "cpu";
BlockHeader m_sealing;
/// A mutex covering m_sealing
Mutex m_submitLock;
};
}
}