@@ -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
9393void 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
0 commit comments