| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include <boost/program_options.hpp> |
| 34 |
#include <boost/logic/tribool_fwd.hpp> |
| 35 |
#include <atomic> |
| 36 |
#include "cryptonote_basic.h" |
| 37 |
#include "verification_context.h" |
| 38 |
#include "difficulty.h" |
| 39 |
#include "math_helper.h" |
| 40 |
#ifdef _WIN32 |
| 41 |
#include <windows.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
namespace cryptonote |
| 45 |
{ |
| 46 |
|
| 47 |
struct i_miner_handler |
| 48 |
{ |
| 49 |
virtual bool handle_block_found(block& b, block_verification_context &bvc) = 0; |
| 50 |
virtual bool get_block_template(block& b, const account_public_address& adr, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce, uint64_t &seed_height, crypto::hash &seed_hash) = 0; |
| 51 |
protected: |
| 52 |
~i_miner_handler(){}; |
| 53 |
}; |
| 54 |
|
| 55 |
typedef std::function<bool(const cryptonote::block&, uint64_t, const crypto::hash*, unsigned int, crypto::hash&)> get_block_hash_t; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
class miner |
| 61 |
{ |
| 62 |
public: |
| 63 |
miner(i_miner_handler* phandler, const get_block_hash_t& gbh); |
| 64 |
~miner(); |
| 65 |
bool init(const boost::program_options::variables_map& vm, network_type nettype); |
| 66 |
static void init_options(boost::program_options::options_description& desc); |
| 67 |
bool set_block_template(const block& bl, const difficulty_type& diffic, uint64_t height, uint64_t block_reward); |
| 68 |
bool on_block_chain_update(); |
| 69 |
bool start(const account_public_address& adr, size_t threads_count, bool do_background = false, bool ignore_battery = false); |
| 70 |
uint64_t get_speed() const; |
| 71 |
uint32_t get_threads_count() const; |
| 72 |
void send_stop_signal(); |
| 73 |
bool stop(); |
| 74 |
bool is_mining() const; |
| 75 |
const account_public_address& get_mining_address() const; |
| 76 |
bool on_idle(); |
| 77 |
void on_synchronized(); |
| 78 |
|
| 79 |
static bool find_nonce_for_given_block(const get_block_hash_t &gbh, block& bl, const difficulty_type& diffic, uint64_t height, const crypto::hash *seed_hash = NULL); |
| 80 |
void pause(); |
| 81 |
void resume(); |
| 82 |
void do_print_hashrate(bool do_hr); |
| 83 |
bool get_is_background_mining_enabled() const; |
| 84 |
bool get_ignore_battery() const; |
| 85 |
uint64_t get_min_idle_seconds() const; |
| 86 |
bool set_min_idle_seconds(uint64_t min_idle_seconds); |
| 87 |
uint8_t get_idle_threshold() const; |
| 88 |
bool set_idle_threshold(uint8_t idle_threshold); |
| 89 |
uint8_t get_mining_target() const; |
| 90 |
bool set_mining_target(uint8_t mining_target); |
| 91 |
uint64_t get_block_reward() const { return m_block_reward; } |
| 92 |
|
| 93 |
static constexpr uint8_t BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE = 90; |
| 94 |
static constexpr uint8_t BACKGROUND_MINING_MIN_IDLE_THRESHOLD_PERCENTAGE = 0; |
| 95 |
static constexpr uint8_t BACKGROUND_MINING_MAX_IDLE_THRESHOLD_PERCENTAGE = 99; |
| 96 |
static constexpr uint16_t BACKGROUND_MINING_DEFAULT_MIN_IDLE_INTERVAL_IN_SECONDS = 10; |
| 97 |
static constexpr uint16_t BACKGROUND_MINING_MIN_MIN_IDLE_INTERVAL_IN_SECONDS = 10; |
| 98 |
static constexpr uint16_t BACKGROUND_MINING_MAX_MIN_IDLE_INTERVAL_IN_SECONDS = 3600; |
| 99 |
static constexpr uint8_t BACKGROUND_MINING_DEFAULT_MINING_TARGET_PERCENTAGE = 40; |
| 100 |
static constexpr uint8_t BACKGROUND_MINING_MIN_MINING_TARGET_PERCENTAGE = 1; |
| 101 |
static constexpr uint8_t BACKGROUND_MINING_MAX_MINING_TARGET_PERCENTAGE = 100; |
| 102 |
static constexpr uint8_t BACKGROUND_MINING_MINER_MONITOR_INVERVAL_IN_SECONDS = 10; |
| 103 |
static constexpr uint64_t BACKGROUND_MINING_DEFAULT_MINER_EXTRA_SLEEP_MILLIS = 400; |
| 104 |
|
| 105 |
private: |
| 106 |
bool worker_thread(); |
| 107 |
bool request_block_template(); |
| 108 |
void merge_hr(); |
| 109 |
void update_autodetection(); |
| 110 |
|
| 111 |
struct miner_config |
| 112 |
{ |
| 113 |
uint64_t current_extra_message_index; |
| 114 |
|
| 115 |
BEGIN_KV_SERIALIZE_MAP() |
| 116 |
KV_SERIALIZE(current_extra_message_index) |
| 117 |
END_KV_SERIALIZE_MAP() |
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
std::atomic<bool> m_stop; |
| 122 |
epee::critical_section m_template_lock; |
| 123 |
block m_template; |
| 124 |
std::atomic<uint32_t> m_template_no; |
| 125 |
std::atomic<uint32_t> m_starter_nonce; |
| 126 |
difficulty_type m_diffic; |
| 127 |
uint64_t m_height; |
| 128 |
std::atomic<uint32_t> m_thread_index; |
| 129 |
volatile uint32_t m_threads_total; |
| 130 |
std::atomic<uint32_t> m_threads_active; |
| 131 |
std::atomic<int32_t> m_pausers_count; |
| 132 |
epee::critical_section m_miners_count_lock; |
| 133 |
|
| 134 |
std::list<boost::thread> m_threads; |
| 135 |
epee::critical_section m_threads_lock; |
| 136 |
i_miner_handler* m_phandler; |
| 137 |
get_block_hash_t m_gbh; |
| 138 |
account_public_address m_mine_address; |
| 139 |
epee::math_helper::once_a_time_seconds<5> m_update_block_template_interval; |
| 140 |
epee::math_helper::once_a_time_seconds<2> m_update_merge_hr_interval; |
| 141 |
epee::math_helper::once_a_time_seconds<1> m_autodetect_interval; |
| 142 |
std::vector<blobdata> m_extra_messages; |
| 143 |
miner_config m_config; |
| 144 |
std::string m_config_folder_path; |
| 145 |
std::atomic<uint64_t> m_last_hr_merge_time; |
| 146 |
std::atomic<uint64_t> m_hashes; |
| 147 |
std::atomic<uint64_t> m_total_hashes; |
| 148 |
std::atomic<uint64_t> m_current_hash_rate; |
| 149 |
epee::critical_section m_last_hash_rates_lock; |
| 150 |
std::list<uint64_t> m_last_hash_rates; |
| 151 |
bool m_do_print_hashrate; |
| 152 |
bool m_do_mining; |
| 153 |
std::vector<std::pair<uint64_t, uint64_t>> m_threads_autodetect; |
| 154 |
boost::thread::attributes m_attrs; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
bool set_is_background_mining_enabled(bool is_background_mining_enabled); |
| 159 |
void set_ignore_battery(bool ignore_battery); |
| 160 |
bool background_worker_thread(); |
| 161 |
std::atomic<bool> m_is_background_mining_enabled; |
| 162 |
bool m_ignore_battery; |
| 163 |
boost::mutex m_is_background_mining_enabled_mutex; |
| 164 |
boost::condition_variable m_is_background_mining_enabled_cond; |
| 165 |
std::atomic<bool> m_is_background_mining_started; |
| 166 |
boost::mutex m_is_background_mining_started_mutex; |
| 167 |
boost::condition_variable m_is_background_mining_started_cond; |
| 168 |
boost::thread m_background_mining_thread; |
| 169 |
uint64_t m_min_idle_seconds; |
| 170 |
uint8_t m_idle_threshold; |
| 171 |
uint8_t m_mining_target; |
| 172 |
std::atomic<uint64_t> m_miner_extra_sleep; |
| 173 |
static bool get_system_times(uint64_t& total_time, uint64_t& idle_time); |
| 174 |
static bool get_process_time(uint64_t& total_time); |
| 175 |
static uint8_t get_percent_of_total(uint64_t some_time, uint64_t total_time); |
| 176 |
static boost::logic::tribool on_battery_power(); |
| 177 |
std::atomic<uint64_t> m_block_reward; |
| 178 |
}; |
| 179 |
} |