From 72c45d882b13293660cc24b6aa0365d9bf56a71d Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 11 Oct 2019 14:55:12 +0700 Subject: [PATCH] Prepare for self select. --- src/base/kernel/config/BaseTransform.cpp | 3 ++ src/base/kernel/interfaces/IConfig.h | 1 + src/base/net/stratum/Job.cpp | 22 ++++++----- src/base/net/stratum/Job.h | 50 +++++++++++++----------- src/base/net/stratum/Pool.cpp | 48 +++++++++++++++++------ src/base/net/stratum/Pool.h | 24 ++++++++---- src/base/net/stratum/Pools.cpp | 10 +---- src/base/net/stratum/Url.cpp | 2 - src/core/config/Config_platform.h | 1 + src/core/config/usage.h | 1 + 10 files changed, 102 insertions(+), 60 deletions(-) diff --git a/src/base/kernel/config/BaseTransform.cpp b/src/base/kernel/config/BaseTransform.cpp index 2f629bcab..ccf5ccf11 100644 --- a/src/base/kernel/config/BaseTransform.cpp +++ b/src/base/kernel/config/BaseTransform.cpp @@ -183,6 +183,9 @@ void xmrig::BaseTransform::transform(rapidjson::Document &doc, int key, const ch case IConfig::FingerprintKey: /* --tls-fingerprint */ return add(doc, kPools, "tls-fingerprint", arg); + case IConfig::SelfSelectKey: /* --self-select */ + return add(doc, kPools, "self-select", arg); + case IConfig::LogFileKey: /* --log-file */ return set(doc, "log-file", arg); diff --git a/src/base/kernel/interfaces/IConfig.h b/src/base/kernel/interfaces/IConfig.h index 848e78794..9f8d96180 100644 --- a/src/base/kernel/interfaces/IConfig.h +++ b/src/base/kernel/interfaces/IConfig.h @@ -72,6 +72,7 @@ public: ProxyDonateKey = 1017, DaemonKey = 1018, DaemonPollKey = 1019, + SelfSelectKey = 1028, // xmrig common CPUPriorityKey = 1021, diff --git a/src/base/net/stratum/Job.cpp b/src/base/net/stratum/Job.cpp index c86ecba53..ef1b03b59 100644 --- a/src/base/net/stratum/Job.cpp +++ b/src/base/net/stratum/Job.cpp @@ -152,16 +152,18 @@ void xmrig::Job::setDiff(uint64_t diff) void xmrig::Job::copy(const Job &other) { - m_algorithm = other.m_algorithm; - m_nicehash = other.m_nicehash; - m_size = other.m_size; - m_clientId = other.m_clientId; - m_id = other.m_id; - m_diff = other.m_diff; - m_height = other.m_height; - m_target = other.m_target; - m_index = other.m_index; - m_seed = other.m_seed; + m_algorithm = other.m_algorithm; + m_nicehash = other.m_nicehash; + m_size = other.m_size; + m_clientId = other.m_clientId; + m_id = other.m_id; + m_diff = other.m_diff; + m_height = other.m_height; + m_target = other.m_target; + m_index = other.m_index; + m_seed = other.m_seed; + m_extraNonce = other.m_extraNonce; + m_poolWallet = other.m_poolWallet; memcpy(m_blob, other.m_blob, sizeof(m_blob)); diff --git a/src/base/net/stratum/Job.h b/src/base/net/stratum/Job.h index d695c561e..3c4c4724c 100644 --- a/src/base/net/stratum/Job.h +++ b/src/base/net/stratum/Job.h @@ -58,28 +58,32 @@ public: bool setTarget(const char *target); void setDiff(uint64_t diff); - inline bool isNicehash() const { return m_nicehash; } - inline bool isValid() const { return m_size > 0 && m_diff > 0; } - inline bool setId(const char *id) { return m_id = id; } - inline const Algorithm &algorithm() const { return m_algorithm; } - inline const Buffer &seed() const { return m_seed; } - inline const String &clientId() const { return m_clientId; } - inline const String &id() const { return m_id; } - inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } - inline const uint8_t *blob() const { return m_blob; } - inline size_t size() const { return m_size; } - inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } - inline uint64_t diff() const { return m_diff; } - inline uint64_t height() const { return m_height; } - inline uint64_t target() const { return m_target; } - inline uint8_t fixedByte() const { return *(m_blob + 42); } - inline uint8_t index() const { return m_index; } - inline void reset() { m_size = 0; m_diff = 0; } - inline void setAlgorithm(const Algorithm::Id id) { m_algorithm = id; } - inline void setAlgorithm(const char *algo) { m_algorithm = algo; } - inline void setClientId(const String &id) { m_clientId = id; } - inline void setHeight(uint64_t height) { m_height = height; } - inline void setIndex(uint8_t index) { m_index = index; } + inline bool isNicehash() const { return m_nicehash; } + inline bool isValid() const { return m_size > 0 && m_diff > 0; } + inline bool setId(const char *id) { return m_id = id; } + inline const Algorithm &algorithm() const { return m_algorithm; } + inline const Buffer &seed() const { return m_seed; } + inline const String &clientId() const { return m_clientId; } + inline const String &extraNonce() const { return m_extraNonce; } + inline const String &id() const { return m_id; } + inline const String &poolWallet() const { return m_poolWallet; } + inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } + inline const uint8_t *blob() const { return m_blob; } + inline size_t size() const { return m_size; } + inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } + inline uint64_t diff() const { return m_diff; } + inline uint64_t height() const { return m_height; } + inline uint64_t target() const { return m_target; } + inline uint8_t fixedByte() const { return *(m_blob + 42); } + inline uint8_t index() const { return m_index; } + inline void reset() { m_size = 0; m_diff = 0; } + inline void setAlgorithm(const Algorithm::Id id) { m_algorithm = id; } + inline void setAlgorithm(const char *algo) { m_algorithm = algo; } + inline void setClientId(const String &id) { m_clientId = id; } + inline void setExtraNonce(const String &extraNonce) { m_extraNonce = extraNonce; } + inline void setHeight(uint64_t height) { m_height = height; } + inline void setIndex(uint8_t index) { m_index = index; } + inline void setPoolWallet(const String &poolWallet) { m_poolWallet = poolWallet; } # ifdef XMRIG_PROXY_PROJECT inline char *rawBlob() { return m_rawBlob; } @@ -103,7 +107,9 @@ private: Buffer m_seed; size_t m_size = 0; String m_clientId; + String m_extraNonce; String m_id; + String m_poolWallet; uint64_t m_diff = 0; uint64_t m_height = 0; uint64_t m_target = 0; diff --git a/src/base/net/stratum/Pool.cpp b/src/base/net/stratum/Pool.cpp index 072dc5347..ccce16c99 100644 --- a/src/base/net/stratum/Pool.cpp +++ b/src/base/net/stratum/Pool.cpp @@ -30,16 +30,12 @@ #include -#include "base/io/json/Json.h" #include "base/net/stratum/Pool.h" +#include "base/io/json/Json.h" +#include "base/io/log/Log.h" #include "rapidjson/document.h" -#ifdef APP_DEBUG -# include "base/io/log/Log.h" -#endif - - namespace xmrig { static const char *kAlgo = "algo"; @@ -52,6 +48,7 @@ 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"; @@ -86,11 +83,18 @@ xmrig::Pool::Pool(const rapidjson::Value &object) : m_pollInterval = Json::getUint64(object, kDaemonPollInterval, kDefaultPollInterval); m_algorithm = Json::getString(object, kAlgo); m_coin = Json::getString(object, kCoin); + m_daemon = Json::getString(object, kSelfSelect); m_flags.set(FLAG_ENABLED, Json::getBool(object, kEnabled, true)); m_flags.set(FLAG_NICEHASH, Json::getBool(object, kNicehash)); m_flags.set(FLAG_TLS, Json::getBool(object, kTls) || m_url.isTLS()); - m_flags.set(FLAG_DAEMON, Json::getBool(object, kDaemon)); + + if (m_daemon.isValid()) { + m_mode = MODE_SELF_SELECT; + } + else if (Json::getBool(object, kDaemon)) { + m_mode = MODE_DAEMON; + } const rapidjson::Value &keepalive = Json::getValue(object, kKeepalive); if (keepalive.IsInt()) { @@ -129,7 +133,7 @@ bool xmrig::Pool::isEnabled() const } # endif - if (isDaemon() && (!algorithm().isValid() && !coin().isValid())) { + if (m_mode == MODE_DAEMON && (!algorithm().isValid() && !coin().isValid())) { return false; } @@ -143,6 +147,7 @@ bool xmrig::Pool::isEqual(const Pool &other) const && m_keepAlive == other.m_keepAlive && m_algorithm == other.m_algorithm && m_coin == other.m_coin + && m_mode == other.m_mode && m_fingerprint == other.m_fingerprint && m_password == other.m_password && m_rigId == other.m_rigId @@ -166,7 +171,7 @@ rapidjson::Value xmrig::Pool::toJSON(rapidjson::Document &doc) const obj.AddMember(StringRef(kUrl), url().toJSON(), allocator); obj.AddMember(StringRef(kUser), m_user.toJSON(), allocator); - if (!isDaemon()) { + if (m_mode != MODE_DAEMON) { obj.AddMember(StringRef(kPass), m_password.toJSON(), allocator); obj.AddMember(StringRef(kRigId), m_rigId.toJSON(), allocator); @@ -185,16 +190,37 @@ rapidjson::Value xmrig::Pool::toJSON(rapidjson::Document &doc) const obj.AddMember(StringRef(kEnabled), m_flags.test(FLAG_ENABLED), allocator); obj.AddMember(StringRef(kTls), isTLS(), allocator); obj.AddMember(StringRef(kFingerprint), m_fingerprint.toJSON(), allocator); - obj.AddMember(StringRef(kDaemon), m_flags.test(FLAG_DAEMON), allocator); + obj.AddMember(StringRef(kDaemon), m_mode == MODE_DAEMON, allocator); - if (isDaemon()) { + if (m_mode == MODE_DAEMON) { obj.AddMember(StringRef(kDaemonPollInterval), m_pollInterval, allocator); } + obj.AddMember(StringRef(kSelfSelect), m_daemon.url().toJSON(), allocator); + return obj; } +std::string xmrig::Pool::printableName() const +{ + std::string out(CSI "1;" + std::to_string(isEnabled() ? (isTLS() ? 32 : 36) : 31) + "m" + url().data() + CLEAR); + + if (m_coin.isValid()) { + out += std::string(" coin ") + WHITE_BOLD_S + m_coin.name() + CLEAR; + } + else { + out += std::string(" algo ") + WHITE_BOLD_S + (m_algorithm.isValid() ? m_algorithm.shortName() : "auto") + CLEAR; + } + + if (m_mode == MODE_SELF_SELECT) { + out += std::string(" self-select ") + CSI "1;" + std::to_string(m_daemon.isTLS() ? 32 : 36) + "m" + m_daemon.url().data() + CLEAR; + } + + return out; +} + + #ifdef APP_DEBUG void xmrig::Pool::print() const { diff --git a/src/base/net/stratum/Pool.h b/src/base/net/stratum/Pool.h index fc6b6326a..3c7436022 100644 --- a/src/base/net/stratum/Pool.h +++ b/src/base/net/stratum/Pool.h @@ -42,12 +42,10 @@ namespace xmrig { class Pool { public: - enum Flags { - FLAG_ENABLED, - FLAG_NICEHASH, - FLAG_TLS, - FLAG_DAEMON, - FLAG_MAX + enum Mode { + MODE_POOL, + MODE_DAEMON, + MODE_SELF_SELECT }; static const String kDefaultPassword; @@ -69,7 +67,7 @@ public: bool tls = false ); - inline bool isDaemon() const { return m_flags.test(FLAG_DAEMON); } + inline bool isDaemon() const { return m_mode == MODE_DAEMON; } inline bool isNicehash() const { return m_flags.test(FLAG_NICEHASH); } inline bool isTLS() const { return m_flags.test(FLAG_TLS); } inline bool isValid() const { return m_url.isValid(); } @@ -81,7 +79,9 @@ public: inline const String &rigId() const { return m_rigId; } inline const String &url() const { return m_url.url(); } inline const String &user() const { return !m_user.isNull() ? m_user : kDefaultUser; } + inline const Url &daemon() const { return m_daemon; } inline int keepAlive() const { return m_keepAlive; } + inline Mode mode() const { return m_mode; } inline uint16_t port() const { return m_url.port(); } inline uint64_t pollInterval() const { return m_pollInterval; } inline void setAlgo(const Algorithm &algorithm) { m_algorithm = algorithm; } @@ -95,24 +95,34 @@ public: bool isEnabled() const; bool isEqual(const Pool &other) const; rapidjson::Value toJSON(rapidjson::Document &doc) const; + std::string printableName() const; # ifdef APP_DEBUG void print() const; # endif private: + enum Flags { + FLAG_ENABLED, + FLAG_NICEHASH, + FLAG_TLS, + FLAG_MAX + }; + inline void setKeepAlive(bool enable) { setKeepAlive(enable ? kKeepAliveTimeout : 0); } inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } Algorithm m_algorithm; Coin m_coin; int m_keepAlive = 0; + Mode m_mode = MODE_POOL; std::bitset m_flags = 0; String m_fingerprint; String m_password; String m_rigId; String m_user; uint64_t m_pollInterval = kDefaultPollInterval; + Url m_daemon; Url m_url; }; diff --git a/src/base/net/stratum/Pools.cpp b/src/base/net/stratum/Pools.cpp index 600c97edb..c88e001fc 100644 --- a/src/base/net/stratum/Pools.cpp +++ b/src/base/net/stratum/Pools.cpp @@ -65,7 +65,7 @@ xmrig::IStrategy *xmrig::Pools::createStrategy(IStrategyListener *listener) cons } } - FailoverStrategy *strategy = new FailoverStrategy(retryPause(), retries(), listener); + auto strategy = new FailoverStrategy(retryPause(), retries(), listener); for (const Pool &pool : m_data) { if (pool.isEnabled()) { strategy->add(pool); @@ -135,13 +135,7 @@ void xmrig::Pools::print() const { size_t i = 1; for (const Pool &pool : m_data) { - Log::print(GREEN_BOLD(" * ") WHITE_BOLD("POOL #%-7zu") CSI "1;%dm%s" CLEAR " %s " WHITE_BOLD("%s"), - i, - (pool.isEnabled() ? (pool.isTLS() ? 32 : 36) : 31), - pool.url().data(), - pool.coin().isValid() ? "coin" : "algo", - pool.coin().isValid() ? pool.coin().name() : (pool.algorithm().isValid() ? pool.algorithm().shortName() : "auto") - ); + Log::print(GREEN_BOLD(" * ") WHITE_BOLD("POOL #%-7zu") "%s", i, pool.printableName().c_str()); i++; } diff --git a/src/base/net/stratum/Url.cpp b/src/base/net/stratum/Url.cpp index 193c72913..3de6bc9b8 100644 --- a/src/base/net/stratum/Url.cpp +++ b/src/base/net/stratum/Url.cpp @@ -81,8 +81,6 @@ bool xmrig::Url::isEqual(const Url &other) const bool xmrig::Url::parse(const char *url) { - assert(url != nullptr); - if (url == nullptr) { return false; } diff --git a/src/core/config/Config_platform.h b/src/core/config/Config_platform.h index ac288793a..a61f1a91a 100644 --- a/src/core/config/Config_platform.h +++ b/src/core/config/Config_platform.h @@ -56,6 +56,7 @@ static const option options[] = { { "http-no-restricted", 0, nullptr, IConfig::HttpRestrictedKey }, { "daemon", 0, nullptr, IConfig::DaemonKey }, { "daemon-poll-interval", 1, nullptr, IConfig::DaemonPollKey }, + { "self-select", 1, nullptr, IConfig::SelfSelectKey }, # endif { "av", 1, nullptr, IConfig::AVKey }, { "background", 0, nullptr, IConfig::BackgroundKey }, diff --git a/src/core/config/usage.h b/src/core/config/usage.h index 5d3718749..f5928fe9b 100644 --- a/src/core/config/usage.h +++ b/src/core/config/usage.h @@ -62,6 +62,7 @@ static inline const std::string &usage() # ifdef XMRIG_FEATURE_HTTP u += " --daemon use daemon RPC instead of pool for solo mining\n"; u += " --daemon-poll-interval=N daemon poll interval in milliseconds (default: 1000)\n"; + u += " --self-select=URL self-select block templates from URL\n"; # endif u += " -r, --retries=N number of times to retry before switch to backup server (default: 5)\n";