net: make orphan reconsideration more granular #36594

Manually merged
darosior merged 2 commits from 2610_granular_orphan_reconsideration into master 2026-10-30 12:18:14 +00:00 AGit
darosior commented 2026-10-21 19:06:13 +00:00 (Migrated from github.com)

We currently concentrate the validation of all the assigned peer’s reconsiderable orphans that are still missing inputs in a single message-processing turn. This ties orphan processing to validation result, and adds latency to processing other peers' messages.

Always process a single reconsiderable orphan per turn instead. This removes the need for an immediate validation result, and shifts the latency from other peers to the assigned peer.

We currently concentrate the validation of all the assigned peer’s reconsiderable orphans that are still missing inputs in a single message-processing turn. This ties orphan processing to validation result, and adds latency to processing other peers' messages. Always process a single reconsiderable orphan per turn instead. This removes the need for an immediate validation result, and shifts the latency from other peers to the assigned peer.
DrahtBot commented 2026-10-21 19:06:23 +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 fametrano, ViniciusCestarii, furszy, instagibbs
Concept ACK mzumsande

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

<!--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/36594 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36594.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36594#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 | [fametrano](https://github.com/bitcoin/bitcoin/pull/36594#issuecomment-6395045852), [ViniciusCestarii](https://github.com/bitcoin/bitcoin/pull/36594#pullrequestreview-5650841204), [furszy](https://github.com/bitcoin/bitcoin/pull/36594#pullrequestreview-5702297226), [instagibbs](https://github.com/bitcoin/bitcoin/pull/36594#issuecomment-6521558421) | | Concept ACK | [mzumsande](https://github.com/bitcoin/bitcoin/pull/36594#pullrequestreview-5650744679) | 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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🔵 Suggestion · Test header omits the per-call limit on orphan reconsideration

The header at denialofservice_tests.cpp:477-478 still says at most one orphan is 'accepted or rejected' per ProcessMessages() call. The new test block also checks that orphans still missing inputs are reconsidered one per call, so the header understates what the test checks.

Location: src/test/denialofservice_tests.cpp:477 (head).


Reviewed: 83003cafaa81 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:7ba49ca38b129bc35f71e566efbd136b17ae87b582376d9eebd04f18959e8274:9b5e0237a3fe9e1d05225291551198f07780ddb92e41e55ade9e98c2687fc127 --> <!-- ralph-base:d1fb1ba36aaeb80eb896318757c98f47c20539b8 --> <!-- ralph-head:83003cafaa81bb116526419246608658ede824b6 --> <!-- ralph-case:67b79dbb0455ef505cabeebbc4645e204b7072fbc23601abe4dd058b1c4b84f4 --> **🔵 Suggestion · Test header omits the per-call limit on orphan reconsideration** The header at denialofservice_tests.cpp:477-478 still says at most one orphan is 'accepted or rejected' per ProcessMessages() call. The new test block also checks that orphans still missing inputs are reconsidered one per call, so the header understates what the test checks. Location: [`src/test/denialofservice_tests.cpp:477`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/83003cafaa81bb116526419246608658ede824b6/src/test/denialofservice_tests.cpp#L477>) (head). <!-- Review tier: sensitive --> --- Reviewed: [`83003cafaa81`](<https://git.fish.foo/bitcoin/bitcoin/commit/83003cafaa81bb116526419246608658ede824b6>) · [Full review report](<https://review.fish.foo/traces/36594-83003cafaa81bb116526419246608658ede824b6-1784-6.html>) · [Previous reports](<https://review.fish.foo/traces/36594/>) [permalink](<https://review.fish.foo/traces/36594-83003cafaa81bb116526419246608658ede824b6-1784-6.html>)
instagibbs (Migrated from github.com) reviewed 2026-10-21 21:02:28 +00:00
instagibbs (Migrated from github.com) left a comment

concept ACK

probably slap on a unit test or smth and/or fuzz to cap the amount of reconsidered stuff in a step

concept ACK probably slap on a unit test or smth and/or fuzz to cap the amount of reconsidered stuff in a step
instagibbs (Migrated from github.com) commented 2026-10-21 19:44:07 +00:00

commit msg: citing the wrong hash, should be 866c8058a7 ?

commit msg: citing the wrong hash, should be 866c8058a7 ?
darosior (Migrated from github.com) reviewed 2026-10-21 21:04:45 +00:00
darosior (Migrated from github.com) commented 2026-10-21 21:04:45 +00:00

Yeah, looks like a copy paste mistake.

Yeah, looks like a copy paste mistake.
darosior (Migrated from github.com) reviewed 2026-10-21 21:33:56 +00:00
darosior (Migrated from github.com) commented 2026-10-21 21:33:56 +00:00

Corrected commit message, thanks.

Corrected commit message, thanks.
darosior commented 2026-10-22 21:47:05 +00:00 (Migrated from github.com)

Added a unit test that demonstrates the behaviour: the test will fail on master, as both child would be reconsidered there (since validation returns a missing-input error).

Added a unit test that demonstrates the behaviour: the test will fail on master, as both child would be reconsidered there (since validation returns a missing-input error).
ViniciusCestarii (Migrated from github.com) reviewed 2026-10-23 13:09:14 +00:00
ViniciusCestarii (Migrated from github.com) left a comment

Concept ACK

Was the missing-inputs exception in 866c8058a7 there because failing in PreChecks was considered cheap? The only hint I found is "after every actual transaction validation attempt" in the 866c8058a7 commit message.

Concept ACK Was the missing-inputs exception in 866c8058a706931f025335b3e794ed2f4d287918 there because failing in PreChecks was considered cheap? The only hint I found is "after every actual transaction validation attempt" in the 866c8058a706931f025335b3e794ed2f4d287918 commit message.
instagibbs commented 2026-10-23 13:13:09 +00:00 (Migrated from github.com)

It seems the focus was likely on avoiding too much script validation, and wiping from the orphanage eagerly to avoid a single announcer dominating the orphanage. The orphanage and how announcements are handled across peers has changed quite a bit since then.

It seems the focus was likely on avoiding too much script validation, and wiping from the orphanage eagerly to avoid a single announcer dominating the orphanage. The orphanage and how announcements are handled across peers has changed quite a bit since then.
fametrano commented 2026-10-25 07:47:37 +00:00 (Migrated from github.com)

tACK 83003cafaa

I measured the trade-off the description names with a scenario of my own. One peer sends four orphans that each spend a parent output and an outpoint that does not exist, then sends the parent and an unrelated valid transaction back to back. On master the unrelated transaction is processed on the peer's second turn, because all four orphans are retried in one turn. With this PR it takes six turns: the parent, one per orphan, then the transaction. So the wait moves onto the assigned peer, as described, and each turn validates at most one orphan.

The new case in denialofservice_tests fails on master, at the ProcessMessagesOnce check in its loop, and passes here.

I tested the head merged into master a773e1e33b, in a Debug build, with the denialofservice, orphanage, txdownload and txpackage unit tests and the p2p_orphan_handling and p2p_opportunistic_1p1c functional tests. I ran them again with #36437 and #36578 merged on top; the three PRs merge without conflicts. I can share the scenario, a Boost test case, as a gist.

tACK 83003cafaa81bb116526419246608658ede824b6 I measured the trade-off the description names with a scenario of my own. One peer sends four orphans that each spend a parent output and an outpoint that does not exist, then sends the parent and an unrelated valid transaction back to back. On master the unrelated transaction is processed on the peer's second turn, because all four orphans are retried in one turn. With this PR it takes six turns: the parent, one per orphan, then the transaction. So the wait moves onto the assigned peer, as described, and each turn validates at most one orphan. The new case in denialofservice_tests fails on master, at the `ProcessMessagesOnce` check in its loop, and passes here. I tested the head merged into master a773e1e33b, in a Debug build, with the denialofservice, orphanage, txdownload and txpackage unit tests and the p2p_orphan_handling and p2p_opportunistic_1p1c functional tests. I ran them again with #36437 and #36578 merged on top; the three PRs merge without conflicts. I can share the scenario, a Boost test case, as a gist.
mzumsande (Migrated from github.com) reviewed 2026-10-26 10:39:56 +00:00
mzumsande (Migrated from github.com) left a comment

Concept ACK

This removes the need for an immediate validation result

what do you mean by that? Looking at the code, there still seems to be the need for it, to call ProcessValidTx/ProcessInvalidTx/neither.

Concept ACK > This removes the need for an immediate validation result what do you mean by that? Looking at the code, there still seems to be the need for it, to call `ProcessValidTx`/`ProcessInvalidTx`/neither.
ViniciusCestarii (Migrated from github.com) reviewed 2026-10-26 10:48:55 +00:00
ViniciusCestarii (Migrated from github.com) left a comment

ACK 83003cafaa

I like how this also simplifies the code by replacing the while loop with an if

ACK 83003cafaa81bb116526419246608658ede824b6 I like how this also simplifies the code by replacing the while loop with an if
darosior commented 2026-10-29 14:57:01 +00:00 (Migrated from github.com)

This removes the need for an immediate validation result

what do you mean by that? Looking at the code, there still seems to be the need for it, to call ProcessValidTx/ProcessInvalidTx/neither.

I've been experimenting with a net_processing / validation split, whereby the message handling and chainstate validation threads talk via message passing. This type of pattern whereby the message handling thread decides how to make progress based on an immediate result it gets from validation ("is this a missing inputs error or something else?") requires a more clumsy API: instead of asking for validation of a transaction, and acting on it once the job comes back, conserving something close to today's behaviour would require passing all reconsiderable transaction in bulk, then the chainstate validation thread to perform today's "validate until result is anything but a missing inputs error", and then return the set of not-yet-reconsidered transactions to net_processing. I think this is a further hint that today's bulk processing is a suboptimal design.

> > This removes the need for an immediate validation result > > what do you mean by that? Looking at the code, there still seems to be the need for it, to call `ProcessValidTx`/`ProcessInvalidTx`/neither. I've been experimenting with a net_processing / validation split, whereby the message handling and chainstate validation threads talk via message passing. This type of pattern whereby the message handling thread decides how to make progress based on an immediate result it gets from validation ("is this a missing inputs error or something else?") requires a more clumsy API: instead of asking for validation of a transaction, and acting on it once the job comes back, conserving something close to today's behaviour would require passing all reconsiderable transaction in bulk, then the chainstate validation thread to perform today's "validate until result is anything but a missing inputs error", and then return the set of not-yet-reconsidered transactions to net_processing. I think this is a further hint that today's bulk processing is a suboptimal design.
furszy (Migrated from github.com) reviewed 2026-10-29 18:27:30 +00:00
furszy (Migrated from github.com) left a comment
ACK 83003cafaa81bb116526419246608658ede824b6
furszy (Migrated from github.com) commented 2026-10-29 17:46:07 +00:00

Unrelated to this PR but still wrong

     * @param[in]  peer     The peer whose orphan transactions we will reconsider. At most
Unrelated to this PR but still wrong ```suggestion * @param[in] peer The peer whose orphan transactions we will reconsider. At most ```
furszy (Migrated from github.com) commented 2026-10-29 17:39:51 +00:00

pedantic nano nit: I know this doesn't worth it, and don't want you to do it here but.. my inner self would be happy to read this as

CTransactionRef porphanTx = m_txdownloadman.GetTxToReconsider(peer.m_id);
if (!porphanTx) return false;
// ... process orphan..
pedantic nano nit: I know this doesn't worth it, and don't want you to do it here but.. my inner self would be happy to read this as ```c++ CTransactionRef porphanTx = m_txdownloadman.GetTxToReconsider(peer.m_id); if (!porphanTx) return false; // ... process orphan.. ```
furszy (Migrated from github.com) commented 2026-10-29 18:22:43 +00:00

tiny nit: as an extra sanity check, could check we still process a third valid child after rejecting the invalid ones.

Click to expand the diff
diff --git a/src/test/denialofservice_tests.cpp b/src/test/denialofservice_tests.cpp
--- a/src/test/denialofservice_tests.cpp
+++ b/src/test/denialofservice_tests.cpp
@@ -538,30 +538,44 @@
     BOOST_CHECK(!connman.ProcessMessagesOnce(peer));
 
     // Create a new parent for the purpose of testing a single orphan is reconsidered per call.
-    mineBlocks(1);
-    const int child_count{2};
+    mineBlocks(1); // Mature a second coinbase output for the new parent to spend.
+    const int num_missing_input_children{2};
+    const int num_new_children{num_missing_input_children + 1}; // Plus a valid child.
     const auto new_parent{MakeTransactionRef(CreateValidMempoolTransaction(
         {m_coinbase_txns[1]}, {COutPoint{m_coinbase_txns[1]->GetHash(), 0}}, /*input_height=*/2, {coinbaseKey},
-        std::vector<CTxOut>(child_count, CTxOut{5 * COIN, script}), /*submit=*/false))};
+        std::vector<CTxOut>(num_new_children, CTxOut{5 * COIN, script}), /*submit=*/false))};
 
-    // Now create 2 children. Each spends the parent and a non-existing input.
-    for (int i{0}; i < child_count; ++i) {
+    // Now create the missing-input children. Each spends the parent and a non-existing input.
+    for (int i{0}; i < num_missing_input_children; ++i) {
         CMutableTransaction mtx;
         mtx.vin.emplace_back(COutPoint{new_parent->GetHash(), uint32_t(i)});
         mtx.vin.emplace_back(COutPoint{Txid::FromUint256(m_rng.rand256()), 0});
         mtx.vout.emplace_back(COIN, script);
         send_tx(MakeTransactionRef(mtx));
     }
-    BOOST_CHECK_EQUAL(peerman.GetOrphanTransactions().size(), child_count);
+    // Then a valid child, spending the parent's last output. Orphans are reconsidered oldest
+    // announcement first, so it is queued behind the missing-input children.
+    const auto valid_child{MakeTransactionRef(CreateValidMempoolTransaction(
+        new_parent, /*input_vout=*/num_missing_input_children, /*input_height=*/101, coinbaseKey, script, 4 * COIN, /*submit=*/false))};
+    send_tx(valid_child);
+    BOOST_CHECK_EQUAL(peerman.GetOrphanTransactions().size(), num_new_children);
 
     // Processing the parent will lead to processing a single orphan per run, even though each fails
-    // on a missing-input error (orphanage size does not decrease).
+    // on a missing-input error (orphanage size does not decrease). The valid child must wait for a
+    // call of its own.
     BOOST_CHECK(send_tx(new_parent));
     BOOST_CHECK(mempool.exists(new_parent->GetHash()));
-    for (int i{0}; i < child_count; ++i) {
+    for (int i{0}; i < num_missing_input_children; ++i) {
         BOOST_CHECK(connman.ProcessMessagesOnce(peer));
-        BOOST_CHECK_EQUAL(peerman.GetOrphanTransactions().size(), child_count);
+        BOOST_CHECK(!mempool.exists(valid_child->GetHash()));
+        BOOST_CHECK_EQUAL(peerman.GetOrphanTransactions().size(), num_new_children);
     }
+
+    // The next call accepts the valid child.
+    BOOST_CHECK(connman.ProcessMessagesOnce(peer));
+    BOOST_CHECK(mempool.exists(valid_child->GetHash()));
+    BOOST_CHECK_EQUAL(peerman.GetOrphanTransactions().size(), num_missing_input_children);
+    // No more orphans to process.
     BOOST_CHECK(!connman.ProcessMessagesOnce(peer));
 
     peerman.FinalizeNode(peer);
tiny nit: as an extra sanity check, could check we still process a third valid child after rejecting the invalid ones. <details> <summary>Click to expand the diff</summary> ```diff diff --git a/src/test/denialofservice_tests.cpp b/src/test/denialofservice_tests.cpp --- a/src/test/denialofservice_tests.cpp +++ b/src/test/denialofservice_tests.cpp @@ -538,30 +538,44 @@ BOOST_CHECK(!connman.ProcessMessagesOnce(peer)); // Create a new parent for the purpose of testing a single orphan is reconsidered per call. - mineBlocks(1); - const int child_count{2}; + mineBlocks(1); // Mature a second coinbase output for the new parent to spend. + const int num_missing_input_children{2}; + const int num_new_children{num_missing_input_children + 1}; // Plus a valid child. const auto new_parent{MakeTransactionRef(CreateValidMempoolTransaction( {m_coinbase_txns[1]}, {COutPoint{m_coinbase_txns[1]->GetHash(), 0}}, /*input_height=*/2, {coinbaseKey}, - std::vector<CTxOut>(child_count, CTxOut{5 * COIN, script}), /*submit=*/false))}; + std::vector<CTxOut>(num_new_children, CTxOut{5 * COIN, script}), /*submit=*/false))}; - // Now create 2 children. Each spends the parent and a non-existing input. - for (int i{0}; i < child_count; ++i) { + // Now create the missing-input children. Each spends the parent and a non-existing input. + for (int i{0}; i < num_missing_input_children; ++i) { CMutableTransaction mtx; mtx.vin.emplace_back(COutPoint{new_parent->GetHash(), uint32_t(i)}); mtx.vin.emplace_back(COutPoint{Txid::FromUint256(m_rng.rand256()), 0}); mtx.vout.emplace_back(COIN, script); send_tx(MakeTransactionRef(mtx)); } - BOOST_CHECK_EQUAL(peerman.GetOrphanTransactions().size(), child_count); + // Then a valid child, spending the parent's last output. Orphans are reconsidered oldest + // announcement first, so it is queued behind the missing-input children. + const auto valid_child{MakeTransactionRef(CreateValidMempoolTransaction( + new_parent, /*input_vout=*/num_missing_input_children, /*input_height=*/101, coinbaseKey, script, 4 * COIN, /*submit=*/false))}; + send_tx(valid_child); + BOOST_CHECK_EQUAL(peerman.GetOrphanTransactions().size(), num_new_children); // Processing the parent will lead to processing a single orphan per run, even though each fails - // on a missing-input error (orphanage size does not decrease). + // on a missing-input error (orphanage size does not decrease). The valid child must wait for a + // call of its own. BOOST_CHECK(send_tx(new_parent)); BOOST_CHECK(mempool.exists(new_parent->GetHash())); - for (int i{0}; i < child_count; ++i) { + for (int i{0}; i < num_missing_input_children; ++i) { BOOST_CHECK(connman.ProcessMessagesOnce(peer)); - BOOST_CHECK_EQUAL(peerman.GetOrphanTransactions().size(), child_count); + BOOST_CHECK(!mempool.exists(valid_child->GetHash())); + BOOST_CHECK_EQUAL(peerman.GetOrphanTransactions().size(), num_new_children); } + + // The next call accepts the valid child. + BOOST_CHECK(connman.ProcessMessagesOnce(peer)); + BOOST_CHECK(mempool.exists(valid_child->GetHash())); + BOOST_CHECK_EQUAL(peerman.GetOrphanTransactions().size(), num_missing_input_children); + // No more orphans to process. BOOST_CHECK(!connman.ProcessMessagesOnce(peer)); peerman.FinalizeNode(peer); ``` </details>
instagibbs commented 2026-10-30 11:29:02 +00:00 (Migrated from github.com)

ACK 83003cafaa

This opens the reconsideration gap a bit longer in certain situations but that's no worse than today: beg https://github.com/bitcoin/bitcoin/pull/36437

Going to throw some mutants and extra fuzzing invariants at it but I expect to not find anything edit: nothing found good to go

ACK 83003cafaa81bb116526419246608658ede824b6 This opens the reconsideration gap a bit longer in certain situations but that's no worse than today: beg https://github.com/bitcoin/bitcoin/pull/36437 Going to throw some mutants and extra fuzzing invariants at it but I expect to not find anything edit: nothing found good to go
fanquake (Migrated from github.com) reviewed 2026-10-30 11:48:47 +00:00
fanquake (Migrated from github.com) commented 2026-10-30 11:48:47 +00:00

Tried to fix this wholesale in #36704.

Tried to fix this wholesale in #36704.
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!36594
No description provided.