From 6774f86fcd217bbdd40d0b0ae14f3579b2867fc4 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 11 Jun 2017 15:32:15 +0300 Subject: [PATCH] Implement job result submitting. --- CMakeLists.txt | 1 + src/interfaces/IJobResultListener.h | 41 +++++++++++++++++++++++++++++ src/net/Client.cpp | 20 ++++++++++++++ src/net/Client.h | 4 ++- src/net/Network.cpp | 9 +++++++ src/net/Network.h | 4 ++- src/workers/Workers.cpp | 8 ++++++ src/workers/Workers.h | 11 +++++--- 8 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 src/interfaces/IJobResultListener.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 15c1d35da..18e5a06e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ set(HEADERS src/Console.h src/Cpu.h src/interfaces/IClientListener.h + src/interfaces/IJobResultListener.h src/interfaces/IWorker.h src/Mem.h src/net/Client.h diff --git a/src/interfaces/IJobResultListener.h b/src/interfaces/IJobResultListener.h new file mode 100644 index 000000000..483a2062d --- /dev/null +++ b/src/interfaces/IJobResultListener.h @@ -0,0 +1,41 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __IJOBRESULTLISTENER_H__ +#define __IJOBRESULTLISTENER_H__ + + +class Client; +class JobResult; + + +class IJobResultListener +{ +public: + virtual ~IJobResultListener() {} + + virtual void onJobResult(const JobResult &result) = 0; +}; + + +#endif // __IJOBRESULTLISTENER_H__ diff --git a/src/net/Client.cpp b/src/net/Client.cpp index 91f582a00..eb776da14 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -28,6 +28,7 @@ #include "Console.h" #include "interfaces/IClientListener.h" #include "net/Client.h" +#include "net/JobResult.h" #include "net/Url.h" @@ -148,6 +149,25 @@ void Client::setUrl(const Url *url) } +void Client::submit(const JobResult &result) +{ + char *req = static_cast(malloc(345)); + char nonce[9]; + char data[65]; + + Job::toHex(reinterpret_cast(&result.nonce), 4, nonce); + nonce[8] = '\0'; + + Job::toHex(result.result, 32, data); + data[64] = '\0'; + + snprintf(req, 345, "{\"id\":%llu,\"jsonrpc\":\"2.0\",\"method\":\"submit\",\"params\":{\"id\":\"%s\",\"job_id\":\"%s\",\"nonce\":\"%s\",\"result\":\"%s\"}}\n", + m_sequence, m_rpcId, result.jobId, nonce, data); + + send(req); +} + + bool Client::parseJob(const json_t *params, int *code) { if (!json_is_object(params)) { diff --git a/src/net/Client.h b/src/net/Client.h index b46a8bf07..75c5b2830 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -32,8 +32,9 @@ #include "net/Job.h" -class Url; class IClientListener; +class JobResult; +class Url; class Client @@ -59,6 +60,7 @@ public: void login(const char *user, const char *pass, const char *agent); void send(char *data); void setUrl(const Url *url); + void submit(const JobResult &result); inline bool isReady() const { return m_state == ConnectedState && m_failures == 0; } inline const char *host() const { return m_host; } diff --git a/src/net/Network.cpp b/src/net/Network.cpp index 02c68fd1b..eef52cd14 100644 --- a/src/net/Network.cpp +++ b/src/net/Network.cpp @@ -39,6 +39,8 @@ Network::Network(const Options *options) : m_pool(0), m_diff(0) { + Workers::setListener(this); + m_pools.reserve(2); m_agent = userAgent(); @@ -103,6 +105,13 @@ void Network::onJobReceived(Client *client, const Job &job) } +void Network::onJobResult(const JobResult &result) +{ + LOG_NOTICE("SHARE FOUND"); + m_pools[result.poolId]->submit(result); +} + + void Network::onLoginCredentialsRequired(Client *client) { client->login(m_options->user(), m_options->pass(), m_agent); diff --git a/src/net/Network.h b/src/net/Network.h index af1ddbbfd..d9192c9fe 100644 --- a/src/net/Network.h +++ b/src/net/Network.h @@ -30,13 +30,14 @@ #include "interfaces/IClientListener.h" +#include "interfaces/IJobResultListener.h" class Options; class Url; -class Network : public IClientListener +class Network : public IClientListener, public IJobResultListener { public: Network(const Options *options); @@ -49,6 +50,7 @@ public: protected: void onClose(Client *client, int failures) override; void onJobReceived(Client *client, const Job &job) override; + void onJobResult(const JobResult &result); void onLoginCredentialsRequired(Client *client) override; void onLoginSuccess(Client *client) override; diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 1f7b76c6a..f3a26cd00 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -21,11 +21,13 @@ * along with this program. If not, see . */ +#include "interfaces/IJobResultListener.h" #include "workers/Handle.h" #include "workers/SingleWorker.h" #include "workers/Workers.h" +IJobResultListener *Workers::m_listener = nullptr; Job Workers::m_job; pthread_mutex_t Workers::m_mutex; pthread_rwlock_t Workers::m_rwlock; @@ -107,4 +109,10 @@ void Workers::onResult(uv_async_t *handle) m_queue.pop_front(); } pthread_mutex_unlock(&m_mutex); + + for (auto result : results) { + m_listener->onJobResult(result); + } + + results.clear(); } diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 68bc6f15f..93913b4ba 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -36,6 +36,7 @@ class Handle; +class IJobResultListener; class Workers @@ -46,15 +47,17 @@ public: static void start(int threads, int64_t affinity, bool nicehash); static void submit(const JobResult &result); - static inline bool isOutdated(uint64_t sequence) { return m_sequence.load(std::memory_order_relaxed) != sequence; } - static inline bool isPaused() { return m_paused.load(std::memory_order_relaxed) == 1; } - static inline uint64_t sequence() { return m_sequence.load(std::memory_order_relaxed); } - static inline void pause() { m_paused = 1; } + static inline bool isOutdated(uint64_t sequence) { return m_sequence.load(std::memory_order_relaxed) != sequence; } + static inline bool isPaused() { return m_paused.load(std::memory_order_relaxed) == 1; } + static inline uint64_t sequence() { return m_sequence.load(std::memory_order_relaxed); } + static inline void pause() { m_paused = 1; } + static inline void setListener(IJobResultListener *listener) { m_listener = listener; } private: static void *onReady(void *arg); static void onResult(uv_async_t *handle); + static IJobResultListener *m_listener; static Job m_job; static pthread_mutex_t m_mutex; static pthread_rwlock_t m_rwlock;