1
0
Fork 0
forked from lthn/blockchain

cpu miner minor improvements (sounds nice)

This commit is contained in:
sowle 2024-10-23 22:14:10 +02:00
parent 2b15aad320
commit 6087a1bd40
No known key found for this signature in database
GPG key ID: C07A24B2D89D49FC
2 changed files with 34 additions and 25 deletions

View file

@ -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<block>()),
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<block>())
, 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<uint32_t>();
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;

View file

@ -101,6 +101,7 @@ namespace currency
volatile uint32_t m_thread_index;
volatile uint32_t m_threads_total;
std::atomic<int32_t> m_pausers_count;
std::atomic<bool> m_block_template_ready;
epee::critical_section m_miners_count_lock;
std::list<boost::thread> m_threads;