Merge bitcoin/bitcoin#30440: Have createNewBlock() return a BlockTemplate interface

a93c171faa Drop unneeded nullptr check from CreateNewBlock() (Sjors Provoost)
dd87b6dff3 Have createNewBlock return BlockTemplate interface (Sjors Provoost)

Pull request description:

  Suggested in https://github.com/bitcoin/bitcoin/pull/29432#issuecomment-2225337100

  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](https://github.com/stratum-mining/sv2-spec/blob/main/07-Template-Distribution-Protocol.md#72-newtemplate-server---client) message message (which doesn't include transactions) as quickly as possible. It does not include the serialized block transactions.

ACKs for top commit:
  achow101:
    ACK a93c171faa
  ryanofsky:
    Code review ACK a93c171faa. Since last review, just rebased with no changes or conflicts
  itornaza:
    Code review ACK a93c171faa
  TheCharlatan:
    Re-ACK a93c171faa

Tree-SHA512: 17cb61eb5548e9d4a69e34dd732f68a06cde2ad3d82c8339efee704c7860d5de144d93b23d6ecd6ee4ec205844e5560ad0f6d3917822fa75bb8e640c5f51af9a
This commit is contained in:
Ava Chow
2024-09-16 15:35:09 -04:00
4 changed files with 112 additions and 50 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; }

View File

@@ -113,10 +113,6 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
resetBlock();
pblocktemplate.reset(new CBlockTemplate());
if (!pblocktemplate.get()) {
return nullptr;
}
CBlock* const pblock = &pblocktemplate->block; // pointer for convenience
// Add dummy coinbase tx as first transaction