Skip to content

net_processing: Ignore MSG_WITNESS_TX entries from INV messages - #35920

Open
ajtowns wants to merge 3 commits into
bitcoin:masterfrom
ajtowns:202608-inv-msg-witness
Open

ajtowns wants to merge 3 commits into
bitcoin:masterfrom
ajtowns:202608-inv-msg-witness

Conversation

@ajtowns

@ajtowns ajtowns commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

When introducing wtxidrelay we added checks to skip attempts to announce txs with the wrong hash (#18044 (comment)), but didn't similarly ignore MSG_WITNESS_TX entries in announcements, which have never been valid (see BIP-144).

This PR corrects that oversight, skipping such announcements. It also moves the check to be after the disconnect code, so that attempting to announce txs on fRelay=false doesn't get treated as okay just because you're doing it wrong. As a result this allows deduping the seen_tx_hashes set.

@DrahtBot

DrahtBot commented Aug 6, 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
ACK fjahr, fametrano
Concept ACK l0rinc, mzumsande

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:

  • #36257 (qa: assert_equal -> assert_true/assert_false by hodlinator)
  • #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.

@ajtowns

ajtowns commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

See also #35917 and #35878 (comment)

@l0rinc

l0rinc commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Concept ACK, thanks for pushing a fix for #35878 (comment).

@ajtowns ajtowns changed the title net_processing: Ignore MSG_TX_WITNESS entries from INV messages net_processing: Ignore MSG_WITNESS_TX entries from INV messages Aug 6, 2026
@DrahtBot DrahtBot closed this Aug 7, 2026
@DrahtBot DrahtBot reopened this Aug 7, 2026
Comment thread src/net_processing.cpp Outdated
Comment on lines +4380 to +4383
if (peer.m_wtxid_relay) {
if (!inv.IsMsgWtx()) continue;
} else {
if (!inv.IsMsgTx()) continue;

@darosior darosior Aug 7, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ignoring MSG_WITNESS_TX for wtxid relay peers seems fine, because i expect wtxid relay codebases to be newer / better maintained, and because BIP 339 explicitly states:

After a node has received a wtxidrelay message from a peer, the node MUST use the MSG_WTX inv type when announcing transactions to that peer.

However for txid relay peers, i'm afraid we are in a similar situation to unannounced transaction processing. We have supported it for a decade, and while it Is Incorrect, i'm afraid there may be an unmaintained codebase out there that relies on it. It's a bit more niche than unannounced transaction processing, but still, i think we should have some evidence this isn't being used in the wild before dropping support.

@ajtowns ajtowns Aug 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Based on the got inv debug.log lines, my lots-of-peers node received no witness-tx invs in April-July, versus:

Month block tx wtx
April 3,210 6,935,321 1,174,811,061
May 2,092 3,530,619 854,676,397
June 1,783 13,683,220 1,793,970,323
July 1,924 10,870,284 2,474,162,561

Numbers from:

zstdcat debug.log-202604*.zst | grep got.inv: | cut -d\  -f5 |
  awk '{c[$0]++} END {for (l in c) printf "%8d %s\n", c[l], l}'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For people who do find instances of this, grep got.inv:.witness-tx .bitcoin/debug.log should indicate the txid being sent, as well as whether your peer already had the tx in question ("have" vs "new").

@fjahr

fjahr commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

@0xB10C

0xB10C commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

ajtowns and others added 3 commits August 13, 2026 08:05
BIP 144 specifies that MSG_WITNESS_TX is "only for use in getdata",
so if we receive it anyway, ignore it, rather than treating it as a
by-txid announcement.

Also changes the code ordering so that attempting to announce txs on a
connection where tx relay is disabled triggers a disconnect, even if we
would have ignored the announcement for being the wrong inv type.
Previously we only checked disconnection occurs on "correct" announcements
(WTX on a wtxidrelay connection); extend that to checking all possible
announcements (still only on a wtxidrelay connection).
After the previous commit only one of these will ever be used
for any particular INV message, so there's no need for two.

Co-Authored-By: Antoine Poinsot <[email protected]>
@ajtowns
ajtowns force-pushed the 202608-inv-msg-witness branch from eaaea13 to 0f9bffe Compare August 12, 2026 22:05
@ajtowns

ajtowns commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Changed to allow witness-tx in invs for non-wtxidrelay peers. Lame.

Concept ACK, thanks for pushing a fix for #35878 (comment). If you touch again, please add me as coauthor.

I didn't copy any code from you, and I'm not even sure what this is in regards to. Nothing here is substantive enough to be worth arguing about authorship.

@fjahr fjahr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code review ACK 0f9bffe

Left a bunch of suggestions to improve comments and tests but this is good to merge as is.

Comment thread src/net_processing.cpp
pfrom.fDisconnect = true;
return;
}
// MSG_WITNESS_TX is treated as a txid, despite only being specified for getdata.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The deleted comment isn't wrong, the code that it referred to still exists below. The difference is that it now only runs for non-wtxidrelay peers. I would keep it around below the new code block or maybe include the information somewhere the new comments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Everything except MSG_WTX is always a txid, see ToGenTxid() or how MSG_WITNESS_TX is defined in bip 144 ("MSG_WITNESS_TX getdata requests should use the non-witness serialized hash."); I don't think this needs an extra comment

assert_equal(log.count(f"got inv: {second_name} {hash_a:064x}"), 0)

self.log.info('Check that txids and wtxids are deduplicated separately')
self.log.info('Check that MSG_WITNESS_TX inv is ignored with wtxid relay')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(note to other reviewers: the actual change to the separate deduplication happens in the third commit)

Based on the redefiniton of the test now only the first of the loop runs seems still relevant. I think I would find it confusing why the test is written this way without the historic background.

         self.log.info('Check that MSG_WITNESS_TX inv is ignored with wtxid relay')
         peer = node.add_p2p_connection(TestP2PConn(wtxidrelay=True))
-        for first_type, second_type, hash_a in [
-            (MSG_WITNESS_TX, MSG_WTX, 0x8899aa),
-            (MSG_WTX, MSG_WITNESS_TX, 0x99aabb),
-        ]:
-            log = send_invs_and_read_log(peer, [
-                CInv(t=first_type, h=hash_a),
-                CInv(t=second_type, h=hash_a),
-            ])
-            assert_equal(log.count(f"got inv: witness-tx {hash_a:064x}"), 0)
-            assert_equal(log.count(f"got inv: wtx {hash_a:064x}"), 1)
+        hash = 0x8899aa
+        log = send_invs_and_read_log(peer, [
+            CInv(t=MSG_WITNESS_TX, h=hash),
+            CInv(t=MSG_WTX, h=hash),
+        ])
+        assert_equal(log.count(f"got inv: witness-tx {hash:064x}"), 0)
+        assert_equal(log.count(f"got inv: wtx {hash:064x}"), 1)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The historic background is readily available via git log: git log -M -C -L 381,392:../test/functional/p2p_tx_download.py I prefer to keep the changes to tests minimal when changing functionality, to avoid accidentally reducing coverage in a way that hides introduced bugs, so I'd rather keep this as-is.

@fjahr fjahr Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think it would occur to me to go back in git history because there is no hint here that there is some historic context that might be helpful. And if I did, looking for the historic context of the test parts that are confusing would let me find the reason because these are the lines that are not being changed here. But not a blocker either.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I just mean if you find a piece of code confusing, looking at the git history should always be one of the first things you consider doing. In this case you'd see the test went from 1 to 0 for witness-tx and the description went from "Check that txids and wtxids are deduplicated separately" to "Check that MSG_WITNESS_TX inv is ignored with wtxid relay" as part of the "net_processing: Ignore MSG_WITNESS_TX entries in inv messages" commit, which you could then trace through to the PR in question if it was still confusing.

self.nodes[0].disconnect_p2ps()
h = 0x12345
for msg_type in [MSG_WTX, MSG_TX, MSG_WITNESS_TX]:
h += 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Since each loop run has its own connection this doesn't seem necessary

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's not necessary but if one (or more) of the invs somehow gets a GETDATA response due to a bug, it makes it clearer which one was the trigger.

peer.wait_for_disconnect()
self.nodes[0].disconnect_p2ps()
h = 0x12345
for msg_type in [MSG_WTX, MSG_TX, MSG_WITNESS_TX]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Could apply the same loop to the self.log.info('Check that tx invs also violate the protocol') test above in blocksonly_mode_tests.

Comment thread src/net_processing.cpp
}
auto& seen_hashes{inv.IsMsgWtx() ? seen_wtxids : seen_txids};
if (!seen_hashes.insert(inv.hash).second) continue;
// Due to the check above, we're only adding hashes of one particular type here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: This would be a bit easier to understand for me

Suggested change
// Due to the check above, we're only adding hashes of one particular type here
// Mixing txids and wtxids here is ok because the check above only lets through
// wtxids (MSG_WTX) from wtxidrelay peers and txids (MSG_TX, MSG_WITNESS_TX) otherwise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We're not mixing txids and wtxids though. Each peer gets precisely one or the other added to seen_tx_hashes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To me this is still mixing different types in a single data structure, independent of which peer these are coming from. But it's not a blocker for me.

@DrahtBot
DrahtBot requested a review from l0rinc September 26, 2026 15:48
@fametrano

Copy link
Copy Markdown

tACK 0f9bffe

Built and ran p2p_blocksonly.py and p2p_tx_download.py. With net_processing.cpp reverted to the base, both fail. Each of five small breakages I tried, such as moving the wtxidrelay filter back above the disconnect or dropping the dedup, fails at least one of them.

Nit: the title and description still say MSG_WITNESS_TX invs are ignored; since the neutrino change, that only holds for wtxidrelay peers.

@Amrrty

This comment was marked as spam.

@instagibbs

Copy link
Copy Markdown
Member

Filed as lightninglabs/neutrino#384

just noting it was merged a month ago

@instagibbs instagibbs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

0f9bffe

LGTM

I'd happily review a PR that would make MSG_WITNESS_TX and MGS_WTX not sound like the same thing.

Could we get a test that ensures we GETDATA the neutrino exception, if we don't have one?

peer.wait_for_disconnect()
self.nodes[0].disconnect_p2ps()
h = 0x12345
for msg_type in [MSG_WTX, MSG_TX, MSG_WITNESS_TX]:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
for msg_type in [MSG_WTX, MSG_TX, MSG_WITNESS_TX]:
for wtxidrelay, msg_type in product([True, False], [MSG_WTX, MSG_TX,
MSG_WITNESS_TX]):
h += 1
peer =
self.nodes[0].add_outbound_p2p_connection(P2PInterface(wtxidrelay=wtxidrelay),
p2p_idx=0, connection_type="block-relay-only")

@mzumsande mzumsande left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK

Filed as lightninglabs/neutrino#384

just noting it was merged a month ago

What is the plan to deal with this? Merge as is and revisit in a few releases to then ignore MSG_WITNESS_TX also for for non-wtxidrelay peers?

sedited added a commit that referenced this pull request Nov 5, 2026
22535b4 test: look for the wtxid in p2p_blocksonly (Ferdinando Ametrano)

Pull request description:

  In `blocks_relay_conn_tests`, the check that a transaction submitted over RPC is not announced to a block-relay-only peer looks for its txid among the peer's invs. The peer is a `P2PTxInvStore`, which negotiates wtxidrelay, so an announcement would carry the wtxid. The MiniWallet transaction has a witness, so its txid and wtxid differ, and the check passes whether or not the node announces it.

  This checks the wtxid instead. To see the difference, open that connection as `outbound-full-relay`: the old check still passes, the new one fails. It also adds an `outbound-full-relay` peer that must receive the wtxid announcement first, so the check cannot pass silently again.

  #35920 also edits `blocks_relay_conn_tests`; the two do not conflict.

  Found while porting these tests to https://github.com/btclib-org/bitcoin-node-tests.

ACKs for top commit:
  maflcko:
    lgtm ACK 22535b4
  AndreaDiazCorreia:
    ACK 22535b4
  sedited:
    ACK 22535b4

Tree-SHA512: ac425fafbd8f2851dc9d11799d70ffd914ea55bdf9e63087ac8313e55748ca2635c0ae266fae0d51400d1b10b05f3ee622f9fecce6d1360ae5803b8bcadc1638
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants