diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 7026cb7c8..9f630f37d 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -945,7 +945,11 @@ void Main::on_preview_triggered() void Main::on_prepNextDAG_triggered() { - EthashAux::computeFull(ethereum()->blockChain().number() + ETHASH_EPOCH_LENGTH); + EthashAux::computeFull( + EthashAux::seedHash( + ethereum()->blockChain().number() + ETHASH_EPOCH_LENGTH + ) + ); } void Main::refreshMining() diff --git a/eth/main.cpp b/eth/main.cpp index 56c9c3534..3324afcdd 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -857,7 +857,7 @@ int main(int argc, char** argv) auto boundary = bi.boundary(); m = boost::to_lower_copy(string(argv[++i])); bi.nonce = h64(m); - auto r = EthashAux::eval((uint64_t)bi.number, powHash, bi.nonce); + auto r = EthashAux::eval(bi.seedHash(), powHash, bi.nonce); bool valid = r.value < boundary; cout << (valid ? "VALID :-)" : "INVALID :-(") << endl; cout << r.value << (valid ? " < " : " >= ") << boundary << endl; @@ -866,7 +866,7 @@ int main(int argc, char** argv) cout << " with seed as " << seedHash << endl; if (valid) cout << "(mixHash = " << r.mixHash << ")" << endl; - cout << "SHA3( light(seed) ) = " << sha3(EthashAux::light((uint64_t)bi.number)->data()) << endl; + cout << "SHA3( light(seed) ) = " << sha3(EthashAux::light(bi.seedHash())->data()) << endl; exit(0); } catch (...) diff --git a/ethminer/main.cpp b/ethminer/main.cpp index a322431b4..97e125e65 100644 --- a/ethminer/main.cpp +++ b/ethminer/main.cpp @@ -269,8 +269,8 @@ void doFarm(MinerType _m, string const& _remote, unsigned _recheckPeriod) cnote << " Header-hash:" << current.headerHash.hex(); cnote << " Seedhash:" << current.seedHash.hex(); cnote << " Target: " << h256(current.boundary).hex(); - cnote << " Ethash: " << h256(EthashAux::eval(EthashAux::number(current.seedHash), current.headerHash, solution.nonce).value).hex(); - if (EthashAux::eval(EthashAux::number(current.seedHash), current.headerHash, solution.nonce).value < current.boundary) + cnote << " Ethash: " << h256(EthashAux::eval(current.seedHash, current.headerHash, solution.nonce).value).hex(); + if (EthashAux::eval(current.seedHash, current.headerHash, solution.nonce).value < current.boundary) { bool ok = rpc.eth_submitWork("0x" + toString(solution.nonce), "0x" + toString(current.headerHash), "0x" + toString(solution.mixHash)); if (ok) @@ -432,7 +432,7 @@ int main(int argc, char** argv) auto boundary = bi.boundary(); m = boost::to_lower_copy(string(argv[++i])); bi.nonce = h64(m); - auto r = EthashAux::eval((uint64_t)bi.number, powHash, bi.nonce); + auto r = EthashAux::eval(bi.seedHash(), powHash, bi.nonce); bool valid = r.value < boundary; cout << (valid ? "VALID :-)" : "INVALID :-(") << endl; cout << r.value << (valid ? " < " : " >= ") << boundary << endl; @@ -441,7 +441,7 @@ int main(int argc, char** argv) cout << " with seed as " << seedHash << endl; if (valid) cout << "(mixHash = " << r.mixHash << ")" << endl; - cout << "SHA3( light(seed) ) = " << sha3(EthashAux::light((uint64_t)bi.number)->data()) << endl; + cout << "SHA3( light(seed) ) = " << sha3(EthashAux::light(bi.seedHash())->data()) << endl; exit(0); } catch (...) diff --git a/libethcore/Ethash.cpp b/libethcore/Ethash.cpp index 80550886c..92f9d2e18 100644 --- a/libethcore/Ethash.cpp +++ b/libethcore/Ethash.cpp @@ -77,7 +77,7 @@ Ethash::WorkPackage Ethash::package(BlockInfo const& _bi) void Ethash::prep(BlockInfo const& _header, std::function const& _f) { - EthashAux::full((unsigned)_header.number, _f); + EthashAux::full(_header.seedHash(), _f); } bool Ethash::preVerify(BlockInfo const& _header) @@ -134,7 +134,7 @@ void Ethash::CPUMiner::workLoop() WorkPackage w = work(); - auto dag = EthashAux::full(EthashAux::number(w.seedHash)); + auto dag = EthashAux::full(w.seedHash); h256 boundary = w.boundary; unsigned hashCount = 1; for (; !shouldStop(); tryNonce++, hashCount++) @@ -283,7 +283,7 @@ Ethash::GPUMiner::~GPUMiner() bool Ethash::GPUMiner::report(uint64_t _nonce) { Nonce n = (Nonce)(u64)_nonce; - Result r = EthashAux::eval(EthashAux::number(work().seedHash), work().headerHash, n); + Result r = EthashAux::eval(work().seedHash, work().headerHash, n); if (r.value < work().boundary) return submitProof(Solution{n, r.mixHash}); return false; @@ -310,14 +310,14 @@ void Ethash::GPUMiner::workLoop() unsigned device = instances() > 1 ? index() : s_deviceId; - while (EthashAux::computeFull(EthashAux::number(w.seedHash)) != 100 && !shouldStop()) + while (EthashAux::computeFull(w.seedHash) != 100 && !shouldStop()) { - cnote << "Awaiting DAG" << EthashAux::computeFull(EthashAux::number(w.seedHash)); + cnote << "Awaiting DAG" << EthashAux::computeFull(w.seedHash); this_thread::sleep_for(chrono::milliseconds(500)); } if (shouldStop()) return; - EthashAux::FullType dag = EthashAux::full(EthashAux::number(w.seedHash)); + EthashAux::FullType dag = EthashAux::full(w.seedHash); bytesConstRef dagData = dag->data(); m_miner->init(dagData.data(), dagData.size(), 32, s_platformId, device); } diff --git a/libethcore/Ethash.h b/libethcore/Ethash.h index 997c32cd0..0fd2e4cd0 100644 --- a/libethcore/Ethash.h +++ b/libethcore/Ethash.h @@ -66,7 +66,7 @@ public: h256 boundary; h256 headerHash; ///< When h256() means "pause until notified a new work package is available". - h256 seedHash; /// LTODO: IS this needed now that we use the block number instead? + h256 seedHash; }; static const WorkPackage NullWorkPackage; @@ -78,7 +78,7 @@ public: static bool preVerify(BlockInfo const& _header); static WorkPackage package(BlockInfo const& _header); static void assignResult(Solution const& _r, BlockInfo& _header) { _header.nonce = _r.nonce; _header.mixHash = _r.mixHash; } - + class CPUMiner: public Miner, Worker { @@ -119,7 +119,7 @@ public: static void setDefaultPlatform(unsigned _id) { s_platformId = _id; } static void setDefaultDevice(unsigned _id) { s_deviceId = _id; } static void setNumInstances(unsigned _instances) { s_numInstances = std::min(_instances, getNumDevices()); } - + protected: void kickOff() override; void pause() override; diff --git a/libethcore/EthashAux.cpp b/libethcore/EthashAux.cpp index 9287bcbfc..aeacf325a 100644 --- a/libethcore/EthashAux.cpp +++ b/libethcore/EthashAux.cpp @@ -105,23 +105,18 @@ void EthashAux::killCache(h256 const& _s) m_lights.erase(_s); } -EthashAux::LightType EthashAux::light(BlockInfo const& _header) -{ - return light((uint64_t)_header.number); -} - -EthashAux::LightType EthashAux::light(uint64_t _blockNumber) +EthashAux::LightType EthashAux::light(h256 const& _seedHash) { RecursiveGuard l(get()->x_lights); - h256 seedHash = EthashAux::seedHash(_blockNumber); - LightType ret = get()->m_lights[seedHash]; - return ret ? ret : (get()->m_lights[seedHash] = make_shared(_blockNumber)); + LightType ret = get()->m_lights[_seedHash]; + return ret ? ret : (get()->m_lights[_seedHash] = make_shared(_seedHash)); } -EthashAux::LightAllocation::LightAllocation(uint64_t _blockNumber) +EthashAux::LightAllocation::LightAllocation(h256 const& _seedHash) { - light = ethash_light_new(_blockNumber); - size = ethash_get_cachesize(_blockNumber); + uint64_t blockNumber = EthashAux::number(_seedHash); + light = ethash_light_new(blockNumber); + size = ethash_get_cachesize(blockNumber); } EthashAux::LightAllocation::~LightAllocation() @@ -156,14 +151,13 @@ static int dagCallbackShim(unsigned _p) return s_dagCallback ? s_dagCallback(_p) : 0; } -EthashAux::FullType EthashAux::full(uint64_t _blockNumber, function const& _f) +EthashAux::FullType EthashAux::full(h256 const& _seedHash, function const& _f) { - auto l = light(_blockNumber); - h256 seedHash = EthashAux::seedHash(_blockNumber); FullType ret; + auto l = light(_seedHash); DEV_GUARDED(get()->x_fulls) - if ((ret = get()->m_fulls[seedHash].lock())) + if ((ret = get()->m_fulls[_seedHash].lock())) { get()->m_lastUsedFull = ret; return ret; @@ -175,15 +169,15 @@ EthashAux::FullType EthashAux::full(uint64_t _blockNumber, functionx_fulls) - get()->m_fulls[seedHash] = get()->m_lastUsedFull = ret; + get()->m_fulls[_seedHash] = get()->m_lastUsedFull = ret; return ret; } -unsigned EthashAux::computeFull(uint64_t _blockNumber) +unsigned EthashAux::computeFull(h256 const& _seedHash) { Guard l(get()->x_fulls); - h256 seedHash = EthashAux::seedHash(_blockNumber); - if (FullType ret = get()->m_fulls[seedHash].lock()) + uint64_t blockNumber = EthashAux::number(_seedHash); + if (FullType ret = get()->m_fulls[_seedHash].lock()) { get()->m_lastUsedFull = ret; return 100; @@ -192,17 +186,17 @@ unsigned EthashAux::computeFull(uint64_t _blockNumber) if (!get()->m_fullGenerator || !get()->m_fullGenerator->joinable()) { get()->m_fullProgress = 0; - get()->m_generatingFullNumber = _blockNumber / ETHASH_EPOCH_LENGTH * ETHASH_EPOCH_LENGTH; + get()->m_generatingFullNumber = blockNumber / ETHASH_EPOCH_LENGTH * ETHASH_EPOCH_LENGTH; get()->m_fullGenerator = unique_ptr(new thread([=](){ - cnote << "Loading full DAG of" << _blockNumber; - get()->full(_blockNumber, [](unsigned p){ get()->m_fullProgress = p; return 0; }); + cnote << "Loading full DAG of seedhash: " << _seedHash; + get()->full(_seedHash, [](unsigned p){ get()->m_fullProgress = p; return 0; }); cnote << "Full DAG loaded"; get()->m_fullProgress = 0; get()->m_generatingFullNumber = NotGenerating; })); } - return (get()->m_generatingFullNumber == _blockNumber) ? get()->m_fullProgress : 0; + return (get()->m_generatingFullNumber == blockNumber) ? get()->m_fullProgress : 0; } Ethash::Result EthashAux::FullAllocation::compute(h256 const& _headerHash, Nonce const& _nonce) const @@ -223,13 +217,12 @@ Ethash::Result EthashAux::LightAllocation::compute(h256 const& _headerHash, Nonc Ethash::Result EthashAux::eval(BlockInfo const& _header, Nonce const& _nonce) { - return eval((uint64_t)_header.number, _header.headerHash(WithoutNonce), _nonce); + return eval(_header.seedHash(), _header.headerHash(WithoutNonce), _nonce); } -Ethash::Result EthashAux::eval(uint64_t _blockNumber, h256 const& _headerHash, Nonce const& _nonce) +Ethash::Result EthashAux::eval(h256 const& _seedHash, h256 const& _headerHash, Nonce const& _nonce) { - h256 seedHash = EthashAux::seedHash(_blockNumber); - if (FullType dag = get()->m_fulls[seedHash].lock()) + if (FullType dag = get()->m_fulls[_seedHash].lock()) return dag->compute(_headerHash, _nonce); - return EthashAux::get()->light(_blockNumber)->compute(_headerHash, _nonce); + return EthashAux::get()->light(_seedHash)->compute(_headerHash, _nonce); } diff --git a/libethcore/EthashAux.h b/libethcore/EthashAux.h index f57f7a4d3..64501e37b 100644 --- a/libethcore/EthashAux.h +++ b/libethcore/EthashAux.h @@ -40,7 +40,7 @@ public: struct LightAllocation { - LightAllocation(uint64_t _blockNumber); + LightAllocation(h256 const& _seedHash); ~LightAllocation(); bytesConstRef data() const; Ethash::Result compute(h256 const& _headerHash, Nonce const& _nonce) const; @@ -65,20 +65,19 @@ public: static uint64_t number(h256 const& _seedHash); static uint64_t cacheSize(BlockInfo const& _header); - static LightType light(BlockInfo const& _header); - static LightType light(uint64_t _blockNumber); + static LightType light(h256 const& _seedHash); static const uint64_t NotGenerating = (uint64_t)-1; - /// Kicks off generation of DAG for @a _blocknumber and @returns false or @returns true if ready. - static unsigned computeFull(uint64_t _blockNumber); + /// Kicks off generation of DAG for @a _seedHash and @returns false or @returns true if ready. + static unsigned computeFull(h256 const& _seedHash); /// Information on the generation progress. static std::pair fullGeneratingProgress() { return std::make_pair(get()->m_generatingFullNumber, get()->m_fullProgress); } /// Kicks off generation of DAG for @a _blocknumber and blocks until ready; @returns result. - static FullType full(uint64_t _blockNumber, std::function const& _f = std::function()); + static FullType full(h256 const& _seedHash, std::function const& _f = std::function()); static Ethash::Result eval(BlockInfo const& _header) { return eval(_header, _header.nonce); } static Ethash::Result eval(BlockInfo const& _header, Nonce const& _nonce); - static Ethash::Result eval(uint64_t _blockNumber, h256 const& _headerHash, Nonce const& _nonce); + static Ethash::Result eval(h256 const& _seedHash, h256 const& _headerHash, Nonce const& _nonce); private: EthashAux() {} diff --git a/test/libethcore/dagger.cpp b/test/libethcore/dagger.cpp index 4faf0a283..59770373d 100644 --- a/test/libethcore/dagger.cpp +++ b/test/libethcore/dagger.cpp @@ -63,14 +63,14 @@ BOOST_AUTO_TEST_CASE(basic_test) unsigned cacheSize(o["cache_size"].get_int()); h256 cacheHash(o["cache_hash"].get_str()); - BOOST_REQUIRE_EQUAL(EthashAux::get()->light(header)->size, cacheSize); - BOOST_REQUIRE_EQUAL(sha3(EthashAux::get()->light(header)->data()), cacheHash); + BOOST_REQUIRE_EQUAL(EthashAux::get()->light(header.seedHash())->size, cacheSize); + BOOST_REQUIRE_EQUAL(sha3(EthashAux::get()->light(header.seedHash())->data()), cacheHash); #if TEST_FULL unsigned fullSize(o["full_size"].get_int()); h256 fullHash(o["full_hash"].get_str()); - BOOST_REQUIRE_EQUAL(EthashAux::get()->full(header)->size(), fullSize); - BOOST_REQUIRE_EQUAL(sha3(EthashAux::get()->full(header)->data()), fullHash); + BOOST_REQUIRE_EQUAL(EthashAux::get()->full(header.seedHash())->size(), fullSize); + BOOST_REQUIRE_EQUAL(sha3(EthashAux::get()->full(header.seedHash())->data()), fullHash); #endif h256 result(o["result"].get_str());