/* 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 2018 SChernykh * Copyright 2016-2019 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 . */ #include #include #include "crypto/CryptoNight_test.h" #include "common/log/Log.h" #include "workers/CpuThread.h" #include "workers/MultiWorker.h" #include "workers/Workers.h" template MultiWorker::MultiWorker(Handle *handle) : Worker(handle) { m_memory = Mem::create(m_ctx, m_thread->algorithm(), N); } template MultiWorker::~MultiWorker() { Mem::release(m_ctx, N, m_memory); } template bool MultiWorker::selfTest() { using namespace xmrig; if (m_thread->algorithm() == CRYPTONIGHT) { if (!verify2(VARIANT_WOW, test_input_WOW)) { LOG_WARN("CryptonightR (Wownero) self-test failed"); return false; } if (!verify2(VARIANT_4, test_input_R)) { LOG_WARN("CryptonightR self-test failed"); return false; } const bool rc = verify(VARIANT_0, test_output_v0) && verify(VARIANT_1, test_output_v1) && verify(VARIANT_2, test_output_v2) && verify(VARIANT_XTL, test_output_xtl) && verify(VARIANT_MSR, test_output_msr) && verify(VARIANT_XAO, test_output_xao) && verify(VARIANT_RTO, test_output_rto) && verify(VARIANT_HALF, test_output_half); # ifndef XMRIG_NO_CN_GPU if (!rc || N > 1) { return rc; } return verify(VARIANT_GPU, test_output_gpu); # else return rc; # endif } # ifndef XMRIG_NO_AEON if (m_thread->algorithm() == CRYPTONIGHT_LITE) { return verify(VARIANT_0, test_output_v0_lite) && verify(VARIANT_1, test_output_v1_lite); } # endif # ifndef XMRIG_NO_SUMO if (m_thread->algorithm() == CRYPTONIGHT_HEAVY) { return verify(VARIANT_0, test_output_v0_heavy) && verify(VARIANT_XHV, test_output_xhv_heavy) && verify(VARIANT_TUBE, test_output_tube_heavy); } # endif # ifndef XMRIG_NO_CN_PICO if (m_thread->algorithm() == CRYPTONIGHT_PICO) { return verify(VARIANT_TRTL, test_output_pico_trtl); } # endif return false; } template void MultiWorker::start() { while (Workers::sequence() > 0) { if (Workers::isPaused()) { do { std::this_thread::sleep_for(std::chrono::milliseconds(200)); } while (Workers::isPaused()); if (Workers::sequence() == 0) { break; } consumeJob(); } while (!Workers::isOutdated(m_sequence)) { if ((m_count & 0x7) == 0) { storeStats(); } m_thread->fn(m_state.job.algorithm().variant())(m_state.blob, m_state.job.size(), m_hash, m_ctx, m_state.job.height()); for (size_t i = 0; i < N; ++i) { if (*reinterpret_cast(m_hash + (i * 32) + 24) < m_state.job.target()) { Workers::submit(JobResult(m_state.job.poolId(), m_state.job.id(), m_state.job.clientId(), *nonce(i), m_hash + (i * 32), m_state.job.diff(), m_state.job.algorithm())); } *nonce(i) += 1; } m_count += N; std::this_thread::yield(); } consumeJob(); } } template bool MultiWorker::resume(const Job &job) { if (m_state.job.poolId() == -1 && job.poolId() >= 0 && job.id() == m_pausedState.job.id()) { m_state = m_pausedState; return true; } return false; } template bool MultiWorker::verify(xmrig::Variant variant, const uint8_t *referenceValue) { xmrig::CpuThread::cn_hash_fun func = m_thread->fn(variant); if (!func) { return false; } func(test_input, 76, m_hash, m_ctx, 0); return memcmp(m_hash, referenceValue, sizeof m_hash) == 0; } template bool MultiWorker::verify2(xmrig::Variant variant, const char *test_data) { xmrig::CpuThread::cn_hash_fun func = m_thread->fn(variant); if (!func) { return false; } std::stringstream s(test_data); std::string expected_hex; std::string input_hex; uint64_t height; while (!s.eof()) { uint8_t referenceValue[N * 32]; uint8_t input[N * 256]; s >> expected_hex; s >> input_hex; s >> height; if ((expected_hex.length() != 64) || (input_hex.length() > 512)) { return false; } bool err = false; for (int i = 0; i < 32; ++i) { referenceValue[i] = (hf_hex2bin(expected_hex[i * 2], err) << 4) + hf_hex2bin(expected_hex[i * 2 + 1], err); } const size_t input_len = input_hex.length() / 2; for (size_t i = 0; i < input_len; ++i) { input[i] = (hf_hex2bin(input_hex[i * 2], err) << 4) + hf_hex2bin(input_hex[i * 2 + 1], err); } if (err) { return false; } for (size_t i = 1; i < N; ++i) { memcpy(input + i * input_len, input, input_len); memcpy(referenceValue + i * 32, referenceValue, 32); } func(input, input_len, m_hash, m_ctx, height); if (memcmp(m_hash, referenceValue, sizeof m_hash) != 0) { return false; } } return true; } template void MultiWorker::consumeJob() { Job job = Workers::job(); m_sequence = Workers::sequence(); if (m_state.job == job) { return; } save(job); if (resume(job)) { return; } m_state.job = job; const size_t size = m_state.job.size(); memcpy(m_state.blob, m_state.job.blob(), m_state.job.size()); if (N > 1) { for (size_t i = 1; i < N; ++i) { memcpy(m_state.blob + (i * size), m_state.blob, size); } } for (size_t i = 0; i < N; ++i) { if (m_state.job.isNicehash()) { *nonce(i) = (*nonce(i) & 0xff000000U) + (0xffffffU / m_totalWays * (m_offset + i)); } else { *nonce(i) = 0xffffffffU / m_totalWays * (m_offset + i); } } } template void MultiWorker::save(const Job &job) { if (job.poolId() == -1 && m_state.job.poolId() >= 0) { m_pausedState = m_state; } } template class MultiWorker<1>; template class MultiWorker<2>; template class MultiWorker<3>; template class MultiWorker<4>; template class MultiWorker<5>;