From ce273972b2547240a3bd5f154fa28d1a8a537874 Mon Sep 17 00:00:00 2001 From: Jan Willem Penterman Date: Thu, 18 Feb 2016 14:47:22 +0100 Subject: [PATCH] stop farm while disconnected --- ethminer/MinerAux.h | 34 ++++++++++----------------------- libethcore/Miner.h | 7 +++++++ libstratum/EthStratumClient.cpp | 31 +++++++++++++++++++++++++----- libstratum/EthStratumClient.h | 8 +++++--- 4 files changed, 48 insertions(+), 32 deletions(-) diff --git a/ethminer/MinerAux.h b/ethminer/MinerAux.h index 79c3b652b..dec4db0d2 100644 --- a/ethminer/MinerAux.h +++ b/ethminer/MinerAux.h @@ -560,13 +560,6 @@ public: ; } - enum class MinerType - { - CPU, - CL, - CUDA - }; - MinerType minerType() const { return m_minerType; } bool shouldPrecompute() const { return m_precompute; } @@ -883,34 +876,27 @@ private: sealers["cuda"] = GenericFarm::SealerDescriptor{ &EthashCUDAMiner::instances, [](GenericMiner::ConstructionInfo ci){ return new EthashCUDAMiner(ci); } }; #endif GenericFarm f; - EthStratumClient client(&f, host, port, user, pass); + EthStratumClient client(&f, _m, host, port, user, pass); f.setSealers(sealers); - - if (_m == MinerType::CPU) - f.start("cpu"); - else if (_m == MinerType::CL) - f.start("opencl"); - else if (_m == MinerType::CUDA) - f.start("cuda"); - - bool completed = false; - + f.onSolutionFound([&](EthashProofOfWork::Solution sol) { client.submit(sol); return false; }); - while (client.isRunning()) + while (true) { auto mp = f.miningProgress(); f.resetMiningProgress(); - if (client.current()) - minelog << "Mining on PoWhash" << client.currentHeaderHash() << ": " << mp; - else - minelog << "Waiting for work package..."; - + if (client.isConnected()) + { + if (client.current()) + minelog << "Mining on PoWhash" << client.currentHeaderHash() << ": " << mp; + else + minelog << "Waiting for work package..."; + } this_thread::sleep_for(chrono::milliseconds(_recheckPeriod)); } } diff --git a/libethcore/Miner.h b/libethcore/Miner.h index 42d17ab53..21f045967 100644 --- a/libethcore/Miner.h +++ b/libethcore/Miner.h @@ -36,6 +36,13 @@ namespace dev namespace eth { +enum class MinerType +{ + CPU, + CL, + CUDA +}; + struct MineInfo: public WorkingProgress {}; inline std::ostream& operator<<(std::ostream& _out, WorkingProgress _p) diff --git a/libstratum/EthStratumClient.cpp b/libstratum/EthStratumClient.cpp index 0bb1c9d2a..f60de4f0d 100644 --- a/libstratum/EthStratumClient.cpp +++ b/libstratum/EthStratumClient.cpp @@ -4,17 +4,20 @@ using boost::asio::ip::tcp; -EthStratumClient::EthStratumClient(GenericFarm * f, string const & host, string const & port, string const & user, string const & pass) +EthStratumClient::EthStratumClient(GenericFarm * f, MinerType m, string const & host, string const & port, string const & user, string const & pass) : m_socket(m_io_service) { + m_minerType = m; m_host = host; m_port = port; m_user = user; m_pass = pass; m_authorized = false; - m_running = true; + m_connected = false; m_precompute = true; m_pending = 0; + + p_farm = f; connect(); } @@ -42,9 +45,15 @@ void EthStratumClient::connect() void EthStratumClient::reconnect() { + if (p_farm->isMining()) + { + cnote << "Stopping farm"; + p_farm->stop(); + } m_socket.close(); m_io_service.reset(); m_authorized = false; + m_connected = false; cnote << "Reconnecting in 3 seconds..."; boost::asio::deadline_timer timer(m_io_service, boost::posix_time::seconds(3)); timer.wait(); @@ -54,8 +63,12 @@ void EthStratumClient::reconnect() void EthStratumClient::disconnect() { cnote << "Disconnecting"; - m_running = false; - + m_connected = false; + if (p_farm->isMining()) + { + cnote << "Stopping farm"; + p_farm->stop(); + } m_socket.close(); m_io_service.stop(); } @@ -71,7 +84,7 @@ void EthStratumClient::resolve_handler(const boost::system::error_code& ec, tcp: else { cerr << "Could not resolve host" << m_host + ":" + m_port + ", " << ec.message(); - disconnect(); + reconnect(); } } @@ -79,7 +92,15 @@ void EthStratumClient::connect_handler(const boost::system::error_code& ec, tcp: { if (!ec) { + m_connected = true; cnote << "Connected to stratum server " << m_host << ":" << m_port; + cnote << "Starting farm"; + if (m_minerType == MinerType::CPU) + p_farm->start("cpu"); + else if (m_minerType == MinerType::CL) + p_farm->start("opencl"); + else if (m_minerType == MinerType::CUDA) + p_farm->start("cuda"); std::ostream os(&m_requestBuffer); os << "{\"id\": 1, \"method\": \"mining.subscribe\", \"params\": []}\n"; diff --git a/libstratum/EthStratumClient.h b/libstratum/EthStratumClient.h index f2df9f2e3..661c0fc34 100644 --- a/libstratum/EthStratumClient.h +++ b/libstratum/EthStratumClient.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "BuildInfo.h" @@ -19,10 +20,10 @@ using namespace dev::eth; class EthStratumClient { public: - EthStratumClient(GenericFarm * f, string const & host, string const & port, string const & user, string const & pass); + EthStratumClient(GenericFarm * f, MinerType m, string const & host, string const & port, string const & user, string const & pass); ~EthStratumClient(); - bool isRunning() { return m_running; } + bool isConnected() { return m_connected; } h256 currentHeaderHash() { return m_current.headerHash; } bool current() { return m_current; } bool submit(EthashProofOfWork::Solution solution); @@ -38,12 +39,13 @@ private: void readResponse(const boost::system::error_code& ec, std::size_t bytes_transferred); void processReponse(Json::Value& responseObject); + MinerType m_minerType; string m_host; string m_port; string m_user; string m_pass; bool m_authorized; - bool m_running; + bool m_connected; bool m_precompute; int m_pending;