Skip to content

Commit bb9078e

Browse files
committed
[refactor] put finality and maturity checking into a lambda
No behavior change.
1 parent bedf246 commit bb9078e

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/txmempool.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,13 @@ void CTxMemPool::removeForReorg(CChainState& active_chainstate, int flags)
638638
{
639639
// Remove transactions spending a coinbase which are now immature and no-longer-final transactions
640640
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);
643648
const CTransaction& tx = it->GetTx();
644649
LockPoints lp = it->GetLockPoints();
645650
const bool validLP = TestLockPointValidity(active_chainstate.m_chain, &lp);
@@ -648,7 +653,7 @@ void CTxMemPool::removeForReorg(CChainState& active_chainstate, int flags)
648653
|| !CheckSequenceLocks(active_chainstate.m_chain.Tip(), view_mempool, tx, flags, &lp, validLP)) {
649654
// Note if CheckSequenceLocks fails the LockPoints may still be invalid
650655
// So it's critical that we remove the tx and not depend on the LockPoints.
651-
txToRemove.insert(it);
656+
should_remove = true;
652657
} else if (it->GetSpendsCoinbase()) {
653658
for (const CTxIn& txin : tx.vin) {
654659
indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
@@ -658,11 +663,17 @@ void CTxMemPool::removeForReorg(CChainState& active_chainstate, int flags)
658663
if (m_check_ratio != 0) assert(!coin.IsSpent());
659664
unsigned int nMemPoolHeight = active_chainstate.m_chain.Tip()->nHeight + 1;
660665
if (coin.IsSpent() || (coin.IsCoinBase() && ((signed long)nMemPoolHeight) - coin.nHeight < COINBASE_MATURITY)) {
661-
txToRemove.insert(it);
666+
should_remove = true;
662667
break;
663668
}
664669
}
665670
}
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);
666677
}
667678
setEntries setAllRemoves;
668679
for (txiter it : txToRemove) {

0 commit comments

Comments
 (0)