[WIP] Use unified Pools class.

This commit is contained in:
XMRig 2019-02-17 08:53:37 +07:00
parent ee667144e8
commit f6699b5929
7 changed files with 107 additions and 94 deletions

View file

@ -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 bool xmrig::Pool::isEqual(const Pool &other) const
{ {
return (m_nicehash == other.m_nicehash return (m_nicehash == other.m_nicehash

View file

@ -83,6 +83,7 @@ public:
inline bool operator==(const Pool &other) const { return isEqual(other); } inline bool operator==(const Pool &other) const { return isEqual(other); }
bool isCompatible(const Algorithm &algorithm) const; bool isCompatible(const Algorithm &algorithm) const;
bool isEnabled() const;
bool isEqual(const Pool &other) const; bool isEqual(const Pool &other) const;
bool parse(const char *url); bool parse(const char *url);
bool setUserpass(const char *userpass); bool setUserpass(const char *userpass);

View file

@ -26,7 +26,56 @@
#include "base/net/Pools.h" #include "base/net/Pools.h"
xmrig::Pools::Pools() : xmrig::Pools::Pools()
m_index(0)
{ {
} }
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);
}
}

View file

@ -40,8 +40,26 @@ class Pools
public: public:
Pools(); Pools();
inline bool setUserpass(const char *userpass) { return current().setUserpass(userpass); }
inline const std::vector<Pool> &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: private:
size_t m_index; Pool &current();
std::vector<Pool> m_data; std::vector<Pool> m_data;
}; };

View file

@ -82,8 +82,6 @@ xmrig::CommonConfig::CommonConfig() :
m_retryPause(5), m_retryPause(5),
m_state(NoneState) m_state(NoneState)
{ {
m_pools.push_back(Pool());
# ifdef XMRIG_PROXY_PROJECT # ifdef XMRIG_PROXY_PROJECT
m_retries = 2; m_retries = 2;
m_retryPause = 1; m_retryPause = 1;
@ -107,21 +105,21 @@ void xmrig::CommonConfig::printAPI()
void xmrig::CommonConfig::printPools() 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()) { if (!isColors()) {
Log::i()->text(" * POOL #%-7zu%s algo=%s, TLS=%d", Log::i()->text(" * POOL #%-7zu%s algo=%s, TLS=%d",
i + 1, i + 1,
m_activePools[i].url(), m_pools.data()[i].url(),
m_activePools[i].algorithm().shortName(), m_pools.data()[i].algorithm().shortName(),
static_cast<int>(m_activePools[i].isTLS()) static_cast<int>(m_pools.data()[i].isTLS())
); );
} }
else { else {
Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("POOL #%-7zu") "\x1B[1;%dm%s\x1B[0m algo " WHITE_BOLD("%s"), Log::i()->text(GREEN_BOLD(" * ") WHITE_BOLD("POOL #%-7zu") "\x1B[1;%dm%s\x1B[0m algo " WHITE_BOLD("%s"),
i + 1, i + 1,
m_activePools[i].isTLS() ? 32 : 36, m_pools.data()[i].isTLS() ? 32 : 36,
m_activePools[i].url(), m_pools.data()[i].url(),
m_activePools[i].algorithm().shortName() m_pools.data()[i].algorithm().shortName()
); );
} }
} }
@ -225,23 +223,9 @@ bool xmrig::CommonConfig::finalize()
return false; return false;
} }
for (Pool &pool : m_pools) { m_pools.adjust(m_algorithm);
pool.adjust(m_algorithm);
if (pool.isValid() && pool.algorithm().isValid()) { if (!m_pools.active()) {
# ifdef XMRIG_NO_TLS
if (pool.isTLS()) {
continue;
}
# endif
m_activePools.push_back(std::move(pool));
}
}
m_pools.clear();
if (m_activePools.empty()) {
m_state = ErrorState; m_state = ErrorState;
return false; return false;
} }
@ -263,16 +247,16 @@ bool xmrig::CommonConfig::parseBoolean(int key, bool enable)
break; break;
case KeepAliveKey: /* --keepalive */ case KeepAliveKey: /* --keepalive */
currentPool().setKeepAlive(enable ? Pool::kKeepAliveTimeout : 0); m_pools.setKeepAlive(enable);
break; break;
case TlsKey: /* --tls */ case TlsKey: /* --tls */
currentPool().setTLS(enable); m_pools.setTLS(enable);
break; break;
# ifndef XMRIG_PROXY_PROJECT # ifndef XMRIG_PROXY_PROJECT
case NicehashKey: /* --nicehash */ case NicehashKey: /* --nicehash */
currentPool().setNicehash(enable); m_pools.setNicehash(enable);
break; break;
# endif # endif
@ -316,50 +300,29 @@ bool xmrig::CommonConfig::parseString(int key, const char *arg)
break; break;
case UserpassKey: /* --userpass */ case UserpassKey: /* --userpass */
if (!currentPool().setUserpass(arg)) { return m_pools.setUserpass(arg);
return false;
}
break;
case UrlKey: /* --url */ case UrlKey: /* --url */
fixup(); return m_pools.setUrl(arg);
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;
case UserKey: /* --user */ case UserKey: /* --user */
currentPool().setUser(arg); m_pools.setUser(arg);
break; break;
case PasswordKey: /* --pass */ case PasswordKey: /* --pass */
currentPool().setPassword(arg); m_pools.setPassword(arg);
break; break;
case RigIdKey: /* --rig-id */ case RigIdKey: /* --rig-id */
currentPool().setRigId(arg); m_pools.setRigId(arg);
break; break;
case FingerprintKey: /* --tls-fingerprint */ case FingerprintKey: /* --tls-fingerprint */
currentPool().setFingerprint(arg); m_pools.setFingerprint(arg);
break; break;
case VariantKey: /* --variant */ case VariantKey: /* --variant */
currentPool().algorithm().parseVariant(arg); m_pools.setVariant(arg);
break; break;
case LogFileKey: /* --log-file */ case LogFileKey: /* --log-file */
@ -453,11 +416,11 @@ bool xmrig::CommonConfig::parseInt(int key, int arg)
break; break;
case KeepAliveKey: /* --keepalive */ case KeepAliveKey: /* --keepalive */
currentPool().setKeepAlive(arg); m_pools.setKeepAlive(arg);
break; break;
case VariantKey: /* --variant */ case VariantKey: /* --variant */
currentPool().algorithm().parseVariant(arg); m_pools.setVariant(arg);
break; break;
case DonateLevelKey: /* --donate-level */ case DonateLevelKey: /* --donate-level */
@ -484,30 +447,3 @@ bool xmrig::CommonConfig::parseInt(int key, int arg)
return true; 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;
}
}

View file

@ -52,7 +52,7 @@ public:
inline const char *apiWorkerId() const { return m_apiWorkerId.data(); } inline const char *apiWorkerId() const { return m_apiWorkerId.data(); }
inline const char *logFile() const { return m_logFile.data(); } inline const char *logFile() const { return m_logFile.data(); }
inline const char *userAgent() const { return m_userAgent.data(); } inline const char *userAgent() const { return m_userAgent.data(); }
inline const std::vector<Pool> &pools() const { return m_activePools; } inline const std::vector<Pool> &pools() const { return m_pools.data(); }
inline int apiPort() const { return m_apiPort; } inline int apiPort() const { return m_apiPort; }
inline int donateLevel() const { return m_donateLevel; } inline int donateLevel() const { return m_donateLevel; }
inline int printTime() const { return m_printTime; } inline int printTime() const { return m_printTime; }
@ -99,9 +99,8 @@ protected:
int m_printTime; int m_printTime;
int m_retries; int m_retries;
int m_retryPause; int m_retryPause;
Pools m_pools;
State m_state; State m_state;
std::vector<Pool> m_activePools;
std::vector<Pool> m_pools;
String m_apiId; String m_apiId;
String m_apiToken; String m_apiToken;
String m_apiWorkerId; String m_apiWorkerId;
@ -111,8 +110,6 @@ protected:
private: private:
bool parseInt(int key, int arg); bool parseInt(int key, int arg);
Pool &currentPool();
void fixup();
}; };

View file

@ -106,7 +106,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const
Value pools(kArrayType); Value pools(kArrayType);
for (const Pool &pool : m_activePools) { for (const Pool &pool : m_pools.data()) {
pools.PushBack(pool.toJSON(doc), allocator); pools.PushBack(pool.toJSON(doc), allocator);
} }