Electroneum
miner.cpp
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 #include <sstream>
33 #include <numeric>
34 #include <boost/utility/value_init.hpp>
35 #include <boost/interprocess/detail/atomic.hpp>
36 #include <boost/algorithm/string.hpp>
37 #include "misc_language.h"
38 #include "syncobj.h"
39 #include "cryptonote_basic_impl.h"
41 #include "file_io_utils.h"
42 #include "common/command_line.h"
43 #include "string_coding.h"
44 #include "string_tools.h"
46 #include "boost/logic/tribool.hpp"
47 
48 #ifdef __APPLE__
49  #include <sys/times.h>
50  #include <IOKit/IOKitLib.h>
51  #include <IOKit/ps/IOPSKeys.h>
52  #include <IOKit/ps/IOPowerSources.h>
53  #include <mach/mach_host.h>
54  #include <AvailabilityMacros.h>
55  #include <TargetConditionals.h>
56 #elif defined(__linux__)
57  #include <unistd.h>
58  #include <sys/resource.h>
59  #include <sys/times.h>
60  #include <time.h>
61 #elif defined(__FreeBSD__)
62  #include <devstat.h>
63  #include <errno.h>
64  #include <fcntl.h>
65  #include <machine/apm_bios.h>
66  #include <stdio.h>
67  #include <sys/resource.h>
68  #include <sys/sysctl.h>
69  #include <sys/times.h>
70  #include <sys/types.h>
71  #include <unistd.h>
72 #endif
73 
74 #undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
75 #define ELECTRONEUM_DEFAULT_LOG_CATEGORY "miner"
76 
77 #define AUTODETECT_WINDOW 10 // seconds
78 #define AUTODETECT_GAIN_THRESHOLD 1.02f // 2%
79 
80 using namespace epee;
81 
82 #include "miner.h"
83 
84 
85 extern "C" void slow_hash_allocate_state();
86 extern "C" void slow_hash_free_state();
87 namespace cryptonote
88 {
89 
90  namespace
91  {
92  const command_line::arg_descriptor<std::string> arg_extra_messages = {"extra-messages-file", "Specify file for extra messages to include into coinbase transactions", "", true};
93  const command_line::arg_descriptor<std::string> arg_start_mining = {"start-mining", "Specify wallet address to mining for", "", true};
94  const command_line::arg_descriptor<uint32_t> arg_mining_threads = {"mining-threads", "Specify mining threads count", 0, true};
95  const command_line::arg_descriptor<bool> arg_bg_mining_enable = {"bg-mining-enable", "enable/disable background mining", true, true};
96  const command_line::arg_descriptor<bool> arg_bg_mining_ignore_battery = {"bg-mining-ignore-battery", "if true, assumes plugged in when unable to query system power status", false, true};
97  const command_line::arg_descriptor<uint64_t> arg_bg_mining_min_idle_interval_seconds = {"bg-mining-min-idle-interval", "Specify min lookback interval in seconds for determining idle state", miner::BACKGROUND_MINING_DEFAULT_MIN_IDLE_INTERVAL_IN_SECONDS, true};
98  const command_line::arg_descriptor<uint16_t> arg_bg_mining_idle_threshold_percentage = {"bg-mining-idle-threshold", "Specify minimum avg idle percentage over lookback interval", miner::BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE, true};
99  const command_line::arg_descriptor<uint16_t> arg_bg_mining_miner_target_percentage = {"bg-mining-miner-target", "Specify maximum percentage cpu use by miner(s)", miner::BACKGROUND_MINING_DEFAULT_MINING_TARGET_PERCENTAGE, true};
100  }
101 
102 
103  miner::miner(i_miner_handler* phandler):m_stop(1),
104  m_template(boost::value_initialized<block>()),
105  m_template_no(0),
106  m_diffic(0),
107  m_thread_index(0),
108  m_phandler(phandler),
109  m_height(0),
110  m_threads_active(0),
111  m_pausers_count(0),
112  m_threads_total(0),
113  m_starter_nonce(0),
114  m_last_hr_merge_time(0),
115  m_hashes(0),
116  m_total_hashes(0),
117  m_do_print_hashrate(false),
118  m_do_mining(false),
119  m_current_hash_rate(0),
120  m_is_background_mining_enabled(false),
121  m_min_idle_seconds(BACKGROUND_MINING_DEFAULT_MIN_IDLE_INTERVAL_IN_SECONDS),
122  m_idle_threshold(BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE),
123  m_mining_target(BACKGROUND_MINING_DEFAULT_MINING_TARGET_PERCENTAGE),
124  m_miner_extra_sleep(BACKGROUND_MINING_DEFAULT_MINER_EXTRA_SLEEP_MILLIS),
125  m_block_reward(0)
126  {
127  m_attrs.set_stack_size(THREAD_STACK_SIZE);
128  }
129  //-----------------------------------------------------------------------------------------------------
131  {
132  try { stop(); }
133  catch (...) { /* ignore */ }
134  }
135  //-----------------------------------------------------------------------------------------------------
136  bool miner::set_block_template(const block& bl, const difficulty_type& di, uint64_t height, uint64_t block_reward)
137  {
138  CRITICAL_REGION_LOCAL(m_template_lock);
139  m_template = bl;
140  m_diffic = di;
141  m_height = height;
142  m_block_reward = block_reward;
143  ++m_template_no;
144  m_starter_nonce = crypto::rand<uint32_t>();
145  return true;
146  }
147  //-----------------------------------------------------------------------------------------------------
149  {
150  if(!is_mining())
151  return true;
152 
153  return request_block_template();
154  }
155  //-----------------------------------------------------------------------------------------------------
156  bool miner::request_block_template()
157  {
158  block bl;
161  uint64_t expected_reward; //only used for RPC calls - could possibly be useful here too?
162 
163  cryptonote::blobdata extra_nonce;
164  if(m_extra_messages.size() && m_config.current_extra_message_index < m_extra_messages.size())
165  {
166  extra_nonce = m_extra_messages[m_config.current_extra_message_index];
167  }
168 
169  if(!m_phandler->get_block_template(bl, m_mine_address, di, height, expected_reward, extra_nonce))
170  {
171  LOG_ERROR("Failed to get_block_template(), stopping mining");
172  return false;
173  }
174  set_block_template(bl, di, height, expected_reward);
175  return true;
176  }
177  //-----------------------------------------------------------------------------------------------------
179  {
180  m_update_block_template_interval.do_call([&](){
181  if(is_mining())request_block_template();
182  return true;
183  });
184 
185  m_update_merge_hr_interval.do_call([&](){
186  merge_hr();
187  return true;
188  });
189 
190  m_autodetect_interval.do_call([&](){
191  update_autodetection();
192  return true;
193  });
194 
195  return true;
196  }
197  //-----------------------------------------------------------------------------------------------------
198  void miner::do_print_hashrate(bool do_hr)
199  {
200  m_do_print_hashrate = do_hr;
201  }
202  //-----------------------------------------------------------------------------------------------------
203  void miner::merge_hr()
204  {
205  if(m_last_hr_merge_time && is_mining())
206  {
207  m_current_hash_rate = m_hashes * 1000 / ((misc_utils::get_tick_count() - m_last_hr_merge_time + 1));
208  CRITICAL_REGION_LOCAL(m_last_hash_rates_lock);
209  m_last_hash_rates.push_back(m_current_hash_rate);
210  if(m_last_hash_rates.size() > 19)
211  m_last_hash_rates.pop_front();
212  if(m_do_print_hashrate)
213  {
214  uint64_t total_hr = std::accumulate(m_last_hash_rates.begin(), m_last_hash_rates.end(), 0);
215  float hr = static_cast<float>(total_hr)/static_cast<float>(m_last_hash_rates.size());
216  const auto flags = std::cout.flags();
217  const auto precision = std::cout.precision();
218  std::cout << "hashrate: " << std::setprecision(4) << std::fixed << hr << std::setiosflags(flags) << std::setprecision(precision) << ENDL;
219  }
220  }
221  m_last_hr_merge_time = misc_utils::get_tick_count();
222  m_hashes = 0;
223  }
224  //-----------------------------------------------------------------------------------------------------
225  void miner::update_autodetection()
226  {
227  if (m_threads_autodetect.empty())
228  return;
229 
231  uint64_t dt = now - m_threads_autodetect.back().first;
232  if (dt < AUTODETECT_WINDOW * 1000000000ull)
233  return;
234 
235  // work out how many more hashes we got
236  m_threads_autodetect.back().first = dt;
237  uint64_t dh = m_total_hashes - m_threads_autodetect.back().second;
238  m_threads_autodetect.back().second = dh;
239  float hs = dh / (dt / (float)1000000000);
240  MGINFO("Mining autodetection: " << m_threads_autodetect.size() << " threads: " << hs << " H/s");
241 
242  // when we don't increase by at least 2%, stop, otherwise check next
243  // if N and N+1 have mostly the same hash rate, we want to "lighter" one
244  bool found = false;
245  if (m_threads_autodetect.size() > 1)
246  {
247  int previdx = m_threads_autodetect.size() - 2;
248  float previous_hs = m_threads_autodetect[previdx].second / (m_threads_autodetect[previdx].first / (float)1000000000);
249  if (previous_hs > 0 && hs / previous_hs < AUTODETECT_GAIN_THRESHOLD)
250  {
251  m_threads_total = m_threads_autodetect.size() - 1;
252  m_threads_autodetect.clear();
253  MGINFO("Optimal number of threads seems to be " << m_threads_total);
254  found = true;
255  }
256  }
257 
258  if (!found)
259  {
260  // setup one more thread
261  m_threads_autodetect.push_back({now, m_total_hashes});
262  m_threads_total = m_threads_autodetect.size();
263  }
264 
265  // restart all threads
266  {
267  CRITICAL_REGION_LOCAL(m_threads_lock);
268  boost::interprocess::ipcdetail::atomic_write32(&m_stop, 1);
269  while (m_threads_active > 0)
271  m_threads.clear();
272  }
273  boost::interprocess::ipcdetail::atomic_write32(&m_stop, 0);
274  boost::interprocess::ipcdetail::atomic_write32(&m_thread_index, 0);
275  for(size_t i = 0; i != m_threads_total; i++)
276  m_threads.push_back(boost::thread(m_attrs, boost::bind(&miner::worker_thread, this)));
277  }
278  //-----------------------------------------------------------------------------------------------------
279  void miner::init_options(boost::program_options::options_description& desc)
280  {
281  command_line::add_arg(desc, arg_extra_messages);
282  command_line::add_arg(desc, arg_start_mining);
283  command_line::add_arg(desc, arg_mining_threads);
284  command_line::add_arg(desc, arg_bg_mining_enable);
285  command_line::add_arg(desc, arg_bg_mining_ignore_battery);
286  command_line::add_arg(desc, arg_bg_mining_min_idle_interval_seconds);
287  command_line::add_arg(desc, arg_bg_mining_idle_threshold_percentage);
288  command_line::add_arg(desc, arg_bg_mining_miner_target_percentage);
289  }
290  //-----------------------------------------------------------------------------------------------------
291  bool miner::init(const boost::program_options::variables_map& vm, network_type nettype, bool fallback_to_pow)
292  {
293  if(command_line::has_arg(vm, arg_extra_messages))
294  {
295  std::string buff;
296  bool r = file_io_utils::load_file_to_string(command_line::get_arg(vm, arg_extra_messages), buff);
297  CHECK_AND_ASSERT_MES(r, false, "Failed to load file with extra messages: " << command_line::get_arg(vm, arg_extra_messages));
298  std::vector<std::string> extra_vec;
299  boost::split(extra_vec, buff, boost::is_any_of("\n"), boost::token_compress_on );
300  m_extra_messages.resize(extra_vec.size());
301  for(size_t i = 0; i != extra_vec.size(); i++)
302  {
303  string_tools::trim(extra_vec[i]);
304  if(!extra_vec[i].size())
305  continue;
306  std::string buff = string_encoding::base64_decode(extra_vec[i]);
307  if(buff != "0")
308  m_extra_messages[i] = buff;
309  }
310  m_config_folder_path = boost::filesystem::path(command_line::get_arg(vm, arg_extra_messages)).parent_path().string();
311  m_config = AUTO_VAL_INIT(m_config);
312  const std::string filename = m_config_folder_path + "/" + MINER_CONFIG_FILE_NAME;
313  CHECK_AND_ASSERT_MES(epee::serialization::load_t_from_json_file(m_config, filename), false, "Failed to load data from " << filename);
314  MINFO("Loaded " << m_extra_messages.size() << " extra messages, current index " << m_config.current_extra_message_index);
315  }
316 
317  if(command_line::has_arg(vm, arg_start_mining))
318  {
320  if(!cryptonote::get_account_address_from_str(info, nettype, command_line::get_arg(vm, arg_start_mining)) || info.is_subaddress)
321  {
322  LOG_ERROR("Target account address " << command_line::get_arg(vm, arg_start_mining) << " has wrong format, starting daemon canceled");
323  return false;
324  }
325  // just a safeguard for the legacy infrastructure
326  if(!(command_line::get_arg(vm, arg_start_mining) == "etnkCys4uGhSi9h48ajL9vBDJTcn2s2ttXtXq3SXWPAbiMHNhHitu5fJ8QgRfFWTzmJ8QgRfFWTzmJ8QgRfFWTzm4t51HTfCtK"))
327  {
328  LOG_ERROR("Target account address " << command_line::get_arg(vm, arg_start_mining) << " isn't equal to the Aurelius legacy mining burn address etnkCys4uGhSi9h48ajL9vBDJTcn2s2ttXtXq3SXWPAbiMHNhHitu5fJ8QgRfFWTzmJ8QgRfFWTzmJ8QgRfFWTzm4t51HTfCtK");
329  return false;
330  }
331  m_mine_address = info.address;
332  m_threads_total = 1;
333  m_do_mining = true;
334  if(command_line::has_arg(vm, arg_mining_threads))
335  {
336  m_threads_total = command_line::get_arg(vm, arg_mining_threads);
337  }
338  }
339 
340  // Background mining parameters
341  // Let init set all parameters even if background mining is not enabled, they can start later with params set
342  if(command_line::has_arg(vm, arg_bg_mining_enable))
343  set_is_background_mining_enabled( command_line::get_arg(vm, arg_bg_mining_enable) );
344  if(command_line::has_arg(vm, arg_bg_mining_ignore_battery))
345  set_ignore_battery( command_line::get_arg(vm, arg_bg_mining_ignore_battery) );
346  if(command_line::has_arg(vm, arg_bg_mining_min_idle_interval_seconds))
347  set_min_idle_seconds( command_line::get_arg(vm, arg_bg_mining_min_idle_interval_seconds) );
348  if(command_line::has_arg(vm, arg_bg_mining_idle_threshold_percentage))
349  set_idle_threshold( command_line::get_arg(vm, arg_bg_mining_idle_threshold_percentage) );
350  if(command_line::has_arg(vm, arg_bg_mining_miner_target_percentage))
351  set_mining_target( command_line::get_arg(vm, arg_bg_mining_miner_target_percentage) );
352 
353  m_fallback_to_pow = fallback_to_pow;
354 
355  return true;
356  }
357  //-----------------------------------------------------------------------------------------------------
358  bool miner::is_mining() const
359  {
360  return !m_stop;
361  }
362  //-----------------------------------------------------------------------------------------------------
364  {
365  return m_mine_address;
366  }
367  //-----------------------------------------------------------------------------------------------------
369  return m_threads_total;
370  }
371  //-----------------------------------------------------------------------------------------------------
372  bool miner::start(const account_public_address& adr, size_t threads_count, bool do_background, bool ignore_battery)
373  {
374  m_block_reward = 0;
375  m_mine_address = adr;
376  m_threads_total = static_cast<uint32_t>(threads_count);
377  if (threads_count == 0)
378  {
379  m_threads_autodetect.clear();
380  m_threads_autodetect.push_back({epee::misc_utils::get_ns_count(), m_total_hashes});
381  m_threads_total = 1;
382  }
383  m_starter_nonce = crypto::rand<uint32_t>();
384  CRITICAL_REGION_LOCAL(m_threads_lock);
385  if(is_mining())
386  {
387  LOG_ERROR("Starting miner but it's already started");
388  return false;
389  }
390 
391  if(!m_threads.empty())
392  {
393  LOG_ERROR("Unable to start miner because there are active mining threads");
394  return false;
395  }
396 
397  request_block_template();//lets update block template
398 
399  boost::interprocess::ipcdetail::atomic_write32(&m_stop, 0);
400  boost::interprocess::ipcdetail::atomic_write32(&m_thread_index, 0);
401  set_is_background_mining_enabled(do_background);
402  set_ignore_battery(ignore_battery);
403 
404  for(size_t i = 0; i != m_threads_total; i++)
405  {
406  m_threads.push_back(boost::thread(m_attrs, boost::bind(&miner::worker_thread, this)));
407  }
408 
409  if (threads_count == 0)
410  MINFO("Mining has started, autodetecting optimal number of threads, good luck!" );
411  else
412  MINFO("Mining has started with " << threads_count << " threads, good luck!" );
413 
415  {
416  m_background_mining_thread = boost::thread(m_attrs, boost::bind(&miner::background_worker_thread, this));
417  LOG_PRINT_L0("Background mining controller thread started" );
418  }
419 
420  if(get_ignore_battery())
421  {
422  MINFO("Ignoring battery");
423  }
424 
425  return true;
426  }
427  //-----------------------------------------------------------------------------------------------------
429  {
430  if(is_mining()) {
431  return m_current_hash_rate;
432  }
433  else {
434  return 0;
435  }
436  }
437  //-----------------------------------------------------------------------------------------------------
439  {
440  boost::interprocess::ipcdetail::atomic_write32(&m_stop, 1);
441  }
442  //-----------------------------------------------------------------------------------------------------
443  bool miner::stop()
444  {
445  MTRACE("Miner has received stop signal");
446 
447  CRITICAL_REGION_LOCAL(m_threads_lock);
448  bool mining = !m_threads.empty();
449  if (!mining)
450  {
451  MTRACE("Not mining - nothing to stop" );
452  return true;
453  }
454 
456 
457  // In case background mining was active and the miner threads are waiting
458  // on the background miner to signal start.
459  while (m_threads_active > 0)
460  {
461  m_is_background_mining_started_cond.notify_all();
463  }
464 
465  // The background mining thread could be sleeping for a long time, so we
466  // interrupt it just in case
467  m_background_mining_thread.interrupt();
468  m_background_mining_thread.join();
469  m_is_background_mining_enabled = false;
470 
471  MINFO("Mining has been stopped, " << m_threads.size() << " finished" );
472  m_threads.clear();
473  m_threads_autodetect.clear();
474  return true;
475  }
476  //-----------------------------------------------------------------------------------------------------
478  {
479  for(; bl.nonce != std::numeric_limits<uint32_t>::max(); bl.nonce++)
480  {
481  crypto::hash h;
482  get_block_longhash(bl, h, height);
483 
484  if(check_hash(h, diffic))
485  {
486  bl.invalidate_hashes();
487  return true;
488  }
489  }
490  bl.invalidate_hashes();
491  return false;
492  }
493  //-----------------------------------------------------------------------------------------------------
495  {
496  if(m_do_mining)
497  {
498  start(m_mine_address, m_threads_total, get_is_background_mining_enabled(), get_ignore_battery());
499  }
500  }
501  //-----------------------------------------------------------------------------------------------------
503  {
504  CRITICAL_REGION_LOCAL(m_miners_count_lock);
505  MDEBUG("miner::pause: " << m_pausers_count << " -> " << (m_pausers_count + 1));
506  ++m_pausers_count;
507  if(m_pausers_count == 1 && is_mining())
508  MDEBUG("MINING PAUSED");
509  }
510  //-----------------------------------------------------------------------------------------------------
512  {
513  CRITICAL_REGION_LOCAL(m_miners_count_lock);
514  MDEBUG("miner::resume: " << m_pausers_count << " -> " << (m_pausers_count - 1));
515  --m_pausers_count;
516  if(m_pausers_count < 0)
517  {
518  m_pausers_count = 0;
519  }
520  if(!m_pausers_count && is_mining())
521  MDEBUG("MINING RESUMED");
522  }
523  //-----------------------------------------------------------------------------------------------------
524  bool miner::worker_thread()
525  {
526  uint32_t th_local_index = boost::interprocess::ipcdetail::atomic_inc32(&m_thread_index);
527  MLOG_SET_THREAD_NAME(std::string("[miner ") + std::to_string(th_local_index) + "]");
528  MGINFO("Miner thread was started ["<< th_local_index << "]");
529  uint32_t nonce = m_starter_nonce + th_local_index;
530  uint64_t height = 0;
531  difficulty_type local_diff = 0;
532  uint32_t local_template_ver = 0;
533  block b;
535  ++m_threads_active;
536  while(!m_stop)
537  {
538  if(m_pausers_count)//anti split workaround
539  {
541  continue;
542  }
543  else if( m_is_background_mining_enabled )
544  {
545  misc_utils::sleep_no_w(m_miner_extra_sleep);
546  while( !m_is_background_mining_started )
547  {
548  MGINFO("background mining is enabled, but not started, waiting until start triggers");
549  boost::unique_lock<boost::mutex> started_lock( m_is_background_mining_started_mutex );
550  m_is_background_mining_started_cond.wait( started_lock );
551  if( m_stop ) break;
552  }
553 
554  if( m_stop ) continue;
555  }
556 
557  if(local_template_ver != m_template_no)
558  {
559  CRITICAL_REGION_BEGIN(m_template_lock);
560  b = m_template;
561 
562  if(!m_fallback_to_pow && b.major_version >= 8 && b.signature.empty()) {
563  continue;
564  }
565 
566  local_diff = m_diffic;
567  height = m_height;
569  local_template_ver = m_template_no;
570  nonce = m_starter_nonce + th_local_index;
571  }
572 
573  if(!local_template_ver)//no any set_block_template call
574  {
575  LOG_PRINT_L2("Block template not set yet");
577  continue;
578  }
579 
580  b.nonce = nonce;
581  crypto::hash h;
582  get_block_longhash(b, h, height);
583 
584  if(check_hash(h, local_diff))
585  {
586  //we lucky!
587  ++m_config.current_extra_message_index;
588  MGINFO_GREEN("Found block " << get_block_hash(b) << " at height " << height << " for difficulty: " << local_diff);
590  if(!m_phandler->handle_block_found(b, bvc) || !bvc.m_added_to_main_chain)
591  {
592  --m_config.current_extra_message_index;
593  }else
594  {
595  //success update, lets update config
596  if (!m_config_folder_path.empty())
597  epee::serialization::store_t_to_json_file(m_config, m_config_folder_path + "/" + MINER_CONFIG_FILE_NAME);
598  }
599  }
600  nonce+=m_threads_total;
601  ++m_hashes;
602  ++m_total_hashes;
603  }
605  MGINFO("Miner thread stopped ["<< th_local_index << "]");
606  --m_threads_active;
607  return true;
608  }
609  //-----------------------------------------------------------------------------------------------------
611  {
612  return m_is_background_mining_enabled;
613  }
614  //-----------------------------------------------------------------------------------------------------
616  {
617  return m_ignore_battery;
618  }
619  //-----------------------------------------------------------------------------------------------------
624  bool miner::set_is_background_mining_enabled(bool is_background_mining_enabled)
625  {
626  m_is_background_mining_enabled = is_background_mining_enabled;
627  // Extra logic will be required if we make this function public in the future
628  // and allow toggling smart mining without start/stop
629  //m_is_background_mining_enabled_cond.notify_one();
630  return true;
631  }
632  //-----------------------------------------------------------------------------------------------------
633  void miner::set_ignore_battery(bool ignore_battery)
634  {
635  m_ignore_battery = ignore_battery;
636  }
637  //-----------------------------------------------------------------------------------------------------
639  {
640  return m_min_idle_seconds;
641  }
642  //-----------------------------------------------------------------------------------------------------
643  bool miner::set_min_idle_seconds(uint64_t min_idle_seconds)
644  {
645  if(min_idle_seconds > BACKGROUND_MINING_MAX_MIN_IDLE_INTERVAL_IN_SECONDS) return false;
646  if(min_idle_seconds < BACKGROUND_MINING_MIN_MIN_IDLE_INTERVAL_IN_SECONDS) return false;
647  m_min_idle_seconds = min_idle_seconds;
648  return true;
649  }
650  //-----------------------------------------------------------------------------------------------------
652  {
653  return m_idle_threshold;
654  }
655  //-----------------------------------------------------------------------------------------------------
656  bool miner::set_idle_threshold(uint8_t idle_threshold)
657  {
658  if(idle_threshold > BACKGROUND_MINING_MAX_IDLE_THRESHOLD_PERCENTAGE) return false;
659  if(idle_threshold < BACKGROUND_MINING_MIN_IDLE_THRESHOLD_PERCENTAGE) return false;
660  m_idle_threshold = idle_threshold;
661  return true;
662  }
663  //-----------------------------------------------------------------------------------------------------
665  {
666  return m_mining_target;
667  }
668  //-----------------------------------------------------------------------------------------------------
669  bool miner::set_mining_target(uint8_t mining_target)
670  {
671  if(mining_target > BACKGROUND_MINING_MAX_MINING_TARGET_PERCENTAGE) return false;
672  if(mining_target < BACKGROUND_MINING_MIN_MINING_TARGET_PERCENTAGE) return false;
673  m_mining_target = mining_target;
674  return true;
675  }
676  //-----------------------------------------------------------------------------------------------------
677  bool miner::background_worker_thread()
678  {
679  uint64_t prev_total_time, current_total_time;
680  uint64_t prev_idle_time, current_idle_time;
681  uint64_t previous_process_time = 0, current_process_time = 0;
682  m_is_background_mining_started = false;
683 
684  if(!get_system_times(prev_total_time, prev_idle_time))
685  {
686  LOG_ERROR("get_system_times call failed, background mining will NOT work!");
687  return false;
688  }
689 
690  while(!m_stop)
691  {
692 
693  try
694  {
695  // Commenting out the below since we're going with privatizing the bg mining enabled
696  // function, but I'll leave the code/comments here for anyone that wants to modify the
697  // patch in the future
698  // -------------------------------------------------------------------------------------
699  // All of this might be overkill if we just enforced some simple requirements
700  // about changing this variable before/after the miner starts, but I envision
701  // in the future a checkbox that you can tick on/off for background mining after
702  // you've clicked "start mining". There's still an issue here where if background
703  // mining is disabled when start is called, this thread is never created, and so
704  // enabling after does nothing, something I have to fix in the future. However,
705  // this should take care of the case where mining is started with bg-enabled,
706  // and then the user decides to un-check background mining, and just do
707  // regular full-speed mining. I might just be over-doing it and thinking up
708  // non-existant use-cases, so if the consensus is to simplify, we can remove all this fluff.
709  /*
710  while( !m_is_background_mining_enabled )
711  {
712  MGINFO("background mining is disabled, waiting until enabled!");
713  boost::unique_lock<boost::mutex> enabled_lock( m_is_background_mining_enabled_mutex );
714  m_is_background_mining_enabled_cond.wait( enabled_lock );
715  }
716  */
717 
718  // If we're already mining, then sleep for the miner monitor interval.
719  // If we're NOT mining, then sleep for the idle monitor interval
721  if( !m_is_background_mining_started ) sleep_for_seconds = get_min_idle_seconds();
722  boost::this_thread::sleep_for(boost::chrono::seconds(sleep_for_seconds));
723  }
724  catch(const boost::thread_interrupted&)
725  {
726  MDEBUG("background miner thread interrupted ");
727  continue; // if interrupted because stop called, loop should end ..
728  }
729 
730  bool on_ac_power = m_ignore_battery;
731  if(!m_ignore_battery)
732  {
733  boost::tribool battery_powered(on_battery_power());
734  if(!indeterminate( battery_powered ))
735  {
736  on_ac_power = !(bool)battery_powered;
737  }
738  }
739 
740  if( m_is_background_mining_started )
741  {
742  // figure out if we need to stop, and monitor mining usage
743 
744  // If we get here, then previous values are initialized.
745  // Let's get some current data for comparison.
746 
747  if(!get_system_times(current_total_time, current_idle_time))
748  {
749  MERROR("get_system_times call failed");
750  continue;
751  }
752 
753  if(!get_process_time(current_process_time))
754  {
755  MERROR("get_process_time call failed!");
756  continue;
757  }
758 
759  uint64_t total_diff = (current_total_time - prev_total_time);
760  uint64_t idle_diff = (current_idle_time - prev_idle_time);
761  uint64_t process_diff = (current_process_time - previous_process_time);
762  uint8_t idle_percentage = get_percent_of_total(idle_diff, total_diff);
763  uint8_t process_percentage = get_percent_of_total(process_diff, total_diff);
764 
765  MDEBUG("idle percentage is " << unsigned(idle_percentage) << "\%, miner percentage is " << unsigned(process_percentage) << "\%, ac power : " << on_ac_power);
766  if( idle_percentage + process_percentage < get_idle_threshold() || !on_ac_power )
767  {
768  MINFO("cpu is " << unsigned(idle_percentage) << "% idle, idle threshold is " << unsigned(get_idle_threshold()) << "\%, ac power : " << on_ac_power << ", background mining stopping, thanks for your contribution!");
769  m_is_background_mining_started = false;
770 
771  // reset process times
772  previous_process_time = 0;
773  current_process_time = 0;
774  }
775  else
776  {
777  previous_process_time = current_process_time;
778 
779  // adjust the miner extra sleep variable
780  int64_t miner_extra_sleep_change = (-1 * (get_mining_target() - process_percentage) );
781  int64_t new_miner_extra_sleep = m_miner_extra_sleep + miner_extra_sleep_change;
782  // if you start the miner with few threads on a multicore system, this could
783  // fall below zero because all the time functions aggregate across all processors.
784  // I'm just hard limiting to 5 millis min sleep here, other options?
785  m_miner_extra_sleep = std::max( new_miner_extra_sleep , (int64_t)5 );
786  MDEBUG("m_miner_extra_sleep " << m_miner_extra_sleep);
787  }
788 
789  prev_total_time = current_total_time;
790  prev_idle_time = current_idle_time;
791  }
792  else if( on_ac_power )
793  {
794  // figure out if we need to start
795 
796  if(!get_system_times(current_total_time, current_idle_time))
797  {
798  MERROR("get_system_times call failed");
799  continue;
800  }
801 
802  uint64_t total_diff = (current_total_time - prev_total_time);
803  uint64_t idle_diff = (current_idle_time - prev_idle_time);
804  uint8_t idle_percentage = get_percent_of_total(idle_diff, total_diff);
805 
806  MDEBUG("idle percentage is " << unsigned(idle_percentage));
807  if( idle_percentage >= get_idle_threshold() && on_ac_power )
808  {
809  MINFO("cpu is " << unsigned(idle_percentage) << "% idle, idle threshold is " << unsigned(get_idle_threshold()) << "\%, ac power : " << on_ac_power << ", background mining started, good luck!");
810  m_is_background_mining_started = true;
811  m_is_background_mining_started_cond.notify_all();
812 
813  // Wait for a little mining to happen ..
814  boost::this_thread::sleep_for(boost::chrono::seconds( 1 ));
815 
816  // Starting data ...
817  if(!get_process_time(previous_process_time))
818  {
819  m_is_background_mining_started = false;
820  MERROR("get_process_time call failed!");
821  }
822  }
823 
824  prev_total_time = current_total_time;
825  prev_idle_time = current_idle_time;
826  }
827  }
828 
829  return true;
830  }
831  //-----------------------------------------------------------------------------------------------------
832  bool miner::get_system_times(uint64_t& total_time, uint64_t& idle_time)
833  {
834  #ifdef _WIN32
835 
836  FILETIME idleTime;
837  FILETIME kernelTime;
838  FILETIME userTime;
839  if ( GetSystemTimes( &idleTime, &kernelTime, &userTime ) != -1 )
840  {
841  total_time =
842  ( (((uint64_t)(kernelTime.dwHighDateTime)) << 32) | ((uint64_t)kernelTime.dwLowDateTime) )
843  + ( (((uint64_t)(userTime.dwHighDateTime)) << 32) | ((uint64_t)userTime.dwLowDateTime) );
844 
845  idle_time = ( (((uint64_t)(idleTime.dwHighDateTime)) << 32) | ((uint64_t)idleTime.dwLowDateTime) );
846 
847  return true;
848  }
849 
850  #elif defined(__linux__)
851 
852  const std::string STAT_FILE_PATH = "/proc/stat";
853 
854  if( !epee::file_io_utils::is_file_exist(STAT_FILE_PATH) )
855  {
856  LOG_ERROR("'" << STAT_FILE_PATH << "' file does not exist");
857  return false;
858  }
859 
860  std::ifstream stat_file_stream(STAT_FILE_PATH);
861  if( stat_file_stream.fail() )
862  {
863  LOG_ERROR("failed to open '" << STAT_FILE_PATH << "'");
864  return false;
865  }
866 
867  std::string line;
868  std::getline(stat_file_stream, line);
869  std::istringstream stat_file_iss(line);
870  stat_file_iss.ignore(65536, ' '); // skip cpu label ...
871  uint64_t utime, ntime, stime, itime;
872  if( !(stat_file_iss >> utime && stat_file_iss >> ntime && stat_file_iss >> stime && stat_file_iss >> itime) )
873  {
874  LOG_ERROR("failed to read '" << STAT_FILE_PATH << "'");
875  return false;
876  }
877 
878  idle_time = itime;
879  total_time = utime + ntime + stime + itime;
880 
881  return true;
882 
883  #elif defined(__APPLE__)
884 
885  mach_msg_type_number_t count;
886  kern_return_t status;
887  host_cpu_load_info_data_t stats;
888  count = HOST_CPU_LOAD_INFO_COUNT;
889  status = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&stats, &count);
890  if(status != KERN_SUCCESS)
891  {
892  return false;
893  }
894 
895  idle_time = stats.cpu_ticks[CPU_STATE_IDLE];
896  total_time = idle_time + stats.cpu_ticks[CPU_STATE_USER] + stats.cpu_ticks[CPU_STATE_SYSTEM];
897 
898  return true;
899 
900  #elif defined(__FreeBSD__)
901 
902  struct statinfo s;
903  size_t n = sizeof(s.cp_time);
904  if( sysctlbyname("kern.cp_time", s.cp_time, &n, NULL, 0) == -1 )
905  {
906  LOG_ERROR("sysctlbyname(\"kern.cp_time\"): " << strerror(errno));
907  return false;
908  }
909  if( n != sizeof(s.cp_time) )
910  {
911  LOG_ERROR("sysctlbyname(\"kern.cp_time\") output is unexpectedly "
912  << n << " bytes instead of the expected " << sizeof(s.cp_time)
913  << " bytes.");
914  return false;
915  }
916 
917  idle_time = s.cp_time[CP_IDLE];
918  total_time =
919  s.cp_time[CP_USER] +
920  s.cp_time[CP_NICE] +
921  s.cp_time[CP_SYS] +
922  s.cp_time[CP_INTR] +
923  s.cp_time[CP_IDLE];
924 
925  return true;
926 
927  #endif
928 
929  return false; // unsupported system
930  }
931  //-----------------------------------------------------------------------------------------------------
932  bool miner::get_process_time(uint64_t& total_time)
933  {
934  #ifdef _WIN32
935 
936  FILETIME createTime;
937  FILETIME exitTime;
938  FILETIME kernelTime;
939  FILETIME userTime;
940  if ( GetProcessTimes( GetCurrentProcess(), &createTime, &exitTime, &kernelTime, &userTime ) != -1 )
941  {
942  total_time =
943  ( (((uint64_t)(kernelTime.dwHighDateTime)) << 32) | ((uint64_t)kernelTime.dwLowDateTime) )
944  + ( (((uint64_t)(userTime.dwHighDateTime)) << 32) | ((uint64_t)userTime.dwLowDateTime) );
945 
946  return true;
947  }
948 
949  #elif (defined(__linux__) && defined(_SC_CLK_TCK)) || defined(__APPLE__) || defined(__FreeBSD__)
950 
951  struct tms tms;
952  if ( times(&tms) != (clock_t)-1 )
953  {
954  total_time = tms.tms_utime + tms.tms_stime;
955  return true;
956  }
957 
958  #endif
959 
960  return false; // unsupported system
961  }
962  //-----------------------------------------------------------------------------------------------------
963  uint8_t miner::get_percent_of_total(uint64_t other, uint64_t total)
964  {
965  return (uint8_t)( ceil( (other * 1.f / total * 1.f) * 100) );
966  }
967  //-----------------------------------------------------------------------------------------------------
968  boost::logic::tribool miner::on_battery_power()
969  {
970  #ifdef _WIN32
971 
972  SYSTEM_POWER_STATUS power_status;
973  if ( GetSystemPowerStatus( &power_status ) != 0 )
974  {
975  return boost::logic::tribool(power_status.ACLineStatus != 1);
976  }
977 
978  #elif defined(__APPLE__)
979 
980  #if TARGET_OS_MAC && (!defined(MAC_OS_X_VERSION_MIN_REQUIRED) || MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7)
981  return boost::logic::tribool(IOPSGetTimeRemainingEstimate() != kIOPSTimeRemainingUnlimited);
982  #else
983  // iOS or OSX <10.7
984  return boost::logic::tribool(boost::logic::indeterminate);
985  #endif
986 
987  #elif defined(__linux__)
988 
989  // Use the power_supply class http://lxr.linux.no/#linux+v4.10.1/Documentation/power/power_supply_class.txt
990  std::string power_supply_class_path = "/sys/class/power_supply";
991 
992  boost::tribool on_battery = boost::logic::tribool(boost::logic::indeterminate);
993  if (boost::filesystem::is_directory(power_supply_class_path))
994  {
995  const boost::filesystem::directory_iterator end_itr;
996  for (boost::filesystem::directory_iterator iter(power_supply_class_path); iter != end_itr; ++iter)
997  {
998  const boost::filesystem::path& power_supply_path = iter->path();
999  if (boost::filesystem::is_directory(power_supply_path))
1000  {
1001  boost::filesystem::path power_supply_type_path = power_supply_path / "type";
1002  if (boost::filesystem::is_regular_file(power_supply_type_path))
1003  {
1004  std::ifstream power_supply_type_stream(power_supply_type_path.string());
1005  if (power_supply_type_stream.fail())
1006  {
1007  LOG_PRINT_L0("Unable to read from " << power_supply_type_path << " to check power supply type");
1008  continue;
1009  }
1010 
1011  std::string power_supply_type;
1012  std::getline(power_supply_type_stream, power_supply_type);
1013 
1014  // If there is an AC adapter that's present and online we can break early
1015  if (boost::starts_with(power_supply_type, "Mains"))
1016  {
1017  boost::filesystem::path power_supply_online_path = power_supply_path / "online";
1018  if (boost::filesystem::is_regular_file(power_supply_online_path))
1019  {
1020  std::ifstream power_supply_online_stream(power_supply_online_path.string());
1021  if (power_supply_online_stream.fail())
1022  {
1023  LOG_PRINT_L0("Unable to read from " << power_supply_online_path << " to check ac power supply status");
1024  continue;
1025  }
1026 
1027  if (power_supply_online_stream.get() == '1')
1028  {
1029  return boost::logic::tribool(false);
1030  }
1031  }
1032  }
1033  else if (boost::starts_with(power_supply_type, "Battery") && boost::logic::indeterminate(on_battery))
1034  {
1035  boost::filesystem::path power_supply_status_path = power_supply_path / "status";
1036  if (boost::filesystem::is_regular_file(power_supply_status_path))
1037  {
1038  std::ifstream power_supply_status_stream(power_supply_status_path.string());
1039  if (power_supply_status_stream.fail())
1040  {
1041  LOG_PRINT_L0("Unable to read from " << power_supply_status_path << " to check battery power supply status");
1042  continue;
1043  }
1044 
1045  // Possible status are Charging, Full, Discharging, Not Charging, and Unknown
1046  // We are only need to handle negative states right now
1047  std::string power_supply_status;
1048  std::getline(power_supply_status_stream, power_supply_status);
1049  if (boost::starts_with(power_supply_status, "Charging") || boost::starts_with(power_supply_status, "Full"))
1050  {
1051  on_battery = boost::logic::tribool(false);
1052  }
1053 
1054  if (boost::starts_with(power_supply_status, "Discharging"))
1055  {
1056  on_battery = boost::logic::tribool(true);
1057  }
1058  }
1059  }
1060  }
1061  }
1062  }
1063  }
1064 
1065  if (boost::logic::indeterminate(on_battery))
1066  {
1067  static bool error_shown = false;
1068  if (!error_shown)
1069  {
1070  LOG_ERROR("couldn't query power status from " << power_supply_class_path);
1071  error_shown = true;
1072  }
1073  }
1074  return on_battery;
1075 
1076  #elif defined(__FreeBSD__)
1077  int ac;
1078  size_t n = sizeof(ac);
1079  if( sysctlbyname("hw.acpi.acline", &ac, &n, NULL, 0) == -1 )
1080  {
1081  if( errno != ENOENT )
1082  {
1083  LOG_ERROR("Cannot query battery status: "
1084  << "sysctlbyname(\"hw.acpi.acline\"): " << strerror(errno));
1085  return boost::logic::tribool(boost::logic::indeterminate);
1086  }
1087 
1088  // If sysctl fails with ENOENT, then try querying /dev/apm.
1089 
1090  static const char* dev_apm = "/dev/apm";
1091  const int fd = open(dev_apm, O_RDONLY);
1092  if( fd == -1 ) {
1093  LOG_ERROR("Cannot query battery status: "
1094  << "open(): " << dev_apm << ": " << strerror(errno));
1095  return boost::logic::tribool(boost::logic::indeterminate);
1096  }
1097 
1098  apm_info info;
1099  if( ioctl(fd, APMIO_GETINFO, &info) == -1 ) {
1100  close(fd);
1101  LOG_ERROR("Cannot query battery status: "
1102  << "ioctl(" << dev_apm << ", APMIO_GETINFO): " << strerror(errno));
1103  return boost::logic::tribool(boost::logic::indeterminate);
1104  }
1105 
1106  close(fd);
1107 
1108  // See apm(8).
1109  switch( info.ai_acline )
1110  {
1111  case 0: // off-line
1112  case 2: // backup power
1113  return boost::logic::tribool(true);
1114  case 1: // on-line
1115  return boost::logic::tribool(false);
1116  }
1117  switch( info.ai_batt_stat )
1118  {
1119  case 0: // high
1120  case 1: // low
1121  case 2: // critical
1122  return boost::logic::tribool(true);
1123  case 3: // charging
1124  return boost::logic::tribool(false);
1125  }
1126 
1127  LOG_ERROR("Cannot query battery status: "
1128  << "sysctl hw.acpi.acline is not available and /dev/apm returns "
1129  << "unexpected ac-line status (" << info.ai_acline << ") and "
1130  << "battery status (" << info.ai_batt_stat << ").");
1131  return boost::logic::tribool(boost::logic::indeterminate);
1132  }
1133  if( n != sizeof(ac) )
1134  {
1135  LOG_ERROR("sysctlbyname(\"hw.acpi.acline\") output is unexpectedly "
1136  << n << " bytes instead of the expected " << sizeof(ac) << " bytes.");
1137  return boost::logic::tribool(boost::logic::indeterminate);
1138  }
1139  return boost::logic::tribool(ac == 0);
1140  #endif
1141 
1142  LOG_ERROR("couldn't query power status");
1143  return boost::logic::tribool(boost::logic::indeterminate);
1144  }
1145 }
void send_stop_signal()
Definition: miner.cpp:438
#define MERROR(x)
Definition: misc_log_ex.h:73
uint64_t get_min_idle_seconds() const
Definition: miner.cpp:638
#define LOG_PRINT_L2(x)
Definition: misc_log_ex.h:101
void slow_hash_free_state()
uint64_t get_tick_count()
#define MTRACE(x)
Definition: misc_log_ex.h:77
bool set_block_template(const block &bl, const difficulty_type &diffic, uint64_t height, uint64_t block_reward)
Definition: miner.cpp:136
#define MINFO(x)
Definition: misc_log_ex.h:75
uint8_t get_mining_target() const
Definition: miner.cpp:664
bool set_mining_target(uint8_t mining_target)
Definition: miner.cpp:669
bool on_idle()
Definition: miner.cpp:178
::std::string string
Definition: gtest-port.h:1097
virtual bool handle_block_found(block &b, block_verification_context &bvc)=0
uint8_t get_idle_threshold() const
Definition: miner.cpp:651
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition: misc_log_ex.h:181
uint64_t height
Definition: blockchain.cpp:91
#define THREAD_STACK_SIZE
#define LOG_PRINT_L0(x)
Definition: misc_log_ex.h:99
#define AUTODETECT_GAIN_THRESHOLD
Definition: miner.cpp:78
static constexpr uint8_t BACKGROUND_MINING_MAX_IDLE_THRESHOLD_PERCENTAGE
Definition: miner.h:94
bool load_file_to_string(const std::string &path_to_file, std::string &target_str, size_t max_size=1000000000)
static constexpr uint8_t BACKGROUND_MINING_MIN_MINING_TARGET_PERCENTAGE
Definition: miner.h:99
uint64_t get_ns_count()
bool is_file_exist(const std::string &path)
Definition: file_io_utils.h:66
unsigned char uint8_t
Definition: stdint.h:124
std::string & trim(std::string &str)
Definition: string_tools.h:288
static constexpr uint8_t BACKGROUND_MINING_MAX_MINING_TARGET_PERCENTAGE
Definition: miner.h:100
bool init(const boost::program_options::variables_map &vm, network_type nettype, bool fallback_to_pow=false)
Definition: miner.cpp:291
static void init_options(boost::program_options::options_description &desc)
Definition: miner.cpp:279
#define AUTODETECT_WINDOW
Definition: miner.cpp:77
#define MGINFO(x)
Definition: misc_log_ex.h:80
#define CRITICAL_REGION_END()
Definition: syncobj.h:233
static constexpr uint8_t BACKGROUND_MINING_MIN_IDLE_THRESHOLD_PERCENTAGE
Definition: miner.h:93
#define MDEBUG(x)
Definition: misc_log_ex.h:76
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
bool start(const account_public_address &adr, size_t threads_count, bool do_background=false, bool ignore_battery=false)
Definition: miner.cpp:372
bool get_block_longhash(const block &b, crypto::hash &res, uint64_t height)
#define MGINFO_GREEN(x)
Definition: misc_log_ex.h:82
mdb_size_t count(MDB_cursor *cur)
static constexpr uint8_t BACKGROUND_MINING_MINER_MONITOR_INVERVAL_IN_SECONDS
Definition: miner.h:101
uint32_t get_threads_count() const
Definition: miner.cpp:368
std::enable_if<!std::is_same< T, bool >::value, bool >::type has_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg)
Definition: command_line.h:258
unsigned int uint32_t
Definition: stdint.h:126
bool on_block_chain_update()
Definition: miner.cpp:148
#define CRITICAL_REGION_BEGIN(x)
Definition: syncobj.h:229
bool get_block_hash(const block &b, crypto::hash &res)
bool store_t_to_json_file(t_struct &str_in, const std::string &fpath)
const account_public_address & get_mining_address() const
Definition: miner.cpp:363
bool check_hash(const crypto::hash &hash, difficulty_type difficulty)
Definition: difficulty.cpp:203
#define MINER_CONFIG_FILE_NAME
unsigned __int64 uint64_t
Definition: stdint.h:136
#define CRITICAL_REGION_LOCAL(x)
Definition: syncobj.h:228
bool set_idle_threshold(uint8_t idle_threshold)
Definition: miner.cpp:656
#define false
Definition: stdbool.h:38
bool sleep_no_w(long ms)
static constexpr uint16_t BACKGROUND_MINING_MIN_MIN_IDLE_INTERVAL_IN_SECONDS
Definition: miner.h:96
uint64_t get_speed() const
Definition: miner.cpp:428
void add_arg(boost::program_options::options_description &description, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg, bool unique=true)
Definition: command_line.h:188
std::string blobdata
Definition: blobdatatype.h:39
CXA_THROW_INFO_T * info
Definition: stack_trace.cpp:91
std::vector< uint8_t > signature
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
boost::multiprecision::uint128_t difficulty_type
Definition: difficulty.h:43
#define ENDL
Definition: misc_log_ex.h:149
void slow_hash_allocate_state()
#define MLOG_SET_THREAD_NAME(x)
Definition: misc_log_ex.h:115
void do_print_hashrate(bool do_hr)
Definition: miner.cpp:198
static bool find_nonce_for_given_block(block &bl, const difficulty_type &diffic, uint64_t height)
Definition: miner.cpp:477
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true > &arg)
Definition: command_line.h:271
signed __int64 int64_t
Definition: stdint.h:135
bool get_account_address_from_str(address_parse_info &info, network_type nettype, std::string const &str)
int bool
Definition: stdbool.h:36
#define AUTO_VAL_INIT(v)
Definition: misc_language.h:53
bool get_ignore_battery() const
Definition: miner.cpp:615
bool get_is_background_mining_enabled() const
Definition: miner.cpp:610
POD_CLASS hash
Definition: hash.h:50
bool is_mining() const
Definition: miner.cpp:358
bool load_t_from_json_file(t_struct &out, const std::string &json_file)
std::string to_string(t_connection_type type)
static constexpr uint16_t BACKGROUND_MINING_MAX_MIN_IDLE_INTERVAL_IN_SECONDS
Definition: miner.h:97
std::string base64_decode(std::string const &encoded_string)
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
void on_synchronized()
Definition: miner.cpp:494
bool do_call(functor_t functr)
Definition: math_helper.h:263
bool set_min_idle_seconds(uint64_t min_idle_seconds)
Definition: miner.cpp:643