Skip to content

Commit 0f1a259

Browse files
committed
miner: Make mempool optional for BlockAssembler
...also adjust callers Changes: - In BlockAssembler::CreateNewBlock, we now only lock m_mempool->cs and call addPackageTxs if m_mempool is not nullptr - BlockAssembler::addPackageTxs now takes in a mempool reference, and is annotated to require that mempool's lock. - In TestChain100Setup::CreateBlock and generateblock, don't construct an empty mempool, just pass in a nullptr for mempool
1 parent cc5739b commit 0f1a259

File tree

9 files changed

+32
-31
lines changed

9 files changed

+32
-31
lines changed

src/node/miner.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ BlockAssembler::Options::Options()
6262
nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
6363
}
6464

65-
BlockAssembler::BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const Options& options)
65+
BlockAssembler::BlockAssembler(CChainState& chainstate, const CTxMemPool* mempool, const Options& options)
6666
: chainparams{chainstate.m_chainman.GetParams()},
6767
m_mempool(mempool),
6868
m_chainstate(chainstate)
@@ -87,7 +87,7 @@ static BlockAssembler::Options DefaultOptions()
8787
return options;
8888
}
8989

90-
BlockAssembler::BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool)
90+
BlockAssembler::BlockAssembler(CChainState& chainstate, const CTxMemPool* mempool)
9191
: BlockAssembler(chainstate, mempool, DefaultOptions()) {}
9292

9393
void BlockAssembler::resetBlock()
@@ -121,7 +121,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
121121
pblocktemplate->vTxFees.push_back(-1); // updated at end
122122
pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end
123123

124-
LOCK2(cs_main, m_mempool.cs);
124+
LOCK(::cs_main);
125125
CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip();
126126
assert(pindexPrev != nullptr);
127127
nHeight = pindexPrev->nHeight + 1;
@@ -138,7 +138,10 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
138138

139139
int nPackagesSelected = 0;
140140
int nDescendantsUpdated = 0;
141-
addPackageTxs(nPackagesSelected, nDescendantsUpdated);
141+
if (m_mempool) {
142+
LOCK(m_mempool->cs);
143+
addPackageTxs(*m_mempool, nPackagesSelected, nDescendantsUpdated);
144+
}
142145

143146
int64_t nTime1 = GetTimeMicros();
144147

@@ -287,17 +290,17 @@ void BlockAssembler::SortForBlock(const CTxMemPool::setEntries& package, std::ve
287290
// Each time through the loop, we compare the best transaction in
288291
// mapModifiedTxs with the next transaction in the mempool to decide what
289292
// transaction package to work on next.
290-
void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpdated)
293+
void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSelected, int& nDescendantsUpdated)
291294
{
292-
AssertLockHeld(m_mempool.cs);
295+
AssertLockHeld(mempool.cs);
293296

294297
// mapModifiedTx will store sorted packages after they are modified
295298
// because some of their txs are already in the block
296299
indexed_modified_transaction_set mapModifiedTx;
297300
// Keep track of entries that failed inclusion, to avoid duplicate work
298301
CTxMemPool::setEntries failedTx;
299302

300-
CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = m_mempool.mapTx.get<ancestor_score>().begin();
303+
CTxMemPool::indexed_transaction_set::index<ancestor_score>::type::iterator mi = mempool.mapTx.get<ancestor_score>().begin();
301304
CTxMemPool::txiter iter;
302305

303306
// Limit the number of attempts to add transactions to the block when it is
@@ -306,7 +309,7 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda
306309
const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
307310
int64_t nConsecutiveFailed = 0;
308311

309-
while (mi != m_mempool.mapTx.get<ancestor_score>().end() || !mapModifiedTx.empty()) {
312+
while (mi != mempool.mapTx.get<ancestor_score>().end() || !mapModifiedTx.empty()) {
310313
// First try to find a new transaction in mapTx to evaluate.
311314
//
312315
// Skip entries in mapTx that are already in a block or are present
@@ -320,9 +323,9 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda
320323
// cached size/sigops/fee values that are not actually correct.
321324
/** Return true if given transaction from mapTx has already been evaluated,
322325
* or if the transaction's cached data in mapTx is incorrect. */
323-
if (mi != m_mempool.mapTx.get<ancestor_score>().end()) {
324-
auto it = m_mempool.mapTx.project<0>(mi);
325-
assert(it != m_mempool.mapTx.end());
326+
if (mi != mempool.mapTx.get<ancestor_score>().end()) {
327+
auto it = mempool.mapTx.project<0>(mi);
328+
assert(it != mempool.mapTx.end());
326329
if (mapModifiedTx.count(it) || inBlock.count(it) || failedTx.count(it)) {
327330
++mi;
328331
continue;
@@ -334,13 +337,13 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda
334337
bool fUsingModified = false;
335338

336339
modtxscoreiter modit = mapModifiedTx.get<ancestor_score>().begin();
337-
if (mi == m_mempool.mapTx.get<ancestor_score>().end()) {
340+
if (mi == mempool.mapTx.get<ancestor_score>().end()) {
338341
// We're out of entries in mapTx; use the entry from mapModifiedTx
339342
iter = modit->iter;
340343
fUsingModified = true;
341344
} else {
342345
// Try to compare the mapTx entry to the mapModifiedTx entry
343-
iter = m_mempool.mapTx.project<0>(mi);
346+
iter = mempool.mapTx.project<0>(mi);
344347
if (modit != mapModifiedTx.get<ancestor_score>().end() &&
345348
CompareTxMemPoolEntryByAncestorFee()(*modit, CTxMemPoolModifiedEntry(iter))) {
346349
// The best entry in mapModifiedTx has higher score
@@ -395,7 +398,7 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda
395398
CTxMemPool::setEntries ancestors;
396399
uint64_t nNoLimit = std::numeric_limits<uint64_t>::max();
397400
std::string dummy;
398-
m_mempool.CalculateMemPoolAncestors(*iter, ancestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
401+
mempool.CalculateMemPoolAncestors(*iter, ancestors, nNoLimit, nNoLimit, nNoLimit, nNoLimit, dummy, false);
399402

400403
onlyUnconfirmed(ancestors);
401404
ancestors.insert(iter);
@@ -425,7 +428,7 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda
425428
++nPackagesSelected;
426429

427430
// Update transactions that depend on each of these
428-
nDescendantsUpdated += UpdatePackagesForAdded(m_mempool, ancestors, mapModifiedTx);
431+
nDescendantsUpdated += UpdatePackagesForAdded(mempool, ancestors, mapModifiedTx);
429432
}
430433
}
431434
} // namespace node

src/node/miner.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class BlockAssembler
147147
int64_t m_lock_time_cutoff;
148148

149149
const CChainParams& chainparams;
150-
const CTxMemPool& m_mempool;
150+
const CTxMemPool* const m_mempool;
151151
CChainState& m_chainstate;
152152

153153
public:
@@ -157,8 +157,8 @@ class BlockAssembler
157157
CFeeRate blockMinFeeRate;
158158
};
159159

160-
explicit BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool);
161-
explicit BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const Options& options);
160+
explicit BlockAssembler(CChainState& chainstate, const CTxMemPool* mempool);
161+
explicit BlockAssembler(CChainState& chainstate, const CTxMemPool* mempool, const Options& options);
162162

163163
/** Construct a new block template with coinbase to scriptPubKeyIn */
164164
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
@@ -177,7 +177,7 @@ class BlockAssembler
177177
/** Add transactions based on feerate including unconfirmed ancestors
178178
* Increments nPackagesSelected / nDescendantsUpdated with corresponding
179179
* statistics from the package selection (for logging statistics). */
180-
void addPackageTxs(int& nPackagesSelected, int& nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs);
180+
void addPackageTxs(const CTxMemPool& mempool, int& nPackagesSelected, int& nDescendantsUpdated) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
181181

182182
// helper functions for addPackageTxs()
183183
/** Remove confirmed (inBlock) entries from given set */

src/rpc/mining.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
144144
{
145145
UniValue blockHashes(UniValue::VARR);
146146
while (nGenerate > 0 && !ShutdownRequested()) {
147-
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler{chainman.ActiveChainstate(), mempool}.CreateNewBlock(coinbase_script));
147+
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler{chainman.ActiveChainstate(), &mempool}.CreateNewBlock(coinbase_script));
148148
if (!pblocktemplate.get())
149149
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
150150
CBlock *pblock = &pblocktemplate->block;
@@ -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(), nullptr}.CreateNewBlock(coinbase_script));
359358
if (!blocktemplate) {
360359
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
361360
}
@@ -753,7 +752,7 @@ static RPCHelpMan getblocktemplate()
753752

754753
// Create new block
755754
CScript scriptDummy = CScript() << OP_TRUE;
756-
pblocktemplate = BlockAssembler{active_chainstate, mempool}.CreateNewBlock(scriptDummy);
755+
pblocktemplate = BlockAssembler{active_chainstate, &mempool}.CreateNewBlock(scriptDummy);
757756
if (!pblocktemplate)
758757
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
759758

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.get()}.CreateNewBlock(scriptPubKey);
6969
CBlock& block = pblocktemplate->block;
7070
block.hashPrevBlock = prev->GetBlockHash();
7171
block.nTime = prev->nTime + 1;

src/test/fuzz/tx_pool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void Finish(FuzzedDataProvider& fuzzed_data_provider, MockedTxPool& tx_pool, CCh
9797
BlockAssembler::Options options;
9898
options.nBlockMaxWeight = fuzzed_data_provider.ConsumeIntegralInRange(0U, MAX_BLOCK_WEIGHT);
9999
options.blockMinFeeRate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
100-
auto assembler = BlockAssembler{chainstate, *static_cast<CTxMemPool*>(&tx_pool), options};
100+
auto assembler = BlockAssembler{chainstate, &tx_pool, options};
101101
auto block_template = assembler.CreateNewBlock(CScript{} << OP_TRUE);
102102
Assert(block_template->block.vtx.size() >= 1);
103103
}

src/test/miner_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ BlockAssembler MinerTestingSetup::AssemblerForTest(const CChainParams& params)
5252

5353
options.nBlockMaxWeight = MAX_BLOCK_WEIGHT;
5454
options.blockMinFeeRate = blockMinFeeRate;
55-
return BlockAssembler{m_node.chainman->ActiveChainstate(), *m_node.mempool, options};
55+
return BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options};
5656
}
5757

5858
constexpr static struct {

src/test/util/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
7777
std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
7878
{
7979
auto block = std::make_shared<CBlock>(
80-
BlockAssembler{Assert(node.chainman)->ActiveChainstate(), *Assert(node.mempool)}
80+
BlockAssembler{Assert(node.chainman)->ActiveChainstate(), Assert(node.mempool.get())}
8181
.CreateNewBlock(coinbase_scriptPubKey)
8282
->block);
8383

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, nullptr}.CreateNewBlock(scriptPubKey)->block;
282281

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

src/test/validation_block_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ std::shared_ptr<CBlock> MinerTestingSetup::Block(const uint256& prev_hash)
6565
static int i = 0;
6666
static uint64_t time = Params().GenesisBlock().nTime;
6767

68-
auto ptemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), *m_node.mempool}.CreateNewBlock(CScript{} << i++ << OP_TRUE);
68+
auto ptemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get()}.CreateNewBlock(CScript{} << i++ << OP_TRUE);
6969
auto pblock = std::make_shared<CBlock>(ptemplate->block);
7070
pblock->hashPrevBlock = prev_hash;
7171
pblock->nTime = ++time;
@@ -327,7 +327,7 @@ BOOST_AUTO_TEST_CASE(witness_commitment_index)
327327
{
328328
CScript pubKey;
329329
pubKey << 1 << OP_TRUE;
330-
auto ptemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), *m_node.mempool}.CreateNewBlock(pubKey);
330+
auto ptemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get()}.CreateNewBlock(pubKey);
331331
CBlock pblock = ptemplate->block;
332332

333333
CTxOut witness;

0 commit comments

Comments
 (0)