DonateStrategy now use FailoverStrategy internally and possible to use any other IStrategy.

This commit is contained in:
XMRig 2018-03-17 14:33:30 +07:00
parent c46c019c83
commit 1cf1d616c6
13 changed files with 73 additions and 47 deletions

View file

@ -61,6 +61,7 @@ public:
inline uint32_t diff() const { return (uint32_t) m_diff; } inline uint32_t diff() const { return (uint32_t) m_diff; }
inline uint64_t target() const { return m_target; } inline uint64_t target() const { return m_target; }
inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; }
inline void setPoolId(int poolId) { m_poolId = poolId; }
inline void setThreadId(int threadId) { m_threadId = threadId; } inline void setThreadId(int threadId) { m_threadId = threadId; }
static bool fromHex(const char* in, unsigned int len, unsigned char* out); static bool fromHex(const char* in, unsigned int len, unsigned char* out);

View file

@ -94,7 +94,7 @@ void Network::stop()
void Network::onActive(IStrategy *strategy, Client *client) void Network::onActive(IStrategy *strategy, Client *client)
{ {
if (client->id() == -1) { if (m_donate && m_donate == strategy) {
LOG_NOTICE("dev donate started"); LOG_NOTICE("dev donate started");
return; return;
} }
@ -107,11 +107,11 @@ void Network::onActive(IStrategy *strategy, Client *client)
void Network::onJob(IStrategy *strategy, Client *client, const Job &job) void Network::onJob(IStrategy *strategy, Client *client, const Job &job)
{ {
if (m_donate && m_donate->isActive() && client->id() != -1) { if (m_donate && m_donate->isActive() && m_donate != strategy) {
return; return;
} }
setJob(client, job); setJob(client, job, m_donate == strategy);
} }
@ -158,7 +158,7 @@ void Network::onResultAccepted(IStrategy *strategy, Client *client, const Submit
} }
void Network::setJob(Client *client, const Job &job) void Network::setJob(Client *client, const Job &job, bool donate)
{ {
if (m_options->colors()) { if (m_options->colors()) {
LOG_INFO("\x1B[01;35mnew job\x1B[0m from \x1B[01;37m%s:%d\x1B[0m diff \x1B[01;37m%d", client->host(), client->port(), job.diff()); LOG_INFO("\x1B[01;35mnew job\x1B[0m from \x1B[01;37m%s:%d\x1B[0m diff \x1B[01;37m%d", client->host(), client->port(), job.diff());
@ -168,7 +168,7 @@ void Network::setJob(Client *client, const Job &job)
} }
m_state.diff = job.diff(); m_state.diff = job.diff();
Workers::setJob(job); Workers::setJob(job, donate);
} }

View file

@ -58,7 +58,7 @@ protected:
private: private:
constexpr static int kTickInterval = 1 * 1000; constexpr static int kTickInterval = 1 * 1000;
void setJob(Client *client, const Job &job); void setJob(Client *client, const Job &job, bool donate);
void tick(); void tick();
static void onTick(uv_timer_t *handle); static void onTick(uv_timer_t *handle);

View file

@ -26,6 +26,7 @@
#include "net/Client.h" #include "net/Client.h"
#include "net/Job.h" #include "net/Job.h"
#include "net/strategies/DonateStrategy.h" #include "net/strategies/DonateStrategy.h"
#include "net/strategies/FailoverStrategy.h"
#include "Platform.h" #include "Platform.h"
#include "xmrig.h" #include "xmrig.h"
@ -36,10 +37,14 @@ extern "C"
} }
const static char *kDonatePool = "thanks.xmrig.com";
DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener) : DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener) :
m_active(false), m_active(false),
m_donateTime(level * 60 * 1000), m_donateTime(level * 60 * 1000),
m_idleTime((100 - level) * 60 * 1000), m_idleTime((100 - level) * 60 * 1000),
m_strategy(nullptr),
m_listener(listener) m_listener(listener)
{ {
uint8_t hash[200]; uint8_t hash[200];
@ -48,14 +53,17 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL
keccak(reinterpret_cast<const uint8_t *>(user), static_cast<int>(strlen(user)), hash, sizeof(hash)); keccak(reinterpret_cast<const uint8_t *>(user), static_cast<int>(strlen(user)), hash, sizeof(hash));
Job::toHex(hash, 32, userId); Job::toHex(hash, 32, userId);
Url *url = new Url("thanks.xmrig.com", algo == xmrig::ALGO_CRYPTONIGHT_LITE ? 5555 : 80, userId, nullptr, false, true); if (algo == xmrig::ALGO_CRYPTONIGHT) {
m_pools.push_back(new Url(kDonatePool, 80, userId, nullptr, false, true));
m_pools.push_back(new Url(kDonatePool, 443, userId, nullptr, false, true));
m_pools.push_back(new Url("emergency.xmrig.com", 5555, "48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD", "emergency", false, false));
}
else {
m_pools.push_back(new Url(kDonatePool, 3333, userId, nullptr, false, true));
m_pools.push_back(new Url(kDonatePool, 5555, userId, nullptr, false, true));
}
m_client = new Client(-1, Platform::userAgent(), this); m_strategy = new FailoverStrategy(m_pools, 1, 1, this, true);
m_client->setUrl(url);
m_client->setRetryPause(1000);
m_client->setQuiet(true);
delete url;
m_timer.data = this; m_timer.data = this;
uv_timer_init(uv_default_loop(), &m_timer); uv_timer_init(uv_default_loop(), &m_timer);
@ -64,15 +72,20 @@ DonateStrategy::DonateStrategy(int level, const char *user, int algo, IStrategyL
} }
DonateStrategy::~DonateStrategy()
{
}
int64_t DonateStrategy::submit(const JobResult &result) int64_t DonateStrategy::submit(const JobResult &result)
{ {
return m_client->submit(result); return m_strategy->submit(result);
} }
void DonateStrategy::connect() void DonateStrategy::connect()
{ {
m_client->connect(); m_strategy->connect();
} }
@ -84,28 +97,17 @@ void DonateStrategy::release()
void DonateStrategy::stop() void DonateStrategy::stop()
{ {
uv_timer_stop(&m_timer); uv_timer_stop(&m_timer);
m_client->disconnect(); m_strategy->stop();
} }
void DonateStrategy::tick(uint64_t now) void DonateStrategy::tick(uint64_t now)
{ {
m_client->tick(now); m_strategy->tick(now);
} }
void DonateStrategy::onClose(Client *client, int failures) void DonateStrategy::onActive(IStrategy *strategy, Client *client)
{
}
void DonateStrategy::onJobReceived(Client *client, const Job &job)
{
m_listener->onJob(this, client, job);
}
void DonateStrategy::onLoginSuccess(Client *client)
{ {
if (!isActive()) { if (!isActive()) {
uv_timer_start(&m_timer, DonateStrategy::onTimer, m_donateTime, 0); uv_timer_start(&m_timer, DonateStrategy::onTimer, m_donateTime, 0);
@ -116,7 +118,18 @@ void DonateStrategy::onLoginSuccess(Client *client)
} }
void DonateStrategy::onResultAccepted(Client *client, const SubmitResult &result, const char *error) void DonateStrategy::onJob(IStrategy *strategy, Client *client, const Job &job)
{
m_listener->onJob(this, client, job);
}
void DonateStrategy::onPause(IStrategy *strategy)
{
}
void DonateStrategy::onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error)
{ {
m_listener->onResultAccepted(this, client, result, error); m_listener->onResultAccepted(this, client, result, error);
} }
@ -130,7 +143,7 @@ void DonateStrategy::idle()
void DonateStrategy::suspend() void DonateStrategy::suspend()
{ {
m_client->disconnect(); m_strategy->stop();
m_active = false; m_active = false;
m_listener->onPause(this); m_listener->onPause(this);

View file

@ -26,10 +26,12 @@
#include <uv.h> #include <uv.h>
#include <vector>
#include "interfaces/IClientListener.h" #include "interfaces/IClientListener.h"
#include "interfaces/IStrategy.h" #include "interfaces/IStrategy.h"
#include "interfaces/IStrategyListener.h"
class Client; class Client;
@ -37,10 +39,11 @@ class IStrategyListener;
class Url; class Url;
class DonateStrategy : public IStrategy, public IClientListener class DonateStrategy : public IStrategy, public IStrategyListener
{ {
public: public:
DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener); DonateStrategy(int level, const char *user, int algo, IStrategyListener *listener);
~DonateStrategy();
public: public:
inline bool isActive() const override { return m_active; } inline bool isActive() const override { return m_active; }
@ -53,10 +56,10 @@ public:
void tick(uint64_t now) override; void tick(uint64_t now) override;
protected: protected:
void onClose(Client *client, int failures) override; void onActive(IStrategy *strategy, Client *client) override;
void onJobReceived(Client *client, const Job &job) override; void onJob(IStrategy *strategy, Client *client, const Job &job) override;
void onLoginSuccess(Client *client) override; void onPause(IStrategy *strategy) override;
void onResultAccepted(Client *client, const SubmitResult &result, const char *error) override; void onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error) override;
private: private:
void idle(); void idle();
@ -65,10 +68,11 @@ private:
static void onTimer(uv_timer_t *handle); static void onTimer(uv_timer_t *handle);
bool m_active; bool m_active;
Client *m_client;
const int m_donateTime; const int m_donateTime;
const int m_idleTime; const int m_idleTime;
IStrategy *m_strategy;
IStrategyListener *m_listener; IStrategyListener *m_listener;
std::vector<Url*> m_pools;
uv_timer_t m_timer; uv_timer_t m_timer;
}; };

View file

@ -28,8 +28,9 @@
#include "Platform.h" #include "Platform.h"
FailoverStrategy::FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, IStrategyListener *listener) : FailoverStrategy::FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet) :
m_release(false), m_release(false),
m_quiet(quiet),
m_retries(retries), m_retries(retries),
m_retryPause(retryPause), m_retryPause(retryPause),
m_active(-1), m_active(-1),
@ -175,6 +176,7 @@ void FailoverStrategy::add(const Url *url)
Client *client = new Client((int) m_pools.size(), Platform::userAgent(), this); Client *client = new Client((int) m_pools.size(), Platform::userAgent(), this);
client->setUrl(url); client->setUrl(url);
client->setRetryPause(m_retryPause * 1000); client->setRetryPause(m_retryPause * 1000);
client->setQuiet(m_quiet);
m_pools.push_back(client); m_pools.push_back(client);
} }

View file

@ -40,7 +40,7 @@ class Url;
class FailoverStrategy : public IStrategy, public IClientListener class FailoverStrategy : public IStrategy, public IClientListener
{ {
public: public:
FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, IStrategyListener *listener); FailoverStrategy(const std::vector<Url*> &urls, int retryPause, int retries, IStrategyListener *listener, bool quiet = false);
~FailoverStrategy(); ~FailoverStrategy();
public: public:
@ -63,6 +63,7 @@ private:
void add(const Url *url); void add(const Url *url);
bool m_release; bool m_release;
const bool m_quiet;
const int m_retries; const int m_retries;
const int m_retryPause; const int m_retryPause;
int m_active; int m_active;

View file

@ -28,7 +28,7 @@
#include "Platform.h" #include "Platform.h"
SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener) : SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener, bool quiet) :
m_active(false), m_active(false),
m_release(false), m_release(false),
m_listener(listener) m_listener(listener)
@ -36,6 +36,7 @@ SinglePoolStrategy::SinglePoolStrategy(const Url *url, int retryPause, IStrategy
m_client = new Client(0, Platform::userAgent(), this); m_client = new Client(0, Platform::userAgent(), this);
m_client->setUrl(url); m_client->setUrl(url);
m_client->setRetryPause(retryPause * 1000); m_client->setRetryPause(retryPause * 1000);
m_client->setQuiet(quiet);
} }

View file

@ -37,7 +37,7 @@ class Url;
class SinglePoolStrategy : public IStrategy, public IClientListener class SinglePoolStrategy : public IStrategy, public IClientListener
{ {
public: public:
SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener); SinglePoolStrategy(const Url *url, int retryPause, IStrategyListener *listener, bool quiet = false);
~SinglePoolStrategy(); ~SinglePoolStrategy();
public: public:

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* * Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones> * Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet> * Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com> * Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com> * Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* * Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by

View file

@ -83,10 +83,14 @@ void Workers::setEnabled(bool enabled)
} }
void Workers::setJob(const Job &job) void Workers::setJob(const Job &job, bool donate)
{ {
uv_rwlock_wrlock(&m_rwlock); uv_rwlock_wrlock(&m_rwlock);
m_job = job; m_job = job;
if (donate) {
m_job.setPoolId(-1);
}
uv_rwlock_wrunlock(&m_rwlock); uv_rwlock_wrunlock(&m_rwlock);
m_active = true; m_active = true;

View file

@ -45,7 +45,7 @@ public:
static Job job(); static Job job();
static void printHashrate(bool detail); static void printHashrate(bool detail);
static void setEnabled(bool enabled); static void setEnabled(bool enabled);
static void setJob(const Job &job); static void setJob(const Job &job, bool donate);
static void start(int64_t affinity, int priority); static void start(int64_t affinity, int priority);
static void stop(); static void stop();
static void submit(const JobResult &result); static void submit(const JobResult &result);