From 45bacf856602f156ebe9a5fd3ceeff330b765216 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 14 Apr 2015 10:18:34 +0200 Subject: [PATCH] AlethZero fixes. --- libethcore/BlockInfo.cpp | 9 +++++++-- libethcore/BlockInfo.h | 1 + libethcore/EthashAux.cpp | 29 ++++++++++++++++++++++++++--- libethcore/EthashAux.h | 4 +++- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/libethcore/BlockInfo.cpp b/libethcore/BlockInfo.cpp index 6cd431931..b45bdc57e 100644 --- a/libethcore/BlockInfo.cpp +++ b/libethcore/BlockInfo.cpp @@ -23,6 +23,7 @@ #include #include #include +#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) { diff --git a/libethcore/BlockInfo.h b/libethcore/BlockInfo.h index dffff73f4..79c12ebb4 100644 --- a/libethcore/BlockInfo.h +++ b/libethcore/BlockInfo.h @@ -39,6 +39,7 @@ enum IncludeNonce enum Strictness { CheckEverything, + QuickNonce, IgnoreNonce, CheckNothing }; diff --git a/libethcore/EthashAux.cpp b/libethcore/EthashAux.cpp index 969310dac..fb4c2820d 100644 --- a/libethcore/EthashAux.cpp +++ b/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); } diff --git a/libethcore/EthashAux.h b/libethcore/EthashAux.h index aec1089a2..94c2243e0 100644 --- a/libethcore/EthashAux.h +++ b/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 m_lights; std::map m_fulls; - std::map m_seedHashes; + std::map m_epochs; + h256s m_seedHashes; }; }