p2p: clean up a 1p1c package child that is already in the mempool by instagibbs · Pull Request #36578 · bitcoin/bitcoin · GitHub
Skip to content

p2p: clean up a 1p1c package child that is already in the mempool - #36578

Open
instagibbs wants to merge 1 commit into
bitcoin:masterfrom
instagibbs:2026-10-1p1c-orphan-in-mempool
Open

instagibbs wants to merge 1 commit into
bitcoin:masterfrom
instagibbs:2026-10-1p1c-orphan-in-mempool

Conversation

@instagibbs

@instagibbs instagibbs commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

An orphan that enters the mempool other than through peer relay (e.g. by
RPC) stays in the orphanage, so Find1P1CPackage can pick it as the child
of a reconsiderable parent, and package evaluation then reports it as
MEMPOOL_ENTRY, which hits Assume(false). Have Find1P1CPackage erase such
children from the orphanage and skip them.

@DrahtBot DrahtBot added the P2P label Oct 19, 2026
@DrahtBot

DrahtBot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

@instagibbs

Copy link
Copy Markdown
Member Author

@ismaelsadeeq

@Yudis-bit

Copy link
Copy Markdown

Tested ACK b0dda32 on Windows.

@ismaelsadeeq

Copy link
Copy Markdown
Member

Not sure, the Assume(false) removed is a no-op in release, and the replacement cleans the orphan only reactively when it's later re-selected as a 1p1c child. If that never happens, the orphan dangles until it expires. I'd rather we clean the orphanage proactively on mempool entry, via TransactionAddedToMempool? so an orphan that enters by any path is removed regardless. This is redundant ofc with the existing p2p cleanup for relayed txs, but we need both: the notification is async and can be processed before the orphan is inserted (it's EraseTx then no-ops), so the synchronous p2p path still does useful work.

diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index ae329489e6..f23303605f 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -576,6 +576,8 @@ public:
     /** Overridden from CValidationInterface. */
     void ActiveTipChange(const CBlockIndex& new_tip, bool) override
         EXCLUSIVE_LOCKS_REQUIRED(!m_tx_download_mutex);
+    void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t mempool_sequence) override
+        EXCLUSIVE_LOCKS_REQUIRED(!m_tx_download_mutex);
     void BlockConnected(const ChainstateRole& role, const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override
         EXCLUSIVE_LOCKS_REQUIRED(!m_tx_download_mutex);
     void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) override
@@ -2190,6 +2192,12 @@ void PeerManagerImpl::ActiveTipChange(const CBlockIndex& new_tip, bool is_ibd)
     }
 }

+void PeerManagerImpl::TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t mempool_sequence)
+{
+    LOCK(m_tx_download_mutex);
+    m_txdownloadman.MempoolAcceptedTx(tx.info.m_tx);
+}
+
 /**
  * Evict orphan txn pool entries based on a newly connected
  * block, remember the recently confirmed transactions, and delete tracked
diff --git a/test/functional/p2p_orphan_handling.py b/test/functional/p2p_orphan_handling.py
index b6e521dbe4..056bd72823 100755
--- a/test/functional/p2p_orphan_handling.py
+++ b/test/functional/p2p_orphan_handling.py
@@ -841,7 +841,7 @@ class OrphanHandlingTest(BitcoinTestFramework):

     @cleanup
     def test_orphan_in_mempool(self):
-        self.log.info("Test that an orphan in the mempool is removed from the orphanage when found as the child of a 1p1c package")
+        self.log.info("Test that an orphan is removed from the orphanage when it enters the mempool other than through peer relay")
         node = self.nodes[0]
         parent = self.wallet.create_self_transfer(fee_rate=0)
         child = self.wallet.create_self_transfer(utxo_to_spend=parent["new_utxo"])
@@ -854,12 +854,10 @@ class OrphanHandlingTest(BitcoinTestFramework):
         peer.send_and_ping(msg_tx(child["tx"]))
         assert tx_in_orphanage(node, child["tx"])

-        # The child enters the mempool by RPC, which does not remove it from the orphanage.
+        # The child enters the mempool by RPC. Even though this is not through peer relay, it is
+        # still removed from the orphanage.
         assert_equal(node.submitpackage([parent["hex"], child["hex"]])["package_msg"], "success")
-        assert tx_in_orphanage(node, child["tx"])
-
-        # The same parent version is paired with the orphan, which turns out to be in the mempool.
-        peer.send_and_ping(msg_tx(parent_bad_wit))
+        node.syncwithvalidationinterfacequeue()
         assert not tx_in_orphanage(node, child["tx"])
         assert child["txid"] in node.getrawmempool()

@instagibbs

Copy link
Copy Markdown
Member Author

Not sure, the Assume(false) removed is a no-op in release

This would not have been an open issue otherwise :)

I'd rather we clean the orphanage proactively on mempool entry

I think if we're going to do that, we should simply have it call EraseTx to avoid additional AddChildrenToWorkSet invocations.

Another alternative to the synchronous piece in this PR would to have Find1P1CPackage detect children in the mempool as well as orphanage and clean it up there.

@ismaelsadeeq

Copy link
Copy Markdown
Member

I think if we're going to do that, we should simply have it call EraseTx to avoid additional AddChildrenToWorkSet invocations.

Seems better to me; is there something I am missing about why you take this approach in this PR?

An orphan that enters the mempool other than through peer relay (e.g. by
RPC) stays in the orphanage, so Find1P1CPackage can pick it as the child
of a reconsiderable parent, and package evaluation then reports it as
MEMPOOL_ENTRY, which hits Assume(false). Have Find1P1CPackage erase such
children from the orphanage and skip them.
@instagibbs

Copy link
Copy Markdown
Member Author

@ismaelsadeeq swtiched to the Find1P1CPackage strategy for now; can still be convinced the extra machinery to async sweep them and taking lock is worth it, but keeping strategy as is for now

@instagibbs
instagibbs force-pushed the 2026-10-1p1c-orphan-in-mempool branch from b0dda32 to a93bdc8 Compare October 21, 2026 14:04
@fanquake fanquake added this to the 33.0 milestone Oct 21, 2026
@fametrano

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants