From 0d496aaf2f638e41937fe8358bc60f7695aeb4b3 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 11 Apr 2019 00:18:33 +0700 Subject: [PATCH] Prepare for daemon support. --- src/base/kernel/interfaces/IClient.h | 1 + src/base/net/stratum/Client.h | 1 + src/base/net/stratum/Job.cpp | 6 +- src/base/net/stratum/Job.h | 4 +- src/base/net/stratum/Pool.cpp | 117 ++++++++++++++++++--------- src/base/net/stratum/Pool.h | 9 ++- src/net/Network.cpp | 5 +- src/net/Network.h | 1 + 8 files changed, 97 insertions(+), 47 deletions(-) diff --git a/src/base/kernel/interfaces/IClient.h b/src/base/kernel/interfaces/IClient.h index 52ef83a1d..c872a37ad 100644 --- a/src/base/kernel/interfaces/IClient.h +++ b/src/base/kernel/interfaces/IClient.h @@ -57,6 +57,7 @@ public: virtual bool hasExtension(Extension extension) const noexcept = 0; virtual bool isEnabled() const = 0; virtual bool isTLS() const = 0; + virtual const char *mode() const = 0; virtual const char *tlsFingerprint() const = 0; virtual const char *tlsVersion() const = 0; virtual const Job &job() const = 0; diff --git a/src/base/net/stratum/Client.h b/src/base/net/stratum/Client.h index e334896cb..b42fa6139 100644 --- a/src/base/net/stratum/Client.h +++ b/src/base/net/stratum/Client.h @@ -79,6 +79,7 @@ public: void tick(uint64_t now) override; inline bool hasExtension(Extension extension) const noexcept override { return m_extensions.test(extension); } + inline const char *mode() const override { return "pool"; } protected: inline void onLine(char *line, size_t size) override { parse(line, size); } diff --git a/src/base/net/stratum/Job.cpp b/src/base/net/stratum/Job.cpp index f8239459c..8ef607ad0 100644 --- a/src/base/net/stratum/Job.cpp +++ b/src/base/net/stratum/Job.cpp @@ -7,6 +7,7 @@ * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett * Copyright 2018 SChernykh + * Copyright 2019 Howard Chu * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -180,9 +181,10 @@ void xmrig::Job::setAlgorithm(const char *algo) } -void xmrig::Job::setHeight(uint64_t height) +void xmrig::Job::setDiff(uint64_t diff) { - m_height = height; + m_diff = diff; + m_target = toDiff(diff); } diff --git a/src/base/net/stratum/Job.h b/src/base/net/stratum/Job.h index c8dced4b7..16e9a8611 100644 --- a/src/base/net/stratum/Job.h +++ b/src/base/net/stratum/Job.h @@ -7,6 +7,7 @@ * Copyright 2017-2018 XMR-Stak , * Copyright 2018 Lee Clagett * Copyright 2018-2019 SChernykh + * Copyright 2019 Howard Chu * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -53,7 +54,7 @@ public: bool setBlob(const char *blob); bool setTarget(const char *target); void setAlgorithm(const char *algo); - void setHeight(uint64_t height); + void setDiff(uint64_t diff); inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_size > 0 && m_diff > 0; } @@ -73,6 +74,7 @@ public: inline uint8_t fixedByte() const { return *(m_blob + 42); } inline void reset() { m_size = 0; m_diff = 0; } inline void setClientId(const String &id) { m_clientId = id; } + inline void setHeight(uint64_t height) { m_height = height; } inline void setPoolId(int poolId) { m_poolId = poolId; } inline void setThreadId(int threadId) { m_threadId = threadId; } inline void setVariant(const char *variant) { m_algorithm.parseVariant(variant); } diff --git a/src/base/net/stratum/Pool.cpp b/src/base/net/stratum/Pool.cpp index 25fd13aae..d23781ad0 100644 --- a/src/base/net/stratum/Pool.cpp +++ b/src/base/net/stratum/Pool.cpp @@ -6,6 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2019 XMR-Stak , * Copyright 2018-2019 SChernykh + * Copyright 2019 Howard Chu * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -41,25 +42,34 @@ #ifdef _MSC_VER # define strncasecmp _strnicmp -# define strcasecmp _stricmp #endif namespace xmrig { -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 *kTls = "tls"; -static const char *kUrl = "url"; -static const char *kUser = "user"; -static const char *kVariant = "variant"; +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 *kTls = "tls"; +static const char *kUrl = "url"; +static const char *kUser = "user"; +static const char *kVariant = "variant"; -const String Pool::kDefaultPassword = "x"; -const String Pool::kDefaultUser = "x"; +const String Pool::kDefaultPassword = "x"; +const String Pool::kDefaultUser = "x"; + +static const char kStratumTcp[] = "stratum+tcp://"; +static const char kStratumSsl[] = "stratum+ssl://"; + +#ifdef XMRIG_FEATURE_HTTP +static const char kDaemonHttp[] = "daemon+http://"; +static const char kDaemonHttps[] = "daemon+https://"; +#endif } @@ -67,7 +77,8 @@ const String Pool::kDefaultUser = "x"; xmrig::Pool::Pool() : m_keepAlive(0), m_flags(0), - m_port(kDefaultPort) + m_port(kDefaultPort), + m_pollInterval(kDefaultPollInterval) { } @@ -86,7 +97,8 @@ xmrig::Pool::Pool() : xmrig::Pool::Pool(const char *url) : m_keepAlive(0), m_flags(1), - m_port(kDefaultPort) + m_port(kDefaultPort), + m_pollInterval(kDefaultPollInterval) { parse(url); } @@ -95,20 +107,23 @@ xmrig::Pool::Pool(const char *url) : xmrig::Pool::Pool(const rapidjson::Value &object) : m_keepAlive(0), m_flags(1), - m_port(kDefaultPort) + m_port(kDefaultPort), + m_pollInterval(kDefaultPollInterval) { if (!parse(Json::getString(object, kUrl))) { return; } - m_user = Json::getString(object, kUser); - m_password = Json::getString(object, kPass); - m_rigId = Json::getString(object, kRigId); - m_fingerprint = Json::getString(object, kFingerprint); + m_user = Json::getString(object, kUser); + m_password = Json::getString(object, kPass); + m_rigId = Json::getString(object, kRigId); + m_fingerprint = Json::getString(object, kFingerprint); + m_pollInterval = Json::getUint64(object, kDaemonPollInterval, kDefaultPollInterval); 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_flags.test(FLAG_TLS))); + m_flags.set(FLAG_DAEMON, Json::getBool(object, kDaemon, m_flags.test(FLAG_DAEMON))); const rapidjson::Value &keepalive = Json::getValue(object, kKeepalive); if (keepalive.IsInt()) { @@ -135,7 +150,8 @@ xmrig::Pool::Pool(const char *host, uint16_t port, const char *user, const char m_host(host), m_password(password), m_user(user), - m_port(port) + m_port(port), + m_pollInterval(kDefaultPollInterval) { const size_t size = m_host.size() + 8; assert(size > 8); @@ -180,22 +196,29 @@ bool xmrig::Pool::isEnabled() const } # endif +# ifndef XMRIG_FEATURE_HTTP + if (isDaemon()) { + return false; + } +# endif + return m_flags.test(FLAG_ENABLED) && isValid() && algorithm().isValid(); } bool xmrig::Pool::isEqual(const Pool &other) const { - return (m_flags == other.m_flags - && m_keepAlive == other.m_keepAlive - && m_port == other.m_port - && m_algorithm == other.m_algorithm - && m_fingerprint == other.m_fingerprint - && m_host == other.m_host - && m_password == other.m_password - && m_rigId == other.m_rigId - && m_url == other.m_url - && m_user == other.m_user); + return (m_flags == other.m_flags + && m_keepAlive == other.m_keepAlive + && m_port == other.m_port + && m_algorithm == other.m_algorithm + && m_fingerprint == other.m_fingerprint + && m_host == other.m_host + && m_password == other.m_password + && m_rigId == other.m_rigId + && m_url == other.m_url + && m_user == other.m_user + && m_pollInterval == other.m_pollInterval); } @@ -203,21 +226,33 @@ bool xmrig::Pool::parse(const char *url) { assert(url != nullptr); - const char *p = strstr(url, "://"); + const char *p = strstr(url, "://"); const char *base = url; if (p) { - if (strncasecmp(url, "stratum+tcp://", 14) == 0) { - m_flags.set(FLAG_TLS, false); + if (strncasecmp(url, kStratumTcp, sizeof(kStratumTcp) - 1) == 0) { + m_flags.set(FLAG_DAEMON, false); + m_flags.set(FLAG_TLS, false); } - else if (strncasecmp(url, "stratum+ssl://", 14) == 0) { - m_flags.set(FLAG_TLS, true); + else if (strncasecmp(url, kStratumSsl, sizeof(kStratumSsl) - 1) == 0) { + m_flags.set(FLAG_DAEMON, false); + m_flags.set(FLAG_TLS, true); } +# ifdef XMRIG_FEATURE_HTTP + else if (strncasecmp(url, kDaemonHttps, sizeof(kDaemonHttps) - 1) == 0) { + m_flags.set(FLAG_DAEMON, true); + m_flags.set(FLAG_TLS, true); + } + else if (strncasecmp(url, kDaemonHttp, sizeof(kDaemonHttp) - 1) == 0) { + m_flags.set(FLAG_DAEMON, true); + m_flags.set(FLAG_TLS, false); + } +# endif else { return false; } - base = url + 14; + base = p + 3; } if (!strlen(base) || *base == '/') { @@ -286,9 +321,11 @@ rapidjson::Value xmrig::Pool::toJSON(rapidjson::Document &doc) const break; } - 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(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(kDaemonPollInterval), m_pollInterval, allocator); return obj; } diff --git a/src/base/net/stratum/Pool.h b/src/base/net/stratum/Pool.h index c8a7c44bb..d3c9b141a 100644 --- a/src/base/net/stratum/Pool.h +++ b/src/base/net/stratum/Pool.h @@ -6,6 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018-2019 SChernykh + * Copyright 2019 Howard Chu * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -45,14 +46,16 @@ public: FLAG_ENABLED, FLAG_NICEHASH, FLAG_TLS, + FLAG_DAEMON, FLAG_MAX }; static const String kDefaultPassword; static const String kDefaultUser; - constexpr static uint16_t kDefaultPort = 3333; - constexpr static int kKeepAliveTimeout = 60; + constexpr static int kKeepAliveTimeout = 60; + constexpr static uint16_t kDefaultPort = 3333; + constexpr static uint64_t kDefaultPollInterval = 1000; Pool(); Pool(const char *url); @@ -67,6 +70,7 @@ public: ); inline Algorithm &algorithm() { return m_algorithm; } + inline bool isDaemon() const { return m_flags.test(FLAG_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_host.isNull() && m_port > 0; } @@ -116,6 +120,7 @@ private: String m_url; String m_user; uint16_t m_port; + uint64_t m_pollInterval; }; diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 9a9944eb6..cc5d59e40 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -6,6 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018-2019 SChernykh + * Copyright 2019 Howard Chu * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -101,8 +102,8 @@ void xmrig::Network::onActive(IStrategy *strategy, IClient *client) m_state.setPool(client->pool().host(), client->pool().port(), client->ip()); const char *tlsVersion = client->tlsVersion(); - LOG_INFO(WHITE_BOLD("use pool ") CYAN_BOLD("%s:%d ") GREEN_BOLD("%s") " " BLACK_BOLD("%s"), - client->pool().host().data(), client->pool().port(), tlsVersion ? tlsVersion : "", client->ip().data()); + LOG_INFO(WHITE_BOLD("use %s ") CYAN_BOLD("%s:%d ") GREEN_BOLD("%s") " " BLACK_BOLD("%s"), + client->mode(), client->pool().host().data(), client->pool().port(), tlsVersion ? tlsVersion : "", client->ip().data()); const char *fingerprint = client->tlsFingerprint(); if (fingerprint != nullptr) { diff --git a/src/net/Network.h b/src/net/Network.h index cee15f697..079d997a4 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -6,6 +6,7 @@ * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , * Copyright 2018-2019 SChernykh + * Copyright 2019 Howard Chu * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify