From 13087dd45e4cb11458bc3afdcebf8cb8611085be Mon Sep 17 00:00:00 2001 From: SChernykh Date: Mon, 20 Nov 2023 22:22:37 +0100 Subject: [PATCH] Submit found aux blocks --- src/block_template.cpp | 33 ++++++++++++++++++++++++++++++--- src/block_template.h | 1 + src/miner.cpp | 2 +- src/p2pool.cpp | 38 +++++++++++++++++++++++++++++--------- src/p2pool.h | 4 +--- src/pool_block.cpp | 4 ++++ src/pool_block.h | 2 ++ src/stratum_server.cpp | 2 +- 8 files changed, 69 insertions(+), 17 deletions(-) diff --git a/src/block_template.cpp b/src/block_template.cpp index 317d860..736d416 100644 --- a/src/block_template.cpp +++ b/src/block_template.cpp @@ -1309,6 +1309,34 @@ std::vector BlockTemplate::get_aux_chains(const uint32_t template_ return m_poolBlockTemplate->m_auxChains; } +bool BlockTemplate::get_aux_proof(const uint32_t template_id, const hash& h, std::vector& proof) const +{ + ReadLock lock(m_lock); + + if (template_id != m_templateId) { + const BlockTemplate* old = m_oldTemplates[template_id % array_size(&BlockTemplate::m_oldTemplates)]; + if (old && (template_id == old->m_templateId)) { + return old->get_aux_proof(template_id, h, proof); + } + + return false; + } + + std::vector> t; + if (!get_merkle_proof(m_poolBlockTemplate->m_merkleTree, m_poolBlockTemplate->m_sidechainId, t)) { + return false; + } + + proof.clear(); + proof.reserve(proof.size()); + + for (const auto& k : t) { + proof.emplace_back(k.second); + } + + return true; +} + std::vector BlockTemplate::get_block_template_blob(uint32_t template_id, uint32_t sidechain_extra_nonce, size_t& nonce_offset, size_t& extra_nonce_offset, size_t& sidechain_id_offset, hash& sidechain_id) const { ReadLock lock(m_lock); @@ -1434,11 +1462,10 @@ void BlockTemplate::init_merge_mining_merkle_proof() } } - std::vector> tree; - merkle_hash_full_tree(hashes, tree); + merkle_hash_full_tree(hashes, m_poolBlockTemplate->m_merkleTree); std::vector> proof; - get_merkle_proof(tree, m_poolBlockTemplate->m_sidechainId, proof); + get_merkle_proof(m_poolBlockTemplate->m_merkleTree, m_poolBlockTemplate->m_sidechainId, proof); m_poolBlockTemplate->m_merkleProof.reserve(proof.size()); diff --git a/src/block_template.h b/src/block_template.h index a245fd2..dfacf59 100644 --- a/src/block_template.h +++ b/src/block_template.h @@ -49,6 +49,7 @@ public: uint32_t get_hashing_blobs(uint32_t extra_nonce_start, uint32_t count, std::vector& blobs, uint64_t& height, difficulty_type& difficulty, difficulty_type& aux_diff, difficulty_type& sidechain_difficulty, hash& seed_hash, size_t& nonce_offset, uint32_t& template_id) const; std::vector get_aux_chains(const uint32_t template_id) const; + bool get_aux_proof(const uint32_t template_id, const hash& h, std::vector& proof) const; std::vector get_block_template_blob(uint32_t template_id, uint32_t sidechain_extra_nonce, size_t& nonce_offset, size_t& extra_nonce_offset, size_t& sidechain_id_offset, hash& sidechain_id) const; diff --git a/src/miner.cpp b/src/miner.cpp index 02d266c..906660a 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -244,7 +244,7 @@ void Miner::run(WorkerData* data) if (j.m_auxDiff.check_pow(h)) { for (const AuxChainData& aux_data : j.m_auxChains) { if (aux_data.difficulty.check_pow(h)) { - m_pool->submit_aux_block_async(aux_data, j.m_templateId, j.m_nonce, j.m_extraNonce); + m_pool->submit_aux_block(aux_data.unique_id, j.m_templateId, j.m_nonce, j.m_extraNonce); } } } diff --git a/src/p2pool.cpp b/src/p2pool.cpp index 9f7a139..aa26af4 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -136,7 +136,6 @@ 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); @@ -201,7 +200,6 @@ p2pool::~p2pool() uv_mutex_destroy(&m_minerLock); #endif uv_mutex_destroy(&m_submitBlockDataLock); - uv_mutex_destroy(&m_submitAuxBlockDataLock); delete m_api; delete m_sideChain; @@ -590,15 +588,37 @@ void p2pool::submit_block_async(std::vector&& blob) } } -void p2pool::submit_aux_block_async(const AuxChainData& aux_data, uint32_t template_id, uint32_t nonce, uint32_t extra_nonce) +void p2pool::submit_aux_block(const hash& chain_id, uint32_t template_id, uint32_t nonce, uint32_t extra_nonce) { - // TODO - MutexLock lock(m_submitAuxBlockDataLock); + size_t nonce_offset = 0; + size_t extra_nonce_offset = 0; + size_t sidechain_id_offset = 0; + hash sidechain_id; - (void)aux_data; - (void)template_id; - (void)nonce; - (void)extra_nonce; + std::vector blob = m_blockTemplate->get_block_template_blob(template_id, extra_nonce, nonce_offset, extra_nonce_offset, sidechain_id_offset, sidechain_id); + + if (blob.empty()) { + LOGWARN(3, "submit_aux_block: block template blob not found"); + return; + } + + uint8_t* p = blob.data(); + memcpy(p + nonce_offset, &nonce, NONCE_SIZE); + memcpy(p + extra_nonce_offset, &extra_nonce, EXTRA_NONCE_SIZE); + memcpy(p + sidechain_id_offset, sidechain_id.h, HASH_SIZE); + + for (MergeMiningClient* c : m_mergeMiningClients) { + if (chain_id == c->aux_id()) { + std::vector proof; + if (m_blockTemplate->get_aux_proof(template_id, c->aux_data(), proof)) { + c->merge_mining_submit_solution(blob, proof); + } + else { + LOGWARN(3, "submit_aux_block: failed to get merkle proof"); + } + return; + } + } } bool init_signals(p2pool* pool, bool init); diff --git a/src/p2pool.h b/src/p2pool.h index ba942e4..0d18624 100644 --- a/src/p2pool.h +++ b/src/p2pool.h @@ -87,7 +87,7 @@ public: 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_async(const AuxChainData& aux_data, uint32_t template_id, uint32_t nonce, uint32_t extra_nonce); + void submit_aux_block(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); @@ -217,8 +217,6 @@ private: mutable uv_mutex_t m_submitBlockDataLock; SubmitBlockData m_submitBlockData; - uv_mutex_t m_submitAuxBlockDataLock; - uv_async_t m_submitBlockAsync; uv_async_t m_blockTemplateAsync; uv_async_t m_stopAsync; diff --git a/src/pool_block.cpp b/src/pool_block.cpp index 38c941c..da51385 100644 --- a/src/pool_block.cpp +++ b/src/pool_block.cpp @@ -101,6 +101,7 @@ PoolBlock& PoolBlock::operator=(const PoolBlock& b) m_sidechainHeight = b.m_sidechainHeight; m_difficulty = b.m_difficulty; m_cumulativeDifficulty = b.m_cumulativeDifficulty; + m_merkleTree = b.m_merkleTree; m_merkleProof = b.m_merkleProof; memcpy(m_sidechainExtraBuf, b.m_sidechainExtraBuf, sizeof(m_sidechainExtraBuf)); m_sidechainId = b.m_sidechainId; @@ -295,6 +296,9 @@ void PoolBlock::reset_offchain_data() m_auxChains.shrink_to_fit(); m_auxNonce = 0; + + m_merkleTree.clear(); + m_merkleTree.shrink_to_fit(); } bool PoolBlock::get_pow_hash(RandomX_Hasher_Base* hasher, uint64_t height, const hash& seed_hash, hash& pow_hash, bool force_light_mode) diff --git a/src/pool_block.h b/src/pool_block.h index 33b8b71..58f81a9 100644 --- a/src/pool_block.h +++ b/src/pool_block.h @@ -151,6 +151,8 @@ struct PoolBlock std::vector m_auxChains; uint32_t m_auxNonce; + std::vector> m_merkleTree; + std::vector serialize_mainchain_data(size_t* header_size = nullptr, size_t* miner_tx_size = nullptr, int* outputs_offset = nullptr, int* outputs_blob_size = nullptr, const uint32_t* nonce = nullptr, const uint32_t* extra_nonce = nullptr) const; std::vector serialize_sidechain_data() const; diff --git a/src/stratum_server.cpp b/src/stratum_server.cpp index e47e0f0..0142f85 100644 --- a/src/stratum_server.cpp +++ b/src/stratum_server.cpp @@ -403,7 +403,7 @@ bool StratumServer::on_submit(StratumClient* client, uint32_t id, const char* jo if (aux_diff.check_pow(resultHash)) { for (const AuxChainData& aux_data : block.get_aux_chains(template_id)) { if (aux_data.difficulty.check_pow(resultHash)) { - m_pool->submit_aux_block_async(aux_data, template_id, nonce, extra_nonce); + m_pool->submit_aux_block(aux_data.unique_id, template_id, nonce, extra_nonce); } } }