diff --git a/src/currency_core/miner.cpp b/src/currency_core/miner.cpp index d52e51e2..109d9966 100644 --- a/src/currency_core/miner.cpp +++ b/src/currency_core/miner.cpp @@ -40,24 +40,25 @@ namespace currency } - miner::miner(i_miner_handler* phandler, blockchain_storage& bc):m_stop(1), - //m_bc(bc), - m_template(boost::value_initialized()), - m_template_no(0), - m_diffic(0), - m_thread_index(0), - m_phandler(phandler), - m_height(0), - m_pausers_count(0), - m_threads_total(0), - m_starter_nonce(0), - m_do_print_hashrate(false), - m_do_mining(false), - m_current_hash_rate(0), - m_last_hr_merge_time(0), - m_hashes(0), - m_config(AUTO_VAL_INIT(m_config)), - m_mine_address{} + miner::miner(i_miner_handler* phandler, blockchain_storage& bc) + : m_stop(1) + , m_template(boost::value_initialized()) + , m_template_no(0) + , m_diffic(0) + , m_thread_index(0) + , m_phandler(phandler) + , m_height(0) + , m_pausers_count(0) + , m_block_template_ready(false) + , m_threads_total(0) + , m_starter_nonce(0) + , m_do_print_hashrate(false) + , m_do_mining(false) + , m_current_hash_rate(0) + , m_last_hr_merge_time(0) + , m_hashes(0) + , m_config(AUTO_VAL_INIT(m_config)) + , m_mine_address{} { } //----------------------------------------------------------------------------------------------------- @@ -76,6 +77,7 @@ namespace currency m_height = height; ++m_template_no; m_starter_nonce = crypto::rand(); + m_block_template_ready = true; return true; } //----------------------------------------------------------------------------------------------------- @@ -100,7 +102,8 @@ namespace currency } if(!m_phandler->get_block_template(bl, m_mine_address, m_mine_address, di, height, extra_nonce)) { - LOG_ERROR("Failed to get_block_template()"); + // it's quite possible that block template cannot be created at particular time; need to wait + m_block_template_ready = false; return false; } set_block_template(bl, di, height); @@ -110,7 +113,8 @@ namespace currency bool miner::on_idle() { m_update_block_template_interval.do_call([&](){ - if(is_mining())request_block_template(); + if(is_mining()) + request_block_template(); return true; }); @@ -233,8 +237,13 @@ namespace currency return false; } - if(!m_template_no) - request_block_template();//lets update block template + while(!m_template_no) + { + if (request_block_template()) + break; + std::this_thread::sleep_for(std::chrono::seconds(2)); + LOG_PRINT_L1("Trying to get block template...."); + } boost::interprocess::ipcdetail::atomic_write32(&m_stop, 0); boost::interprocess::ipcdetail::atomic_write32(&m_thread_index, 0); @@ -313,12 +322,11 @@ namespace currency uint64_t local_height = 0; crypto::hash local_blob_data_hash = null_hash; - //uint64_t local_template_height = 0; - block b; + block b{}; while(!m_stop) { - if(m_pausers_count)//anti split workaround + if(m_pausers_count != 0 || !m_block_template_ready) { misc_utils::sleep_no_w(100); continue; diff --git a/src/currency_core/miner.h b/src/currency_core/miner.h index 94caea2b..c8a37225 100644 --- a/src/currency_core/miner.h +++ b/src/currency_core/miner.h @@ -101,6 +101,7 @@ namespace currency volatile uint32_t m_thread_index; volatile uint32_t m_threads_total; std::atomic m_pausers_count; + std::atomic m_block_template_ready; epee::critical_section m_miners_count_lock; std::list m_threads;