danicoin/src/CryptoNoteCore/Miner.h

91 lines
2.6 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.
2014-06-25 17:21:42 +00:00
#pragma once
2014-03-03 22:07:58 +00:00
#include <atomic>
2015-07-30 15:22:07 +00:00
#include <list>
#include <mutex>
#include <thread>
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
#include "CryptoNoteCore/CryptoNoteBasic.h"
#include "CryptoNoteCore/Currency.h"
#include "CryptoNoteCore/Difficulty.h"
#include "CryptoNoteCore/IMinerHandler.h"
#include "CryptoNoteCore/MinerConfig.h"
#include "CryptoNoteCore/OnceInInterval.h"
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
#include <Logging/LoggerRef.h>
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
#include "Serialization/ISerializer.h"
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
namespace CryptoNote {
2014-08-13 10:51:37 +00:00
class miner {
2014-06-25 17:21:42 +00:00
public:
2015-07-30 15:22:07 +00:00
miner(const Currency& currency, IMinerHandler& handler, Logging::ILogger& log);
2014-03-03 22:07:58 +00:00
~miner();
bool init(const MinerConfig& config);
2014-08-13 10:51:37 +00:00
bool set_block_template(const Block& bl, const difficulty_type& diffic);
2014-03-03 22:07:58 +00:00
bool on_block_chain_update();
2015-07-30 15:22:07 +00:00
bool start(const AccountPublicAddress& adr, size_t threads_count);
2014-03-03 22:07:58 +00:00
uint64_t get_speed();
void send_stop_signal();
bool stop();
bool is_mining();
bool on_idle();
void on_synchronized();
//synchronous analog (for fast calls)
2015-07-30 15:22:07 +00:00
static bool find_nonce_for_given_block(Crypto::cn_context &context, Block& bl, const difficulty_type& diffic);
2014-03-03 22:07:58 +00:00
void pause();
void resume();
void do_print_hashrate(bool do_hr);
private:
2015-05-27 12:08:46 +00:00
bool worker_thread(uint32_t th_local_index);
2014-03-03 22:07:58 +00:00
bool request_block_template();
void merge_hr();
2014-06-25 17:21:42 +00:00
2014-03-03 22:07:58 +00:00
struct miner_config
{
uint64_t current_extra_message_index;
2015-07-15 12:23:00 +00:00
void serialize(ISerializer& s) {
KV_MEMBER(current_extra_message_index)
}
2014-03-03 22:07:58 +00:00
};
2014-08-13 10:51:37 +00:00
const Currency& m_currency;
2015-05-27 12:08:46 +00:00
Logging::LoggerRef logger;
std::atomic<bool> m_stop;
std::mutex m_template_lock;
2014-08-13 10:51:37 +00:00
Block m_template;
2014-03-03 22:07:58 +00:00
std::atomic<uint32_t> m_template_no;
std::atomic<uint32_t> m_starter_nonce;
difficulty_type m_diffic;
2015-05-27 12:08:46 +00:00
std::atomic<uint32_t> m_threads_total;
2014-03-03 22:07:58 +00:00
std::atomic<int32_t> m_pausers_count;
2015-05-27 12:08:46 +00:00
std::mutex m_miners_count_lock;
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
std::list<std::thread> m_threads;
2015-05-27 12:08:46 +00:00
std::mutex m_threads_lock;
2015-07-30 15:22:07 +00:00
IMinerHandler& m_handler;
2014-08-13 10:51:37 +00:00
AccountPublicAddress m_mine_address;
2015-05-27 12:08:46 +00:00
OnceInInterval m_update_block_template_interval;
OnceInInterval m_update_merge_hr_interval;
2015-07-30 15:22:07 +00:00
std::vector<BinaryArray> m_extra_messages;
2014-03-03 22:07:58 +00:00
miner_config m_config;
2014-06-25 17:21:42 +00:00
std::string m_config_folder_path;
2014-03-03 22:07:58 +00:00
std::atomic<uint64_t> m_last_hr_merge_time;
std::atomic<uint64_t> m_hashes;
std::atomic<uint64_t> m_current_hash_rate;
2015-05-27 12:08:46 +00:00
std::mutex m_last_hash_rates_lock;
2014-03-03 22:07:58 +00:00
std::list<uint64_t> m_last_hash_rates;
bool m_do_print_hashrate;
bool m_do_mining;
};
}