Skip to content

Commit 9590c8d

Browse files
committed
miner: allow CreateNewBlock without non-coinbase txns
There are a few places (in tests) where we want to manually add transactions to the block template. We currently do this by constructing an empty mempool for the BlockAssembler to pull transactions from. This is hacky and potentially unsafe. We should narrow the interface for constructing new mempools. Instead of passing in an empty mempool, just add the option to not add any transactions to the template. In the future, this param be easily changed to a set of transactions to ask BlockAssembler to force-include.
1 parent cc5739b commit 9590c8d

File tree

5 files changed

+10
-9
lines changed

5 files changed

+10
-9
lines changed

src/node/miner.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void BlockAssembler::resetBlock()
103103
nFees = 0;
104104
}
105105

106-
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
106+
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn, bool manual_txns)
107107
{
108108
int64_t nTimeStart = GetTimeMicros();
109109

@@ -138,7 +138,9 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
138138

139139
int nPackagesSelected = 0;
140140
int nDescendantsUpdated = 0;
141-
addPackageTxs(nPackagesSelected, nDescendantsUpdated);
141+
if (!manual_txns) {
142+
addPackageTxs(nPackagesSelected, nDescendantsUpdated);
143+
}
142144

143145
int64_t nTime1 = GetTimeMicros();
144146

src/node/miner.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ class BlockAssembler
160160
explicit BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool);
161161
explicit BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const Options& options);
162162

163-
/** Construct a new block template with coinbase to scriptPubKeyIn */
164-
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
163+
/** Construct a new block template with coinbase to scriptPubKeyIn.
164+
* @param[in] manual_txns if true, don't add any non-coinbase transactions. */
165+
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, bool manual_txns = false);
165166

166167
inline static std::optional<int64_t> m_last_block_num_txs{};
167168
inline static std::optional<int64_t> m_last_block_weight{};

src/rpc/mining.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,7 @@ static RPCHelpMan generateblock()
354354
{
355355
LOCK(cs_main);
356356

357-
CTxMemPool empty_mempool;
358-
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler{chainman.ActiveChainstate(), empty_mempool}.CreateNewBlock(coinbase_script));
357+
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler{chainman.ActiveChainstate(), mempool}.CreateNewBlock(coinbase_script, /*manual_txns=*/ true));
359358
if (!blocktemplate) {
360359
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
361360
}

src/test/blockfilter_index_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
6565
const std::vector<CMutableTransaction>& txns,
6666
const CScript& scriptPubKey)
6767
{
68-
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), *m_node.mempool}.CreateNewBlock(scriptPubKey);
68+
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), *m_node.mempool}.CreateNewBlock(scriptPubKey, /*manual_txns=*/ true);
6969
CBlock& block = pblocktemplate->block;
7070
block.hashPrevBlock = prev->GetBlockHash();
7171
block.nTime = prev->nTime + 1;

src/test/util/setup_common.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ CBlock TestChain100Setup::CreateBlock(
277277
const CScript& scriptPubKey,
278278
CChainState& chainstate)
279279
{
280-
CTxMemPool empty_pool;
281-
CBlock block = BlockAssembler{chainstate, empty_pool}.CreateNewBlock(scriptPubKey)->block;
280+
CBlock block = BlockAssembler{chainstate, *m_node.mempool.get()}.CreateNewBlock(scriptPubKey, /*manual_txns=*/ true)->block;
282281

283282
Assert(block.vtx.size() == 1);
284283
for (const CMutableTransaction& tx : txns) {

0 commit comments

Comments
 (0)