Simplify removeRecursive

This commit is contained in:
Suhas Daftuar committed 2025-11-30 10:57:48 -05:00
1 parent 01d8520038
commit a5a7905d83
2 files changed
+37 -23

No files matched your search

+29 -23
View File
@@ -308,35 +308,41 @@ CTxMemPool::txiter CTxMemPool::CalculateDescendants(const CTxMemPoolEntry& entry
return mapTx.iterator_to(entry); return mapTx.iterator_to(entry);
} }
void CTxMemPool::removeRecursive(CTxMemPool::txiter to_remove, MemPoolRemovalReason reason)
{
AssertLockHeld(cs);
Assume(!m_have_changeset);
auto descendants = m_txgraph->GetDescendants(*to_remove, TxGraph::Level::MAIN);
for (auto tx: descendants) {
removeUnchecked(mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*tx)), reason);
}
}
void CTxMemPool::removeRecursive(const CTransaction &origTx, MemPoolRemovalReason reason) void CTxMemPool::removeRecursive(const CTransaction &origTx, MemPoolRemovalReason reason)
{ {
// Remove transaction from memory pool // Remove transaction from memory pool
AssertLockHeld(cs); AssertLockHeld(cs);
Assume(!m_have_changeset); Assume(!m_have_changeset);
setEntries txToRemove; txiter origit = mapTx.find(origTx.GetHash());
txiter origit = mapTx.find(origTx.GetHash()); if (origit != mapTx.end()) {
if (origit != mapTx.end()) { removeRecursive(origit, reason);
txToRemove.insert(origit); } else {
} else { // When recursively removing but origTx isn't in the mempool
// When recursively removing but origTx isn't in the mempool // be sure to remove any descendants that are in the pool. This can
// be sure to remove any children that are in the pool. This can // happen during chain re-orgs if origTx isn't re-accepted into
// happen during chain re-orgs if origTx isn't re-accepted into // the mempool for any reason.
// the mempool for any reason. auto iter = mapNextTx.lower_bound(COutPoint(origTx.GetHash(), 0));
for (unsigned int i = 0; i < origTx.vout.size(); i++) { std::vector<const TxGraph::Ref*> to_remove;
auto it = mapNextTx.find(COutPoint(origTx.GetHash(), i)); while (iter != mapNextTx.end() && iter->first->hash == origTx.GetHash()) {
if (it == mapNextTx.end()) to_remove.emplace_back(&*(iter->second));
continue; ++iter;
txiter nextit = it->second;
assert(nextit != mapTx.end());
txToRemove.insert(nextit);
}
} }
setEntries setAllRemoves; auto all_removes = m_txgraph->GetDescendantsUnion(to_remove, TxGraph::Level::MAIN);
for (txiter it : txToRemove) { for (auto ref : all_removes) {
CalculateDescendants(it, setAllRemoves); auto tx = mapTx.iterator_to(static_cast<const CTxMemPoolEntry&>(*ref));
removeUnchecked(tx, reason);
} }
}
RemoveStaged(setAllRemoves, reason);
} }
void CTxMemPool::removeForReorg(CChain& chain, std::function<bool(txiter)> check_final_and_mature) void CTxMemPool::removeForReorg(CChain& chain, std::function<bool(txiter)> check_final_and_mature)
@@ -372,7 +378,7 @@ void CTxMemPool::removeConflicts(const CTransaction &tx)
if (Assume(txConflict.GetHash() != tx.GetHash())) if (Assume(txConflict.GetHash() != tx.GetHash()))
{ {
ClearPrioritisation(txConflict.GetHash()); ClearPrioritisation(txConflict.GetHash());
removeRecursive(txConflict, MemPoolRemovalReason::CONFLICT); removeRecursive(it->second, MemPoolRemovalReason::CONFLICT);
} }
} }
} }
+8
View File
@@ -320,6 +320,11 @@ public:
*/ */
void check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main); void check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
/**
* Remove a transaction from the mempool along with any descendants.
* If the transaction is not already in the mempool, find any descendants
* and remove them.
*/
void removeRecursive(const CTransaction& tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs); void removeRecursive(const CTransaction& tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
/** After reorg, filter the entries that would no longer be valid in the next block, and update /** After reorg, filter the entries that would no longer be valid in the next block, and update
* the entries' cached LockPoints if needed. The mempool does not have any knowledge of * the entries' cached LockPoints if needed. The mempool does not have any knowledge of
@@ -581,6 +586,9 @@ private:
*/ */
void RemoveStaged(setEntries& stage, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs); void RemoveStaged(setEntries& stage, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
/* Helper for the public removeRecursive() */
void removeRecursive(txiter to_remove, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
/** Before calling removeUnchecked for a given transaction, /** Before calling removeUnchecked for a given transaction,
* UpdateForRemoveFromMempool must be called on the entire (dependent) set * UpdateForRemoveFromMempool must be called on the entire (dependent) set
* of transactions being removed at the same time. We use each * of transactions being removed at the same time. We use each