From b758820080e66444d7ac87d5b8f8ff2e6060af09 Mon Sep 17 00:00:00 2001 From: Jan Willem Penterman Date: Mon, 15 Feb 2016 16:32:12 +0100 Subject: [PATCH] borked responses.. --- ethminer/MinerAux.h | 9 +- libstratum/EthStratumClient.cpp | 192 +++++++++++++++++--------------- libstratum/EthStratumClient.h | 5 +- 3 files changed, 111 insertions(+), 95 deletions(-) diff --git a/ethminer/MinerAux.h b/ethminer/MinerAux.h index 106c10538..eb9f6a252 100644 --- a/ethminer/MinerAux.h +++ b/ethminer/MinerAux.h @@ -886,22 +886,25 @@ private: EthStratumClient client(&f, 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; EthashProofOfWork::Solution solution; + /* f.onSolutionFound([&](EthashProofOfWork::Solution sol) { solution = sol; minelog << "Solution!"; - return client.submit(solution); + client.submit(solution); + return false; }); - + */ while (client.isRunning()) { auto mp = f.miningProgress(); diff --git a/libstratum/EthStratumClient.cpp b/libstratum/EthStratumClient.cpp index cc9209708..ecf7d4f62 100644 --- a/libstratum/EthStratumClient.cpp +++ b/libstratum/EthStratumClient.cpp @@ -103,101 +103,21 @@ void EthStratumClient::readResponse(const boost::system::error_code& ec, std::si std::istream is(&m_responseBuffer); std::string response; getline(is, response); - //cnote << response; + cnote << response; + + if (response.back() != '}') { + m_response = response; + } + if (response.front() != '{') { + response = m_response + response; + } Json::Value responseObject; Json::Reader reader; if (reader.parse(response.c_str(), responseObject)) { - if (responseObject.isMember("id")) { - Json::Value error = responseObject.get("error", NULL); - if (error.isArray()) - { - string msg = error.get(1, "Unknown error").asString(); - cnote << msg; - } - std::ostream os(&m_requestBuffer); - Json::Value params; - int id = responseObject.get("id", NULL).asInt(); - switch (id) - { - case 1: - cnote << "Subscribed to stratum server"; - - os << "{\"id\": 2, \"method\": \"mining.authorize\", \"params\": [\"" << m_user << "\",\"" << m_pass << "\"]}\n"; - - async_write(m_socket, m_requestBuffer, - boost::bind(&EthStratumClient::handleResponse, this, - boost::asio::placeholders::error)); - break; - case 2: - m_authorized = responseObject.get("result", NULL).asBool(); - if (!m_authorized) - { - disconnect(); - return; - } - cnote << "Authorized worker " << m_user; - break; - case 4: - cnote << response; - default: - string method = responseObject.get("method", "").asString(); - if (method == "mining.notify") - { - params = responseObject.get("params", NULL); - if (params.isArray()) - { - m_job = params.get((Json::Value::ArrayIndex)0, "").asString(); - string sHeaderHash = params.get((Json::Value::ArrayIndex)1, "").asString(); - string sSeedHash = params.get((Json::Value::ArrayIndex)2, "").asString(); - string sShareTarget = params.get((Json::Value::ArrayIndex)3, "").asString(); - bool cleanJobs = params.get((Json::Value::ArrayIndex)4, "").asBool(); - if (sHeaderHash != "" && sSeedHash != "" && sShareTarget != "") - { - cnote << "Received new job #" + m_job; - cnote << "Header hash: 0x" + sHeaderHash; - cnote << "Seed hash: 0x" + sSeedHash; - cnote << "Share target: 0x" + sShareTarget; - - h256 seedHash = h256(sSeedHash); - h256 headerHash = h256(sHeaderHash); - EthashAux::FullType dag; - - - if (seedHash != m_current.seedHash) - cnote << "Grabbing DAG for" << seedHash; - if (!(dag = EthashAux::full(seedHash, true, [&](unsigned _pc){ cout << "\rCreating DAG. " << _pc << "% done..." << flush; return 0; }))) - BOOST_THROW_EXCEPTION(DAGCreationFailure()); - if (m_precompute) - EthashAux::computeFull(sha3(seedHash), true); - if (headerHash != m_current.headerHash) - { - m_current.headerHash = h256(sHeaderHash); - m_current.seedHash = seedHash; - m_current.boundary = h256(sShareTarget, h256::AlignRight); - p_farm->setWork(m_current); - } - } - } - } - else if (method == "mining.set_difficulty") - { - - } - else if (method == "client.get_version") - { - os << "{\"error\": null, \"id\" : " << id << ", \"result\" : \"" << ETH_PROJECT_VERSION << "\"}"; - async_write(m_socket, m_requestBuffer, - boost::bind(&EthStratumClient::handleResponse, this, - boost::asio::placeholders::error)); - } - break; - } - } - async_read_until(m_socket, m_responseBuffer, "\n", - boost::bind(&EthStratumClient::readResponse, this, - boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); + processReponse(responseObject); + m_response = response; } else { @@ -208,6 +128,98 @@ void EthStratumClient::readResponse(const boost::system::error_code& ec, std::si { cwarn << "Read response failed: " << ec.message(); } + + async_read_until(m_socket, m_responseBuffer, "\n", + boost::bind(&EthStratumClient::readResponse, this, + boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); +} + +void EthStratumClient::processReponse(Json::Value& responseObject) +{ + Json::Value error = responseObject.get("error", NULL); + if (error.isArray()) + { + string msg = error.get(1, "Unknown error").asString(); + cnote << msg; + } + std::ostream os(&m_requestBuffer); + Json::Value params; + int id = responseObject.get("id", NULL).asInt(); + switch (id) + { + case 1: + cnote << "Subscribed to stratum server"; + + os << "{\"id\": 2, \"method\": \"mining.authorize\", \"params\": [\"" << m_user << "\",\"" << m_pass << "\"]}\n"; + + async_write(m_socket, m_requestBuffer, + boost::bind(&EthStratumClient::handleResponse, this, + boost::asio::placeholders::error)); + break; + case 2: + m_authorized = responseObject.get("result", NULL).asBool(); + if (!m_authorized) + { + disconnect(); + return; + } + cnote << "Authorized worker " << m_user; + break; + case 4: + cnote << m_response; + default: + string method = responseObject.get("method", "").asString(); + if (method == "mining.notify") + { + params = responseObject.get("params", NULL); + if (params.isArray()) + { + m_job = params.get((Json::Value::ArrayIndex)0, "").asString(); + string sHeaderHash = params.get((Json::Value::ArrayIndex)1, "").asString(); + string sSeedHash = params.get((Json::Value::ArrayIndex)2, "").asString(); + string sShareTarget = params.get((Json::Value::ArrayIndex)3, "").asString(); + bool cleanJobs = params.get((Json::Value::ArrayIndex)4, "").asBool(); + if (sHeaderHash != "" && sSeedHash != "" && sShareTarget != "") + { + cnote << "Received new job #" + m_job; + cnote << "Header hash: 0x" + sHeaderHash; + cnote << "Seed hash: 0x" + sSeedHash; + cnote << "Share target: 0x" + sShareTarget; + + h256 seedHash = h256(sSeedHash); + h256 headerHash = h256(sHeaderHash); + EthashAux::FullType dag; + + + if (seedHash != m_current.seedHash) + cnote << "Grabbing DAG for" << seedHash; + if (!(dag = EthashAux::full(seedHash, true, [&](unsigned _pc){ cout << "\rCreating DAG. " << _pc << "% done..." << flush; return 0; }))) + BOOST_THROW_EXCEPTION(DAGCreationFailure()); + if (m_precompute) + EthashAux::computeFull(sha3(seedHash), true); + if (headerHash != m_current.headerHash) + { + m_current.headerHash = h256(sHeaderHash); + m_current.seedHash = seedHash; + m_current.boundary = h256(sShareTarget, h256::AlignRight); + p_farm->setWork(m_current); + } + } + } + } + else if (method == "mining.set_difficulty") + { + + } + else if (method == "client.get_version") + { + os << "{\"error\": null, \"id\" : " << id << ", \"result\" : \"" << ETH_PROJECT_VERSION << "\"}"; + async_write(m_socket, m_requestBuffer, + boost::bind(&EthStratumClient::handleResponse, this, + boost::asio::placeholders::error)); + } + break; + } } bool EthStratumClient::submit(EthashProofOfWork::Solution solution) { diff --git a/libstratum/EthStratumClient.h b/libstratum/EthStratumClient.h index 3e58ae613..0971f233e 100644 --- a/libstratum/EthStratumClient.h +++ b/libstratum/EthStratumClient.h @@ -34,9 +34,8 @@ private: void handleResponse(const boost::system::error_code& ec); void readResponse(const boost::system::error_code& ec, std::size_t bytes_transferred); - + void processReponse(Json::Value& responseObject); - string m_host; string m_port; string m_user; @@ -45,6 +44,8 @@ private: bool m_running; bool m_precompute; + string m_response; + GenericFarm * p_farm; EthashProofOfWork::WorkPackage m_current; string m_job;