Browse Source

AlethZero fixes.

cl-refactor
Gav Wood 10 years ago
parent
commit
45bacf8566
  1. 9
      libethcore/BlockInfo.cpp
  2. 1
      libethcore/BlockInfo.h
  3. 29
      libethcore/EthashAux.cpp
  4. 4
      libethcore/EthashAux.h

9
libethcore/BlockInfo.cpp

@ -23,6 +23,7 @@
#include <libdevcore/RLP.h> #include <libdevcore/RLP.h>
#include <libdevcrypto/TrieDB.h> #include <libdevcrypto/TrieDB.h>
#include <libethcore/Common.h> #include <libethcore/Common.h>
#include "EthashAux.h"
#include "ProofOfWork.h" #include "ProofOfWork.h"
#include "Exceptions.h" #include "Exceptions.h"
#include "Params.h" #include "Params.h"
@ -63,8 +64,7 @@ void BlockInfo::clear()
h256 const& BlockInfo::seedHash() const h256 const& BlockInfo::seedHash() const
{ {
if (!m_seedHash) if (!m_seedHash)
for (u256 n = number; n >= c_epochDuration; n -= c_epochDuration) m_seedHash = EthashAux::seedHash((unsigned)number);
m_seedHash = sha3(m_seedHash);
return m_seedHash; return m_seedHash;
} }
@ -145,9 +145,14 @@ void BlockInfo::populateFromHeader(RLP const& _header, Strictness _s, h256 const
throw; throw;
} }
if (number > ~(unsigned)0)
throw InvalidNumber();
// check it hashes according to proof of work or that it's the genesis block. // check it hashes according to proof of work or that it's the genesis block.
if (_s == CheckEverything && parentHash && !ProofOfWork::verify(*this)) if (_s == CheckEverything && parentHash && !ProofOfWork::verify(*this))
BOOST_THROW_EXCEPTION(InvalidBlockNonce() << errinfo_hash256(headerHash(WithoutNonce)) << errinfo_nonce(nonce) << errinfo_difficulty(difficulty)); BOOST_THROW_EXCEPTION(InvalidBlockNonce() << errinfo_hash256(headerHash(WithoutNonce)) << errinfo_nonce(nonce) << errinfo_difficulty(difficulty));
else if (_s == QuickNonce && parentHash && !ProofOfWork::preVerify(*this))
BOOST_THROW_EXCEPTION(InvalidBlockNonce() << errinfo_hash256(headerHash(WithoutNonce)) << errinfo_nonce(nonce) << errinfo_difficulty(difficulty));
if (_s != CheckNothing) if (_s != CheckNothing)
{ {

1
libethcore/BlockInfo.h

@ -39,6 +39,7 @@ enum IncludeNonce
enum Strictness enum Strictness
{ {
CheckEverything, CheckEverything,
QuickNonce,
IgnoreNonce, IgnoreNonce,
CheckNothing CheckNothing
}; };

29
libethcore/EthashAux.cpp

@ -63,24 +63,47 @@ ethash_params EthashAux::params(unsigned _n)
return p; return p;
} }
h256 EthashAux::seedHash(unsigned _number)
{
unsigned epoch = _number / ETHASH_EPOCH_LENGTH;
RecursiveGuard l(get()->x_this);
if (_number < get()->m_seedHashes.size())
return get()->m_seedHashes[_number];
h256 ret;
unsigned n = 0;
if (!get()->m_seedHashes.empty())
{
ret = get()->m_seedHashes.back();
n = get()->m_seedHashes.size() - 1;
}
cdebug << "Searching for seedHash of epoch " << epoch;
for (; n < epoch; ++n, ret = sha3(ret))
cdebug << "Epoch" << n << "is" << ret.abridged();
return ret;
}
ethash_params EthashAux::params(h256 const& _seedHash) ethash_params EthashAux::params(h256 const& _seedHash)
{ {
RecursiveGuard l(get()->x_this); RecursiveGuard l(get()->x_this);
unsigned epoch = 0; unsigned epoch = 0;
try try
{ {
epoch = get()->m_seedHashes.at(_seedHash); epoch = get()->m_epochs.at(_seedHash);
} }
catch (...) catch (...)
{ {
for (h256 h; h != _seedHash && epoch < 2048; ++epoch, h = h256(h)) {} cdebug << "Searching for seedHash " << _seedHash.abridged();
for (h256 h; h != _seedHash && epoch < 2048; ++epoch, h = sha3(h))
{
cdebug << "Epoch" << epoch << "is" << h.abridged();
}
if (epoch == 2048) if (epoch == 2048)
{ {
std::ostringstream error; std::ostringstream error;
error << "apparent block number for " << _seedHash.abridged() << " is too high; max is " << (ETHASH_EPOCH_LENGTH * 2048); error << "apparent block number for " << _seedHash.abridged() << " is too high; max is " << (ETHASH_EPOCH_LENGTH * 2048);
throw std::invalid_argument(error.str()); throw std::invalid_argument(error.str());
} }
get()->m_seedHashes[_seedHash] = epoch; get()->m_epochs[_seedHash] = epoch;
} }
return params(epoch * ETHASH_EPOCH_LENGTH); return params(epoch * ETHASH_EPOCH_LENGTH);
} }

4
libethcore/EthashAux.h

@ -36,6 +36,7 @@ public:
using LightType = void const*; using LightType = void const*;
using FullType = void const*; using FullType = void const*;
static h256 seedHash(unsigned _number);
static ethash_params params(BlockInfo const& _header); static ethash_params params(BlockInfo const& _header);
static ethash_params params(h256 const& _seedHash); static ethash_params params(h256 const& _seedHash);
static ethash_params params(unsigned _n); static ethash_params params(unsigned _n);
@ -58,7 +59,8 @@ private:
std::map<h256, LightType> m_lights; std::map<h256, LightType> m_lights;
std::map<h256, bytesRef> m_fulls; std::map<h256, bytesRef> m_fulls;
std::map<h256, unsigned> m_seedHashes; std::map<h256, unsigned> m_epochs;
h256s m_seedHashes;
}; };
} }

Loading…
Cancel
Save