From 0ab26a16193e79d7ddf848e35d390fdae5469c23 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 18 Jul 2019 22:35:15 +0700 Subject: [PATCH] Restored algo field in login request. --- src/backend/cpu/CpuWorker.cpp | 4 ++ .../kernel/interfaces/IStrategyListener.h | 3 +- src/base/net/stratum/Client.cpp | 13 ----- .../stratum/strategies/FailoverStrategy.cpp | 8 +++- .../net/stratum/strategies/FailoverStrategy.h | 6 +-- .../stratum/strategies/SinglePoolStrategy.cpp | 8 +++- .../stratum/strategies/SinglePoolStrategy.h | 6 +-- src/core/Miner.cpp | 48 +++++++++++++++++-- src/core/Miner.h | 2 + src/crypto/common/Algorithm.h | 2 +- src/net/Network.cpp | 15 ++++++ src/net/Network.h | 1 + src/net/strategies/DonateStrategy.cpp | 43 ++++++++++++++--- src/net/strategies/DonateStrategy.h | 5 +- 14 files changed, 129 insertions(+), 35 deletions(-) diff --git a/src/backend/cpu/CpuWorker.cpp b/src/backend/cpu/CpuWorker.cpp index 4318b8ce3..e35c51553 100644 --- a/src/backend/cpu/CpuWorker.cpp +++ b/src/backend/cpu/CpuWorker.cpp @@ -174,6 +174,10 @@ void xmrig::CpuWorker::start() const Job &job = m_job.currentJob(); + if (job.algorithm().memory() != m_algorithm.memory()) { + break; + } + # ifdef XMRIG_ALGO_RANDOMX if (job.algorithm().family() == Algorithm::RANDOM_X) { randomx_calculate_hash(m_vm->get(), m_job.blob(), job.size(), m_hash); diff --git a/src/base/kernel/interfaces/IStrategyListener.h b/src/base/kernel/interfaces/IStrategyListener.h index 2e63449b6..01e668d40 100644 --- a/src/base/kernel/interfaces/IStrategyListener.h +++ b/src/base/kernel/interfaces/IStrategyListener.h @@ -26,7 +26,7 @@ #define XMRIG_ISTRATEGYLISTENER_H -#include +#include "rapidjson/fwd.h" namespace xmrig { @@ -45,6 +45,7 @@ public: virtual void onActive(IStrategy *strategy, IClient *client) = 0; virtual void onJob(IStrategy *strategy, IClient *client, const Job &job) = 0; + virtual void onLogin(IStrategy *strategy, IClient *client, rapidjson::Document &doc, rapidjson::Value ¶ms) = 0; virtual void onPause(IStrategy *strategy) = 0; virtual void onResultAccepted(IStrategy *strategy, IClient *client, const SubmitResult &result, const char *error) = 0; }; diff --git a/src/base/net/stratum/Client.cpp b/src/base/net/stratum/Client.cpp index c15195731..0be86ecae 100644 --- a/src/base/net/stratum/Client.cpp +++ b/src/base/net/stratum/Client.cpp @@ -575,19 +575,6 @@ void xmrig::Client::login() params.AddMember("rigid", m_pool.rigId().toJSON(), allocator); } -//# ifdef XMRIG_PROXY_PROJECT FIXME -// if (m_pool.algorithm().variant() != xmrig::VARIANT_AUTO) -//# endif -// { -// Value algo(kArrayType); - -// for (const auto &a : m_pool.algorithms()) { -// algo.PushBack(StringRef(a.shortName()), allocator); -// } - -// params.AddMember("algo", algo, allocator); -// } - m_listener->onLogin(this, doc, params); JsonRequest::create(doc, 1, "login", params); diff --git a/src/base/net/stratum/strategies/FailoverStrategy.cpp b/src/base/net/stratum/strategies/FailoverStrategy.cpp index 9545e9e16..4a35f3a54 100644 --- a/src/base/net/stratum/strategies/FailoverStrategy.cpp +++ b/src/base/net/stratum/strategies/FailoverStrategy.cpp @@ -113,7 +113,7 @@ void xmrig::FailoverStrategy::resume() } -void xmrig::FailoverStrategy::setAlgo(const xmrig::Algorithm &algo) +void xmrig::FailoverStrategy::setAlgo(const Algorithm &algo) { for (IClient *client : m_pools) { client->setAlgo(algo); @@ -163,6 +163,12 @@ void xmrig::FailoverStrategy::onClose(IClient *client, int failures) } +void xmrig::FailoverStrategy::onLogin(IClient *client, rapidjson::Document &doc, rapidjson::Value ¶ms) +{ + m_listener->onLogin(this, client, doc, params); +} + + void xmrig::FailoverStrategy::onJobReceived(IClient *client, const Job &job, const rapidjson::Value &) { if (m_active == client->id()) { diff --git a/src/base/net/stratum/strategies/FailoverStrategy.h b/src/base/net/stratum/strategies/FailoverStrategy.h index b1fe8bace..5336a634b 100644 --- a/src/base/net/stratum/strategies/FailoverStrategy.h +++ b/src/base/net/stratum/strategies/FailoverStrategy.h @@ -51,9 +51,8 @@ public: void add(const Pool &pool); protected: - inline bool isActive() const override { return m_active >= 0; } - inline IClient *client() const override { return isActive() ? active() : m_pools[m_index]; } - inline void onLogin(IClient *, rapidjson::Document &, rapidjson::Value &) override {} + inline bool isActive() const override { return m_active >= 0; } + inline IClient *client() const override { return isActive() ? active() : m_pools[m_index]; } int64_t submit(const JobResult &result) override; void connect() override; @@ -64,6 +63,7 @@ protected: void onClose(IClient *client, int failures) override; void onJobReceived(IClient *client, const Job &job, const rapidjson::Value ¶ms) override; + void onLogin(IClient *client, rapidjson::Document &doc, rapidjson::Value ¶ms) override; void onLoginSuccess(IClient *client) override; void onResultAccepted(IClient *client, const SubmitResult &result, const char *error) override; diff --git a/src/base/net/stratum/strategies/SinglePoolStrategy.cpp b/src/base/net/stratum/strategies/SinglePoolStrategy.cpp index 6c6a6fc12..5f09d174f 100644 --- a/src/base/net/stratum/strategies/SinglePoolStrategy.cpp +++ b/src/base/net/stratum/strategies/SinglePoolStrategy.cpp @@ -84,7 +84,7 @@ void xmrig::SinglePoolStrategy::resume() } -void xmrig::SinglePoolStrategy::setAlgo(const xmrig::Algorithm &algo) +void xmrig::SinglePoolStrategy::setAlgo(const Algorithm &algo) { m_client->setAlgo(algo); } @@ -119,6 +119,12 @@ void xmrig::SinglePoolStrategy::onJobReceived(IClient *client, const Job &job, c } +void xmrig::SinglePoolStrategy::onLogin(IClient *client, rapidjson::Document &doc, rapidjson::Value ¶ms) +{ + m_listener->onLogin(this, client, doc, params); +} + + void xmrig::SinglePoolStrategy::onLoginSuccess(IClient *client) { m_active = true; diff --git a/src/base/net/stratum/strategies/SinglePoolStrategy.h b/src/base/net/stratum/strategies/SinglePoolStrategy.h index af0bd7d65..04eef40ec 100644 --- a/src/base/net/stratum/strategies/SinglePoolStrategy.h +++ b/src/base/net/stratum/strategies/SinglePoolStrategy.h @@ -45,9 +45,8 @@ public: ~SinglePoolStrategy() override; protected: - inline bool isActive() const override { return m_active; } - inline IClient *client() const override { return m_client; } - inline void onLogin(IClient *, rapidjson::Document &, rapidjson::Value &) override {} + inline bool isActive() const override { return m_active; } + inline IClient *client() const override { return m_client; } int64_t submit(const JobResult &result) override; void connect() override; @@ -58,6 +57,7 @@ protected: void onClose(IClient *client, int failures) override; void onJobReceived(IClient *client, const Job &job, const rapidjson::Value ¶ms) override; + void onLogin(IClient *client, rapidjson::Document &doc, rapidjson::Value ¶ms) override; void onLoginSuccess(IClient *client) override; void onResultAccepted(IClient *client, const SubmitResult &result, const char *error) override; diff --git a/src/core/Miner.cpp b/src/core/Miner.cpp index 4135e8aba..891a0f34d 100644 --- a/src/core/Miner.cpp +++ b/src/core/Miner.cpp @@ -62,17 +62,46 @@ public: } + bool isEnabled(const Algorithm &algorithm) const + { + for (IBackend *backend : backends) { + if (backend->isEnabled(algorithm)) { + return true; + } + } + + return false; + } + + + inline void rebuild() + { + algorithms.clear(); + + for (int i = 0; i < Algorithm::MAX; ++i) { + const Algorithm algo(static_cast(i)); + + if (isEnabled(algo)) { + algorithms.push_back(algo); + } + } + } + + inline void handleJobChange() { active = true; - if (enabled) { - Nonce::pause(false);; - } for (IBackend *backend : backends) { backend->setJob(job); } + if (enabled) { + Nonce::pause(false);; + } + + Nonce::reset(job.index()); + if (ticks == 0) { ticks++; timer->start(500, 500); @@ -80,6 +109,7 @@ public: } + Algorithms algorithms; bool active = false; bool enabled = true; Controller *controller; @@ -104,6 +134,8 @@ xmrig::Miner::Miner(Controller *controller) d_ptr->timer = new Timer(this); d_ptr->backends.push_back(new CpuBackend(controller)); + + d_ptr->rebuild(); } @@ -119,6 +151,12 @@ bool xmrig::Miner::isEnabled() const } +const xmrig::Algorithms &xmrig::Miner::algorithms() const +{ + return d_ptr->algorithms; +} + + const std::vector &xmrig::Miner::backends() const { return d_ptr->backends; @@ -202,8 +240,6 @@ void xmrig::Miner::setJob(const Job &job, bool donate) d_ptr->job = job; d_ptr->job.setIndex(index); - Nonce::reset(index); - uv_rwlock_wrunlock(&d_ptr->rwlock); d_ptr->handleJobChange(); @@ -222,6 +258,8 @@ void xmrig::Miner::stop() void xmrig::Miner::onConfigChanged(Config *config, Config *previousConfig) { + d_ptr->rebuild(); + if (config->pools() != previousConfig->pools() && config->pools().active() > 0) { return; } diff --git a/src/core/Miner.h b/src/core/Miner.h index 23497eae5..dd195e29c 100644 --- a/src/core/Miner.h +++ b/src/core/Miner.h @@ -31,6 +31,7 @@ #include "base/kernel/interfaces/IBaseListener.h" #include "base/kernel/interfaces/ITimerListener.h" +#include "crypto/common/Algorithm.h" namespace xmrig { @@ -49,6 +50,7 @@ public: ~Miner() override; bool isEnabled() const; + const Algorithms &algorithms() const; const std::vector &backends() const; Job job() const; void pause(); diff --git a/src/crypto/common/Algorithm.h b/src/crypto/common/Algorithm.h index a6ec22bef..a1d8ded2b 100644 --- a/src/crypto/common/Algorithm.h +++ b/src/crypto/common/Algorithm.h @@ -115,7 +115,7 @@ private: }; -typedef std::vector Algorithms; +typedef std::vector Algorithms; } /* namespace xmrig */ diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 6622a0804..a1c1ec8d0 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -154,6 +154,21 @@ void xmrig::Network::onJobResult(const JobResult &result) } +void xmrig::Network::onLogin(IStrategy *, IClient *, rapidjson::Document &doc, rapidjson::Value ¶ms) +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + + Value algo(kArrayType); + + for (const auto &a : m_controller->miner()->algorithms()) { + algo.PushBack(StringRef(a.shortName()), allocator); + } + + params.AddMember("algo", algo, allocator); +} + + void xmrig::Network::onPause(IStrategy *strategy) { if (m_donate && m_donate == strategy) { diff --git a/src/net/Network.h b/src/net/Network.h index eaec9472f..bf61a9b67 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -63,6 +63,7 @@ protected: void onConfigChanged(Config *config, Config *previousConfig) override; void onJob(IStrategy *strategy, IClient *client, const Job &job) override; void onJobResult(const JobResult &result) override; + void onLogin(IStrategy *strategy, IClient *client, rapidjson::Document &doc, rapidjson::Value ¶ms) override; void onPause(IStrategy *strategy) override; void onRequest(IApiRequest &request) override; void onResultAccepted(IStrategy *strategy, IClient *client, const SubmitResult &result, const char *error) override; diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 2d0a5b43e..4393cd463 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -23,7 +23,9 @@ */ +#include #include +#include #include "base/kernel/Platform.h" @@ -35,6 +37,7 @@ #include "base/tools/Timer.h" #include "core/config/Config.h" #include "core/Controller.h" +#include "core/Miner.h" #include "crypto/common/keccak.h" #include "net/Network.h" #include "net/strategies/DonateStrategy.h" @@ -57,10 +60,10 @@ static const char *kDonateHostTls = "donate.ssl.xmrig.com"; xmrig::DonateStrategy::DonateStrategy(Controller *controller, IStrategyListener *listener) : m_tls(false), m_userId(), - m_proxy(nullptr), m_donateTime(static_cast(controller->config()->pools().donateLevel()) * 60 * 1000), m_idleTime((100 - static_cast(controller->config()->pools().donateLevel())) * 60 * 1000), m_controller(controller), + m_proxy(nullptr), m_strategy(nullptr), m_listener(listener), m_state(STATE_NEW), @@ -78,10 +81,6 @@ xmrig::DonateStrategy::DonateStrategy(Controller *controller, IStrategyListener # endif m_pools.push_back(Pool(kDonateHost, 3333, m_userId, nullptr, 0, true)); -// for (Pool &pool : m_pools) { -// pool.adjust(Algorithm()); // FIXME -// } - if (m_pools.size() > 1) { m_strategy = new FailoverStrategy(m_pools, 1, 2, this, true); } @@ -129,6 +128,8 @@ void xmrig::DonateStrategy::connect() void xmrig::DonateStrategy::setAlgo(const xmrig::Algorithm &algo) { + m_algorithm = algo; + m_strategy->setAlgo(algo); } @@ -185,13 +186,14 @@ void xmrig::DonateStrategy::onClose(IClient *, int failures) void xmrig::DonateStrategy::onLogin(IClient *, rapidjson::Document &doc, rapidjson::Value ¶ms) { + using namespace rapidjson; auto &allocator = doc.GetAllocator(); # ifdef XMRIG_FEATURE_TLS if (m_tls) { char buf[40] = { 0 }; snprintf(buf, sizeof(buf), "stratum+ssl://%s", m_pools[0].url().data()); - params.AddMember("url", rapidjson::Value(buf, allocator), allocator); + params.AddMember("url", Value(buf, allocator), allocator); } else { params.AddMember("url", m_pools[1].url().toJSON(), allocator); @@ -199,6 +201,14 @@ void xmrig::DonateStrategy::onLogin(IClient *, rapidjson::Document &doc, rapidjs # else params.AddMember("url", m_pools[0].url().toJSON(), allocator); # endif + + setAlgorithms(doc, params); +} + + +void xmrig::DonateStrategy::onLogin(IStrategy *, IClient *, rapidjson::Document &doc, rapidjson::Value ¶ms) +{ + setAlgorithms(doc, params); } @@ -250,6 +260,27 @@ void xmrig::DonateStrategy::idle(double min, double max) } +void xmrig::DonateStrategy::setAlgorithms(rapidjson::Document &doc, rapidjson::Value ¶ms) +{ + using namespace rapidjson; + auto &allocator = doc.GetAllocator(); + + Algorithms algorithms = m_controller->miner()->algorithms(); + const size_t index = static_cast(std::distance(algorithms.begin(), std::find(algorithms.begin(), algorithms.end(), m_algorithm))); + if (index > 0 && index < algorithms.size()) { + std::swap(algorithms[0], algorithms[index]); + } + + Value algo(kArrayType); + + for (const auto &a : algorithms) { + algo.PushBack(StringRef(a.shortName()), allocator); + } + + params.AddMember("algo", algo, allocator); +} + + void xmrig::DonateStrategy::setJob(IClient *client, const Job &job) { if (isActive()) { diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index c9fc312d8..5350aefa3 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -70,6 +70,7 @@ protected: void onClose(IClient *client, int failures) override; void onLogin(IClient *client, rapidjson::Document &doc, rapidjson::Value ¶ms) override; + void onLogin(IStrategy *strategy, IClient *client, rapidjson::Document &doc, rapidjson::Value ¶ms) override; void onLoginSuccess(IClient *client) override; void onTimer(const Timer *timer) override; @@ -87,16 +88,18 @@ private: Client *createProxy(); void idle(double min, double max); + void setAlgorithms(rapidjson::Document &doc, rapidjson::Value ¶ms); void setJob(IClient *client, const Job &job); void setResult(IClient *client, const SubmitResult &result, const char *error); void setState(State state); + Algorithm m_algorithm; bool m_tls; char m_userId[65]; - IClient *m_proxy; const uint64_t m_donateTime; const uint64_t m_idleTime; Controller *m_controller; + IClient *m_proxy; IStrategy *m_strategy; IStrategyListener *m_listener; State m_state;