Skip to content

Commit 05ae4ca

Browse files
committed
Add submitSolution to BlockTemplate interface
1 parent de0cc0b commit 05ae4ca

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/interfaces/mining.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class BlockTemplate
4141
virtual std::vector<unsigned char> getCoinbaseCommitment() = 0;
4242
virtual int getWitnessCommitmentIndex() = 0;
4343
virtual std::vector<uint256> getCoinbaseMerklePath() = 0;
44+
45+
/**
46+
* Construct and broadcast the block.
47+
*
48+
* @returns if the block was processed, independent of block validity
49+
*/
50+
virtual bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CMutableTransaction coinbase) = 0;
4451
};
4552

4653
//! Interface giving clients (RPC, Stratum v2 Template Provider in the future)

src/node/interfaces.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ class ChainImpl : public Chain
850850
class BlockTemplateImpl : public BlockTemplate
851851
{
852852
public:
853-
explicit BlockTemplateImpl(std::unique_ptr<CBlockTemplate> block_template) : m_block_template(std::move(block_template))
853+
explicit BlockTemplateImpl(std::unique_ptr<CBlockTemplate> block_template, NodeContext& node) : m_block_template(std::move(block_template)), m_node(node)
854854
{
855855
assert(m_block_template);
856856
}
@@ -895,7 +895,32 @@ class BlockTemplateImpl : public BlockTemplate
895895
return BlockMerkleBranch(m_block_template->block);
896896
}
897897

898+
bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CMutableTransaction coinbase) override
899+
{
900+
CBlock block{m_block_template->block};
901+
902+
auto cb = MakeTransactionRef(std::move(coinbase));
903+
904+
if (block.vtx.size() == 0) {
905+
block.vtx.push_back(cb);
906+
} else {
907+
block.vtx[0] = cb;
908+
}
909+
910+
block.nVersion = version;
911+
block.nTime = timestamp;
912+
block.nNonce = nonce;
913+
914+
block.hashMerkleRoot = BlockMerkleRoot(block);
915+
916+
auto block_ptr = std::make_shared<const CBlock>(block);
917+
return chainman().ProcessNewBlock(block_ptr, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/nullptr);
918+
}
919+
898920
const std::unique_ptr<CBlockTemplate> m_block_template;
921+
922+
ChainstateManager& chainman() { return *Assert(m_node.chainman); }
923+
NodeContext& m_node;
899924
};
900925

901926
class MinerImpl : public Mining
@@ -948,7 +973,7 @@ class MinerImpl : public Mining
948973
{
949974
BlockAssembler::Options assemble_options{options};
950975
ApplyArgsManOptions(*Assert(m_node.args), assemble_options);
951-
return std::make_unique<BlockTemplateImpl>(BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(script_pub_key));
976+
return std::make_unique<BlockTemplateImpl>(BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(script_pub_key), m_node);
952977
}
953978

954979
NodeContext* context() override { return &m_node; }

0 commit comments

Comments
 (0)