Skip to content

p2p: don't let one peer's same-txid variant cancel another peer's 1p1c or orphan resolution - #36369

Closed
instagibbs wants to merge 9 commits into
bitcoin:masterfrom
instagibbs:2026-09-1p1c-preserve-orphan
Closed

instagibbs wants to merge 9 commits into
bitcoin:masterfrom
instagibbs:2026-09-1p1c-preserve-orphan

Conversation

@instagibbs

@instagibbs instagibbs commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Opportunistic 1p1c relay and orphan resolution fetch a missing parent by txid, but several outcomes
are recorded by wtxid, or against a single package member, in ways another peer could exploit. A peer
sending a variant of a parent with the same txid and a different or missing witness could make the node
drop the honest child, cancel the honest peer's request for the parent, or reject later children of
that parent. Each case delays a CPFP package until a new announcer appears or the parent confirms.

This PR fixes four such cases deemed the easiest to accomplish by a direct node peer trying to frustrate 1P1C relay at each individual hop.

There are remaining residuals that are cataloged here in sloppy form: https://gist.github.com/instagibbs/880a76535fd802fced9f9a5253cdbb40

To close these classes of issues by construction, we would have to replace our opportunistic protocol with a sender-initiated one, but that requires a network wide protocol update. This is beyond the scope of this PR and effort, for now.

Follow-up PR enforcing invariants we want to see post-fixes: #36371

Given these are 4 essentially individual fixes, I can also split off fixes to make review easier, but I will keep this PR open in some form to show the scope of the desired changes.

Related minor PR: #36328

Investigation kicked off by a Project Loupe report

When a 1p1c package fails the package feerate check, the child gets a
TX_RECONSIDERABLE result, and ProcessPackageResult passed it to
ProcessInvalidTx, which erased the child from the orphanage for all of
its announcers.

The failure is a property of that pair of wtxids, which
MempoolRejectedPackage already caches, not of the child. The child can
still succeed with another version of the parent, such as the one an
honest announcer holds, so it should stay available.

Skip ProcessInvalidTx for package members with a TX_RECONSIDERABLE
result. A child that is too low feerate on its own is still rejected
when its parent's work set is processed.
@DrahtBot DrahtBot added the P2P label Sep 28, 2026
@DrahtBot

DrahtBot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

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
Concept ACK darosior

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:

  • #36486 (p2p: keep orphan resolution requests for non-witness 1p1c parents by FlashWayne)
  • #36318 (validation: Ensure Invalid ValidationState has result by optout21)
  • #36015 (txorphanage: bound orphan memory by storing transactions serialized by brunoerg)
  • #35713 (Remove boost as a unit test runner by rustaceanrob)
  • #35569 (Encapsulation for CTransaction by purpleKarrot)
  • #35502 (refactor: extract per-message helpers from ProcessMessage (move-only) by w0xlt)

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.

@darosior

Copy link
Copy Markdown
Member

Concept ACK

@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/actions/runs/36449058662/job/109018784014
LLM reason (✨ experimental): CI failed because ruff linting (py_lint) reported an unused import (test_framework.messages.msg_notfound) in test/functional/p2p_orphan_handling.py.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@instagibbs
instagibbs force-pushed the 2026-09-1p1c-preserve-orphan branch from 21dc168 to bf732fa Compare September 28, 2026 16:46
Check that when another announcer of the child delivers a witness-padded
parent that fails the package feerate check, the child stays in the
orphanage and the honest peer's package is still accepted, for both v2
and TRUC packages.
A TX_RECONSIDERABLE rejection forgets the transaction's wtxid in
TxRequestTracker for all peers. For a transaction without witness data
the wtxid equals the txid, which is the hash orphan resolution uses to
request a missing parent. Such a rejection therefore cancelled every
announcer's request for the parent, and the orphan stayed unresolved
until a new announcer appeared or the parent confirmed.

Keep those requests when the rejected transaction has no witness and an
orphan spends it.
Check that after another peer delivers a witnessless parent that is
rejected as reconsiderable, the child's announcer is still asked for the
parent and the package is accepted, for both a witness-stripped and a
nonsegwit parent.
Add invariants to the txdownloadman_impl target: an event about one
transaction or peer must not remove other peers' transaction requests or
orphan announcements beyond that transaction's own hashes and entries.

Uniform random operations almost never reach the states where this
matters, so bias the harness towards orphans with missing parents and
towards TX_MISSING_INPUTS and TX_RECONSIDERABLE results. Without the
bias, reverting the witnessless-parent fix survived 57k runs.
Orphan handling treated every input's parent as missing: each was
checked against the reject filters and requested from the orphan's
announcers. A confirmed parent whose txid had entered the reject filter,
which a rejected witnessless copy of it can cause, made the node drop
every orphan spending one of its outputs.

Have validation report which parents it found missing, and use only
those when storing a new orphan. Parents that are present are no longer
held against the orphan or requested.

Update the orphan handling functional test, which expected present
parents to be requested.
Check that a child spending a zero-fee parent and a confirmed output is
stored and resolved even after the confirmed transaction's txid enters
the reject filter, and that only the missing parent is requested.

Have the txdownloadman_impl fuzz target pass a random non-empty subset
of parents as missing.
TX_CONFLICT means the transaction is already known, in the mempool or
confirmed, not that it is invalid. Its wtxid still went into the reject
filter, and for a witnessless copy that is also the txid. If the known
parent then leaves the mempool before the next block, through eviction
or replacement, a child arriving afterwards is dropped as having a
rejected parent.

Don't add TX_CONFLICT results to the reject filter. The entry only saved
re-downloading and re-checking a transaction we already have.

Update the rejection-type table for TX_CONFLICT.
Check that after a stripped copy of a mempool parent is rejected as a
conflict and the parent is evicted, a child spending it is still stored
as an orphan.
@instagibbs
instagibbs force-pushed the 2026-09-1p1c-preserve-orphan branch from bf732fa to 32cf270 Compare September 28, 2026 17:13
@fanquake fanquake added this to the 33.0 milestone Sep 29, 2026
@instagibbs

instagibbs commented Sep 29, 2026 •

Copy link
Copy Markdown
Member Author

note: I was considering the "average case" cost of c2d498b since theoretically it could be a common pattern to be missing an early input and we could skip more validation, but it turns out there is essentially no meaningful load like this, after analyzing over 36k orphans I could reconstruct history of and do some qualatative benchmarks, it adds maybe 10ms of verification in a day spread over up to ~75k orphans. I think the clarity of reporting what we're actually missing is better.

The worst case is unchanged, where the entire tx is citing real inputs but they're all not the transaction maker's, so they fail script validation much later.

@instagibbs

instagibbs commented Oct 1, 2026 •

Copy link
Copy Markdown
Member Author

ah, #33066 probably needs ot be directly addressed for F3+F4

from a local skill I'm tweaking, from which I got an Approach NACK:

Click to expand additional details A. Working notes

Status: PR #36369 by instagibbs is open and not a draft. It has 5 commits of
fixes plus tests and fuzz coverage, 728+/55−, touching validation.cpp,
net_processing.cpp, txdownloadman and txorphanage. The thread has one Concept
ACK (darosior) and no pushback. The only author follow-up is a perf note on
the validation change. The description matches the diff.

  1. Problem. A peer sends a same-txid, different-witness copy of a CPFP parent.
    The honest package is then delayed at that hop "until a new announcer appears
    or the parent confirms." Anyone using 1p1c can hit this, for example LN
    anchor or TRUC closes. The evidence is a Project Loupe report plus functional
    tests and fuzz reproductions. There are no incidents in the wild. Goal one
    level up: fee-bumped, time-sensitive transactions should reach miners even
    when cheap peer-level griefing is attempted.

  2. Baseline. An attacker who is directly connected to a node can stall a
    chosen package at that node. Stalling it network-wide needs connections to
    many nodes on the path to miners. Nothing has been observed in practice, but
    the attack is cheap and targeted.

  3. Payoff. The PR closes four reproduced vectors (F1–F4 in the gist), and each
    one has a test showing the honest package now gets through. The author's own
    gist lists 11 residual issues of the same class (R0–R10), so the PR gives a
    partial liveness improvement, not a guarantee. fuzz: add end-to-end liveness target for opportunistic 1p1c relay #36371 (the liveness fuzz
    target) is follow-up work and doesn't count toward this PR's payoff.

  4. Risks.

  • Invariant enforced by discipline. The property the PR wants is "one peer's
    variant of a tx must not affect other peers' requests or orphan state." The
    code enforces it with special cases inside the shared handler,
    MempoolRejectedTx:

    • !(RECONSIDERABLE && !HasWitness() && HaveChildren()) around ForgetTxHash;
    • a TX_CONFLICT exclusion from the reject filter;
    • a TX_RECONSIDERABLE skip in ProcessPackageResult;
    • a new exception to the m_tx_download_mutex lock invariant.

    R1 in the gist shows a further hole in the same shared ForgetTxHash path, so
    the failure rate of this approach is already visible.

  • Coupling into validation. F3 changes MemPoolAccept::PreChecks: it now scans
    every input, and it adds m_missing_parents to MempoolAcceptResult. That
    pushes an orphan-handling concern into validation, which is the costliest
    place to put it.

  • Conflicts. DrahtBot lists 5 conflicting PRs, including txorphanage: bound orphan memory by storing transactions serialized #36015 (orphanage)
    and validation: Ensure Invalid ValidationState has result #36318 (validation).

  • Reversibility. All changes are internal or policy, so they are easy to
    reverse.

  1. Prior attempts.
  1. Alternatives.

Kept:

  • Never check the reject filter by txid for orphan parents (prior: p2p: never check tx rejections by txid #33066).
    This removes the R7 class (F3 and F4) by construction, since no
    witness-dependent outcome can be read through the txid at all. It needs no
    PreChecks or MempoolAcceptResult change and no TX_CONFLICT carve-out. Its
    cost: a little bandwidth when re-fetching truly bad parents, and glozow
    measured zero txid hits over 2 weeks. Neither the PR nor its thread raises
    it.
  • Type-aware TxRequestTracker forgetting (blind; also the author's own
    proposed fix for R1 in the gist). A wtxid-keyed failure would forget only
    wtxid announcements and leave txid orphan-resolution requests alone. That
    covers F2 and R1 with one rule instead of the HasWitness/HaveChildren
    special case. Its cost: txrequest has to track announcement type, and
    accept/block paths still forget by hash. Not raised in the thread.

Rejected:

  • Document only (blind): fixes nothing; equivalent to the baseline.
  • Detect and punish same-txid variants (blind): legitimate witness variants
    exist, and punishing the sender doesn't recover the honest package. (My
    argument.)
  • Retry the parent from other announcers (blind): this is what F2 already
    does, so it isn't a different design.
  • L2 multi-node submitpackage or a contrib watcher script (blind): only helps
    the submitter's own node, not later hops. (My argument.)
  • BIP331 sender-initiated relay (prior/PR): needs a network-wide upgrade.
    (The author's argument, and it holds.)

F1 (keep the orphan after a package-feerate failure) has no competing design
and looks correct as it is.
4. Prior attempts.

  • p2p: never check tx rejections by txid #33066 "p2p: never check tx rejections by txid" (glozow, 2025). It removed
    reject-filter lookups by parent txid, which is the read at
    txdownloadman_impl.cpp:379 and the txid path in AlreadyHaveTx. It got
    Concept ACKs from sipa and darosior. The author closed it "for now" after
    validation: detect witness stripping without re-running Script checks #33105 solved the triple-validation cost. Cause of death: deprioritized,
    not rejected. ajtowns's objection was narrow: keep the cache for
    INPUTS_NOT_STANDARD. Addressed here: not mentioned at all.

  • p2p: stop special-casing witness-stripped error for unconfirmed transactions #32379 (darosior, closed draft). It proposed dropping the WITNESS_STRIPPED
    special case. ajtowns argued it is still needed while orphan resolution
    removed reject-filter lookups by parent txid, which is the read at
    txdownloadman_impl.cpp:379 and the txid path in AlreadyHaveTx. It got
    Concept ACKs from sipa and darosior. The author closed it "for now" after

    • a TX_CONFLICT exclusion from the reject filter;
    • a TX_RECONSIDERABLE skip in ProcessPackageResult;
    • a new exception to the m_tx_download_mutex lock invariant.

    R1 in the gist shows a further hole in the same shared ForgetTxHash path, so the failure rate of this
    approach is already visible.

  • Coupling into validation. F3 changes MemPoolAccept::PreChecks: it now scans every input, and it adds
    m_missing_parents to MempoolAcceptResult. That pushes an orphan-handling concern into validation, which is
    the costliest place to put it.

  • Conflicts. DrahtBot lists 5 conflicting PRs, including txorphanage: bound orphan memory by storing transactions serialized #36015 (orphanage) and validation: Ensure Invalid ValidationState has result #36318 (validation).

  • Reversibility. All changes are internal or policy, so they are easy to reverse.

  1. Prior attempts.
  1. Alternatives.

Kept:

  • Never check the reject filter by txid for orphan parents (prior: p2p: never check tx rejections by txid #33066). This removes the R7 class (F3 and
    F4) by construction, since no witness-dependent outcome can be read through the txid at all. It needs no
    PreChecks or MempoolAcceptResult change and no TX_CONFLICT carve-out. Its cost: a little bandwidth when
    re-fetching truly bad parents, and glozow measured zero txid hits over 2 weeks. Neither the PR nor its
    thread raises it.
  • Type-aware TxRequestTracker forgetting (blind; also the author's own proposed fix for R1 in the gist). A
    wtxid-keyed failure would forget only wtxid announcements and leave txid orphan-resolution requests alone.
    That covers F2 and R1 with one rule instead of the HasWitness/HaveChildren special case. Its cost: txrequest
    has to track announcement type, and accept/block paths still forget by hash. Not raised in the thread.

Rejected:

  • Document only (blind): fixes nothing; equivalent to the baseline.
  • Detect and punish same-txid variants (blind): legitimate witness variants exist, and punishing the sender
    doesn't recover the honest package. (My argument.)
  • Retry the parent from other announcers (blind): this is what F2 already does, so it isn't a different
    design.
  • L2 multi-node submitpackage or a contrib watcher script (blind): only helps the submitter's own node, not
    later hops. (My argument.)
  • BIP331 sender-initiated relay (prior/PR): needs a network-wide upgrade. (The author's argument, and it
    holds.)

F1 (keep the orphan after a package-feerate failure) has no competing design and looks correct as it is.

Dominance: #33066's approach dominates F3+F4. It has the same payoff, removes the discipline risk by
construction, and avoids the validation.cpp change. Type-aware forgetting likely dominates F2 and also closes
R1.

  1. Verdict. Concept ACK, Approach NACK. An alternative dominates per (5), and (0) and (2) are earned.

@fametrano fametrano left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked that the new tests catch what they fix, at 32cf270. I reverted each of the four fixes on its own and ran txdownload_tests, p2p_opportunistic_1p1c.py and p2p_orphan_handling.py. Each revert makes at least one new test fail. F4 is caught only by the unit tests; no functional test covers it. I did not review the C++ changes.

One nit inline, in test_orphan_multiple_parents.

Happy to redo this check once F3 and F4 are reworked around #33066.

Comment on lines +294 to +295
assert_equal(len(peer.last_message["getdata"].inv), 1)
peer.wait_for_parent_requests([int(missing_tx["txid"], 16)])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: assert_equal(len(peer.last_message["getdata"].inv), 1) can pass on the earlier getdata for the orphan, which also has one item. wait_for_parent_requests already checks the count. assert_never_requested checks directly that the long-confirmed parent is never requested. With this change the test passes, and with F3 reverted it fails at wait_for_parent_requests.

Suggested change
assert_equal(len(peer.last_message["getdata"].inv), 1)
peer.wait_for_parent_requests([int(missing_tx["txid"], 16)])
peer.wait_for_parent_requests([int(missing_tx["txid"], 16)])
peer.assert_never_requested(int(utxo_conf_old["txid"], 16))

@instagibbs
instagibbs marked this pull request as draft October 5, 2026 13:05
@instagibbs

Copy link
Copy Markdown
Member Author

putting this in draft since I want higher assurances that the gaps we care about are found and filled properly: #36437

Other PR has a more straight forward fix + invariant fuzzer

@instagibbs

Copy link
Copy Markdown
Member Author

replaced by #36506

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.

5 participants