Create pools directly from JSON objects.

This commit is contained in:
XMRig 2019-02-17 15:43:55 +07:00
parent 3a0fdcac6f
commit bdff4064a2
9 changed files with 76 additions and 47 deletions

View file

@ -36,3 +36,14 @@ bool xmrig::Json::getBool(const rapidjson::Value &obj, const char *key, bool def
return defaultValue;
}
const char *xmrig::Json::getString(const rapidjson::Value &obj, const char *key, const char *defaultValue)
{
auto i = obj.FindMember(key);
if (i != obj.MemberEnd() && i->value.IsString()) {
return i->value.GetString();
}
return defaultValue;
}

View file

@ -36,6 +36,7 @@ class Json
{
public:
static bool getBool(const rapidjson::Value &obj, const char *key, bool defaultValue = false);
static const char *getString(const rapidjson::Value &obj, const char *key, const char *defaultValue = nullptr);
static bool get(const char *fileName, rapidjson::Document &doc);
static bool save(const char *fileName, const rapidjson::Document &doc);

View file

@ -29,6 +29,7 @@
#include <stdio.h>
#include "base/io/Json.h"
#include "base/net/Pool.h"
#include "rapidjson/document.h"
@ -44,6 +45,17 @@
#endif
static const char *kTls = "tls";
static const char *kUrl = "url";
static const char *kUser = "user";
static const char *kPass = "pass";
static const char *kRigId = "rig-id";
static const char *kNicehash = "nicehash";
static const char *kKeepalive = "keepalive";
static const char *kVariant = "variant";
static const char *kFingerprint = "tls-fingerprint";
xmrig::Pool::Pool() :
m_nicehash(false),
m_tls(false),
@ -80,24 +92,24 @@ xmrig::Pool::Pool(const rapidjson::Value &object) :
m_keepAlive(0),
m_port(kDefaultPort)
{
if (!parse(object["url"].GetString())) {
if (!parse(Json::getString(object, kUrl))) {
return;
}
setUser(object["user"].GetString());
setPassword(object["pass"].GetString());
setRigId(object["rig-id"].GetString());
setNicehash(object["nicehash"].GetBool());
setUser(Json::getString(object, kUser));
setPassword(Json::getString(object, kPass));
setRigId(Json::getString(object, kRigId));
setNicehash(Json::getBool(object, kNicehash));
const rapidjson::Value &keepalive = object["keepalive"];
const rapidjson::Value &keepalive = object[kKeepalive];
if (keepalive.IsInt()) {
setKeepAlive(keepalive.GetInt());
}
else if (keepalive.IsBool()) {
setKeepAlive(keepalive.IsTrue() ? kKeepAliveTimeout : 0);
setKeepAlive(keepalive.GetBool());
}
const rapidjson::Value &variant = object["variant"];
const rapidjson::Value &variant = object[kVariant];
if (variant.IsString()) {
algorithm().parseVariant(variant.GetString());
}
@ -105,12 +117,8 @@ xmrig::Pool::Pool(const rapidjson::Value &object) :
algorithm().parseVariant(variant.GetInt());
}
const rapidjson::Value &tls = object["tls"];
if (tls.IsBool()) {
m_tls = tls.IsTrue();
}
m_fingerprint = object["tls-fingerprint"].GetString();
m_tls = Json::getBool(object, kTls);
m_fingerprint = Json::getString(object, kFingerprint);
}
@ -219,7 +227,7 @@ bool xmrig::Pool::parse(const char *url)
return true;
}
const size_t size = port++ - base + 1;
const size_t size = static_cast<size_t>(port++ - base + 1);
char *host = new char[size]();
memcpy(host, base, size - 1);
@ -238,7 +246,7 @@ bool xmrig::Pool::setUserpass(const char *userpass)
}
char *user = new char[p - userpass + 1]();
strncpy(user, userpass, p - userpass);
strncpy(user, userpass, static_cast<size_t>(p - userpass));
m_user = user;
m_password = p + 1;
@ -255,40 +263,40 @@ rapidjson::Value xmrig::Pool::toJSON(rapidjson::Document &doc) const
Value obj(kObjectType);
obj.AddMember("url", m_url.toJSON(), allocator);
obj.AddMember("user", m_user.toJSON(), allocator);
obj.AddMember("pass", m_password.toJSON(), allocator);
obj.AddMember("rig-id", m_rigId.toJSON(), allocator);
obj.AddMember(StringRef(kUrl), m_url.toJSON(), allocator);
obj.AddMember(StringRef(kUser), m_user.toJSON(), allocator);
obj.AddMember(StringRef(kPass), m_password.toJSON(), allocator);
obj.AddMember(StringRef(kRigId), m_rigId.toJSON(), allocator);
# ifndef XMRIG_PROXY_PROJECT
obj.AddMember("nicehash", isNicehash(), allocator);
obj.AddMember(StringRef(kNicehash), isNicehash(), allocator);
# endif
if (m_keepAlive == 0 || m_keepAlive == kKeepAliveTimeout) {
obj.AddMember("keepalive", m_keepAlive > 0, allocator);
obj.AddMember(StringRef(kKeepalive), m_keepAlive > 0, allocator);
}
else {
obj.AddMember("keepalive", m_keepAlive, allocator);
obj.AddMember(StringRef(kKeepalive), m_keepAlive, allocator);
}
switch (m_algorithm.variant()) {
case VARIANT_AUTO:
case VARIANT_0:
case VARIANT_1:
obj.AddMember("variant", m_algorithm.variant(), allocator);
obj.AddMember(StringRef(kVariant), m_algorithm.variant(), allocator);
break;
case VARIANT_2:
obj.AddMember("variant", 2, allocator);
obj.AddMember(StringRef(kVariant), 2, allocator);
break;
default:
obj.AddMember("variant", StringRef(m_algorithm.variantName()), allocator);
obj.AddMember(StringRef(kVariant), StringRef(m_algorithm.variantName()), allocator);
break;
}
obj.AddMember("tls", isTLS(), allocator);
obj.AddMember("tls-fingerprint", m_fingerprint.toJSON(), allocator);
obj.AddMember(StringRef(kTls), isTLS(), allocator);
obj.AddMember(StringRef(kFingerprint), m_fingerprint.toJSON(), allocator);
return obj;
}
@ -345,7 +353,7 @@ bool xmrig::Pool::parseIPv6(const char *addr)
return false;
}
const size_t size = end - addr;
const size_t size = static_cast<size_t>(end - addr);
char *host = new char[size]();
memcpy(host, addr + 1, size - 1);

View file

@ -72,6 +72,7 @@ public:
inline uint16_t port() const { return m_port; }
inline void setFingerprint(const char *fingerprint) { m_fingerprint = fingerprint; }
inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; }
inline void setKeepAlive(bool enable) { setKeepAlive(enable ? kKeepAliveTimeout : 0); }
inline void setNicehash(bool nicehash) { m_nicehash = nicehash; }
inline void setPassword(const char *password) { m_password = password; }
inline void setRigId(const char *rigId) { m_rigId = rigId; }

View file

@ -57,7 +57,7 @@ bool xmrig::Pools::setUrl(const char *url)
Pool pool(url);
if (pool.isValid()) {
m_data.push_back(pool);
m_data.push_back(std::move(pool));
return true;
}
@ -127,6 +127,23 @@ void xmrig::Pools::adjust(const Algorithm &algorithm)
}
void xmrig::Pools::load(const rapidjson::Value &pools)
{
m_data.clear();
for (const rapidjson::Value &value : pools.GetArray()) {
if (!value.IsObject()) {
continue;
}
Pool pool(value);
if (pool.isValid()) {
m_data.push_back(std::move(pool));
}
}
}
void xmrig::Pools::print()
{
size_t i = 1;

View file

@ -49,7 +49,7 @@ public:
inline int retries() const { return m_retries; }
inline int retryPause() const { return m_retryPause; }
inline void setFingerprint(const char *fingerprint) { current().setFingerprint(fingerprint); }
inline void setKeepAlive(bool enable) { setKeepAlive(enable ? Pool::kKeepAliveTimeout : 0); }
inline void setKeepAlive(bool enable) { current().setKeepAlive(enable); }
inline void setKeepAlive(int keepAlive) { current().setKeepAlive(keepAlive); }
inline void setNicehash(bool enable) { current().setNicehash(enable); }
inline void setPassword(const char *password) { current().setPassword(password); }
@ -64,6 +64,7 @@ public:
rapidjson::Value toJSON(rapidjson::Document &doc) const;
size_t active() const;
void adjust(const Algorithm &algorithm);
void load(const rapidjson::Value &pools);
void print();
void setRetries(int retries);
void setRetryPause(int retryPause);

View file

@ -364,7 +364,10 @@ bool xmrig::CommonConfig::parseUint64(int key, uint64_t arg)
void xmrig::CommonConfig::parseJSON(const rapidjson::Document &doc)
{
const rapidjson::Value &pools = doc["pools"];
if (pools.IsArray()) {
m_pools.load(pools);
}
}

View file

@ -96,19 +96,6 @@ bool xmrig::ConfigLoader::loadFromJSON(xmrig::IConfig *config, const rapidjson::
parseJSON(config, &config_options[i], doc);
}
const rapidjson::Value &pools = doc["pools"];
if (pools.IsArray()) {
for (const rapidjson::Value &value : pools.GetArray()) {
if (!value.IsObject()) {
continue;
}
for (size_t i = 0; i < ARRAY_SIZE(pool_options); i++) {
parseJSON(config, &pool_options[i], value);
}
}
}
const rapidjson::Value &api = doc["api"];
if (api.IsObject()) {
for (size_t i = 0; i < ARRAY_SIZE(api_options); i++) {

View file

@ -140,7 +140,7 @@ void xmrig::Network::onPause(IStrategy *strategy)
}
void xmrig::Network::onResultAccepted(IStrategy *strategy, Client *client, const SubmitResult &result, const char *error)
void xmrig::Network::onResultAccepted(IStrategy *, Client *, const SubmitResult &result, const char *error)
{
m_state.add(result, error);
@ -159,7 +159,7 @@ void xmrig::Network::onResultAccepted(IStrategy *strategy, Client *client, const
bool xmrig::Network::isColors() const
{
return m_controller->config()->isColors();
return Log::colors;
}