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

Commit 7956308

Browse files
committedDec 11, 2018
EthereumStratum/2.0.0 Implementation
Implementation of EthereumStratum/2.0.0. Does not break previous flavors nor autodetection. New flavor is either autodetected or set with stratum3+...
1 parent 591216f commit 7956308

11 files changed

+482
-189
lines changed
 

‎libdevcore/CommonData.h

+26-4
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,42 @@ inline std::string toHex(u256 val, HexPrefix prefix = HexPrefix::DontAdd)
165165
return (prefix == HexPrefix::Add) ? "0x" + str : str;
166166
}
167167

168-
inline std::string toHex(uint64_t _n, HexPrefix _prefix = HexPrefix::DontAdd)
168+
inline std::string toHex(uint64_t _n, HexPrefix _prefix = HexPrefix::DontAdd, short _bytes = 8)
169169
{
170+
// sizeof returns the number of bytes (not the number of bits)
171+
// thus if CHAR_BIT != 8 sizeof(uint64_t) will return != 8
172+
// Use fixed constant multiplier of 16
170173
std::ostringstream ret;
171-
ret << std::hex << std::setfill('0') << std::setw(sizeof(_n) * 2) << _n;
174+
ret << std::hex << std::setfill('0') << std::setw(_bytes) << _n;
172175
return (_prefix == HexPrefix::Add) ? "0x" + ret.str() : ret.str();
173176
}
174177

175-
inline std::string toHex(uint32_t _n, HexPrefix _prefix = HexPrefix::DontAdd)
178+
inline std::string toHex(uint32_t _n, HexPrefix _prefix = HexPrefix::DontAdd, short _bytes = 4)
176179
{
180+
// sizeof returns the number of bytes (not the number of bits)
181+
// thus if CHAR_BIT != 8 sizeof(uint64_t) will return != 4
182+
// Use fixed constant multiplier of 8
177183
std::ostringstream ret;
178-
ret << std::hex << std::setfill('0') << std::setw(sizeof(_n) * 2) << _n;
184+
ret << std::hex << std::setfill('0') << std::setw(_bytes) << _n;
179185
return (_prefix == HexPrefix::Add) ? "0x" + ret.str() : ret.str();
180186
}
181187

188+
inline std::string toCompactHex(uint64_t _n, HexPrefix _prefix = HexPrefix::DontAdd)
189+
{
190+
std::ostringstream ret;
191+
ret << std::hex << _n;
192+
return (_prefix == HexPrefix::Add) ? "0x" + ret.str() : ret.str();
193+
}
194+
195+
inline std::string toCompactHex(uint32_t _n, HexPrefix _prefix = HexPrefix::DontAdd)
196+
{
197+
std::ostringstream ret;
198+
ret << std::hex << _n;
199+
return (_prefix == HexPrefix::Add) ? "0x" + ret.str() : ret.str();
200+
}
201+
202+
203+
182204
// Algorithms for string and string-like collections.
183205

184206
/// Escapes a string into the C-string representation.

‎libethcore/EthashAux.h

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ struct WorkPackage
6666

6767
uint64_t startNonce = 0;
6868
uint16_t exSizeBytes = 0;
69+
70+
std::string algo = "ethash";
6971
};
7072

7173
struct Solution

‎libpoolprotocols/PoolClient.h

+18-4
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,23 @@ struct Session
2929
return (chrono::duration_cast<chrono::minutes>(chrono::steady_clock::now() - start))
3030
.count();
3131
}
32+
33+
// EthereumStratum (1 and 2)
34+
3235
// Extranonce currently active
3336
uint64_t extraNonce = 0;
3437
// Length of extranonce in bytes
3538
unsigned int extraNonceSizeBytes = 0;
3639
// Next work target
3740
h256 nextWorkBoundary =
3841
h256("0x00000000ffff0000000000000000000000000000000000000000000000000000");
42+
43+
// EthereumStratum (2 only)
44+
string sessionId = "";
45+
string workerId = "";
46+
string algo = "ethash";
47+
unsigned int epoch = 0;
48+
3949
};
4050

4151
class PoolClient
@@ -58,10 +68,9 @@ class PoolClient
5868

5969
virtual void connect() = 0;
6070
virtual void disconnect() = 0;
61-
62-
virtual void submitHashrate(string const& rate, string const& id) = 0;
71+
virtual void submitHashrate(uint64_t const& rate, string const& id) = 0;
6372
virtual void submitSolution(const Solution& solution) = 0;
64-
virtual bool isConnected() { return (m_session ? true : false); }
73+
virtual bool isConnected() { return m_connected.load(memory_order_relaxed); }
6574
virtual bool isPendingState() { return false; }
6675

6776
virtual bool isSubscribed()
@@ -73,7 +82,10 @@ class PoolClient
7382
return (m_session ? m_session->authorized.load(memory_order_relaxed) : false);
7483
}
7584

76-
virtual string ActiveEndPoint() { return (m_session ? " [" + toString(m_endpoint) + "]" : ""); }
85+
virtual string ActiveEndPoint()
86+
{
87+
return (m_connected.load(memory_order_relaxed) ? " [" + toString(m_endpoint) + "]" : "");
88+
}
7789

7890
using SolutionAccepted = function<void(chrono::milliseconds const&, unsigned const&)>;
7991
using SolutionRejected = function<void(chrono::milliseconds const&, unsigned const&)>;
@@ -90,6 +102,8 @@ class PoolClient
90102
protected:
91103
unique_ptr<Session> m_session = nullptr;
92104

105+
std::atomic<bool> m_connected = {false}; // This is related to socket ! Not session
106+
93107
boost::asio::ip::basic_endpoint<boost::asio::ip::tcp> m_endpoint;
94108

95109
std::shared_ptr<URI> m_conn = nullptr;

‎libpoolprotocols/PoolManager.cpp

+23-8
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,34 @@ void PoolManager::setClientHandlers()
158158
return;
159159

160160
int _currentEpoch = m_currentWp.epoch;
161-
bool newEpoch = (_currentEpoch == -1 || wp.seed != m_currentWp.seed);
161+
bool newEpoch = (_currentEpoch == -1);
162+
163+
// In EthereumStratum/2.0.0 epoch number is set in session
164+
if (!newEpoch)
165+
{
166+
if (p_client->getConnection()->StratumMode() == 3)
167+
newEpoch = (wp.epoch != m_currentWp.epoch);
168+
else
169+
newEpoch = (wp.seed != m_currentWp.seed);
170+
}
171+
162172
bool newDiff = (wp.boundary != m_currentWp.boundary);
173+
163174
m_currentWp = wp;
164175

165176
if (newEpoch)
166177
{
167178
m_epochChanges.fetch_add(1, std::memory_order_relaxed);
168-
if (m_currentWp.block > 0)
169-
m_currentWp.epoch = m_currentWp.block / 30000;
170-
else
171-
m_currentWp.epoch =
172-
ethash::find_epoch_number(ethash::hash256_from_bytes(m_currentWp.seed.data()));
179+
180+
// If epoch is valued in workpackage take it
181+
if (wp.epoch == -1)
182+
{
183+
if (m_currentWp.block > 0)
184+
m_currentWp.epoch = m_currentWp.block / 30000;
185+
else
186+
m_currentWp.epoch = ethash::find_epoch_number(
187+
ethash::hash256_from_bytes(m_currentWp.seed.data()));
188+
}
173189
}
174190
else
175191
{
@@ -479,10 +495,9 @@ void PoolManager::submithrtimer_elapsed(const boost::system::error_code& ec)
479495
{
480496
if (m_running.load(std::memory_order_relaxed))
481497
{
482-
std::string hr_hex = toHex((uint64_t)Farm::f().HashRate(), HexPrefix::Add);
483498

484499
if (p_client && p_client->isConnected())
485-
p_client->submitHashrate(hr_hex, m_Settings.hashRateId);
500+
p_client->submitHashrate((uint32_t)Farm::f().HashRate(), m_Settings.hashRateId);
486501

487502
// Resubmit actor
488503
m_submithrtimer.expires_from_now(boost::posix_time::seconds(m_Settings.hashRateInterval));

‎libpoolprotocols/PoolURI.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,19 @@ static std::map<std::string, SchemeAttributes> s_schemes = {
4141
{"stratum+tcp", {ProtocolFamily::STRATUM, SecureLevel::NONE, 0}},
4242
{"stratum1+tcp", {ProtocolFamily::STRATUM, SecureLevel::NONE, 1}},
4343
{"stratum2+tcp", {ProtocolFamily::STRATUM, SecureLevel::NONE, 2}},
44+
{"stratum3+tcp", {ProtocolFamily::STRATUM, SecureLevel::NONE, 3}},
4445
{"stratum+tls", {ProtocolFamily::STRATUM, SecureLevel::TLS, 0}},
4546
{"stratum1+tls", {ProtocolFamily::STRATUM, SecureLevel::TLS, 1}},
4647
{"stratum2+tls", {ProtocolFamily::STRATUM, SecureLevel::TLS, 2}},
48+
{"stratum3+tls", {ProtocolFamily::STRATUM, SecureLevel::TLS, 3}},
4749
{"stratum+tls12", {ProtocolFamily::STRATUM, SecureLevel::TLS12, 0}},
4850
{"stratum1+tls12", {ProtocolFamily::STRATUM, SecureLevel::TLS12, 1}},
4951
{"stratum2+tls12", {ProtocolFamily::STRATUM, SecureLevel::TLS12, 2}},
52+
{"stratum3+tls12", {ProtocolFamily::STRATUM, SecureLevel::TLS12, 3}},
5053
{"stratum+ssl", {ProtocolFamily::STRATUM, SecureLevel::TLS12, 0}},
5154
{"stratum1+ssl", {ProtocolFamily::STRATUM, SecureLevel::TLS12, 1}},
5255
{"stratum2+ssl", {ProtocolFamily::STRATUM, SecureLevel::TLS12, 2}},
56+
{"stratum3+ssl", {ProtocolFamily::STRATUM, SecureLevel::TLS12, 3}},
5357
{"http", {ProtocolFamily::GETWORK, SecureLevel::NONE, 0}},
5458
{"getwork", {ProtocolFamily::GETWORK, SecureLevel::NONE, 0}},
5559

‎libpoolprotocols/getwork/EthGetworkClient.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ void EthGetworkClient::connect()
7676
void EthGetworkClient::disconnect()
7777
{
7878
// Release session
79+
m_connected.store(false, memory_order_relaxed);
7980
m_conn->addDuration(m_session->duration());
8081
m_session = nullptr;
8182

@@ -117,6 +118,7 @@ void EthGetworkClient::handle_connect(const boost::system::error_code& ec)
117118
if (m_connecting.load(std::memory_order_relaxed))
118119
{
119120
// Initialize new session
121+
m_connected.store(true, memory_order_relaxed);
120122
m_session = unique_ptr<Session>(new Session);
121123
m_session->subscribed.store(true, memory_order_relaxed);
122124
m_session->authorized.store(true, memory_order_relaxed);
@@ -516,7 +518,7 @@ void EthGetworkClient::send(std::string const& sReq)
516518
begin_connect();
517519
}
518520

519-
void EthGetworkClient::submitHashrate(string const& rate, string const& id)
521+
void EthGetworkClient::submitHashrate(uint64_t const& rate, string const& id)
520522
{
521523
// No need to check for authorization
522524
if (m_session)
@@ -526,8 +528,8 @@ void EthGetworkClient::submitHashrate(string const& rate, string const& id)
526528
jReq["jsonrpc"] = "2.0";
527529
jReq["method"] = "eth_submitHashrate";
528530
jReq["params"] = Json::Value(Json::arrayValue);
529-
jReq["params"].append(rate); // Already expressed as hex
530-
jReq["params"].append(id); // Already prefixed by 0x
531+
jReq["params"].append(toHex(rate, HexPrefix::Add)); // Already expressed as hex
532+
jReq["params"].append(id); // Already prefixed by 0x
531533
send(jReq);
532534
}
533535

‎libpoolprotocols/getwork/EthGetworkClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class EthGetworkClient : public PoolClient
2525
void connect() override;
2626
void disconnect() override;
2727

28-
void submitHashrate(string const& rate, string const& id) override;
28+
void submitHashrate(uint64_t const& rate, string const& id) override;
2929
void submitSolution(const Solution& solution) override;
3030

3131
private:

‎libpoolprotocols/stratum/EthStratumClient.cpp

+397-166
Large diffs are not rendered by default.

‎libpoolprotocols/stratum/EthStratumClient.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ class EthStratumClient : public PoolClient
7777
m_disconnecting.load(std::memory_order_relaxed));
7878
}
7979

80-
void submitHashrate(string const& rate, string const& id) override;
80+
void submitHashrate(uint64_t const& rate, string const& id) override;
8181
void submitSolution(const Solution& solution) override;
8282

8383
h256 currentHeaderHash() { return m_current.header; }
8484
bool current() { return static_cast<bool>(m_current); }
8585

8686
private:
87+
void startSession();
8788
void disconnect_finalize();
8889
void enqueue_response_plea();
8990
std::chrono::milliseconds dequeue_response_plea();

‎libpoolprotocols/testing/SimulateClient.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ SimulateClient::~SimulateClient() = default;
1818
void SimulateClient::connect()
1919
{
2020
// Initialize new session
21+
m_connected.store(true, memory_order_relaxed);
2122
m_session = unique_ptr<Session>(new Session);
2223
m_session->subscribed.store(true, memory_order_relaxed);
2324
m_session->authorized.store(true, memory_order_relaxed);
@@ -38,12 +39,13 @@ void SimulateClient::disconnect()
3839

3940
m_conn->addDuration(m_session->duration());
4041
m_session = nullptr;
42+
m_connected.store(false, memory_order_relaxed);
4143

4244
if (m_onDisconnected)
4345
m_onDisconnected();
4446
}
4547

46-
void SimulateClient::submitHashrate(string const& rate, string const& id)
48+
void SimulateClient::submitHashrate(uint64_t const& rate, string const& id)
4749
{
4850
(void)rate;
4951
(void)id;

‎libpoolprotocols/testing/SimulateClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class SimulateClient : public PoolClient, Worker
2525
bool isPendingState() override { return false; }
2626
string ActiveEndPoint() override { return ""; };
2727

28-
void submitHashrate(string const& rate, string const& id) override;
28+
void submitHashrate(uint64_t const& rate, string const& id) override;
2929
void submitSolution(const Solution& solution) override;
3030

3131
private:

0 commit comments

Comments
 (0)
This repository has been archived.