diff --git a/src/Options.cpp b/src/Options.cpp index 36d570b4..17d8654b 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -83,6 +83,7 @@ Options:\n\ --print-time=N print hashrate report every N seconds\n\ -h, --help display this help and exit\n\ -V, --version output version information and exit\n\ + --rand-nonce generate random nonces instead of monotonically increasing ones\n\ "; @@ -116,6 +117,7 @@ static struct option const options[] = { { "user-agent", 1, nullptr, 1008 }, { "userpass", 1, nullptr, 'O' }, { "version", 0, nullptr, 'V' }, + { "rand-nonce", 0, nullptr, 1008 }, { 0, 0, 0, 0 } }; @@ -477,6 +479,10 @@ bool Options::parseBoolean(int key, bool enable) m_colors = enable; break; + case 1008: /* --rand-nonce */ + m_pools.back()->setRandNonce(true); + break; + default: break; } diff --git a/src/net/Client.cpp b/src/net/Client.cpp index c2518be9..92d51abe 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -228,7 +228,7 @@ bool Client::parseJob(const json_t *params, int *code) return false; } - Job job(m_id, m_url.isNicehash()); + Job job(m_id, m_url.isNicehash(), m_url.isRandNonce()); if (!job.setId(json_string_value(json_object_get(params, "job_id")))) { *code = 3; return false; diff --git a/src/net/Job.cpp b/src/net/Job.cpp index bce65e62..bc39925a 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -56,8 +56,9 @@ static inline char hf_bin2hex(unsigned char c) } -Job::Job(int poolId, bool nicehash) : +Job::Job(int poolId, bool nicehash, bool randnonce) : m_nicehash(nicehash), + m_randnonce(randnonce), m_poolId(poolId), m_size(0), m_diff(0), diff --git a/src/net/Job.h b/src/net/Job.h index 86160584..3989d1d8 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -27,19 +27,19 @@ #include - #include "align.h" class Job { public: - Job(int poolId = -2, bool nicehash = false); + Job(int poolId = -2, bool nicehash = false, bool randnonce = false); bool setBlob(const char *blob); bool setId(const char *id); bool setTarget(const char *target); inline bool isNicehash() const { return m_nicehash; } + inline bool isRandNonce() const { return m_randnonce; } inline bool isValid() const { return m_size > 0 && m_diff > 0; } inline const char *id() const { return m_id; } inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } @@ -50,6 +50,7 @@ public: inline uint32_t diff() const { return (uint32_t) m_diff; } inline uint64_t target() const { return m_target; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } + inline void setRandNonce(bool randnonce) { m_randnonce = randnonce; } # ifdef XMRIG_PROXY_PROJECT inline char *rawBlob() { return m_rawBlob; } @@ -65,6 +66,7 @@ public: private: bool m_nicehash; + bool m_randnonce; int m_poolId; VAR_ALIGN(16, char m_id[64]); 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. diff --git a/src/net/Url.cpp b/src/net/Url.cpp index a0024d26..00983028 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -25,7 +25,6 @@ #include #include - #include "net/Url.h" @@ -37,6 +36,7 @@ Url::Url() : m_keepAlive(false), m_nicehash(false), + m_randnonce(false), m_host(nullptr), m_password(nullptr), m_user(nullptr), @@ -59,6 +59,7 @@ Url::Url() : Url::Url(const char *url) : m_keepAlive(false), m_nicehash(false), + m_randnonce(false), m_host(nullptr), m_password(nullptr), m_user(nullptr), @@ -68,9 +69,10 @@ Url::Url(const char *url) : } -Url::Url(const char *host, uint16_t port, const char *user, const char *password, bool keepAlive, bool nicehash) : +Url::Url(const char *host, uint16_t port, const char *user, const char *password, bool keepAlive, bool nicehash, bool randnonce) : m_keepAlive(keepAlive), m_nicehash(nicehash), + m_randnonce(randnonce), m_password(password ? strdup(password) : nullptr), m_user(user ? strdup(user) : nullptr), m_port(port) @@ -91,6 +93,10 @@ bool Url::isNicehash() const { return isValid() && (m_nicehash || strstr(m_host, ".nicehash.com")); } +bool Url::isRandNonce() const +{ + return m_randnonce; +} bool Url::parse(const char *url) @@ -170,6 +176,7 @@ Url &Url::operator=(const Url *other) { m_keepAlive = other->m_keepAlive; m_nicehash = other->m_nicehash; + m_randnonce = other->m_randnonce; m_port = other->m_port; free(m_host); diff --git a/src/net/Url.h b/src/net/Url.h index 43197195..4b9a56bd 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -37,7 +37,7 @@ 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 ); + Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, bool keepAlive = false, bool nicehash = false, bool randnonce = false ); ~Url(); inline bool isKeepAlive() const { return m_keepAlive; } @@ -48,8 +48,10 @@ public: inline uint16_t port() const { return m_port; } inline void setKeepAlive(bool keepAlive) { m_keepAlive = keepAlive; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } + inline void setRandNonce(bool randnonce) { m_randnonce = randnonce; } bool isNicehash() const; + bool isRandNonce() const; bool parse(const char *url); bool setUserpass(const char *userpass); void setPassword(const char *password); @@ -60,6 +62,7 @@ public: private: bool m_keepAlive; bool m_nicehash; + bool m_randnonce; char *m_host; char *m_password; char *m_user; diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index 34045f74..4d590dcb 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -23,6 +23,7 @@ #include +#include #include "crypto/CryptoNight.h" @@ -38,6 +39,8 @@ SingleWorker::SingleWorker(Handle *handle) void SingleWorker::start() { + srand(getpid() * time(NULL)); + bool israndnonce = false; while (Workers::sequence() > 0) { if (Workers::isPaused()) { do { @@ -52,16 +55,25 @@ void SingleWorker::start() consumeJob(); } + israndnonce = m_job.isRandNonce(); + if (israndnonce) { + m_result.nonce += rand(); + } while (!Workers::isOutdated(m_sequence)) { if ((m_count & 0xF) == 0) { storeStats(); } m_count++; - *m_job.nonce() = ++m_result.nonce; + if (israndnonce && (m_count & 0xFF) == 0) { + *m_job.nonce() = m_result.nonce += (rand() + time(NULL) + getpid()); + } else { + *m_job.nonce() = ++m_result.nonce; + } if (CryptoNight::hash(m_job, m_result, m_ctx)) { Workers::submit(m_result); + m_result.nonce += (rand() + time(NULL) + getpid()); } std::this_thread::yield();