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

1
libethcore/BlockInfo.h

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

29
libethcore/EthashAux.cpp

@ -63,24 +63,47 @@ ethash_params EthashAux::params(unsigned _n)
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)
{
RecursiveGuard l(get()->x_this);
unsigned epoch = 0;
try
{
epoch = get()->m_seedHashes.at(_seedHash);
epoch = get()->m_epochs.at(_seedHash);
}
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)
{
std::ostringstream error;
error << "apparent block number for " << _seedHash.abridged() << " is too high; max is " << (ETHASH_EPOCH_LENGTH * 2048);
throw std::invalid_argument(error.str());
}
get()->m_seedHashes[_seedHash] = epoch;
get()->m_epochs[_seedHash] = epoch;
}
return params(epoch * ETHASH_EPOCH_LENGTH);
}

4
libethcore/EthashAux.h

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

Loading…
Cancel
Save