@@ -638,8 +638,13 @@ void CTxMemPool::removeForReorg(CChainState& active_chainstate, int flags)
638
638
{
639
639
// Remove transactions spending a coinbase which are now immature and no-longer-final transactions
640
640
AssertLockHeld (cs);
641
- setEntries txToRemove;
642
- for (indexed_transaction_set::const_iterator it = mapTx.begin (); it != mapTx.end (); it++) {
641
+ AssertLockHeld (::cs_main);
642
+
643
+ const auto check_final_and_mature = [this , &active_chainstate, flags](txiter it)
644
+ EXCLUSIVE_LOCKS_REQUIRED (cs, ::cs_main) {
645
+ bool should_remove = false ;
646
+ AssertLockHeld (cs);
647
+ AssertLockHeld (::cs_main);
643
648
const CTransaction& tx = it->GetTx ();
644
649
LockPoints lp = it->GetLockPoints ();
645
650
const bool validLP = TestLockPointValidity (active_chainstate.m_chain , &lp);
@@ -648,7 +653,7 @@ void CTxMemPool::removeForReorg(CChainState& active_chainstate, int flags)
648
653
|| !CheckSequenceLocks (active_chainstate.m_chain .Tip (), view_mempool, tx, flags, &lp, validLP)) {
649
654
// Note if CheckSequenceLocks fails the LockPoints may still be invalid
650
655
// So it's critical that we remove the tx and not depend on the LockPoints.
651
- txToRemove. insert (it) ;
656
+ should_remove = true ;
652
657
} else if (it->GetSpendsCoinbase ()) {
653
658
for (const CTxIn& txin : tx.vin ) {
654
659
indexed_transaction_set::const_iterator it2 = mapTx.find (txin.prevout .hash );
@@ -658,11 +663,17 @@ void CTxMemPool::removeForReorg(CChainState& active_chainstate, int flags)
658
663
if (m_check_ratio != 0 ) assert (!coin.IsSpent ());
659
664
unsigned int nMemPoolHeight = active_chainstate.m_chain .Tip ()->nHeight + 1 ;
660
665
if (coin.IsSpent () || (coin.IsCoinBase () && ((signed long )nMemPoolHeight) - coin.nHeight < COINBASE_MATURITY)) {
661
- txToRemove. insert (it) ;
666
+ should_remove = true ;
662
667
break ;
663
668
}
664
669
}
665
670
}
671
+ return should_remove;
672
+ };
673
+
674
+ setEntries txToRemove;
675
+ for (indexed_transaction_set::const_iterator it = mapTx.begin (); it != mapTx.end (); it++) {
676
+ if (check_final_and_mature (it)) txToRemove.insert (it);
666
677
}
667
678
setEntries setAllRemoves;
668
679
for (txiter it : txToRemove) {
0 commit comments