From a0d4e4ed3f5fa1ead45a7a808b223e99c16f0029 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 20 Feb 2018 23:22:34 +0700 Subject: [PATCH 01/23] Run internal http server in main loop to avoid requirement to thread synchronization. --- src/api/Api.cpp | 14 ++------------ src/api/Api.h | 3 +-- src/api/Httpd.cpp | 28 ++++++++++++++++++++++++++-- src/api/Httpd.h | 5 ++++- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/api/Api.cpp b/src/api/Api.cpp index 729ebccd..98d441c1 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -4,7 +4,7 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig + * Copyright 2016-2018 XMRig * * * This program is free software: you can redistribute it and/or modify @@ -29,12 +29,10 @@ ApiState *Api::m_state = nullptr; -uv_mutex_t Api::m_mutex; bool Api::start() { - uv_mutex_init(&m_mutex); m_state = new ApiState(); return true; @@ -53,11 +51,7 @@ char *Api::get(const char *url, int *status) return nullptr; } - uv_mutex_lock(&m_mutex); - char *buf = m_state->get(url, status); - uv_mutex_unlock(&m_mutex); - - return buf; + return m_state->get(url, status); } @@ -67,9 +61,7 @@ void Api::tick(const Hashrate *hashrate) return; } - uv_mutex_lock(&m_mutex); m_state->tick(hashrate); - uv_mutex_unlock(&m_mutex); } @@ -79,7 +71,5 @@ void Api::tick(const NetworkState &network) return; } - uv_mutex_lock(&m_mutex); m_state->tick(network); - uv_mutex_unlock(&m_mutex); } diff --git a/src/api/Api.h b/src/api/Api.h index 72c65c3c..1f91b382 100644 --- a/src/api/Api.h +++ b/src/api/Api.h @@ -4,7 +4,7 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig + * Copyright 2016-2018 XMRig * * * This program is free software: you can redistribute it and/or modify @@ -45,7 +45,6 @@ public: private: static ApiState *m_state; - static uv_mutex_t m_mutex; }; #endif /* __API_H__ */ diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index 996bc007..be696542 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -4,7 +4,7 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig + * Copyright 2016-2018 XMRig * * * This program is free software: you can redistribute it and/or modify @@ -36,6 +36,18 @@ Httpd::Httpd(int port, const char *accessToken) : m_port(port), m_daemon(nullptr) { + uv_idle_init(uv_default_loop(), &m_idle); + m_idle.data = this; +} + + +Httpd::~Httpd() +{ + uv_idle_stop(&m_idle); + + if (m_daemon) { + MHD_stop_daemon(m_daemon); + } } @@ -45,12 +57,18 @@ bool Httpd::start() return false; } - m_daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); + unsigned int flags = 0; + if (MHD_is_feature_supported(MHD_FEATURE_EPOLL)) { + flags |= MHD_USE_EPOLL_LINUX_ONLY; + } + + m_daemon = MHD_start_daemon(flags, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); if (!m_daemon) { LOG_ERR("HTTP Daemon failed to start."); return false; } + uv_idle_start(&m_idle, Httpd::onIdle); return true; } @@ -114,3 +132,9 @@ int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url MHD_Response *rsp = MHD_create_response_from_buffer(strlen(buf), (void*) buf, MHD_RESPMEM_MUST_FREE); return done(connection, status, rsp); } + + +void Httpd::onIdle(uv_idle_t *handle) +{ + MHD_run(static_cast(handle->data)->m_daemon); +} diff --git a/src/api/Httpd.h b/src/api/Httpd.h index 7a4dd932..6563364d 100644 --- a/src/api/Httpd.h +++ b/src/api/Httpd.h @@ -4,7 +4,7 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig + * Copyright 2016-2018 XMRig * * * This program is free software: you can redistribute it and/or modify @@ -37,6 +37,7 @@ class Httpd { public: Httpd(int port, const char *accessToken); + ~Httpd(); bool start(); private: @@ -44,10 +45,12 @@ private: static int done(MHD_Connection *connection, int status, MHD_Response *rsp); static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls); + static void onIdle(uv_idle_t *handle); const char *m_accessToken; const int m_port; MHD_Daemon *m_daemon; + uv_idle_t m_idle; }; #endif /* __HTTPD_H__ */ From ff6dc727f56f7adb2a10127cb4fc9e02dbc0bbea Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 21 Feb 2018 00:52:52 +0700 Subject: [PATCH 02/23] Added XMRIG_DEPS cmake variable for unified dependencies. --- cmake/FindMHD.cmake | 8 ++++++-- cmake/FindUV.cmake | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/cmake/FindMHD.cmake b/cmake/FindMHD.cmake index 8505b337..a20ecfec 100644 --- a/cmake/FindMHD.cmake +++ b/cmake/FindMHD.cmake @@ -8,12 +8,16 @@ find_path( MHD_INCLUDE_DIR NAMES microhttpd.h + PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" + PATH_SUFFIXES "include" DOC "microhttpd include dir" ) find_library( MHD_LIBRARY - NAMES microhttpd microhttpd-10 libmicrohttpd libmicrohttpd-dll + NAMES microhttpd libmicrohttpd + PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" + PATH_SUFFIXES "lib" DOC "microhttpd library" ) @@ -34,6 +38,6 @@ if (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC) endif() include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(mhd DEFAULT_MSG MHD_INCLUDE_DIR MHD_LIBRARY) +find_package_handle_standard_args(MHD DEFAULT_MSG MHD_LIBRARY MHD_INCLUDE_DIR) mark_as_advanced(MHD_INCLUDE_DIR MHD_LIBRARY) diff --git a/cmake/FindUV.cmake b/cmake/FindUV.cmake index e3c22d28..7906633f 100644 --- a/cmake/FindUV.cmake +++ b/cmake/FindUV.cmake @@ -1,5 +1,16 @@ -find_path(UV_INCLUDE_DIR NAMES uv.h) -find_library(UV_LIBRARY NAMES uv libuv) +find_path( + UV_INCLUDE_DIR + NAMES uv.h + PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" + PATH_SUFFIXES "include" +) + +find_library( + UV_LIBRARY + NAMES libuv.a uv libuv + PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" + PATH_SUFFIXES "lib" +) set(UV_LIBRARIES ${UV_LIBRARY}) set(UV_INCLUDE_DIRS ${UV_INCLUDE_DIR}) From 3edc30d0672c867ffea87392b743fcd3b4f53b87 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 1 Mar 2018 09:53:27 +0700 Subject: [PATCH 03/23] Use adaptive timer instead of idle handler for HTTP server. --- src/api/Httpd.cpp | 33 ++++++++++++++++++++++++++------- src/api/Httpd.h | 9 +++++++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index be696542..51476e67 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -32,22 +32,25 @@ Httpd::Httpd(int port, const char *accessToken) : - m_accessToken(accessToken), + m_idle(true), + m_accessToken(accessToken ? strdup(accessToken) : nullptr), m_port(port), m_daemon(nullptr) { - uv_idle_init(uv_default_loop(), &m_idle); - m_idle.data = this; + uv_timer_init(uv_default_loop(), &m_timer); + m_timer.data = this; } Httpd::~Httpd() { - uv_idle_stop(&m_idle); + uv_timer_stop(&m_timer); if (m_daemon) { MHD_stop_daemon(m_daemon); } + + delete m_accessToken; } @@ -68,7 +71,7 @@ bool Httpd::start() return false; } - uv_idle_start(&m_idle, Httpd::onIdle); + uv_timer_start(&m_timer, Httpd::onTimer, kIdleInterval, kIdleInterval); return true; } @@ -92,6 +95,22 @@ int Httpd::auth(const char *header) } +void Httpd::run() +{ + MHD_run(m_daemon); + + const MHD_DaemonInfo *info = MHD_get_daemon_info(m_daemon, MHD_DAEMON_INFO_CURRENT_CONNECTIONS); + if (m_idle && info->num_connections) { + uv_timer_set_repeat(&m_timer, kActiveInterval); + m_idle = false; + } + else if (!m_idle && !info->num_connections) { + uv_timer_set_repeat(&m_timer, kIdleInterval); + m_idle = true; + } +} + + int Httpd::done(MHD_Connection *connection, int status, MHD_Response *rsp) { if (!rsp) { @@ -134,7 +153,7 @@ int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url } -void Httpd::onIdle(uv_idle_t *handle) +void Httpd::onTimer(uv_timer_t *handle) { - MHD_run(static_cast(handle->data)->m_daemon); + static_cast(handle->data)->run(); } diff --git a/src/api/Httpd.h b/src/api/Httpd.h index 6563364d..1bdfeeb6 100644 --- a/src/api/Httpd.h +++ b/src/api/Httpd.h @@ -41,16 +41,21 @@ public: bool start(); private: + constexpr static const int kIdleInterval = 200; + constexpr static const int kActiveInterval = 50; + int auth(const char *header); + void run(); static int done(MHD_Connection *connection, int status, MHD_Response *rsp); static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls); - static void onIdle(uv_idle_t *handle); + static void onTimer(uv_timer_t *handle); + bool m_idle; const char *m_accessToken; const int m_port; MHD_Daemon *m_daemon; - uv_idle_t m_idle; + uv_timer_t m_timer; }; #endif /* __HTTPD_H__ */ From 48b1de0b59bc2969fd9a2b951773488b9501ae4e Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Fri, 2 Mar 2018 22:10:46 -0500 Subject: [PATCH 04/23] Changes for the Monero v1 PoW --- src/crypto/CryptoNight.cpp | 102 +++++++++++++++++++++++------------ src/crypto/CryptoNight.h | 4 +- src/crypto/CryptoNight_arm.h | 26 +++++++-- src/crypto/CryptoNight_x86.h | 26 +++++++-- src/workers/DoubleWorker.cpp | 18 ++++--- src/workers/SingleWorker.cpp | 4 +- 6 files changed, 128 insertions(+), 52 deletions(-) diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index 3ac9e94b..ecc051f2 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -24,6 +24,34 @@ #include "crypto/CryptoNight.h" +// VARIANT ALTERATIONS +#define VARIANT1_CHECK() \ + if (MONERO && version > 6 && size < 43) \ + { \ + return false; \ + } + +#define VARIANT1_INIT(part) \ + const uint64_t tweak1_2_##part =\ + (MONERO && version > 6) ? \ + (*reinterpret_cast(reinterpret_cast(input) + 35 + part * size) ^ \ + *(reinterpret_cast(ctx->state##part) + 24)) : 0; + +#define VARIANT1_1(p) \ + do if(version > 6) \ + { \ + const uint8_t tmp = reinterpret_cast(p)[11]; \ + static const uint32_t table = 0x75310; \ + const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; \ + ((uint8_t*)(p))[11] = tmp ^ ((table >> index) & 0x30); \ + } while(0) + +#define VARIANT1_2(p, part) \ + do if(MONERO && version > 6) { \ + (p) ^= tweak1_2_##part ; \ + } while(0) + + #if defined(XMRIG_ARM) # include "crypto/CryptoNight_arm.h" #else @@ -36,58 +64,67 @@ #include "Options.h" -void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx) = nullptr; + +bool (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = nullptr; -static void cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx) { +static bool cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, false>(input, size, output, ctx); + return cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, false, true>(input, size, output, ctx, version); +# else + return false; # endif } -static void cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { +static bool cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, false>(input, size, output, ctx); + return cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, false, true>(input, size, output, ctx, version); +# else + return false; # endif } -static void cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, true>(input, size, output, ctx); +static bool cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + return cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, true, true>(input, size, output, ctx, version); } -static void cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, true>(input, size, output, ctx); +static bool cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + return cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, true, true>(input, size, output, ctx, version); } #ifndef XMRIG_NO_AEON -static void cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - # if !defined(XMRIG_ARMv7) - cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, false>(input, size, output, ctx); -#endif -} - - -static void cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { +static bool cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, false>(input, size, output, ctx); + return cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, false, false>(input, size, output, ctx, version); +# else + return false; # endif } -static void cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, true>(input, size, output, ctx); +static bool cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +# if !defined(XMRIG_ARMv7) + return cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, false, false>(input, size, output, ctx, version); +# else + return false; +# endif } -static void cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx) { - cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, true>(input, size, output, ctx); +static bool cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + return cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, true, false>(input, size, output, ctx, version); } -void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx) = { + +static bool cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + return cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, true, false>(input, size, output, ctx, version); +} + +bool (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -98,7 +135,7 @@ void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_lite_av4_softaes_double }; #else -void (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx) = { +bool (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -107,11 +144,10 @@ void (*cryptonight_variations[4])(const void *input, size_t size, void *output, #endif -bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx) +bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx, uint8_t version) { - cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx); - - return *reinterpret_cast(result.result + 24) < job.target(); + const bool rc = cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, version); + return rc && *reinterpret_cast(result.result + 24) < job.target(); } @@ -133,9 +169,9 @@ bool CryptoNight::init(int algo, int variant) } -void CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx) +bool CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version) { - cryptonight_hash_ctx(input, size, output, ctx); + return cryptonight_hash_ctx(input, size, output, ctx, version); } @@ -149,14 +185,14 @@ bool CryptoNight::selfTest(int algo) { struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) _mm_malloc(sizeof(struct cryptonight_ctx), 16); ctx->memory = (uint8_t *) _mm_malloc(MEMORY * 2, 16); - cryptonight_hash_ctx(test_input, 76, output, ctx); + const bool rc = cryptonight_hash_ctx(test_input, 76, output, ctx, 0); _mm_free(ctx->memory); _mm_free(ctx); # ifndef XMRIG_NO_AEON - return memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; + return rc && memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; # else - return memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; + return rc && memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; # endif } diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index 64fc0fd1..890e6263 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -50,9 +50,9 @@ class JobResult; class CryptoNight { public: - static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx); + static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx, uint8_t version); static bool init(int algo, int variant); - static void hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx); + static bool hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version); private: static bool selfTest(int algo); diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 4ac14f34..513fa884 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -344,11 +344,14 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } -template -inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx) +template +inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak(static_cast(input), (int) size, ctx->state0, 200); + VARIANT1_CHECK(); + VARIANT1_INIT(0); + cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); const uint8_t* l0 = ctx->memory; @@ -374,6 +377,7 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * } _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + VARIANT1_1(&l0[idx0 & MASK]); idx0 = EXTRACT64(cx); bx0 = cx; @@ -385,8 +389,10 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*)&l0[idx0 & MASK])[0] = al0; ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -397,15 +403,20 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * keccakf(h0, 24); extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); + return true; } -template -inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +template +inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); + VARIANT1_CHECK(); + VARIANT1_INIT(0); + VARIANT1_INIT(1); + const uint8_t* l0 = ctx->memory; const uint8_t* l1 = ctx->memory + MEM; uint64_t* h0 = reinterpret_cast(ctx->state0); @@ -443,6 +454,8 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + VARIANT1_1(&l0[idx0 & MASK]); + VARIANT1_1(&l1[idx1 & MASK]); idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); @@ -458,8 +471,10 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*) &l0[idx0 & MASK])[0] = al0; ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -472,8 +487,10 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, al1 += hi; ah1 += lo; + VARIANT1_2(ah1, 1); ((uint64_t*) &l1[idx1 & MASK])[0] = al1; ((uint64_t*) &l1[idx1 & MASK])[1] = ah1; + VARIANT1_2(ah1, 1); ah1 ^= ch; al1 ^= cl; @@ -488,6 +505,7 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, static_cast(output) + 32); + return true; } #endif /* __CRYPTONIGHT_ARM_H__ */ diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 927aab72..a9500850 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -307,11 +307,14 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } -template -inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx) +template +inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak(static_cast(input), (int) size, ctx->state0, 200); + VARIANT1_CHECK(); + VARIANT1_INIT(0); + cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); const uint8_t* l0 = ctx->memory; @@ -334,6 +337,7 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); } _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx)); + VARIANT1_1(&l0[idx0 & MASK]); idx0 = EXTRACT64(cx); bx0 = cx; @@ -345,8 +349,10 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*)&l0[idx0 & MASK])[0] = al0; ((uint64_t*)&l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -357,15 +363,20 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * keccakf(h0, 24); extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); + return true; } -template -inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) +template +inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); + VARIANT1_CHECK(); + VARIANT1_INIT(0); + VARIANT1_INIT(1); + const uint8_t* l0 = ctx->memory; const uint8_t* l1 = ctx->memory + MEM; uint64_t* h0 = reinterpret_cast(ctx->state0); @@ -401,6 +412,8 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, _mm_store_si128((__m128i *) &l0[idx0 & MASK], _mm_xor_si128(bx0, cx0)); _mm_store_si128((__m128i *) &l1[idx1 & MASK], _mm_xor_si128(bx1, cx1)); + VARIANT1_1(&l0[idx0 & MASK]); + VARIANT1_1(&l1[idx1 & MASK]); idx0 = EXTRACT64(cx0); idx1 = EXTRACT64(cx1); @@ -416,8 +429,10 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, al0 += hi; ah0 += lo; + VARIANT1_2(ah0, 0); ((uint64_t*) &l0[idx0 & MASK])[0] = al0; ((uint64_t*) &l0[idx0 & MASK])[1] = ah0; + VARIANT1_2(ah0, 0); ah0 ^= ch; al0 ^= cl; @@ -430,8 +445,10 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, al1 += hi; ah1 += lo; + VARIANT1_2(ah1, 1); ((uint64_t*) &l1[idx1 & MASK])[0] = al1; ((uint64_t*) &l1[idx1 & MASK])[1] = ah1; + VARIANT1_2(ah1, 1); ah1 ^= ch; al1 ^= cl; @@ -446,6 +463,7 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, static_cast(output) + 32); + return true; } #endif /* __CRYPTONIGHT_X86_H__ */ diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index 46d6f366..8c49649c 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -76,6 +76,8 @@ void DoubleWorker::start() consumeJob(); } + const uint8_t version = m_state->job.size() ? m_state->job.blob()[0] : 0; + while (!Workers::isOutdated(m_sequence)) { if ((m_count & 0xF) == 0) { storeStats(); @@ -85,15 +87,15 @@ void DoubleWorker::start() *Job::nonce(m_state->blob) = ++m_state->nonce1; *Job::nonce(m_state->blob + m_state->job.size()) = ++m_state->nonce2; - CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx); + if (CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, version)) { + if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { + Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); + } - if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { - Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); - } - - if (*reinterpret_cast(m_hash + 32 + 24) < m_state->job.target()) { - Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce2, m_hash + 32, m_state->job.diff())); - } + if (*reinterpret_cast(m_hash + 32 + 24) < m_state->job.target()) { + Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce2, m_hash + 32, m_state->job.diff())); + } + } // print on error ? std::this_thread::yield(); } diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index ecb566f8..3a2cc32c 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -52,6 +52,8 @@ void SingleWorker::start() consumeJob(); } + const uint8_t version = m_job.size() ? m_job.blob()[0] : 0; + while (!Workers::isOutdated(m_sequence)) { if ((m_count & 0xF) == 0) { storeStats(); @@ -60,7 +62,7 @@ void SingleWorker::start() m_count++; *m_job.nonce() = ++m_result.nonce; - if (CryptoNight::hash(m_job, m_result, m_ctx)) { + if (CryptoNight::hash(m_job, m_result, m_ctx, version)) { Workers::submit(m_result); } From df6a397e52dc3f08afc9b9e97cccd9ac9a8178f6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 6 Mar 2018 17:06:07 +0700 Subject: [PATCH 05/23] Update copyright and move version into Job class. --- src/crypto/CryptoNight.cpp | 9 +++++---- src/crypto/CryptoNight.h | 7 ++++--- src/crypto/CryptoNight_arm.h | 5 +++-- src/crypto/CryptoNight_x86.h | 5 +++-- src/net/Job.cpp | 5 +++-- src/net/Job.h | 6 ++++-- src/workers/DoubleWorker.cpp | 9 ++++----- src/workers/DoubleWorker.h | 5 +++-- src/workers/SingleWorker.cpp | 9 ++++----- src/workers/SingleWorker.h | 5 +++-- 10 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index ecc051f2..8de52c5a 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 @@ -144,9 +145,9 @@ bool (*cryptonight_variations[4])(const void *input, size_t size, void *output, #endif -bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx, uint8_t version) +bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx) { - const bool rc = cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, version); + const bool rc = cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, job.version()); return rc && *reinterpret_cast(result.result + 24) < job.target(); } diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index 890e6263..6fa4719c 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 @@ -50,7 +51,7 @@ class JobResult; class CryptoNight { public: - static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx, uint8_t version); + static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx); static bool init(int algo, int variant); static bool hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version); diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 513fa884..b5dc789d 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -5,8 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2016 Imran Yusuff - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index a9500850..c2f02645 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 diff --git a/src/net/Job.cpp b/src/net/Job.cpp index c3d33739..e0e49b43 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 diff --git a/src/net/Job.h b/src/net/Job.h index bf8f8835..d74b64ed 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 @@ -54,6 +55,7 @@ public: inline uint32_t *nonce() { return reinterpret_cast(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 m_blob[0]; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } inline void setThreadId(int threadId) { m_threadId = threadId; } diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index 8c49649c..aac503f1 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 @@ -76,8 +77,6 @@ void DoubleWorker::start() consumeJob(); } - const uint8_t version = m_state->job.size() ? m_state->job.blob()[0] : 0; - while (!Workers::isOutdated(m_sequence)) { if ((m_count & 0xF) == 0) { storeStats(); @@ -87,7 +86,7 @@ void DoubleWorker::start() *Job::nonce(m_state->blob) = ++m_state->nonce1; *Job::nonce(m_state->blob + m_state->job.size()) = ++m_state->nonce2; - if (CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, version)) { + if (CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, m_state->job.version())) { if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); } diff --git a/src/workers/DoubleWorker.h b/src/workers/DoubleWorker.h index 711f4bd1..9f29fa5b 100644 --- a/src/workers/DoubleWorker.h +++ b/src/workers/DoubleWorker.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index 3a2cc32c..9f4a7484 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 @@ -52,8 +53,6 @@ void SingleWorker::start() consumeJob(); } - const uint8_t version = m_job.size() ? m_job.blob()[0] : 0; - while (!Workers::isOutdated(m_sequence)) { if ((m_count & 0xF) == 0) { storeStats(); @@ -62,7 +61,7 @@ void SingleWorker::start() m_count++; *m_job.nonce() = ++m_result.nonce; - if (CryptoNight::hash(m_job, m_result, m_ctx, version)) { + if (CryptoNight::hash(m_job, m_result, m_ctx)) { Workers::submit(m_result); } diff --git a/src/workers/SingleWorker.h b/src/workers/SingleWorker.h index 08ab1857..f7d9cff8 100644 --- a/src/workers/SingleWorker.h +++ b/src/workers/SingleWorker.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 From 8b6058500439b9a38c072796a42d7beaf3b70cc1 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 6 Mar 2018 21:34:20 +0700 Subject: [PATCH 06/23] PoW changes WIP --- CMakeLists.txt | 1 + src/Mem.cpp | 17 +++--- src/Mem_unix.cpp | 9 ++-- src/Mem_win.cpp | 7 +-- src/crypto/CryptoNight.cpp | 92 +++++++++++---------------------- src/crypto/CryptoNight.h | 10 ++-- src/crypto/CryptoNight_monero.h | 57 ++++++++++++++++++++ src/crypto/CryptoNight_x86.h | 3 +- src/workers/DoubleWorker.cpp | 16 +++--- 9 files changed, 121 insertions(+), 91 deletions(-) create mode 100644 src/crypto/CryptoNight_monero.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e1673ea..068642bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,7 @@ set(HEADERS_CRYPTO src/crypto/c_keccak.h src/crypto/c_skein.h src/crypto/CryptoNight.h + src/crypto/CryptoNight_monero.h src/crypto/CryptoNight_test.h src/crypto/groestl_tables.h src/crypto/hash.h diff --git a/src/Mem.cpp b/src/Mem.cpp index 4437c673..1943b22a 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 @@ -46,10 +47,10 @@ cryptonight_ctx *Mem::create(int threadId) } # endif - cryptonight_ctx *ctx = reinterpret_cast(&m_memory[MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); + cryptonight_ctx *ctx = reinterpret_cast(&m_memory[MONERO_MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); const int ratio = m_doubleHash ? 2 : 1; - ctx->memory = &m_memory[MEMORY * (threadId * ratio + 1)]; + ctx->memory = &m_memory[MONERO_MEMORY * (threadId * ratio + 1)]; return ctx; } @@ -72,15 +73,15 @@ cryptonight_ctx *Mem::createLite(int threadId) { cryptonight_ctx *ctx; if (!m_doubleHash) { - const size_t offset = MEMORY * (threadId + 1); + const size_t offset = MONERO_MEMORY * (threadId + 1); - ctx = reinterpret_cast(&m_memory[offset + MEMORY_LITE]); + ctx = reinterpret_cast(&m_memory[offset + AEON_MEMORY]); ctx->memory = &m_memory[offset]; return ctx; } - ctx = reinterpret_cast(&m_memory[MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); - ctx->memory = &m_memory[MEMORY * (threadId + 1)]; + ctx = reinterpret_cast(&m_memory[MONERO_MEMORY - sizeof(cryptonight_ctx) * (threadId + 1)]); + ctx->memory = &m_memory[MONERO_MEMORY * (threadId + 1)]; return ctx; } diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 43ffa664..ec4017da 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 @@ -46,7 +47,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_doubleHash = doubleHash; const int ratio = (doubleHash && algo != Options::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; - const size_t size = MEMORY * (threads * ratio + 1); + const size_t size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { m_memory = static_cast(_mm_malloc(size, 16)); @@ -83,7 +84,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) void Mem::release() { - const int size = MEMORY * (m_threads + 1); + const int size = MONERO_MEMORY * (m_threads + 1); if (m_flags & HugepagesEnabled) { if (m_flags & Lock) { diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index a4f92cfe..79c6d12e 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 @@ -151,7 +152,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_doubleHash = doubleHash; const int ratio = (doubleHash && algo != Options::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; - const size_t size = MEMORY * (threads * ratio + 1); + const size_t size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { m_memory = static_cast(_mm_malloc(size, 16)); diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index 8de52c5a..cdba4051 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -25,33 +25,6 @@ #include "crypto/CryptoNight.h" -// VARIANT ALTERATIONS -#define VARIANT1_CHECK() \ - if (MONERO && version > 6 && size < 43) \ - { \ - return false; \ - } - -#define VARIANT1_INIT(part) \ - const uint64_t tweak1_2_##part =\ - (MONERO && version > 6) ? \ - (*reinterpret_cast(reinterpret_cast(input) + 35 + part * size) ^ \ - *(reinterpret_cast(ctx->state##part) + 24)) : 0; - -#define VARIANT1_1(p) \ - do if(version > 6) \ - { \ - const uint8_t tmp = reinterpret_cast(p)[11]; \ - static const uint32_t table = 0x75310; \ - const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; \ - ((uint8_t*)(p))[11] = tmp ^ ((table >> index) & 0x30); \ - } while(0) - -#define VARIANT1_2(p, part) \ - do if(MONERO && version > 6) { \ - (p) ^= tweak1_2_##part ; \ - } while(0) - #if defined(XMRIG_ARM) # include "crypto/CryptoNight_arm.h" @@ -66,66 +39,58 @@ -bool (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = nullptr; +void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = nullptr; -static bool cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx, uint8_t version) { +static void cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - return cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, false, true>(input, size, output, ctx, version); -# else - return false; + cryptonight_hash(input, size, output, ctx, version); # endif } -static bool cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +static void cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - return cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, false, true>(input, size, output, ctx, version); -# else - return false; + cryptonight_double_hash(input, size, output, ctx, version); # endif } -static bool cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - return cryptonight_hash<0x80000, MEMORY, 0x1FFFF0, true, true>(input, size, output, ctx, version); +static void cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + cryptonight_hash(input, size, output, ctx, version); } -static bool cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - return cryptonight_double_hash<0x80000, MEMORY, 0x1FFFF0, true, true>(input, size, output, ctx, version); +static void cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + cryptonight_double_hash(input, size, output, ctx, version); } #ifndef XMRIG_NO_AEON -static bool cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +static void cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - return cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, false, false>(input, size, output, ctx, version); -# else - return false; + cryptonight_hash(input, size, output, ctx, version); # endif } -static bool cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +static void cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - return cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, false, false>(input, size, output, ctx, version); -# else - return false; + cryptonight_double_hash(input, size, output, ctx, version); # endif } -static bool cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - return cryptonight_hash<0x40000, MEMORY_LITE, 0xFFFF0, true, false>(input, size, output, ctx, version); +static void cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + cryptonight_hash(input, size, output, ctx, version); } -static bool cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - return cryptonight_double_hash<0x40000, MEMORY_LITE, 0xFFFF0, true, false>(input, size, output, ctx, version); +static void cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { + cryptonight_double_hash(input, size, output, ctx, version); } -bool (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { +void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -136,7 +101,7 @@ bool (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_lite_av4_softaes_double }; #else -bool (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { +void (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -147,8 +112,9 @@ bool (*cryptonight_variations[4])(const void *input, size_t size, void *output, bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx) { - const bool rc = cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, job.version()); - return rc && *reinterpret_cast(result.result + 24) < job.target(); + cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, job.version()); + + return *reinterpret_cast(result.result + 24) < job.target(); } @@ -170,9 +136,9 @@ bool CryptoNight::init(int algo, int variant) } -bool CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version) +void CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version) { - return cryptonight_hash_ctx(input, size, output, ctx, version); + cryptonight_hash_ctx(input, size, output, ctx, version); } @@ -183,17 +149,17 @@ bool CryptoNight::selfTest(int algo) { char output[64]; - struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) _mm_malloc(sizeof(struct cryptonight_ctx), 16); - ctx->memory = (uint8_t *) _mm_malloc(MEMORY * 2, 16); + struct cryptonight_ctx *ctx = static_cast(_mm_malloc(sizeof(cryptonight_ctx), 16)); + ctx->memory = static_cast(_mm_malloc(MONERO_MEMORY * 2, 16)); - const bool rc = cryptonight_hash_ctx(test_input, 76, output, ctx, 0); + cryptonight_hash_ctx(test_input, 76, output, ctx, 0); _mm_free(ctx->memory); _mm_free(ctx); # ifndef XMRIG_NO_AEON - return rc && memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; + return memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; # else - return rc && memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; + return memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; # endif } diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index 6fa4719c..06bb9888 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -32,9 +32,13 @@ #include "align.h" +#define AEON_MEMORY 1048576 +#define AEON_MASK 0xFFFF0 +#define AEON_ITER 0x40000 -#define MEMORY 2097152 /* 2 MiB */ -#define MEMORY_LITE 1048576 /* 1 MiB */ +#define MONERO_MEMORY 2097152 +#define MONERO_MASK 0x1FFFF0 +#define MONERO_ITER 0x80000 struct cryptonight_ctx { @@ -53,7 +57,7 @@ class CryptoNight public: static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx); static bool init(int algo, int variant); - static bool hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version); + static void hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version); private: static bool selfTest(int algo); diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h new file mode 100644 index 00000000..9bf3db84 --- /dev/null +++ b/src/crypto/CryptoNight_monero.h @@ -0,0 +1,57 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __CRYPTONIGHT_MONERO_H__ +#define __CRYPTONIGHT_MONERO_H__ + + +// VARIANT ALTERATIONS +#define VARIANT1_INIT(part) \ + uint64_t tweak1_2_##part = 0; \ + if (MONERO) { \ + if (version > 6) { \ + tweak1_2_##part = (*reinterpret_cast(reinterpret_cast(input) + 35 + part * size) ^ \ + *(reinterpret_cast(ctx->state##part) + 24)); \ + } \ + } + +#define VARIANT1_1(p) \ + if (MONERO) { \ + if (version > 6) { \ + const uint8_t tmp = reinterpret_cast(p)[11]; \ + static const uint32_t table = 0x75310; \ + const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; \ + ((uint8_t*)(p))[11] = tmp ^ ((table >> index) & 0x30); \ + } \ + } + +#define VARIANT1_2(p, part) \ + if (MONERO) { \ + if (version > 6) { \ + (p) ^= tweak1_2_##part; \ + } \ + } + + +#endif /* __CRYPTONIGHT_MONERO_H__ */ diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index c2f02645..d39948e8 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -35,6 +35,7 @@ #include "crypto/CryptoNight.h" +#include "crypto/CryptoNight_monero.h" #include "crypto/soft_aes.h" @@ -313,7 +314,6 @@ inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void * { keccak(static_cast(input), (int) size, ctx->state0, 200); - VARIANT1_CHECK(); VARIANT1_INIT(0); cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); @@ -374,7 +374,6 @@ inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); - VARIANT1_CHECK(); VARIANT1_INIT(0); VARIANT1_INIT(1); diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index aac503f1..d2960320 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -86,15 +86,15 @@ void DoubleWorker::start() *Job::nonce(m_state->blob) = ++m_state->nonce1; *Job::nonce(m_state->blob + m_state->job.size()) = ++m_state->nonce2; - if (CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, m_state->job.version())) { - if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { - Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); - } + CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, m_state->job.version()); - if (*reinterpret_cast(m_hash + 32 + 24) < m_state->job.target()) { - Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce2, m_hash + 32, m_state->job.diff())); - } - } // print on error ? + if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { + Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); + } + + if (*reinterpret_cast(m_hash + 32 + 24) < m_state->job.target()) { + Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce2, m_hash + 32, m_state->job.diff())); + } std::this_thread::yield(); } From 79779b51da5b2384e9fba3d50ceb299f843901e8 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 7 Mar 2018 15:32:44 +0700 Subject: [PATCH 07/23] Added reference hashes. --- src/Mem.h | 5 ++-- src/crypto/CryptoNight.cpp | 48 +++++++++++++++++++++++++++-------- src/crypto/CryptoNight_arm.h | 23 +++-------------- src/crypto/CryptoNight_test.h | 14 ++++++++-- 4 files changed, 56 insertions(+), 34 deletions(-) diff --git a/src/Mem.h b/src/Mem.h index 58dba848..a9635a17 100644 --- a/src/Mem.h +++ b/src/Mem.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index cdba4051..8a818fda 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -39,30 +39,50 @@ -void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = nullptr; +void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) = nullptr; static void cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - cryptonight_hash(input, size, output, ctx, version); + if (version > 6) { + cryptonight_hash(input, size, output, ctx, version); + } + else { + cryptonight_hash(input, size, output, ctx, version); + } # endif } static void cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { # if !defined(XMRIG_ARMv7) - cryptonight_double_hash(input, size, output, ctx, version); + if (version > 6) { + cryptonight_double_hash(input, size, output, ctx, version); + } + else { + cryptonight_double_hash(input, size, output, ctx, version); + } # endif } static void cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - cryptonight_hash(input, size, output, ctx, version); + if (version > 6) { + cryptonight_hash(input, size, output, ctx, version); + } + else { + cryptonight_hash(input, size, output, ctx, version); + } } static void cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - cryptonight_double_hash(input, size, output, ctx, version); + if (version > 6) { + cryptonight_double_hash(input, size, output, ctx, version); + } + else { + cryptonight_double_hash(input, size, output, ctx, version); + } } @@ -154,12 +174,20 @@ bool CryptoNight::selfTest(int algo) { cryptonight_hash_ctx(test_input, 76, output, ctx, 0); +# ifndef XMRIG_NO_AEON + bool rc = memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; +# else + bool rc = memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; +# endif + + if (rc && algo == Options::ALGO_CRYPTONIGHT) { + cryptonight_hash_ctx(test_input, 76, output, ctx, 7); + + rc = memcmp(output, test_output2, (Options::i()->doubleHash() ? 64 : 32)) == 0; + } + _mm_free(ctx->memory); _mm_free(ctx); -# ifndef XMRIG_NO_AEON - return memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; -# else - return memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; -# endif + return rc; } diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index b5dc789d..6d8c454e 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -35,6 +35,7 @@ #include "crypto/CryptoNight.h" +#include "crypto/CryptoNight_monero.h" #include "crypto/soft_aes.h" @@ -137,20 +138,6 @@ static inline __m128i sl_xor(__m128i tmp1) } -template -static inline void aes_genkey_sub(__m128i* xout0, __m128i* xout2) -{ -// __m128i xout1 = _mm_aeskeygenassist_si128(*xout2, rcon); -// xout1 = _mm_shuffle_epi32(xout1, 0xFF); // see PSHUFD, set all elems to 4th elem -// *xout0 = sl_xor(*xout0); -// *xout0 = _mm_xor_si128(*xout0, xout1); -// xout1 = _mm_aeskeygenassist_si128(*xout0, 0x00); -// xout1 = _mm_shuffle_epi32(xout1, 0xAA); // see PSHUFD, set all elems to 3rd elem -// *xout2 = sl_xor(*xout2); -// *xout2 = _mm_xor_si128(*xout2, xout1); -} - - template static inline void soft_aes_genkey_sub(__m128i* xout0, __m128i* xout2) { @@ -346,11 +333,10 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) template -inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) +inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak(static_cast(input), (int) size, ctx->state0, 200); - VARIANT1_CHECK(); VARIANT1_INIT(0); cn_explode_scratchpad((__m128i*) ctx->state0, (__m128i*) ctx->memory); @@ -404,17 +390,15 @@ inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void * keccakf(h0, 24); extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); - return true; } template -inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) +inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); - VARIANT1_CHECK(); VARIANT1_INIT(0); VARIANT1_INIT(1); @@ -506,7 +490,6 @@ inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, static_cast(output) + 32); - return true; } #endif /* __CRYPTONIGHT_ARM_H__ */ diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index b2985379..65200f75 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -4,8 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2016-2018 XMRig , * * 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 @@ -57,4 +58,13 @@ const static uint8_t test_output1[64] = { #endif +// Monero v7 +const static uint8_t test_output2[64] = { + 0xC9, 0xFA, 0xE8, 0x42, 0x5D, 0x86, 0x88, 0xDC, 0x23, 0x6B, 0xCD, 0xBC, 0x42, 0xFD, 0xB4, 0x2D, + 0x37, 0x6C, 0x6E, 0xC1, 0x90, 0x50, 0x1A, 0xA8, 0x4B, 0x04, 0xA4, 0xB4, 0xCF, 0x1E, 0xE1, 0x22, + 0xF2, 0x2D, 0x3D, 0x62, 0x03, 0xD2, 0xA0, 0x8B, 0x41, 0xD9, 0x02, 0x72, 0x78, 0xD8, 0xBC, 0xC9, + 0x83, 0xAC, 0xAD, 0xA9, 0xB6, 0x8E, 0x52, 0xE3, 0xC6, 0x89, 0x69, 0x2A, 0x50, 0xE9, 0x21, 0xD9 +}; + + #endif /* __CRYPTONIGHT_TEST_H__ */ From 8a6988d381551cb27ccfa19dd719dd28d09c863e Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 7 Mar 2018 16:38:58 +0700 Subject: [PATCH 08/23] Added full IPv6 support. --- CMakeLists.txt | 2 +- src/api/Httpd.cpp | 18 +++++-- src/api/Httpd.h | 4 +- src/net/Client.cpp | 110 ++++++++++++++++++++++++-------------- src/net/Client.h | 14 +++-- src/net/{JobId.h => Id.h} | 35 ++++++++---- src/net/Job.h | 6 +-- src/net/JobResult.h | 12 ++--- src/net/Url.cpp | 67 +++++++++++++++++++++-- src/net/Url.h | 9 +++- 10 files changed, 200 insertions(+), 77 deletions(-) rename src/net/{JobId.h => Id.h} (75%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 068642bb..342951f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,8 +29,8 @@ set(HEADERS src/log/Log.h src/Mem.h src/net/Client.h + src/net/Id.h src/net/Job.h - src/net/JobId.h src/net/JobResult.h src/net/Network.h src/net/strategies/DonateStrategy.h diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index 996bc007..a604b5de 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * 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 @@ -45,7 +45,19 @@ bool Httpd::start() return false; } - m_daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); + unsigned int flags = 0; + if (MHD_is_feature_supported(MHD_FEATURE_EPOLL)) { + flags = MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_EPOLL_INTERNALLY_LINUX_ONLY; + } + else { + flags = MHD_USE_SELECT_INTERNALLY; + } + + if (MHD_is_feature_supported(MHD_FEATURE_IPv6)) { + flags |= MHD_USE_DUAL_STACK; + } + + m_daemon = MHD_start_daemon(flags, m_port, nullptr, nullptr, &Httpd::handler, this, MHD_OPTION_END); if (!m_daemon) { LOG_ERR("HTTP Daemon failed to start."); return false; diff --git a/src/api/Httpd.h b/src/api/Httpd.h index 7a4dd932..30618e2a 100644 --- a/src/api/Httpd.h +++ b/src/api/Httpd.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * 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 diff --git a/src/net/Client.cpp b/src/net/Client.cpp index eb07bf21..d347ed22 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -54,6 +54,7 @@ int64_t Client::m_sequence = 1; Client::Client(int id, const char *agent, IClientListener *listener) : + m_ipv6(false), m_quiet(false), m_agent(agent), m_listener(listener), @@ -71,7 +72,7 @@ Client::Client(int id, const char *agent, IClientListener *listener) : m_resolver.data = this; - m_hints.ai_family = PF_INET; + m_hints.ai_family = AF_UNSPEC; m_hints.ai_socktype = SOCK_STREAM; m_hints.ai_protocol = IPPROTO_TCP; @@ -109,19 +110,6 @@ void Client::connect(const Url *url) } -void Client::disconnect() -{ -# ifndef XMRIG_PROXY_PROJECT - uv_timer_stop(&m_keepAliveTimer); -# endif - - m_expire = 0; - m_failures = -1; - - close(); -} - - void Client::setUrl(const Url *url) { if (!url || !url->isValid()) { @@ -150,6 +138,19 @@ void Client::tick(uint64_t now) } +bool Client::disconnect() +{ +# ifndef XMRIG_PROXY_PROJECT + uv_timer_stop(&m_keepAliveTimer); +# endif + + m_expire = 0; + m_failures = -1; + + return close(); +} + + int64_t Client::submit(const JobResult &result) { # ifdef XMRIG_PROXY_PROJECT @@ -167,13 +168,29 @@ int64_t Client::submit(const JobResult &result) # endif const size_t size = snprintf(m_sendBuf, sizeof(m_sendBuf), "{\"id\":%" PRIu64 ",\"jsonrpc\":\"2.0\",\"method\":\"submit\",\"params\":{\"id\":\"%s\",\"job_id\":\"%s\",\"nonce\":\"%s\",\"result\":\"%s\"}}\n", - m_sequence, m_rpcId, result.jobId.data(), nonce, data); + m_sequence, m_rpcId.data(), result.jobId.data(), nonce, data); m_results[m_sequence] = SubmitResult(m_sequence, result.diff, result.actualDiff()); return send(size); } +bool Client::close() +{ + if (m_state == UnconnectedState || m_state == ClosingState || !m_socket) { + return false; + } + + setState(ClosingState); + + if (uv_is_closing(reinterpret_cast(m_socket)) == 0) { + uv_close(reinterpret_cast(m_socket), Client::onClose); + } + + return true; +} + + bool Client::isCriticalError(const char *message) { if (!message) { @@ -235,15 +252,11 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) bool Client::parseLogin(const rapidjson::Value &result, int *code) { - const char *id = result["id"].GetString(); - if (!id || strlen(id) >= sizeof(m_rpcId)) { + if (!m_rpcId.setId(result["id"].GetString())) { *code = 1; return false; } - memset(m_rpcId, 0, sizeof(m_rpcId)); - memcpy(m_rpcId, id, strlen(id)); - return parseJob(result["job"], code); } @@ -291,21 +304,25 @@ int64_t Client::send(size_t size) } -void Client::close() +void Client::connect(const std::vector &ipv4, const std::vector &ipv6) { - if (m_state == UnconnectedState || m_state == ClosingState || !m_socket) { - return; + addrinfo *addr = nullptr; + m_ipv6 = ipv4.empty() && !ipv6.empty(); + + if (m_ipv6) { + addr = ipv6[ipv6.size() == 1 ? 0 : rand() % ipv6.size()]; + uv_ip6_name(reinterpret_cast(addr->ai_addr), m_ip, 45); + } + else { + addr = ipv4[ipv4.size() == 1 ? 0 : rand() % ipv4.size()]; + uv_ip4_name(reinterpret_cast(addr->ai_addr), m_ip, 16); } - setState(ClosingState); - - if (uv_is_closing(reinterpret_cast(m_socket)) == 0) { - uv_close(reinterpret_cast(m_socket), Client::onClose); - } + connect(addr->ai_addr); } -void Client::connect(struct sockaddr *addr) +void Client::connect(sockaddr *addr) { setState(ConnectingState); @@ -374,6 +391,11 @@ void Client::parse(char *line, size_t len) LOG_DEBUG("[%s:%u] received (%d bytes): \"%s\"", m_url.host(), m_url.port(), len, line); + if (len < 32 || line[0] != '{') { + LOG_ERR("[%s:%u] JSON decode failed", m_url.host(), m_url.port()); + return; + } + rapidjson::Document doc; if (doc.ParseInsitu(line).HasParseError()) { if (!m_quiet) { @@ -456,7 +478,8 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap LOG_ERR("[%s:%u] login error code: %d", m_url.host(), m_url.port(), code); } - return close(); + close(); + return; } m_failures = 0; @@ -476,7 +499,7 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap void Client::ping() { - send(snprintf(m_sendBuf, sizeof(m_sendBuf), "{\"id\":%" PRId64 ",\"jsonrpc\":\"2.0\",\"method\":\"keepalived\",\"params\":{\"id\":\"%s\"}}\n", m_sequence, m_rpcId)); + send(snprintf(m_sendBuf, sizeof(m_sendBuf), "{\"id\":%" PRId64 ",\"jsonrpc\":\"2.0\",\"method\":\"keepalived\",\"params\":{\"id\":\"%s\"}}\n", m_sequence, m_rpcId.data())); } @@ -532,7 +555,7 @@ void Client::onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t auto client = getClient(handle->data); buf->base = &client->m_recvBuf.base[client->m_recvBufPos]; - buf->len = client->m_recvBuf.len - (unsigned long)client->m_recvBufPos; + buf->len = client->m_recvBuf.len - client->m_recvBufPos; } @@ -582,11 +605,13 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) LOG_ERR("[%s:%u] read error: \"%s\"", client->m_url.host(), client->m_url.port(), uv_strerror((int) nread)); } - return client->close(); + client->close(); + return; } if ((size_t) nread > (sizeof(m_buf) - 8 - client->m_recvBufPos)) { - return client->close(); + client->close(); + return; } client->m_recvBufPos += nread; @@ -628,24 +653,27 @@ void Client::onResolved(uv_getaddrinfo_t *req, int status, struct addrinfo *res) addrinfo *ptr = res; std::vector ipv4; + std::vector ipv6; while (ptr != nullptr) { if (ptr->ai_family == AF_INET) { ipv4.push_back(ptr); } + if (ptr->ai_family == AF_INET6) { + ipv6.push_back(ptr); + } + ptr = ptr->ai_next; } - if (ipv4.empty()) { - LOG_ERR("[%s:%u] DNS error: \"No IPv4 records found\"", client->m_url.host(), client->m_url.port()); + if (ipv4.empty() && ipv6.empty()) { + LOG_ERR("[%s:%u] DNS error: \"No IPv4 (A) or IPv6 (AAAA) records found\"", client->m_url.host(), client->m_url.port()); + + uv_freeaddrinfo(res); return client->reconnect(); } - ptr = ipv4[rand() % ipv4.size()]; - - uv_ip4_name(reinterpret_cast(ptr->ai_addr), client->m_ip, 16); - - client->connect(ptr->ai_addr); + client->connect(ipv4, ipv6); uv_freeaddrinfo(res); } diff --git a/src/net/Client.h b/src/net/Client.h index 2cc16915..db480461 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -27,8 +27,10 @@ #include #include +#include +#include "net/Id.h" #include "net/Job.h" #include "net/SubmitResult.h" #include "net/Url.h" @@ -56,10 +58,10 @@ public: Client(int id, const char *agent, IClientListener *listener); ~Client(); + bool disconnect(); int64_t submit(const JobResult &result); void connect(); void connect(const Url *url); - void disconnect(); void setUrl(const Url *url); void tick(uint64_t now); @@ -74,13 +76,14 @@ public: inline void setRetryPause(int ms) { m_retryPause = ms; } private: + bool close(); bool isCriticalError(const char *message); bool parseJob(const rapidjson::Value ¶ms, int *code); bool parseLogin(const rapidjson::Value &result, int *code); int resolve(const char *host); int64_t send(size_t size); - void close(); - void connect(struct sockaddr *addr); + void connect(const std::vector &ipv4, const std::vector &ipv6); + void connect(sockaddr *addr); void login(); void parse(char *line, size_t len); void parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error); @@ -99,10 +102,10 @@ private: static inline Client *getClient(void *data) { return static_cast(data); } addrinfo m_hints; + bool m_ipv6; bool m_quiet; char m_buf[2048]; - char m_ip[17]; - char m_rpcId[64]; + char m_ip[46]; char m_sendBuf[768]; const char *m_agent; IClientListener *m_listener; @@ -120,6 +123,7 @@ private: uv_getaddrinfo_t m_resolver; uv_stream_t *m_stream; uv_tcp_t *m_socket; + xmrig::Id m_rpcId; # ifndef XMRIG_PROXY_PROJECT uv_timer_t m_keepAliveTimer; diff --git a/src/net/JobId.h b/src/net/Id.h similarity index 75% rename from src/net/JobId.h rename to src/net/Id.h index 06189779..5c77d1d4 100644 --- a/src/net/JobId.h +++ b/src/net/Id.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * 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,40 +21,51 @@ * along with this program. If not, see . */ -#ifndef __JOBID_H__ -#define __JOBID_H__ +#ifndef __ID_H__ +#define __ID_H__ #include -class JobId +namespace xmrig { + + +class Id { public: - inline JobId() + inline Id() { memset(m_data, 0, sizeof(m_data)); } - inline JobId(const char *id, size_t sizeFix = 0) + inline Id(const char *id, size_t sizeFix = 0) { setId(id, sizeFix); } - inline bool operator==(const JobId &other) const + inline bool operator==(const Id &other) const { return memcmp(m_data, other.m_data, sizeof(m_data)) == 0; } - inline bool operator!=(const JobId &other) const + inline bool operator!=(const Id &other) const { return memcmp(m_data, other.m_data, sizeof(m_data)) != 0; } + Id &operator=(const Id &other) + { + memcpy(m_data, other.m_data, sizeof(m_data)); + + return *this; + } + + inline bool setId(const char *id, size_t sizeFix = 0) { memset(m_data, 0, sizeof(m_data)); @@ -80,4 +91,8 @@ private: char m_data[64]; }; -#endif /* __JOBID_H__ */ + +} /* namespace xmrig */ + + +#endif /* __ID_H__ */ diff --git a/src/net/Job.h b/src/net/Job.h index d74b64ed..e2fe9f16 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -31,7 +31,7 @@ #include "align.h" -#include "net/JobId.h" +#include "net/Id.h" class Job @@ -46,9 +46,9 @@ public: 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); } - inline const JobId &id() const { return m_id; } inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } inline const uint8_t *blob() const { return m_blob; } + inline const xmrig::Id &id() const { return m_id; } inline int poolId() const { return m_poolId; } inline int threadId() const { return m_threadId; } inline size_t size() const { return m_size; } @@ -77,10 +77,10 @@ private: bool m_nicehash; int m_poolId; int m_threadId; - JobId m_id; size_t m_size; uint64_t m_diff; uint64_t m_target; + xmrig::Id m_id; # ifdef XMRIG_PROXY_PROJECT VAR_ALIGN(16, char m_rawBlob[169]); diff --git a/src/net/JobResult.h b/src/net/JobResult.h index 520022a7..e3282584 100644 --- a/src/net/JobResult.h +++ b/src/net/JobResult.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * 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 @@ -36,11 +36,11 @@ class JobResult { public: inline JobResult() : poolId(0), diff(0), nonce(0) {} - inline JobResult(int poolId, const JobId &jobId, uint32_t nonce, const uint8_t *result, uint32_t diff) : + inline JobResult(int poolId, const xmrig::Id &jobId, uint32_t nonce, const uint8_t *result, uint32_t diff) : poolId(poolId), - jobId(jobId), diff(diff), - nonce(nonce) + nonce(nonce), + jobId(jobId) { memcpy(this->result, result, sizeof(this->result)); } @@ -71,10 +71,10 @@ public: int poolId; - JobId jobId; uint32_t diff; uint32_t nonce; uint8_t result[32]; + xmrig::Id jobId; }; #endif /* __JOBRESULT_H__ */ diff --git a/src/net/Url.cpp b/src/net/Url.cpp index dcbe82af..f58ca48a 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * 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 @@ -41,6 +41,7 @@ Url::Url() : m_host(nullptr), m_password(nullptr), m_user(nullptr), + m_url(nullptr), m_port(kDefaultPort) { } @@ -63,6 +64,7 @@ Url::Url(const char *url) : m_host(nullptr), m_password(nullptr), m_user(nullptr), + m_url(nullptr), m_port(kDefaultPort) { parse(url); @@ -74,6 +76,7 @@ Url::Url(const char *host, uint16_t port, const char *user, const char *password m_nicehash(nicehash), m_password(password ? strdup(password) : nullptr), m_user(user ? strdup(user) : nullptr), + m_url(nullptr), m_port(port) { m_host = strdup(host); @@ -85,6 +88,10 @@ Url::~Url() free(m_host); free(m_password); free(m_user); + + if (m_url) { + delete [] m_url; + } } @@ -105,6 +112,10 @@ bool Url::parse(const char *url) return false; } + if (base[0] == '[') { + return parseIPv6(base); + } + const char *port = strchr(base, ':'); if (!port) { m_host = strdup(base); @@ -112,9 +123,8 @@ bool Url::parse(const char *url) } const size_t size = port++ - base + 1; - m_host = static_cast(malloc(size)); + m_host = new char[size](); memcpy(m_host, base, size - 1); - m_host[size - 1] = '\0'; m_port = (uint16_t) strtol(port, nullptr, 10); return true; @@ -139,6 +149,19 @@ bool Url::setUserpass(const char *userpass) } +const char *Url::url() const +{ + if (!m_url) { + const size_t size = strlen(m_host) + 8; + m_url = new char[size]; + + snprintf(m_url, size - 1, "%s:%d", m_host, m_port); + } + + return m_url; +} + + void Url::applyExceptions() { if (!isValid()) { @@ -178,6 +201,20 @@ void Url::setUser(const char *user) } +bool Url::operator==(const Url &other) const +{ + if (m_port != other.m_port || m_keepAlive != other.m_keepAlive || m_nicehash != other.m_nicehash) { + return false; + } + + if (strcmp(host(), other.host()) != 0 || strcmp(user(), other.user()) != 0 || strcmp(password(), other.password()) != 0) { + return false; + } + + return true; +} + + Url &Url::operator=(const Url *other) { m_keepAlive = other->m_keepAlive; @@ -192,3 +229,25 @@ Url &Url::operator=(const Url *other) return *this; } + + +bool Url::parseIPv6(const char *addr) +{ + const char *end = strchr(addr, ']'); + if (!end) { + return false; + } + + const char *port = strchr(end, ':'); + if (!port) { + return false; + } + + const size_t size = end - addr; + m_host = new char[size](); + memcpy(m_host, addr + 1, size - 1); + + m_port = (uint16_t) strtol(port + 1, nullptr, 10); + + return true; +} diff --git a/src/net/Url.h b/src/net/Url.h index a1982300..330f2d8e 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * 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 @@ -52,18 +52,23 @@ public: bool parse(const char *url); bool setUserpass(const char *userpass); + const char *url() const; void applyExceptions(); void setPassword(const char *password); void setUser(const char *user); + bool operator==(const Url &other) const; Url &operator=(const Url *other); private: + bool parseIPv6(const char *addr); + bool m_keepAlive; bool m_nicehash; char *m_host; char *m_password; char *m_user; + mutable char *m_url; uint16_t m_port; }; From 69b8a4faf19097835665e7010657ad25af6e8d7e Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 7 Mar 2018 19:12:18 +0700 Subject: [PATCH 09/23] Added option to disable Monero v7 PoW, may useful in future if other coins update their network to v7 without PoW change. --- src/Options.cpp | 14 +++++++++++--- src/Options.h | 4 ++-- src/net/Client.cpp | 2 +- src/net/Job.cpp | 3 ++- src/net/Job.h | 6 ++++-- src/net/Url.cpp | 11 ++++++++++- src/net/Url.h | 5 ++++- 7 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/Options.cpp b/src/Options.cpp index 4e7c75ca..f60b1285 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * 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 @@ -74,6 +74,7 @@ Options:\n\ --cpu-priority set process priority (0 idle, 2 normal to 5 highest)\n\ --no-huge-pages disable huge pages support\n\ --no-color disable colored output\n\ + --no-monero disable Monero v7 PoW\n\ --donate-level=N donate level, default 5%% (5 minutes in 100 minutes)\n\ --user-agent set custom user-agent string for pool\n\ -B, --background run the miner in the background\n\ @@ -118,6 +119,7 @@ static struct option const options[] = { { "nicehash", 0, nullptr, 1006 }, { "no-color", 0, nullptr, 1002 }, { "no-huge-pages", 0, nullptr, 1009 }, + { "no-monero", 0, nullptr, 1010 }, { "pass", 1, nullptr, 'p' }, { "print-time", 1, nullptr, 1007 }, { "retries", 1, nullptr, 'r' }, @@ -158,11 +160,12 @@ static struct option const config_options[] = { static struct option const pool_options[] = { - { "url", 1, nullptr, 'o' }, { "pass", 1, nullptr, 'p' }, + { "url", 1, nullptr, 'o' }, { "user", 1, nullptr, 'u' }, { "userpass", 1, nullptr, 'O' }, { "keepalive", 0, nullptr ,'k' }, + { "monero", 0, nullptr, 1010 }, { "nicehash", 0, nullptr, 1006 }, { 0, 0, 0, 0 } }; @@ -392,6 +395,7 @@ bool Options::parseArg(int key, const char *arg) case 1002: /* --no-color */ case 1009: /* --no-huge-pages */ + case 1010: /* --no-monero */ return parseBoolean(key, false); case 't': /* --threads */ @@ -557,6 +561,10 @@ bool Options::parseBoolean(int key, bool enable) m_hugePages = enable; break; + case 1010: /* monero */ + m_pools.back()->setMonero(enable); + break; + case 2000: /* colors */ m_colors = enable; break; diff --git a/src/Options.h b/src/Options.h index 6f074917..625ac78d 100644 --- a/src/Options.h +++ b/src/Options.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * 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 diff --git a/src/net/Client.cpp b/src/net/Client.cpp index d347ed22..a023ef38 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -220,7 +220,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } - Job job(m_id, m_url.isNicehash()); + Job job(m_id, m_url.isNicehash(), m_url.isMonero()); if (!job.setId(params["job_id"].GetString())) { *code = 3; return false; diff --git a/src/net/Job.cpp b/src/net/Job.cpp index e0e49b43..5820581f 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -56,7 +56,8 @@ static inline char hf_bin2hex(unsigned char c) } -Job::Job(int poolId, bool nicehash) : +Job::Job(int poolId, bool nicehash, bool monero) : + m_monero(monero), m_nicehash(nicehash), m_poolId(poolId), m_threadId(-1), diff --git a/src/net/Job.h b/src/net/Job.h index e2fe9f16..dc87eb8c 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -37,12 +37,13 @@ class Job { public: - Job(int poolId = -2, bool nicehash = false); + Job(int poolId = -2, bool nicehash = false, bool monero = true); ~Job(); bool setBlob(const char *blob); bool setTarget(const char *target); + 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); } @@ -55,7 +56,7 @@ public: inline uint32_t *nonce() { return reinterpret_cast(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 m_blob[0]; } + 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; } @@ -74,6 +75,7 @@ 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; int m_poolId; int m_threadId; diff --git a/src/net/Url.cpp b/src/net/Url.cpp index f58ca48a..cd020b32 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -37,6 +37,7 @@ Url::Url() : m_keepAlive(false), + m_monero(true), m_nicehash(false), m_host(nullptr), m_password(nullptr), @@ -60,6 +61,7 @@ Url::Url() : */ Url::Url(const char *url) : m_keepAlive(false), + m_monero(true), m_nicehash(false), m_host(nullptr), m_password(nullptr), @@ -71,8 +73,9 @@ 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 monero) : m_keepAlive(keepAlive), + m_monero(monero), m_nicehash(nicehash), m_password(password ? strdup(password) : nullptr), m_user(user ? strdup(user) : nullptr), @@ -218,6 +221,7 @@ bool Url::operator==(const Url &other) const Url &Url::operator=(const Url *other) { m_keepAlive = other->m_keepAlive; + m_monero = other->m_monero; m_nicehash = other->m_nicehash; m_port = other->m_port; @@ -227,6 +231,11 @@ Url &Url::operator=(const Url *other) setPassword(other->m_password); setUser(other->m_user); + if (m_url) { + delete [] m_url; + m_url = nullptr; + } + return *this; } diff --git a/src/net/Url.h b/src/net/Url.h index 330f2d8e..32eea5bf 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -37,10 +37,11 @@ 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 monero = true); ~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; } @@ -48,6 +49,7 @@ public: inline const char *user() const { return m_user ? m_user : kDefaultUser; } 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; } bool parse(const char *url); @@ -64,6 +66,7 @@ private: bool parseIPv6(const char *addr); bool m_keepAlive; + bool m_monero; bool m_nicehash; char *m_host; char *m_password; From 37ac1aa62c5ac1159ef7e89dd0b5d576f0a7e060 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 7 Mar 2018 21:56:43 +0700 Subject: [PATCH 10/23] Automatically enable nicehash when use with upcoming xmrig-proxy 2.5. --- src/net/Client.cpp | 27 ++++++++++++++++++++++++++- src/net/Client.h | 2 ++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index a023ef38..9c7f373e 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -55,6 +55,7 @@ int64_t Client::m_sequence = 1; Client::Client(int id, const char *agent, IClientListener *listener) : m_ipv6(false), + m_nicehash(false), m_quiet(false), m_agent(agent), m_listener(listener), @@ -220,7 +221,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } - Job job(m_id, m_url.isNicehash(), m_url.isMonero()); + Job job(m_id, m_nicehash, m_url.isMonero()); if (!job.setId(params["job_id"].GetString())) { *code = 3; return false; @@ -257,6 +258,12 @@ bool Client::parseLogin(const rapidjson::Value &result, int *code) return false; } + m_nicehash = m_url.isNicehash(); + + if (result.HasMember("extensions")) { + parseExtensions(result["extensions"]); + } + return parseJob(result["job"], code); } @@ -419,6 +426,24 @@ void Client::parse(char *line, size_t len) } +void Client::parseExtensions(const rapidjson::Value &value) +{ + if (!value.IsArray()) { + return; + } + + for (const rapidjson::Value &ext : value.GetArray()) { + if (!ext.IsString()) { + continue; + } + + if (strcmp(ext.GetString(), "nicehash") == 0) { + m_nicehash = true; + } + } +} + + void Client::parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error) { if (error.IsObject()) { diff --git a/src/net/Client.h b/src/net/Client.h index db480461..a927bd19 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -86,6 +86,7 @@ private: void connect(sockaddr *addr); void login(); void parse(char *line, size_t len); + void parseExtensions(const rapidjson::Value &value); void parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error); void parseResponse(int64_t id, const rapidjson::Value &result, const rapidjson::Value &error); void ping(); @@ -103,6 +104,7 @@ private: addrinfo m_hints; bool m_ipv6; + bool m_nicehash; bool m_quiet; char m_buf[2048]; char m_ip[46]; From 84970e9e9b5ad1689899033482921436818adab6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 9 Mar 2018 00:50:06 +0700 Subject: [PATCH 11/23] Added coin field support added in xmrig-proxy 2.5. --- src/net/Client.cpp | 4 ++++ src/net/Job.cpp | 13 +++++++++++++ src/net/Job.h | 3 +++ 3 files changed, 20 insertions(+) diff --git a/src/net/Client.cpp b/src/net/Client.cpp index 9c7f373e..b0148336 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -237,6 +237,10 @@ bool Client::parseJob(const rapidjson::Value ¶ms, int *code) return false; } + if (params.HasMember("coin")) { + job.setCoin(params["coin"].GetString()); + } + if (m_job == job) { if (!m_quiet) { LOG_WARN("[%s:%u] duplicate job received, reconnect", m_url.host(), m_url.port()); diff --git a/src/net/Job.cpp b/src/net/Job.cpp index 5820581f..3faed6a1 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -65,6 +65,7 @@ Job::Job(int poolId, bool nicehash, bool monero) : m_diff(0), m_target(0) { + memset(m_coin, 0, sizeof(m_coin)); } @@ -148,6 +149,18 @@ bool Job::setTarget(const char *target) } +void Job::setCoin(const char *coin) +{ + if (!coin || strlen(coin) > 4) { + memset(m_coin, 0, sizeof(m_coin)); + return; + } + + strncpy(m_coin, coin, sizeof(m_coin)); + m_monero = strcmp(m_coin, "XMR") == 0; +} + + bool Job::fromHex(const char* in, unsigned int len, unsigned char* out) { bool error = false; diff --git a/src/net/Job.h b/src/net/Job.h index dc87eb8c..755de9ec 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -42,11 +42,13 @@ public: bool setBlob(const char *blob); bool setTarget(const char *target); + void setCoin(const char *coin); 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); } + inline const char *coin() const { return m_coin; } inline const uint32_t *nonce() const { return reinterpret_cast(m_blob + 39); } inline const uint8_t *blob() const { return m_blob; } inline const xmrig::Id &id() const { return m_id; } @@ -77,6 +79,7 @@ private: bool m_monero; bool m_nicehash; + char m_coin[5]; int m_poolId; int m_threadId; size_t m_size; From da4aa69c891519699232d5dbe269352c2e138404 Mon Sep 17 00:00:00 2001 From: xmrig Date: Fri, 9 Mar 2018 00:56:29 +0700 Subject: [PATCH 12/23] Update CHANGELOG.md --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e2e6522..5c50842f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# v2.5.0 +- **Added support for Monero v7 PoW, scheduled on March 28.** +- Added full IPv6 support. +- Added protocol extension, when use the miner with xmrig-proxy 2.5+ no more need manually specify `nicehash` option. +- [#123](https://github.com/xmrig/xmrig-proxy/issues/123) Fixed regression (all versions since 2.4 affected) fragmented responses from pool/proxy was parsed incorrectly. +- [#428](https://github.com/xmrig/xmrig/issues/428) Fixed regression (version 2.4.5 affected) with CPU cache size detection. + # v2.4.5 - [#324](https://github.com/xmrig/xmrig/pull/324) Fixed build without libmicrohttpd (CMake cache issue). - [#341](https://github.com/xmrig/xmrig/issues/341) Fixed wrong exit code and added command line option `--dry-run`. From 6e95b0d05248cd37fe8e9440d1ea2185de4206d7 Mon Sep 17 00:00:00 2001 From: xmrig Date: Fri, 9 Mar 2018 01:30:32 +0700 Subject: [PATCH 13/23] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c50842f..b00b1f1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ # v2.5.0 -- **Added support for Monero v7 PoW, scheduled on March 28.** +- [#434](https://github.com/xmrig/xmrig/issues/434) **Added support for Monero v7 PoW, scheduled on March 28.** - Added full IPv6 support. - Added protocol extension, when use the miner with xmrig-proxy 2.5+ no more need manually specify `nicehash` option. - [#123](https://github.com/xmrig/xmrig-proxy/issues/123) Fixed regression (all versions since 2.4 affected) fragmented responses from pool/proxy was parsed incorrectly. From b662cb77a4b8d0f80df348f4799a8164530cb9f6 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 9 Mar 2018 01:47:44 +0700 Subject: [PATCH 14/23] Revert changes in Api class, single threaded http server will not be included in 2.5 release. --- src/api/Api.cpp | 14 ++++++++++++-- src/api/Api.h | 3 ++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/api/Api.cpp b/src/api/Api.cpp index 98d441c1..729ebccd 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -4,7 +4,7 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2018 XMRig + * Copyright 2016-2017 XMRig * * * This program is free software: you can redistribute it and/or modify @@ -29,10 +29,12 @@ ApiState *Api::m_state = nullptr; +uv_mutex_t Api::m_mutex; bool Api::start() { + uv_mutex_init(&m_mutex); m_state = new ApiState(); return true; @@ -51,7 +53,11 @@ char *Api::get(const char *url, int *status) return nullptr; } - return m_state->get(url, status); + uv_mutex_lock(&m_mutex); + char *buf = m_state->get(url, status); + uv_mutex_unlock(&m_mutex); + + return buf; } @@ -61,7 +67,9 @@ void Api::tick(const Hashrate *hashrate) return; } + uv_mutex_lock(&m_mutex); m_state->tick(hashrate); + uv_mutex_unlock(&m_mutex); } @@ -71,5 +79,7 @@ void Api::tick(const NetworkState &network) return; } + uv_mutex_lock(&m_mutex); m_state->tick(network); + uv_mutex_unlock(&m_mutex); } diff --git a/src/api/Api.h b/src/api/Api.h index 1f91b382..72c65c3c 100644 --- a/src/api/Api.h +++ b/src/api/Api.h @@ -4,7 +4,7 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2018 XMRig + * Copyright 2016-2017 XMRig * * * This program is free software: you can redistribute it and/or modify @@ -45,6 +45,7 @@ public: private: static ApiState *m_state; + static uv_mutex_t m_mutex; }; #endif /* __API_H__ */ From ff68c047f9c3ad6ffa7ef0d81bf3bcfe2ae82cd7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 9 Mar 2018 01:49:24 +0700 Subject: [PATCH 15/23] v2.5.0-dev --- src/version.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/version.h b/src/version.h index d34b45a5..e0d4511a 100644 --- a/src/version.h +++ b/src/version.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2018 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * 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 @@ -27,15 +27,15 @@ #define APP_ID "xmrig" #define APP_NAME "XMRig" #define APP_DESC "XMRig CPU miner" -#define APP_VERSION "2.4.5" +#define APP_VERSION "2.5.0-dev" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" #define APP_KIND "cpu" #define APP_VER_MAJOR 2 -#define APP_VER_MINOR 4 -#define APP_VER_BUILD 5 +#define APP_VER_MINOR 5 +#define APP_VER_BUILD 0 #define APP_VER_REV 0 #ifdef _MSC_VER From 244ca93aa4cd9e10dfc159fc2d7a7be579f8f804 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 9 Mar 2018 13:15:55 +0700 Subject: [PATCH 16/23] Change donation address to separate old and new versions. --- src/net/strategies/DonateStrategy.cpp | 6 +++--- src/net/strategies/DonateStrategy.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index d7c721c6..288e6e48 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * 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 @@ -48,7 +48,7 @@ DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) : keccak(reinterpret_cast(user), static_cast(strlen(user)), hash, sizeof(hash)); Job::toHex(hash, 32, userId); - Url *url = new Url("fee.xmrig.com", Options::i()->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 443, userId, nullptr, false, true); + Url *url = new Url("thanks.xmrig.com", Options::i()->algo() == Options::ALGO_CRYPTONIGHT_LITE ? 3333 : 80, userId, nullptr, false, true); m_client = new Client(-1, agent, this); m_client->setUrl(url); diff --git a/src/net/strategies/DonateStrategy.h b/src/net/strategies/DonateStrategy.h index 302de292..07011053 100644 --- a/src/net/strategies/DonateStrategy.h +++ b/src/net/strategies/DonateStrategy.h @@ -4,8 +4,8 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2017 XMRig - * + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , * * 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 From 83e5832bc16275002d1bb98e018446c8a67be08a Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 12 Mar 2018 14:44:23 +0700 Subject: [PATCH 17/23] Some small fixes. --- src/Cpu_stub.cpp | 15 ++++++++++----- src/crypto/CryptoNight_x86.h | 6 ++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Cpu_stub.cpp b/src/Cpu_stub.cpp index 0b9196ee..87d9de75 100644 --- a/src/Cpu_stub.cpp +++ b/src/Cpu_stub.cpp @@ -24,13 +24,18 @@ #ifdef _MSC_VER # include - -# define bit_AES (1 << 25) -# define bit_BMI2 (1 << 8) #else # include #endif +#ifndef bit_AES +# define bit_AES (1 << 25) +#endif + +#ifndef bit_BMI2 +# define bit_BMI2 (1 << 8) +#endif + #include @@ -87,7 +92,7 @@ static inline bool has_aes_ni() int cpu_info[4] = { 0 }; cpuid(PROCESSOR_INFO, cpu_info); - return cpu_info[ECX_Reg] & bit_AES; + return (cpu_info[ECX_Reg] & bit_AES) != 0; } @@ -95,7 +100,7 @@ static inline bool has_bmi2() { int cpu_info[4] = { 0 }; cpuid(EXTENDED_FEATURES, cpu_info); - return cpu_info[EBX_Reg] & bit_BMI2; + return (cpu_info[EBX_Reg] & bit_BMI2) != 0; } diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index d39948e8..7393bbfa 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -310,7 +310,7 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) template -inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) +inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak(static_cast(input), (int) size, ctx->state0, 200); @@ -364,12 +364,11 @@ inline bool cryptonight_hash(const void *__restrict__ input, size_t size, void * keccakf(h0, 24); extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); - return true; } template -inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) +inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) { keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); @@ -463,7 +462,6 @@ inline bool cryptonight_double_hash(const void *__restrict__ input, size_t size, extra_hashes[ctx->state0[0] & 3](ctx->state0, 200, static_cast(output)); extra_hashes[ctx->state1[0] & 3](ctx->state1, 200, static_cast(output) + 32); - return true; } #endif /* __CRYPTONIGHT_X86_H__ */ From aec31c43c0efd7e228018336db8ed8e352e11ea9 Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 12 Mar 2018 22:29:44 +0700 Subject: [PATCH 18/23] Better v1 PoW implementation, added variant option. --- CMakeLists.txt | 1 + src/Mem.cpp | 3 +- src/Mem_unix.cpp | 3 +- src/Mem_win.cpp | 3 +- src/Options.cpp | 37 +++++++----- src/Options.h | 6 +- src/crypto/CryptoNight.cpp | 87 ++++++++++++--------------- src/crypto/CryptoNight.h | 2 +- src/crypto/CryptoNight_arm.h | 8 +-- src/crypto/CryptoNight_monero.h | 26 +++----- src/crypto/CryptoNight_x86.h | 8 +-- src/net/Client.cpp | 6 +- src/net/Job.cpp | 53 +++++++++++++++- src/net/Job.h | 10 +-- src/net/Url.cpp | 34 +++++++++-- src/net/Url.h | 13 ++-- src/net/strategies/DonateStrategy.cpp | 3 +- src/workers/DoubleWorker.cpp | 2 +- src/xmrig.h | 47 +++++++++++++++ 19 files changed, 236 insertions(+), 116 deletions(-) create mode 100644 src/xmrig.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 342951f3..47a4d741 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ set(HEADERS src/workers/SingleWorker.h src/workers/Worker.h src/workers/Workers.h + src/xmrig.h ) set(HEADERS_CRYPTO diff --git a/src/Mem.cpp b/src/Mem.cpp index 1943b22a..73576d20 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -29,6 +29,7 @@ #include "crypto/CryptoNight.h" #include "Mem.h" #include "Options.h" +#include "xmrig.h" bool Mem::m_doubleHash = false; @@ -42,7 +43,7 @@ uint8_t *Mem::m_memory = nullptr; cryptonight_ctx *Mem::create(int threadId) { # ifndef XMRIG_NO_AEON - if (m_algo == Options::ALGO_CRYPTONIGHT_LITE) { + if (m_algo == xmrig::ALGO_CRYPTONIGHT_LITE) { return createLite(threadId); } # endif diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index ec4017da..93cdbec0 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -38,6 +38,7 @@ #include "log/Log.h" #include "Mem.h" #include "Options.h" +#include "xmrig.h" bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) @@ -46,7 +47,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_threads = threads; m_doubleHash = doubleHash; - const int ratio = (doubleHash && algo != Options::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; + const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; const size_t size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index 79c6d12e..28eaff49 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -38,6 +38,7 @@ #include "crypto/CryptoNight.h" #include "Mem.h" #include "Options.h" +#include "xmrig.h" /***************************************************************** @@ -151,7 +152,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_threads = threads; m_doubleHash = doubleHash; - const int ratio = (doubleHash && algo != Options::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; + const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; const size_t size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { diff --git a/src/Options.cpp b/src/Options.cpp index f60b1285..939075f8 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -47,6 +47,7 @@ #include "rapidjson/error/en.h" #include "rapidjson/filereadstream.h" #include "version.h" +#include "xmrig.h" #ifndef ARRAY_SIZE @@ -74,7 +75,7 @@ Options:\n\ --cpu-priority set process priority (0 idle, 2 normal to 5 highest)\n\ --no-huge-pages disable huge pages support\n\ --no-color disable colored output\n\ - --no-monero disable Monero v7 PoW\n\ + --variant algorithm PoW variant\n\ --donate-level=N donate level, default 5%% (5 minutes in 100 minutes)\n\ --user-agent set custom user-agent string for pool\n\ -B, --background run the miner in the background\n\ @@ -119,7 +120,7 @@ static struct option const options[] = { { "nicehash", 0, nullptr, 1006 }, { "no-color", 0, nullptr, 1002 }, { "no-huge-pages", 0, nullptr, 1009 }, - { "no-monero", 0, nullptr, 1010 }, + { "variant", 1, nullptr, 1010 }, { "pass", 1, nullptr, 'p' }, { "print-time", 1, nullptr, 1007 }, { "retries", 1, nullptr, 'r' }, @@ -160,12 +161,12 @@ static struct option const config_options[] = { static struct option const pool_options[] = { - { "pass", 1, nullptr, 'p' }, { "url", 1, nullptr, 'o' }, + { "pass", 1, nullptr, 'p' }, { "user", 1, nullptr, 'u' }, { "userpass", 1, nullptr, 'O' }, { "keepalive", 0, nullptr ,'k' }, - { "monero", 0, nullptr, 1010 }, + { "variant", 1, nullptr, 1010 }, { "nicehash", 0, nullptr, 1006 }, { 0, 0, 0, 0 } }; @@ -275,9 +276,7 @@ Options::Options(int argc, char **argv) : } } - for (Url *url : m_pools) { - url->applyExceptions(); - } + adjust(); m_ready = true; } @@ -383,6 +382,7 @@ bool Options::parseArg(int key, const char *arg) case 1007: /* --print-time */ case 1021: /* --cpu-priority */ case 4000: /* --api-port */ + case 1010: /* --variant */ return parseArg(key, strtol(arg, nullptr, 10)); case 'B': /* --background */ @@ -395,7 +395,6 @@ bool Options::parseArg(int key, const char *arg) case 1002: /* --no-color */ case 1009: /* --no-huge-pages */ - case 1010: /* --no-monero */ return parseBoolean(key, false); case 't': /* --threads */ @@ -502,6 +501,10 @@ bool Options::parseArg(int key, uint64_t arg) m_printTime = (int) arg; break; + case 1010: /* --variant */ + m_pools.back()->setVariant((int) arg); + break; + case 1020: /* --cpu-affinity */ if (arg) { m_affinity = arg; @@ -561,10 +564,6 @@ bool Options::parseBoolean(int key, bool enable) m_hugePages = enable; break; - case 1010: /* monero */ - m_pools.back()->setMonero(enable); - break; - case 2000: /* colors */ m_colors = enable; break; @@ -593,6 +592,14 @@ Url *Options::parseUrl(const char *arg) const } +void Options::adjust() +{ + for (Url *url : m_pools) { + url->adjust(m_algo); + } +} + + void Options::parseConfig(const char *fileName) { rapidjson::Document doc; @@ -637,7 +644,7 @@ void Options::parseJSON(const struct option *option, const rapidjson::Value &obj if (option->has_arg && value.IsString()) { parseArg(option->val, value.GetString()); } - else if (option->has_arg && value.IsUint64()) { + else if (option->has_arg && value.IsInt64()) { parseArg(option->val, value.GetUint64()); } else if (!option->has_arg && value.IsBool()) { @@ -703,7 +710,7 @@ bool Options::setAlgo(const char *algo) # ifndef XMRIG_NO_AEON if (i == ARRAY_SIZE(algo_names) - 1 && !strcmp(algo, "cryptonight-light")) { - m_algo = ALGO_CRYPTONIGHT_LITE; + m_algo = xmrig::ALGO_CRYPTONIGHT_LITE; break; } # endif @@ -721,7 +728,7 @@ bool Options::setAlgo(const char *algo) int Options::getAlgoVariant() const { # ifndef XMRIG_NO_AEON - if (m_algo == ALGO_CRYPTONIGHT_LITE) { + if (m_algo == xmrig::ALGO_CRYPTONIGHT_LITE) { return getAlgoVariantLite(); } # endif diff --git a/src/Options.h b/src/Options.h index 625ac78d..e29059ec 100644 --- a/src/Options.h +++ b/src/Options.h @@ -39,11 +39,6 @@ struct option; class Options { public: - enum Algo { - ALGO_CRYPTONIGHT, /* CryptoNight (Monero) */ - ALGO_CRYPTONIGHT_LITE, /* CryptoNight-Lite (AEON) */ - }; - enum AlgoVariant { AV0_AUTO, AV1_AESNI, @@ -96,6 +91,7 @@ private: bool parseArg(int key, uint64_t arg); bool parseBoolean(int key, bool enable); Url *parseUrl(const char *arg) const; + void adjust(); void parseConfig(const char *fileName); void parseJSON(const struct option *option, const rapidjson::Value &object); void showUsage(int status) const; diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index 8a818fda..1d71d1cd 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -36,81 +36,74 @@ #include "net/Job.h" #include "net/JobResult.h" #include "Options.h" +#include "xmrig.h" - -void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) = nullptr; +void (*cryptonight_hash_ctx)(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) = nullptr; -static void cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx, uint8_t version) { +#define CRYPTONIGHT_HASH(NAME, ITERATIONS, MEM, MASK, SOFT_AES) \ + switch (variant) { \ + case xmrig::VARIANT_V1: \ + return cryptonight_##NAME##_hash(input, size, output, ctx); \ + \ + case xmrig::VARIANT_NONE: \ + return cryptonight_##NAME##_hash(input, size, output, ctx); \ + \ + default: \ + break; \ + } + + +static void cryptonight_av1_aesni(const void *input, size_t size, void *output, struct cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) - if (version > 6) { - cryptonight_hash(input, size, output, ctx, version); - } - else { - cryptonight_hash(input, size, output, ctx, version); - } + CRYPTONIGHT_HASH(single, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, false) # endif } -static void cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +static void cryptonight_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) - if (version > 6) { - cryptonight_double_hash(input, size, output, ctx, version); - } - else { - cryptonight_double_hash(input, size, output, ctx, version); - } + CRYPTONIGHT_HASH(double, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, false) # endif } -static void cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - if (version > 6) { - cryptonight_hash(input, size, output, ctx, version); - } - else { - cryptonight_hash(input, size, output, ctx, version); - } +static void cryptonight_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { + CRYPTONIGHT_HASH(single, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, true) } -static void cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - if (version > 6) { - cryptonight_double_hash(input, size, output, ctx, version); - } - else { - cryptonight_double_hash(input, size, output, ctx, version); - } +static void cryptonight_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { + CRYPTONIGHT_HASH(double, MONERO_ITER, MONERO_MEMORY, MONERO_MASK, true) } #ifndef XMRIG_NO_AEON -static void cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +static void cryptonight_lite_av1_aesni(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) - cryptonight_hash(input, size, output, ctx, version); + CRYPTONIGHT_HASH(single, AEON_ITER, AEON_MEMORY, AEON_MASK, false) # endif } -static void cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { +static void cryptonight_lite_av2_aesni_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { # if !defined(XMRIG_ARMv7) - cryptonight_double_hash(input, size, output, ctx, version); + CRYPTONIGHT_HASH(double, AEON_ITER, AEON_MEMORY, AEON_MASK, false) # endif } -static void cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - cryptonight_hash(input, size, output, ctx, version); +static void cryptonight_lite_av3_softaes(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { + CRYPTONIGHT_HASH(single, AEON_ITER, AEON_MEMORY, AEON_MASK, true) } -static void cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t version) { - cryptonight_double_hash(input, size, output, ctx, version); +static void cryptonight_lite_av4_softaes_double(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) { + CRYPTONIGHT_HASH(double, AEON_ITER, AEON_MEMORY, AEON_MASK, true) } -void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { +void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -121,7 +114,7 @@ void (*cryptonight_variations[8])(const void *input, size_t size, void *output, cryptonight_lite_av4_softaes_double }; #else -void (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, uint8_t) = { +void (*cryptonight_variations[4])(const void *input, size_t size, void *output, cryptonight_ctx *ctx, int variant) = { cryptonight_av1_aesni, cryptonight_av2_aesni_double, cryptonight_av3_softaes, @@ -132,7 +125,7 @@ void (*cryptonight_variations[4])(const void *input, size_t size, void *output, bool CryptoNight::hash(const Job &job, JobResult &result, cryptonight_ctx *ctx) { - cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, job.version()); + cryptonight_hash_ctx(job.blob(), job.size(), result.result, ctx, job.variant()); return *reinterpret_cast(result.result + 24) < job.target(); } @@ -145,7 +138,7 @@ bool CryptoNight::init(int algo, int variant) } # ifndef XMRIG_NO_AEON - const int index = algo == Options::ALGO_CRYPTONIGHT_LITE ? (variant + 3) : (variant - 1); + const int index = algo == xmrig::ALGO_CRYPTONIGHT_LITE ? (variant + 3) : (variant - 1); # else const int index = variant - 1; # endif @@ -156,9 +149,9 @@ bool CryptoNight::init(int algo, int variant) } -void CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version) +void CryptoNight::hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant) { - cryptonight_hash_ctx(input, size, output, ctx, version); + cryptonight_hash_ctx(input, size, output, ctx, variant); } @@ -175,13 +168,13 @@ bool CryptoNight::selfTest(int algo) { cryptonight_hash_ctx(test_input, 76, output, ctx, 0); # ifndef XMRIG_NO_AEON - bool rc = memcmp(output, algo == Options::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; + bool rc = memcmp(output, algo == xmrig::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; # else bool rc = memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; # endif - if (rc && algo == Options::ALGO_CRYPTONIGHT) { - cryptonight_hash_ctx(test_input, 76, output, ctx, 7); + if (rc && algo == xmrig::ALGO_CRYPTONIGHT) { + cryptonight_hash_ctx(test_input, 76, output, ctx, 1); rc = memcmp(output, test_output2, (Options::i()->doubleHash() ? 64 : 32)) == 0; } diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index 06bb9888..9526309d 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -57,7 +57,7 @@ class CryptoNight public: static bool hash(const Job &job, JobResult &result, cryptonight_ctx *ctx); static bool init(int algo, int variant); - static void hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, uint8_t version); + static void hash(const uint8_t *input, size_t size, uint8_t *output, cryptonight_ctx *ctx, int variant); private: static bool selfTest(int algo); diff --git a/src/crypto/CryptoNight_arm.h b/src/crypto/CryptoNight_arm.h index 6d8c454e..6b214d66 100644 --- a/src/crypto/CryptoNight_arm.h +++ b/src/crypto/CryptoNight_arm.h @@ -332,8 +332,8 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } -template -inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) +template +inline void cryptonight_single_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx) { keccak(static_cast(input), (int) size, ctx->state0, 200); @@ -393,8 +393,8 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * } -template -inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) +template +inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) { keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); diff --git a/src/crypto/CryptoNight_monero.h b/src/crypto/CryptoNight_monero.h index 9bf3db84..3c6146ac 100644 --- a/src/crypto/CryptoNight_monero.h +++ b/src/crypto/CryptoNight_monero.h @@ -29,28 +29,22 @@ // VARIANT ALTERATIONS #define VARIANT1_INIT(part) \ uint64_t tweak1_2_##part = 0; \ - if (MONERO) { \ - if (version > 6) { \ - tweak1_2_##part = (*reinterpret_cast(reinterpret_cast(input) + 35 + part * size) ^ \ - *(reinterpret_cast(ctx->state##part) + 24)); \ - } \ + if (VARIANT > 0) { \ + tweak1_2_##part = (*reinterpret_cast(reinterpret_cast(input) + 35 + part * size) ^ \ + *(reinterpret_cast(ctx->state##part) + 24)); \ } #define VARIANT1_1(p) \ - if (MONERO) { \ - if (version > 6) { \ - const uint8_t tmp = reinterpret_cast(p)[11]; \ - static const uint32_t table = 0x75310; \ - const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; \ - ((uint8_t*)(p))[11] = tmp ^ ((table >> index) & 0x30); \ - } \ + if (VARIANT > 0) { \ + const uint8_t tmp = reinterpret_cast(p)[11]; \ + static const uint32_t table = 0x75310; \ + const uint8_t index = (((tmp >> 3) & 6) | (tmp & 1)) << 1; \ + ((uint8_t*)(p))[11] = tmp ^ ((table >> index) & 0x30); \ } #define VARIANT1_2(p, part) \ - if (MONERO) { \ - if (version > 6) { \ - (p) ^= tweak1_2_##part; \ - } \ + if (VARIANT > 0) { \ + (p) ^= tweak1_2_##part; \ } diff --git a/src/crypto/CryptoNight_x86.h b/src/crypto/CryptoNight_x86.h index 7393bbfa..1e5190d8 100644 --- a/src/crypto/CryptoNight_x86.h +++ b/src/crypto/CryptoNight_x86.h @@ -309,8 +309,8 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) } -template -inline void cryptonight_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx, uint8_t version) +template +inline void cryptonight_single_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, cryptonight_ctx *__restrict__ ctx) { keccak(static_cast(input), (int) size, ctx->state0, 200); @@ -367,8 +367,8 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * } -template -inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx, uint8_t version) +template +inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, void *__restrict__ output, struct cryptonight_ctx *__restrict__ ctx) { keccak((const uint8_t *) input, (int) size, ctx->state0, 200); keccak((const uint8_t *) input + size, (int) size, ctx->state1, 200); diff --git a/src/net/Client.cpp b/src/net/Client.cpp index b0148336..f6543b4e 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -221,7 +221,7 @@ bool Client::parseJob(const rapidjson::Value ¶ms, 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 ¶ms, 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()); diff --git a/src/net/Job.cpp b/src/net/Job.cpp index 3faed6a1..c8331fb9 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -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; + } } diff --git a/src/net/Job.h b/src/net/Job.h index 755de9ec..28d5e111 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -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(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; diff --git a/src/net/Url.cpp b/src/net/Url.cpp index cd020b32..c17ef690 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -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; diff --git a/src/net/Url.h b/src/net/Url.h index 32eea5bf..f861fec5 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -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; }; diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 288e6e48..22cffd28 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -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(user), static_cast(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); diff --git a/src/workers/DoubleWorker.cpp b/src/workers/DoubleWorker.cpp index d2960320..46c8ed2e 100644 --- a/src/workers/DoubleWorker.cpp +++ b/src/workers/DoubleWorker.cpp @@ -86,7 +86,7 @@ void DoubleWorker::start() *Job::nonce(m_state->blob) = ++m_state->nonce1; *Job::nonce(m_state->blob + m_state->job.size()) = ++m_state->nonce2; - CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, m_state->job.version()); + CryptoNight::hash(m_state->blob, m_state->job.size(), m_hash, m_ctx, m_state->job.variant()); if (*reinterpret_cast(m_hash + 24) < m_state->job.target()) { Workers::submit(JobResult(m_state->job.poolId(), m_state->job.id(), m_state->nonce1, m_hash, m_state->job.diff())); diff --git a/src/xmrig.h b/src/xmrig.h new file mode 100644 index 00000000..103e8a68 --- /dev/null +++ b/src/xmrig.h @@ -0,0 +1,47 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2016-2018 XMRig , + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __XMRIG_H__ +#define __XMRIG_H__ + + +namespace xmrig +{ + + +enum Algo { + ALGO_CRYPTONIGHT, /* CryptoNight (Monero) */ + ALGO_CRYPTONIGHT_LITE, /* CryptoNight-Lite (AEON) */ +}; + + +enum Variant { + VARIANT_AUTO = -1, + VARIANT_NONE = 0, + VARIANT_V1 = 1 +}; + +} /* xmrig */ + + +#endif /* __XMRIG_H__ */ From 125072a30e2465b6a77d9b4342ac14b62b3468c3 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 13 Mar 2018 01:29:31 +0700 Subject: [PATCH 19/23] Added test hashes for AEON. --- src/crypto/CryptoNight.cpp | 14 ++++++++++---- src/crypto/CryptoNight_test.h | 33 +++++++++++++++++++++------------ src/net/Job.cpp | 4 ++++ 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/crypto/CryptoNight.cpp b/src/crypto/CryptoNight.cpp index 1d71d1cd..996622c4 100644 --- a/src/crypto/CryptoNight.cpp +++ b/src/crypto/CryptoNight.cpp @@ -167,16 +167,22 @@ bool CryptoNight::selfTest(int algo) { cryptonight_hash_ctx(test_input, 76, output, ctx, 0); + const bool doubleHash = Options::i()->doubleHash(); + # ifndef XMRIG_NO_AEON - bool rc = memcmp(output, algo == xmrig::ALGO_CRYPTONIGHT_LITE ? test_output1 : test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; + bool rc = memcmp(output, algo == xmrig::ALGO_CRYPTONIGHT_LITE ? test_output_v0_lite : test_output_v0, (doubleHash ? 64 : 32)) == 0; # else - bool rc = memcmp(output, test_output0, (Options::i()->doubleHash() ? 64 : 32)) == 0; + bool rc = memcmp(output, test_output_v0, (doubleHash ? 64 : 32)) == 0; # endif - if (rc && algo == xmrig::ALGO_CRYPTONIGHT) { + if (rc) { cryptonight_hash_ctx(test_input, 76, output, ctx, 1); - rc = memcmp(output, test_output2, (Options::i()->doubleHash() ? 64 : 32)) == 0; +# ifndef XMRIG_NO_AEON + rc = memcmp(output, algo == xmrig::ALGO_CRYPTONIGHT_LITE ? test_output_v1_lite : test_output_v1, (doubleHash ? 64 : 32)) == 0; +# else + rc = memcmp(output, test_output_v1, (doubleHash ? 64 : 32)) == 0; +# endif } _mm_free(ctx->memory); diff --git a/src/crypto/CryptoNight_test.h b/src/crypto/CryptoNight_test.h index 65200f75..0f7feda1 100644 --- a/src/crypto/CryptoNight_test.h +++ b/src/crypto/CryptoNight_test.h @@ -40,7 +40,7 @@ const static uint8_t test_input[152] = { }; -const static uint8_t test_output0[64] = { +const static uint8_t test_output_v0[64] = { 0x1B, 0x60, 0x6A, 0x3F, 0x4A, 0x07, 0xD6, 0x48, 0x9A, 0x1B, 0xCD, 0x07, 0x69, 0x7B, 0xD1, 0x66, 0x96, 0xB6, 0x1C, 0x8A, 0xE9, 0x82, 0xF6, 0x1A, 0x90, 0x16, 0x0F, 0x4E, 0x52, 0x82, 0x8A, 0x7F, 0x1A, 0x3F, 0xFB, 0xEE, 0x90, 0x9B, 0x42, 0x0D, 0x91, 0xF7, 0xBE, 0x6E, 0x5F, 0xB5, 0x6D, 0xB7, @@ -48,18 +48,8 @@ const static uint8_t test_output0[64] = { }; -#ifndef XMRIG_NO_AEON -const static uint8_t test_output1[64] = { - 0x28, 0xA2, 0x2B, 0xAD, 0x3F, 0x93, 0xD1, 0x40, 0x8F, 0xCA, 0x47, 0x2E, 0xB5, 0xAD, 0x1C, 0xBE, - 0x75, 0xF2, 0x1D, 0x05, 0x3C, 0x8C, 0xE5, 0xB3, 0xAF, 0x10, 0x5A, 0x57, 0x71, 0x3E, 0x21, 0xDD, - 0x36, 0x95, 0xB4, 0xB5, 0x3B, 0xB0, 0x03, 0x58, 0xB0, 0xAD, 0x38, 0xDC, 0x16, 0x0F, 0xEB, 0x9E, - 0x00, 0x4E, 0xEC, 0xE0, 0x9B, 0x83, 0xA7, 0x2E, 0xF6, 0xBA, 0x98, 0x64, 0xD3, 0x51, 0x0C, 0x88, -}; -#endif - - // Monero v7 -const static uint8_t test_output2[64] = { +const static uint8_t test_output_v1[64] = { 0xC9, 0xFA, 0xE8, 0x42, 0x5D, 0x86, 0x88, 0xDC, 0x23, 0x6B, 0xCD, 0xBC, 0x42, 0xFD, 0xB4, 0x2D, 0x37, 0x6C, 0x6E, 0xC1, 0x90, 0x50, 0x1A, 0xA8, 0x4B, 0x04, 0xA4, 0xB4, 0xCF, 0x1E, 0xE1, 0x22, 0xF2, 0x2D, 0x3D, 0x62, 0x03, 0xD2, 0xA0, 0x8B, 0x41, 0xD9, 0x02, 0x72, 0x78, 0xD8, 0xBC, 0xC9, @@ -67,4 +57,23 @@ const static uint8_t test_output2[64] = { }; +#ifndef XMRIG_NO_AEON +const static uint8_t test_output_v0_lite[64] = { + 0x28, 0xA2, 0x2B, 0xAD, 0x3F, 0x93, 0xD1, 0x40, 0x8F, 0xCA, 0x47, 0x2E, 0xB5, 0xAD, 0x1C, 0xBE, + 0x75, 0xF2, 0x1D, 0x05, 0x3C, 0x8C, 0xE5, 0xB3, 0xAF, 0x10, 0x5A, 0x57, 0x71, 0x3E, 0x21, 0xDD, + 0x36, 0x95, 0xB4, 0xB5, 0x3B, 0xB0, 0x03, 0x58, 0xB0, 0xAD, 0x38, 0xDC, 0x16, 0x0F, 0xEB, 0x9E, + 0x00, 0x4E, 0xEC, 0xE0, 0x9B, 0x83, 0xA7, 0x2E, 0xF6, 0xBA, 0x98, 0x64, 0xD3, 0x51, 0x0C, 0x88 +}; + + +// AEON v2 +const static uint8_t test_output_v1_lite[64] = { + 0x87, 0xC4, 0xE5, 0x70, 0x65, 0x3E, 0xB4, 0xC2, 0xB4, 0x2B, 0x7A, 0x0D, 0x54, 0x65, 0x59, 0x45, + 0x2D, 0xFA, 0xB5, 0x73, 0xB8, 0x2E, 0xC5, 0x2F, 0x15, 0x2B, 0x7F, 0xF9, 0x8E, 0x79, 0x44, 0x6F, + 0x6D, 0x8C, 0xDC, 0x44, 0x4E, 0x9B, 0xBB, 0xFD, 0x68, 0xFC, 0x43, 0xFC, 0xD4, 0x85, 0x5B, 0x22, + 0x8C, 0x8A, 0x1B, 0xD9, 0x1D, 0x9D, 0x00, 0x28, 0x5B, 0xEC, 0x02, 0xB7, 0xCA, 0x2D, 0x67, 0x41 +}; +#endif + + #endif /* __CRYPTONIGHT_TEST_H__ */ diff --git a/src/net/Job.cpp b/src/net/Job.cpp index c8331fb9..b9d7372c 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -173,11 +173,15 @@ int Job::variant() const const uint8_t version = m_blob[0]; +# if !defined(XMRIG_NO_AEON) if (m_algo == xmrig::ALGO_CRYPTONIGHT) { return version > 6 ? 1 : 0; } return version > 1 ? 1 : 0; +# else + return version > 6 ? 1 : 0; +# endif } From 07f0a107d3be11bbd96cd21abfcd80e99d4e12c2 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 13 Mar 2018 02:17:19 +0700 Subject: [PATCH 20/23] Change port for AEON donate. --- src/net/strategies/DonateStrategy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net/strategies/DonateStrategy.cpp b/src/net/strategies/DonateStrategy.cpp index 22cffd28..84d1e4da 100644 --- a/src/net/strategies/DonateStrategy.cpp +++ b/src/net/strategies/DonateStrategy.cpp @@ -49,7 +49,7 @@ DonateStrategy::DonateStrategy(const char *agent, IStrategyListener *listener) : keccak(reinterpret_cast(user), static_cast(strlen(user)), hash, sizeof(hash)); Job::toHex(hash, 32, userId); - Url *url = new Url("thanks.xmrig.com", Options::i()->algo() == xmrig::ALGO_CRYPTONIGHT_LITE ? 3333 : 80, userId, nullptr, false, true); + Url *url = new Url("thanks.xmrig.com", Options::i()->algo() == xmrig::ALGO_CRYPTONIGHT_LITE ? 5555 : 80, userId, nullptr, false, true); m_client = new Client(-1, agent, this); m_client->setUrl(url); From a01b4d05668572d41be3bf68b843b5b7d251fe8a Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 13 Mar 2018 17:50:09 +0700 Subject: [PATCH 21/23] Simplify variant selection. --- src/net/Job.cpp | 21 --------------------- src/net/Job.h | 3 ++- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/src/net/Job.cpp b/src/net/Job.cpp index b9d7372c..8b1966aa 100644 --- a/src/net/Job.cpp +++ b/src/net/Job.cpp @@ -27,7 +27,6 @@ #include "net/Job.h" -#include "xmrig.h" static inline unsigned char hf_hex2bin(char c, bool &err) @@ -165,26 +164,6 @@ 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 !defined(XMRIG_NO_AEON) - if (m_algo == xmrig::ALGO_CRYPTONIGHT) { - return version > 6 ? 1 : 0; - } - - return version > 1 ? 1 : 0; -# else - return version > 6 ? 1 : 0; -# endif -} - - void Job::setCoin(const char *coin) { if (!coin || strlen(coin) > 4) { diff --git a/src/net/Job.h b/src/net/Job.h index 28d5e111..545eaa88 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -32,6 +32,7 @@ #include "align.h" #include "net/Id.h" +#include "xmrig.h" class Job @@ -43,7 +44,6 @@ public: bool setBlob(const char *blob); bool setTarget(const char *target); - int variant() const; void setCoin(const char *coin); void setVariant(int variant); @@ -56,6 +56,7 @@ public: inline const xmrig::Id &id() const { return m_id; } inline int poolId() const { return m_poolId; } inline int threadId() const { return m_threadId; } + inline int variant() const { return (m_variant == xmrig::VARIANT_AUTO ? (m_blob[0] > 6 ? 1 : 0) : m_variant); } inline size_t size() const { return m_size; } inline uint32_t *nonce() { return reinterpret_cast(m_blob + 39); } inline uint32_t diff() const { return (uint32_t) m_diff; } From 41abe17286d66c812f12f48907d9250319beea5d Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 13 Mar 2018 20:59:35 +0700 Subject: [PATCH 22/23] #438 Fixed memory release. --- src/Mem.cpp | 1 + src/Mem.h | 1 + src/Mem_unix.cpp | 22 ++++++++++------------ src/Mem_win.cpp | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Mem.cpp b/src/Mem.cpp index 73576d20..9c608f73 100644 --- a/src/Mem.cpp +++ b/src/Mem.cpp @@ -37,6 +37,7 @@ int Mem::m_algo = 0; int Mem::m_flags = 0; int Mem::m_threads = 0; size_t Mem::m_offset = 0; +size_t Mem::m_size = 0; uint8_t *Mem::m_memory = nullptr; diff --git a/src/Mem.h b/src/Mem.h index a9635a17..eaa4f49a 100644 --- a/src/Mem.h +++ b/src/Mem.h @@ -62,6 +62,7 @@ private: static int m_flags; static int m_threads; static size_t m_offset; + static size_t m_size; VAR_ALIGN(16, static uint8_t *m_memory); # ifndef XMRIG_NO_AEON diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 93cdbec0..f09b5096 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -47,8 +47,8 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_threads = threads; m_doubleHash = doubleHash; - const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; - const size_t size = MONERO_MEMORY * (threads * ratio + 1); + const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; + m_size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { m_memory = static_cast(_mm_malloc(size, 16)); @@ -58,24 +58,24 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_flags |= HugepagesAvailable; # if defined(__APPLE__) - m_memory = static_cast(mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0)); + m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0)); # elif defined(__FreeBSD__) - m_memory = static_cast(mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0)); + m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER | MAP_PREFAULT_READ, -1, 0)); # else - m_memory = static_cast(mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0)); + m_memory = static_cast(mmap(0, m_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_POPULATE, 0, 0)); # endif if (m_memory == MAP_FAILED) { - m_memory = static_cast(_mm_malloc(size, 16)); + m_memory = static_cast(_mm_malloc(m_size, 16)); return true; } m_flags |= HugepagesEnabled; - if (madvise(m_memory, size, MADV_RANDOM | MADV_WILLNEED) != 0) { + if (madvise(m_memory, m_size, MADV_RANDOM | MADV_WILLNEED) != 0) { LOG_ERR("madvise failed"); } - if (mlock(m_memory, size) == 0) { + if (mlock(m_memory, m_size) == 0) { m_flags |= Lock; } @@ -85,14 +85,12 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) void Mem::release() { - const int size = MONERO_MEMORY * (m_threads + 1); - if (m_flags & HugepagesEnabled) { if (m_flags & Lock) { - munlock(m_memory, size); + munlock(m_memory, m_size); } - munmap(m_memory, size); + munmap(m_memory, m_size); } else { _mm_free(m_memory); diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index 28eaff49..239bda7d 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -153,10 +153,10 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_doubleHash = doubleHash; const int ratio = (doubleHash && algo != xmrig::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; - const size_t size = MONERO_MEMORY * (threads * ratio + 1); + m_size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { - m_memory = static_cast(_mm_malloc(size, 16)); + m_memory = static_cast(_mm_malloc(m_size, 16)); return true; } @@ -164,9 +164,9 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_flags |= HugepagesAvailable; } - m_memory = static_cast(VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE)); + m_memory = static_cast(VirtualAlloc(NULL, m_size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE)); if (!m_memory) { - m_memory = static_cast(_mm_malloc(size, 16)); + m_memory = static_cast(_mm_malloc(m_size, 16)); } else { m_flags |= HugepagesEnabled; From 81ef40fbb778b16cb8069b497314e9883f987ac7 Mon Sep 17 00:00:00 2001 From: XMRig Date: Tue, 13 Mar 2018 21:01:00 +0700 Subject: [PATCH 23/23] Fix for previous commit. --- src/Mem_unix.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index f09b5096..3e699544 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -51,7 +51,7 @@ bool Mem::allocate(int algo, int threads, bool doubleHash, bool enabled) m_size = MONERO_MEMORY * (threads * ratio + 1); if (!enabled) { - m_memory = static_cast(_mm_malloc(size, 16)); + m_memory = static_cast(_mm_malloc(m_size, 16)); return true; }