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  m_mine_address = info.address;
326  m_threads_total = 1;
327  m_do_mining = true;
328  if(command_line::has_arg(vm, arg_mining_threads))
329  {
330  m_threads_total = command_line::get_arg(vm, arg_mining_threads);
331  }
332  }
333 
334  // Background mining parameters
335  // Let init set all parameters even if background mining is not enabled, they can start later with params set
336  if(command_line::has_arg(vm, arg_bg_mining_enable))
337  set_is_background_mining_enabled( command_line::get_arg(vm, arg_bg_mining_enable) );
338  if(command_line::has_arg(vm, arg_bg_mining_ignore_battery))
339  set_ignore_battery( command_line::get_arg(vm, arg_bg_mining_ignore_battery) );
340  if(command_line::has_arg(vm, arg_bg_mining_min_idle_interval_seconds))
341  set_min_idle_seconds( command_line::get_arg(vm, arg_bg_mining_min_idle_interval_seconds) );
342  if(command_line::has_arg(vm, arg_bg_mining_idle_threshold_percentage))
343  set_idle_threshold( command_line::get_arg(vm, arg_bg_mining_idle_threshold_percentage) );
344  if(command_line::has_arg(vm, arg_bg_mining_miner_target_percentage))
345  set_mining_target( command_line::get_arg(vm, arg_bg_mining_miner_target_percentage) );
346 
347  m_fallback_to_pow = fallback_to_pow;
348 
349  return true;
350  }
351  //-----------------------------------------------------------------------------------------------------
352  bool miner::is_mining() const
353  {
354  return !m_stop;
355  }
356  //-----------------------------------------------------------------------------------------------------
358  {
359  return m_mine_address;
360  }
361  //-----------------------------------------------------------------------------------------------------
363  return m_threads_total;
364  }
365  //-----------------------------------------------------------------------------------------------------
366  bool miner::start(const account_public_address& adr, size_t threads_count, bool do_background, bool ignore_battery)
367  {
368  m_block_reward = 0;
369  m_mine_address = adr;
370  m_threads_total = static_cast<uint32_t>(threads_count);
371  if (threads_count == 0)
372  {
373  m_threads_autodetect.clear();
374  m_threads_autodetect.push_back({epee::misc_utils::get_ns_count(), m_total_hashes});
375  m_threads_total = 1;
376  }
377  m_starter_nonce = crypto::rand<uint32_t>();
378  CRITICAL_REGION_LOCAL(m_threads_lock);
379  if(is_mining())
380  {
381  LOG_ERROR("Starting miner but it's already started");
382  return false;
383  }
384 
385  if(!m_threads.empty())
386  {
387  LOG_ERROR("Unable to start miner because there are active mining threads");
388  return false;
389  }
390 
391  request_block_template();//lets update block template
392 
393  boost::interprocess::ipcdetail::atomic_write32(&m_stop, 0);
394  boost::interprocess::ipcdetail::atomic_write32(&m_thread_index, 0);
395  set_is_background_mining_enabled(do_background);
396  set_ignore_battery(ignore_battery);
397 
398  for(size_t i = 0; i != m_threads_total; i++)
399  {
400  m_threads.push_back(boost::thread(m_attrs, boost::bind(&miner::worker_thread, this)));
401  }
402 
403  if (threads_count == 0)
404  MINFO("Mining has started, autodetecting optimal number of threads, good luck!" );
405  else
406  MINFO("Mining has started with " << threads_count << " threads, good luck!" );
407 
409  {
410  m_background_mining_thread = boost::thread(m_attrs, boost::bind(&miner::background_worker_thread, this));
411  LOG_PRINT_L0("Background mining controller thread started" );
412  }
413 
414  if(get_ignore_battery())
415  {
416  MINFO("Ignoring battery");
417  }
418 
419  return true;
420  }
421  //-----------------------------------------------------------------------------------------------------
423  {
424  if(is_mining()) {
425  return m_current_hash_rate;
426  }
427  else {
428  return 0;
429  }
430  }
431  //-----------------------------------------------------------------------------------------------------
433  {
434  boost::interprocess::ipcdetail::atomic_write32(&m_stop, 1);
435  }
436  //-----------------------------------------------------------------------------------------------------
437  bool miner::stop()
438  {
439  MTRACE("Miner has received stop signal");
440 
441  CRITICAL_REGION_LOCAL(m_threads_lock);
442  bool mining = !m_threads.empty();
443  if (!mining)
444  {
445  MTRACE("Not mining - nothing to stop" );
446  return true;
447  }
448 
450 
451  // In case background mining was active and the miner threads are waiting
452  // on the background miner to signal start.
453  while (m_threads_active > 0)
454  {
455  m_is_background_mining_started_cond.notify_all();
457  }
458 
459  // The background mining thread could be sleeping for a long time, so we
460  // interrupt it just in case
461  m_background_mining_thread.interrupt();
462  m_background_mining_thread.join();
463  m_is_background_mining_enabled = false;
464 
465  MINFO("Mining has been stopped, " << m_threads.size() << " finished" );
466  m_threads.clear();
467  m_threads_autodetect.clear();
468  return true;
469  }
470  //-----------------------------------------------------------------------------------------------------
472  {
473  for(; bl.nonce != std::numeric_limits<uint32_t>::max(); bl.nonce++)
474  {
475  crypto::hash h;
476  get_block_longhash(bl, h, height);
477 
478  if(check_hash(h, diffic))
479  {
480  bl.invalidate_hashes();
481  return true;
482  }
483  }
484  bl.invalidate_hashes();
485  return false;
486  }
487  //-----------------------------------------------------------------------------------------------------
489  {
490  if(m_do_mining)
491  {
492  start(m_mine_address, m_threads_total, get_is_background_mining_enabled(), get_ignore_battery());
493  }
494  }
495  //-----------------------------------------------------------------------------------------------------
497  {
498  CRITICAL_REGION_LOCAL(m_miners_count_lock);
499  MDEBUG("miner::pause: " << m_pausers_count << " -> " << (m_pausers_count + 1));
500  ++m_pausers_count;
501  if(m_pausers_count == 1 && is_mining())
502  MDEBUG("MINING PAUSED");
503  }
504  //-----------------------------------------------------------------------------------------------------
506  {
507  CRITICAL_REGION_LOCAL(m_miners_count_lock);
508  MDEBUG("miner::resume: " << m_pausers_count << " -> " << (m_pausers_count - 1));
509  --m_pausers_count;
510  if(m_pausers_count < 0)
511  {
512  m_pausers_count = 0;
513  }
514  if(!m_pausers_count && is_mining())
515  MDEBUG("MINING RESUMED");
516  }
517  //-----------------------------------------------------------------------------------------------------
518  bool miner::worker_thread()
519  {
520  uint32_t th_local_index = boost::interprocess::ipcdetail::atomic_inc32(&m_thread_index);
521  MLOG_SET_THREAD_NAME(std::string("[miner ") + std::to_string(th_local_index) + "]");
522  MGINFO("Miner thread was started ["<< th_local_index << "]");
523  uint32_t nonce = m_starter_nonce + th_local_index;
524  uint64_t height = 0;
525  difficulty_type local_diff = 0;
526  uint32_t local_template_ver = 0;
527  block b;
529  ++m_threads_active;
530  while(!m_stop)
531  {
532  if(m_pausers_count)//anti split workaround
533  {
535  continue;
536  }
537  else if( m_is_background_mining_enabled )
538  {
539  misc_utils::sleep_no_w(m_miner_extra_sleep);
540  while( !m_is_background_mining_started )
541  {
542  MGINFO("background mining is enabled, but not started, waiting until start triggers");
543  boost::unique_lock<boost::mutex> started_lock( m_is_background_mining_started_mutex );
544  m_is_background_mining_started_cond.wait( started_lock );
545  if( m_stop ) break;
546  }
547 
548  if( m_stop ) continue;
549  }
550 
551  if(local_template_ver != m_template_no)
552  {
553  CRITICAL_REGION_BEGIN(m_template_lock);
554  b = m_template;
555 
556  if(!m_fallback_to_pow && b.major_version >= 8 && b.signature.empty()) {
557  continue;
558  }
559 
560  local_diff = m_diffic;
561  height = m_height;
563  local_template_ver = m_template_no;
564  nonce = m_starter_nonce + th_local_index;
565  }
566 
567  if(!local_template_ver)//no any set_block_template call
568  {
569  LOG_PRINT_L2("Block template not set yet");
571  continue;
572  }
573 
574  b.nonce = nonce;
575  crypto::hash h;
576  get_block_longhash(b, h, height);
577 
578  if(check_hash(h, local_diff))
579  {
580  //we lucky!
581  ++m_config.current_extra_message_index;
582  MGINFO_GREEN("Found block " << get_block_hash(b) << " at height " << height << " for difficulty: " << local_diff);
584  if(!m_phandler->handle_block_found(b, bvc) || !bvc.m_added_to_main_chain)
585  {
586  --m_config.current_extra_message_index;
587  }else
588  {
589  //success update, lets update config
590  if (!m_config_folder_path.empty())
591  epee::serialization::store_t_to_json_file(m_config, m_config_folder_path + "/" + MINER_CONFIG_FILE_NAME);
592  }
593  }
594  nonce+=m_threads_total;
595  ++m_hashes;
596  ++m_total_hashes;
597  }
599  MGINFO("Miner thread stopped ["<< th_local_index << "]");
600  --m_threads_active;
601  return true;
602  }
603  //-----------------------------------------------------------------------------------------------------
605  {
606  return m_is_background_mining_enabled;
607  }
608  //-----------------------------------------------------------------------------------------------------
610  {
611  return m_ignore_battery;
612  }
613  //-----------------------------------------------------------------------------------------------------
618  bool miner::set_is_background_mining_enabled(bool is_background_mining_enabled)
619  {
620  m_is_background_mining_enabled = is_background_mining_enabled;
621  // Extra logic will be required if we make this function public in the future
622  // and allow toggling smart mining without start/stop
623  //m_is_background_mining_enabled_cond.notify_one();
624  return true;
625  }
626  //-----------------------------------------------------------------------------------------------------
627  void miner::set_ignore_battery(bool ignore_battery)
628  {
629  m_ignore_battery = ignore_battery;
630  }
631  //-----------------------------------------------------------------------------------------------------
633  {
634  return m_min_idle_seconds;
635  }
636  //-----------------------------------------------------------------------------------------------------
637  bool miner::set_min_idle_seconds(uint64_t min_idle_seconds)
638  {
639  if(min_idle_seconds > BACKGROUND_MINING_MAX_MIN_IDLE_INTERVAL_IN_SECONDS) return false;
640  if(min_idle_seconds < BACKGROUND_MINING_MIN_MIN_IDLE_INTERVAL_IN_SECONDS) return false;
641  m_min_idle_seconds = min_idle_seconds;
642  return true;
643  }
644  //-----------------------------------------------------------------------------------------------------
646  {
647  return m_idle_threshold;
648  }
649  //-----------------------------------------------------------------------------------------------------
650  bool miner::set_idle_threshold(uint8_t idle_threshold)
651  {
652  if(idle_threshold > BACKGROUND_MINING_MAX_IDLE_THRESHOLD_PERCENTAGE) return false;
653  if(idle_threshold < BACKGROUND_MINING_MIN_IDLE_THRESHOLD_PERCENTAGE) return false;
654  m_idle_threshold = idle_threshold;
655  return true;
656  }
657  //-----------------------------------------------------------------------------------------------------
659  {
660  return m_mining_target;
661  }
662  //-----------------------------------------------------------------------------------------------------
663  bool miner::set_mining_target(uint8_t mining_target)
664  {
665  if(mining_target > BACKGROUND_MINING_MAX_MINING_TARGET_PERCENTAGE) return false;
666  if(mining_target < BACKGROUND_MINING_MIN_MINING_TARGET_PERCENTAGE) return false;
667  m_mining_target = mining_target;
668  return true;
669  }
670  //-----------------------------------------------------------------------------------------------------
671  bool miner::background_worker_thread()
672  {
673  uint64_t prev_total_time, current_total_time;
674  uint64_t prev_idle_time, current_idle_time;
675  uint64_t previous_process_time = 0, current_process_time = 0;
676  m_is_background_mining_started = false;
677 
678  if(!get_system_times(prev_total_time, prev_idle_time))
679  {
680  LOG_ERROR("get_system_times call failed, background mining will NOT work!");
681  return false;
682  }
683 
684  while(!m_stop)
685  {
686 
687  try
688  {
689  // Commenting out the below since we're going with privatizing the bg mining enabled
690  // function, but I'll leave the code/comments here for anyone that wants to modify the
691  // patch in the future
692  // -------------------------------------------------------------------------------------
693  // All of this might be overkill if we just enforced some simple requirements
694  // about changing this variable before/after the miner starts, but I envision
695  // in the future a checkbox that you can tick on/off for background mining after
696  // you've clicked "start mining". There's still an issue here where if background
697  // mining is disabled when start is called, this thread is never created, and so
698  // enabling after does nothing, something I have to fix in the future. However,
699  // this should take care of the case where mining is started with bg-enabled,
700  // and then the user decides to un-check background mining, and just do
701  // regular full-speed mining. I might just be over-doing it and thinking up
702  // non-existant use-cases, so if the consensus is to simplify, we can remove all this fluff.
703  /*
704  while( !m_is_background_mining_enabled )
705  {
706  MGINFO("background mining is disabled, waiting until enabled!");
707  boost::unique_lock<boost::mutex> enabled_lock( m_is_background_mining_enabled_mutex );
708  m_is_background_mining_enabled_cond.wait( enabled_lock );
709  }
710  */
711 
712  // If we're already mining, then sleep for the miner monitor interval.
713  // If we're NOT mining, then sleep for the idle monitor interval
715  if( !m_is_background_mining_started ) sleep_for_seconds = get_min_idle_seconds();
716  boost::this_thread::sleep_for(boost::chrono::seconds(sleep_for_seconds));
717  }
718  catch(const boost::thread_interrupted&)
719  {
720  MDEBUG("background miner thread interrupted ");
721  continue; // if interrupted because stop called, loop should end ..
722  }
723 
724  bool on_ac_power = m_ignore_battery;
725  if(!m_ignore_battery)
726  {
727  boost::tribool battery_powered(on_battery_power());
728  if(!indeterminate( battery_powered ))
729  {
730  on_ac_power = !(bool)battery_powered;
731  }
732  }
733 
734  if( m_is_background_mining_started )
735  {
736  // figure out if we need to stop, and monitor mining usage
737 
738  // If we get here, then previous values are initialized.
739  // Let's get some current data for comparison.
740 
741  if(!get_system_times(current_total_time, current_idle_time))
742  {
743  MERROR("get_system_times call failed");
744  continue;
745  }
746 
747  if(!get_process_time(current_process_time))
748  {
749  MERROR("get_process_time call failed!");
750  continue;
751  }
752 
753  uint64_t total_diff = (current_total_time - prev_total_time);
754  uint64_t idle_diff = (current_idle_time - prev_idle_time);
755  uint64_t process_diff = (current_process_time - previous_process_time);
756  uint8_t idle_percentage = get_percent_of_total(idle_diff, total_diff);
757  uint8_t process_percentage = get_percent_of_total(process_diff, total_diff);
758 
759  MDEBUG("idle percentage is " << unsigned(idle_percentage) << "\%, miner percentage is " << unsigned(process_percentage) << "\%, ac power : " << on_ac_power);
760  if( idle_percentage + process_percentage < get_idle_threshold() || !on_ac_power )
761  {
762  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!");
763  m_is_background_mining_started = false;
764 
765  // reset process times
766  previous_process_time = 0;
767  current_process_time = 0;
768  }
769  else
770  {
771  previous_process_time = current_process_time;
772 
773  // adjust the miner extra sleep variable
774  int64_t miner_extra_sleep_change = (-1 * (get_mining_target() - process_percentage) );
775  int64_t new_miner_extra_sleep = m_miner_extra_sleep + miner_extra_sleep_change;
776  // if you start the miner with few threads on a multicore system, this could
777  // fall below zero because all the time functions aggregate across all processors.
778  // I'm just hard limiting to 5 millis min sleep here, other options?
779  m_miner_extra_sleep = std::max( new_miner_extra_sleep , (int64_t)5 );
780  MDEBUG("m_miner_extra_sleep " << m_miner_extra_sleep);
781  }
782 
783  prev_total_time = current_total_time;
784  prev_idle_time = current_idle_time;
785  }
786  else if( on_ac_power )
787  {
788  // figure out if we need to start
789 
790  if(!get_system_times(current_total_time, current_idle_time))
791  {
792  MERROR("get_system_times call failed");
793  continue;
794  }
795 
796  uint64_t total_diff = (current_total_time - prev_total_time);
797  uint64_t idle_diff = (current_idle_time - prev_idle_time);
798  uint8_t idle_percentage = get_percent_of_total(idle_diff, total_diff);
799 
800  MDEBUG("idle percentage is " << unsigned(idle_percentage));
801  if( idle_percentage >= get_idle_threshold() && on_ac_power )
802  {
803  MINFO("cpu is " << unsigned(idle_percentage) << "% idle, idle threshold is " << unsigned(get_idle_threshold()) << "\%, ac power : " << on_ac_power << ", background mining started, good luck!");
804  m_is_background_mining_started = true;
805  m_is_background_mining_started_cond.notify_all();
806 
807  // Wait for a little mining to happen ..
808  boost::this_thread::sleep_for(boost::chrono::seconds( 1 ));
809 
810  // Starting data ...
811  if(!get_process_time(previous_process_time))
812  {
813  m_is_background_mining_started = false;
814  MERROR("get_process_time call failed!");
815  }
816  }
817 
818  prev_total_time = current_total_time;
819  prev_idle_time = current_idle_time;
820  }
821  }
822 
823  return true;
824  }
825  //-----------------------------------------------------------------------------------------------------
826  bool miner::get_system_times(uint64_t& total_time, uint64_t& idle_time)
827  {
828  #ifdef _WIN32
829 
830  FILETIME idleTime;
831  FILETIME kernelTime;
832  FILETIME userTime;
833  if ( GetSystemTimes( &idleTime, &kernelTime, &userTime ) != -1 )
834  {
835  total_time =
836  ( (((uint64_t)(kernelTime.dwHighDateTime)) << 32) | ((uint64_t)kernelTime.dwLowDateTime) )
837  + ( (((uint64_t)(userTime.dwHighDateTime)) << 32) | ((uint64_t)userTime.dwLowDateTime) );
838 
839  idle_time = ( (((uint64_t)(idleTime.dwHighDateTime)) << 32) | ((uint64_t)idleTime.dwLowDateTime) );
840 
841  return true;
842  }
843 
844  #elif defined(__linux__)
845 
846  const std::string STAT_FILE_PATH = "/proc/stat";
847 
848  if( !epee::file_io_utils::is_file_exist(STAT_FILE_PATH) )
849  {
850  LOG_ERROR("'" << STAT_FILE_PATH << "' file does not exist");
851  return false;
852  }
853 
854  std::ifstream stat_file_stream(STAT_FILE_PATH);
855  if( stat_file_stream.fail() )
856  {
857  LOG_ERROR("failed to open '" << STAT_FILE_PATH << "'");
858  return false;
859  }
860 
861  std::string line;
862  std::getline(stat_file_stream, line);
863  std::istringstream stat_file_iss(line);
864  stat_file_iss.ignore(65536, ' '); // skip cpu label ...
865  uint64_t utime, ntime, stime, itime;
866  if( !(stat_file_iss >> utime && stat_file_iss >> ntime && stat_file_iss >> stime && stat_file_iss >> itime) )
867  {
868  LOG_ERROR("failed to read '" << STAT_FILE_PATH << "'");
869  return false;
870  }
871 
872  idle_time = itime;
873  total_time = utime + ntime + stime + itime;
874 
875  return true;
876 
877  #elif defined(__APPLE__)
878 
879  mach_msg_type_number_t count;
880  kern_return_t status;
881  host_cpu_load_info_data_t stats;
882  count = HOST_CPU_LOAD_INFO_COUNT;
883  status = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&stats, &count);
884  if(status != KERN_SUCCESS)
885  {
886  return false;
887  }
888 
889  idle_time = stats.cpu_ticks[CPU_STATE_IDLE];
890  total_time = idle_time + stats.cpu_ticks[CPU_STATE_USER] + stats.cpu_ticks[CPU_STATE_SYSTEM];
891 
892  return true;
893 
894  #elif defined(__FreeBSD__)
895 
896  struct statinfo s;
897  size_t n = sizeof(s.cp_time);
898  if( sysctlbyname("kern.cp_time", s.cp_time, &n, NULL, 0) == -1 )
899  {
900  LOG_ERROR("sysctlbyname(\"kern.cp_time\"): " << strerror(errno));
901  return false;
902  }
903  if( n != sizeof(s.cp_time) )
904  {
905  LOG_ERROR("sysctlbyname(\"kern.cp_time\") output is unexpectedly "
906  << n << " bytes instead of the expected " << sizeof(s.cp_time)
907  << " bytes.");
908  return false;
909  }
910 
911  idle_time = s.cp_time[CP_IDLE];
912  total_time =
913  s.cp_time[CP_USER] +
914  s.cp_time[CP_NICE] +
915  s.cp_time[CP_SYS] +
916  s.cp_time[CP_INTR] +
917  s.cp_time[CP_IDLE];
918 
919  return true;
920 
921  #endif
922 
923  return false; // unsupported system
924  }
925  //-----------------------------------------------------------------------------------------------------
926  bool miner::get_process_time(uint64_t& total_time)
927  {
928  #ifdef _WIN32
929 
930  FILETIME createTime;
931  FILETIME exitTime;
932  FILETIME kernelTime;
933  FILETIME userTime;
934  if ( GetProcessTimes( GetCurrentProcess(), &createTime, &exitTime, &kernelTime, &userTime ) != -1 )
935  {
936  total_time =
937  ( (((uint64_t)(kernelTime.dwHighDateTime)) << 32) | ((uint64_t)kernelTime.dwLowDateTime) )
938  + ( (((uint64_t)(userTime.dwHighDateTime)) << 32) | ((uint64_t)userTime.dwLowDateTime) );
939 
940  return true;
941  }
942 
943  #elif (defined(__linux__) && defined(_SC_CLK_TCK)) || defined(__APPLE__) || defined(__FreeBSD__)
944 
945  struct tms tms;
946  if ( times(&tms) != (clock_t)-1 )
947  {
948  total_time = tms.tms_utime + tms.tms_stime;
949  return true;
950  }
951 
952  #endif
953 
954  return false; // unsupported system
955  }
956  //-----------------------------------------------------------------------------------------------------
957  uint8_t miner::get_percent_of_total(uint64_t other, uint64_t total)
958  {
959  return (uint8_t)( ceil( (other * 1.f / total * 1.f) * 100) );
960  }
961  //-----------------------------------------------------------------------------------------------------
962  boost::logic::tribool miner::on_battery_power()
963  {
964  #ifdef _WIN32
965 
966  SYSTEM_POWER_STATUS power_status;
967  if ( GetSystemPowerStatus( &power_status ) != 0 )
968  {
969  return boost::logic::tribool(power_status.ACLineStatus != 1);
970  }
971 
972  #elif defined(__APPLE__)
973 
974  #if TARGET_OS_MAC && (!defined(MAC_OS_X_VERSION_MIN_REQUIRED) || MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7)
975  return boost::logic::tribool(IOPSGetTimeRemainingEstimate() != kIOPSTimeRemainingUnlimited);
976  #else
977  // iOS or OSX <10.7
978  return boost::logic::tribool(boost::logic::indeterminate);
979  #endif
980 
981  #elif defined(__linux__)
982 
983  // Use the power_supply class http://lxr.linux.no/#linux+v4.10.1/Documentation/power/power_supply_class.txt
984  std::string power_supply_class_path = "/sys/class/power_supply";
985 
986  boost::tribool on_battery = boost::logic::tribool(boost::logic::indeterminate);
987  if (boost::filesystem::is_directory(power_supply_class_path))
988  {
989  const boost::filesystem::directory_iterator end_itr;
990  for (boost::filesystem::directory_iterator iter(power_supply_class_path); iter != end_itr; ++iter)
991  {
992  const boost::filesystem::path& power_supply_path = iter->path();
993  if (boost::filesystem::is_directory(power_supply_path))
994  {
995  boost::filesystem::path power_supply_type_path = power_supply_path / "type";
996  if (boost::filesystem::is_regular_file(power_supply_type_path))
997  {
998  std::ifstream power_supply_type_stream(power_supply_type_path.string());
999  if (power_supply_type_stream.fail())
1000  {
1001  LOG_PRINT_L0("Unable to read from " << power_supply_type_path << " to check power supply type");
1002  continue;
1003  }
1004 
1005  std::string power_supply_type;
1006  std::getline(power_supply_type_stream, power_supply_type);
1007 
1008  // If there is an AC adapter that's present and online we can break early
1009  if (boost::starts_with(power_supply_type, "Mains"))
1010  {
1011  boost::filesystem::path power_supply_online_path = power_supply_path / "online";
1012  if (boost::filesystem::is_regular_file(power_supply_online_path))
1013  {
1014  std::ifstream power_supply_online_stream(power_supply_online_path.string());
1015  if (power_supply_online_stream.fail())
1016  {
1017  LOG_PRINT_L0("Unable to read from " << power_supply_online_path << " to check ac power supply status");
1018  continue;
1019  }
1020 
1021  if (power_supply_online_stream.get() == '1')
1022  {
1023  return boost::logic::tribool(false);
1024  }
1025  }
1026  }
1027  else if (boost::starts_with(power_supply_type, "Battery") && boost::logic::indeterminate(on_battery))
1028  {
1029  boost::filesystem::path power_supply_status_path = power_supply_path / "status";
1030  if (boost::filesystem::is_regular_file(power_supply_status_path))
1031  {
1032  std::ifstream power_supply_status_stream(power_supply_status_path.string());
1033  if (power_supply_status_stream.fail())
1034  {
1035  LOG_PRINT_L0("Unable to read from " << power_supply_status_path << " to check battery power supply status");
1036  continue;
1037  }
1038 
1039  // Possible status are Charging, Full, Discharging, Not Charging, and Unknown
1040  // We are only need to handle negative states right now
1041  std::string power_supply_status;
1042  std::getline(power_supply_status_stream, power_supply_status);
1043  if (boost::starts_with(power_supply_status, "Charging") || boost::starts_with(power_supply_status, "Full"))
1044  {
1045  on_battery = boost::logic::tribool(false);
1046  }
1047 
1048  if (boost::starts_with(power_supply_status, "Discharging"))
1049  {
1050  on_battery = boost::logic::tribool(true);
1051  }
1052  }
1053  }
1054  }
1055  }
1056  }
1057  }
1058 
1059  if (boost::logic::indeterminate(on_battery))
1060  {
1061  static bool error_shown = false;
1062  if (!error_shown)
1063  {
1064  LOG_ERROR("couldn't query power status from " << power_supply_class_path);
1065  error_shown = true;
1066  }
1067  }
1068  return on_battery;
1069 
1070  #elif defined(__FreeBSD__)
1071  int ac;
1072  size_t n = sizeof(ac);
1073  if( sysctlbyname("hw.acpi.acline", &ac, &n, NULL, 0) == -1 )
1074  {
1075  if( errno != ENOENT )
1076  {
1077  LOG_ERROR("Cannot query battery status: "
1078  << "sysctlbyname(\"hw.acpi.acline\"): " << strerror(errno));
1079  return boost::logic::tribool(boost::logic::indeterminate);
1080  }
1081 
1082  // If sysctl fails with ENOENT, then try querying /dev/apm.
1083 
1084  static const char* dev_apm = "/dev/apm";
1085  const int fd = open(dev_apm, O_RDONLY);
1086  if( fd == -1 ) {
1087  LOG_ERROR("Cannot query battery status: "
1088  << "open(): " << dev_apm << ": " << strerror(errno));
1089  return boost::logic::tribool(boost::logic::indeterminate);
1090  }
1091 
1092  apm_info info;
1093  if( ioctl(fd, APMIO_GETINFO, &info) == -1 ) {
1094  close(fd);
1095  LOG_ERROR("Cannot query battery status: "
1096  << "ioctl(" << dev_apm << ", APMIO_GETINFO): " << strerror(errno));
1097  return boost::logic::tribool(boost::logic::indeterminate);
1098  }
1099 
1100  close(fd);
1101 
1102  // See apm(8).
1103  switch( info.ai_acline )
1104  {
1105  case 0: // off-line
1106  case 2: // backup power
1107  return boost::logic::tribool(true);
1108  case 1: // on-line
1109  return boost::logic::tribool(false);
1110  }
1111  switch( info.ai_batt_stat )
1112  {
1113  case 0: // high
1114  case 1: // low
1115  case 2: // critical
1116  return boost::logic::tribool(true);
1117  case 3: // charging
1118  return boost::logic::tribool(false);
1119  }
1120 
1121  LOG_ERROR("Cannot query battery status: "
1122  << "sysctl hw.acpi.acline is not available and /dev/apm returns "
1123  << "unexpected ac-line status (" << info.ai_acline << ") and "
1124  << "battery status (" << info.ai_batt_stat << ").");
1125  return boost::logic::tribool(boost::logic::indeterminate);
1126  }
1127  if( n != sizeof(ac) )
1128  {
1129  LOG_ERROR("sysctlbyname(\"hw.acpi.acline\") output is unexpectedly "
1130  << n << " bytes instead of the expected " << sizeof(ac) << " bytes.");
1131  return boost::logic::tribool(boost::logic::indeterminate);
1132  }
1133  return boost::logic::tribool(ac == 0);
1134  #endif
1135 
1136  LOG_ERROR("couldn't query power status");
1137  return boost::logic::tribool(boost::logic::indeterminate);
1138  }
1139 }
uint64_t height
Definition: blockchain.cpp:91
const account_public_address & get_mining_address() const
Definition: miner.cpp:357
static constexpr uint8_t BACKGROUND_MINING_MAX_IDLE_THRESHOLD_PERCENTAGE
Definition: miner.h:94
void on_synchronized()
Definition: miner.cpp:488
static constexpr uint8_t BACKGROUND_MINING_MIN_IDLE_THRESHOLD_PERCENTAGE
Definition: miner.h:93
bool is_mining() const
Definition: miner.cpp:352
uint64_t get_min_idle_seconds() const
Definition: miner.cpp:632
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:422
static void init_options(boost::program_options::options_description &desc)
Definition: miner.cpp:279
bool set_block_template(const block &bl, const difficulty_type &diffic, uint64_t height, uint64_t block_reward)
Definition: miner.cpp:136
uint32_t get_threads_count() const
Definition: miner.cpp:362
static constexpr uint8_t BACKGROUND_MINING_MAX_MINING_TARGET_PERCENTAGE
Definition: miner.h:100
void send_stop_signal()
Definition: miner.cpp:432
bool get_ignore_battery() const
Definition: miner.cpp:609
bool set_min_idle_seconds(uint64_t min_idle_seconds)
Definition: miner.cpp:637
bool on_block_chain_update()
Definition: miner.cpp:148
bool get_is_background_mining_enabled() const
Definition: miner.cpp:604
bool init(const boost::program_options::variables_map &vm, network_type nettype, bool fallback_to_pow=false)
Definition: miner.cpp:291
static bool find_nonce_for_given_block(block &bl, const difficulty_type &diffic, uint64_t height)
Definition: miner.cpp:471
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:645
bool start(const account_public_address &adr, size_t threads_count, bool do_background=false, bool ignore_battery=false)
Definition: miner.cpp:366
void do_print_hashrate(bool do_hr)
Definition: miner.cpp:198
bool on_idle()
Definition: miner.cpp:178
uint8_t get_mining_target() const
Definition: miner.cpp:658
bool set_idle_threshold(uint8_t idle_threshold)
Definition: miner.cpp:650
bool set_mining_target(uint8_t mining_target)
Definition: miner.cpp:663
static constexpr uint8_t BACKGROUND_MINING_MIN_MINING_TARGET_PERCENTAGE
Definition: miner.h:99
bool do_call(functor_t functr)
Definition: math_helper.h:263
#define THREAD_STACK_SIZE
#define MINER_CONFIG_FILE_NAME
#define AUTODETECT_GAIN_THRESHOLD
Definition: miner.cpp:78
#define AUTODETECT_WINDOW
Definition: miner.cpp:77
void slow_hash_allocate_state()
void slow_hash_free_state()
#define AUTO_VAL_INIT(v)
Definition: misc_language.h:53
#define MERROR(x)
Definition: misc_log_ex.h:73
#define MGINFO_GREEN(x)
Definition: misc_log_ex.h:82
#define MDEBUG(x)
Definition: misc_log_ex.h:76
#define ENDL
Definition: misc_log_ex.h:149
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition: misc_log_ex.h:181
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
#define MGINFO(x)
Definition: misc_log_ex.h:80
#define MTRACE(x)
Definition: misc_log_ex.h:77
#define MINFO(x)
Definition: misc_log_ex.h:75
#define LOG_PRINT_L2(x)
Definition: misc_log_ex.h:101
#define MLOG_SET_THREAD_NAME(x)
Definition: misc_log_ex.h:115
#define LOG_PRINT_L0(x)
Definition: misc_log_ex.h:99
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::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
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true > &arg)
Definition: command_line.h:271
POD_CLASS hash
Definition: hash.h:50
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
boost::multiprecision::uint128_t difficulty_type
Definition: difficulty.h:43
bool get_block_hash(const block &b, crypto::hash &res)
bool get_account_address_from_str(address_parse_info &info, network_type nettype, std::string const &str)
bool check_hash(const crypto::hash &hash, difficulty_type difficulty)
Definition: difficulty.cpp:203
bool get_block_longhash(const block &b, crypto::hash &res, uint64_t height)
std::string blobdata
Definition: blobdatatype.h:39
bool load_file_to_string(const std::string &path_to_file, std::string &target_str, size_t max_size=1000000000)
bool is_file_exist(const std::string &path)
Definition: file_io_utils.h:66
uint64_t get_tick_count()
bool sleep_no_w(long ms)
uint64_t get_ns_count()
std::string to_string(t_connection_type type)
bool store_t_to_json_file(t_struct &str_in, const std::string &fpath)
bool load_t_from_json_file(t_struct &out, const std::string &json_file)
std::string base64_decode(std::string const &encoded_string)
std::string & trim(std::string &str)
Definition: string_tools.h:288
mdb_size_t count(MDB_cursor *cur)
::std::string string
Definition: gtest-port.h:1097
CXA_THROW_INFO_T * info
Definition: stack_trace.cpp:91
int bool
Definition: stdbool.h:36
#define false
Definition: stdbool.h:38
signed __int64 int64_t
Definition: stdint.h:135
unsigned int uint32_t
Definition: stdint.h:126
unsigned char uint8_t
Definition: stdint.h:124
unsigned __int64 uint64_t
Definition: stdint.h:136
std::vector< uint8_t > signature
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
#define CRITICAL_REGION_LOCAL(x)
Definition: syncobj.h:228
#define CRITICAL_REGION_END()
Definition: syncobj.h:233
#define CRITICAL_REGION_BEGIN(x)
Definition: syncobj.h:229