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

Commit ec5addf

Browse files
committedJun 18, 2018
Fixed CPP Warnings and simplified logic for EthGetworkClient
1 parent 70824b4 commit ec5addf

File tree

6 files changed

+37
-42
lines changed

6 files changed

+37
-42
lines changed
 

‎libpoolprotocols/PoolClient.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ namespace dev
2222
void setConnection(URI &conn)
2323
{
2424
m_conn = &conn;
25-
m_connection_changed = true;
2625
}
2726

2827
virtual void connect() = 0;
@@ -47,9 +46,10 @@ namespace dev
4746
void onWorkReceived(WorkReceived const& _handler) { m_onWorkReceived = _handler; }
4847

4948
protected:
50-
bool m_authorized = false;
51-
bool m_connected = false;
52-
bool m_connection_changed = false;
49+
std::atomic<bool> m_subscribed = { false };
50+
std::atomic<bool> m_authorized = { false };
51+
std::atomic<bool> m_connected = { false };
52+
5353
boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> m_endpoint;
5454

5555
URI* m_conn = nullptr;

‎libpoolprotocols/getwork/EthGetworkClient.cpp

+22-28
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ using namespace eth;
1111
EthGetworkClient::EthGetworkClient(unsigned farmRecheckPeriod, bool submitHashrate) : PoolClient(), Worker("getwork"), m_submit_hashrate(submitHashrate)
1212
{
1313
m_farmRecheckPeriod = farmRecheckPeriod;
14-
m_authorized = true;
15-
m_connection_changed = true;
14+
m_subscribed.store(true, std::memory_order_relaxed);
15+
m_authorized.store(true, std::memory_order_relaxed);
1616
if (m_submit_hashrate)
1717
m_client_id = h256::random();
18-
startWorking();
1918
}
2019

2120
EthGetworkClient::~EthGetworkClient()
@@ -25,22 +24,25 @@ EthGetworkClient::~EthGetworkClient()
2524

2625
void EthGetworkClient::connect()
2726
{
28-
if (m_connection_changed) {
29-
stringstream ss;
30-
ss << "http://" + m_conn->Host() << ':' << m_conn->Port();
31-
if (m_conn->Path().length())
32-
ss << m_conn->Path();
33-
p_client = new ::JsonrpcGetwork(new jsonrpc::HttpClient(ss.str()));
27+
stringstream ss;
28+
ss << "http://" + m_conn->Host() << ':' << m_conn->Port();
29+
if (m_conn->Path().length())
30+
ss << m_conn->Path();
31+
p_client = new ::JsonrpcGetwork(new jsonrpc::HttpClient(ss.str()));
32+
33+
// Since we do not have a real connected state with getwork, we just fake it.
34+
if (m_onConnected) {
35+
m_onConnected();
3436
}
3537

36-
m_connection_changed = false;
37-
m_justConnected = true; // We set a fake flag, that we can check with workhandler if connection works
38+
// No need to worry about starting again.
39+
// Worker class prevents that
40+
startWorking();
3841
}
3942

4043
void EthGetworkClient::disconnect()
4144
{
42-
m_connected = false;
43-
m_justConnected = false;
45+
m_connected.store(false, std::memory_order_relaxed);
4446

4547
// Since we do not have a real connected state with getwork, we just fake it.
4648
if (m_onDisconnected) {
@@ -59,7 +61,7 @@ void EthGetworkClient::submitHashrate(string const & rate)
5961
void EthGetworkClient::submitSolution(const Solution& solution)
6062
{
6163
// Immediately send found solution without wait for loop
62-
if (m_connected || m_justConnected) {
64+
if (m_connected.load(std::memory_order_relaxed)) {
6365
try
6466
{
6567
bool accepted = p_client->eth_submitWork("0x" + toHex(solution.nonce), "0x" + toString(solution.work.header), "0x" + toString(solution.mixHash));
@@ -85,9 +87,9 @@ void EthGetworkClient::submitSolution(const Solution& solution)
8587
// Handles all getwork communication.
8688
void EthGetworkClient::workLoop()
8789
{
88-
while (true)
90+
while (!shouldStop())
8991
{
90-
if (m_connected || m_justConnected) {
92+
if (m_connected.load(std::memory_order_relaxed)) {
9193

9294
// Get Work
9395
try
@@ -98,14 +100,6 @@ void EthGetworkClient::workLoop()
98100
newWorkPackage.epoch = ethash::find_epoch_number(
99101
ethash::hash256_from_bytes(h256{v[1].asString()}.data()));
100102

101-
// Since we do not have a real connected state with getwork, we just fake it.
102-
// If getting work succeeds we know that the connection works
103-
if (m_justConnected && m_onConnected) {
104-
m_justConnected = false;
105-
m_connected = true;
106-
m_onConnected();
107-
}
108-
109103
// Check if header changes so the new workpackage is really new
110104
if (newWorkPackage.header != m_prevWorkPackage.header) {
111105
m_prevWorkPackage.header = newWorkPackage.header;
@@ -129,16 +123,16 @@ void EthGetworkClient::workLoop()
129123
{
130124
p_client->eth_submitHashrate(m_currentHashrateToSubmit, "0x" + m_client_id.hex());
131125
}
132-
catch (const jsonrpc::JsonRpcException&)
126+
catch (const jsonrpc::JsonRpcException& _e)
133127
{
134-
//cwarn << "Failed to submit hashrate.";
135-
//cwarn << boost::diagnostic_information(_e);
128+
cwarn << "Failed to submit hashrate.";
129+
cwarn << boost::diagnostic_information(_e);
136130
}
137131
m_currentHashrateToSubmit.clear();
138132
}
139133
}
140134

141-
// Sleep
135+
// Sleep for --farm-recheck defined period
142136
this_thread::sleep_for(chrono::milliseconds(m_farmRecheckPeriod));
143137
}
144138
}

‎libpoolprotocols/getwork/EthGetworkClient.h

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class EthGetworkClient : public PoolClient, Worker
3333

3434
string m_currentHashrateToSubmit = "";
3535

36-
bool m_justConnected = false;
3736
h256 m_client_id;
3837
JsonrpcGetwork *p_client = nullptr;
3938
WorkPackage m_prevWorkPackage;

‎libpoolprotocols/stratum/EthStratumClient.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void diffToTarget(uint32_t *target, double diff)
4040
}
4141

4242

43-
EthStratumClient::EthStratumClient(boost::asio::io_service & io_service, int worktimeout, int responsetimeout, string email, bool submitHashrate) : PoolClient(),
43+
EthStratumClient::EthStratumClient(boost::asio::io_service & io_service, int worktimeout, int responsetimeout, const string& email, bool submitHashrate) : PoolClient(),
4444
m_worktimeout(worktimeout),
4545
m_responsetimeout(responsetimeout),
4646
m_io_service(io_service),

‎libpoolprotocols/stratum/EthStratumClient.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class EthStratumClient : public PoolClient
2424

2525
typedef enum { STRATUM = 0, ETHPROXY, ETHEREUMSTRATUM } StratumProtocol;
2626

27-
EthStratumClient(boost::asio::io_service & io_service, int worktimeout, int responsetimeout, string email, bool submitHashrate);
27+
EthStratumClient(boost::asio::io_service & io_service, int worktimeout, int responsetimeout, const string& email, bool submitHashrate);
2828
~EthStratumClient();
2929

3030
void connect() override;
@@ -68,9 +68,6 @@ class EthStratumClient : public PoolClient
6868

6969
string m_worker; // eth-proxy only; No ! It's for all !!!
7070

71-
std::atomic<bool> m_subscribed = { false };
72-
std::atomic<bool> m_authorized = { false };
73-
std::atomic<bool> m_connected = { false };
7471
std::atomic<bool> m_disconnecting = { false };
7572
std::atomic<bool> m_connecting = { false };
7673

‎libpoolprotocols/testing/SimulateClient.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ SimulateClient::SimulateClient(unsigned const & difficulty, unsigned const & blo
1111
{
1212
m_difficulty = difficulty -1;
1313
m_block = block;
14-
startWorking();
14+
1515
}
1616

1717
SimulateClient::~SimulateClient()
@@ -21,17 +21,22 @@ SimulateClient::~SimulateClient()
2121

2222
void SimulateClient::connect()
2323
{
24-
m_connected = true;
24+
m_connected.store(true, std::memory_order_relaxed);
2525
m_uppDifficulty = true;
2626

2727
if (m_onConnected) {
2828
m_onConnected();
2929
}
30+
31+
// No need to worry about starting again.
32+
// Worker class prevents that
33+
startWorking();
34+
3035
}
3136

3237
void SimulateClient::disconnect()
3338
{
34-
m_connected = false;
39+
m_connected.store(false, std::memory_order_relaxed);
3540

3641
if (m_onDisconnected) {
3742
m_onDisconnected();
@@ -73,7 +78,7 @@ void SimulateClient::workLoop()
7378
m_time = std::chrono::steady_clock::now();
7479
while (true)
7580
{
76-
if (m_connected) {
81+
if (m_connected.load(std::memory_order_relaxed)) {
7782
if (m_uppDifficulty) {
7883
m_uppDifficulty = false;
7984

0 commit comments

Comments
 (0)
This repository has been archived.