From 33e1ebd3fefa7ff8772f4b644fd512025397a5a4 Mon Sep 17 00:00:00 2001 From: SChernykh <15806605+SChernykh@users.noreply.github.com> Date: Thu, 30 May 2024 16:53:32 +0200 Subject: [PATCH] Calculate hashing blobs in parallel Improved performance with high number of connected miners. --- src/block_template.cpp | 51 ++++++++++++++++++++++++++---------------- src/stratum_server.cpp | 2 +- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/block_template.cpp b/src/block_template.cpp index 2c99941..b94b9bf 100644 --- a/src/block_template.cpp +++ b/src/block_template.cpp @@ -1276,8 +1276,6 @@ uint32_t BlockTemplate::get_hashing_blobs(uint32_t extra_nonce_start, uint32_t c blobs.reserve(required_capacity * 2); } - uint32_t blob_size = 0; - ReadLock lock(m_lock); height = m_height; @@ -1288,25 +1286,40 @@ uint32_t BlockTemplate::get_hashing_blobs(uint32_t extra_nonce_start, uint32_t c nonce_offset = m_nonceOffset; template_id = m_templateId; - for (uint32_t i = 0; i < count; ++i) { - uint8_t blob[128]; - uint32_t n = get_hashing_blob_nolock(extra_nonce_start + i, blob); + constexpr size_t MIN_BLOB_SIZE = 76; + constexpr size_t MAX_BLOB_SIZE = 128; - if (n > sizeof(blob)) { - LOGERR(1, "internal error: get_hashing_blob_nolock returned too large blob size " << n << ", expected <= " << sizeof(blob)); - n = sizeof(blob); - } - else if (n < 76) { - LOGERR(1, "internal error: get_hashing_blob_nolock returned too little blob size " << n << ", expected >= 76"); - } + blobs.resize(MAX_BLOB_SIZE); + const uint32_t blob_size = get_hashing_blob_nolock(extra_nonce_start, blobs.data()); - if (blob_size == 0) { - blob_size = n; - } - else if (n != blob_size) { - LOGERR(1, "internal error: get_hashing_blob_nolock returned different blob size " << n << ", expected " << blob_size); - } - blobs.insert(blobs.end(), blob, blob + blob_size); + if (blob_size > MAX_BLOB_SIZE) { + LOGERR(1, "internal error: get_hashing_blob_nolock returned too large blob size " << blob_size << ", expected <= " << MAX_BLOB_SIZE); + PANIC_STOP(); + } + else if (blob_size < MIN_BLOB_SIZE) { + LOGERR(1, "internal error: get_hashing_blob_nolock returned too little blob size " << blob_size << ", expected >= " << MIN_BLOB_SIZE); + } + + blobs.resize(static_cast(blob_size) * count); + + if (count > 1) { + uint8_t* blobs_data = blobs.data(); + + std::atomic counter = 1; + + parallel_run(uv_default_loop_checked(), [this, blob_size, extra_nonce_start, count, &counter, blobs_data]() { + for (;;) { + const uint32_t i = counter.fetch_add(1); + if (i >= count) { + return; + } + + const uint32_t n = get_hashing_blob_nolock(extra_nonce_start + i, blobs_data + static_cast(i) * blob_size); + if (n != blob_size) { + LOGERR(1, "internal error: get_hashing_blob_nolock returned different blob size " << n << ", expected " << blob_size); + } + } + }, true); } return blob_size; diff --git a/src/stratum_server.cpp b/src/stratum_server.cpp index f22c88e..f290ddf 100644 --- a/src/stratum_server.cpp +++ b/src/stratum_server.cpp @@ -147,7 +147,7 @@ void StratumServer::on_block(const BlockTemplate& block) else if (blobs_data->m_blobs.size() != blobs_data->m_blobSize * num_connections) { LOGERR(1, "internal error: get_hashing_blobs returned wrong amount of data"); } - else if (num_connections > 1) { + else if (pool_block_debug() && (num_connections > 1)) { std::vector blob_hashes; blob_hashes.reserve(num_connections);