mining: Return shared_ptr from CreateNewBlock

This commit is contained in:
Luke Dashjr 2024-08-08 20:03:28 +00:00
parent d3e5f1a69f
commit 8ef56b3f03
7 changed files with 12 additions and 12 deletions

View File

@ -46,8 +46,8 @@ public:
* @param[in] options options for creating the block * @param[in] options options for creating the block
* @returns a block template * @returns a block template
*/ */
virtual std::unique_ptr<node::CBlockTemplate> createNewBlock(const CScript& script_pub_key, const node::BlockCreateOptions& options={}) = 0; virtual std::shared_ptr<node::CBlockTemplate> createNewBlock(const CScript& script_pub_key, const node::BlockCreateOptions& options={}) = 0;
virtual std::unique_ptr<node::CBlockTemplate> createNewBlock2(const CScript& script_pub_key, const node::BlockCreateOptions& assemble_options) = 0; virtual std::shared_ptr<node::CBlockTemplate> createNewBlock2(const CScript& script_pub_key, const node::BlockCreateOptions& assemble_options) = 0;
/** /**
* Processes new block. A valid new block is automatically relayed to peers. * Processes new block. A valid new block is automatically relayed to peers.

View File

@ -909,14 +909,14 @@ public:
return TestBlockValidity(state, chainman().GetParams(), chainman().ActiveChainstate(), block, tip, /*fCheckPOW=*/false, check_merkle_root); 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::shared_ptr<CBlockTemplate> createNewBlock(const CScript& script_pub_key, const BlockCreateOptions& options) override
{ {
BlockAssembler::Options assemble_options{options}; BlockAssembler::Options assemble_options{options};
ApplyArgsManOptions(*Assert(m_node.args), assemble_options); ApplyArgsManOptions(*Assert(m_node.args), assemble_options);
return createNewBlock2(script_pub_key, assemble_options); return createNewBlock2(script_pub_key, assemble_options);
} }
std::unique_ptr<CBlockTemplate> createNewBlock2(const CScript& script_pub_key, const BlockCreateOptions& assemble_options) override std::shared_ptr<CBlockTemplate> createNewBlock2(const CScript& script_pub_key, const BlockCreateOptions& assemble_options) override
{ {
return BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(script_pub_key); return BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(script_pub_key);
} }

View File

@ -130,7 +130,7 @@ void BlockAssembler::resetBlock()
nFees = 0; nFees = 0;
} }
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn) std::shared_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
{ {
const auto time_start{SteadyClock::now()}; const auto time_start{SteadyClock::now()};

View File

@ -165,7 +165,7 @@ public:
explicit BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options); explicit BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options);
/** Construct a new block template with coinbase to scriptPubKeyIn */ /** Construct a new block template with coinbase to scriptPubKeyIn */
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn); std::shared_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
inline static std::optional<int64_t> m_last_block_num_txs{}; inline static std::optional<int64_t> m_last_block_num_txs{};
inline static std::optional<int64_t> m_last_block_weight{}; inline static std::optional<int64_t> m_last_block_weight{};

View File

@ -162,7 +162,7 @@ static UniValue generateBlocks(ChainstateManager& chainman, Mining& miner, const
{ {
UniValue blockHashes(UniValue::VARR); UniValue blockHashes(UniValue::VARR);
while (nGenerate > 0 && !chainman.m_interrupt) { while (nGenerate > 0 && !chainman.m_interrupt) {
std::unique_ptr<CBlockTemplate> pblocktemplate(miner.createNewBlock(coinbase_script)); auto pblocktemplate = miner.createNewBlock(coinbase_script);
if (!pblocktemplate.get()) if (!pblocktemplate.get())
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block"); throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
@ -373,7 +373,7 @@ static RPCHelpMan generateblock()
{ {
LOCK(chainman.GetMutex()); LOCK(chainman.GetMutex());
{ {
std::unique_ptr<CBlockTemplate> blocktemplate{miner.createNewBlock(coinbase_script, {.use_mempool = false})}; auto blocktemplate = miner.createNewBlock(coinbase_script, {.use_mempool = false});
if (!blocktemplate) { if (!blocktemplate) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block"); throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
} }
@ -852,7 +852,7 @@ static RPCHelpMan getblocktemplate()
// Update block // Update block
static CBlockIndex* pindexPrev; static CBlockIndex* pindexPrev;
static int64_t time_start; static int64_t time_start;
static std::unique_ptr<CBlockTemplate> pblocktemplate; static std::shared_ptr<CBlockTemplate> pblocktemplate;
if (!pindexPrev || pindexPrev->GetBlockHash() != tip || if (!pindexPrev || pindexPrev->GetBlockHash() != tip ||
bypass_cache || bypass_cache ||
(miner.getTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - time_start > 5)) (miner.getTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - time_start > 5))

View File

@ -68,7 +68,7 @@ CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
const CScript& scriptPubKey) const CScript& scriptPubKey)
{ {
BlockAssembler::Options options; BlockAssembler::Options options;
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options}.CreateNewBlock(scriptPubKey); std::shared_ptr<CBlockTemplate> pblocktemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options}.CreateNewBlock(scriptPubKey);
CBlock& block = pblocktemplate->block; CBlock& block = pblocktemplate->block;
block.hashPrevBlock = prev->GetBlockHash(); block.hashPrevBlock = prev->GetBlockHash();
block.nTime = prev->nTime + 1; block.nTime = prev->nTime + 1;

View File

@ -137,7 +137,7 @@ void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const
Txid hashHighFeeTx = tx.GetHash(); Txid hashHighFeeTx = tx.GetHash();
tx_mempool.addUnchecked(entry.Fee(50000).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx)); tx_mempool.addUnchecked(entry.Fee(50000).Time(Now<NodeSeconds>()).SpendsCoinbase(false).FromTx(tx));
std::unique_ptr<CBlockTemplate> pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey); auto pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 4U); BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 4U);
BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashParentTx); BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashParentTx);
BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashHighFeeTx); BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashHighFeeTx);
@ -609,7 +609,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
{ {
// Note that by default, these tests run with size accounting enabled. // Note that by default, these tests run with size accounting enabled.
CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG; CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
std::unique_ptr<CBlockTemplate> pblocktemplate; std::shared_ptr<CBlockTemplate> pblocktemplate;
CTxMemPool& tx_mempool{*m_node.mempool}; CTxMemPool& tx_mempool{*m_node.mempool};
// Simple block creation, nothing special yet: // Simple block creation, nothing special yet: