Add nice messages for accepted and rejected shares with diff and latency.

This commit is contained in:
XMRig 2017-07-02 05:33:10 +03:00
parent 71522214ae
commit 074db6bb72
17 changed files with 128 additions and 14 deletions

View file

@ -26,6 +26,7 @@ set(HEADERS
src/net/Job.h
src/net/JobResult.h
src/net/Network.h
src/net/SubmitResult.h
src/net/Url.h
src/net/strategies/DonateStrategy.h
src/net/strategies/FailoverStrategy.h

View file

@ -25,6 +25,9 @@
#define __ICLIENTLISTENER_H__
#include <stdint.h>
class Client;
class Job;
@ -34,9 +37,10 @@ class IClientListener
public:
virtual ~IClientListener() {}
virtual void onClose(Client *client, int failures) = 0;
virtual void onJobReceived(Client *client, const Job &job) = 0;
virtual void onLoginSuccess(Client *client) = 0;
virtual void onClose(Client *client, int failures) = 0;
virtual void onJobReceived(Client *client, const Job &job) = 0;
virtual void onLoginSuccess(Client *client) = 0;
virtual void onResultAccepted(Client *client, uint32_t diff, uint64_t ms, const char *error) = 0;
};

View file

@ -25,6 +25,9 @@
#define __ISTRATEGYLISTENER_H__
#include <stdint.h>
class Client;
class IStrategy;
class Job;
@ -35,9 +38,10 @@ class IStrategyListener
public:
virtual ~IStrategyListener() {}
virtual void onActive(Client *client) = 0;
virtual void onJob(Client *client, const Job &job) = 0;
virtual void onPause(IStrategy *strategy) = 0;
virtual void onActive(Client *client) = 0;
virtual void onJob(Client *client, const Job &job) = 0;
virtual void onPause(IStrategy *strategy) = 0;
virtual void onResultAccepted(Client *client, uint32_t diff, uint64_t ms, const char *error) = 0;
};

View file

@ -22,6 +22,7 @@
*/
#include <iterator>
#include <utility>
@ -155,6 +156,7 @@ void Client::submit(const JobResult &result)
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);
m_results[m_sequence] = SubmitResult(result.diff);
send(req);
}
@ -264,6 +266,7 @@ void Client::connect(struct sockaddr *addr)
void Client::login()
{
m_sequence = 1;
m_results.clear();
const size_t size = 96 + strlen(m_url.user()) + strlen(m_url.password()) + strlen(m_agent);
char *req = static_cast<char*>(malloc(size));
@ -334,7 +337,12 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error
if (json_is_object(error)) {
const char *message = json_string_value(json_object_get(error, "message"));
if (!m_quiet) {
auto it = m_results.find(id);
if (it != m_results.end()) {
m_listener->onResultAccepted(this, it->second.diff, it->second.elapsed(), message);
m_results.erase(it);
}
else if (!m_quiet) {
LOG_ERR("[%s:%u] error: \"%s\", code: %lld", m_url.host(), m_url.port(), message, json_integer_value(json_object_get(error, "code")));
}
@ -364,6 +372,12 @@ void Client::parseResponse(int64_t id, const json_t *result, const json_t *error
m_listener->onJobReceived(this, m_job);
return;
}
auto it = m_results.find(id);
if (it != m_results.end()) {
m_listener->onResultAccepted(this, it->second.diff, it->second.elapsed(), nullptr);
m_results.erase(it);
}
}

View file

@ -26,10 +26,12 @@
#include <jansson.h>
#include <map>
#include <uv.h>
#include "net/Job.h"
#include "net/SubmitResult.h"
#include "net/Url.h"
@ -108,6 +110,7 @@ private:
Job m_job;
size_t m_recvBufPos;
SocketState m_state;
std::map<int64_t, SubmitResult> m_results;
struct addrinfo m_hints;
Url m_url;
uv_buf_t m_recvBuf;

View file

@ -32,8 +32,8 @@
class JobResult
{
public:
inline JobResult() : poolId(0), nonce(0) {}
inline JobResult(int poolId, const char *jobId, uint32_t nonce, const uint8_t *result) : poolId(poolId), nonce(nonce)
inline JobResult() : poolId(0), diff(0), nonce(0) {}
inline JobResult(int poolId, const char *jobId, uint32_t nonce, const uint8_t *result, uint32_t diff) : poolId(poolId), diff(diff), nonce(nonce)
{
memcpy(this->jobId, jobId, sizeof(this->jobId));
memcpy(this->result, result, sizeof(this->result));
@ -41,6 +41,7 @@ public:
char jobId[64];
int poolId;
uint32_t diff;
uint32_t nonce;
uint8_t result[32];
};

View file

@ -39,7 +39,9 @@
Network::Network(const Options *options) :
m_donateActive(false),
m_options(options),
m_donate(nullptr)
m_donate(nullptr),
m_accepted(0),
m_rejected(0)
{
Workers::setListener(this);
m_agent = userAgent();
@ -94,8 +96,6 @@ void Network::onJob(Client *client, const Job &job)
void Network::onJobResult(const JobResult &result)
{
LOG_NOTICE(m_options->colors() ? "\x1B[01;32mSHARE FOUND" : "SHARE FOUND");
if (result.poolId == -1 && m_donate) {
return m_donate->submit(result);
}
@ -118,6 +118,21 @@ void Network::onPause(IStrategy *strategy)
}
void Network::onResultAccepted(Client *client, uint32_t diff, uint64_t ms, const char *error)
{
if (error) {
m_rejected++;
LOG_INFO(m_options->colors() ? "\x1B[01;31mrejected\x1B[0m (%lld/%lld) diff \x1B[01;37m%u\x1B[0m \x1B[31m\"%s\"\x1B[0m \x1B[01;30m(%llu ms)" : "accepted (%lld/%lld) diff %u \"%s\" (%llu ms)", m_accepted, m_rejected, diff, error, ms);
}
else {
m_accepted++;
LOG_INFO(m_options->colors() ? "\x1B[01;32maccepted\x1B[0m (%lld/%lld) diff \x1B[01;37m%u\x1B[0m \x1B[01;30m(%llu ms)" : "accepted (%lld/%lld) diff %u (%llu ms)", m_accepted, m_rejected, diff, ms);
}
}
void Network::setJob(Client *client, const Job &job)
{
if (m_options->colors()) {

View file

@ -54,6 +54,7 @@ protected:
void onJob(Client *client, const Job &job) override;
void onJobResult(const JobResult &result) override;
void onPause(IStrategy *strategy) override;
void onResultAccepted(Client *client, uint32_t diff, uint64_t ms, const char *error) override;
private:
void addPool(const Url *url);
@ -66,6 +67,8 @@ private:
const Options *m_options;
IStrategy *m_donate;
IStrategy *m_strategy;
uint64_t m_accepted;
uint64_t m_rejected;
};

47
src/net/SubmitResult.h Normal file
View file

@ -0,0 +1,47 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com>
*
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __SUBMITRESULT_H__
#define __SUBMITRESULT_H__
#include <uv.h>
class SubmitResult
{
public:
inline SubmitResult() : diff(0), start(0) {}
inline SubmitResult(uint32_t diff) :
diff(diff)
{
start = uv_hrtime();
}
inline uint64_t elapsed() const { return (uv_hrtime() - start) / 1000000; }
uint32_t diff;
uint64_t start;
};
#endif /* __SUBMITRESULT_H__ */

View file

@ -84,6 +84,12 @@ void DonateStrategy::onLoginSuccess(Client *client)
}
void DonateStrategy::onResultAccepted(Client *client, uint32_t diff, uint64_t ms, const char *error)
{
m_listener->onResultAccepted(client, diff, ms, error);
}
void DonateStrategy::idle()
{
uv_timer_start(&m_timer, DonateStrategy::onTimer, m_idleTime, 0);

View file

@ -53,6 +53,7 @@ protected:
void onClose(Client *client, int failures) override;
void onJobReceived(Client *client, const Job &job) override;
void onLoginSuccess(Client *client) override;
void onResultAccepted(Client *client, uint32_t diff, uint64_t ms, const char *error) override;
private:
void idle();

View file

@ -111,6 +111,12 @@ void FailoverStrategy::onLoginSuccess(Client *client)
}
void FailoverStrategy::onResultAccepted(Client *client, uint32_t diff, uint64_t ms, const char *error)
{
m_listener->onResultAccepted(client, diff, ms, error);
}
void FailoverStrategy::add(const Url *url, const char *agent)
{
Client *client = new Client(m_pools.size(), agent, this);

View file

@ -53,6 +53,7 @@ protected:
void onClose(Client *client, int failures) override;
void onJobReceived(Client *client, const Job &job) override;
void onLoginSuccess(Client *client) override;
void onResultAccepted(Client *client, uint32_t diff, uint64_t ms, const char *error) override;
private:
void add(const Url *url, const char *agent);

View file

@ -82,3 +82,9 @@ void SinglePoolStrategy::onLoginSuccess(Client *client)
m_active = true;
m_listener->onActive(client);
}
void SinglePoolStrategy::onResultAccepted(Client *client, uint32_t diff, uint64_t ms, const char *error)
{
m_listener->onResultAccepted(client, diff, ms, error);
}

View file

@ -50,6 +50,7 @@ protected:
void onClose(Client *client, int failures) override;
void onJobReceived(Client *client, const Job &job) override;
void onLoginSuccess(Client *client) override;
void onResultAccepted(Client *client, uint32_t diff, uint64_t ms, const char *error) override;
private:
bool m_active;

View file

@ -62,11 +62,11 @@ void DoubleWorker::start()
CryptoNight::hash(m_blob, m_job.size(), m_hash, m_ctx);
if (*reinterpret_cast<uint64_t*>(m_hash + 24) < m_job.target()) {
Workers::submit(JobResult(m_job.poolId(), m_job.id(), m_nonce1, m_hash));
Workers::submit(JobResult(m_job.poolId(), m_job.id(), m_nonce1, m_hash, m_job.diff()));
}
if (*reinterpret_cast<uint64_t*>(m_hash + 32 + 24) < m_job.target()) {
Workers::submit(JobResult(m_job.poolId(), m_job.id(), m_nonce2, m_hash + 32));
Workers::submit(JobResult(m_job.poolId(), m_job.id(), m_nonce2, m_hash + 32, m_job.diff()));
}
std::this_thread::yield();

View file

@ -76,6 +76,7 @@ void SingleWorker::consumeJob()
memcpy(m_result.jobId, m_job.id(), sizeof(m_result.jobId));
m_result.poolId = m_job.poolId();
m_result.diff = m_job.diff();
if (m_job.isNicehash()) {
m_result.nonce = (*m_job.nonce() & 0xff000000U) + (0xffffffU / m_threads * m_id);