From b07e8813c69e4853943eddf24ad9d30656f2ec7c Mon Sep 17 00:00:00 2001 From: SChernykh <15806605+SChernykh@users.noreply.github.com> Date: Thu, 23 May 2024 00:16:13 +0200 Subject: [PATCH] Tari: call `SubmitBlock` RPC in background to avoid stalling the main thread --- src/merge_mining_client_tari.cpp | 50 ++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/src/merge_mining_client_tari.cpp b/src/merge_mining_client_tari.cpp index 527fc0c..ac2d155 100644 --- a/src/merge_mining_client_tari.cpp +++ b/src/merge_mining_client_tari.cpp @@ -242,20 +242,46 @@ void MergeMiningClientTari::submit_solution(const BlockTemplate* block_tpl, cons pow->set_pow_data(data); } - grpc::ClientContext ctx; - SubmitBlockResponse response; + struct Work + { + uv_work_t req; + MergeMiningClientTari* client; + Block block; + } *work = new Work{ {}, this, std::move(block) }; - const grpc::Status status = m_TariNode->SubmitBlock(&ctx, block, &response); + work->req.data = work; - if (!status.ok()) { - LOGWARN(5, "SubmitBlock failed: " << status.error_message()); - if (!status.error_details().empty()) { - LOGWARN(5, "SubmitBlock failed: " << status.error_details()); - } - } - else { - const std::string& h = response.block_hash(); - LOGINFO(0, log::LightGreen() << "Mined Tari block " << log::hex_buf(h.data(), h.size()) << " at height " << block.header().height()); + const int err = uv_queue_work(uv_default_loop_checked(), &work->req, + [](uv_work_t* req) + { + BACKGROUND_JOB_START(MergeMiningClientTari::submit_solution); + + grpc::ClientContext ctx; + SubmitBlockResponse response; + + const Work* w = reinterpret_cast(req->data); + const grpc::Status status = w->client->m_TariNode->SubmitBlock(&ctx, w->block, &response); + + if (!status.ok()) { + LOGWARN(5, "SubmitBlock failed: " << status.error_message()); + if (!status.error_details().empty()) { + LOGWARN(5, "SubmitBlock failed: " << status.error_details()); + } + } + else { + const std::string& h = response.block_hash(); + LOGINFO(0, log::LightGreen() << "Mined Tari block " << log::hex_buf(h.data(), h.size()) << " at height " << w->block.header().height()); + } + }, + [](uv_work_t* req, int /*status*/) + { + delete reinterpret_cast(req->data); + BACKGROUND_JOB_STOP(MergeMiningClientTari::submit_solution); + }); + + if (err) { + LOGERR(1, "submit_solution: uv_queue_work failed, error " << uv_err_name(err)); + delete work; } }