From d8f9501ac80757a135c099bddbbe3f157e590358 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 17 Nov 2020 04:59:26 +0700 Subject: [PATCH] Added DNS cache for online benchmark. --- src/backend/common/benchmark/BenchState.cpp | 1 + .../net/stratum/benchmark/BenchClient.cpp | 86 ++++++++++++------- src/base/net/stratum/benchmark/BenchClient.h | 11 ++- 3 files changed, 62 insertions(+), 36 deletions(-) diff --git a/src/backend/common/benchmark/BenchState.cpp b/src/backend/common/benchmark/BenchState.cpp index 6e64cfca9..ae063571c 100644 --- a/src/backend/common/benchmark/BenchState.cpp +++ b/src/backend/common/benchmark/BenchState.cpp @@ -88,6 +88,7 @@ uint64_t xmrig::BenchState::start(size_t threads, const IBackend *backend) void xmrig::BenchState::destroy() { + xmrig::done = true; async.reset(); } diff --git a/src/base/net/stratum/benchmark/BenchClient.cpp b/src/base/net/stratum/benchmark/BenchClient.cpp index 237ccc25a..2f5a11a61 100644 --- a/src/base/net/stratum/benchmark/BenchClient.cpp +++ b/src/base/net/stratum/benchmark/BenchClient.cpp @@ -26,6 +26,7 @@ #include "base/io/log/Log.h" #include "base/io/log/Tags.h" #include "base/kernel/interfaces/IClientListener.h" +#include "base/net/dns/Dns.h" #include "base/net/http/Fetch.h" #include "base/net/http/HttpData.h" #include "base/net/http/HttpListener.h" @@ -84,23 +85,21 @@ xmrig::BenchClient::~BenchClient() } +const char *xmrig::BenchClient::tag() const +{ + return Tags::bench(); +} + + void xmrig::BenchClient::connect() { # ifdef XMRIG_FEATURE_HTTP - switch (m_mode) { - case STATIC_BENCH: - case STATIC_VERIFY: - return start(); - - case ONLINE_BENCH: - return createBench(); - - case ONLINE_VERIFY: - return getBench(); + if (m_mode == ONLINE_BENCH || m_mode == ONLINE_VERIFY) { + return resolve(); } -# else - start(); # endif + + start(); } @@ -130,7 +129,7 @@ void xmrig::BenchClient::onBenchDone(uint64_t result, uint64_t ts) const uint64_t ref = referenceHash(); const char *color = ref ? ((result == ref) ? GREEN_BOLD_S : RED_BOLD_S) : BLACK_BOLD_S; - LOG_NOTICE("%s " WHITE_BOLD("benchmark finished in ") CYAN_BOLD("%.3f seconds") WHITE_BOLD_S " hash sum = " CLEAR "%s%016" PRIX64 CLEAR, Tags::bench(), static_cast(ts - m_startTime) / 1000.0, color, result); + LOG_NOTICE("%s " WHITE_BOLD("benchmark finished in ") CYAN_BOLD("%.3f seconds") WHITE_BOLD_S " hash sum = " CLEAR "%s%016" PRIX64 CLEAR, tag(), static_cast(ts - m_startTime) / 1000.0, color, result); if (m_mode != ONLINE_BENCH) { printExit(); @@ -174,7 +173,7 @@ void xmrig::BenchClient::onHttpData(const HttpData &data) } if (m_doneTime) { - LOG_NOTICE("%s " WHITE_BOLD("benchmark submitted ") CYAN_BOLD("https://xmrig.com/benchmark/%s"), Tags::bench(), m_job.id().data()); + LOG_NOTICE("%s " WHITE_BOLD("benchmark submitted ") CYAN_BOLD("https://xmrig.com/benchmark/%s"), tag(), m_job.id().data()); printExit(); return; @@ -194,6 +193,28 @@ void xmrig::BenchClient::onHttpData(const HttpData &data) } +void xmrig::BenchClient::onResolved(const Dns &dns, int status) +{ +# ifdef XMRIG_FEATURE_HTTP + assert(!m_httpListener); + + if (status < 0) { + return setError(dns.error(), "DNS error"); + } + + m_ip = dns.get().ip(); + m_httpListener = std::make_shared(this, tag()); + + if (m_mode == ONLINE_BENCH) { + createBench(); + } + else { + getBench(); + } +# endif +} + + uint64_t xmrig::BenchClient::referenceHash() const { if (m_hash || m_mode == ONLINE_BENCH) { @@ -206,7 +227,7 @@ uint64_t xmrig::BenchClient::referenceHash() const void xmrig::BenchClient::printExit() { - LOG_INFO("%s " WHITE_BOLD("press ") MAGENTA_BOLD("Ctrl+C") WHITE_BOLD(" to exit"), Tags::bench()); + LOG_INFO("%s " WHITE_BOLD("press ") MAGENTA_BOLD("Ctrl+C") WHITE_BOLD(" to exit"), tag()); } @@ -221,8 +242,6 @@ void xmrig::BenchClient::start() #ifdef XMRIG_FEATURE_HTTP void xmrig::BenchClient::createBench() { - createHttpListener(); - using namespace rapidjson; Document doc(kObjectType); @@ -233,31 +252,34 @@ void xmrig::BenchClient::createBench() doc.AddMember("version", APP_VERSION, allocator); doc.AddMember("cpu", Cpu::toJSON(doc), allocator); - FetchRequest req(HTTP_POST, BenchConfig::kApiHost, BenchConfig::kApiPort, "/1/benchmark", doc, BenchConfig::kApiTLS, true); + FetchRequest req(HTTP_POST, m_ip, BenchConfig::kApiPort, "/1/benchmark", doc, BenchConfig::kApiTLS, true); fetch(std::move(req), m_httpListener); } -void xmrig::BenchClient::createHttpListener() -{ - if (!m_httpListener) { - m_httpListener = std::make_shared(this, Tags::bench()); - } -} - - void xmrig::BenchClient::getBench() { - createHttpListener(); - - FetchRequest req(HTTP_GET, BenchConfig::kApiHost, BenchConfig::kApiPort, fmt::format("/1/benchmark/{}", m_job.id()).c_str(), BenchConfig::kApiTLS, true); + FetchRequest req(HTTP_GET, m_ip, BenchConfig::kApiPort, fmt::format("/1/benchmark/{}", m_job.id()).c_str(), BenchConfig::kApiTLS, true); fetch(std::move(req), m_httpListener); } -void xmrig::BenchClient::setError(const char *message) +void xmrig::BenchClient::resolve() { - LOG_ERR("%s " RED("benchmark failed ") RED_BOLD("\"%s\""), Tags::bench(), message); + m_dns = std::make_shared(this); + + if (!m_dns->resolve(BenchConfig::kApiHost)) { + setError(m_dns->error(), "getaddrinfo error"); + } +} + + +void xmrig::BenchClient::setError(const char *message, const char *label) +{ + LOG_ERR("%s " RED("%s: ") RED_BOLD("\"%s\""), tag(), label ? label : "benchmark failed", message); + printExit(); + + BenchState::destroy(); } @@ -291,7 +313,7 @@ void xmrig::BenchClient::update(const rapidjson::Value &body) { assert(!m_token.isEmpty()); - FetchRequest req(HTTP_PATCH, BenchConfig::kApiHost, BenchConfig::kApiPort, fmt::format("/1/benchmark/{}", m_job.id()).c_str(), body, BenchConfig::kApiTLS, true); + FetchRequest req(HTTP_PATCH, m_ip, BenchConfig::kApiPort, fmt::format("/1/benchmark/{}", m_job.id()).c_str(), body, BenchConfig::kApiTLS, true); req.headers.insert({ "Authorization", fmt::format("Bearer {}", m_token)}); fetch(std::move(req), m_httpListener); diff --git a/src/base/net/stratum/benchmark/BenchClient.h b/src/base/net/stratum/benchmark/BenchClient.h index 34c415c6e..54e0f6dc2 100644 --- a/src/base/net/stratum/benchmark/BenchClient.h +++ b/src/base/net/stratum/benchmark/BenchClient.h @@ -21,6 +21,7 @@ #include "backend/common/interfaces/IBenchListener.h" +#include "base/kernel/interfaces/IDnsListener.h" #include "base/kernel/interfaces/IHttpListener.h" #include "base/net/stratum/Client.h" @@ -28,7 +29,7 @@ namespace xmrig { -class BenchClient : public IClient, public IHttpListener, public IBenchListener +class BenchClient : public IClient, public IHttpListener, public IBenchListener, public IDnsListener { public: XMRIG_DISABLE_COPY_MOVE_DEFAULT(BenchClient) @@ -41,7 +42,6 @@ public: inline bool isEnabled() const override { return true; } inline bool isTLS() const override { return false; } inline const char *mode() const override { return "benchmark"; } - inline const char *tag() const override { return "null"; } inline const char *tlsFingerprint() const override { return nullptr; } inline const char *tlsVersion() const override { return nullptr; } inline const Job &job() const override { return m_job; } @@ -62,6 +62,7 @@ public: inline void setRetryPause(uint64_t ms) override {} inline void tick(uint64_t now) override {} + const char *tag() const override; void connect() override; void setPool(const Pool &pool) override; @@ -69,6 +70,7 @@ protected: void onBenchDone(uint64_t result, uint64_t ts) override; void onBenchStart(uint64_t ts, uint32_t threads, const IBackend *backend) override; void onHttpData(const HttpData &data) override; + void onResolved(const Dns &dns, int status) override; private: enum Mode : uint32_t { @@ -84,9 +86,9 @@ private: # ifdef XMRIG_FEATURE_HTTP void createBench(); - void createHttpListener(); void getBench(); - void setError(const char *message); + void resolve(); + void setError(const char *message, const char *label = nullptr); void startBench(const rapidjson::Value &value); void startVerify(const rapidjson::Value &value); void update(const rapidjson::Value &body); @@ -98,6 +100,7 @@ private: Mode m_mode = STATIC_BENCH; Pool m_pool; std::shared_ptr m_benchmark; + std::shared_ptr m_dns; std::shared_ptr m_httpListener; String m_ip; String m_token;