Have createNewBlock return BlockTemplate interface

An external program that uses the Mining interface may need quick access to some information in the block template, while it can wait a bit longer for the full raw transaction data.

This would be the case for a Stratum v2 Template Provider which needs to send a NewTemplate message (which doesn't include transactions) as quickly as possible.
This commit is contained in:
Sjors Provoost
2024-08-26 10:51:19 +02:00
parent cf0120ff02
commit dd87b6dff3
3 changed files with 112 additions and 46 deletions

View File

@@ -67,6 +67,7 @@
#include <boost/signals2/signal.hpp>
using interfaces::BlockTemplate;
using interfaces::BlockTip;
using interfaces::Chain;
using interfaces::FoundBlock;
@@ -863,6 +864,52 @@ public:
NodeContext& m_node;
};
class BlockTemplateImpl : public BlockTemplate
{
public:
explicit BlockTemplateImpl(std::unique_ptr<CBlockTemplate> block_template) : m_block_template(std::move(block_template))
{
assert(m_block_template);
}
CBlockHeader getBlockHeader() override
{
return m_block_template->block;
}
CBlock getBlock() override
{
return m_block_template->block;
}
std::vector<CAmount> getTxFees() override
{
return m_block_template->vTxFees;
}
std::vector<int64_t> getTxSigops() override
{
return m_block_template->vTxSigOpsCost;
}
CTransactionRef getCoinbaseTx() override
{
return m_block_template->block.vtx[0];
}
std::vector<unsigned char> getCoinbaseCommitment() override
{
return m_block_template->vchCoinbaseCommitment;
}
int getWitnessCommitmentIndex() override
{
return GetWitnessCommitmentIndex(m_block_template->block);
}
const std::unique_ptr<CBlockTemplate> m_block_template;
};
class MinerImpl : public Mining
{
public:
@@ -909,11 +956,11 @@ public:
return TestBlockValidity(state, chainman().GetParams(), chainman().ActiveChainstate(), block, tip, /*fCheckPOW=*/false, check_merkle_root);
}
std::unique_ptr<CBlockTemplate> createNewBlock(const CScript& script_pub_key, const BlockCreateOptions& options) override
std::unique_ptr<BlockTemplate> createNewBlock(const CScript& script_pub_key, const BlockCreateOptions& options) override
{
BlockAssembler::Options assemble_options{options};
ApplyArgsManOptions(*Assert(m_node.args), assemble_options);
return 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));
}
NodeContext* context() override { return &m_node; }