@@ -329,24 +329,14 @@ static bool IsCurrentForFeeEstimation(CChainState& active_chainstate) EXCLUSIVE_
329329 return true ;
330330}
331331
332- /* *
333- * Make mempool consistent after a reorg, by re-adding or recursively erasing
334- * disconnected block transactions from the mempool, and also removing any
335- * other transactions from the mempool that are no longer valid given the new
336- * tip/height.
337- *
338- * Note: we assume that disconnectpool only contains transactions that are NOT
339- * confirmed in the current chain nor already in the mempool (otherwise,
340- * in-mempool descendants of such transactions would be removed).
341- *
342- * Passing fAddToMempool=false will skip trying to add the transactions back,
343- * and instead just erase from the mempool as needed.
344- */
345-
346- static void UpdateMempoolForReorg (CChainState& active_chainstate, CTxMemPool& mempool, DisconnectedBlockTransactions& disconnectpool, bool fAddToMempool ) EXCLUSIVE_LOCKS_REQUIRED(cs_main, mempool.cs)
332+ void CChainState::MaybeUpdateMempoolForReorg (
333+ DisconnectedBlockTransactions& disconnectpool,
334+ bool fAddToMempool )
347335{
336+ if (!m_mempool) return ;
337+
348338 AssertLockHeld (cs_main);
349- AssertLockHeld (mempool. cs );
339+ AssertLockHeld (m_mempool-> cs );
350340 std::vector<uint256> vHashUpdate;
351341 // disconnectpool's insertion_order index sorts the entries from
352342 // oldest to newest, but the oldest entry will be the last tx from the
@@ -358,11 +348,13 @@ static void UpdateMempoolForReorg(CChainState& active_chainstate, CTxMemPool& me
358348 while (it != disconnectpool.queuedTx .get <insertion_order>().rend ()) {
359349 // ignore validation errors in resurrected transactions
360350 if (!fAddToMempool || (*it)->IsCoinBase () ||
361- AcceptToMemoryPool (active_chainstate, mempool, *it, true /* bypass_limits */ ).m_result_type != MempoolAcceptResult::ResultType::VALID) {
351+ AcceptToMemoryPool (
352+ *this , *m_mempool, *it, true /* bypass_limits */ ).m_result_type !=
353+ MempoolAcceptResult::ResultType::VALID) {
362354 // If the transaction doesn't make it in to the mempool, remove any
363355 // transactions that depend on it (which would now be orphans).
364- mempool. removeRecursive (**it, MemPoolRemovalReason::REORG);
365- } else if (mempool. exists ((*it)->GetHash ())) {
356+ m_mempool-> removeRecursive (**it, MemPoolRemovalReason::REORG);
357+ } else if (m_mempool-> exists ((*it)->GetHash ())) {
366358 vHashUpdate.push_back ((*it)->GetHash ());
367359 }
368360 ++it;
@@ -373,12 +365,16 @@ static void UpdateMempoolForReorg(CChainState& active_chainstate, CTxMemPool& me
373365 // previously-confirmed transactions back to the mempool.
374366 // UpdateTransactionsFromBlock finds descendants of any transactions in
375367 // the disconnectpool that were added back and cleans up the mempool state.
376- mempool. UpdateTransactionsFromBlock (vHashUpdate);
368+ m_mempool-> UpdateTransactionsFromBlock (vHashUpdate);
377369
378370 // We also need to remove any now-immature transactions
379- mempool. removeForReorg (active_chainstate , STANDARD_LOCKTIME_VERIFY_FLAGS);
371+ m_mempool-> removeForReorg (* this , STANDARD_LOCKTIME_VERIFY_FLAGS);
380372 // Re-limit mempool size, in case we added any transactions
381- LimitMempoolSize (mempool, active_chainstate.CoinsTip (), gArgs .GetArg (" -maxmempool" , DEFAULT_MAX_MEMPOOL_SIZE) * 1000000 , std::chrono::hours{gArgs .GetArg (" -mempoolexpiry" , DEFAULT_MEMPOOL_EXPIRY)});
373+ LimitMempoolSize (
374+ *m_mempool,
375+ this ->CoinsTip (),
376+ gArgs .GetArg (" -maxmempool" , DEFAULT_MAX_MEMPOOL_SIZE) * 1000000 ,
377+ std::chrono::hours{gArgs .GetArg (" -mempoolexpiry" , DEFAULT_MEMPOOL_EXPIRY)});
382378}
383379
384380/* *
@@ -2247,7 +2243,7 @@ static void UpdateTip(CTxMemPool* mempool, const CBlockIndex* pindexNew, const C
22472243/* * Disconnect m_chain's tip.
22482244 * After calling, the mempool will be in an inconsistent state, with
22492245 * transactions from disconnected blocks being added to disconnectpool. You
2250- * should make the mempool consistent again by calling UpdateMempoolForReorg .
2246+ * should make the mempool consistent again by calling MaybeUpdateMempoolForReorg .
22512247 * with cs_main held.
22522248 *
22532249 * If disconnectpool is nullptr, then no disconnected transactions are added to
@@ -2512,7 +2508,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
25122508 if (!DisconnectTip (state, &disconnectpool)) {
25132509 // This is likely a fatal error, but keep the mempool consistent,
25142510 // just in case. Only remove from the mempool in this case.
2515- if (m_mempool) UpdateMempoolForReorg (* this , *m_mempool, disconnectpool, false );
2511+ MaybeUpdateMempoolForReorg ( disconnectpool, false );
25162512
25172513 // If we're unable to disconnect a block during normal operation,
25182514 // then that is a failure of our local system -- we should abort
@@ -2556,7 +2552,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
25562552 // A system error occurred (disk space, database error, ...).
25572553 // Make the mempool consistent with the current tip, just in case
25582554 // any observers try to use it before shutdown.
2559- if (m_mempool) UpdateMempoolForReorg (* this , *m_mempool, disconnectpool, false );
2555+ MaybeUpdateMempoolForReorg ( disconnectpool, false );
25602556 return false ;
25612557 }
25622558 } else {
@@ -2570,10 +2566,10 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
25702566 }
25712567 }
25722568
2573- if (fBlocksDisconnected && m_mempool ) {
2569+ if (fBlocksDisconnected ) {
25742570 // If any blocks were disconnected, disconnectpool may be non empty. Add
25752571 // any disconnected transactions back to the mempool.
2576- UpdateMempoolForReorg (* this , *m_mempool, disconnectpool, true );
2572+ MaybeUpdateMempoolForReorg ( disconnectpool, true );
25772573 }
25782574 if (m_mempool) m_mempool->check (*this );
25792575
@@ -2798,7 +2794,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
27982794 LimitValidationInterfaceQueue ();
27992795
28002796 LOCK (cs_main);
2801- // Lock for as long as disconnectpool is in scope to make sure UpdateMempoolForReorg is
2797+ // Lock for as long as disconnectpool is in scope to make sure MaybeUpdateMempoolForReorg is
28022798 // called after DisconnectTip without unlocking in between
28032799 LOCK (MempoolMutex ());
28042800 if (!m_chain.Contains (pindex)) break ;
@@ -2814,9 +2810,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
28142810 // transactions back to the mempool if disconnecting was successful,
28152811 // and we're not doing a very deep invalidation (in which case
28162812 // keeping the mempool up to date is probably futile anyway).
2817- if (m_mempool) {
2818- UpdateMempoolForReorg (*this , *m_mempool, disconnectpool, /* fAddToMempool = */ (++disconnected <= 10 ) && ret);
2819- }
2813+ MaybeUpdateMempoolForReorg (disconnectpool, /* fAddToMempool = */ (++disconnected <= 10 ) && ret);
28202814 if (!ret) return false ;
28212815 assert (invalid_walk_tip->pprev == m_chain.Tip ());
28222816
0 commit comments