diff --git a/src/currency_core/currency_core.cpp b/src/currency_core/currency_core.cpp index 8d29c38d..37e67e93 100644 --- a/src/currency_core/currency_core.cpp +++ b/src/currency_core/currency_core.cpp @@ -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()) +#endif + //, m_miner_address(boost::value_initialized()) , 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& 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; } diff --git a/src/currency_core/currency_core.h b/src/currency_core/currency_core.h index 32f62b0d..a7df6051 100644 --- a/src/currency_core/currency_core.h +++ b/src/currency_core/currency_core.h @@ -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; diff --git a/src/currency_core/miner.cpp b/src/currency_core/miner.cpp index c0cbd789..dc37f3f6 100644 --- a/src/currency_core/miner.cpp +++ b/src/currency_core/miner.cpp @@ -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 diff --git a/src/currency_core/miner.h b/src/currency_core/miner.h index 6b3886d1..674bb4d2 100644 --- a/src/currency_core/miner.h +++ b/src/currency_core/miner.h @@ -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 #include #include @@ -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::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::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 diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp index 604c0bbe..4e5d0de0 100644 --- a/src/daemon/daemon.cpp +++ b/src/daemon/daemon.cpp @@ -174,7 +174,9 @@ int main(int argc, char* argv[]) currency::core_rpc_server::init_options(desc_cmd_sett); typedef nodetool::node_server > 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); diff --git a/src/wallet/wallets_manager.cpp b/src/wallet/wallets_manager.cpp index b51f929f..caa6c9ef 100644 --- a/src/wallet/wallets_manager.cpp +++ b/src/wallet/wallets_manager.cpp @@ -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 >::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 diff --git a/tests/core_tests/alias_tests.cpp b/tests/core_tests/alias_tests.cpp index b8a1e4fd..704702cc 100644 --- a/tests/core_tests/alias_tests.cpp +++ b/tests/core_tests/alias_tests.cpp @@ -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); diff --git a/tests/core_tests/chaingen.cpp b/tests/core_tests/chaingen.cpp index 2679dd39..cfcd8376 100644 --- a/tests/core_tests/chaingen.cpp +++ b/tests/core_tests/chaingen.cpp @@ -836,7 +836,7 @@ bool test_generator::find_nounce(currency::block& blk, std::vector& 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++; }*/ diff --git a/tests/core_tests/chaingen_helpers.h b/tests/core_tests/chaingen_helpers.h index 703aec6f..f4033fae 100644 --- a/tests/core_tests/chaingen_helpers.h +++ b/tests/core_tests/chaingen_helpers.h @@ -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{}; diff --git a/tests/core_tests/emission_test.cpp b/tests/core_tests/emission_test.cpp index 035726ca..7bd66823 100644 --- a/tests/core_tests/emission_test.cpp +++ b/tests/core_tests/emission_test.cpp @@ -69,7 +69,7 @@ bool emission_test::c1(currency::core& c, size_t ev_index, const std::vector