Skip to content

Commit e6c2f4c

Browse files
committed
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.
1 parent 02d4bc7 commit e6c2f4c

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

src/node/interfaces.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -929,22 +929,8 @@ class BlockTemplateImpl : public BlockTemplate
929929

930930
bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CTransactionRef coinbase) override
931931
{
932-
CBlock block{m_block_template->block};
933-
934-
if (block.vtx.size() == 0) {
935-
block.vtx.push_back(coinbase);
936-
} else {
937-
block.vtx[0] = coinbase;
938-
}
939-
940-
block.nVersion = version;
941-
block.nTime = timestamp;
942-
block.nNonce = nonce;
943-
944-
block.hashMerkleRoot = BlockMerkleRoot(block);
945-
946-
auto block_ptr = std::make_shared<const CBlock>(block);
947-
return chainman().ProcessNewBlock(block_ptr, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/nullptr);
932+
AddMerkleRootAndCoinbase(m_block_template->block, std::move(coinbase), version, timestamp, nonce);
933+
return chainman().ProcessNewBlock(std::make_shared<const CBlock>(m_block_template->block), /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/nullptr);
948934
}
949935

950936
std::unique_ptr<BlockTemplate> waitNext(BlockWaitOptions options) override

src/node/miner.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,17 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda
434434
nDescendantsUpdated += UpdatePackagesForAdded(mempool, ancestors, mapModifiedTx);
435435
}
436436
}
437+
438+
void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce)
439+
{
440+
if (block.vtx.size() == 0) {
441+
block.vtx.emplace_back(coinbase);
442+
} else {
443+
block.vtx[0] = coinbase;
444+
}
445+
block.nVersion = version;
446+
block.nTime = timestamp;
447+
block.nNonce = nonce;
448+
block.hashMerkleRoot = BlockMerkleRoot(block);
449+
}
437450
} // namespace node

src/node/miner.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ void RegenerateCommitments(CBlock& block, ChainstateManager& chainman);
229229

230230
/** Apply -blockmintxfee and -blockmaxweight options from ArgsManager to BlockAssembler options. */
231231
void ApplyArgsManOptions(const ArgsManager& gArgs, BlockAssembler::Options& options);
232+
233+
/* Compute the block's merkle root, insert or replace the coinbase transaction and the merkle root into the block */
234+
void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce);
232235
} // namespace node
233236

234237
#endif // BITCOIN_NODE_MINER_H

0 commit comments

Comments
 (0)