Skip to content
This repository was archived by the owner on Apr 24, 2022. It is now read-only.

Commit 2c8f46f

Browse files
committedJul 20, 2017
Simplify Ethash
1 parent 1c02015 commit 2c8f46f

File tree

3 files changed

+44
-48
lines changed

3 files changed

+44
-48
lines changed
 

‎libethcore/BlockHeader.h

-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ class BlockHeader
8686

8787
void noteDirty() const { m_hashWithout = m_boundary = h256(); }
8888

89-
h256 const& seedHash() const;
9089
uint64_t nonce() const { return m_nonce; }
9190

9291
private:

‎libethcore/EthashAux.cpp

+24-26
Original file line numberDiff line numberDiff line change
@@ -27,47 +27,42 @@ using namespace chrono;
2727
using namespace dev;
2828
using namespace eth;
2929

30-
h256 const& BlockHeader::seedHash() const
31-
{
32-
if (!m_seedHash)
33-
m_seedHash = EthashAux::seedHash((unsigned)m_number);
34-
return m_seedHash;
35-
}
36-
37-
EthashAux* EthashAux::get()
30+
EthashAux& EthashAux::get()
3831
{
3932
static EthashAux instance;
40-
return &instance;
33+
return instance;
4134
}
4235

4336
h256 EthashAux::seedHash(unsigned _number)
4437
{
4538
unsigned epoch = _number / ETHASH_EPOCH_LENGTH;
46-
Guard l(get()->x_epochs);
47-
if (epoch >= get()->m_seedHashes.size())
39+
EthashAux& ethash = EthashAux::get();
40+
Guard l(ethash.x_epochs);
41+
if (epoch >= ethash.m_seedHashes.size())
4842
{
4943
h256 ret;
5044
unsigned n = 0;
51-
if (!get()->m_seedHashes.empty())
45+
if (!ethash.m_seedHashes.empty())
5246
{
53-
ret = get()->m_seedHashes.back();
54-
n = get()->m_seedHashes.size() - 1;
47+
ret = ethash.m_seedHashes.back();
48+
n = ethash.m_seedHashes.size() - 1;
5549
}
56-
get()->m_seedHashes.resize(epoch + 1);
50+
ethash.m_seedHashes.resize(epoch + 1);
5751
for (; n <= epoch; ++n, ret = sha3(ret))
58-
get()->m_seedHashes[n] = ret;
52+
ethash.m_seedHashes[n] = ret;
5953
}
60-
return get()->m_seedHashes[epoch];
54+
return ethash.m_seedHashes[epoch];
6155
}
6256

6357
uint64_t EthashAux::number(h256 const& _seedHash)
6458
{
65-
Guard l(get()->x_epochs);
59+
EthashAux& ethash = EthashAux::get();
60+
Guard l(ethash.x_epochs);
6661
unsigned epoch = 0;
67-
auto epochIter = get()->m_epochs.find(_seedHash);
68-
if (epochIter == get()->m_epochs.end())
62+
auto epochIter = ethash.m_epochs.find(_seedHash);
63+
if (epochIter == ethash.m_epochs.end())
6964
{
70-
for (h256 h; h != _seedHash && epoch < 2048; ++epoch, h = sha3(h), get()->m_epochs[h] = epoch) {}
65+
for (h256 h; h != _seedHash && epoch < 2048; ++epoch, h = sha3(h), ethash.m_epochs[h] = epoch) {}
7166
if (epoch == 2048)
7267
{
7368
std::ostringstream error;
@@ -82,10 +77,13 @@ uint64_t EthashAux::number(h256 const& _seedHash)
8277

8378
EthashAux::LightType EthashAux::light(h256 const& _seedHash)
8479
{
85-
Guard l(get()->x_lights);
86-
if (get()->m_lights.count(_seedHash))
87-
return get()->m_lights.at(_seedHash);
88-
return (get()->m_lights[_seedHash] = make_shared<LightAllocation>(_seedHash));
80+
// TODO: Use epoch number instead of seed hash?
81+
82+
EthashAux& ethash = EthashAux::get();
83+
Guard l(ethash.x_lights);
84+
if (ethash.m_lights.count(_seedHash))
85+
return ethash.m_lights.at(_seedHash);
86+
return (ethash.m_lights[_seedHash] = make_shared<LightAllocation>(_seedHash));
8987
}
9088

9189
EthashAux::LightAllocation::LightAllocation(h256 const& _seedHash)
@@ -119,7 +117,7 @@ Result EthashAux::eval(h256 const& _seedHash, h256 const& _headerHash, uint64_t
119117
{
120118
try
121119
{
122-
return EthashAux::get()->light(_seedHash)->compute(_headerHash, _nonce);
120+
return get().light(_seedHash)->compute(_headerHash, _nonce);
123121
}
124122
catch(...)
125123
{

‎libethcore/EthashAux.h

+20-21
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,9 @@ struct Result
4747
h256 mixHash;
4848
};
4949

50-
struct WorkPackage
51-
{
52-
WorkPackage() = default;
53-
WorkPackage(BlockHeader const& _bh) :
54-
boundary(_bh.boundary()),
55-
header(_bh.hashWithout()),
56-
seed(_bh.seedHash())
57-
{ }
58-
void reset() { header = h256(); }
59-
operator bool() const { return header != h256(); }
60-
61-
h256 boundary;
62-
h256 header; ///< When h256() means "pause until notified a new work package is available".
63-
h256 seed;
64-
65-
uint64_t startNonce = 0;
66-
int exSizeBits = -1;
67-
};
68-
6950
class EthashAux
7051
{
7152
public:
72-
static EthashAux* get();
73-
7453
struct LightAllocation
7554
{
7655
LightAllocation(h256 const& _seedHash);
@@ -92,6 +71,7 @@ class EthashAux
9271

9372
private:
9473
EthashAux() = default;
74+
static EthashAux& get();
9575

9676
Mutex x_lights;
9777
std::unordered_map<h256, LightType> m_lights;
@@ -101,5 +81,24 @@ class EthashAux
10181
h256s m_seedHashes;
10282
};
10383

84+
struct WorkPackage
85+
{
86+
WorkPackage() = default;
87+
WorkPackage(BlockHeader const& _bh) :
88+
boundary(_bh.boundary()),
89+
header(_bh.hashWithout()),
90+
seed(EthashAux::seedHash(static_cast<unsigned>(_bh.number())))
91+
{ }
92+
void reset() { header = h256(); }
93+
operator bool() const { return header != h256(); }
94+
95+
h256 boundary;
96+
h256 header; ///< When h256() means "pause until notified a new work package is available".
97+
h256 seed;
98+
99+
uint64_t startNonce = 0;
100+
int exSizeBits = -1;
101+
};
102+
104103
}
105104
}

0 commit comments

Comments
 (0)
This repository has been archived.