From f6699b5929aa24f14a215632d5ee0334f5e02c16 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 17 Feb 2019 08:53:37 +0700 Subject: [PATCH] [WIP] Use unified Pools class. --- src/base/net/Pool.cpp | 12 ++++ src/base/net/Pool.h | 1 + src/base/net/Pools.cpp | 53 ++++++++++++++- src/base/net/Pools.h | 20 +++++- src/common/config/CommonConfig.cpp | 106 ++++++----------------------- src/common/config/CommonConfig.h | 7 +- src/core/Config.cpp | 2 +- 7 files changed, 107 insertions(+), 94 deletions(-) diff --git a/src/base/net/Pool.cpp b/src/base/net/Pool.cpp index b81593b4..cacf3494 100644 --- a/src/base/net/Pool.cpp +++ b/src/base/net/Pool.cpp @@ -155,6 +155,18 @@ bool xmrig::Pool::isCompatible(const Algorithm &algorithm) const } +bool xmrig::Pool::isEnabled() const +{ +# ifdef XMRIG_NO_TLS + if (isTLS()) { + return false; + } +# endif + + return isValid() && algorithm().isValid(); +} + + bool xmrig::Pool::isEqual(const Pool &other) const { return (m_nicehash == other.m_nicehash diff --git a/src/base/net/Pool.h b/src/base/net/Pool.h index 06ccf2bc..ec051277 100644 --- a/src/base/net/Pool.h +++ b/src/base/net/Pool.h @@ -83,6 +83,7 @@ public: inline bool operator==(const Pool &other) const { return isEqual(other); } bool isCompatible(const Algorithm &algorithm) const; + bool isEnabled() const; bool isEqual(const Pool &other) const; bool parse(const char *url); bool setUserpass(const char *userpass); diff --git a/src/base/net/Pools.cpp b/src/base/net/Pools.cpp index 49a1607c..6ebafdc3 100644 --- a/src/base/net/Pools.cpp +++ b/src/base/net/Pools.cpp @@ -26,7 +26,56 @@ #include "base/net/Pools.h" -xmrig::Pools::Pools() : - m_index(0) +xmrig::Pools::Pools() { } + + +xmrig::Pool &xmrig::Pools::current() +{ + if (m_data.empty()) { + m_data.push_back(Pool()); + } + + return m_data.back(); +} + + +bool xmrig::Pools::setUrl(const char *url) +{ + if (m_data.empty() || m_data.back().isValid()) { + Pool pool(url); + + if (pool.isValid()) { + m_data.push_back(pool); + return true; + } + + return false; + } + + current().parse(url); + + return m_data.back().isValid(); +} + + +size_t xmrig::Pools::active() const +{ + size_t count = 0; + for (const Pool &pool : m_data) { + if (pool.isEnabled()) { + count++; + } + } + + return count; +} + + +void xmrig::Pools::adjust(const Algorithm &algorithm) +{ + for (Pool &pool : m_data) { + pool.adjust(algorithm); + } +} diff --git a/src/base/net/Pools.h b/src/base/net/Pools.h index 1f8a8c77..d882a6b3 100644 --- a/src/base/net/Pools.h +++ b/src/base/net/Pools.h @@ -40,8 +40,26 @@ class Pools public: Pools(); + inline bool setUserpass(const char *userpass) { return current().setUserpass(userpass); } + inline const std::vector &data() const { return m_data; } + inline void setFingerprint(const char *fingerprint) { current().setFingerprint(fingerprint); } + inline void setKeepAlive(bool enable) { setKeepAlive(enable ? Pool::kKeepAliveTimeout : 0); } + inline void setKeepAlive(int keepAlive) { current().setKeepAlive(keepAlive); } + inline void setNicehash(bool enable) { current().setNicehash(enable); } + inline void setPassword(const char *password) { current().setPassword(password); } + inline void setRigId(const char *rigId) { current().setRigId(rigId); } + inline void setTLS(bool enable) { current().setTLS(enable); } + inline void setUser(const char *user) { current().setUser(user); } + inline void setVariant(const char *variant) { current().algorithm().parseVariant(variant); } + inline void setVariant(int variant) { current().algorithm().parseVariant(variant); } + + bool setUrl(const char *url); + size_t active() const; + void adjust(const Algorithm &algorithm); + private: - size_t m_index; + Pool ¤t(); + std::vector m_data; }; diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 604ab089..10a7b03a 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -82,8 +82,6 @@ xmrig::CommonConfig::CommonConfig() : m_retryPause(5), m_state(NoneState) { - m_pools.push_back(Pool()); - # ifdef XMRIG_PROXY_PROJECT m_retries = 2; m_retryPause = 1; @@ -107,21 +105,21 @@ void xmrig::CommonConfig::printAPI() void xmrig::CommonConfig::printPools() { - for (size_t i = 0; i < m_activePools.size(); ++i) { + for (size_t i = 0; i < m_pools.data().size(); ++i) { if (!isColors()) { Log::i()->text(" * POOL #%-7zu%s algo=%s, TLS=%d", i + 1, - m_activePools[i].url(), - m_activePools[i].algorithm().shortName(), - static_cast(m_activePools[i].isTLS()) + m_pools.data()[i].url(), + m_pools.data()[i].algorithm().shortName(), + static_cast(m_pools.data()[i].isTLS()) ); } else { Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("POOL #%-7zu") "\x1B[1;%dm%s\x1B[0m algo " WHITE_BOLD("%s"), i + 1, - m_activePools[i].isTLS() ? 32 : 36, - m_activePools[i].url(), - m_activePools[i].algorithm().shortName() + m_pools.data()[i].isTLS() ? 32 : 36, + m_pools.data()[i].url(), + m_pools.data()[i].algorithm().shortName() ); } } @@ -225,23 +223,9 @@ bool xmrig::CommonConfig::finalize() return false; } - for (Pool &pool : m_pools) { - pool.adjust(m_algorithm); + m_pools.adjust(m_algorithm); - if (pool.isValid() && pool.algorithm().isValid()) { -# ifdef XMRIG_NO_TLS - if (pool.isTLS()) { - continue; - } -# endif - - m_activePools.push_back(std::move(pool)); - } - } - - m_pools.clear(); - - if (m_activePools.empty()) { + if (!m_pools.active()) { m_state = ErrorState; return false; } @@ -263,16 +247,16 @@ bool xmrig::CommonConfig::parseBoolean(int key, bool enable) break; case KeepAliveKey: /* --keepalive */ - currentPool().setKeepAlive(enable ? Pool::kKeepAliveTimeout : 0); + m_pools.setKeepAlive(enable); break; case TlsKey: /* --tls */ - currentPool().setTLS(enable); + m_pools.setTLS(enable); break; # ifndef XMRIG_PROXY_PROJECT case NicehashKey: /* --nicehash */ - currentPool().setNicehash(enable); + m_pools.setNicehash(enable); break; # endif @@ -316,50 +300,29 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg) break; case UserpassKey: /* --userpass */ - if (!currentPool().setUserpass(arg)) { - return false; - } - - break; + return m_pools.setUserpass(arg); case UrlKey: /* --url */ - fixup(); - - if (m_pools.size() > 1 || m_pools[0].isValid()) { - Pool pool(arg); - - if (pool.isValid()) { - m_pools.push_back(std::move(pool)); - } - } - else { - m_pools[0].parse(arg); - } - - if (!m_pools.back().isValid()) { - return false; - } - - break; + return m_pools.setUrl(arg); case UserKey: /* --user */ - currentPool().setUser(arg); + m_pools.setUser(arg); break; case PasswordKey: /* --pass */ - currentPool().setPassword(arg); + m_pools.setPassword(arg); break; case RigIdKey: /* --rig-id */ - currentPool().setRigId(arg); + m_pools.setRigId(arg); break; case FingerprintKey: /* --tls-fingerprint */ - currentPool().setFingerprint(arg); + m_pools.setFingerprint(arg); break; case VariantKey: /* --variant */ - currentPool().algorithm().parseVariant(arg); + m_pools.setVariant(arg); break; case LogFileKey: /* --log-file */ @@ -453,11 +416,11 @@ bool xmrig::CommonConfig::parseInt(int key, int arg) break; case KeepAliveKey: /* --keepalive */ - currentPool().setKeepAlive(arg); + m_pools.setKeepAlive(arg); break; case VariantKey: /* --variant */ - currentPool().algorithm().parseVariant(arg); + m_pools.setVariant(arg); break; case DonateLevelKey: /* --donate-level */ @@ -484,30 +447,3 @@ bool xmrig::CommonConfig::parseInt(int key, int arg) return true; } - - -xmrig::Pool &xmrig::CommonConfig::currentPool() -{ - fixup(); - - return m_pools.back(); -} - - -void xmrig::CommonConfig::fixup() -{ - if (m_state == NoneState) { - return; - } - - if (m_pools.empty()) { - if (!m_activePools.empty()) { - std::swap(m_pools, m_activePools); - } - else { - m_pools.push_back(Pool()); - } - - m_state = NoneState; - } -} diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index 6316a078..9c6c32bb 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -52,7 +52,7 @@ public: inline const char *apiWorkerId() const { return m_apiWorkerId.data(); } inline const char *logFile() const { return m_logFile.data(); } inline const char *userAgent() const { return m_userAgent.data(); } - inline const std::vector &pools() const { return m_activePools; } + inline const std::vector &pools() const { return m_pools.data(); } inline int apiPort() const { return m_apiPort; } inline int donateLevel() const { return m_donateLevel; } inline int printTime() const { return m_printTime; } @@ -99,9 +99,8 @@ protected: int m_printTime; int m_retries; int m_retryPause; + Pools m_pools; State m_state; - std::vector m_activePools; - std::vector m_pools; String m_apiId; String m_apiToken; String m_apiWorkerId; @@ -111,8 +110,6 @@ protected: private: bool parseInt(int key, int arg); - Pool ¤tPool(); - void fixup(); }; diff --git a/src/core/Config.cpp b/src/core/Config.cpp index f3487b15..6ff19588 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -106,7 +106,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const Value pools(kArrayType); - for (const Pool &pool : m_activePools) { + for (const Pool &pool : m_pools.data()) { pools.PushBack(pool.toJSON(doc), allocator); }