From bb3990a07694e31a3a1f490680ebc527b1ff2994 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 3 Sep 2018 15:39:55 +0300 Subject: [PATCH] Rewrite automatic variant handling. --- src/common/net/Client.cpp | 6 ++--- src/common/net/Job.cpp | 46 +++++++++++++++++++++++-------------- src/common/net/Job.h | 10 +++++--- src/net/Network.cpp | 5 +--- src/workers/MultiWorker.cpp | 2 +- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/common/net/Client.cpp b/src/common/net/Client.cpp index f4553d973..2a9db4445 100644 --- a/src/common/net/Client.cpp +++ b/src/common/net/Client.cpp @@ -270,17 +270,17 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) } if (params.HasMember("algo")) { - job.algorithm().parseAlgorithm(params["algo"].GetString()); + job.setAlgorithm(params["algo"].GetString()); } if (params.HasMember("variant")) { const rapidjson::Value &variant = params["variant"]; if (variant.IsInt()) { - job.algorithm().parseVariant(variant.GetInt()); + job.setVariant(variant.GetInt()); } else if (variant.IsString()){ - job.algorithm().parseVariant(variant.GetString()); + job.setVariant(variant.GetString()); } } diff --git a/src/common/net/Job.cpp b/src/common/net/Job.cpp index 3d2d88ad3..bb5f6a12c 100644 --- a/src/common/net/Job.cpp +++ b/src/common/net/Job.cpp @@ -59,6 +59,7 @@ static inline char hf_bin2hex(unsigned char c) Job::Job() : + m_autoVariant(false), m_nicehash(false), m_poolId(-2), m_threadId(-1), @@ -70,7 +71,8 @@ Job::Job() : } -Job::Job(int poolId, bool nicehash, xmrig::Algorithm algorithm, const xmrig::Id &clientId) : +Job::Job(int poolId, bool nicehash, const xmrig::Algorithm &algorithm, const xmrig::Id &clientId) : + m_autoVariant(algorithm.variant() == xmrig::VARIANT_AUTO), m_nicehash(nicehash), m_poolId(poolId), m_threadId(-1), @@ -113,6 +115,10 @@ bool Job::setBlob(const char *blob) m_nicehash = true; } + if (m_autoVariant) { + m_algorithm.setVariant(variant()); + } + # ifdef XMRIG_PROXY_PROJECT memset(m_rawBlob, 0, sizeof(m_rawBlob)); memcpy(m_rawBlob, blob, m_size * 2); @@ -164,22 +170,6 @@ bool Job::setTarget(const char *target) } -xmrig::Variant Job::variant() const -{ - if (m_algorithm.variant() == xmrig::VARIANT_AUTO) { - if (m_algorithm.algo() == xmrig::CRYPTONIGHT_HEAVY) { - return xmrig::VARIANT_0; - } else if (m_algorithm.algo() == xmrig::CRYPTONIGHT_LITE) { - return xmrig::VARIANT_1; - } - - return (m_blob[0] >= 8) ? xmrig::VARIANT_2 : xmrig::VARIANT_1; - } - - return m_algorithm.variant(); -} - - bool Job::fromHex(const char* in, unsigned int len, unsigned char* out) { bool error = false; @@ -224,3 +214,25 @@ bool Job::operator!=(const Job &other) const { return m_id != other.m_id || memcmp(m_blob, other.m_blob, sizeof(m_blob)) != 0; } + + +xmrig::Variant Job::variant() const +{ + using namespace xmrig; + + switch (m_algorithm.algo()) { + case CRYPTONIGHT: + return (m_blob[0] >= 8) ? VARIANT_2 : VARIANT_1; + + case CRYPTONIGHT_LITE: + return VARIANT_1; + + case CRYPTONIGHT_HEAVY: + return VARIANT_0; + + default: + break; + } + + return m_algorithm.variant(); +} diff --git a/src/common/net/Job.h b/src/common/net/Job.h index 7ea539a22..8bd1b8adc 100644 --- a/src/common/net/Job.h +++ b/src/common/net/Job.h @@ -39,12 +39,11 @@ class Job { public: Job(); - Job(int poolId, bool nicehash, xmrig::Algorithm algorithm, const xmrig::Id &clientId); + Job(int poolId, bool nicehash, const xmrig::Algorithm &algorithm, const xmrig::Id &clientId); ~Job(); bool setBlob(const char *blob); bool setTarget(const char *target); - xmrig::Variant variant() const; inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_size > 0 && m_diff > 0; } @@ -61,10 +60,12 @@ public: inline uint32_t diff() const { return static_cast(m_diff); } inline uint64_t target() const { return m_target; } inline void reset() { m_size = 0; m_diff = 0; } + inline void setAlgorithm(const char *algo) { m_algorithm.parseAlgorithm(algo); } inline void setClientId(const xmrig::Id &id) { m_clientId = id; } inline void setPoolId(int poolId) { m_poolId = poolId; } inline void setThreadId(int threadId) { m_threadId = threadId; } - inline xmrig::Algorithm &algorithm() { return m_algorithm; } + inline void setVariant(const char *variant) { m_algorithm.parseVariant(variant); } + inline void setVariant(int variant) { m_algorithm.parseVariant(variant); } # ifdef XMRIG_PROXY_PROJECT inline char *rawBlob() { return m_rawBlob; } @@ -84,6 +85,9 @@ public: bool operator!=(const Job &other) const; private: + xmrig::Variant variant() const; + + bool m_autoVariant; bool m_nicehash; int m_poolId; int m_threadId; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index cc979635b..7293a0ac9 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -166,12 +166,9 @@ bool Network::isColors() const void Network::setJob(Client *client, const Job &job, bool donate) { - xmrig::Algorithm algorithm = job.algorithm(); - algorithm.setVariant(job.variant()); - LOG_INFO(isColors() ? MAGENTA_BOLD("new job") " from " WHITE_BOLD("%s:%d") " diff " WHITE_BOLD("%d") " algo " WHITE_BOLD("%s") : "new job from %s:%d diff %d algo %s", - client->host(), client->port(), job.diff(), algorithm.shortName()); + client->host(), client->port(), job.diff(), job.algorithm().shortName()); m_state.diff = job.diff(); Workers::setJob(job, donate); diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 1916b205c..a6dbc73ac 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -104,7 +104,7 @@ void MultiWorker::start() storeStats(); } - m_thread->fn(m_state.job.variant())(m_state.blob, m_state.job.size(), m_hash, m_ctx); + m_thread->fn(m_state.job.algorithm().variant())(m_state.blob, m_state.job.size(), m_hash, m_ctx); for (size_t i = 0; i < N; ++i) { if (*reinterpret_cast(m_hash + (i * 32) + 24) < m_state.job.target()) {