From bdff4064a295b47b3c98858f29645a76c3af6969 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 17 Feb 2019 15:43:55 +0700 Subject: [PATCH] Create pools directly from JSON objects. --- src/base/io/Json.cpp | 11 +++++ src/base/io/Json.h | 1 + src/base/net/Pool.cpp | 66 +++++++++++++++++------------- src/base/net/Pool.h | 1 + src/base/net/Pools.cpp | 19 ++++++++- src/base/net/Pools.h | 3 +- src/common/config/CommonConfig.cpp | 5 ++- src/common/config/ConfigLoader.cpp | 13 ------ src/net/Network.cpp | 4 +- 9 files changed, 76 insertions(+), 47 deletions(-) diff --git a/src/base/io/Json.cpp b/src/base/io/Json.cpp index 13a0d7e3d..ee5211d8c 100644 --- a/src/base/io/Json.cpp +++ b/src/base/io/Json.cpp @@ -36,3 +36,14 @@ bool xmrig::Json::getBool(const rapidjson::Value &obj, const char *key, bool def return defaultValue; } + + +const char *xmrig::Json::getString(const rapidjson::Value &obj, const char *key, const char *defaultValue) +{ + auto i = obj.FindMember(key); + if (i != obj.MemberEnd() && i->value.IsString()) { + return i->value.GetString(); + } + + return defaultValue; +} diff --git a/src/base/io/Json.h b/src/base/io/Json.h index 54f927583..c6cde0d82 100644 --- a/src/base/io/Json.h +++ b/src/base/io/Json.h @@ -36,6 +36,7 @@ class Json { public: static bool getBool(const rapidjson::Value &obj, const char *key, bool defaultValue = false); + static const char *getString(const rapidjson::Value &obj, const char *key, const char *defaultValue = nullptr); static bool get(const char *fileName, rapidjson::Document &doc); static bool save(const char *fileName, const rapidjson::Document &doc); diff --git a/src/base/net/Pool.cpp b/src/base/net/Pool.cpp index cacf3494f..850d23fa2 100644 --- a/src/base/net/Pool.cpp +++ b/src/base/net/Pool.cpp @@ -29,6 +29,7 @@ #include +#include "base/io/Json.h" #include "base/net/Pool.h" #include "rapidjson/document.h" @@ -44,6 +45,17 @@ #endif +static const char *kTls = "tls"; +static const char *kUrl = "url"; +static const char *kUser = "user"; +static const char *kPass = "pass"; +static const char *kRigId = "rig-id"; +static const char *kNicehash = "nicehash"; +static const char *kKeepalive = "keepalive"; +static const char *kVariant = "variant"; +static const char *kFingerprint = "tls-fingerprint"; + + xmrig::Pool::Pool() : m_nicehash(false), m_tls(false), @@ -80,24 +92,24 @@ xmrig::Pool::Pool(const rapidjson::Value &object) : m_keepAlive(0), m_port(kDefaultPort) { - if (!parse(object["url"].GetString())) { + if (!parse(Json::getString(object, kUrl))) { return; } - setUser(object["user"].GetString()); - setPassword(object["pass"].GetString()); - setRigId(object["rig-id"].GetString()); - setNicehash(object["nicehash"].GetBool()); + setUser(Json::getString(object, kUser)); + setPassword(Json::getString(object, kPass)); + setRigId(Json::getString(object, kRigId)); + setNicehash(Json::getBool(object, kNicehash)); - const rapidjson::Value &keepalive = object["keepalive"]; + const rapidjson::Value &keepalive = object[kKeepalive]; if (keepalive.IsInt()) { setKeepAlive(keepalive.GetInt()); } else if (keepalive.IsBool()) { - setKeepAlive(keepalive.IsTrue() ? kKeepAliveTimeout : 0); + setKeepAlive(keepalive.GetBool()); } - const rapidjson::Value &variant = object["variant"]; + const rapidjson::Value &variant = object[kVariant]; if (variant.IsString()) { algorithm().parseVariant(variant.GetString()); } @@ -105,12 +117,8 @@ xmrig::Pool::Pool(const rapidjson::Value &object) : algorithm().parseVariant(variant.GetInt()); } - const rapidjson::Value &tls = object["tls"]; - if (tls.IsBool()) { - m_tls = tls.IsTrue(); - } - - m_fingerprint = object["tls-fingerprint"].GetString(); + m_tls = Json::getBool(object, kTls); + m_fingerprint = Json::getString(object, kFingerprint); } @@ -219,7 +227,7 @@ bool xmrig::Pool::parse(const char *url) return true; } - const size_t size = port++ - base + 1; + const size_t size = static_cast(port++ - base + 1); char *host = new char[size](); memcpy(host, base, size - 1); @@ -238,7 +246,7 @@ bool xmrig::Pool::setUserpass(const char *userpass) } char *user = new char[p - userpass + 1](); - strncpy(user, userpass, p - userpass); + strncpy(user, userpass, static_cast(p - userpass)); m_user = user; m_password = p + 1; @@ -255,40 +263,40 @@ rapidjson::Value xmrig::Pool::toJSON(rapidjson::Document &doc) const Value obj(kObjectType); - obj.AddMember("url", m_url.toJSON(), allocator); - obj.AddMember("user", m_user.toJSON(), allocator); - obj.AddMember("pass", m_password.toJSON(), allocator); - obj.AddMember("rig-id", m_rigId.toJSON(), allocator); + obj.AddMember(StringRef(kUrl), m_url.toJSON(), allocator); + obj.AddMember(StringRef(kUser), m_user.toJSON(), allocator); + obj.AddMember(StringRef(kPass), m_password.toJSON(), allocator); + obj.AddMember(StringRef(kRigId), m_rigId.toJSON(), allocator); # ifndef XMRIG_PROXY_PROJECT - obj.AddMember("nicehash", isNicehash(), allocator); + obj.AddMember(StringRef(kNicehash), isNicehash(), allocator); # endif if (m_keepAlive == 0 || m_keepAlive == kKeepAliveTimeout) { - obj.AddMember("keepalive", m_keepAlive > 0, allocator); + obj.AddMember(StringRef(kKeepalive), m_keepAlive > 0, allocator); } else { - obj.AddMember("keepalive", m_keepAlive, allocator); + obj.AddMember(StringRef(kKeepalive), m_keepAlive, allocator); } switch (m_algorithm.variant()) { case VARIANT_AUTO: case VARIANT_0: case VARIANT_1: - obj.AddMember("variant", m_algorithm.variant(), allocator); + obj.AddMember(StringRef(kVariant), m_algorithm.variant(), allocator); break; case VARIANT_2: - obj.AddMember("variant", 2, allocator); + obj.AddMember(StringRef(kVariant), 2, allocator); break; default: - obj.AddMember("variant", StringRef(m_algorithm.variantName()), allocator); + obj.AddMember(StringRef(kVariant), StringRef(m_algorithm.variantName()), allocator); break; } - obj.AddMember("tls", isTLS(), allocator); - obj.AddMember("tls-fingerprint", m_fingerprint.toJSON(), allocator); + obj.AddMember(StringRef(kTls), isTLS(), allocator); + obj.AddMember(StringRef(kFingerprint), m_fingerprint.toJSON(), allocator); return obj; } @@ -345,7 +353,7 @@ bool xmrig::Pool::parseIPv6(const char *addr) return false; } - const size_t size = end - addr; + const size_t size = static_cast(end - addr); char *host = new char[size](); memcpy(host, addr + 1, size - 1); diff --git a/src/base/net/Pool.h b/src/base/net/Pool.h index ec051277e..3d25e7082 100644 --- a/src/base/net/Pool.h +++ b/src/base/net/Pool.h @@ -72,6 +72,7 @@ public: inline uint16_t port() const { return m_port; } inline void setFingerprint(const char *fingerprint) { m_fingerprint = fingerprint; } inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } + inline void setKeepAlive(bool enable) { setKeepAlive(enable ? kKeepAliveTimeout : 0); } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } inline void setPassword(const char *password) { m_password = password; } inline void setRigId(const char *rigId) { m_rigId = rigId; } diff --git a/src/base/net/Pools.cpp b/src/base/net/Pools.cpp index 9d4e6db46..c21cfe214 100644 --- a/src/base/net/Pools.cpp +++ b/src/base/net/Pools.cpp @@ -57,7 +57,7 @@ bool xmrig::Pools::setUrl(const char *url) Pool pool(url); if (pool.isValid()) { - m_data.push_back(pool); + m_data.push_back(std::move(pool)); return true; } @@ -127,6 +127,23 @@ void xmrig::Pools::adjust(const Algorithm &algorithm) } +void xmrig::Pools::load(const rapidjson::Value &pools) +{ + m_data.clear(); + + for (const rapidjson::Value &value : pools.GetArray()) { + if (!value.IsObject()) { + continue; + } + + Pool pool(value); + if (pool.isValid()) { + m_data.push_back(std::move(pool)); + } + } +} + + void xmrig::Pools::print() { size_t i = 1; diff --git a/src/base/net/Pools.h b/src/base/net/Pools.h index d0755aac7..8657b7750 100644 --- a/src/base/net/Pools.h +++ b/src/base/net/Pools.h @@ -49,7 +49,7 @@ public: inline int retries() const { return m_retries; } inline int retryPause() const { return m_retryPause; } inline void setFingerprint(const char *fingerprint) { current().setFingerprint(fingerprint); } - inline void setKeepAlive(bool enable) { setKeepAlive(enable ? Pool::kKeepAliveTimeout : 0); } + inline void setKeepAlive(bool enable) { current().setKeepAlive(enable); } 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); } @@ -64,6 +64,7 @@ public: rapidjson::Value toJSON(rapidjson::Document &doc) const; size_t active() const; void adjust(const Algorithm &algorithm); + void load(const rapidjson::Value &pools); void print(); void setRetries(int retries); void setRetryPause(int retryPause); diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index 7acc58a0f..36d156a31 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -364,7 +364,10 @@ bool xmrig::CommonConfig::parseUint64(int key, uint64_t arg) void xmrig::CommonConfig::parseJSON(const rapidjson::Document &doc) { - + const rapidjson::Value &pools = doc["pools"]; + if (pools.IsArray()) { + m_pools.load(pools); + } } diff --git a/src/common/config/ConfigLoader.cpp b/src/common/config/ConfigLoader.cpp index e5eeb2b9f..fd46e7916 100644 --- a/src/common/config/ConfigLoader.cpp +++ b/src/common/config/ConfigLoader.cpp @@ -96,19 +96,6 @@ bool xmrig::ConfigLoader::loadFromJSON(xmrig::IConfig *config, const rapidjson:: parseJSON(config, &config_options[i], doc); } - const rapidjson::Value &pools = doc["pools"]; - if (pools.IsArray()) { - for (const rapidjson::Value &value : pools.GetArray()) { - if (!value.IsObject()) { - continue; - } - - for (size_t i = 0; i < ARRAY_SIZE(pool_options); i++) { - parseJSON(config, &pool_options[i], value); - } - } - } - const rapidjson::Value &api = doc["api"]; if (api.IsObject()) { for (size_t i = 0; i < ARRAY_SIZE(api_options); i++) { diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 7be630fa3..e2df21f79 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -140,7 +140,7 @@ void xmrig::Network::onPause(IStrategy *strategy) } -void xmrig::Network::onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) +void xmrig::Network::onResultAccepted(IStrategy *, Client *, const SubmitResult &result, const char *error) { m_state.add(result, error); @@ -159,7 +159,7 @@ void xmrig::Network::onResultAccepted(IStrategy *strategy, Client *client, const bool xmrig::Network::isColors() const { - return m_controller->config()->isColors(); + return Log::colors; }