Skip to content

Commit

Permalink
Add submitSolution to BlockTemplate interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Sep 26, 2024
1 parent 47b4875 commit 525e9dc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/interfaces/mining.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class BlockTemplate
* @return merkle path ordered from the deepest
*/
virtual std::vector<uint256> getCoinbaseMerklePath() = 0;

/**
* Construct and broadcast the block.
*
* @returns if the block was processed, independent of block validity
*/
virtual bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CMutableTransaction coinbase) = 0;
};

//! Interface giving clients (RPC, Stratum v2 Template Provider in the future)
Expand Down
1 change: 1 addition & 0 deletions src/ipc/capnp/mining.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface BlockTemplate $Proxy.wrap("interfaces::BlockTemplate") {
getCoinbaseCommitment @5 (context: Proxy.Context) -> (result: Data);
getWitnessCommitmentIndex @6 (context: Proxy.Context) -> (result: Int32);
getCoinbaseMerklePath @7 (context: Proxy.Context) -> (result: List(Data));
submitSolution@8 (context: Proxy.Context, version: UInt32, timestamp: UInt32, nonce: UInt32, coinbase :Data) -> (result: Bool);
}

struct BlockCreateOptions $Proxy.wrap("node::BlockCreateOptions") {
Expand Down
29 changes: 27 additions & 2 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ class ChainImpl : public Chain
class BlockTemplateImpl : public BlockTemplate
{
public:
explicit BlockTemplateImpl(std::unique_ptr<CBlockTemplate> block_template) : m_block_template(std::move(block_template))
explicit BlockTemplateImpl(std::unique_ptr<CBlockTemplate> block_template, NodeContext& node) : m_block_template(std::move(block_template)), m_node(node)
{
assert(m_block_template);
}
Expand Down Expand Up @@ -916,7 +916,32 @@ class BlockTemplateImpl : public BlockTemplate
return BlockMerkleBranch(m_block_template->block);
}

bool submitSolution(uint32_t version, uint32_t timestamp, uint32_t nonce, CMutableTransaction coinbase) override
{
CBlock block{m_block_template->block};

auto cb = MakeTransactionRef(std::move(coinbase));

if (block.vtx.size() == 0) {
block.vtx.push_back(cb);
} else {
block.vtx[0] = cb;
}

block.nVersion = version;
block.nTime = timestamp;
block.nNonce = nonce;

block.hashMerkleRoot = BlockMerkleRoot(block);

auto block_ptr = std::make_shared<const CBlock>(block);
return chainman().ProcessNewBlock(block_ptr, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/nullptr);
}

const std::unique_ptr<CBlockTemplate> m_block_template;

ChainstateManager& chainman() { return *Assert(m_node.chainman); }
NodeContext& m_node;
};

class MinerImpl : public Mining
Expand Down Expand Up @@ -990,7 +1015,7 @@ class MinerImpl : public Mining
{
BlockAssembler::Options assemble_options{options};
ApplyArgsManOptions(*Assert(m_node.args), assemble_options);
return std::make_unique<BlockTemplateImpl>(BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(script_pub_key));
return std::make_unique<BlockTemplateImpl>(BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(script_pub_key), m_node);
}

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

0 comments on commit 525e9dc

Please sign in to comment.