From 15f74352e3352faaa55477b6eeee83f36c4b0128 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 13 Apr 2015 00:08:52 +0200 Subject: [PATCH] Compile fixes. --- libethcore/Common.h | 37 +++++++++++++------------------ libethcore/Ethash.cpp | 40 +++++++++++++++------------------- libethcore/Ethash.h | 12 ++++++---- libethcore/EthashAux.cpp | 36 +++++++++++++++--------------- libethcore/EthashAux.h | 6 +++-- libethcore/Miner.h | 16 ++++++++------ libethereum/Client.h | 2 +- libethereum/Farm.h | 2 +- libethereum/Interface.h | 5 ----- libethereum/TransactionQueue.h | 2 +- mix/MixClient.cpp | 10 --------- mix/MixClient.h | 2 -- 12 files changed, 75 insertions(+), 95 deletions(-) diff --git a/libethcore/Common.h b/libethcore/Common.h index b5291648b..4d01055f1 100644 --- a/libethcore/Common.h +++ b/libethcore/Common.h @@ -96,42 +96,35 @@ enum class ImportResult BadChain }; -class Signal; - -class Handler +/// Super-duper signal mechanism. TODO: replace with somthing a bit heavier weight. +class Signal { public: - Handler() = default; - Handler(Handler const&) = delete; - ~Handler() { reset(); } + class HandlerAux + { + friend class Signal; - Handler& operator=(Handler const& _h) = delete; + public: + ~HandlerAux() { if (m_s) m_s->m_fire.erase(m_i); m_s = nullptr; } - void reset(); - -private: - Handler(unsigned _i, Signal* _s): m_i(_i), m_s(_s) {} + private: + HandlerAux(unsigned _i, Signal* _s): m_i(_i), m_s(_s) {} - unsigned m_i = 0; - Signal* m_s = nullptr; -}; + unsigned m_i = 0; + Signal* m_s = nullptr; + }; -/// Super-duper signal mechanism. TODO: replace with somthing a bit heavier weight. -class Signal -{ -public: using Callback = std::function; - using Callbacks = std::vector; - Handler add(Callback const& _h) { auto n = m_onReady.size() ? m_onReady.rbegin()->first + 1 : 0; m_onReady[n] = _h; return Handler(n, this); } + std::shared_ptr add(Callback const& _h) { auto n = m_fire.empty() ? 0 : (m_fire.rbegin()->first + 1); m_fire[n] = _h; return std::shared_ptr(new HandlerAux(n, this)); } void operator()() { for (auto const& f: m_fire) f.second(); } private: - std::map m_fire; + std::map m_fire; }; -inline void Handler::reset() { if (m_s) m_s->m_fire->erase(m_i); m_s = nullptr; } +using Handler = std::shared_ptr; } } diff --git a/libethcore/Ethash.cpp b/libethcore/Ethash.cpp index 82e349b4c..f3b4d1b18 100644 --- a/libethcore/Ethash.cpp +++ b/libethcore/Ethash.cpp @@ -62,7 +62,7 @@ unsigned Ethash::revision() void Ethash::prep(BlockInfo const& _header) { if (_header.number % ETHASH_EPOCH_LENGTH == 1) - EthashAux::full(bi); + EthashAux::full(_header); } bool Ethash::preVerify(BlockInfo const& _header) @@ -88,7 +88,7 @@ bool Ethash::verify(BlockInfo const& _header) #endif h256 boundary = u256((bigint(1) << 256) / _header.difficulty); - auto result = eval(_header); + auto result = EthashAux::eval(_header); bool slow = result.value <= boundary && result.mixHash == _header.mixHash; #if ETH_DEBUG || !ETH_TRUE @@ -111,29 +111,27 @@ bool Ethash::verify(BlockInfo const& _header) void Ethash::CPUMiner::workLoop() { auto tid = std::this_thread::get_id(); - static std::mt19937_64 s_eng((time(0) + *reinterpret_cast(m_last.data()) + std::hash()(tid))); + static std::mt19937_64 s_eng((time(0) + std::hash()(tid))); - uint64_t tryNonce = Nonce::random(s_eng); + uint64_t tryNonce = (uint64_t)(u64)Nonce::random(s_eng); ethash_return_value ethashReturn; - auto p = Ethash::params(m_work.seedHash); - void const* dagPointer = Ethash::full(m_work.headerHash).data(); + auto p = EthashAux::params(m_work.seedHash); + void const* dagPointer = EthashAux::full(m_work.headerHash).data(); uint8_t const* headerHashPointer = m_work.headerHash.data(); - h256 boundary = m_work.boundary(); + h256 boundary = m_work.boundary; unsigned hashCount = 0; for (; !shouldStop(); tryNonce++, hashCount++) { ethash_compute_full(ðashReturn, dagPointer, &p, headerHashPointer, tryNonce); h256 value = h256(ethashReturn.result, h256::ConstructFromPointer); - if (value <= boundary && submitProof(Solution{value, h256(ethashReturn.mix_hash, h256::ConstructFromPointer)})) + if (value <= boundary && submitProof(Solution{(Nonce)(u64)tryNonce, h256(ethashReturn.mix_hash, h256::ConstructFromPointer)})) break; } } #if ETH_ETHASHCL || !ETH_TRUE -namespace dev { namespace eth { - class EthashCLHook: public ethash_cl_miner::search_hook { public: @@ -148,8 +146,8 @@ public: m_abort = true; for (unsigned timeout = 0; timeout < 100 && !m_aborted; ++timeout) std::this_thread::sleep_for(chrono::milliseconds(30)); - if (!m_aborted) - cwarn << "Couldn't abort. Abandoning OpenCL process."; +// if (!m_aborted) +// cwarn << "Couldn't abort. Abandoning OpenCL process."; m_aborted = m_abort = false; } @@ -161,7 +159,7 @@ protected: // cdebug << "Found nonces: " << vector(_nonces, _nonces + _count); for (uint32_t i = 0; i < _count; ++i) { - if (m_owner->found(_nonces[i])) + if (m_owner->report(_nonces[i])) { m_aborted = true; return true; @@ -193,19 +191,17 @@ private: Ethash::GPUMiner* m_owner = nullptr; }; -} } - Ethash::GPUMiner::GPUMiner(ConstructionInfo const& _ci): Miner(_ci), m_hook(new EthashCLHook(this)) { } -void Ethash::GPUMiner::report(uint64_t _nonce) +bool Ethash::GPUMiner::report(uint64_t _nonce) { Nonce n = (Nonce)(u64)_nonce; - Result r = Ethash::eval(m_work.seedHash, m_work.headerHash, n); - if (r.value < m_work.boundary) + Result r = EthashAux::eval(m_lastWork.seedHash, m_lastWork.headerHash, n); + if (r.value < m_lastWork.boundary) return submitProof(Solution{n, r.mixHash}); return false; } @@ -217,17 +213,17 @@ void Ethash::GPUMiner::kickOff(WorkPackage const& _work) if (m_miner) m_hook->abort(); m_miner.reset(new ethash_cl_miner); - auto p = Ethash::params(_work.seedHash); - auto cb = [&](void* d) { EthashAux::readFull(_work.seedHash, bytesRef((byte*)d, p.full_size)); }; + auto p = EthashAux::params(_work.seedHash); + auto cb = [&](void* d) { EthashAux::full(_work.seedHash, bytesRef((byte*)d, p.full_size)); }; m_miner->init(p, cb, 32); } if (m_lastWork.headerHash != _work.headerHash) { m_hook->abort(); uint64_t upper64OfBoundary = (uint64_t)(u64)((u256)_work.boundary >> 192); - m_miner->search(_work.headerHash, upper64OfBoundary, *m_hook); + m_miner->search(_work.headerHash.data(), upper64OfBoundary, *m_hook); } - m_work = _work; + m_lastWork = _work; } void Ethash::GPUMiner::pause() diff --git a/libethcore/Ethash.h b/libethcore/Ethash.h index bdd6bf6c5..b87f06549 100644 --- a/libethcore/Ethash.h +++ b/libethcore/Ethash.h @@ -26,6 +26,7 @@ #include #include #include +#include #include "Common.h" #include "BlockInfo.h" #include "Miner.h" @@ -37,9 +38,13 @@ namespace dev namespace eth { +class EthashCLHook; + class Ethash { public: + using Miner = GenericMiner; + struct Solution { Nonce nonce; @@ -73,7 +78,7 @@ public: public: CPUMiner(ConstructionInfo const& _ci): Miner(_ci), Worker("miner" + toString(index())) {} - static unsigned instances() { return thread::hardware_concurrency(); } + static unsigned instances() { return std::thread::hardware_concurrency(); } protected: void kickOff(WorkPackage const& _work) override @@ -93,10 +98,9 @@ public: }; #if ETH_ETHASHCL || !ETH_TRUE - class EthashCLHook; class GPUMiner: public Miner { - friend class EthashCLHook; + friend class dev::eth::EthashCLHook; public: GPUMiner(ConstructionInfo const& _ci); @@ -108,7 +112,7 @@ public: void pause() override; private: - void report(uint64_t _nonce); + bool report(uint64_t _nonce); std::unique_ptr m_hook; std::unique_ptr m_miner; diff --git a/libethcore/EthashAux.cpp b/libethcore/EthashAux.cpp index a6143e264..969310dac 100644 --- a/libethcore/EthashAux.cpp +++ b/libethcore/EthashAux.cpp @@ -102,13 +102,13 @@ void const* EthashAux::light(BlockInfo const& _header) void const* EthashAux::light(h256 const& _seedHash) { - RecursiveGuard l(x_this); - if (!m_lights.count(_header.seedHash())) + RecursiveGuard l(get()->x_this); + if (!get()->m_lights.count(_seedHash)) { ethash_params p = params(_seedHash); - m_lights[_seedHash] = ethash_new_light(&p, _seedHash.data()); + get()->m_lights[_seedHash] = ethash_new_light(&p, _seedHash.data()); } - return m_lights[_seedHash]; + return get()->m_lights[_seedHash]; } bytesConstRef EthashAux::full(BlockInfo const& _header, bytesRef _dest) @@ -118,14 +118,14 @@ bytesConstRef EthashAux::full(BlockInfo const& _header, bytesRef _dest) bytesConstRef EthashAux::full(h256 const& _seedHash, bytesRef _dest) { - RecursiveGuard l(x_this); - if (m_fulls.count(_seedHash) && _dest) + RecursiveGuard l(get()->x_this); + if (get()->m_fulls.count(_seedHash) && _dest) { - assert(m_fulls.size() <= _dest.size()); - m_fulls.at(_seedHash).copyTo(_dest); - return; + assert(get()->m_fulls.size() <= _dest.size()); + get()->m_fulls.at(_seedHash).copyTo(_dest); + return _dest; } - if (!m_fulls.count(_seedHash)) + if (!get()->m_fulls.count(_seedHash)) { // @memoryleak @bug place it on a pile for deletion - perhaps use shared_ptr. /* if (!m_fulls.empty()) @@ -138,7 +138,7 @@ bytesConstRef EthashAux::full(h256 const& _seedHash, bytesRef _dest) boost::filesystem::create_directories(getDataDir("ethash")); } catch (...) {} - auto info = rlpList(revision(), _seedHash); + auto info = rlpList(Ethash::revision(), _seedHash); std::string oldMemoFile = getDataDir("ethash") + "/full"; std::string memoFile = getDataDir("ethash") + "/full-R" + toString(ETHASH_REVISION) + "-" + toHex(_seedHash.ref().cropped(0, 8)); if (boost::filesystem::exists(oldMemoFile) && contents(oldMemoFile + ".info") == info) @@ -147,8 +147,8 @@ bytesConstRef EthashAux::full(h256 const& _seedHash, bytesRef _dest) boost::filesystem::rename(oldMemoFile, memoFile); } - IGNORE_EXCEPTIONS(boost::filesystem::remove(oldMemoFile)); - IGNORE_EXCEPTIONS(boost::filesystem::remove(oldMemoFile + ".info")); + ETH_IGNORE_EXCEPTIONS(boost::filesystem::remove(oldMemoFile)); + ETH_IGNORE_EXCEPTIONS(boost::filesystem::remove(oldMemoFile + ".info")); ethash_params p = params(_seedHash); assert(!_dest || _dest.size() >= p.full_size); // must be big enough. @@ -162,14 +162,14 @@ bytesConstRef EthashAux::full(h256 const& _seedHash, bytesRef _dest) r = _dest; else r = bytesRef(new byte[p.full_size], p.full_size); - ethash_prep_full(r, &p, light(_seedHash)); + ethash_prep_full(r.data(), &p, light(_seedHash)); writeFile(memoFile, r); } if (_dest) return _dest; - m_fulls[_seedHash] = r; + get()->m_fulls[_seedHash] = r; } - return m_fulls[_seedHash]; + return get()->m_fulls[_seedHash]; } Ethash::Result EthashAux::eval(BlockInfo const& _header, Nonce const& _nonce) @@ -179,12 +179,12 @@ Ethash::Result EthashAux::eval(BlockInfo const& _header, Nonce const& _nonce) Ethash::Result EthashAux::eval(h256 const& _seedHash, h256 const& _headerHash, Nonce const& _nonce) { - auto p = EthashAux::params(_header); + auto p = EthashAux::params(_seedHash); ethash_return_value r; if (EthashAux::get()->m_fulls.count(_seedHash)) ethash_compute_full(&r, EthashAux::get()->full(_seedHash).data(), &p, _headerHash.data(), (uint64_t)(u64)_nonce); else ethash_compute_light(&r, EthashAux::get()->light(_seedHash), &p, _headerHash.data(), (uint64_t)(u64)_nonce); // cdebug << "EthashAux::eval sha3(cache):" << sha3(EthashAux::get()->cache(_header)) << "hh:" << _header.headerHash(WithoutNonce) << "nonce:" << _nonce << " => " << h256(r.result, h256::ConstructFromPointer); - return Result{h256(r.result, h256::ConstructFromPointer), h256(r.mix_hash, h256::ConstructFromPointer)}; + return Ethash::Result{h256(r.result, h256::ConstructFromPointer), h256(r.mix_hash, h256::ConstructFromPointer)}; } diff --git a/libethcore/EthashAux.h b/libethcore/EthashAux.h index bfd01a594..aec1089a2 100644 --- a/libethcore/EthashAux.h +++ b/libethcore/EthashAux.h @@ -28,7 +28,7 @@ namespace eth{ class EthashAux { - EthashAux() {} +public: ~EthashAux(); static EthashAux* get() { if (!s_this) s_this = new EthashAux(); return s_this; } @@ -49,9 +49,11 @@ class EthashAux static Ethash::Result eval(h256 const& _seedHash, h256 const& _headerHash, Nonce const& _nonce); private: + EthashAux() {} + void killCache(h256 const& _s); - static Ethash* s_this; + static EthashAux* s_this; RecursiveMutex x_this; std::map m_lights; diff --git a/libethcore/Miner.h b/libethcore/Miner.h index e7157e660..9372c06b1 100644 --- a/libethcore/Miner.h +++ b/libethcore/Miner.h @@ -58,19 +58,19 @@ struct MiningProgress unsigned ms = 0; ///< Total number of milliseconds of mining thus far. }; -template class Miner; +template class GenericMiner; /** * @brief Class for hosting one or more Miners. * @warning Must be implemented in a threadsafe manner since it will be called from multiple * miner threads. */ -template class FarmFace +template class GenericFarmFace { public: using WorkPackage = typename PoW::WorkPackage; using Solution = typename PoW::Solution; - using Miner = Miner; + using Miner = GenericMiner; /** * @brief Called from a Miner to note a WorkPackage has a solution. @@ -85,15 +85,15 @@ public: /** * @brief A miner - a member and adoptee of the Farm. */ -template class Miner +template class GenericMiner { public: - using ConstructionInfo = std::pair*, unsigned>; using WorkPackage = typename PoW::WorkPackage; using Solution = typename PoW::Solution; - using FarmFace = FarmFace; + using FarmFace = GenericFarmFace; + using ConstructionInfo = std::pair; - Miner(ConstructionInfo const& _ci): + GenericMiner(ConstructionInfo const& _ci): m_farm(_ci.first), m_index(_ci.second) {} @@ -144,6 +144,8 @@ protected: return true; } + WorkPackage const& work() const { return m_work; } + private: FarmFace* m_farm = nullptr; unsigned m_index; diff --git a/libethereum/Client.h b/libethereum/Client.h index 8eac2e577..a136033e5 100644 --- a/libethereum/Client.h +++ b/libethereum/Client.h @@ -290,7 +290,7 @@ private: std::weak_ptr m_host; ///< Our Ethereum Host. Don't do anything if we can't lock. - Farm m_farm; ///< Our mining farm. + GenericFarm m_farm; ///< Our mining farm. mutable Mutex x_remoteMiner; ///< The remote miner lock. RemoteMiner m_remoteMiner; ///< The remote miner. diff --git a/libethereum/Farm.h b/libethereum/Farm.h index 55d1aa8df..39988c8d7 100644 --- a/libethereum/Farm.h +++ b/libethereum/Farm.h @@ -42,7 +42,7 @@ namespace eth * @threadsafe */ template -class Farm: public FarmFace +class GenericFarm: public GenericFarmFace { public: /** diff --git a/libethereum/Interface.h b/libethereum/Interface.h index cf2e7f5ea..134bed53b 100644 --- a/libethereum/Interface.h +++ b/libethereum/Interface.h @@ -171,11 +171,6 @@ public: /// Get the coinbase address. virtual Address address() const = 0; - /// Stops mining and sets the number of mining threads (0 for automatic). - virtual void setMiningThreads(unsigned _threads = 0) = 0; - /// Get the effective number of mining threads. - virtual unsigned miningThreads() const = 0; - /// Start mining. /// NOT thread-safe - call it & stopMining only from a single thread virtual void startMining() = 0; diff --git a/libethereum/TransactionQueue.h b/libethereum/TransactionQueue.h index e18f47e80..c3df00d25 100644 --- a/libethereum/TransactionQueue.h +++ b/libethereum/TransactionQueue.h @@ -48,7 +48,7 @@ class TransactionQueue public: using ImportCallback = std::function; - ImportResult import(bytes const& _tx, ImportCallback const& _cb = ImportCallback()) { return import(&_tx); } + ImportResult import(bytes const& _tx, ImportCallback const& _cb = ImportCallback()) { return import(&_tx, _cb); } ImportResult import(bytesConstRef _tx, ImportCallback const& _cb = ImportCallback()); void drop(h256 _txHash); diff --git a/mix/MixClient.cpp b/mix/MixClient.cpp index 5c905ef0b..2e8bd5384 100644 --- a/mix/MixClient.cpp +++ b/mix/MixClient.cpp @@ -372,16 +372,6 @@ void MixClient::setAddress(Address _us) m_state.setAddress(_us); } -void MixClient::setMiningThreads(unsigned _threads) -{ - m_miningThreads = _threads; -} - -unsigned MixClient::miningThreads() const -{ - return m_miningThreads; -} - void MixClient::startMining() { //no-op diff --git a/mix/MixClient.h b/mix/MixClient.h index 357e22930..649c7694f 100644 --- a/mix/MixClient.h +++ b/mix/MixClient.h @@ -63,8 +63,6 @@ public: dev::eth::ExecutionResult call(Secret _secret, u256 _value, Address _dest, bytes const& _data, u256 _gas, u256 _gasPrice, eth::BlockNumber _blockNumber, bool _gasAuto, eth::FudgeFactor _ff = eth::FudgeFactor::Strict); void setAddress(Address _us) override; - void setMiningThreads(unsigned _threads) override; - unsigned miningThreads() const override; void startMining() override; void stopMining() override; bool isMining() const override;