@@ -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
/* *
@@ -2251,7 +2247,7 @@ static void UpdateTip(CTxMemPool* mempool, const CBlockIndex* pindexNew, const C
2251
2247
/* * Disconnect m_chain's tip.
2252
2248
* After calling, the mempool will be in an inconsistent state, with
2253
2249
* transactions from disconnected blocks being added to disconnectpool. You
2254
- * should make the mempool consistent again by calling UpdateMempoolForReorg .
2250
+ * should make the mempool consistent again by calling MaybeUpdateMempoolForReorg .
2255
2251
* with cs_main held.
2256
2252
*
2257
2253
* If disconnectpool is nullptr, then no disconnected transactions are added to
@@ -2516,7 +2512,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
2516
2512
if (!DisconnectTip (state, &disconnectpool)) {
2517
2513
// This is likely a fatal error, but keep the mempool consistent,
2518
2514
// just in case. Only remove from the mempool in this case.
2519
- if (m_mempool) UpdateMempoolForReorg (* this , *m_mempool, disconnectpool, false );
2515
+ MaybeUpdateMempoolForReorg ( disconnectpool, false );
2520
2516
2521
2517
// If we're unable to disconnect a block during normal operation,
2522
2518
// then that is a failure of our local system -- we should abort
@@ -2560,7 +2556,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
2560
2556
// A system error occurred (disk space, database error, ...).
2561
2557
// Make the mempool consistent with the current tip, just in case
2562
2558
// any observers try to use it before shutdown.
2563
- if (m_mempool) UpdateMempoolForReorg (* this , *m_mempool, disconnectpool, false );
2559
+ MaybeUpdateMempoolForReorg ( disconnectpool, false );
2564
2560
return false ;
2565
2561
}
2566
2562
} else {
@@ -2574,10 +2570,10 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
2574
2570
}
2575
2571
}
2576
2572
2577
- if (fBlocksDisconnected && m_mempool ) {
2573
+ if (fBlocksDisconnected ) {
2578
2574
// If any blocks were disconnected, disconnectpool may be non empty. Add
2579
2575
// any disconnected transactions back to the mempool.
2580
- UpdateMempoolForReorg (* this , *m_mempool, disconnectpool, true );
2576
+ MaybeUpdateMempoolForReorg ( disconnectpool, true );
2581
2577
}
2582
2578
if (m_mempool) m_mempool->check (*this );
2583
2579
@@ -2802,7 +2798,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
2802
2798
LimitValidationInterfaceQueue ();
2803
2799
2804
2800
LOCK (cs_main);
2805
- // Lock for as long as disconnectpool is in scope to make sure UpdateMempoolForReorg is
2801
+ // Lock for as long as disconnectpool is in scope to make sure MaybeUpdateMempoolForReorg is
2806
2802
// called after DisconnectTip without unlocking in between
2807
2803
LOCK (MempoolMutex ());
2808
2804
if (!m_chain.Contains (pindex)) break ;
@@ -2818,9 +2814,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
2818
2814
// transactions back to the mempool if disconnecting was successful,
2819
2815
// and we're not doing a very deep invalidation (in which case
2820
2816
// keeping the mempool up to date is probably futile anyway).
2821
- if (m_mempool) {
2822
- UpdateMempoolForReorg (*this , *m_mempool, disconnectpool, /* fAddToMempool = */ (++disconnected <= 10 ) && ret);
2823
- }
2817
+ MaybeUpdateMempoolForReorg (disconnectpool, /* fAddToMempool = */ (++disconnected <= 10 ) && ret);
2824
2818
if (!ret) return false ;
2825
2819
assert (invalid_walk_tip->pprev == m_chain.Tip ());
2826
2820
0 commit comments