Better v1 PoW implementation, added variant option.

This commit is contained in:
XMRig 2018-03-12 22:29:44 +07:00
parent 83e5832bc1
commit aec31c43c0
19 changed files with 236 additions and 116 deletions

View file

@ -221,7 +221,7 @@ bool Client::parseJob(const rapidjson::Value &params, int *code)
return false;
}
Job job(m_id, m_nicehash, m_url.isMonero());
Job job(m_id, m_nicehash, m_url.algo(), m_url.variant());
if (!job.setId(params["job_id"].GetString())) {
*code = 3;
return false;
@ -241,6 +241,10 @@ bool Client::parseJob(const rapidjson::Value &params, int *code)
job.setCoin(params["coin"].GetString());
}
if (params.HasMember("variant")) {
job.setVariant(params["variant"].GetInt());
}
if (m_job == job) {
if (!m_quiet) {
LOG_WARN("[%s:%u] duplicate job received, reconnect", m_url.host(), m_url.port());

View file

@ -27,6 +27,7 @@
#include "net/Job.h"
#include "xmrig.h"
static inline unsigned char hf_hex2bin(char c, bool &err)
@ -56,11 +57,26 @@ static inline char hf_bin2hex(unsigned char c)
}
Job::Job(int poolId, bool nicehash, bool monero) :
m_monero(monero),
Job::Job() :
m_nicehash(false),
m_algo(xmrig::ALGO_CRYPTONIGHT),
m_poolId(-2),
m_threadId(-1),
m_variant(xmrig::VARIANT_AUTO),
m_size(0),
m_diff(0),
m_target(0)
{
memset(m_coin, 0, sizeof(m_coin));
}
Job::Job(int poolId, bool nicehash, int algo, int variant) :
m_nicehash(nicehash),
m_algo(algo),
m_poolId(poolId),
m_threadId(-1),
m_variant(variant),
m_size(0),
m_diff(0),
m_target(0)
@ -149,6 +165,22 @@ bool Job::setTarget(const char *target)
}
int Job::variant() const
{
if (m_variant != xmrig::VARIANT_AUTO) {
return m_variant;
}
const uint8_t version = m_blob[0];
if (m_algo == xmrig::ALGO_CRYPTONIGHT) {
return version > 6 ? 1 : 0;
}
return version > 1 ? 1 : 0;
}
void Job::setCoin(const char *coin)
{
if (!coin || strlen(coin) > 4) {
@ -157,7 +189,22 @@ void Job::setCoin(const char *coin)
}
strncpy(m_coin, coin, sizeof(m_coin));
m_monero = strcmp(m_coin, "XMR") == 0;
m_algo = strcmp(m_coin, "AEON") == 0 ? xmrig::ALGO_CRYPTONIGHT_LITE : xmrig::ALGO_CRYPTONIGHT;
}
void Job::setVariant(int variant)
{
switch (variant) {
case xmrig::VARIANT_AUTO:
case xmrig::VARIANT_NONE:
case xmrig::VARIANT_V1:
m_variant = variant;
break;
default:
break;
}
}

View file

@ -37,14 +37,16 @@
class Job
{
public:
Job(int poolId = -2, bool nicehash = false, bool monero = true);
Job();
Job(int poolId, bool nicehash, int algo, int variant);
~Job();
bool setBlob(const char *blob);
bool setTarget(const char *target);
int variant() const;
void setCoin(const char *coin);
void setVariant(int variant);
inline bool isMonero() const { return m_monero; }
inline bool isNicehash() const { return m_nicehash; }
inline bool isValid() const { return m_size > 0 && m_diff > 0; }
inline bool setId(const char *id) { return m_id.setId(id); }
@ -58,7 +60,6 @@ public:
inline uint32_t *nonce() { return reinterpret_cast<uint32_t*>(m_blob + 39); }
inline uint32_t diff() const { return (uint32_t) m_diff; }
inline uint64_t target() const { return m_target; }
inline uint8_t version() const { return isMonero() ? m_blob[0] : 0; }
inline void setNicehash(bool nicehash) { m_nicehash = nicehash; }
inline void setThreadId(int threadId) { m_threadId = threadId; }
@ -77,11 +78,12 @@ public:
private:
VAR_ALIGN(16, uint8_t m_blob[84]); // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk.
bool m_monero;
bool m_nicehash;
char m_coin[5];
int m_algo;
int m_poolId;
int m_threadId;
int m_variant;
size_t m_size;
uint64_t m_diff;
uint64_t m_target;

View file

@ -28,6 +28,7 @@
#include "net/Url.h"
#include "xmrig.h"
#ifdef _MSC_VER
@ -37,11 +38,12 @@
Url::Url() :
m_keepAlive(false),
m_monero(true),
m_nicehash(false),
m_host(nullptr),
m_password(nullptr),
m_user(nullptr),
m_algo(xmrig::ALGO_CRYPTONIGHT),
m_variant(xmrig::VARIANT_AUTO),
m_url(nullptr),
m_port(kDefaultPort)
{
@ -61,11 +63,12 @@ Url::Url() :
*/
Url::Url(const char *url) :
m_keepAlive(false),
m_monero(true),
m_nicehash(false),
m_host(nullptr),
m_password(nullptr),
m_user(nullptr),
m_algo(xmrig::ALGO_CRYPTONIGHT),
m_variant(xmrig::VARIANT_AUTO),
m_url(nullptr),
m_port(kDefaultPort)
{
@ -73,12 +76,13 @@ Url::Url(const char *url) :
}
Url::Url(const char *host, uint16_t port, const char *user, const char *password, bool keepAlive, bool nicehash, bool monero) :
Url::Url(const char *host, uint16_t port, const char *user, const char *password, bool keepAlive, bool nicehash, int variant) :
m_keepAlive(keepAlive),
m_monero(monero),
m_nicehash(nicehash),
m_password(password ? strdup(password) : nullptr),
m_user(user ? strdup(user) : nullptr),
m_algo(xmrig::ALGO_CRYPTONIGHT),
m_variant(variant),
m_url(nullptr),
m_port(port)
{
@ -165,12 +169,14 @@ const char *Url::url() const
}
void Url::applyExceptions()
void Url::adjust(int algo)
{
if (!isValid()) {
return;
}
m_algo = algo;
if (strstr(m_host, ".nicehash.com")) {
m_keepAlive = false;
m_nicehash = true;
@ -204,6 +210,21 @@ void Url::setUser(const char *user)
}
void Url::setVariant(int variant)
{
switch (variant) {
case xmrig::VARIANT_AUTO:
case xmrig::VARIANT_NONE:
case xmrig::VARIANT_V1:
m_variant = variant;
break;
default:
break;
}
}
bool Url::operator==(const Url &other) const
{
if (m_port != other.m_port || m_keepAlive != other.m_keepAlive || m_nicehash != other.m_nicehash) {
@ -221,7 +242,8 @@ bool Url::operator==(const Url &other) const
Url &Url::operator=(const Url *other)
{
m_keepAlive = other->m_keepAlive;
m_monero = other->m_monero;
m_algo = other->m_algo;
m_variant = other->m_variant;
m_nicehash = other->m_nicehash;
m_port = other->m_port;

View file

@ -37,27 +37,29 @@ public:
Url();
Url(const char *url);
Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, bool keepAlive = false, bool nicehash = false, bool monero = true);
Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, bool keepAlive = false, bool nicehash = false, int variant = -1);
~Url();
inline bool isKeepAlive() const { return m_keepAlive; }
inline bool isMonero() const { return m_monero; }
inline bool isNicehash() const { return m_nicehash; }
inline bool isValid() const { return m_host && m_port > 0; }
inline const char *host() const { return m_host; }
inline const char *password() const { return m_password ? m_password : kDefaultPassword; }
inline const char *user() const { return m_user ? m_user : kDefaultUser; }
inline int algo() const { return m_algo; }
inline int variant() const { return m_variant; }
inline uint16_t port() const { return m_port; }
inline void setKeepAlive(bool keepAlive) { m_keepAlive = keepAlive; }
inline void setMonero(bool monero) { m_monero = monero; }
inline void setNicehash(bool nicehash) { m_nicehash = nicehash; }
inline void setVariant(bool monero) { m_variant = monero; }
bool parse(const char *url);
bool setUserpass(const char *userpass);
const char *url() const;
void applyExceptions();
void adjust(int algo);
void setPassword(const char *password);
void setUser(const char *user);
void setVariant(int variant);
bool operator==(const Url &other) const;
Url &operator=(const Url *other);
@ -66,11 +68,12 @@ private:
bool parseIPv6(const char *addr);
bool m_keepAlive;
bool m_monero;
bool m_nicehash;
char *m_host;
char *m_password;
char *m_user;
int m_algo;
int m_variant;
mutable char *m_url;
uint16_t m_port;
};

View file

@ -27,6 +27,7 @@
#include "net/Job.h"
#include "net/strategies/DonateStrategy.h"
#include "Options.h"
#include "xmrig.h"
extern "C"
@ -48,7 +49,7 @@ DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) :
keccak(reinterpret_cast<const uint8_t *>(user), static_cast<int>(strlen(user)), hash, sizeof(hash));
Job::toHex(hash, 32, userId);
Url *url = new Url("thanks.xmrig.com", Options::i()->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 80, userId, nullptr, false, true);
Url *url = new Url("thanks.xmrig.com", Options::i()->algo() == xmrig::ALGO_CRYPTONIGHT_LITE ? 3333 : 80, userId, nullptr, false, true);
m_client = new Client(-1, agent, this);
m_client->setUrl(url);