Monero
miner.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2020, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
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 
48  {
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:
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;
76  bool on_idle();
77  void on_synchronized();
78  //synchronous analog (for fast calls)
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);
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 
95  static constexpr uint8_t BACKGROUND_MINING_MAX_IDLE_THRESHOLD_PERCENTAGE = 99;
98  static constexpr uint16_t BACKGROUND_MINING_MAX_MIN_IDLE_INTERVAL_IN_SECONDS = 3600;
101  static constexpr uint8_t BACKGROUND_MINING_MAX_MINING_TARGET_PERCENTAGE = 100;
103  static constexpr uint64_t BACKGROUND_MINING_DEFAULT_MINER_EXTRA_SLEEP_MILLIS = 400; // ramp up
104 
105  private:
106  bool worker_thread();
107  bool request_block_template();
108  void merge_hr();
109  void update_autodetection();
110 
112  {
114 
115  BEGIN_KV_SERIALIZE_MAP()
116  KV_SERIALIZE(current_extra_message_index)
117  END_KV_SERIALIZE_MAP()
118  };
119 
120 
121  volatile uint32_t m_stop;
122  epee::critical_section m_template_lock;
124  std::atomic<uint32_t> m_template_no;
125  std::atomic<uint32_t> m_starter_nonce;
127  uint64_t m_height;
128  volatile 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;
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;
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;
153  std::vector<std::pair<uint64_t, uint64_t>> m_threads_autodetect;
154  boost::thread::attributes m_attrs;
155 
156  // background mining stuffs ..
157 
158  bool set_is_background_mining_enabled(bool is_background_mining_enabled);
159  void set_ignore_battery(bool ignore_battery);
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 }
cryptonote::miner::on_idle
bool on_idle()
Definition: miner.cpp:183
cryptonote::miner::m_is_background_mining_enabled_cond
boost::condition_variable m_is_background_mining_enabled_cond
Definition: miner.h:164
cryptonote::miner::m_attrs
boost::thread::attributes m_attrs
Definition: miner.h:154
cryptonote::miner::m_current_hash_rate
std::atomic< uint64_t > m_current_hash_rate
Definition: miner.h:148
cryptonote_basic.h
cryptonote::i_miner_handler::get_block_template
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
cryptonote::miner::update_autodetection
void update_autodetection()
Definition: miner.cpp:230
cryptonote::miner::m_template
block m_template
Definition: miner.h:123
cryptonote::miner::m_do_print_hashrate
bool m_do_print_hashrate
Definition: miner.h:151
cryptonote::miner::m_pausers_count
std::atomic< int32_t > m_pausers_count
Definition: miner.h:131
cryptonote::miner::BACKGROUND_MINING_DEFAULT_MIN_IDLE_INTERVAL_IN_SECONDS
static constexpr uint16_t BACKGROUND_MINING_DEFAULT_MIN_IDLE_INTERVAL_IN_SECONDS
Definition: miner.h:96
cryptonote::miner::get_idle_threshold
uint8_t get_idle_threshold() const
Definition: miner.cpp:646
cryptonote::miner::BACKGROUND_MINING_MINER_MONITOR_INVERVAL_IN_SECONDS
static constexpr uint8_t BACKGROUND_MINING_MINER_MONITOR_INVERVAL_IN_SECONDS
Definition: miner.h:102
cryptonote::miner::get_speed
uint64_t get_speed() const
Definition: miner.cpp:425
cryptonote::miner::get_process_time
static bool get_process_time(uint64_t &total_time)
Definition: miner.cpp:927
cryptonote::miner::m_mine_address
account_public_address m_mine_address
Definition: miner.h:138
cryptonote::miner::init
bool init(const boost::program_options::variables_map &vm, network_type nettype)
Definition: miner.cpp:296
cryptonote::miner::m_threads_lock
epee::critical_section m_threads_lock
Definition: miner.h:135
cryptonote::miner::BACKGROUND_MINING_DEFAULT_MINER_EXTRA_SLEEP_MILLIS
static constexpr uint64_t BACKGROUND_MINING_DEFAULT_MINER_EXTRA_SLEEP_MILLIS
Definition: miner.h:103
cryptonote::get_block_hash_t
std::function< bool(const cryptonote::block &, uint64_t, const crypto::hash *, unsigned int, crypto::hash &)> get_block_hash_t
Definition: miner.h:55
verification_context.h
cryptonote::miner::on_battery_power
static boost::logic::tribool on_battery_power()
Definition: miner.cpp:963
cryptonote::miner::m_hashes
std::atomic< uint64_t > m_hashes
Definition: miner.h:146
cryptonote::miner::m_is_background_mining_enabled_mutex
boost::mutex m_is_background_mining_enabled_mutex
Definition: miner.h:163
cryptonote::miner::stop
bool stop()
Definition: miner.cpp:441
cryptonote::i_miner_handler
Definition: miner.h:48
cryptonote::miner::m_phandler
i_miner_handler * m_phandler
Definition: miner.h:136
cryptonote::miner::get_threads_count
uint32_t get_threads_count() const
Definition: miner.cpp:365
cryptonote::miner::resume
void resume()
Definition: miner.cpp:510
cryptonote::network_type
network_type
Definition: cryptonote_config.h:268
cryptonote::difficulty_type
boost::multiprecision::uint128_t difficulty_type
Definition: difficulty.h:41
cryptonote::block_verification_context
Definition: verification_context.h:59
boost
Definition: unordered_containers_boost_serialization.h:38
cryptonote::miner::BACKGROUND_MINING_MAX_MINING_TARGET_PERCENTAGE
static constexpr uint8_t BACKGROUND_MINING_MAX_MINING_TARGET_PERCENTAGE
Definition: miner.h:101
cryptonote::miner::m_extra_messages
std::vector< blobdata > m_extra_messages
Definition: miner.h:142
cryptonote::miner::m_threads_autodetect
std::vector< std::pair< uint64_t, uint64_t > > m_threads_autodetect
Definition: miner.h:153
cryptonote::miner::worker_thread
bool worker_thread()
Definition: miner.cpp:524
cryptonote::miner::BACKGROUND_MINING_MIN_IDLE_THRESHOLD_PERCENTAGE
static constexpr uint8_t BACKGROUND_MINING_MIN_IDLE_THRESHOLD_PERCENTAGE
Definition: miner.h:94
cryptonote::miner::m_ignore_battery
bool m_ignore_battery
Definition: miner.h:162
cryptonote::miner::m_autodetect_interval
epee::math_helper::once_a_time_seconds< 1 > m_autodetect_interval
Definition: miner.h:141
cryptonote::miner::m_last_hash_rates
std::list< uint64_t > m_last_hash_rates
Definition: miner.h:150
cryptonote::miner::start
bool start(const account_public_address &adr, size_t threads_count, bool do_background=false, bool ignore_battery=false)
Definition: miner.cpp:369
cryptonote::miner::m_starter_nonce
std::atomic< uint32_t > m_starter_nonce
Definition: miner.h:125
cryptonote::miner::BACKGROUND_MINING_MIN_MINING_TARGET_PERCENTAGE
static constexpr uint8_t BACKGROUND_MINING_MIN_MINING_TARGET_PERCENTAGE
Definition: miner.h:100
cryptonote::miner::m_last_hr_merge_time
std::atomic< uint64_t > m_last_hr_merge_time
Definition: miner.h:145
cryptonote::miner::m_update_block_template_interval
epee::math_helper::once_a_time_seconds< 5 > m_update_block_template_interval
Definition: miner.h:139
cryptonote::miner::m_template_no
std::atomic< uint32_t > m_template_no
Definition: miner.h:124
cryptonote::miner::m_stop
volatile uint32_t m_stop
Definition: miner.h:121
base.b
int b
Definition: base.py:1
cryptonote::miner::m_update_merge_hr_interval
epee::math_helper::once_a_time_seconds< 2 > m_update_merge_hr_interval
Definition: miner.h:140
cryptonote::miner::get_min_idle_seconds
uint64_t get_min_idle_seconds() const
Definition: miner.cpp:633
cryptonote::miner::m_config
miner_config m_config
Definition: miner.h:143
cryptonote::miner::set_is_background_mining_enabled
bool set_is_background_mining_enabled(bool is_background_mining_enabled)
Definition: miner.cpp:619
cryptonote::i_miner_handler::handle_block_found
virtual bool handle_block_found(block &b, block_verification_context &bvc)=0
cryptonote::miner::set_idle_threshold
bool set_idle_threshold(uint8_t idle_threshold)
Definition: miner.cpp:651
cryptonote::miner::m_threads_total
volatile uint32_t m_threads_total
Definition: miner.h:129
cryptonote::miner::get_system_times
static bool get_system_times(uint64_t &total_time, uint64_t &idle_time)
Definition: miner.cpp:827
cryptonote::miner::BACKGROUND_MINING_MAX_IDLE_THRESHOLD_PERCENTAGE
static constexpr uint8_t BACKGROUND_MINING_MAX_IDLE_THRESHOLD_PERCENTAGE
Definition: miner.h:95
cryptonote::miner::m_do_mining
bool m_do_mining
Definition: miner.h:152
cryptonote::block
Definition: cryptonote_basic.h:464
cryptonote::miner::on_block_chain_update
bool on_block_chain_update()
Definition: miner.cpp:151
cryptonote::miner::m_gbh
get_block_hash_t m_gbh
Definition: miner.h:137
cryptonote::miner::m_height
uint64_t m_height
Definition: miner.h:127
cryptonote::miner::m_min_idle_seconds
uint64_t m_min_idle_seconds
Definition: miner.h:169
cryptonote::miner::m_mining_target
uint8_t m_mining_target
Definition: miner.h:171
cryptonote::miner::m_config_folder_path
std::string m_config_folder_path
Definition: miner.h:144
cryptonote::miner::BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE
static constexpr uint8_t BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE
Definition: miner.h:93
cryptonote::miner::miner
miner(i_miner_handler *phandler, const get_block_hash_t &gbh)
Definition: miner.cpp:105
cryptonote
Holds cryptonote related classes and helpers.
Definition: blockchain_db.cpp:45
cryptonote::miner::m_last_hash_rates_lock
epee::critical_section m_last_hash_rates_lock
Definition: miner.h:149
cryptonote::miner::get_is_background_mining_enabled
bool get_is_background_mining_enabled() const
Definition: miner.cpp:605
cryptonote::miner::m_is_background_mining_started_cond
boost::condition_variable m_is_background_mining_started_cond
Definition: miner.h:167
cryptonote::miner::find_nonce_for_given_block
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)
Definition: miner.cpp:476
cryptonote::miner::m_total_hashes
std::atomic< uint64_t > m_total_hashes
Definition: miner.h:147
cryptonote::miner::is_mining
bool is_mining() const
Definition: miner.cpp:355
cryptonote::miner::m_diffic
difficulty_type m_diffic
Definition: miner.h:126
cryptonote::miner::get_percent_of_total
static uint8_t get_percent_of_total(uint64_t some_time, uint64_t total_time)
Definition: miner.cpp:958
cryptonote::miner::miner_config::current_extra_message_index
uint64_t current_extra_message_index
Definition: miner.h:113
cryptonote::miner::set_mining_target
bool set_mining_target(uint8_t mining_target)
Definition: miner.cpp:664
cryptonote::miner::merge_hr
void merge_hr()
Definition: miner.cpp:208
cryptonote::miner::m_thread_index
volatile uint32_t m_thread_index
Definition: miner.h:128
cryptonote::miner::BACKGROUND_MINING_MIN_MIN_IDLE_INTERVAL_IN_SECONDS
static constexpr uint16_t BACKGROUND_MINING_MIN_MIN_IDLE_INTERVAL_IN_SECONDS
Definition: miner.h:97
cryptonote::miner::get_mining_address
const account_public_address & get_mining_address() const
Definition: miner.cpp:360
cryptonote::i_miner_handler::~i_miner_handler
~i_miner_handler()
Definition: miner.h:52
difficulty.h
cryptonote::miner::background_worker_thread
bool background_worker_thread()
Definition: miner.cpp:672
cryptonote::blobdata
std::string blobdata
Definition: blobdatatype.h:39
bool
int bool
Definition: stdbool.h:35
cryptonote::miner::set_ignore_battery
void set_ignore_battery(bool ignore_battery)
Definition: miner.cpp:628
cryptonote::miner::~miner
~miner()
Definition: miner.cpp:133
cryptonote::miner::BACKGROUND_MINING_MAX_MIN_IDLE_INTERVAL_IN_SECONDS
static constexpr uint16_t BACKGROUND_MINING_MAX_MIN_IDLE_INTERVAL_IN_SECONDS
Definition: miner.h:98
cryptonote::miner::m_background_mining_thread
boost::thread m_background_mining_thread
Definition: miner.h:168
std
Definition: blockchain_ancestry.cpp:72
cryptonote::account_public_address
Definition: cryptonote_basic.h:501
cryptonote::miner::m_is_background_mining_started_mutex
boost::mutex m_is_background_mining_started_mutex
Definition: miner.h:166
cryptonote::miner::do_print_hashrate
void do_print_hashrate(bool do_hr)
Definition: miner.cpp:203
cryptonote::miner::get_ignore_battery
bool get_ignore_battery() const
Definition: miner.cpp:610
cryptonote::miner::m_block_reward
std::atomic< uint64_t > m_block_reward
Definition: miner.h:177
cryptonote::miner::miner_config
Definition: miner.h:112
cryptonote::miner::m_is_background_mining_started
std::atomic< bool > m_is_background_mining_started
Definition: miner.h:165
cryptonote::miner::m_threads_active
std::atomic< uint32_t > m_threads_active
Definition: miner.h:130
cryptonote::miner::m_miner_extra_sleep
std::atomic< uint64_t > m_miner_extra_sleep
Definition: miner.h:172
cryptonote::miner::m_is_background_mining_enabled
std::atomic< bool > m_is_background_mining_enabled
Definition: miner.h:161
cryptonote::miner::on_synchronized
void on_synchronized()
Definition: miner.cpp:493
cryptonote::miner::m_miners_count_lock
epee::critical_section m_miners_count_lock
Definition: miner.h:132
cryptonote::miner::m_threads
std::list< boost::thread > m_threads
Definition: miner.h:134
cryptonote::miner::init_options
static void init_options(boost::program_options::options_description &desc)
Definition: miner.cpp:284
epee
Definition: cryptonote_format_utils.h:44
cryptonote::miner::set_min_idle_seconds
bool set_min_idle_seconds(uint64_t min_idle_seconds)
Definition: miner.cpp:638
cryptonote::miner::set_block_template
bool set_block_template(const block &bl, const difficulty_type &diffic, uint64_t height, uint64_t block_reward)
Definition: miner.cpp:139
cryptonote::miner::send_stop_signal
void send_stop_signal()
Definition: miner.cpp:435
cryptonote::miner
Definition: miner.h:61
cryptonote::miner::m_idle_threshold
uint8_t m_idle_threshold
Definition: miner.h:170
list
c list(APPEND GENERATED_SOURCES ${OUTPUT_C_SOURCE}) set(INPUT_DAT_FILE "$
Definition: CMakeLists.txt:33
cryptonote::miner::get_mining_target
uint8_t get_mining_target() const
Definition: miner.cpp:659
cryptonote::miner::pause
void pause()
Definition: miner.cpp:501
cryptonote::miner::request_block_template
bool request_block_template()
Definition: miner.cpp:159
cryptonote::miner::m_template_lock
epee::critical_section m_template_lock
Definition: miner.h:122
crypto::hash
POD_CLASS hash
Definition: hash.h:48
cryptonote::miner::BACKGROUND_MINING_DEFAULT_MINING_TARGET_PERCENTAGE
static constexpr uint8_t BACKGROUND_MINING_DEFAULT_MINING_TARGET_PERCENTAGE
Definition: miner.h:99
cryptonote::miner::get_block_reward
uint64_t get_block_reward() const
Definition: miner.h:91