From e6c2f4ce7a841153510971f0236c527d1a499649 Mon Sep 17 00:00:00 2001 From: ismaelsadeeq Date: Mon, 28 Apr 2025 17:00:50 +0100 Subject: [PATCH] interfaces: refactor: move `submitSolution` implementation to miner - Create a new function `AddMerkleRootAndCoinbase` that compute the block's merkle root, insert the coinbase transaction and the merkle root into the block. --- src/node/interfaces.cpp | 18 ++---------------- src/node/miner.cpp | 13 +++++++++++++ src/node/miner.h | 3 +++ 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 629db507ef4..99a793e9a8a 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -929,22 +929,8 @@ public: bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CTransactionRef coinbase) override { - CBlock block{m_block_template->block}; - - if (block.vtx.size() == 0) { - block.vtx.push_back(coinbase); - } else { - block.vtx[0] = coinbase; - } - - block.nVersion = version; - block.nTime = timestamp; - block.nNonce = nonce; - - block.hashMerkleRoot = BlockMerkleRoot(block); - - auto block_ptr = std::make_shared(block); - return chainman().ProcessNewBlock(block_ptr, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/nullptr); + AddMerkleRootAndCoinbase(m_block_template->block, std::move(coinbase), version, timestamp, nonce); + return chainman().ProcessNewBlock(std::make_shared(m_block_template->block), /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/nullptr); } std::unique_ptr waitNext(BlockWaitOptions options) override diff --git a/src/node/miner.cpp b/src/node/miner.cpp index 68d71ab785b..0c982bedfe2 100644 --- a/src/node/miner.cpp +++ b/src/node/miner.cpp @@ -434,4 +434,17 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda nDescendantsUpdated += UpdatePackagesForAdded(mempool, ancestors, mapModifiedTx); } } + +void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce) +{ + if (block.vtx.size() == 0) { + block.vtx.emplace_back(coinbase); + } else { + block.vtx[0] = coinbase; + } + block.nVersion = version; + block.nTime = timestamp; + block.nNonce = nonce; + block.hashMerkleRoot = BlockMerkleRoot(block); +} } // namespace node diff --git a/src/node/miner.h b/src/node/miner.h index c09e9eb5d1c..641f8a031bb 100644 --- a/src/node/miner.h +++ b/src/node/miner.h @@ -229,6 +229,9 @@ void RegenerateCommitments(CBlock& block, ChainstateManager& chainman); /** Apply -blockmintxfee and -blockmaxweight options from ArgsManager to BlockAssembler options. */ void ApplyArgsManOptions(const ArgsManager& gArgs, BlockAssembler::Options& options); + +/* Compute the block's merkle root, insert or replace the coinbase transaction and the merkle root into the block */ +void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce); } // namespace node #endif // BITCOIN_NODE_MINER_H