From c6530e352f8eb339640e040bfca28a9f075987a2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 7 Jan 2020 10:13:01 +0700 Subject: [PATCH] Code cleanup. --- src/base/api/interfaces/IApiRequest.h | 36 +++---- src/base/api/requests/HttpApiRequest.cpp | 64 +++++++++---- src/base/api/requests/HttpApiRequest.h | 8 +- src/base/io/json/Json.cpp | 48 +++++++--- src/base/io/json/Json.h | 5 +- src/base/kernel/config/BaseConfig.cpp | 55 +++++++---- src/base/kernel/config/BaseConfig.h | 20 +++- src/base/kernel/config/BaseTransform.cpp | 114 +++++++++++------------ src/base/kernel/config/BaseTransform.h | 4 +- src/base/net/http/Http.cpp | 18 ++-- src/base/net/http/Http.h | 11 ++- src/base/net/stratum/Pool.cpp | 39 ++++---- src/base/net/stratum/Pool.h | 19 +++- src/base/net/stratum/Pools.cpp | 34 ++++--- src/base/net/stratum/Pools.h | 16 +++- src/core/config/Config.cpp | 50 +++++----- src/net/strategies/DonateStrategy.cpp | 8 +- src/net/strategies/DonateStrategy.h | 4 +- 18 files changed, 341 insertions(+), 212 deletions(-) diff --git a/src/base/api/interfaces/IApiRequest.h b/src/base/api/interfaces/IApiRequest.h index 4f74581c8..c23c16523 100644 --- a/src/base/api/interfaces/IApiRequest.h +++ b/src/base/api/interfaces/IApiRequest.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -69,21 +69,23 @@ public: virtual ~IApiRequest() = default; - virtual bool accept() = 0; - virtual bool hasParseError() const = 0; - virtual bool isDone() const = 0; - virtual bool isNew() const = 0; - virtual bool isRestricted() const = 0; - virtual const rapidjson::Value &json() const = 0; - virtual const String &rpcMethod() const = 0; - virtual const String &url() const = 0; - virtual int version() const = 0; - virtual Method method() const = 0; - virtual rapidjson::Document &doc() = 0; - virtual rapidjson::Value &reply() = 0; - virtual RequestType type() const = 0; - virtual Source source() const = 0; - virtual void done(int status) = 0; + virtual bool accept() = 0; + virtual bool hasParseError() const = 0; + virtual bool isDone() const = 0; + virtual bool isNew() const = 0; + virtual bool isRestricted() const = 0; + virtual const rapidjson::Value &json() const = 0; + virtual const String &rpcMethod() const = 0; + virtual const String &url() const = 0; + virtual int version() const = 0; + virtual Method method() const = 0; + virtual rapidjson::Document &doc() = 0; + virtual rapidjson::Value &reply() = 0; + virtual RequestType type() const = 0; + virtual Source source() const = 0; + virtual void done(int status) = 0; + virtual void setRpcError(int code, const char *message = nullptr) = 0; + virtual void setRpcResult(rapidjson::Value &result) = 0; }; diff --git a/src/base/api/requests/HttpApiRequest.cpp b/src/base/api/requests/HttpApiRequest.cpp index ed13e47d2..6be25e968 100644 --- a/src/base/api/requests/HttpApiRequest.cpp +++ b/src/base/api/requests/HttpApiRequest.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -53,6 +53,10 @@ static inline const char *rpcError(int code) { return "Invalid params"; } + if (code >= HTTP_STATUS_BAD_REQUEST && code <= HTTP_STATUS_NETWORK_AUTHENTICATION_REQUIRED) { + return http_status_str(static_cast(code)); + } + return "Internal error"; } @@ -82,7 +86,7 @@ xmrig::HttpApiRequest::HttpApiRequest(const HttpData &req, bool restricted) : return; } - m_rpcMethod = Json::getString(json(), "method"); + m_rpcMethod = Json::getString(m_body, "method"); if (m_rpcMethod.isEmpty()) { done(RPC_INVALID_REQUEST); @@ -129,6 +133,10 @@ bool xmrig::HttpApiRequest::accept() const rapidjson::Value &xmrig::HttpApiRequest::json() const { + if (type() == REQ_JSON_RPC) { + return Json::getValue(m_body, "params"); + } + return m_body; } @@ -150,25 +158,14 @@ void xmrig::HttpApiRequest::done(int status) m_res.setStatus(HTTP_STATUS_OK); if (status != HTTP_STATUS_OK) { - if (status == HTTP_STATUS_NOT_FOUND) { - status = RPC_METHOD_NOT_FOUND; - } - - Value error(kObjectType); - error.AddMember("code", status, allocator); - error.AddMember("message", StringRef(rpcError(status)), allocator); - - reply().AddMember(StringRef(kError), error, allocator); + setRpcError(status == HTTP_STATUS_NOT_FOUND ? RPC_METHOD_NOT_FOUND : status); } else if (!reply().HasMember(kResult)) { Value result(kObjectType); result.AddMember("status", "OK", allocator); - reply().AddMember(StringRef(kResult), result, allocator); + setRpcResult(result); } - - reply().AddMember("jsonrpc", "2.0", allocator); - reply().AddMember(StringRef(kId), Value().CopyFrom(Json::getValue(json(), kId), allocator), allocator); } else { m_res.setStatus(status); @@ -176,3 +173,38 @@ void xmrig::HttpApiRequest::done(int status) m_res.end(); } + + +void xmrig::HttpApiRequest::setRpcError(int code, const char *message) +{ + using namespace rapidjson; + auto &allocator = doc().GetAllocator(); + + Value error(kObjectType); + error.AddMember("code", code, allocator); + error.AddMember("message", message ? StringRef(message) : StringRef(rpcError(code)), allocator); + + rpcDone(kError, error); +} + + +void xmrig::HttpApiRequest::setRpcResult(rapidjson::Value &result) +{ + rpcDone(kResult, result); +} + + +void xmrig::HttpApiRequest::rpcDone(const char *key, rapidjson::Value &value) +{ + ApiRequest::done(0); + + using namespace rapidjson; + auto &allocator = doc().GetAllocator(); + + reply().AddMember(StringRef(key), value, allocator); + reply().AddMember("jsonrpc", "2.0", allocator); + reply().AddMember(StringRef(kId), Value().CopyFrom(Json::getValue(m_body, kId), allocator), allocator); + + m_res.setStatus(HTTP_STATUS_OK); + m_res.end(); +} diff --git a/src/base/api/requests/HttpApiRequest.h b/src/base/api/requests/HttpApiRequest.h index 309b5a6b0..2a355582a 100644 --- a/src/base/api/requests/HttpApiRequest.h +++ b/src/base/api/requests/HttpApiRequest.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -53,8 +53,12 @@ protected: const rapidjson::Value &json() const override; Method method() const override; void done(int status) override; + void setRpcError(int code, const char *message = nullptr) override; + void setRpcResult(rapidjson::Value &result) override; private: + void rpcDone(const char *key, rapidjson::Value &value); + const HttpData &m_req; HttpApiResponse m_res; int m_parsed = 0; diff --git a/src/base/io/json/Json.cpp b/src/base/io/json/Json.cpp index 738621448..cd7213d22 100644 --- a/src/base/io/json/Json.cpp +++ b/src/base/io/json/Json.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,7 +40,9 @@ static const rapidjson::Value kNullValue; bool xmrig::Json::getBool(const rapidjson::Value &obj, const char *key, bool defaultValue) { - assert(obj.IsObject()); + if (isEmpty(obj)) { + return defaultValue; + } auto i = obj.FindMember(key); if (i != obj.MemberEnd() && i->value.IsBool()) { @@ -51,9 +53,17 @@ bool xmrig::Json::getBool(const rapidjson::Value &obj, const char *key, bool def } +bool xmrig::Json::isEmpty(const rapidjson::Value &obj) +{ + return !obj.IsObject() || obj.ObjectEmpty(); +} + + const char *xmrig::Json::getString(const rapidjson::Value &obj, const char *key, const char *defaultValue) { - assert(obj.IsObject()); + if (isEmpty(obj)) { + return defaultValue; + } auto i = obj.FindMember(key); if (i != obj.MemberEnd() && i->value.IsString()) { @@ -66,7 +76,9 @@ const char *xmrig::Json::getString(const rapidjson::Value &obj, const char *key, const rapidjson::Value &xmrig::Json::getArray(const rapidjson::Value &obj, const char *key) { - assert(obj.IsObject()); + if (isEmpty(obj)) { + return kNullValue; + } auto i = obj.FindMember(key); if (i != obj.MemberEnd() && i->value.IsArray()) { @@ -79,7 +91,9 @@ const rapidjson::Value &xmrig::Json::getArray(const rapidjson::Value &obj, const const rapidjson::Value &xmrig::Json::getObject(const rapidjson::Value &obj, const char *key) { - assert(obj.IsObject()); + if (isEmpty(obj)) { + return kNullValue; + } auto i = obj.FindMember(key); if (i != obj.MemberEnd() && i->value.IsObject()) { @@ -92,7 +106,9 @@ const rapidjson::Value &xmrig::Json::getObject(const rapidjson::Value &obj, cons const rapidjson::Value &xmrig::Json::getValue(const rapidjson::Value &obj, const char *key) { - assert(obj.IsObject()); + if (isEmpty(obj)) { + return kNullValue; + } auto i = obj.FindMember(key); if (i != obj.MemberEnd()) { @@ -105,7 +121,9 @@ const rapidjson::Value &xmrig::Json::getValue(const rapidjson::Value &obj, const int xmrig::Json::getInt(const rapidjson::Value &obj, const char *key, int defaultValue) { - assert(obj.IsObject()); + if (isEmpty(obj)) { + return defaultValue; + } auto i = obj.FindMember(key); if (i != obj.MemberEnd() && i->value.IsInt()) { @@ -118,7 +136,9 @@ int xmrig::Json::getInt(const rapidjson::Value &obj, const char *key, int defaul int64_t xmrig::Json::getInt64(const rapidjson::Value &obj, const char *key, int64_t defaultValue) { - assert(obj.IsObject()); + if (isEmpty(obj)) { + return defaultValue; + } auto i = obj.FindMember(key); if (i != obj.MemberEnd() && i->value.IsInt64()) { @@ -131,7 +151,9 @@ int64_t xmrig::Json::getInt64(const rapidjson::Value &obj, const char *key, int6 uint64_t xmrig::Json::getUint64(const rapidjson::Value &obj, const char *key, uint64_t defaultValue) { - assert(obj.IsObject()); + if (isEmpty(obj)) { + return defaultValue; + } auto i = obj.FindMember(key); if (i != obj.MemberEnd() && i->value.IsUint64()) { @@ -144,7 +166,9 @@ uint64_t xmrig::Json::getUint64(const rapidjson::Value &obj, const char *key, ui unsigned xmrig::Json::getUint(const rapidjson::Value &obj, const char *key, unsigned defaultValue) { - assert(obj.IsObject()); + if (isEmpty(obj)) { + return defaultValue; + } auto i = obj.FindMember(key); if (i != obj.MemberEnd() && i->value.IsUint()) { @@ -169,5 +193,5 @@ rapidjson::Value xmrig::Json::normalize(double value, bool zero) bool xmrig::JsonReader::isEmpty() const { - return !m_obj.IsObject() || m_obj.ObjectEmpty(); + return Json::isEmpty(m_obj); } diff --git a/src/base/io/json/Json.h b/src/base/io/json/Json.h index b5e2a3c8a..15f340a76 100644 --- a/src/base/io/json/Json.h +++ b/src/base/io/json/Json.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,6 +37,7 @@ class Json { public: static bool getBool(const rapidjson::Value &obj, const char *key, bool defaultValue = false); + static bool isEmpty(const rapidjson::Value &obj); static const char *getString(const rapidjson::Value &obj, const char *key, const char *defaultValue = nullptr); static const rapidjson::Value &getArray(const rapidjson::Value &obj, const char *key); static const rapidjson::Value &getObject(const rapidjson::Value &obj, const char *key); diff --git a/src/base/kernel/config/BaseConfig.cpp b/src/base/kernel/config/BaseConfig.cpp index 7b647ed0e..9150dbb1c 100644 --- a/src/base/kernel/config/BaseConfig.cpp +++ b/src/base/kernel/config/BaseConfig.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -47,6 +47,28 @@ #endif +namespace xmrig { + + +const char *BaseConfig::kApi = "api"; +const char *BaseConfig::kApiId = "id"; +const char *BaseConfig::kApiWorkerId = "worker-id"; +const char *BaseConfig::kAutosave = "autosave"; +const char *BaseConfig::kBackground = "background"; +const char *BaseConfig::kColors = "colors"; +const char *BaseConfig::kDryRun = "dry-run"; +const char *BaseConfig::kHttp = "http"; +const char *BaseConfig::kLogFile = "log-file"; +const char *BaseConfig::kPrintTime = "print-time"; +const char *BaseConfig::kSyslog = "syslog"; +const char *BaseConfig::kUserAgent = "user-agent"; +const char *BaseConfig::kVerbose = "verbose"; +const char *BaseConfig::kWatch = "watch"; + + +} // namespace xmrig + + bool xmrig::BaseConfig::read(const IJsonReader &reader, const char *fileName) { m_fileName = fileName; @@ -55,26 +77,25 @@ bool xmrig::BaseConfig::read(const IJsonReader &reader, const char *fileName) return false; } - m_autoSave = reader.getBool("autosave", m_autoSave); - m_background = reader.getBool("background", m_background); - m_dryRun = reader.getBool("dry-run", m_dryRun); - m_syslog = reader.getBool("syslog", m_syslog); - m_watch = reader.getBool("watch", m_watch); - m_logFile = reader.getString("log-file"); - m_userAgent = reader.getString("user-agent"); - m_version = reader.getUint("version"); + m_autoSave = reader.getBool(kAutosave, m_autoSave); + m_background = reader.getBool(kBackground, m_background); + m_dryRun = reader.getBool(kDryRun, m_dryRun); + m_syslog = reader.getBool(kSyslog, m_syslog); + m_watch = reader.getBool(kWatch, m_watch); + m_logFile = reader.getString(kLogFile); + m_userAgent = reader.getString(kUserAgent); - Log::setColors(reader.getBool("colors", Log::isColors())); - setPrintTime(reader.getUint("print-time", 60)); - setVerbose(reader.getValue("verbose")); + Log::setColors(reader.getBool(kColors, Log::isColors())); + setPrintTime(reader.getUint(kPrintTime, 60)); + setVerbose(reader.getValue(kVerbose)); - const rapidjson::Value &api = reader.getObject("api"); + const rapidjson::Value &api = reader.getObject(kApi); if (api.IsObject()) { - m_apiId = Json::getString(api, "id"); - m_apiWorkerId = Json::getString(api, "worker-id"); + m_apiId = Json::getString(api, kApiId); + m_apiWorkerId = Json::getString(api, kApiWorkerId); } - m_http.load(reader.getObject("http")); + m_http.load(reader.getObject(kHttp)); m_pools.load(reader); return m_pools.active() > 0; diff --git a/src/base/kernel/config/BaseConfig.h b/src/base/kernel/config/BaseConfig.h index 7be81828c..ac0478e13 100644 --- a/src/base/kernel/config/BaseConfig.h +++ b/src/base/kernel/config/BaseConfig.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,6 +40,21 @@ class IJsonReader; class BaseConfig : public IConfig { public: + static const char *kApi; + static const char *kApiId; + static const char *kApiWorkerId; + static const char *kAutosave; + static const char *kBackground; + static const char *kColors; + static const char *kDryRun; + static const char *kHttp; + static const char *kLogFile; + static const char *kPrintTime; + static const char *kSyslog; + static const char *kUserAgent; + static const char *kVerbose; + static const char *kWatch; + BaseConfig() = default; inline bool isAutoSave() const { return m_autoSave; } @@ -78,7 +93,6 @@ protected: String m_logFile; String m_userAgent; uint32_t m_printTime = 60; - uint32_t m_version = 0; private: inline void setPrintTime(uint32_t printTime) { if (printTime <= 3600) { m_printTime = printTime; } } diff --git a/src/base/kernel/config/BaseTransform.cpp b/src/base/kernel/config/BaseTransform.cpp index 8a3a140ec..71b3b3ad0 100644 --- a/src/base/kernel/config/BaseTransform.cpp +++ b/src/base/kernel/config/BaseTransform.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,27 +33,17 @@ #endif +#include "base/kernel/config/BaseTransform.h" #include "base/io/json/JsonChain.h" #include "base/io/log/Log.h" -#include "base/kernel/config/BaseTransform.h" +#include "base/kernel/config/BaseConfig.h" #include "base/kernel/interfaces/IConfig.h" #include "base/kernel/Process.h" #include "base/net/stratum/Pool.h" +#include "base/net/stratum/Pools.h" #include "core/config/Config_platform.h" -namespace xmrig -{ - -static const char *kAlgo = "algo"; -static const char *kApi = "api"; -static const char *kCoin = "coin"; -static const char *kHttp = "http"; -static const char *kPools = "pools"; - -} // namespace xmrig - - void xmrig::BaseTransform::load(JsonChain &chain, Process *process, IConfigTransform &transform) { using namespace rapidjson; @@ -95,26 +85,26 @@ void xmrig::BaseTransform::finalize(rapidjson::Document &doc) using namespace rapidjson; auto &allocator = doc.GetAllocator(); - if (m_algorithm.isValid() && doc.HasMember(kPools)) { - auto &pools = doc[kPools]; + if (m_algorithm.isValid() && doc.HasMember(Pools::kPools)) { + auto &pools = doc[Pools::kPools]; for (Value &pool : pools.GetArray()) { - if (!pool.HasMember(kAlgo)) { - pool.AddMember(StringRef(kAlgo), m_algorithm.toJSON(), allocator); + if (!pool.HasMember(Pool::kAlgo)) { + pool.AddMember(StringRef(Pool::kAlgo), m_algorithm.toJSON(), allocator); } } } - if (m_coin.isValid() && doc.HasMember(kPools)) { - auto &pools = doc[kPools]; + if (m_coin.isValid() && doc.HasMember(Pools::kPools)) { + auto &pools = doc[Pools::kPools]; for (Value &pool : pools.GetArray()) { - if (!pool.HasMember(kCoin)) { - pool.AddMember(StringRef(kCoin), m_coin.toJSON(), allocator); + if (!pool.HasMember(Pool::kCoin)) { + pool.AddMember(StringRef(Pool::kCoin), m_coin.toJSON(), allocator); } } } if (m_http) { - set(doc, kHttp, "enabled", true); + set(doc, BaseConfig::kHttp, Http::kEnabled, true); } } @@ -123,20 +113,20 @@ void xmrig::BaseTransform::transform(rapidjson::Document &doc, int key, const ch { switch (key) { case IConfig::AlgorithmKey: /* --algo */ - if (!doc.HasMember(kPools)) { + if (!doc.HasMember(Pools::kPools)) { m_algorithm = arg; } else { - return add(doc, kPools, kAlgo, arg); + return add(doc, Pools::kPools, Pool::kAlgo, arg); } break; case IConfig::CoinKey: /* --coin */ - if (!doc.HasMember(kPools)) { + if (!doc.HasMember(Pools::kPools)) { m_coin = arg; } else { - return add(doc, kPools, kCoin, arg); + return add(doc, Pools::kPools, Pool::kCoin, arg); } break; @@ -150,61 +140,61 @@ void xmrig::BaseTransform::transform(rapidjson::Document &doc, int key, const ch char *user = new char[p - arg + 1](); strncpy(user, arg, static_cast(p - arg)); - add(doc, kPools, "user", user); - add(doc, kPools, "pass", p + 1); + add(doc, Pools::kPools, Pool::kUser, user); + add(doc, Pools::kPools, Pool::kPass, p + 1); delete [] user; } break; case IConfig::UrlKey: /* --url */ { - if (!doc.HasMember(kPools)) { - doc.AddMember(rapidjson::StringRef(kPools), rapidjson::kArrayType, doc.GetAllocator()); + if (!doc.HasMember(Pools::kPools)) { + doc.AddMember(rapidjson::StringRef(Pools::kPools), rapidjson::kArrayType, doc.GetAllocator()); } - rapidjson::Value &array = doc[kPools]; + rapidjson::Value &array = doc[Pools::kPools]; if (array.Size() == 0 || Pool(array[array.Size() - 1]).isValid()) { array.PushBack(rapidjson::kObjectType, doc.GetAllocator()); } - set(doc, array[array.Size() - 1], "url", arg); + set(doc, array[array.Size() - 1], Pool::kUrl, arg); break; } case IConfig::UserKey: /* --user */ - return add(doc, kPools, "user", arg); + return add(doc, Pools::kPools, Pool::kUser, arg); case IConfig::PasswordKey: /* --pass */ - return add(doc, kPools, "pass", arg); + return add(doc, Pools::kPools, Pool::kPass, arg); case IConfig::RigIdKey: /* --rig-id */ - return add(doc, kPools, "rig-id", arg); + return add(doc, Pools::kPools, Pool::kRigId, arg); case IConfig::FingerprintKey: /* --tls-fingerprint */ - return add(doc, kPools, "tls-fingerprint", arg); + return add(doc, Pools::kPools, Pool::kFingerprint, arg); case IConfig::SelfSelectKey: /* --self-select */ - return add(doc, kPools, "self-select", arg); + return add(doc, Pools::kPools, Pool::kSelfSelect, arg); case IConfig::LogFileKey: /* --log-file */ - return set(doc, "log-file", arg); + return set(doc, BaseConfig::kLogFile, arg); case IConfig::HttpAccessTokenKey: /* --http-access-token */ m_http = true; - return set(doc, kHttp, "access-token", arg); + return set(doc, BaseConfig::kHttp, Http::kToken, arg); case IConfig::HttpHostKey: /* --http-host */ m_http = true; - return set(doc, kHttp, "host", arg); + return set(doc, BaseConfig::kHttp, Http::kHost, arg); case IConfig::ApiWorkerIdKey: /* --api-worker-id */ - return set(doc, kApi, "worker-id", arg); + return set(doc, BaseConfig::kApi, BaseConfig::kApiWorkerId, arg); case IConfig::ApiIdKey: /* --api-id */ - return set(doc, kApi, "id", arg); + return set(doc, BaseConfig::kApi, BaseConfig::kApiId, arg); case IConfig::UserAgentKey: /* --user-agent */ - return set(doc, "user-agent", arg); + return set(doc, BaseConfig::kUserAgent, arg); case IConfig::RetriesKey: /* --retries */ case IConfig::RetryPauseKey: /* --retry-pause */ @@ -239,43 +229,43 @@ void xmrig::BaseTransform::transformBoolean(rapidjson::Document &doc, int key, b { switch (key) { case IConfig::BackgroundKey: /* --background */ - return set(doc, "background", enable); + return set(doc, BaseConfig::kBackground, enable); case IConfig::SyslogKey: /* --syslog */ - return set(doc, "syslog", enable); + return set(doc, BaseConfig::kSyslog, enable); case IConfig::KeepAliveKey: /* --keepalive */ - return add(doc, kPools, "keepalive", enable); + return add(doc, Pools::kPools, Pool::kKeepalive, enable); case IConfig::TlsKey: /* --tls */ - return add(doc, kPools, "tls", enable); + return add(doc, Pools::kPools, Pool::kTls, enable); # ifdef XMRIG_FEATURE_HTTP case IConfig::DaemonKey: /* --daemon */ - return add(doc, kPools, "daemon", enable); + return add(doc, Pools::kPools, Pool::kDaemon, enable); # endif # ifndef XMRIG_PROXY_PROJECT case IConfig::NicehashKey: /* --nicehash */ - return add(doc, kPools, "nicehash", enable); + return add(doc, Pools::kPools, Pool::kNicehash, enable); # endif case IConfig::ColorKey: /* --no-color */ - return set(doc, "colors", enable); + return set(doc, BaseConfig::kColors, enable); case IConfig::HttpRestrictedKey: /* --http-no-restricted */ m_http = true; - return set(doc, kHttp, "restricted", enable); + return set(doc, BaseConfig::kHttp, Http::kRestricted, enable); case IConfig::HttpEnabledKey: /* --http-enabled */ m_http = true; break; case IConfig::DryRunKey: /* --dry-run */ - return set(doc, "dry-run", enable); + return set(doc, BaseConfig::kDryRun, enable); case IConfig::VerboseKey: /* --verbose */ - return set(doc, "verbose", enable); + return set(doc, BaseConfig::kVerbose, enable); default: break; @@ -287,27 +277,27 @@ void xmrig::BaseTransform::transformUint64(rapidjson::Document &doc, int key, ui { switch (key) { case IConfig::RetriesKey: /* --retries */ - return set(doc, "retries", arg); + return set(doc, Pools::kRetries, arg); case IConfig::RetryPauseKey: /* --retry-pause */ - return set(doc, "retry-pause", arg); + return set(doc, Pools::kRetryPause, arg); case IConfig::DonateLevelKey: /* --donate-level */ - return set(doc, "donate-level", arg); + return set(doc, Pools::kDonateLevel, arg); case IConfig::ProxyDonateKey: /* --donate-over-proxy */ - return set(doc, "donate-over-proxy", arg); + return set(doc, Pools::kDonateOverProxy, arg); case IConfig::HttpPort: /* --http-port */ m_http = true; - return set(doc, kHttp, "port", arg); + return set(doc, BaseConfig::kHttp, Http::kPort, arg); case IConfig::PrintTimeKey: /* --print-time */ - return set(doc, "print-time", arg); + return set(doc, BaseConfig::kPrintTime, arg); # ifdef XMRIG_FEATURE_HTTP case IConfig::DaemonPollKey: /* --daemon-poll-interval */ - return add(doc, kPools, "daemon-poll-interval", arg); + return add(doc, Pools::kPools, Pool::kDaemonPollInterval, arg); # endif default: diff --git a/src/base/kernel/config/BaseTransform.h b/src/base/kernel/config/BaseTransform.h index 704f08993..16ad843fe 100644 --- a/src/base/kernel/config/BaseTransform.h +++ b/src/base/kernel/config/BaseTransform.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/base/net/http/Http.cpp b/src/base/net/http/Http.cpp index c58080e6b..0aef85dc8 100644 --- a/src/base/net/http/Http.cpp +++ b/src/base/net/http/Http.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,12 +30,14 @@ namespace xmrig { -static const char *kEnabled = "enabled"; -static const char *kHost = "host"; -static const char *kLocalhost = "127.0.0.1"; -static const char *kPort = "port"; -static const char *kRestricted = "restricted"; -static const char *kToken = "access-token"; + +const char *Http::kEnabled = "enabled"; +const char *Http::kHost = "host"; +const char *Http::kLocalhost = "127.0.0.1"; +const char *Http::kPort = "port"; +const char *Http::kRestricted = "restricted"; +const char *Http::kToken = "access-token"; + } diff --git a/src/base/net/http/Http.h b/src/base/net/http/Http.h index 0e8e4fbb6..7ee179cd0 100644 --- a/src/base/net/http/Http.h +++ b/src/base/net/http/Http.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,6 +36,13 @@ namespace xmrig { class Http { public: + static const char *kEnabled; + static const char *kHost; + static const char *kLocalhost; + static const char *kPort; + static const char *kRestricted; + static const char *kToken; + Http(); inline bool isAuthRequired() const { return !m_restricted || !m_token.isNull(); } diff --git a/src/base/net/stratum/Pool.cpp b/src/base/net/stratum/Pool.cpp index 59b70b11f..78dd2554c 100644 --- a/src/base/net/stratum/Pool.cpp +++ b/src/base/net/stratum/Pool.cpp @@ -5,9 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2019 XMR-Stak , - * Copyright 2018-2019 SChernykh * Copyright 2019 Howard Chu - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -47,23 +47,26 @@ namespace xmrig { -static const char *kAlgo = "algo"; -static const char *kCoin = "coin"; -static const char *kDaemon = "daemon"; -static const char *kDaemonPollInterval = "daemon-poll-interval"; -static const char *kEnabled = "enabled"; -static const char *kFingerprint = "tls-fingerprint"; -static const char *kKeepalive = "keepalive"; -static const char *kNicehash = "nicehash"; -static const char *kPass = "pass"; -static const char *kRigId = "rig-id"; -static const char *kSelfSelect = "self-select"; -static const char *kTls = "tls"; -static const char *kUrl = "url"; -static const char *kUser = "user"; -const String Pool::kDefaultPassword = "x"; -const String Pool::kDefaultUser = "x"; +const String Pool::kDefaultPassword = "x"; +const String Pool::kDefaultUser = "x"; + + +const char *Pool::kAlgo = "algo"; +const char *Pool::kCoin = "coin"; +const char *Pool::kDaemon = "daemon"; +const char *Pool::kDaemonPollInterval = "daemon-poll-interval"; +const char *Pool::kEnabled = "enabled"; +const char *Pool::kFingerprint = "tls-fingerprint"; +const char *Pool::kKeepalive = "keepalive"; +const char *Pool::kNicehash = "nicehash"; +const char *Pool::kPass = "pass"; +const char *Pool::kRigId = "rig-id"; +const char *Pool::kSelfSelect = "self-select"; +const char *Pool::kTls = "tls"; +const char *Pool::kUrl = "url"; +const char *Pool::kUser = "user"; + } diff --git a/src/base/net/stratum/Pool.h b/src/base/net/stratum/Pool.h index 509b3975c..782ae4492 100644 --- a/src/base/net/stratum/Pool.h +++ b/src/base/net/stratum/Pool.h @@ -5,9 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh * Copyright 2019 Howard Chu - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -55,6 +55,21 @@ public: static const String kDefaultPassword; static const String kDefaultUser; + static const char *kAlgo; + static const char *kCoin; + static const char *kDaemon; + static const char *kDaemonPollInterval; + static const char *kEnabled; + static const char *kFingerprint; + static const char *kKeepalive; + static const char *kNicehash; + static const char *kPass; + static const char *kRigId; + static const char *kSelfSelect; + static const char *kTls; + static const char *kUrl; + static const char *kUser; + constexpr static int kKeepAliveTimeout = 60; constexpr static uint16_t kDefaultPort = 3333; constexpr static uint64_t kDefaultPollInterval = 1000; diff --git a/src/base/net/stratum/Pools.cpp b/src/base/net/stratum/Pools.cpp index c88e001fc..a65e26b1d 100644 --- a/src/base/net/stratum/Pools.cpp +++ b/src/base/net/stratum/Pools.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,20 +23,30 @@ */ +#include "base/net/stratum/Pools.h" #include "base/io/log/Log.h" #include "base/kernel/interfaces/IJsonReader.h" -#include "base/net/stratum/Pools.h" #include "base/net/stratum/strategies/FailoverStrategy.h" #include "base/net/stratum/strategies/SinglePoolStrategy.h" #include "donate.h" #include "rapidjson/document.h" +namespace xmrig { + + +const char *Pools::kDonateLevel = "donate-level"; +const char *Pools::kDonateOverProxy = "donate-over-proxy"; +const char *Pools::kPools = "pools"; +const char *Pools::kRetries = "retries"; +const char *Pools::kRetryPause = "retry-pause"; + + +} // namespace xmrig + + xmrig::Pools::Pools() : - m_donateLevel(kDefaultDonateLevel), - m_retries(5), - m_retryPause(5), - m_proxyDonate(PROXY_DONATE_AUTO) + m_donateLevel(kDefaultDonateLevel) { # ifdef XMRIG_PROXY_PROJECT m_retries = 2; @@ -108,7 +118,7 @@ void xmrig::Pools::load(const IJsonReader &reader) { m_data.clear(); - const rapidjson::Value &pools = reader.getArray("pools"); + const rapidjson::Value &pools = reader.getArray(kPools); if (!pools.IsArray()) { return; } @@ -124,10 +134,10 @@ void xmrig::Pools::load(const IJsonReader &reader) } } - setDonateLevel(reader.getInt("donate-level", kDefaultDonateLevel)); - setProxyDonate(reader.getInt("donate-over-proxy", PROXY_DONATE_AUTO)); - setRetries(reader.getInt("retries")); - setRetryPause(reader.getInt("retry-pause")); + setDonateLevel(reader.getInt(kDonateLevel, kDefaultDonateLevel)); + setProxyDonate(reader.getInt(kDonateOverProxy, PROXY_DONATE_AUTO)); + setRetries(reader.getInt(kRetries)); + setRetryPause(reader.getInt(kRetryPause)); } diff --git a/src/base/net/stratum/Pools.h b/src/base/net/stratum/Pools.h index 70e172255..c2713198e 100644 --- a/src/base/net/stratum/Pools.h +++ b/src/base/net/stratum/Pools.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -43,6 +43,12 @@ class IStrategyListener; class Pools { public: + static const char *kDonateLevel; + static const char *kDonateOverProxy; + static const char *kPools; + static const char *kRetries; + static const char *kRetryPause; + enum ProxyDonate { PROXY_DONATE_NONE, PROXY_DONATE_AUTO, @@ -74,9 +80,9 @@ private: void setRetryPause(int retryPause); int m_donateLevel; - int m_retries; - int m_retryPause; - ProxyDonate m_proxyDonate; + int m_retries = 5; + int m_retryPause = 5; + ProxyDonate m_proxyDonate = PROXY_DONATE_AUTO; std::vector m_data; }; diff --git a/src/core/config/Config.cpp b/src/core/config/Config.cpp index dde626f73..3998c2813 100644 --- a/src/core/config/Config.cpp +++ b/src/core/config/Config.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -211,41 +211,41 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const auto &allocator = doc.GetAllocator(); Value api(kObjectType); - api.AddMember("id", m_apiId.toJSON(), allocator); - api.AddMember("worker-id", m_apiWorkerId.toJSON(), allocator); + api.AddMember(StringRef(kApiId), m_apiId.toJSON(), allocator); + api.AddMember(StringRef(kApiWorkerId), m_apiWorkerId.toJSON(), allocator); - doc.AddMember("api", api, allocator); - doc.AddMember("http", m_http.toJSON(doc), allocator); - doc.AddMember("autosave", isAutoSave(), allocator); - doc.AddMember("background", isBackground(), allocator); - doc.AddMember("colors", Log::isColors(), allocator); + doc.AddMember(StringRef(kApi), api, allocator); + doc.AddMember(StringRef(kHttp), m_http.toJSON(doc), allocator); + doc.AddMember(StringRef(kAutosave), isAutoSave(), allocator); + doc.AddMember(StringRef(kBackground), isBackground(), allocator); + doc.AddMember(StringRef(kColors), Log::isColors(), allocator); # ifdef XMRIG_ALGO_RANDOMX - doc.AddMember(StringRef(kRandomX), rx().toJSON(doc), allocator); + doc.AddMember(StringRef(kRandomX), rx().toJSON(doc), allocator); # endif - doc.AddMember(StringRef(kCPU), cpu().toJSON(doc), allocator); + doc.AddMember(StringRef(kCPU), cpu().toJSON(doc), allocator); # ifdef XMRIG_FEATURE_OPENCL - doc.AddMember(StringRef(kOcl), cl().toJSON(doc), allocator); + doc.AddMember(StringRef(kOcl), cl().toJSON(doc), allocator); # endif # ifdef XMRIG_FEATURE_CUDA - doc.AddMember(StringRef(kCuda), cuda().toJSON(doc), allocator); + doc.AddMember(StringRef(kCuda), cuda().toJSON(doc), allocator); # endif - doc.AddMember("donate-level", m_pools.donateLevel(), allocator); - doc.AddMember("donate-over-proxy", m_pools.proxyDonate(), allocator); - doc.AddMember("log-file", m_logFile.toJSON(), allocator); - doc.AddMember("pools", m_pools.toJSON(doc), allocator); - doc.AddMember("print-time", printTime(), allocator); + doc.AddMember(StringRef(Pools::kDonateLevel), m_pools.donateLevel(), allocator); + doc.AddMember(StringRef(Pools::kDonateOverProxy), m_pools.proxyDonate(), allocator); + doc.AddMember(StringRef(kLogFile), m_logFile.toJSON(), allocator); + doc.AddMember(StringRef(Pools::kPools), m_pools.toJSON(doc), allocator); + doc.AddMember(StringRef(kPrintTime), printTime(), allocator); # if defined(XMRIG_FEATURE_NVML) - doc.AddMember(StringRef(kHealthPrintTime), healthPrintTime(), allocator); + doc.AddMember(StringRef(kHealthPrintTime), healthPrintTime(), allocator); # endif - doc.AddMember("retries", m_pools.retries(), allocator); - doc.AddMember("retry-pause", m_pools.retryPause(), allocator); - doc.AddMember("syslog", isSyslog(), allocator); - doc.AddMember("user-agent", m_userAgent.toJSON(), allocator); - doc.AddMember("verbose", Log::verbose(), allocator); - doc.AddMember("watch", m_watch, allocator); + doc.AddMember(StringRef(Pools::kRetries), m_pools.retries(), allocator); + doc.AddMember(StringRef(Pools::kRetryPause), m_pools.retryPause(), allocator); + doc.AddMember(StringRef(kSyslog), isSyslog(), allocator); + doc.AddMember(StringRef(kUserAgent), m_userAgent.toJSON(), allocator); + doc.AddMember(StringRef(kVerbose), Log::verbose(), allocator); + doc.AddMember(StringRef(kWatch), m_watch, allocator); } diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 2be0af049..86d1c4f7d 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -110,9 +110,7 @@ void xmrig::DonateStrategy::connect() if (m_proxy) { m_proxy->connect(); } - else if (m_controller->config()->pools().proxyDonate() == Pools::PROXY_DONATE_ALWAYS) { - setState(STATE_IDLE); - } + else { m_strategy->connect(); } diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index c249284b5..6bc0c0756 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2019 SChernykh - * Copyright 2016-2019 XMRig , + * Copyright 2018-2020 SChernykh + * Copyright 2016-2020 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by