Skip to content

Commit 525e9dc

Browse files
committed
Add submitSolution to BlockTemplate interface
1 parent 47b4875 commit 525e9dc

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/interfaces/mining.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ class BlockTemplate
4949
* @return merkle path ordered from the deepest
5050
*/
5151
virtual std::vector<uint256> getCoinbaseMerklePath() = 0;
52+
53+
/**
54+
* Construct and broadcast the block.
55+
*
56+
* @returns if the block was processed, independent of block validity
57+
*/
58+
virtual bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CMutableTransaction coinbase) = 0;
5259
};
5360

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

src/ipc/capnp/mining.capnp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ interface BlockTemplate $Proxy.wrap("interfaces::BlockTemplate") {
3232
getCoinbaseCommitment @5 (context: Proxy.Context) -> (result: Data);
3333
getWitnessCommitmentIndex @6 (context: Proxy.Context) -> (result: Int32);
3434
getCoinbaseMerklePath @7 (context: Proxy.Context) -> (result: List(Data));
35+
submitSolution@8 (context: Proxy.Context, version: UInt32, timestamp: UInt32, nonce: UInt32, coinbase :Data) -> (result: Bool);
3536
}
3637

3738
struct BlockCreateOptions $Proxy.wrap("node::BlockCreateOptions") {

src/node/interfaces.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ class ChainImpl : public Chain
871871
class BlockTemplateImpl : public BlockTemplate
872872
{
873873
public:
874-
explicit BlockTemplateImpl(std::unique_ptr<CBlockTemplate> block_template) : m_block_template(std::move(block_template))
874+
explicit BlockTemplateImpl(std::unique_ptr<CBlockTemplate> block_template, NodeContext& node) : m_block_template(std::move(block_template)), m_node(node)
875875
{
876876
assert(m_block_template);
877877
}
@@ -916,7 +916,32 @@ class BlockTemplateImpl : public BlockTemplate
916916
return BlockMerkleBranch(m_block_template->block);
917917
}
918918

919+
bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CMutableTransaction coinbase) override
920+
{
921+
CBlock block{m_block_template->block};
922+
923+
auto cb = MakeTransactionRef(std::move(coinbase));
924+
925+
if (block.vtx.size() == 0) {
926+
block.vtx.push_back(cb);
927+
} else {
928+
block.vtx[0] = cb;
929+
}
930+
931+
block.nVersion = version;
932+
block.nTime = timestamp;
933+
block.nNonce = nonce;
934+
935+
block.hashMerkleRoot = BlockMerkleRoot(block);
936+
937+
auto block_ptr = std::make_shared<const CBlock>(block);
938+
return chainman().ProcessNewBlock(block_ptr, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/nullptr);
939+
}
940+
919941
const std::unique_ptr<CBlockTemplate> m_block_template;
942+
943+
ChainstateManager& chainman() { return *Assert(m_node.chainman); }
944+
NodeContext& m_node;
920945
};
921946

922947
class MinerImpl : public Mining
@@ -990,7 +1015,7 @@ class MinerImpl : public Mining
9901015
{
9911016
BlockAssembler::Options assemble_options{options};
9921017
ApplyArgsManOptions(*Assert(m_node.args), assemble_options);
993-
return std::make_unique<BlockTemplateImpl>(BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(script_pub_key));
1018+
return std::make_unique<BlockTemplateImpl>(BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(script_pub_key), m_node);
9941019
}
9951020

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

0 commit comments

Comments
 (0)