Electroneum
Loading...
Searching...
No Matches
miner.h
Go to the documentation of this file.
1// Copyrights(c) 2017-2021, The Electroneum Project
2// Copyrights(c) 2014-2019, The Monero Project
3//
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without modification, are
7// permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this list of
10// conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13// of conditions and the following disclaimer in the documentation and/or other
14// materials provided with the distribution.
15//
16// 3. Neither the name of the copyright holder nor the names of its contributors may be
17// used to endorse or promote products derived from this software without specific
18// prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31
32#pragma once
33
34#include <boost/program_options.hpp>
35#include <boost/logic/tribool_fwd.hpp>
36#include <atomic>
37#include "cryptonote_basic.h"
39#include "difficulty.h"
40#include "math_helper.h"
41#ifdef _WIN32
42#include <windows.h>
43#endif
44
45namespace cryptonote
46{
47
49 {
51 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) = 0;
52 protected:
54 };
55
56 /************************************************************************/
57 /* */
58 /************************************************************************/
59 class miner
60 {
61 public:
62 miner(i_miner_handler* phandler);
63 ~miner();
64 bool init(const boost::program_options::variables_map& vm, network_type nettype, bool fallback_to_pow = false);
65 static void init_options(boost::program_options::options_description& desc);
66 bool set_block_template(const block& bl, const difficulty_type& diffic, uint64_t height, uint64_t block_reward);
68 bool start(const account_public_address& adr, size_t threads_count, bool do_background = false, bool ignore_battery = false);
69 uint64_t get_speed() const;
71 void send_stop_signal();
72 bool stop();
73 bool is_mining() const;
75 bool on_idle();
76 void on_synchronized();
77 //synchronous analog (for fast calls)
78 static bool find_nonce_for_given_block(block& bl, const difficulty_type& diffic, uint64_t height);
79 void pause();
80 void resume();
81 void do_print_hashrate(bool do_hr);
83 bool get_ignore_battery() const;
85 bool set_min_idle_seconds(uint64_t min_idle_seconds);
87 bool set_idle_threshold(uint8_t idle_threshold);
89 bool set_mining_target(uint8_t mining_target);
90 uint64_t get_block_reward() const { return m_block_reward; }
91
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
116 KV_SERIALIZE(current_extra_message_index)
118 };
119
120
121 volatile uint32_t 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 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;
136 i_miner_handler* m_phandler;
137 account_public_address m_mine_address;
138 epee::math_helper::once_a_time_seconds<5> m_update_block_template_interval;
139 epee::math_helper::once_a_time_seconds<2> m_update_merge_hr_interval;
140 epee::math_helper::once_a_time_seconds<1> m_autodetect_interval;
141 std::vector<blobdata> m_extra_messages;
142 miner_config m_config;
143 std::string m_config_folder_path;
144 std::atomic<uint64_t> m_last_hr_merge_time;
145 std::atomic<uint64_t> m_hashes;
146 std::atomic<uint64_t> m_total_hashes;
147 std::atomic<uint64_t> m_current_hash_rate;
148 epee::critical_section m_last_hash_rates_lock;
149 std::list<uint64_t> m_last_hash_rates;
150 bool m_do_print_hashrate;
151 bool m_do_mining;
152 std::vector<std::pair<uint64_t, uint64_t>> m_threads_autodetect;
153 boost::thread::attributes m_attrs;
154
155 // background mining stuffs ..
156
157 bool set_is_background_mining_enabled(bool is_background_mining_enabled);
158 void set_ignore_battery(bool ignore_battery);
159 bool background_worker_thread();
160 std::atomic<bool> m_is_background_mining_enabled;
161 bool m_ignore_battery;
162 boost::mutex m_is_background_mining_enabled_mutex;
163 boost::condition_variable m_is_background_mining_enabled_cond;
164 std::atomic<bool> m_is_background_mining_started;
165 boost::mutex m_is_background_mining_started_mutex;
166 boost::condition_variable m_is_background_mining_started_cond;
167 boost::thread m_background_mining_thread;
168 uint64_t m_min_idle_seconds;
169 uint8_t m_idle_threshold;
170 uint8_t m_mining_target;
171 std::atomic<uint64_t> m_miner_extra_sleep;
172 static bool get_system_times(uint64_t& total_time, uint64_t& idle_time);
173 static bool get_process_time(uint64_t& total_time);
174 static uint8_t get_percent_of_total(uint64_t some_time, uint64_t total_time);
175 static boost::logic::tribool on_battery_power();
176 std::atomic<uint64_t> m_block_reward;
177 bool m_fallback_to_pow;
178 };
179}
uint64_t height
const account_public_address & get_mining_address() const
Definition miner.cpp:364
static constexpr uint8_t BACKGROUND_MINING_MAX_IDLE_THRESHOLD_PERCENTAGE
Definition miner.h:94
static constexpr uint8_t BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE
Definition miner.h:92
static constexpr uint16_t BACKGROUND_MINING_DEFAULT_MIN_IDLE_INTERVAL_IN_SECONDS
Definition miner.h:95
void on_synchronized()
Definition miner.cpp:495
static constexpr uint64_t BACKGROUND_MINING_MIN_MINER_EXTRA_SLEEP_MILLIS
Definition miner.h:103
static constexpr uint8_t BACKGROUND_MINING_MIN_IDLE_THRESHOLD_PERCENTAGE
Definition miner.h:93
bool is_mining() const
Definition miner.cpp:359
uint64_t get_block_reward() const
Definition miner.h:90
static constexpr uint8_t BACKGROUND_MINING_DEFAULT_MINING_TARGET_PERCENTAGE
Definition miner.h:98
uint64_t get_min_idle_seconds() const
Definition miner.cpp:639
static constexpr uint16_t BACKGROUND_MINING_MIN_MIN_IDLE_INTERVAL_IN_SECONDS
Definition miner.h:96
static constexpr uint8_t BACKGROUND_MINING_MINER_MONITOR_INVERVAL_IN_SECONDS
Definition miner.h:101
uint64_t get_speed() const
Definition miner.cpp:429
static void init_options(boost::program_options::options_description &desc)
Definition miner.cpp:280
bool set_block_template(const block &bl, const difficulty_type &diffic, uint64_t height, uint64_t block_reward)
Definition miner.cpp:137
uint32_t get_threads_count() const
Definition miner.cpp:369
static constexpr uint8_t BACKGROUND_MINING_MAX_MINING_TARGET_PERCENTAGE
Definition miner.h:100
void send_stop_signal()
Definition miner.cpp:439
miner(i_miner_handler *phandler)
Definition miner.cpp:104
bool get_ignore_battery() const
Definition miner.cpp:616
bool set_min_idle_seconds(uint64_t min_idle_seconds)
Definition miner.cpp:644
bool on_block_chain_update()
Definition miner.cpp:149
bool get_is_background_mining_enabled() const
Definition miner.cpp:611
bool init(const boost::program_options::variables_map &vm, network_type nettype, bool fallback_to_pow=false)
Definition miner.cpp:292
static bool find_nonce_for_given_block(block &bl, const difficulty_type &diffic, uint64_t height)
Definition miner.cpp:478
static constexpr uint16_t BACKGROUND_MINING_MAX_MIN_IDLE_INTERVAL_IN_SECONDS
Definition miner.h:97
uint8_t get_idle_threshold() const
Definition miner.cpp:652
bool start(const account_public_address &adr, size_t threads_count, bool do_background=false, bool ignore_battery=false)
Definition miner.cpp:373
void do_print_hashrate(bool do_hr)
Definition miner.cpp:199
static constexpr uint64_t BACKGROUND_MINING_DEFAULT_MINER_EXTRA_SLEEP_MILLIS
Definition miner.h:102
uint8_t get_mining_target() const
Definition miner.cpp:665
bool set_idle_threshold(uint8_t idle_threshold)
Definition miner.cpp:657
bool set_mining_target(uint8_t mining_target)
Definition miner.cpp:670
static constexpr uint8_t BACKGROUND_MINING_MIN_MINING_TARGET_PERCENTAGE
Definition miner.h:99
#define KV_SERIALIZE(varialble)
#define END_KV_SERIALIZE_MAP()
#define BEGIN_KV_SERIALIZE_MAP()
Holds cryptonote related classes and helpers.
Definition ban.cpp:40
boost::multiprecision::uint128_t difficulty_type
Definition difficulty.h:43
std::string blobdata
STL namespace.
unsigned short uint16_t
Definition stdint.h:125
unsigned int uint32_t
Definition stdint.h:126
signed int int32_t
Definition stdint.h:123
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
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)=0
virtual bool handle_block_found(block &b, block_verification_context &bvc)=0