Code cleanup & sync.

This commit is contained in:
XMRig
2019-12-30 04:06:53 +07:00
parent 693c1a93c7
commit 690c702332
23 changed files with 170 additions and 140 deletions

View File

@@ -28,6 +28,7 @@
#include "base/tools/Buffer.h"
#include "core/config/Config.h"
#include "core/Controller.h"
#include "proxy/Counters.h"
#include "proxy/Miner.h"
#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
@@ -186,7 +187,7 @@ void xmrig::ApiRouter::getMinersSummary(rapidjson::Value &reply, rapidjson::Docu
upstreams.AddMember("sleep", stats.upstreams.sleep, allocator);
upstreams.AddMember("error", stats.upstreams.error, allocator);
upstreams.AddMember("total", stats.upstreams.total, allocator);
upstreams.AddMember("ratio", normalize(stats.upstreams.ratio), allocator);
upstreams.AddMember("ratio", normalize(stats.upstreams.ratio(Counters::miners())), allocator);
reply.AddMember("upstreams", upstreams, allocator);
}

View File

@@ -22,23 +22,27 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "core/config/Config.h"
#include "base/io/log/Log.h"
#include "donate.h"
#include "rapidjson/document.h"
#include "base/kernel/interfaces/IJsonReader.h"
#include <array>
#include <cassert>
#include <climits>
#include <cstring>
#include <uv.h>
#include "base/io/log/Log.h"
#include "core/config/Config.h"
#include "donate.h"
#include "rapidjson/document.h"
#include "base/kernel/interfaces/IJsonReader.h"
namespace xmrig {
static const char *modeNames[] = {
"nicehash",
"simple"
};
static const std::array<const char *, 2> modeNames = { "nicehash", "simple" };
} // namespace xmrig
#if defined(_WIN32) && !defined(strncasecmp)
@@ -46,9 +50,6 @@ static const char *modeNames[] = {
#endif
xmrig::Config::Config() = default;
bool xmrig::Config::isTLS() const
{
# ifdef XMRIG_FEATURE_TLS
@@ -196,11 +197,9 @@ void xmrig::Config::setMode(const char *mode)
return;
}
constexpr const size_t size = sizeof(modeNames) / sizeof((modeNames)[0]);
for (size_t i = 0; i < size; i++) {
for (size_t i = 0; i < modeNames.size(); i++) {
if (modeNames[i] && !strcmp(mode, modeNames[i])) {
m_mode = static_cast<int>(i);
m_mode = static_cast<Mode>(i);
break;
}
}

View File

@@ -26,10 +26,6 @@
#define XMRIG_CONFIG_H
#include <cstdint>
#include <vector>
#include "base/kernel/config/BaseConfig.h"
#include "base/tools/String.h"
#include "proxy/BindHost.h"
@@ -42,6 +38,10 @@
#endif
#include <cstdint>
#include <vector>
namespace xmrig {
@@ -50,17 +50,6 @@ class IConfigListener;
class Process;
/**
* @brief The Config class
*
* Options with dynamic reload:
* colors
* debug
* verbose
* custom-diff (only for new connections)
* api/worker-id
* pools/
*/
class Config : public BaseConfig
{
public:
@@ -69,7 +58,7 @@ public:
SIMPLE_MODE
};
Config();
Config() = default;
bool isTLS() const;
const char *modeName() const;

View File

@@ -37,9 +37,6 @@ static const char *kTls = "tls";
}
xmrig::ConfigTransform::ConfigTransform() = default;
void xmrig::ConfigTransform::transform(rapidjson::Document &doc, int key, const char *arg)
{
BaseTransform::transform(doc, key, arg);
@@ -81,6 +78,9 @@ void xmrig::ConfigTransform::transform(rapidjson::Document &doc, int key, const
case IConfig::ReuseTimeoutKey: /* --reuse-timeout */
return transformUint64(doc, key, static_cast<uint64_t>(strtol(arg, nullptr, 10)));
case IConfig::LoginFileKey: /* --login-file */
return set(doc, "login-file", arg);
# ifdef XMRIG_FEATURE_TLS
case IConfig::TlsCertKey: /* --tls-cert */
return set(doc, kTls, "cert", arg);

View File

@@ -5,7 +5,8 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.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>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/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
@@ -34,7 +35,7 @@ namespace xmrig {
class ConfigTransform : public BaseTransform
{
public:
ConfigTransform();
ConfigTransform() = default;
protected:
void transform(rapidjson::Document &doc, int key, const char *arg) override;

View File

@@ -40,7 +40,7 @@
namespace xmrig {
static char const short_options[] = "c:khBp:Px:r:R:s:T:o:u:O:Vl:Sb:A:a:C:m:";
static char const short_options[] = "c:khBp:Px:r:R:s:T:o:u:O:Vl:Sb:A:a:C:m:L:";
static struct option const options[] = {
@@ -90,6 +90,7 @@ static struct option const options[] = {
{ "tls-ciphersuites", 1, nullptr, IConfig::TlsCipherSuitesKey},
{ "no-algo-ext", 0, nullptr, IConfig::AlgoExtKey },
{ "access-password", 1, nullptr, IConfig::ProxyPasswordKey },
{ "login-file", 1, nullptr, IConfig::LoginFileKey },
{ nullptr, 0, nullptr, 0 }
};

View File

@@ -38,11 +38,12 @@ static const char *kUnknownError = "Unknown error";
static const char *kIncompatibleAlgorithm = "No compatible algorithm found, change algo option in your miner.";
static const char *kIncorrectAlgorithm = "Incorrect algorithm";
static const char *kForbidden = "Permission denied";
static const char *kRouteNotFound = "Algorithm negotiation failed";
} /* namespace xmrig */
const char *xmrig::Error::toString(Code code)
const char *xmrig::Error::toString(int code)
{
switch (code)
{
@@ -73,6 +74,9 @@ const char *xmrig::Error::toString(Code code)
case Forbidden:
return kForbidden;
case RouteNotFound:
return kRouteNotFound;
default:
break;
}

View File

@@ -42,10 +42,11 @@ public:
Unauthenticated,
IncompatibleAlgorithm,
IncorrectAlgorithm,
Forbidden
Forbidden,
RouteNotFound
};
static const char *toString(Code code);
static const char *toString(int code);
};

View File

@@ -22,15 +22,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include "proxy/Miner.h"
#include "base/io/json/Json.h"
#include "base/io/log/Log.h"
#include "base/net/stratum/Job.h"
#include "base/net/stratum/SubmitResult.h"
#include "base/tools/Buffer.h"
#include "base/tools/Chrono.h"
#include "base/tools/Handle.h"
@@ -38,11 +33,10 @@
#include "proxy/Counters.h"
#include "proxy/Error.h"
#include "proxy/Events.h"
#include "proxy/events/AcceptEvent.h"
#include "proxy/events/CloseEvent.h"
#include "proxy/events/LoginEvent.h"
#include "proxy/events/AcceptEvent.h"
#include "proxy/events/SubmitEvent.h"
#include "proxy/Miner.h"
#include "proxy/tls/TlsContext.h"
#include "proxy/Uuid.h"
#include "rapidjson/document.h"
@@ -56,6 +50,11 @@
#endif
#include <cinttypes>
#include <cstdio>
#include <cstring>
namespace xmrig {
static int64_t nextId = 0;
char Miner::m_sendBuf[2048] = { 0 };
@@ -64,21 +63,10 @@ namespace xmrig {
xmrig::Miner::Miner(const TlsContext *ctx, uint16_t port) :
m_routeId(-1),
m_id(++nextId),
m_loginId(0),
m_recvBufPos(0),
m_mapperId(-1),
m_state(WaitLoginState),
m_tls(nullptr),
m_localPort(port),
m_customDiff(0),
m_diff(0),
m_expire(Chrono::steadyMSecs() + kLoginTimeout),
m_rx(0),
m_timestamp(Chrono::currentMSecsSinceEpoch()),
m_tx(0),
m_fixedByte(0)
m_expire(Chrono::currentMSecsSinceEpoch() + kLoginTimeout),
m_timestamp(Chrono::currentMSecsSinceEpoch())
{
m_key = m_storage.add(this);
@@ -215,8 +203,15 @@ bool xmrig::Miner::parseRequest(int64_t id, const char *method, const rapidjson:
const rapidjson::Value &value = params["algo"];
if (value.IsArray()) {
for (const rapidjson::Value &algo : value.GetArray()) {
algorithms.push_back(algo.GetString());
algorithms.reserve(value.Size());
for (const auto &i : value.GetArray()) {
Algorithm algo(i.GetString());
if (!algo.isValid()) {
continue;
}
algorithms.emplace_back(algo);
}
}
}
@@ -320,7 +315,7 @@ bool xmrig::Miner::send(BIO *bio)
void xmrig::Miner::heartbeat()
{
m_expire = Chrono::steadyMSecs() + kSocketTimeout;
m_expire = Chrono::currentMSecsSinceEpoch() + kSocketTimeout;
}
@@ -572,7 +567,7 @@ void xmrig::Miner::shutdown(bool)
}
void xmrig::Miner::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
void xmrig::Miner::onAllocBuffer(uv_handle_t *handle, size_t, uv_buf_t *buf)
{
auto miner = getMiner(handle->data);
if (!miner) {
@@ -584,7 +579,7 @@ void xmrig::Miner::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_
}
void xmrig::Miner::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
void xmrig::Miner::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *)
{
Miner *miner = getMiner(stream->data);
if (!miner) {

View File

@@ -32,11 +32,12 @@
#include "base/net/tools/Storage.h"
#include "base/tools/Object.h"
#include "base/tools/String.h"
#include "rapidjson/fwd.h"
typedef struct bio_st BIO;
using BIO = struct bio_st;
namespace xmrig {
@@ -49,6 +50,8 @@ class TlsContext;
class Miner
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Miner)
enum State {
WaitLoginState,
WaitReadyState,
@@ -126,26 +129,26 @@ private:
char m_buf[4096]{};
char m_ip[46]{};
char m_rpcId[37]{};
int32_t m_routeId;
int32_t m_routeId = -1;
int64_t m_id;
int64_t m_loginId;
size_t m_recvBufPos;
ssize_t m_mapperId;
State m_state;
int64_t m_loginId = 0;
size_t m_recvBufPos = 0;
ssize_t m_mapperId = -1;
State m_state = WaitLoginState;
std::bitset<EXT_MAX> m_extensions;
String m_agent;
String m_password;
String m_rigId;
String m_user;
Tls *m_tls;
Tls *m_tls = nullptr;
uint16_t m_localPort;
uint64_t m_customDiff;
uint64_t m_diff;
uint64_t m_customDiff = 0;
uint64_t m_diff = 0;
uint64_t m_expire;
uint64_t m_rx;
uint64_t m_rx = 0;
uint64_t m_timestamp;
uint64_t m_tx;
uint8_t m_fixedByte;
uint64_t m_tx = 0;
uint8_t m_fixedByte = 0;
uintptr_t m_key;
uv_buf_t m_recvBuf{};
uv_tcp_t *m_socket;

View File

@@ -41,10 +41,7 @@ namespace xmrig {
class StatsData
{
public:
inline StatsData()
{
startTime = Chrono::currentMSecsSinceEpoch();
}
inline StatsData() : startTime(Chrono::currentMSecsSinceEpoch()) {}
inline uint32_t avgTime() const
@@ -71,9 +68,26 @@ public:
}
inline uint64_t uptime() const
inline double ratio() const { return upstreams.ratio(miners); }
inline uint64_t uptime() const { return (Chrono::currentMSecsSinceEpoch() - startTime) / 1000; }
inline StatsData &operator+=(const StatsData &other)
{
return (Chrono::currentMSecsSinceEpoch() - startTime) / 1000;
upstreams += other.upstreams;
accepted += other.accepted;
connections += other.connections;
donateHashes += other.donateHashes;
expired += other.expired;
hashes += other.hashes;
invalid += other.invalid;
rejected += other.rejected;
for (size_t i = 0; i < 6; ++i) {
hashrate[i] += other.hashrate[i];
}
return *this;
}

View File

@@ -4,8 +4,9 @@
* 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>
*
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/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
@@ -21,22 +22,19 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TICKINGCOUNTER_H__
#define __TICKINGCOUNTER_H__
#ifndef XMRIG_TICKINGCOUNTER_H
#define XMRIG_TICKINGCOUNTER_H
#include <stdint.h>
#include <stdio.h>
#include <cstdint>
#include <cstdio>
#include <vector>
template <class T> class TickingCounter
{
public:
inline TickingCounter(size_t tickTime) :
m_tickTime(tickTime),
m_pending(0)
{}
inline TickingCounter(size_t tickTime) : m_tickTime(tickTime) {}
inline double calc(size_t seconds) const
@@ -64,8 +62,8 @@ public:
private:
size_t m_tickTime;
std::vector<T> m_data;
T m_pending;
T m_pending = 0;
};
#endif /* __TICKINGCOUNTER_H__ */
#endif /* XMRIG_TICKINGCOUNTER_H */

View File

@@ -38,10 +38,7 @@ namespace xmrig {
class Event : public IEvent
{
public:
inline Event(Type type) :
m_rejected(false),
m_type(type)
{}
inline Event(Type type) : m_type(type) {}
static bool exec(IEvent *event);
@@ -52,8 +49,8 @@ public:
inline bool start() { return exec(this); }
protected:
bool m_rejected;
Type m_type;
bool m_rejected = false;
const Type m_type;
static char m_buf[4096];
};

View File

@@ -26,9 +26,10 @@
#define XMRIG_LOGINEVENT_H
#include <stdint.h>
#include <cstdint>
#include "base/tools/String.h"
#include "crypto/common/Algorithm.h"
#include "proxy/events/MinerEvent.h"
#include "rapidjson/fwd.h"
@@ -49,6 +50,8 @@ public:
const Algorithms &algorithms;
const int64_t loginId;
const rapidjson::Value &params;
int error = -1;
String flow;
protected:

View File

@@ -34,3 +34,9 @@ const char *xmrig::MinerEvent::ip() const
{
return m_miner ? m_miner->ip() : kDefaultIP;
}
int32_t xmrig::MinerEvent::route() const
{
return m_miner ? m_miner->routeId() : -1;
}

View File

@@ -45,10 +45,9 @@ public:
protected:
inline MinerEvent(Type type, Miner *miner) :
Event(type),
m_miner(miner)
{}
inline MinerEvent(Type type, Miner *miner) : Event(type), m_miner(miner) {}
int32_t route() const override;
private:

View File

@@ -44,6 +44,7 @@ public:
}
bool expired = false;
JobResult request;

View File

@@ -26,6 +26,9 @@
#define XMRIG_IEVENT_H
#include <cstdint>
namespace xmrig {
@@ -43,6 +46,7 @@ public:
virtual ~IEvent() = default;
virtual bool isRejected() const = 0;
virtual int32_t route() const = 0;
virtual Type type() const = 0;
virtual void reject() = 0;
};

View File

@@ -26,7 +26,7 @@
#define XMRIG_ISPLITTER_H
#include <stdint.h>
#include <cstdint>
namespace xmrig {
@@ -35,23 +35,35 @@ namespace xmrig {
class Upstreams
{
public:
inline Upstreams() : active(0), sleep(0), total(0), error(0), ratio(0.0) {}
Upstreams() = default;
inline Upstreams(uint64_t active, uint64_t sleep, uint64_t total, uint64_t miners) :
inline Upstreams(uint64_t active, uint64_t sleep, uint64_t total) :
active(active),
sleep(sleep),
total(total),
error(total - active - sleep),
ratio(active > 0 ? (static_cast<double>(miners) / active) : 0.0)
error(total - active - sleep)
{}
uint64_t active;
uint64_t sleep;
uint64_t total;
uint64_t error;
double ratio;
inline double ratio(uint64_t miners) const { return active > 0 ? (static_cast<double>(miners) / active) : 0.0; }
inline Upstreams &operator+=(const Upstreams &other)
{
active += other.active;
sleep += other.sleep;
total += other.total;
error += other.error;
return *this;
}
uint64_t active = 0;
uint64_t sleep = 0;
uint64_t total = 0;
uint64_t error = 0;
};

View File

@@ -23,23 +23,23 @@
*/
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include "proxy/log/AccessLog.h"
#include "base/tools/Chrono.h"
#include "core/config/Config.h"
#include "core/Controller.h"
#include "proxy/Counters.h"
#include "proxy/events/CloseEvent.h"
#include "proxy/events/LoginEvent.h"
#include "proxy/log/AccessLog.h"
#include "proxy/Miner.h"
#include "proxy/Stats.h"
#include <cinttypes>
#include <cstdarg>
#include <cstdio>
#include <ctime>
xmrig::AccessLog::AccessLog(Controller *controller) :
m_file(-1)
{
@@ -54,9 +54,7 @@ xmrig::AccessLog::AccessLog(Controller *controller) :
}
xmrig::AccessLog::~AccessLog()
{
}
xmrig::AccessLog::~AccessLog() = default;
void xmrig::AccessLog::onEvent(IEvent *event)
@@ -67,19 +65,22 @@ void xmrig::AccessLog::onEvent(IEvent *event)
switch (event->type())
{
case IEvent::LoginType: {
case IEvent::LoginType:
{
auto e = static_cast<LoginEvent*>(event);
write("#%" PRId64 " login: %s, \"%s\", ua: \"%s\", count: %" PRIu64, e->miner()->id(), e->miner()->ip(), e->miner()->user().data(), e->miner()->agent().data(), Counters::miners());
write("#%" PRId64 " login: %s, \"%s\", flow: \"%s\", ua: \"%s\", count: %" PRIu64,
e->miner()->id(), e->miner()->ip(), e->miner()->user().data(), e->flow.data(), e->miner()->agent().data(), Counters::miners());
}
break;
case IEvent::CloseType: {
case IEvent::CloseType:
{
auto e = static_cast<CloseEvent*>(event);
if (e->miner()->mapperId() == -1) {
break;
}
const double time = (Chrono::currentMSecsSinceEpoch() - e->miner()->timestamp()) / 1000.0;
const double time = static_cast<double>(Chrono::currentMSecsSinceEpoch() - e->miner()->timestamp()) / 1000.0;
write("#%" PRId64 " close: %s, \"%s\", time: %03.1fs, rx/tx: %" PRIu64 "/%" PRIu64 ", count: %" PRIu64,
e->miner()->id(), e->miner()->ip(), e->miner()->user().data(), time, e->miner()->rx(), e->miner()->tx(), Counters::miners());
@@ -112,7 +113,7 @@ void xmrig::AccessLog::write(const char *fmt, ...)
va_start(args, fmt);
time_t now = time(nullptr);
tm stime;
tm stime{};
# ifdef _WIN32
localtime_s(&stime, &now);
@@ -133,7 +134,7 @@ void xmrig::AccessLog::write(const char *fmt, ...)
buf[size] = '\n';
uv_buf_t ubuf = uv_buf_init(buf, (unsigned int) size + 1);
uv_fs_t *req = new uv_fs_t;
auto req = new uv_fs_t;
req->data = ubuf.base;
uv_fs_write(uv_default_loop(), req, m_file, &ubuf, 1, 0, AccessLog::onWrite);

View File

@@ -29,7 +29,7 @@
#include "proxy/interfaces/IEventListener.h"
typedef struct uv_fs_s uv_fs_t;
using uv_fs_t = struct uv_fs_s;
class Stats;

View File

@@ -71,7 +71,7 @@ xmrig::Upstreams xmrig::NonceSplitter::upstreams() const
}
}
return Upstreams(active, sleep, m_upstreams.size(), Counters::miners());
return Upstreams(active, sleep, m_upstreams.size());
}
@@ -100,13 +100,14 @@ void xmrig::NonceSplitter::gc()
void xmrig::NonceSplitter::printConnections()
{
const Upstreams info = upstreams();
const auto info = upstreams();
const auto ratio = info.ratio(Counters::miners());
LOG_INFO("\x1B[01;32m* \x1B[01;37mupstreams\x1B[0m" LABEL("active") "%s%" PRIu64 "\x1B[0m" LABEL("sleep") "\x1B[01;37m%" PRIu64 "\x1B[0m" LABEL("error") "%s%" PRIu64 "\x1B[0m" LABEL("total") "\x1B[01;37m%" PRIu64,
info.active ? "\x1B[01;32m" : "\x1B[01;31m", info.active, info.sleep, info.error ? "\x1B[01;31m" : "\x1B[01;37m", info.error, info.total);
LOG_INFO("\x1B[01;32m* \x1B[01;37mminers \x1B[0m" LABEL("active") "%s%" PRIu64 "\x1B[0m" LABEL("max") "\x1B[01;37m%" PRIu64 "\x1B[0m" LABEL("ratio") "%s1:%3.1f",
Counters::miners() ? "\x1B[01;32m" : "\x1B[01;31m", Counters::miners(), Counters::maxMiners(), (info.ratio > 200 ? "\x1B[01;32m" : "\x1B[01;33m"), info.ratio);
Counters::miners() ? "\x1B[01;32m" : "\x1B[01;31m", Counters::miners(), Counters::maxMiners(), (ratio > 200 ? "\x1B[01;32m" : "\x1B[01;33m"), ratio);
}

View File

@@ -70,7 +70,7 @@ xmrig::Upstreams xmrig::SimpleSplitter::upstreams() const
}
}
return Upstreams(active, m_idles.size(), m_upstreams.size(), Counters::miners());
return Upstreams(active, m_idles.size(), m_upstreams.size());
}