1
0
Fork 0
forked from lthn/blockchain

2nd iteration on diminishing CPU mining presence in mainnet builds

This commit is contained in:
sowle 2025-01-24 04:41:55 +01:00
parent f6d363d313
commit 38e6556a49
No known key found for this signature in database
GPG key ID: C07A24B2D89D49FC
12 changed files with 73 additions and 43 deletions

View file

@ -1,4 +1,4 @@
// Copyright (c) 2014-2018 Zano Project
// Copyright (c) 2014-2025 Zano Project
// Copyright (c) 2014-2018 The Louisdor Project
// Copyright (c) 2012-2013 The Cryptonote developers
// Copyright (c) 2012-2013 The Boolberry developers
@ -34,8 +34,10 @@ namespace currency
core::core(i_currency_protocol* pprotocol)
: m_mempool(m_blockchain_storage, pprotocol)
, m_blockchain_storage(m_mempool)
#ifdef CPU_MINING_ENABLED
, m_miner(this, m_blockchain_storage)
, m_miner_address(boost::value_initialized<account_public_address>())
#endif
//, m_miner_address(boost::value_initialized<account_public_address>())
, m_starter_message_showed(false)
, m_critical_error_handler(nullptr)
, m_stop_after_height(0)
@ -165,8 +167,10 @@ namespace currency
m_mempool.remove_incompatible_txs();
#ifdef CPU_MINING_ENABLED
r = m_miner.init(vm);
CHECK_AND_ASSERT_MES(r, false, "Failed to initialize miner");
#endif
//check if tx_pool module synchronized with blockchaine storage
// if (m_blockchain_storage.get_top_block_id() != m_mempool.get_last_core_hash())
@ -193,8 +197,10 @@ namespace currency
{
//m_mempool.set_last_core_hash(m_blockchain_storage.get_top_block_id());
#ifdef CPU_MINING_ENABLED
m_miner.stop();
m_miner.deinit();
#endif
m_mempool.deinit();
m_blockchain_storage.deinit();
return true;
@ -274,7 +280,9 @@ namespace currency
//-----------------------------------------------------------------------------------------------
bool core::get_stat_info(const core_stat_info::params& pr, core_stat_info& st_inf)
{
#ifdef CPU_MINING_ENABLED
st_inf.mining_speed = m_miner.get_speed();
#endif
st_inf.alternative_blocks = m_blockchain_storage.get_alternative_blocks_count();
st_inf.blockchain_height = m_blockchain_storage.get_current_blockchain_size();
st_inf.tx_pool_size = m_mempool.get_transactions_count();
@ -405,12 +413,16 @@ namespace currency
//-----------------------------------------------------------------------------------------------
void core::pause_mine()
{
#ifdef CPU_MINING_ENABLED
m_miner.pause();
#endif
}
//-----------------------------------------------------------------------------------------------
void core::resume_mine()
{
#ifdef CPU_MINING_ENABLED
m_miner.resume();
#endif
}
//-----------------------------------------------------------------------------------------------
bool core::handle_block_found(const block& b, block_verification_context* p_verification_result, bool need_update_miner_block_template)
@ -422,10 +434,10 @@ namespace currency
if (!p_verification_result)
p_verification_result = &bvc;
m_miner.pause();
pause_mine();
misc_utils::auto_scope_leave_caller scope_exit_handler = misc_utils::create_scope_leave_handler([this]()
{
m_miner.resume();
resume_mine();
});
TIME_MEASURE_START_MS(time_add_new_block_ms);
@ -520,7 +532,9 @@ namespace currency
//-----------------------------------------------------------------------------------------------
void core::on_synchronized()
{
#ifdef CPU_MINING_ENABLED
m_miner.on_synchronized();
#endif
}
bool core::get_backward_blocks_sizes(uint64_t from_height, std::vector<size_t>& sizes, size_t count)
{
@ -685,7 +699,9 @@ namespace currency
bool core::update_miner_block_template()
{
notify_blockchain_update_listeners();
#ifdef CPU_MINING_ENABLED
m_miner.on_block_chain_update();
#endif
return true;
}
//-----------------------------------------------------------------------------------------------
@ -708,7 +724,9 @@ namespace currency
m_prune_alt_blocks_interval.do_call([this](){return m_blockchain_storage.prune_aged_alt_blocks();});
m_check_free_space_interval.do_call([this](){ check_free_space(); return true; });
#ifdef CPU_MINING_ENABLED
m_miner.on_idle();
#endif
m_mempool.on_idle();
return true;
}

View file

@ -1,4 +1,4 @@
// Copyright (c) 2014-2018 Zano Project
// Copyright (c) 2014-2025 Zano Project
// Copyright (c) 2014-2018 The Louisdor Project
// Copyright (c) 2012-2013 The Cryptonote developers
// Copyright (c) 2012-2013 The Boolberry developers
@ -57,7 +57,9 @@ namespace currency
virtual bool get_block_template(const create_block_template_params& params, create_block_template_response& resp);
bool get_block_template(block& b, const account_public_address& adr, const account_public_address& stakeholder_address, wide_difficulty_type& diffic, uint64_t& height, const blobdata& ex_nonce, bool pos = false, const pos_entry& pe = pos_entry());
#ifdef CPU_MINING_ENABLED
miner& get_miner(){ return m_miner; }
#endif
static void init_options(boost::program_options::options_description& desc);
bool init(const boost::program_options::variables_map& vm);
bool set_genesis_block(const block& b);
@ -141,8 +143,10 @@ namespace currency
i_currency_protocol* m_pprotocol;
i_critical_error_handler* m_critical_error_handler;
epee::critical_section m_incoming_tx_lock;
#ifdef CPU_MINING_ENABLED
miner m_miner;
account_public_address m_miner_address;
#endif
//account_public_address m_miner_address;
std::string m_config_folder;
uint64_t m_stop_after_height;
currency_protocol_stub m_protocol_stub;

View file

@ -36,17 +36,18 @@ namespace currency
// CPU mining disabled
// currency::miner stub implementation
/*
miner::miner(i_miner_handler* phandler, blockchain_storage& bc)
{}
miner::~miner()
{}
bool miner::init(const boost::program_options::variables_map& vm)
{
return false;
return true;
}
bool miner::deinit()
{
return false;
return true;
}
void miner::init_options(boost::program_options::options_description& desc)
{}
@ -84,7 +85,7 @@ namespace currency
}
// end of currency::miner stub implementation
*/
#else

View file

@ -1,4 +1,4 @@
// Copyright (c) 2014-2018 Zano Project
// Copyright (c) 2014-2025 Zano Project
// Copyright (c) 2014-2018 The Louisdor Project
// Copyright (c) 2012-2013 The Cryptonote developers
// Copyright (c) 2012-2013 The Boolberry developers
@ -11,7 +11,6 @@
#define CPU_MINING_ENABLED // disable CPU mining capabilities in mainnet
#endif // #ifndef TESTNET
#include <boost/atomic.hpp>
#include <boost/program_options.hpp>
#include <atomic>
@ -35,6 +34,29 @@ namespace currency
~i_miner_handler(){};
};
inline
static bool find_nonce_for_given_block(block& bl, const wide_difficulty_type& diffic, uint64_t height)
{
bl.nonce = 0;
blobdata bd = get_block_hashing_blob(bl);
crypto::hash bd_hash = crypto::cn_fast_hash(bd.data(), bd.size());
//uint64_t& nonce_ref = access_nonce_in_block_blob(bd);
//nonce_ref = 0;
for(; bl.nonce != std::numeric_limits<uint64_t>::max(); bl.nonce++)
{
crypto::hash h = get_block_longhash(height, bd_hash, bl.nonce);
if(check_hash(h, diffic))
{
LOG_PRINT_L1("Found nonce for block: " << get_block_hash(bl) << "[" << height << "]: PoW:" << h << " (diff:" << diffic << "), ts: " << bl.timestamp);
return true;
}
}
return false;
}
#ifdef CPU_MINING_ENABLED
/************************************************************************/
/* */
/************************************************************************/
@ -59,27 +81,6 @@ namespace currency
void resume();
void do_print_hashrate(bool do_hr);
inline
static bool find_nonce_for_given_block(block& bl, const wide_difficulty_type& diffic, uint64_t height)
{
bl.nonce = 0;
blobdata bd = get_block_hashing_blob(bl);
crypto::hash bd_hash = crypto::cn_fast_hash(bd.data(), bd.size());
//uint64_t& nonce_ref = access_nonce_in_block_blob(bd);
//nonce_ref = 0;
for(; bl.nonce != std::numeric_limits<uint64_t>::max(); bl.nonce++)
{
crypto::hash h = get_block_longhash(height, bd_hash, bl.nonce);
if(check_hash(h, diffic))
{
LOG_PRINT_L1("Found nonce for block: " << get_block_hash(bl) << "[" << height << "]: PoW:" << h << " (diff:" << diffic << "), ts: " << bl.timestamp);
return true;
}
}
return false;
}
private:
bool set_block_template(const block& bl, const wide_difficulty_type& diffic, uint64_t height);
bool worker_thread();
@ -127,5 +128,7 @@ namespace currency
bool m_do_mining;
};
}
#endif // #ifdef CPU_MINING_ENABLED
} // namespace currency

View file

@ -174,7 +174,9 @@ int main(int argc, char* argv[])
currency::core_rpc_server::init_options(desc_cmd_sett);
typedef nodetool::node_server<currency::t_currency_protocol_handler<currency::core> > p2psrv_t;
p2psrv_t::init_options(desc_cmd_sett);
#ifdef CPU_MINING_ENABLED
currency::miner::init_options(desc_cmd_sett);
#endif
bc_services::bc_offers_service::init_options(desc_cmd_sett);
currency::stratum_server::init_options(desc_cmd_sett);
tools::db::db_backend_selector::init_options(desc_cmd_sett);

View file

@ -201,7 +201,9 @@ bool wallets_manager::init_command_line(int argc, char* argv[], std::string& fai
currency::core::init_options(desc_cmd_sett);
currency::core_rpc_server::init_options(desc_cmd_sett);
nodetool::node_server<currency::t_currency_protocol_handler<currency::core> >::init_options(desc_cmd_sett);
#ifdef CPU_MINING_ENABLED
currency::miner::init_options(desc_cmd_sett);
#endif
bc_services::bc_offers_service::init_options(desc_cmd_sett);
tools::db::db_backend_selector::init_options(desc_cmd_sett);
#endif

View file

@ -381,7 +381,7 @@ bool gen_alias_tests::check_too_many_aliases_registration(currency::core& c, siz
CHECK_AND_ASSERT_MES(c.get_pool_transactions_count() == total_alias_to_gen, false, "Unexpected number of txs in the pool: " << c.get_pool_transactions_count() << ", expected: " << total_alias_to_gen);
// complete block template and try to process it
r = miner::find_nonce_for_given_block(b, diff, height);
r = find_nonce_for_given_block(b, diff, height);
CHECK_AND_ASSERT_MES(r, false, "find_nonce_for_given_block failed");
currency::block_verification_context bvc = AUTO_VAL_INIT(bvc);
@ -1317,7 +1317,7 @@ bool gen_alias_switch_and_check_block_template::add_block_from_template(currency
bool r = c.get_block_template(b, acc.get_public_address(), acc.get_public_address(), diff, height, extra);
CHECK_AND_ASSERT_MES(r, false, "get_block_template failed");
r = miner::find_nonce_for_given_block(b, diff, height);
r = find_nonce_for_given_block(b, diff, height);
CHECK_AND_ASSERT_MES(r, false, "find_nonce_for_given_block failed");
currency::block_verification_context bvc = AUTO_VAL_INIT(bvc);
@ -1439,7 +1439,7 @@ bool gen_alias_too_many_regs_in_block_template::add_block_from_template(currency
bool r = c.get_block_template(b, acc.get_public_address(), acc.get_public_address(), diff, height, extra);
CHECK_AND_ASSERT_MES(r, false, "get_block_template failed");
r = miner::find_nonce_for_given_block(b, diff, height);
r = find_nonce_for_given_block(b, diff, height);
CHECK_AND_ASSERT_MES(r, false, "find_nonce_for_given_block failed");
currency::block_verification_context bvc = AUTO_VAL_INIT(bvc);

View file

@ -836,7 +836,7 @@ bool test_generator::find_nounce(currency::block& blk, std::vector<const block_i
if(height != blocks.size())
throw std::runtime_error("wrong height in find_nounce");
return miner::find_nonce_for_given_block(blk, dif, height);
return find_nonce_for_given_block(blk, dif, height);
}
bool test_generator::construct_genesis_block(currency::block& blk, const currency::account_base& miner_acc, uint64_t timestamp)
@ -1647,7 +1647,7 @@ bool fill_tx_sources_and_destinations(const std::vector<test_event_entry>& event
void fill_nonce(currency::block& blk, const wide_difficulty_type& diffic, uint64_t height)
{
blk.nonce = 0;
while (!miner::find_nonce_for_given_block(blk, diffic, height))
while (!find_nonce_for_given_block(blk, diffic, height))
blk.timestamp++;
}*/

View file

@ -33,7 +33,7 @@ inline bool mine_next_pow_block_in_playtime(const currency::account_public_addre
test_core_time::adjust(b.timestamp);
modify_block_cb(b);
r = currency::miner::find_nonce_for_given_block(b, cbtr.diffic, cbtr.height);
r = currency::find_nonce_for_given_block(b, cbtr.diffic, cbtr.height);
CHECK_AND_ASSERT_MES(r, false, "find_nonce_for_given_block failed");
currency::block_verification_context bvc{};
@ -119,7 +119,7 @@ inline bool mine_next_pow_block_in_playtime_with_given_txs(const currency::accou
height = cbtr.height;
}
r = currency::miner::find_nonce_for_given_block(b, cbtr.diffic, cbtr.height);
r = currency::find_nonce_for_given_block(b, cbtr.diffic, cbtr.height);
CHECK_AND_ASSERT_MES(r, false, "find_nonce_for_given_block failed");
currency::block_verification_context bvc{};

View file

@ -69,7 +69,7 @@ bool emission_test::c1(currency::core& c, size_t ev_index, const std::vector<tes
uint64_t height_from_template = 0;
r = c.get_block_template(b, m_miner_acc.get_public_address(), m_miner_acc.get_public_address(), difficulty, height_from_template, extra);
CHECK_AND_ASSERT_MES(r || height_from_template != height, false, "get_block_template failed");
r = miner::find_nonce_for_given_block(b, difficulty, height);
r = find_nonce_for_given_block(b, difficulty, height);
CHECK_AND_ASSERT_MES(r, false, "find_nonce_for_given_block failed");
c.handle_incoming_block(t_serializable_object_to_blob(b), bvc);
CHECK_AND_NO_ASSERT_MES(!bvc.m_verification_failed && !bvc.m_marked_as_orphaned && !bvc.m_already_exists, false, "block verification context check failed");

View file

@ -239,7 +239,7 @@ bool hard_fork_1_unlock_time_2_in_coinbase::generate(std::vector<test_event_entr
ut2.unlock_time_array.resize(blk_5.miner_tx.vout.size());
ut2.unlock_time_array[0] = get_block_height(blk_5) + CURRENCY_MINED_MONEY_UNLOCK_WINDOW;
blk_5.miner_tx.extra.push_back(ut2);
miner::find_nonce_for_given_block(blk_5, diff, get_block_height(blk_5));
find_nonce_for_given_block(blk_5, diff, get_block_height(blk_5));
// add blk_5 with modified miner tx
events.push_back(blk_5);

View file

@ -155,7 +155,7 @@ bool generate_events(currency::core& c, cct_events_t& events, const cct_wallets_
test_core_time::adjust(b.timestamp);
currency::wide_difficulty_type diff = 0;
r = currency::miner::find_nonce_for_given_block(b, diff, height);
r = currency::find_nonce_for_given_block(b, diff, height);
CHECK_AND_ASSERT_MES(r, false, "find_nonce_for_given_block failed");
currency::block_verification_context bvc = AUTO_VAL_INIT(bvc);