From 7e17f77c1158fca71cd9ee007fcaf7157fd7d330 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 30 Jun 2017 03:20:50 +0300 Subject: [PATCH] Implemented DonateStrategy. --- src/interfaces/IStrategy.h | 1 + src/net/Client.cpp | 32 ++++++++++++++---- src/net/Client.h | 2 ++ src/net/Network.cpp | 21 ++++-------- src/net/strategies/DonateStrategy.cpp | 40 ++++++++++++++++++----- src/net/strategies/DonateStrategy.h | 9 ++++- src/net/strategies/SinglePoolStrategy.cpp | 16 +++++---- src/net/strategies/SinglePoolStrategy.h | 4 ++- 8 files changed, 88 insertions(+), 37 deletions(-) diff --git a/src/interfaces/IStrategy.h b/src/interfaces/IStrategy.h index eddf300b3..78f3f6236 100644 --- a/src/interfaces/IStrategy.h +++ b/src/interfaces/IStrategy.h @@ -35,6 +35,7 @@ public: virtual bool isActive() const = 0; virtual void connect() = 0; + virtual void resume() = 0; virtual void submit(const JobResult &result) = 0; }; diff --git a/src/net/Client.cpp b/src/net/Client.cpp index 86928ed0e..e9fcb0a78 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -38,6 +38,7 @@ Client::Client(int id, const char *agent, IClientListener *listener) : + m_quiet(false), m_agent(agent), m_listener(listener), m_id(id), @@ -93,6 +94,7 @@ void Client::connect(const Url *url) void Client::disconnect() { + uv_timer_stop(&m_retriesTimer); m_failures = -1; close(); @@ -207,10 +209,13 @@ int Client::resolve(const char *host) setState(HostLookupState); m_recvBufPos = 0; + m_failures = 0; const int r = uv_getaddrinfo(uv_default_loop(), &m_resolver, Client::onResolved, host, NULL, &m_hints); if (r) { - LOG_ERR("[%s:%u] getaddrinfo error: \"%s\"", host, m_url.port(), uv_strerror(r)); + if (!m_quiet) { + LOG_ERR("[%s:%u] getaddrinfo error: \"%s\"", host, m_url.port(), uv_strerror(r)); + } return 1; } @@ -277,7 +282,9 @@ void Client::parse(char *line, size_t len) json_t *val = json_loads(line, 0, &err); if (!val) { - LOG_ERR("[%s:%u] JSON decode failed: \"%s\"", m_url.host(), m_url.port(), err.text); + if (!m_quiet) { + LOG_ERR("[%s:%u] JSON decode failed: \"%s\"", m_url.host(), m_url.port(), err.text); + } return; } @@ -296,7 +303,9 @@ void Client::parse(char *line, size_t len) void Client::parseNotification(const char *method, const json_t *params, const json_t *error) { if (json_is_object(error)) { - LOG_ERR("[%s:%u] error: \"%s\", code: %lld", m_url.host(), m_url.port(), json_string_value(json_object_get(error, "message")), json_integer_value(json_object_get(error, "code"))); + if (!m_quiet) { + LOG_ERR("[%s:%u] error: \"%s\", code: %lld", m_url.host(), m_url.port(), json_string_value(json_object_get(error, "message")), json_integer_value(json_object_get(error, "code"))); + } return; } @@ -321,7 +330,10 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error { if (json_is_object(error)) { const char *message = json_string_value(json_object_get(error, "message")); - LOG_ERR("[%s:%u] error: \"%s\", code: %lld", m_url.host(), m_url.port(), message, json_integer_value(json_object_get(error, "code"))); + + if (!m_quiet) { + LOG_ERR("[%s:%u] error: \"%s\", code: %lld", m_url.host(), m_url.port(), message, json_integer_value(json_object_get(error, "code"))); + } if (id == 1 || (message && strncasecmp(message, "Unauthenticated", 15) == 0)) { close(); @@ -337,7 +349,10 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error if (id == 1) { int code = -1; if (!parseLogin(result, &code)) { - LOG_ERR("[%s:%u] login error code: %d", m_url.host(), m_url.port(), code); + if (!m_quiet) { + LOG_ERR("[%s:%u] login error code: %d", m_url.host(), m_url.port(), code); + } + return close(); } @@ -426,7 +441,10 @@ void Client::onConnect(uv_connect_t *req, int status) { auto client = getClient(req->data); if (status < 0) { - LOG_ERR("[%s:%u] connect error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status)); + if (!client->m_quiet) { + LOG_ERR("[%s:%u] connect error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(status)); + } + free(req); client->close(); return; @@ -447,7 +465,7 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { auto client = getClient(stream->data); if (nread < 0) { - if (nread != UV_EOF) { + if (nread != UV_EOF && !client->m_quiet) { LOG_ERR("[%s:%u] read error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror(nread)); } diff --git a/src/net/Client.h b/src/net/Client.h index a4ff846d2..65fdc42a6 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -67,6 +67,7 @@ public: inline int id() const { return m_id; } inline SocketState state() const { return m_state; } inline uint16_t port() const { return m_url.port(); } + inline void setQuiet(bool quiet) { m_quiet = quiet; } inline void setRetryPause(int ms) { m_retryPause = ms; } private: @@ -94,6 +95,7 @@ private: static Client *getClient(void *data); + bool m_quiet; char m_rpcId[64]; const char *m_agent; IClientListener *m_listener; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 364431046..8e064e9a1 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -147,9 +147,14 @@ void Network::onJobResult(const JobResult &result) void Network::onPause(IStrategy *strategy) { - if ((m_donate && !m_donate->isActive()) || !m_strategy->isActive()) { + if (m_donate && m_donate == strategy) { + LOG_NOTICE("dev donate finished"); + m_strategy->resume(); + } + + if (!m_strategy->isActive()) { LOG_ERR("no active pools, pause mining"); - Workers::pause(); + return Workers::pause(); } } @@ -168,18 +173,6 @@ void Network::setJob(Client *client, const Job &job) } -void Network::startDonate() -{ - if (m_donateActive) { - return; - } - - LOG_NOTICE("dev donate started"); - - m_donateActive = true; -} - - void Network::stopDonate() { // if (!m_donateActive) { diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 9b7fb12ba..a3c595ed9 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -30,6 +30,8 @@ DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) : m_active(false), + m_donateTime(Options::i()->donateLevel() * 60 * 1000), + m_idleTime((100 - Options::i()->donateLevel()) * 60 * 1000), m_listener(listener) { Url *url = new Url("donate.xmrig.com", Options::i()->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 443, Options::i()->pools().front()->user()); @@ -37,24 +39,20 @@ DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) : m_client = new Client(-1, agent, this); m_client->setUrl(url); m_client->setRetryPause(Options::i()->retryPause() * 1000); + m_client->setQuiet(true); delete url; m_timer.data = this; uv_timer_init(uv_default_loop(), &m_timer); - uv_timer_start(&m_timer, DonateStrategy::onTimer, (100 - Options::i()->donateLevel()) * 60 * 1000, 0); -} - - -bool DonateStrategy::isActive() const -{ - return m_active; + idle(); } void DonateStrategy::connect() { + m_client->connect(); } @@ -66,7 +64,6 @@ void DonateStrategy::submit(const JobResult &result) void DonateStrategy::onClose(Client *client, int failures) { - } @@ -78,12 +75,39 @@ void DonateStrategy::onJobReceived(Client *client, const Job &job) void DonateStrategy::onLoginSuccess(Client *client) { + if (!isActive()) { + uv_timer_start(&m_timer, DonateStrategy::onTimer, m_donateTime, 0); + } + m_active = true; m_listener->onActive(client); } +void DonateStrategy::idle() +{ + uv_timer_start(&m_timer, DonateStrategy::onTimer, m_idleTime, 0); +} + + +void DonateStrategy::stop() +{ + m_client->disconnect(); + + m_active = false; + m_listener->onPause(this); + + idle(); +} + + void DonateStrategy::onTimer(uv_timer_t *handle) { + auto strategy = static_cast(handle->data); + if (!strategy->isActive()) { + return strategy->connect(); + } + + strategy->stop(); } diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index 128757cab..33cdf5364 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -43,7 +43,9 @@ public: DonateStrategy(const char *agent, IStrategyListener *listener); public: - bool isActive() const override; + inline bool isActive() const override { return m_active; } + inline void resume() override {} + void connect() override; void submit(const JobResult &result) override; @@ -53,10 +55,15 @@ protected: void onLoginSuccess(Client *client) override; private: + void idle(); + void stop(); + static void onTimer(uv_timer_t *handle); bool m_active; Client *m_client; + const int m_donateTime; + const int m_idleTime; IStrategyListener *m_listener; uv_timer_t m_timer; }; diff --git a/src/net/strategies/SinglePoolStrategy.cpp b/src/net/strategies/SinglePoolStrategy.cpp index c7d2a8d03..c1d1a08ff 100644 --- a/src/net/strategies/SinglePoolStrategy.cpp +++ b/src/net/strategies/SinglePoolStrategy.cpp @@ -38,18 +38,22 @@ SinglePoolStrategy::SinglePoolStrategy(const Url *url, const char *agent, IStrat } -bool SinglePoolStrategy::isActive() const -{ - return m_active; -} - - void SinglePoolStrategy::connect() { m_client->connect(); } +void SinglePoolStrategy::resume() +{ + if (!isActive()) { + return; + } + + m_listener->onJob(m_client, m_client->job()); +} + + void SinglePoolStrategy::submit(const JobResult &result) { m_client->submit(result); diff --git a/src/net/strategies/SinglePoolStrategy.h b/src/net/strategies/SinglePoolStrategy.h index fb8eca60d..b59398593 100644 --- a/src/net/strategies/SinglePoolStrategy.h +++ b/src/net/strategies/SinglePoolStrategy.h @@ -40,8 +40,10 @@ public: SinglePoolStrategy(const Url *url, const char *agent, IStrategyListener *listener); public: - bool isActive() const override; + inline bool isActive() const override { return m_active; } + void connect() override; + void resume() override; void submit(const JobResult &result) override; protected: