From a38a7be73f3d066c096ff6dc32bc44d3a1f0bd37 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Wed, 25 Aug 2021 12:45:14 +0200 Subject: [PATCH] p2pool: submit block from the main thread only --- src/p2pool.cpp | 27 +++++++++++++++++++++++++-- src/p2pool.h | 12 +++++++++++- src/stratum_server.cpp | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/p2pool.cpp b/src/p2pool.cpp index 86f45c8..a7a01d8 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -44,6 +44,7 @@ p2pool::p2pool(int argc, char* argv[]) : m_stopped(false) , m_params(new Params(argc, argv)) , m_updateSeed(true) + , m_submitBlockData{} { if (!m_params->m_wallet.valid()) { LOGERR(1, "Invalid wallet address. Try \"p2pool --help\"."); @@ -59,7 +60,14 @@ p2pool::p2pool(int argc, char* argv[]) LOGWARN(1, "Mining to a stagenet wallet address"); } - int err = uv_async_init(uv_default_loop_checked(), &m_blockTemplateAsync, on_update_block_template); + int err = uv_async_init(uv_default_loop_checked(), &m_submitBlockAsync, on_submit_block); + if (err) { + LOGERR(1, "uv_async_init failed, error " << uv_err_name(err)); + panic(); + } + m_submitBlockAsync.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)); panic(); @@ -86,6 +94,7 @@ p2pool::p2pool(int argc, char* argv[]) p2pool::~p2pool() { + uv_close(reinterpret_cast(&m_submitBlockAsync), nullptr); uv_close(reinterpret_cast(&m_blockTemplateAsync), nullptr); uv_close(reinterpret_cast(&m_stopAsync), nullptr); uv_rwlock_destroy(&m_mainchainLock); @@ -245,8 +254,22 @@ void p2pool::handle_chain_main(ChainMain& data, const char* extra) } } -void p2pool::submit_block(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce) const +void p2pool::submit_block_async(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce) { + m_submitBlockData = { template_id, nonce, extra_nonce }; + + const int err = uv_async_send(&m_submitBlockAsync); + if (err) { + LOGERR(1, "uv_async_send failed, error " << uv_err_name(err)); + } +} + +void p2pool::submit_block() const +{ + const uint32_t template_id = m_submitBlockData.template_id; + uint32_t nonce = m_submitBlockData.nonce; + uint32_t extra_nonce = m_submitBlockData.extra_nonce; + const uint64_t height = m_blockTemplate->height(); const difficulty_type diff = m_blockTemplate->difficulty(); LOGINFO(0, "submit_block: height = " << height << ", template id = " << template_id << ", nonce = " << nonce << ", extra_nonce = " << extra_nonce); diff --git a/src/p2pool.h b/src/p2pool.h index c349bdb..fdc9621 100644 --- a/src/p2pool.h +++ b/src/p2pool.h @@ -60,7 +60,7 @@ public: virtual void handle_miner_data(MinerData& data) override; virtual void handle_chain_main(ChainMain& data, const char* extra) override; - void submit_block(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce) const; + void submit_block_async(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce); void submit_sidechain_block(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce); void update_block_template_async(); @@ -74,9 +74,12 @@ private: p2pool(const p2pool&) = delete; p2pool(p2pool&&) = delete; + static void on_submit_block(uv_async_t* async) { reinterpret_cast(async->data)->submit_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*) {} + void submit_block() const; + bool m_stopped; Params* m_params; @@ -110,6 +113,13 @@ private: ConsoleCommands* m_consoleCommands; + struct { + uint32_t template_id; + uint32_t nonce; + uint32_t extra_nonce; + } m_submitBlockData; + + uv_async_t m_submitBlockAsync; uv_async_t m_blockTemplateAsync; uv_async_t m_stopAsync; }; diff --git a/src/stratum_server.cpp b/src/stratum_server.cpp index bb9f885..7b74441 100644 --- a/src/stratum_server.cpp +++ b/src/stratum_server.cpp @@ -410,7 +410,7 @@ void StratumServer::on_share_found(uv_work_t* req) // First check if we found a mainnet block if (difficulty.check_pow(pow_hash)) { - pool->submit_block(share->m_templateId, share->m_nonce, share->m_extraNonce); + pool->submit_block_async(share->m_templateId, share->m_nonce, share->m_extraNonce); block.update_tx_keys(); }