@@ -329,24 +329,14 @@ static bool IsCurrentForFeeEstimation(CChainState& active_chainstate) EXCLUSIVE_
329
329
return true ;
330
330
}
331
331
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 )
347
335
{
336
+ if (!m_mempool) return ;
337
+
348
338
AssertLockHeld (cs_main);
349
- AssertLockHeld (mempool. cs );
339
+ AssertLockHeld (m_mempool-> cs );
350
340
std::vector<uint256> vHashUpdate;
351
341
// disconnectpool's insertion_order index sorts the entries from
352
342
// 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
358
348
while (it != disconnectpool.queuedTx .get <insertion_order>().rend ()) {
359
349
// ignore validation errors in resurrected transactions
360
350
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) {
362
354
// If the transaction doesn't make it in to the mempool, remove any
363
355
// 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 ())) {
366
358
vHashUpdate.push_back ((*it)->GetHash ());
367
359
}
368
360
++it;
@@ -373,12 +365,16 @@ static void UpdateMempoolForReorg(CChainState& active_chainstate, CTxMemPool& me
373
365
// previously-confirmed transactions back to the mempool.
374
366
// UpdateTransactionsFromBlock finds descendants of any transactions in
375
367
// the disconnectpool that were added back and cleans up the mempool state.
376
- mempool. UpdateTransactionsFromBlock (vHashUpdate);
368
+ m_mempool-> UpdateTransactionsFromBlock (vHashUpdate);
377
369
378
370
// 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);
380
372
// 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)});
382
378
}
383
379
384
380
/* *
@@ -2247,7 +2243,7 @@ static void UpdateTip(CTxMemPool* mempool, const CBlockIndex* pindexNew, const C
2247
2243
/* * Disconnect m_chain's tip.
2248
2244
* After calling, the mempool will be in an inconsistent state, with
2249
2245
* 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 .
2251
2247
* with cs_main held.
2252
2248
*
2253
2249
* If disconnectpool is nullptr, then no disconnected transactions are added to
@@ -2512,7 +2508,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
2512
2508
if (!DisconnectTip (state, &disconnectpool)) {
2513
2509
// This is likely a fatal error, but keep the mempool consistent,
2514
2510
// 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 );
2516
2512
2517
2513
// If we're unable to disconnect a block during normal operation,
2518
2514
// then that is a failure of our local system -- we should abort
@@ -2556,7 +2552,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
2556
2552
// A system error occurred (disk space, database error, ...).
2557
2553
// Make the mempool consistent with the current tip, just in case
2558
2554
// any observers try to use it before shutdown.
2559
- if (m_mempool) UpdateMempoolForReorg (* this , *m_mempool, disconnectpool, false );
2555
+ MaybeUpdateMempoolForReorg ( disconnectpool, false );
2560
2556
return false ;
2561
2557
}
2562
2558
} else {
@@ -2570,10 +2566,10 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
2570
2566
}
2571
2567
}
2572
2568
2573
- if (fBlocksDisconnected && m_mempool ) {
2569
+ if (fBlocksDisconnected ) {
2574
2570
// If any blocks were disconnected, disconnectpool may be non empty. Add
2575
2571
// any disconnected transactions back to the mempool.
2576
- UpdateMempoolForReorg (* this , *m_mempool, disconnectpool, true );
2572
+ MaybeUpdateMempoolForReorg ( disconnectpool, true );
2577
2573
}
2578
2574
if (m_mempool) m_mempool->check (*this );
2579
2575
@@ -2798,7 +2794,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
2798
2794
LimitValidationInterfaceQueue ();
2799
2795
2800
2796
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
2802
2798
// called after DisconnectTip without unlocking in between
2803
2799
LOCK (MempoolMutex ());
2804
2800
if (!m_chain.Contains (pindex)) break ;
@@ -2814,9 +2810,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
2814
2810
// transactions back to the mempool if disconnecting was successful,
2815
2811
// and we're not doing a very deep invalidation (in which case
2816
2812
// 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);
2820
2814
if (!ret) return false ;
2821
2815
assert (invalid_walk_tip->pprev == m_chain.Tip ());
2822
2816
0 commit comments