Tari: call SubmitBlock RPC in background

to avoid stalling the main thread
This commit is contained in:
SChernykh 2024-05-23 00:16:13 +02:00
parent 406aa72112
commit b07e8813c6

View file

@ -242,10 +242,25 @@ void MergeMiningClientTari::submit_solution(const BlockTemplate* block_tpl, cons
pow->set_pow_data(data); pow->set_pow_data(data);
} }
struct Work
{
uv_work_t req;
MergeMiningClientTari* client;
Block block;
} *work = new Work{ {}, this, std::move(block) };
work->req.data = work;
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; grpc::ClientContext ctx;
SubmitBlockResponse response; SubmitBlockResponse response;
const grpc::Status status = m_TariNode->SubmitBlock(&ctx, block, &response); const Work* w = reinterpret_cast<Work*>(req->data);
const grpc::Status status = w->client->m_TariNode->SubmitBlock(&ctx, w->block, &response);
if (!status.ok()) { if (!status.ok()) {
LOGWARN(5, "SubmitBlock failed: " << status.error_message()); LOGWARN(5, "SubmitBlock failed: " << status.error_message());
@ -255,7 +270,18 @@ void MergeMiningClientTari::submit_solution(const BlockTemplate* block_tpl, cons
} }
else { else {
const std::string& h = response.block_hash(); 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()); 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<Work*>(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;
} }
} }