diff --git a/src/miner.cpp b/src/miner.cpp index fb53b947..0053f9b1 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -245,7 +245,7 @@ void Miner::run(WorkerData* data) for (const AuxChainData& aux_data : j.m_auxChains) { if (aux_data.difficulty.check_pow(h)) { LOGINFO(0, log::Green() << "AUX BLOCK FOUND: chain_id " << aux_data.unique_id << ", diff " << j.m_auxDiff << ", worker thread " << data->m_index << '/' << data->m_count); - m_pool->submit_aux_block(aux_data.unique_id, j.m_templateId, j.m_nonce, j.m_extraNonce); + m_pool->submit_aux_block_async(aux_data.unique_id, j.m_templateId, j.m_nonce, j.m_extraNonce); } } } diff --git a/src/p2pool.cpp b/src/p2pool.cpp index 8de0c0bb..a42f9d07 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -114,6 +114,13 @@ p2pool::p2pool(int argc, char* argv[]) } m_submitBlockAsync.data = this; + err = uv_async_init(uv_default_loop_checked(), &m_submitAuxBlockAsync, on_submit_aux_block); + if (err) { + LOGERR(1, "uv_async_init failed, error " << uv_err_name(err)); + throw std::exception(); + } + m_submitAuxBlockAsync.data = this; + err = uv_async_init(uv_default_loop_checked(), &m_blockTemplateAsync, on_update_block_template); if (err) { LOGERR(1, "uv_async_init failed, error " << uv_err_name(err)); @@ -145,6 +152,7 @@ p2pool::p2pool(int argc, char* argv[]) uv_mutex_init_checked(&m_minerLock); #endif uv_mutex_init_checked(&m_submitBlockDataLock); + uv_mutex_init_checked(&m_submitAuxBlockDataLock); m_api = p->m_apiPath.empty() ? nullptr : new p2pool_api(p->m_apiPath, p->m_localStats); @@ -215,6 +223,7 @@ p2pool::~p2pool() uv_mutex_destroy(&m_minerLock); #endif uv_mutex_destroy(&m_submitBlockDataLock); + uv_mutex_destroy(&m_submitAuxBlockDataLock); delete m_api; delete m_sideChain; @@ -627,69 +636,102 @@ void p2pool::submit_block_async(std::vector&& blob) } } -void p2pool::submit_aux_block(const hash& chain_id, uint32_t template_id, uint32_t nonce, uint32_t extra_nonce) const +void p2pool::submit_aux_block_async(const hash& chain_id, uint32_t template_id, uint32_t nonce, uint32_t extra_nonce) { - LOGINFO(3, "submit_aux_block: template id = " << template_id << ", chain_id = " << chain_id << ", nonce = " << nonce << ", extra_nonce = " << extra_nonce); + { + MutexLock lock(m_submitAuxBlockDataLock); + m_submitAuxBlockData.emplace_back(SubmitAuxBlockData{ chain_id, template_id, nonce, extra_nonce }); + } - size_t nonce_offset = 0; - size_t extra_nonce_offset = 0; - size_t merkle_root_offset = 0; - root_hash merge_mining_root; - const BlockTemplate* block_tpl = nullptr; + // If p2pool is stopped, m_submitAuxBlockAsync is most likely already closed + if (m_stopped) { + LOGWARN(0, "p2pool is shutting down, but a block was found. Trying to submit it anyway!"); + submit_aux_block(); + return; + } - std::vector blob = m_blockTemplate->get_block_template_blob(template_id, extra_nonce, nonce_offset, extra_nonce_offset, merkle_root_offset, merge_mining_root, &block_tpl); + const int err = uv_async_send(&m_submitAuxBlockAsync); + if (err) { + LOGERR(1, "uv_async_send failed, error " << uv_err_name(err)); + } +} - uint8_t hashing_blob[128] = {}; - uint64_t height = 0; - difficulty_type diff, aux_diff, sidechain_diff; - hash seed_hash; +void p2pool::submit_aux_block() const +{ + std::vector submit_data; + { + MutexLock lock(m_submitAuxBlockDataLock); + m_submitAuxBlockData.swap(submit_data); + } - m_blockTemplate->get_hashing_blob(template_id, extra_nonce, hashing_blob, height, diff, aux_diff, sidechain_diff, seed_hash, nonce_offset); + for (size_t i = 0; i < submit_data.size(); ++i) { + const hash chain_id = submit_data[i].chain_id; + const uint32_t template_id = submit_data[i].template_id; + const uint32_t nonce = submit_data[i].nonce; + const uint32_t extra_nonce = submit_data[i].extra_nonce; - if (blob.empty()) { - LOGWARN(3, "submit_aux_block: block template blob not found"); - return; - } + LOGINFO(3, "submit_aux_block: template id = " << template_id << ", chain_id = " << chain_id << ", nonce = " << nonce << ", extra_nonce = " << extra_nonce); - uint8_t* p = blob.data(); - memcpy(p + nonce_offset, &nonce, NONCE_SIZE); - memcpy(p + extra_nonce_offset, &extra_nonce, EXTRA_NONCE_SIZE); - memcpy(p + merkle_root_offset, merge_mining_root.h, HASH_SIZE); + size_t nonce_offset = 0; + size_t extra_nonce_offset = 0; + size_t merkle_root_offset = 0; + root_hash merge_mining_root; + const BlockTemplate* block_tpl = nullptr; - ReadLock lock(m_mergeMiningClientsLock); + std::vector blob = m_blockTemplate->get_block_template_blob(template_id, extra_nonce, nonce_offset, extra_nonce_offset, merkle_root_offset, merge_mining_root, &block_tpl); - IMergeMiningClient::ChainParameters params; + uint8_t hashing_blob[128] = {}; + uint64_t height = 0; + difficulty_type diff, aux_diff, sidechain_diff; + hash seed_hash; - for (IMergeMiningClient* c : m_mergeMiningClients) { - if (!c->get_params(params)) { - continue; + m_blockTemplate->get_hashing_blob(template_id, extra_nonce, hashing_blob, height, diff, aux_diff, sidechain_diff, seed_hash, nonce_offset); + + if (blob.empty()) { + LOGWARN(3, "submit_aux_block: block template blob not found"); + return; } - if (chain_id == params.aux_id) { - std::vector proof; - uint32_t path; + uint8_t* p = blob.data(); + memcpy(p + nonce_offset, &nonce, NONCE_SIZE); + memcpy(p + extra_nonce_offset, &extra_nonce, EXTRA_NONCE_SIZE); + memcpy(p + merkle_root_offset, merge_mining_root.h, HASH_SIZE); - if (m_blockTemplate->get_aux_proof(template_id, extra_nonce, params.aux_hash, proof, path)) { - if (pool_block_debug()) { - const MinerData data = miner_data(); - const uint32_t n_aux_chains = static_cast(data.aux_chains.size() + 1); - const uint32_t index = get_aux_slot(params.aux_id, data.aux_nonce, n_aux_chains); + ReadLock lock(m_mergeMiningClientsLock); - if (!verify_merkle_proof(params.aux_hash, proof, index, n_aux_chains, merge_mining_root)) { - LOGERR(0, "submit_aux_block: verify_merkle_proof (1) failed for chain_id " << chain_id); - } - if (!verify_merkle_proof(params.aux_hash, proof, path, merge_mining_root)) { - LOGERR(0, "submit_aux_block: verify_merkle_proof (2) failed for chain_id " << chain_id); + IMergeMiningClient::ChainParameters params; + + for (IMergeMiningClient* c : m_mergeMiningClients) { + if (!c->get_params(params)) { + continue; + } + + if (chain_id == params.aux_id) { + std::vector proof; + uint32_t path; + + if (m_blockTemplate->get_aux_proof(template_id, extra_nonce, params.aux_hash, proof, path)) { + if (pool_block_debug()) { + const MinerData data = miner_data(); + const uint32_t n_aux_chains = static_cast(data.aux_chains.size() + 1); + const uint32_t index = get_aux_slot(params.aux_id, data.aux_nonce, n_aux_chains); + + if (!verify_merkle_proof(params.aux_hash, proof, index, n_aux_chains, merge_mining_root)) { + LOGERR(0, "submit_aux_block: verify_merkle_proof (1) failed for chain_id " << chain_id); + } + if (!verify_merkle_proof(params.aux_hash, proof, path, merge_mining_root)) { + LOGERR(0, "submit_aux_block: verify_merkle_proof (2) failed for chain_id " << chain_id); + } } + + c->submit_solution(block_tpl, hashing_blob, nonce_offset, seed_hash, blob, proof, path); + } + else { + LOGWARN(3, "submit_aux_block: failed to get merkle proof for chain_id " << chain_id); } - c->submit_solution(block_tpl, hashing_blob, nonce_offset, seed_hash, blob, proof, path); - } - else { - LOGWARN(3, "submit_aux_block: failed to get merkle proof for chain_id " << chain_id); + return; } - - return; } } } @@ -707,6 +749,7 @@ void p2pool::on_stop(uv_async_t* async) } uv_close(reinterpret_cast(&pool->m_submitBlockAsync), nullptr); + uv_close(reinterpret_cast(&pool->m_submitAuxBlockAsync), nullptr); uv_close(reinterpret_cast(&pool->m_blockTemplateAsync), nullptr); uv_close(reinterpret_cast(&pool->m_stopAsync), nullptr); uv_close(reinterpret_cast(&pool->m_reconnectToHostAsync), nullptr); diff --git a/src/p2pool.h b/src/p2pool.h index ffa213b1..e455baf7 100644 --- a/src/p2pool.h +++ b/src/p2pool.h @@ -89,7 +89,7 @@ class p2pool : public MinerCallbackHandler, public nocopy_nomove void submit_block_async(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce); void submit_block_async(std::vector&& blob); - void submit_aux_block(const hash& chain_id, uint32_t template_id, uint32_t nonce, uint32_t extra_nonce) const; + void submit_aux_block_async(const hash& chain_id, uint32_t template_id, uint32_t nonce, uint32_t extra_nonce); bool submit_sidechain_block(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce); @@ -123,11 +123,13 @@ class p2pool : public MinerCallbackHandler, public nocopy_nomove const Params::Host& switch_host(); static void on_submit_block(uv_async_t* async) { reinterpret_cast(async->data)->submit_block(); } + static void on_submit_aux_block(uv_async_t* async) { reinterpret_cast(async->data)->submit_aux_block(); } static void on_update_block_template(uv_async_t* async) { reinterpret_cast(async->data)->update_block_template(); } static void on_stop(uv_async_t*); static void on_reconnect_to_host(uv_async_t* async) { reinterpret_cast(async->data)->reconnect_to_host(); } void submit_block() const; + void submit_aux_block() const; std::atomic m_stopped; @@ -219,7 +221,19 @@ class p2pool : public MinerCallbackHandler, public nocopy_nomove mutable uv_mutex_t m_submitBlockDataLock; SubmitBlockData m_submitBlockData; + struct SubmitAuxBlockData + { + hash chain_id; + uint32_t template_id = 0; + uint32_t nonce = 0; + uint32_t extra_nonce = 0; + }; + + mutable uv_mutex_t m_submitAuxBlockDataLock; + mutable std::vector m_submitAuxBlockData; + uv_async_t m_submitBlockAsync; + uv_async_t m_submitAuxBlockAsync; uv_async_t m_blockTemplateAsync; uv_async_t m_stopAsync; diff --git a/src/stratum_server.cpp b/src/stratum_server.cpp index dfb82837..f22c88eb 100644 --- a/src/stratum_server.cpp +++ b/src/stratum_server.cpp @@ -407,7 +407,7 @@ bool StratumServer::on_submit(StratumClient* client, uint32_t id, const char* jo if (aux_data.difficulty.check_pow(resultHash)) { const char* s = client->m_customUser; LOGINFO(0, log::Green() << "client " << static_cast(client->m_addrString) << (*s ? " user " : "") << s << " found an aux block for chain_id " << aux_data.unique_id << ", diff " << aux_data.difficulty << ", submitting it"); - m_pool->submit_aux_block(aux_data.unique_id, template_id, nonce, extra_nonce); + m_pool->submit_aux_block_async(aux_data.unique_id, template_id, nonce, extra_nonce); } } }