Skip to content
This repository was archived by the owner on Apr 24, 2022. It is now read-only.

Commit 4733b7a

Browse files
committedNov 19, 2018
GetWork Refactor
Now totally async without the need of an own thread. Also differentiates among socket errors and json errors (eg. "Still sincing"). Implements workTimeout (which was only for Stratum) Totally built on boost without the need of json-rpc-cpp
1 parent f7b92f7 commit 4733b7a

9 files changed

+562
-171
lines changed
 

‎ethminer/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ class MinerCLI
11811181
client = new SimulateClient(20, m_benchmarkBlock);
11821182
break;
11831183
case MinerCLI::OperationMode::Farm:
1184-
client = new EthGetworkClient(m_farmPollInterval);
1184+
client = new EthGetworkClient(m_poolWorkTimeout, m_farmPollInterval);
11851185
break;
11861186
case MinerCLI::OperationMode::Stratum:
11871187
client = new EthStratumClient(m_poolWorkTimeout, m_poolRespTimeout);

‎libpoolprotocols/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ set(SOURCES
44
PoolManager.h PoolManager.cpp
55
testing/SimulateClient.h testing/SimulateClient.cpp
66
stratum/EthStratumClient.h stratum/EthStratumClient.cpp
7-
getwork/EthGetworkClient.h getwork/EthGetworkClient.cpp getwork/jsonrpc_getwork.h
7+
getwork/EthGetworkClient.h getwork/EthGetworkClient.cpp
88
)
99

1010
hunter_add_package(OpenSSL)
1111
find_package(OpenSSL REQUIRED)
1212

1313
add_library(poolprotocols ${SOURCES})
14-
target_link_libraries(poolprotocols PRIVATE devcore ethminer-buildinfo ethash::ethash libjson-rpc-cpp::client Boost::system jsoncpp_lib_static OpenSSL::SSL OpenSSL::Crypto)
14+
target_link_libraries(poolprotocols PRIVATE devcore ethminer-buildinfo ethash::ethash Boost::system jsoncpp_lib_static OpenSSL::SSL OpenSSL::Crypto)
1515
target_include_directories(poolprotocols PRIVATE ..)

‎libpoolprotocols/PoolManager.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ PoolManager::PoolManager(PoolClient* client, MinerType const& minerType, unsigne
3333
p_client->onConnected([&]() {
3434
{
3535
Guard l(m_activeConnectionMutex);
36-
m_selectedHost.append(p_client->ActiveEndPoint());
36+
37+
// If HostName is already an IP address no need to append the
38+
// effective ip address.
39+
if (p_client->getConnection()->HostNameType() == dev::UriHostNameType::Dns ||
40+
p_client->getConnection()->HostNameType() == dev::UriHostNameType::Basic)
41+
m_selectedHost.append(p_client->ActiveEndPoint());
42+
3743
cnote << "Established connection to " << m_selectedHost;
3844

3945
// Reset current WorkPackage

‎libpoolprotocols/getwork/EthGetworkClient.cpp

+503-86
Large diffs are not rendered by default.
+48-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
#pragma once
22

33
#include <iostream>
4+
#include <string>
45

5-
#include "jsonrpc_getwork.h"
6-
#include <jsonrpccpp/client/connectors/httpclient.h>
6+
#include <boost/algorithm/string/predicate.hpp>
7+
#include <boost/lexical_cast.hpp>
8+
#include <boost/lockfree/queue.hpp>
79

8-
#include <libdevcore/Worker.h>
10+
#include <json/json.h>
911

1012
#include "../PoolClient.h"
1113

1214
using namespace std;
1315
using namespace dev;
1416
using namespace eth;
1517

16-
class EthGetworkClient : public PoolClient, Worker
18+
class EthGetworkClient : public PoolClient
1719
{
1820
public:
19-
EthGetworkClient(unsigned farmRecheckPeriod);
21+
EthGetworkClient(int worktimeout, unsigned farmRecheckPeriod);
2022
~EthGetworkClient();
2123

2224
void connect() override;
@@ -25,18 +27,52 @@ class EthGetworkClient : public PoolClient, Worker
2527
bool isConnected() override { return m_connected; }
2628
bool isPendingState() override { return false; }
2729

28-
string ActiveEndPoint() override { return ""; };
30+
string ActiveEndPoint() override { return " [" + toString(m_endpoint) + "]"; };
2931

3032
void submitHashrate(string const& rate, string const& id) override;
3133
void submitSolution(const Solution& solution) override;
3234

3335
private:
34-
void workLoop() override;
35-
unsigned m_farmRecheckPeriod = 500;
3636

37-
string m_HashrateHex; // Hashrate value already as hex string
38-
string m_HashrateId; // Hashrate unique identifier
37+
unsigned m_farmRecheckPeriod = 500; // In milliseconds
3938

40-
JsonrpcGetwork* p_client = nullptr;
41-
WorkPackage m_prevWorkPackage;
39+
void begin_connect();
40+
void handle_resolve(
41+
const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::iterator i);
42+
void handle_connect(const boost::system::error_code& ec);
43+
void handle_write(const boost::system::error_code& ec);
44+
void handle_read(const boost::system::error_code& ec, std::size_t bytes_transferred);
45+
std::string processError(Json::Value& JRes);
46+
void processResponse(Json::Value& JRes);
47+
void send(Json::Value const& jReq);
48+
void send(std::string const& sReq);
49+
void getwork_timer_elapsed(const boost::system::error_code& ec);
50+
51+
WorkPackage m_current;
52+
53+
std::atomic<bool> m_connecting = {false}; // Whether or not socket is on first try connect
54+
std::atomic<bool> m_txPending = {false}; // Whether or not an async socket operation is pending
55+
boost::lockfree::queue<std::string*> m_txQueue;
56+
57+
boost::asio::io_service& m_io_service; // The IO service reference passed in the constructor
58+
boost::asio::io_service::strand m_io_strand;
59+
60+
boost::asio::ip::tcp::socket m_socket;
61+
boost::asio::ip::tcp::resolver m_resolver;
62+
std::queue<boost::asio::ip::basic_endpoint<boost::asio::ip::tcp>> m_endpoints;
63+
64+
boost::asio::streambuf m_request;
65+
boost::asio::streambuf m_response;
66+
Json::StreamWriterBuilder m_jSwBuilder;
67+
std::string m_jsonGetWork;
68+
Json::Value m_pendingJReq;
69+
std::chrono::time_point<std::chrono::steady_clock> m_pending_tstamp;
70+
71+
boost::asio::deadline_timer m_getwork_timer; // The timer which triggers getWork requests
72+
73+
// seconds to trigger a work_timeout (overwritten in constructor)
74+
int m_worktimeout;
75+
std::chrono::time_point<std::chrono::steady_clock> m_current_tstamp;
76+
77+
unsigned m_solution_submitted_max_id; // maximum json id we used to send a solution
4278
};

‎libpoolprotocols/getwork/getwork.json

-5
This file was deleted.

‎libpoolprotocols/getwork/jsonrpc_getwork.h

-57
This file was deleted.

‎libpoolprotocols/stratum/EthStratumClient.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ void EthStratumClient::connect()
206206
m_workloop_timer.async_wait(m_io_strand.wrap(boost::bind(
207207
&EthStratumClient::workloop_timer_elapsed, this, boost::asio::placeholders::error)));
208208

209-
210209
// Reset status flags
211210
m_canconnect.store(false, std::memory_order_relaxed);
212211
m_connected.store(false, std::memory_order_relaxed);
@@ -230,7 +229,6 @@ void EthStratumClient::connect()
230229
if (!m_socket)
231230
init_socket();
232231

233-
234232
// Initialize a new queue of end points
235233
m_endpoints = std::queue<boost::asio::ip::basic_endpoint<boost::asio::ip::tcp>>();
236234
m_endpoint = boost::asio::ip::basic_endpoint<boost::asio::ip::tcp>();
@@ -381,9 +379,7 @@ void EthStratumClient::disconnect_finalize()
381379

382380
// Trigger handlers
383381
if (m_onDisconnected)
384-
{
385382
m_onDisconnected();
386-
}
387383
}
388384

389385
void EthStratumClient::resolve_handler(

‎libpoolprotocols/stratum/EthStratumClient.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class EthStratumClient : public PoolClient
130130
boost::asio::io_service& m_io_service; // The IO service reference passed in the constructor
131131
boost::asio::io_service::strand m_io_strand;
132132
boost::asio::ip::tcp::socket* m_socket;
133-
std::string m_message; // The internal message string buffer
133+
std::string m_message; // The internal message string buffer
134134
bool m_newjobprocessed = false;
135135

136136
// Use shared ptrs to avoid crashes due to async_writes
@@ -155,8 +155,6 @@ class EthStratumClient : public PoolClient
155155
boost::asio::ip::tcp::resolver m_resolver;
156156
std::queue<boost::asio::ip::basic_endpoint<boost::asio::ip::tcp>> m_endpoints;
157157

158-
string m_rate;
159-
160158
h256 m_nextWorkBoundary = h256("0x00000000ffff0000000000000000000000000000000000000000000000000000");
161159

162160
uint64_t m_extraNonce = 0;

0 commit comments

Comments
 (0)
This repository has been archived.