From 8ef56b3f03eb112b49e9475debb3ea31a97ec57b Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Thu, 8 Aug 2024 20:03:28 +0000 Subject: [PATCH] mining: Return shared_ptr from CreateNewBlock --- src/interfaces/mining.h | 4 ++-- src/node/interfaces.cpp | 4 ++-- src/node/miner.cpp | 2 +- src/node/miner.h | 2 +- src/rpc/mining.cpp | 6 +++--- src/test/blockfilter_index_tests.cpp | 2 +- src/test/miner_tests.cpp | 4 ++-- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/interfaces/mining.h b/src/interfaces/mining.h index 9d98a3919e..1577a4cc50 100644 --- a/src/interfaces/mining.h +++ b/src/interfaces/mining.h @@ -46,8 +46,8 @@ public: * @param[in] options options for creating the block * @returns a block template */ - virtual std::unique_ptr createNewBlock(const CScript& script_pub_key, const node::BlockCreateOptions& options={}) = 0; - virtual std::unique_ptr createNewBlock2(const CScript& script_pub_key, const node::BlockCreateOptions& assemble_options) = 0; + virtual std::shared_ptr createNewBlock(const CScript& script_pub_key, const node::BlockCreateOptions& options={}) = 0; + virtual std::shared_ptr createNewBlock2(const CScript& script_pub_key, const node::BlockCreateOptions& assemble_options) = 0; /** * Processes new block. A valid new block is automatically relayed to peers. diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index b5dca8c61e..76097b3ce6 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -909,14 +909,14 @@ public: return TestBlockValidity(state, chainman().GetParams(), chainman().ActiveChainstate(), block, tip, /*fCheckPOW=*/false, check_merkle_root); } - std::unique_ptr createNewBlock(const CScript& script_pub_key, const BlockCreateOptions& options) override + std::shared_ptr createNewBlock(const CScript& script_pub_key, const BlockCreateOptions& options) override { BlockAssembler::Options assemble_options{options}; ApplyArgsManOptions(*Assert(m_node.args), assemble_options); return createNewBlock2(script_pub_key, assemble_options); } - std::unique_ptr createNewBlock2(const CScript& script_pub_key, const BlockCreateOptions& assemble_options) override + std::shared_ptr createNewBlock2(const CScript& script_pub_key, const BlockCreateOptions& assemble_options) override { return BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(script_pub_key); } diff --git a/src/node/miner.cpp b/src/node/miner.cpp index b9bc58e9f6..fb692426d2 100644 --- a/src/node/miner.cpp +++ b/src/node/miner.cpp @@ -130,7 +130,7 @@ void BlockAssembler::resetBlock() nFees = 0; } -std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn) +std::shared_ptr BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn) { const auto time_start{SteadyClock::now()}; diff --git a/src/node/miner.h b/src/node/miner.h index e376d9b720..10aded0a57 100644 --- a/src/node/miner.h +++ b/src/node/miner.h @@ -165,7 +165,7 @@ public: explicit BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options); /** Construct a new block template with coinbase to scriptPubKeyIn */ - std::unique_ptr CreateNewBlock(const CScript& scriptPubKeyIn); + std::shared_ptr CreateNewBlock(const CScript& scriptPubKeyIn); inline static std::optional m_last_block_num_txs{}; inline static std::optional m_last_block_weight{}; diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index fef3fd0a10..0769b1e656 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -162,7 +162,7 @@ static UniValue generateBlocks(ChainstateManager& chainman, Mining& miner, const { UniValue blockHashes(UniValue::VARR); while (nGenerate > 0 && !chainman.m_interrupt) { - std::unique_ptr pblocktemplate(miner.createNewBlock(coinbase_script)); + auto pblocktemplate = miner.createNewBlock(coinbase_script); if (!pblocktemplate.get()) throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block"); @@ -373,7 +373,7 @@ static RPCHelpMan generateblock() { LOCK(chainman.GetMutex()); { - std::unique_ptr blocktemplate{miner.createNewBlock(coinbase_script, {.use_mempool = false})}; + auto blocktemplate = miner.createNewBlock(coinbase_script, {.use_mempool = false}); if (!blocktemplate) { throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block"); } @@ -852,7 +852,7 @@ static RPCHelpMan getblocktemplate() // Update block static CBlockIndex* pindexPrev; static int64_t time_start; - static std::unique_ptr pblocktemplate; + static std::shared_ptr pblocktemplate; if (!pindexPrev || pindexPrev->GetBlockHash() != tip || bypass_cache || (miner.getTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - time_start > 5)) diff --git a/src/test/blockfilter_index_tests.cpp b/src/test/blockfilter_index_tests.cpp index 067a32d6a4..fd6ad7cac9 100644 --- a/src/test/blockfilter_index_tests.cpp +++ b/src/test/blockfilter_index_tests.cpp @@ -68,7 +68,7 @@ CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev, const CScript& scriptPubKey) { BlockAssembler::Options options; - std::unique_ptr pblocktemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options}.CreateNewBlock(scriptPubKey); + std::shared_ptr pblocktemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options}.CreateNewBlock(scriptPubKey); CBlock& block = pblocktemplate->block; block.hashPrevBlock = prev->GetBlockHash(); block.nTime = prev->nTime + 1; diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp index 5819a0340f..f8df99bf18 100644 --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -137,7 +137,7 @@ void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const Txid hashHighFeeTx = tx.GetHash(); tx_mempool.addUnchecked(entry.Fee(50000).Time(Now()).SpendsCoinbase(false).FromTx(tx)); - std::unique_ptr pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey); + auto pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey); BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 4U); BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashParentTx); 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. CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG; - std::unique_ptr pblocktemplate; + std::shared_ptr pblocktemplate; CTxMemPool& tx_mempool{*m_node.mempool}; // Simple block creation, nothing special yet: