danicoin/src/CryptoNoteCore/Miner.cpp

402 lines
12 KiB
C++
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
2014-03-03 22:07:58 +00:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2015-07-30 15:22:07 +00:00
#include "Miner.h"
2015-05-27 12:08:46 +00:00
#include <future>
2014-03-03 22:07:58 +00:00
#include <numeric>
2015-05-27 12:08:46 +00:00
#include <sstream>
#include <thread>
2014-08-13 10:51:37 +00:00
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
2015-05-27 12:08:46 +00:00
#include <boost/algorithm/string/trim.hpp>
2015-07-15 12:23:00 +00:00
#include <boost/filesystem.hpp>
2014-03-03 22:07:58 +00:00
#include <boost/limits.hpp>
2014-08-13 10:51:37 +00:00
#include <boost/utility/value_init.hpp>
2015-07-30 15:22:07 +00:00
#include "crypto/crypto.h"
#include "Common/CommandLine.h"
#include "Common/StringTools.h"
#include "Serialization/SerializationTools.h"
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
#include "CryptoNoteFormatUtils.h"
#include "TransactionExtra.h"
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
using namespace Logging;
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
namespace CryptoNote
2014-03-03 22:07:58 +00:00
{
2015-07-30 15:22:07 +00:00
miner::miner(const Currency& currency, IMinerHandler& handler, Logging::ILogger& log) :
2014-08-13 10:51:37 +00:00
m_currency(currency),
2015-05-27 12:08:46 +00:00
logger(log, "miner"),
m_stop(true),
2014-08-13 10:51:37 +00:00
m_template(boost::value_initialized<Block>()),
2014-03-03 22:07:58 +00:00
m_template_no(0),
m_diffic(0),
2015-05-27 12:08:46 +00:00
m_handler(handler),
2014-06-25 17:21:42 +00:00
m_pausers_count(0),
2014-03-03 22:07:58 +00:00
m_threads_total(0),
2014-06-25 17:21:42 +00:00
m_starter_nonce(0),
2014-03-03 22:07:58 +00:00
m_last_hr_merge_time(0),
m_hashes(0),
m_do_print_hashrate(false),
2014-04-07 15:02:15 +00:00
m_do_mining(false),
2015-05-27 12:08:46 +00:00
m_current_hash_rate(0),
m_update_block_template_interval(5),
m_update_merge_hr_interval(2)
2014-03-03 22:07:58 +00:00
{
}
//-----------------------------------------------------------------------------------------------------
2014-08-13 10:51:37 +00:00
miner::~miner() {
2014-03-03 22:07:58 +00:00
stop();
}
//-----------------------------------------------------------------------------------------------------
2014-08-13 10:51:37 +00:00
bool miner::set_block_template(const Block& bl, const difficulty_type& di) {
2015-05-27 12:08:46 +00:00
std::lock_guard<decltype(m_template_lock)> lk(m_template_lock);
2014-08-13 10:51:37 +00:00
2014-03-03 22:07:58 +00:00
m_template = bl;
m_diffic = di;
++m_template_no;
2015-07-30 15:22:07 +00:00
m_starter_nonce = Crypto::rand<uint32_t>();
2014-03-03 22:07:58 +00:00
return true;
}
//-----------------------------------------------------------------------------------------------------
2014-08-13 10:51:37 +00:00
bool miner::on_block_chain_update() {
if (!is_mining()) {
2014-03-03 22:07:58 +00:00
return true;
2014-08-13 10:51:37 +00:00
}
2014-03-03 22:07:58 +00:00
return request_block_template();
}
//-----------------------------------------------------------------------------------------------------
2014-08-13 10:51:37 +00:00
bool miner::request_block_template() {
2015-05-27 12:08:46 +00:00
Block bl = boost::value_initialized<Block>();
difficulty_type di = 0;
uint32_t height;
2015-07-30 15:22:07 +00:00
CryptoNote::BinaryArray extra_nonce;
2015-05-27 12:08:46 +00:00
if(m_extra_messages.size() && m_config.current_extra_message_index < m_extra_messages.size()) {
2014-03-03 22:07:58 +00:00
extra_nonce = m_extra_messages[m_config.current_extra_message_index];
}
2015-05-27 12:08:46 +00:00
if(!m_handler.get_block_template(bl, m_mine_address, di, height, extra_nonce)) {
logger(ERROR) << "Failed to get_block_template(), stopping mining";
2014-03-03 22:07:58 +00:00
return false;
}
2015-05-27 12:08:46 +00:00
2014-08-13 10:51:37 +00:00
set_block_template(bl, di);
2014-03-03 22:07:58 +00:00
return true;
}
//-----------------------------------------------------------------------------------------------------
bool miner::on_idle()
{
2015-05-27 12:08:46 +00:00
m_update_block_template_interval.call([&](){
if(is_mining())
request_block_template();
2014-03-03 22:07:58 +00:00
return true;
});
2015-05-27 12:08:46 +00:00
m_update_merge_hr_interval.call([&](){
2014-03-03 22:07:58 +00:00
merge_hr();
return true;
});
2014-06-25 17:21:42 +00:00
2014-03-03 22:07:58 +00:00
return true;
}
//-----------------------------------------------------------------------------------------------------
void miner::do_print_hashrate(bool do_hr)
{
m_do_print_hashrate = do_hr;
}
2015-05-27 12:08:46 +00:00
uint64_t millisecondsSinceEpoch() {
auto now = std::chrono::steady_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
}
2014-03-03 22:07:58 +00:00
//-----------------------------------------------------------------------------------------------------
void miner::merge_hr()
{
2015-05-27 12:08:46 +00:00
if(m_last_hr_merge_time && is_mining()) {
m_current_hash_rate = m_hashes * 1000 / (millisecondsSinceEpoch() - m_last_hr_merge_time + 1);
std::lock_guard<std::mutex> lk(m_last_hash_rates_lock);
2014-03-03 22:07:58 +00:00
m_last_hash_rates.push_back(m_current_hash_rate);
if(m_last_hash_rates.size() > 19)
m_last_hash_rates.pop_front();
2015-05-27 12:08:46 +00:00
if(m_do_print_hashrate) {
uint64_t total_hr = std::accumulate(m_last_hash_rates.begin(), m_last_hash_rates.end(), static_cast<uint64_t>(0));
2014-03-03 22:07:58 +00:00
float hr = static_cast<float>(total_hr)/static_cast<float>(m_last_hash_rates.size());
std::cout << "hashrate: " << std::setprecision(4) << std::fixed << hr << ENDL;
}
}
2015-05-27 12:08:46 +00:00
m_last_hr_merge_time = millisecondsSinceEpoch();
2014-03-03 22:07:58 +00:00
m_hashes = 0;
}
bool miner::init(const MinerConfig& config) {
if (!config.extraMessages.empty()) {
2014-03-03 22:07:58 +00:00
std::string buff;
2015-05-27 12:08:46 +00:00
if (!Common::loadFileToString(config.extraMessages, buff)) {
logger(ERROR, BRIGHT_RED) << "Failed to load file with extra messages: " << config.extraMessages;
return false;
}
2014-03-03 22:07:58 +00:00
std::vector<std::string> extra_vec;
boost::split(extra_vec, buff, boost::is_any_of("\n"), boost::token_compress_on );
m_extra_messages.resize(extra_vec.size());
for(size_t i = 0; i != extra_vec.size(); i++) {
2015-05-27 12:08:46 +00:00
boost::algorithm::trim(extra_vec[i]);
2014-03-03 22:07:58 +00:00
if(!extra_vec[i].size())
continue;
2015-07-30 15:22:07 +00:00
BinaryArray ba = Common::asBinaryArray(Common::base64Decode(extra_vec[i]));
2014-03-03 22:07:58 +00:00
if(buff != "0")
2015-07-30 15:22:07 +00:00
m_extra_messages[i] = ba;
2014-03-03 22:07:58 +00:00
}
m_config_folder_path = boost::filesystem::path(config.extraMessages).parent_path().string();
2015-05-27 12:08:46 +00:00
m_config = boost::value_initialized<decltype(m_config)>();
2015-07-15 12:23:00 +00:00
std::string filebuf;
if (Common::loadFileToString(m_config_folder_path + "/" + CryptoNote::parameters::MINER_CONFIG_FILE_NAME, filebuf)) {
loadFromJson(m_config, filebuf);
}
2015-05-27 12:08:46 +00:00
logger(INFO) << "Loaded " << m_extra_messages.size() << " extra messages, current index " << m_config.current_extra_message_index;
2014-03-03 22:07:58 +00:00
}
if(!config.startMining.empty()) {
if (!m_currency.parseAccountAddressString(config.startMining, m_mine_address)) {
2015-07-15 12:23:00 +00:00
logger(ERROR) << "Target account address " << config.startMining << " has wrong format, starting daemon canceled";
2014-03-03 22:07:58 +00:00
return false;
}
m_threads_total = 1;
m_do_mining = true;
if(config.miningThreads > 0) {
m_threads_total = config.miningThreads;
2014-03-03 22:07:58 +00:00
}
}
return true;
}
//-----------------------------------------------------------------------------------------------------
bool miner::is_mining()
{
return !m_stop;
}
2014-06-25 17:21:42 +00:00
//-----------------------------------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
bool miner::start(const AccountPublicAddress& adr, size_t threads_count)
2015-05-27 12:08:46 +00:00
{
if (is_mining()) {
logger(ERROR) << "Starting miner but it's already started";
2014-03-03 22:07:58 +00:00
return false;
}
2015-05-27 12:08:46 +00:00
std::lock_guard<std::mutex> lk(m_threads_lock);
if(!m_threads.empty()) {
logger(ERROR) << "Unable to start miner because there are active mining threads";
2014-03-03 22:07:58 +00:00
return false;
}
2015-05-27 12:08:46 +00:00
m_mine_address = adr;
m_threads_total = static_cast<uint32_t>(threads_count);
2015-07-30 15:22:07 +00:00
m_starter_nonce = Crypto::rand<uint32_t>();
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
if (!m_template_no) {
request_block_template(); //lets update block template
}
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
m_stop = false;
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
for (uint32_t i = 0; i != threads_count; i++) {
2015-07-30 15:22:07 +00:00
m_threads.push_back(std::thread(std::bind(&miner::worker_thread, this, i)));
2014-04-30 16:21:49 +00:00
}
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
logger(INFO) << "Mining has started with " << threads_count << " threads, good luck!";
2014-03-03 22:07:58 +00:00
return true;
}
2015-05-27 12:08:46 +00:00
2014-03-03 22:07:58 +00:00
//-----------------------------------------------------------------------------------------------------
uint64_t miner::get_speed()
{
2014-04-07 15:02:15 +00:00
if(is_mining())
return m_current_hash_rate;
else
return 0;
2014-03-03 22:07:58 +00:00
}
2015-05-27 12:08:46 +00:00
2014-03-03 22:07:58 +00:00
//-----------------------------------------------------------------------------------------------------
2015-05-27 12:08:46 +00:00
void miner::send_stop_signal()
2014-03-03 22:07:58 +00:00
{
2015-05-27 12:08:46 +00:00
m_stop = true;
2014-03-03 22:07:58 +00:00
}
2015-05-27 12:08:46 +00:00
2014-03-03 22:07:58 +00:00
//-----------------------------------------------------------------------------------------------------
bool miner::stop()
{
send_stop_signal();
2015-05-27 12:08:46 +00:00
std::lock_guard<std::mutex> lk(m_threads_lock);
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
for (auto& th : m_threads) {
2014-03-03 22:07:58 +00:00
th.join();
2015-05-27 12:08:46 +00:00
}
2014-03-03 22:07:58 +00:00
m_threads.clear();
2015-05-27 12:08:46 +00:00
logger(INFO) << "Mining has been stopped, " << m_threads.size() << " finished" ;
2014-03-03 22:07:58 +00:00
return true;
}
//-----------------------------------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
bool miner::find_nonce_for_given_block(Crypto::cn_context &context, Block& bl, const difficulty_type& diffic) {
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
unsigned nthreads = std::thread::hardware_concurrency();
if (nthreads > 0 && diffic > 5) {
std::vector<std::future<void>> threads(nthreads);
std::atomic<uint32_t> foundNonce;
std::atomic<bool> found(false);
2015-07-30 15:22:07 +00:00
uint32_t startNonce = Crypto::rand<uint32_t>();
2014-08-13 10:51:37 +00:00
for (unsigned i = 0; i < nthreads; ++i) {
threads[i] = std::async(std::launch::async, [&, i]() {
2015-07-30 15:22:07 +00:00
Crypto::cn_context localctx;
Crypto::Hash h;
2014-08-13 10:51:37 +00:00
Block lb(bl); // copy to local block
for (uint32_t nonce = startNonce + i; !found; nonce += nthreads) {
lb.nonce = nonce;
if (!get_block_longhash(localctx, lb, h)) {
return;
}
if (check_hash(h, diffic)) {
foundNonce = nonce;
found = true;
return;
}
}
});
}
for (auto& t : threads) {
t.wait();
}
if (found) {
bl.nonce = foundNonce.load();
}
return found;
} else {
for (; bl.nonce != std::numeric_limits<uint32_t>::max(); bl.nonce++) {
2015-07-30 15:22:07 +00:00
Crypto::Hash h;
2014-08-13 10:51:37 +00:00
if (!get_block_longhash(context, bl, h)) {
return false;
}
if (check_hash(h, diffic)) {
return true;
}
2014-03-03 22:07:58 +00:00
}
}
2014-08-13 10:51:37 +00:00
2014-03-03 22:07:58 +00:00
return false;
}
//-----------------------------------------------------------------------------------------------------
void miner::on_synchronized()
{
2015-07-30 15:22:07 +00:00
if(m_do_mining) {
start(m_mine_address, m_threads_total);
2014-03-03 22:07:58 +00:00
}
}
//-----------------------------------------------------------------------------------------------------
void miner::pause()
{
2015-05-27 12:08:46 +00:00
std::lock_guard<std::mutex> lk(m_miners_count_lock);
2014-03-03 22:07:58 +00:00
++m_pausers_count;
if(m_pausers_count == 1 && is_mining())
2015-05-27 12:08:46 +00:00
logger(TRACE) << "MINING PAUSED";
2014-03-03 22:07:58 +00:00
}
//-----------------------------------------------------------------------------------------------------
void miner::resume()
{
2015-05-27 12:08:46 +00:00
std::lock_guard<std::mutex> lk(m_miners_count_lock);
2014-03-03 22:07:58 +00:00
--m_pausers_count;
if(m_pausers_count < 0)
{
m_pausers_count = 0;
2015-05-27 12:08:46 +00:00
logger(ERROR) << "Unexpected miner::resume() called";
2014-03-03 22:07:58 +00:00
}
if(!m_pausers_count && is_mining())
2015-05-27 12:08:46 +00:00
logger(TRACE) << "MINING RESUMED";
2014-03-03 22:07:58 +00:00
}
//-----------------------------------------------------------------------------------------------------
2015-05-27 12:08:46 +00:00
bool miner::worker_thread(uint32_t th_local_index)
2014-03-03 22:07:58 +00:00
{
2015-05-27 12:08:46 +00:00
logger(INFO) << "Miner thread was started ["<< th_local_index << "]";
2014-03-03 22:07:58 +00:00
uint32_t nonce = m_starter_nonce + th_local_index;
difficulty_type local_diff = 0;
uint32_t local_template_ver = 0;
2015-07-30 15:22:07 +00:00
Crypto::cn_context context;
2014-08-13 10:51:37 +00:00
Block b;
2015-05-27 12:08:46 +00:00
2014-03-03 22:07:58 +00:00
while(!m_stop)
{
2015-05-27 12:08:46 +00:00
if(m_pausers_count) //anti split workaround
2014-03-03 22:07:58 +00:00
{
2015-05-27 12:08:46 +00:00
std::this_thread::sleep_for(std::chrono::milliseconds(100));
2014-03-03 22:07:58 +00:00
continue;
}
2015-05-27 12:08:46 +00:00
if(local_template_ver != m_template_no) {
std::unique_lock<std::mutex> lk(m_template_lock);
2014-03-03 22:07:58 +00:00
b = m_template;
local_diff = m_diffic;
2015-05-27 12:08:46 +00:00
lk.unlock();
2014-03-03 22:07:58 +00:00
local_template_ver = m_template_no;
nonce = m_starter_nonce + th_local_index;
}
if(!local_template_ver)//no any set_block_template call
{
2015-05-27 12:08:46 +00:00
logger(TRACE) << "Block template not set yet";
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
2014-03-03 22:07:58 +00:00
continue;
}
b.nonce = nonce;
2015-07-30 15:22:07 +00:00
Crypto::Hash h;
2014-08-13 10:51:37 +00:00
if (!m_stop && !get_block_longhash(context, b, h)) {
2015-05-27 12:08:46 +00:00
logger(ERROR) << "Failed to get block long hash";
2014-08-13 10:51:37 +00:00
m_stop = true;
}
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
if (!m_stop && check_hash(h, local_diff))
2014-03-03 22:07:58 +00:00
{
//we lucky!
++m_config.current_extra_message_index;
2015-05-27 12:08:46 +00:00
logger(INFO, GREEN) << "Found block for difficulty: " << local_diff;
if(!m_handler.handle_block_found(b)) {
2014-03-03 22:07:58 +00:00
--m_config.current_extra_message_index;
2015-05-27 12:08:46 +00:00
} else {
2014-03-03 22:07:58 +00:00
//success update, lets update config
2015-07-15 12:23:00 +00:00
Common::saveStringToFile(m_config_folder_path + "/" + CryptoNote::parameters::MINER_CONFIG_FILE_NAME, storeToJson(m_config));
2014-03-03 22:07:58 +00:00
}
}
2014-06-20 15:56:33 +00:00
2015-05-27 12:08:46 +00:00
nonce += m_threads_total;
2014-03-03 22:07:58 +00:00
++m_hashes;
}
2015-05-27 12:08:46 +00:00
logger(INFO) << "Miner thread stopped ["<< th_local_index << "]";
2014-03-03 22:07:58 +00:00
return true;
}
//-----------------------------------------------------------------------------------------------------
}