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

Manually merged
instagibbs merged 1 commit from 2026-10-1p1c-orphan-in-mempool into master 2026-11-04 15:01:59 +00:00 AGit
instagibbs commented 2026-10-19 21:08:45 +00:00 (Migrated from github.com)

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.

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 commented 2026-10-19 21:08:54 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK marcofleon, ismaelsadeeq
Stale ACK Yudis-bit, fametrano

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36015 (txorphanage: bound orphan memory by storing transactions serialized by brunoerg)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36578 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36578.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36578#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [marcofleon](https://github.com/bitcoin/bitcoin/pull/36578#pullrequestreview-5767825718), [ismaelsadeeq](https://github.com/bitcoin/bitcoin/pull/36578#issuecomment-6631101867) | | Stale ACK | [Yudis-bit](https://github.com/bitcoin/bitcoin/pull/36578#issuecomment-6286975704), [fametrano](https://github.com/bitcoin/bitcoin/pull/36578#issuecomment-6395070625) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36015](https://github.com/bitcoin/bitcoin/pull/36015) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36015.svg"></sub> (txorphanage: bound orphan memory by storing transactions serialized by brunoerg) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
instagibbs commented 2026-10-19 21:09:26 +00:00 (Migrated from github.com)

@ismaelsadeeq

@ismaelsadeeq
First-time contributor

No verified findings are available from this review.

Review coverage was incomplete.


Reviewed: 9e8e61ae8e39 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:965af1044d9e41839565702e4d9bd0a751eda81f6e8b6402747fa296ef84ebea:7efd3c3ba41667382004cfa7a7db56605da7d8f9b7ab69bc483f27117390ba48 --> <!-- ralph-base:fb19fed3df0fa791d6648dfc7d721fcd211d89c4 --> <!-- ralph-head:9e8e61ae8e398f36c52c7eb2c4e57f2359c9db8d --> <!-- ralph-case:de028183ee495db7c8e7c57204cce8108c1aa4a4fd0ec628be3a4a1223ebbbfb --> No verified findings are available from this review. Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`9e8e61ae8e39`](<https://git.fish.foo/bitcoin/bitcoin/commit/9e8e61ae8e398f36c52c7eb2c4e57f2359c9db8d>) · [Full review report](<https://review.fish.foo/traces/36578-9e8e61ae8e398f36c52c7eb2c4e57f2359c9db8d-2083-6.html>) · [Previous reports](<https://review.fish.foo/traces/36578/>) [permalink](<https://review.fish.foo/traces/36578-9e8e61ae8e398f36c52c7eb2c4e57f2359c9db8d-2083-6.html>)
Yudis-bit commented 2026-10-20 08:39:00 +00:00 (Migrated from github.com)

Tested ACK b0dda32aff on Windows.

Tested ACK b0dda32aff on Windows.
ismaelsadeeq commented 2026-10-21 10:14:43 +00:00 (Migrated from github.com)

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()


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 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 commented 2026-10-21 13:08:19 +00:00 (Migrated from github.com)

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.

> 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 commented 2026-10-21 13:33:52 +00:00 (Migrated from github.com)

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?

> 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?
instagibbs commented 2026-10-21 13:56:08 +00:00 (Migrated from github.com)

@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

@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
fametrano commented 2026-10-25 07:50:36 +00:00 (Migrated from github.com)

tACK a93bdc8320

The Assume(false) is also reached through sendrawtransaction, not only through submitpackage as in the new test. I checked on master in a Debug build. The parent pays the minimum relay fee for its own size. A peer sends a copy with a padded witness, which has the same txid but falls below the minimum relay feerate, so it is rejected as reconsiderable. The same peer's child is then kept as an orphan. Two sendrawtransaction calls put the real parent and the child in the mempool. When the peer sends the padded parent again, the node aborts at net_processing.cpp:3477. With this PR the child leaves the orphanage at that point and stays in the mempool.

In the same scenario the child stays in the orphanage after both sendrawtransaction calls, with or without this PR, until the padded parent pairs with it. That is the reactive cleanup ismaelsadeeq pointed out, and I agree with keeping a mempool hook out of this PR.

I tested the head merged into master a773e1e33b, in a Debug build, with p2p_orphan_handling (the new case aborts the node on master), p2p_opportunistic_1p1c and the txdownload and txpackage unit tests. I ran them again with #36594 and #36437 merged on top. I can share the functional test as a gist.

tACK a93bdc83202e00375b643e6df3b9daa801f7de23 The `Assume(false)` is also reached through `sendrawtransaction`, not only through `submitpackage` as in the new test. I checked on master in a Debug build. The parent pays the minimum relay fee for its own size. A peer sends a copy with a padded witness, which has the same txid but falls below the minimum relay feerate, so it is rejected as reconsiderable. The same peer's child is then kept as an orphan. Two `sendrawtransaction` calls put the real parent and the child in the mempool. When the peer sends the padded parent again, the node aborts at net_processing.cpp:3477. With this PR the child leaves the orphanage at that point and stays in the mempool. In the same scenario the child stays in the orphanage after both `sendrawtransaction` calls, with or without this PR, until the padded parent pairs with it. That is the reactive cleanup ismaelsadeeq pointed out, and I agree with keeping a mempool hook out of this PR. I tested the head merged into master a773e1e33b, in a Debug build, with p2p_orphan_handling (the new case aborts the node on master), p2p_opportunistic_1p1c and the txdownload and txpackage unit tests. I ran them again with #36594 and #36437 merged on top. I can share the functional test as a gist.
ismaelsadeeq (Migrated from github.com) reviewed 2026-11-02 13:02:16 +00:00
ismaelsadeeq (Migrated from github.com) left a comment

Code review ACK a93bdc8320

Code review ACK a93bdc83202e00375b643e6df3b9daa801f7de23
marcofleon (Migrated from github.com) reviewed 2026-11-03 17:53:03 +00:00
marcofleon (Migrated from github.com) commented 2026-11-03 17:53:04 +00:00

Is there a reason to check by wtxid and not txid? If there's an orphan with the same txid but different witness, we still evaluate the package here even though it can't get into the mempool.

I know it's still just a PR but In combination with 152f7b74ce in https://github.com/bitcoin/bitcoin/pull/36506, I think this means the orphan would end up staying in the orphanage.

Is there a reason to check by wtxid and not txid? If there's an orphan with the same txid but different witness, we still evaluate the package here even though it can't get into the mempool. I know it's still just a PR but In combination with 152f7b74ce14020d03cfaad007eee1d91183bf9b in https://github.com/bitcoin/bitcoin/pull/36506, I think this means the orphan would end up staying in the orphanage.
instagibbs (Migrated from github.com) reviewed 2026-11-03 18:04:26 +00:00
instagibbs (Migrated from github.com) commented 2026-11-03 18:04:26 +00:00

yes you're correct, and this would also cover the case where a different wtxid enters through p2p, which the current fix misses.

yes you're correct, and this would also cover the case where a different wtxid enters through p2p, which the current fix misses.
marcofleon (Migrated from github.com) reviewed 2026-11-04 11:58:27 +00:00
marcofleon (Migrated from github.com) left a comment
ACK 9e8e61ae8e398f36c52c7eb2c4e57f2359c9db8d
ismaelsadeeq commented 2026-11-04 12:53:06 +00:00 (Migrated from github.com)

reACK 9e8e61ae8e

reACK 9e8e61ae8e398f36c52c7eb2c4e57f2359c9db8d
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36578
No description provided.