Skip to content

net: keep finished private broadcast txs in memory - #34707

Closed
andrewtoth wants to merge 4 commits into
bitcoin:masterfrom
andrewtoth:private_broadcast_store
Closed

andrewtoth wants to merge 4 commits into
bitcoin:masterfrom
andrewtoth:private_broadcast_store

Conversation

@andrewtoth

@andrewtoth andrewtoth commented Mar 2, 2026 •

Copy link
Copy Markdown
Contributor

Follow up from #34329.

Private broadcast transaction data is removed from memory when either 1) the tx is received back from another peer and inserted into our mempool or 2) the tx is no longer acceptable to our mempool.

We want to persist this data to be able to verify that transactions have been received back, and to use that information for statistical analysis. Also see #30471.

This PR changes the behavior of the private broadcast queue to keep received txs in memory. The peer's address we received the tx from and the time is now also returned via getprivatebroadcastinfo RPC.

Mempool rejected txs are still removed from the queue as before.

@DrahtBot DrahtBot added the P2P label Mar 2, 2026
@DrahtBot

DrahtBot commented Mar 2, 2026 •

Copy link
Copy Markdown
Contributor

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

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34707.

Reviews

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

Type Reviewers
ACK vasild, optout21
Concept NACK instagibbs
Stale ACK danielabrozzoni

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:

  • #36429 (Delayed transaction broadcast, minimal by optout21)
  • #36277 (net: always complete all initial private broadcast connections by andrewtoth)
  • #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.

@andrewtoth
andrewtoth force-pushed the private_broadcast_store branch from a88810a to afca844 Compare March 2, 2026 02:03
@DrahtBot

DrahtBot commented Mar 2, 2026

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task macOS native: https://github.com/bitcoin/bitcoin/actions/runs/22558096885/job/65339168667
LLM reason (✨ experimental): private_broadcast_tests failed with an assertion error in netaddress.cpp (ToStringAddr), causing the test subprocess to abort.

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.

Comment thread src/net_processing.cpp Resolved

@optout21 optout21 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.

ACK 1df06de

Re-checked following previous rounds of changes, re-ack'd.

Previous:
ACK d5d7c28e1b4b76daddad95a23cbf56209ca0b86e
ConceptACK bd31f7391dd45b2582092762466e40ad2594b178
Another round of review, no concerns (but keeping the status for now).
Prev:
Pre-review, left some minor comments.
A general conceptual point is raised in separate comment (main-thread).

Comment thread src/private_broadcast.cpp Resolved
Comment thread src/private_broadcast.cpp Resolved
Comment thread src/private_broadcast.h Resolved
@optout21

optout21 commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

A more important conceptual point I'd like to raise:
this work is the precursor of features that are related to locally-originated transactions, and not private broadcast. I think that it is important to clarify this and use the right nomenclature at this early stage.

As I see:

  • there where wishes/proposals related to local-originated transaction handling, private broadcast being one of them
  • private broadcast for locally-originating txs was implemented (🔥), and it introduced a short-lived queue needed for private broadcast
  • other features are starting to be implemented on top of the private broadcast queue, but logically they are not strictly related to the private nature of the broadcast, but the locally-generated property of it.
  • a good test is to answer the question: would this feature make sense even if the broadcast was not private, but 'regular' broadcast? If the answer is yes or likely yes, than the feaure is not related to the private broadcast, and its naming should not be based on it.
  • it should be even possible to decouple private broadcast from other local-originating features (though it makes sense to use private bc always).
  • With the current direction of things I even propose that the new RPC's names (getprivatebroadcastinfo) should not be based on the 'private' feature, but 'local tx' . Also, if the mentioned features are being considered, the name of the queue could be reconsidered at this point.

In short, for the newly considered features I consider the "locally-originated TX" to be more relevant than the "private" nature of the broadcast, and suggest using it in namings.

(fyi @vasild)

@vasild vasild 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.

Reviewed up to and including fa857a980c net: track received peer and mempool status in private broadcast.

@optout21, good points. Consider also #34533 and #3828. In the light of that, a sensitive transaction may not have originated locally. So "local" may not be the best term to use for these.

Comment thread src/rpc/mempool.cpp Resolved
Comment thread src/private_broadcast.cpp Resolved
Comment thread src/net_processing.cpp Resolved
Comment thread src/net_processing.cpp Resolved
Comment thread src/private_broadcast.cpp Resolved
Comment thread src/private_broadcast.cpp
return !m_transactions.empty();
return std::ranges::any_of(m_transactions, [](const auto& el) {
return !el.second.mempool_rejection_reason.has_value();
});
}

std::vector<CTransactionRef> PrivateBroadcast::GetStale() const

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.

GetStale() needs to be changed to omit the concluded (finished) transactions.

@andrewtoth andrewtoth Mar 10, 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.

I was thinking more about this and I don't see a way to have "finished" transactions.

If a tx is acceptable to our mempool, it is not yet finished because it has not been received.
If a tx exists in our mempool, it is not yet finished because it has not been mined and may be evicted.
If a tx is not acceptable to our mempool, it or a conflicting tx may have been mined. However, it may also be the case that its parent tx has been evicted from our mempool as well and not yet mined. Either way, checking the confirmed utxo set will also show this tx as unacceptable, when it will be acceptable if the parent enters the mempool again. This tx is not yet finished since it may be acceptable to our mempool again if the parent returns.

So, is there a heuristic we can use to determine if a tx is concluded (finished)? I don't see one. We need to keep txs in the queue indefinitely.
One way is to have an expiry on txs which you suggested in #34322, but I believe that is out of scope for this PR.

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.

A transaction is certainly finished if it is mined or if a conflicting transaction is mined, no?

Currently (on master without this PR) we consider a transaction finished once we receive it back from the network without even checking whether it will make it to our mempool. Then we call Remove() and forget about the transaction.

This is deliberate to avoid infinite broadcasting of unacceptable transactions. That leaves retries of transactions that made it back but after a while need to be broadcast again at the hands of the users. I think that is worth reviewing and better be done in a focused PR that does just that. That would slim the current PR to just keeping the finished transactions in memory, without changing the meaning of "finished".

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.

A transaction is certainly finished if it is mined or if a conflicting transaction is mined, no?

How to determine this is tricky. There are a lot of ways to potentially solve this problem with different trade-offs. Let's leave this to a follow-up.

Comment thread src/private_broadcast.cpp Resolved
@andrewtoth
andrewtoth force-pushed the private_broadcast_store branch 2 times, most recently from 2c3c80d to 9f58d9a Compare March 11, 2026 01:40
@andrewtoth

Copy link
Copy Markdown
Contributor Author

Thank you for your reviews @optout21 and @vasild.

  • Add no longer clears received state, so it is a simpler change. However, it does not rebroadcast txs if they are already received and sent again with sendrawtransaction. Removed unit test and functional test that covered this.
  • Only clear received state if transitioning from mempool rejected -> mempool acceptable. This happens if the tx is evicted but is not conflicted. In this case it is no longer "received" and we want to start the broadcast flow again.
  • Check if a tx already exists in mempool in ReattemptPrivateBroadcast before checking mempool acceptance.

Also addressed several other refactor and style changes, and split up and reordered commits for easier review.

git range-diff 9cad97f6cdf1bd660732cd10e844a6a7e0771ea0..afca84489c26f920d842f34d00f8418349255a65 eed316189376d72fc959e8d2e45cafef279f77da..9f58d9a1691b10341784e13ae32549bb0244bbda

I consider the "locally-originated TX" to be more relevant than the "private" nature of the broadcast, and suggest using it in namings.

@optout21 that's an interesting point. I do think it is out of scope for this PR though. Could be raised independently.

@andrewtoth
andrewtoth force-pushed the private_broadcast_store branch from 9f58d9a to c9b19d7 Compare March 11, 2026 13:39
@andrewtoth

Copy link
Copy Markdown
Contributor Author

Updated test to fix a bad rebase. Also, addressed feedback to make rebroadcast logic more clear #34707 (comment).

git diff 9f58d9a1691b10341784e13ae32549bb0244bbda..c9b19d76dd336b2342d6d766b408410e1b93107b

@andrewtoth
andrewtoth force-pushed the private_broadcast_store branch from c9b19d7 to 6ecbf18 Compare March 11, 2026 15:12
@andrewtoth
andrewtoth force-pushed the private_broadcast_store branch 2 times, most recently from 911d4eb to bd31f73 Compare March 12, 2026 00:12
Comment thread src/private_broadcast.h Resolved
@andrewtoth

Copy link
Copy Markdown
Contributor Author

Changed the approach to use an enum for the mempool states we care about:
IN_MEMPOOL,
ACCEPTABLE,
REJECTED.

This should make it easier to reason about what is happening in this PR.
I also split up the commits so they are easier to review.

@andrewtoth
andrewtoth force-pushed the private_broadcast_store branch from bd31f73 to 420abcb Compare March 20, 2026 19:29
@andrewtoth andrewtoth changed the title net: keep finished private broadcast txs in memory, rebroadcast if evicted from mempool net: keep finished private broadcast txs in memory Mar 20, 2026

@vasild vasild 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.

ACK a0508a3

Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

ACK a0508a315935eec89e92b29d8cb99ba073e22f61
-----BEGIN PGP SIGNATURE-----

iQRPBAEBCAA5FiEE5k2NRWFNsHVF2czBVN8G9ktVy78FAmqYQFUbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMiwzAAoJEFTfBvZLVcu/ghIf/ib9EdCLB/4pfiM0hPn/
eCR/HvGTEir1QNxUE0LBReE0xQwyUaTf5Lvuj3qrBgexoEwGcHTUsY0lJxoz85YH
3RSNPP/58W33GyWfP9x08BXxkX2polxPCX+mMg0Upn2UI4B7D7+cLRmxVMujJU3o
S58aMef7zQvHgHwo3xT6LGsRjb08fVTo9QyYheNDSzwAYd7ETbpVhA9vlukJKLPB
2Hk+v2kIIseBOnWJiJXgfsDyEJnqKXm8bsfSEsNLib5Pv2kw7W8NQTFb+HdJQDvK
6QMi5EbQ8Zl2FRn5ykY2pm2EzOaQLBoDIZ2tiB8esZ73VBgh0sutGxzE64BVGDXk
g6Wzws72f4QF1DNYk+jRg5N0ASw4dMJgW9PDFctujNbnObnZXuP8mwyUNH7Dt3+S
eNsLOeZu80xYgdUHzqk156AlobanCyf+Is1JpURycZugA7OpIOsDlTK7KERkRNPM
CyI9jxGs8NofcbDdoHhct/dxa/OSXU5+5AfLwEhhH7hdxZp+FTJsrhvyXU9JshoS
Cj4FrPUsRih5ggsIeVjjxX+TBBVi7JhgSzIST5GU8j7qmMMPUhpDPk0ccEDFQ2yl
cXT71hZU1EsPa0KfubztqIAIZlc8FrReGOGm4V10Yw2r1YWt0v9g5ju99BV/lvOD
xXqhlQ9dHl25XR9BRr9Erfsn7j2km10G/K9/bYui5UzvIG00OgipPGxwaECjwAy3
vtwNATwTK2HHxHhzW23qC6zzwLQgrFAW5nmjx5jBO1RXLzqP+A6k5KaDcyh9fzf+
PjCvQT1g2p8XHzl5kHQnSj0Oh4eypXqkfvBEm832SOZFw2jP0x1artX7II0q9vEd
RJyUzEZOFrYW/yItVy4tQZXpKIbt2E890F4jpWPTlb79l9GYbQLtpl8540cD4+1/
n26wfF9r550zv5VVwQ5FcTL2ATdjtErQB/UF0jKOrpTAJbJhVZ9ETm7YhKXWJNE2
7gxosp2cZJlS3aeIAl3FTX/GF291CztT3vaSHpi0OrUTGOju04fa7LxKY+vl9Rn5
oJOCUAvvMpd3L0sWwUiM3V6KlVhflvDFxhCv0tQJEfoKPTn8Aq3akWxv70NjrrH2
7vKJXlk4KT7n0gt2nzIe2jBCKaoN9Nq4Un/BI59wXiq5G6aRGe+2+sOl70Zr2d6l
Cs7w7JRJcvgMxEZACh3XM26cHpXbrxEClLQzb/Q8iQUiliedyE84p92djZyqDxoO
i7YXtAeMMJD1JC6XPhwbUXPthxC6DiK9Cy8zJfSgBQc4L+4h1mnBhz9Q5RjYA18d
I8UfCdimN9eUNfzdVtxraeWOdEoxVhKpxbXq41VqB8nloJvjO0jXctcdeXgyHx7D
/8o=
=YbDU
-----END PGP SIGNATURE-----

vasild's public key is on openpgp.org

Comment thread src/test/fuzz/private_broadcast.cpp Resolved
Comment thread src/test/fuzz/private_broadcast.cpp Resolved
Comment thread src/test/private_broadcast_tests.cpp Resolved
Comment thread src/test/private_broadcast_tests.cpp Resolved
Comment thread doc/release-notes-34707.md Resolved

@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.

One correctness issue identified, a double-decrement against already-confirmed tx: instagibbs@ea4bba7

concept NACK for now

I'm not persuaded this change is net positive. An average user simply will not need this feature, so it's being aimed at, certain devs I guess, with the tradeoff being things not promptly leaving during the happy path.

Why not just set -debug=privbroadcast and inspect mempool directly? Can we just do better logging if we're missing something?

I'd like to see where currently tooling in master falls flat and see if there's a better way to satisfy the requirements.

Comment thread src/rpc/mempool.cpp Resolved
@andrewtoth

Copy link
Copy Markdown
Contributor Author

An average user simply will not need this feature, so it's being aimed at, certain devs I guess, with the tradeoff being things not promptly leaving during the happy path.

One thing an average user will want is rebroadcasting txs in the private broadcast queue until they confirm. See #30471. Users want a way to have a tx continually rebroadcast, and right now there's no way to do that without using the wallet. If we keep these txs around after they are received back, we can periodically check if they have not yet confirmed or no longer in mempool (they were evicted due to size limits or expired) and rebroadcast them. This would be a first step to that, next would be #34322.

@instagibbs

Copy link
Copy Markdown
Member

sers want a way to have a tx continually rebroadcast, and right now there's no way to do that without using the wallet

getrawtransaction?

check if they have not yet confirmed or no longer in mempool (they were evicted due to size limits or expired) and rebroadcast them. This would be a first step to that, next would be #34322.

Ok, directionally I disagree with this. I do not think private broadcast should become some omni broadcast logic, and rather we push that complexity to the edges.

@andrewtoth

Copy link
Copy Markdown
Contributor Author

getrawtransaction is an RPC your wallet can call when you open it. But, our node can be always on and wallets can rely on it to continually rebroadcast until it is confirmed. This is a pain point for many wallets and services. See the list in the OP of #30471.

Introduce MarkReceived method in PrivateBroadcast to record which peer
relayed the transaction back to us, and at what time. The transaction stays in the
queue instead of being removed, preserving broadcast metadata.
However, the transaction will not be selected for broadcasting again.

The received metadata is cleared when a transaction is added to the queue again,
allowing the transaction to be broadcast again.
@andrewtoth
andrewtoth force-pushed the private_broadcast_store branch from a0508a3 to 9217011 Compare September 3, 2026 01:20
@instagibbs

Copy link
Copy Markdown
Member

But, our node can be always on and wallets can rely on it to continually rebroadcast until it is confirmed

If bitcoind is open, a wallet can be open. I don't think increasing scope of Bitcoin Core is justified.

(I don't want to drown this PR in meta-commentary in case this PR moves forward on its own merits, maybe continue on the issue #30471)

@vasild vasild 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.

ACK 9217011

@instagibbs,

An average user simply will not need this feature, so it's being aimed at, certain devs I guess

I think it is useful for users and node operators to see what happened with their transaction. Like, how long ago was it broadcast. The network roundtrip time is also useful in general, IMO.

with the tradeoff being things not promptly leaving during the happy path.

Why would that be a tradeoff? In my view the stats disappearing from the RPC output as soon as the transaction is received back is a downside. And there is zero downside with the stats remaining in the output. The user can ignore the data if not interested in it.

Why not just set -debug=privbroadcast and inspect mempool directly? Can we just do better logging if we're missing something?

Because parsing the log requires local access to the filesystem and is error prone. A successful broadcast is not the same as mempool presence. The transaction might enter the mempool by other means without being broadcast to anybody or the other way around - it might be broadcast successfully, come back to us and not enter our mempool.

The private broadcast feature ideally works without the mempool.

I'd like to see where currently tooling in master falls flat and see if there's a better way to satisfy the requirements.

IMO it "falls flat" because the stats disappear once the transaction comes back and the best way to improve that is to not remove them from the RPC output like this PR does. The stats contain information like to whom we sent the transaction and at what time and when did we receive it back and from whom. This info can't be derived by looking at the mempool.

Further, imagine writing a program (e.g. a wallet) around the RPC interface. Currently, without this PR, that program cannot distinguish between the following states: "not yet in the queue", "broadcast concluded successfully", "node was restarted, unsure if broadcast was successful or not".

Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

ACK 9217011129558f3bda626bce0cb343f2861a2967

@instagibbs,

> An average user simply will not need this feature, so it's being aimed at, certain devs I guess

I think it is useful for users and node operators to see what happened with their transaction. Like, how long ago was it broadcast. The network roundtrip time is also useful in general, IMO.

> with the tradeoff being things not promptly leaving during the happy path.

Why would that be a tradeoff? In my view the stats disappearing from the RPC output as soon as the transaction is received back is a downside. And there is zero downside with the stats remaining in the output. The user can ignore the data if not interested in it.

> Why not just set -debug=privbroadcast and inspect mempool directly? Can we just do better logging if we're missing something?

Because parsing the log requires local access to the filesystem and is error prone. A successful broadcast is not the same as mempool presence. The transaction might enter the mempool by other means without being broadcast to anybody or the other way around - it might be broadcast successfully, come back to us and not enter our mempool.

The private broadcast feature ideally works without the mempool.

> I'd like to see where currently tooling in master falls flat and see if there's a better way to satisfy the requirements.

IMO it "falls flat" because the stats disappear once the transaction comes back and the best way to improve that is to not remove them from the RPC output like this PR does. The stats contain information like to whom we sent the transaction and at what time and when did we receive it back and from whom. This info can't be derived by looking at the mempool.

Further, imagine writing a program (e.g. a wallet) around the RPC interface. Currently, without this PR, that program cannot distinguish between the following states: "not yet in the queue", "broadcast concluded successfully", "node was restarted, unsure if broadcast was successful or not".
-----BEGIN PGP SIGNATURE-----

iQRPBAEBCAA5FiEE5k2NRWFNsHVF2czBVN8G9ktVy78FAmqhJRUbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMiwzAAoJEFTfBvZLVcu/zH4f/0FAz/CmqvBfveKT/Yhe
c27NEsGkYggcO4KAXlqiCW58SV/mZYetfFAEw543PoLfTzn3UutbYh+fWEh8/nFL
xMkseR5gbam1oLa3cIdF/NT1opC4VePzlfLkPUyQfDBm5UyWyWge6tYp5bRzGcHY
9dMCdjBseWCbuSCk3fufjKWbBWaBkkxJRxDOJxOp/KzvnJGP6seZcFpqImafVypT
IKFGXL0gIQIF1Zspr9/12QTeZQcjKqmlkz7spM/Er/dzRdMxAzAzIbOIVa90iDER
8EqQxV0dE/H/moH73Gpt8Ccbgh+uDDLIO2yFygyIwrSaowRlNGPS+CzVOUaCABhR
5eSBq5pL4EFMwkHVmiHAw41TgNytkofY9CNrUaLjWdpL53TTr6F/0KV/FdKUjVmI
16iBJ6eDQY6hi1CpsaGi9MNrFuPX6OzTc21MtlN8KGZWtIfIGT1u76Dlkiczk9o0
YaSJky1JXs9DblatSuRvipO88OETxF6+2yG3CDS34gQMPcdOM939zPHLGuN7F4XU
1Xi8ghPWFLK4j4Anc5RTGXCWJBoTDdpEz6l/2FTHNlqRYQtOaBuUUbz/vIVHI9Mt
bQ/RJO+fgy6ydWF2CJFHx9bgp2Y1sfSg9ebpglDuLXymqJjGC5EzH5Rznt5V3daT
hXDPVwnpBywQcOFDGwDhpReemptetEk/Xx6iGQCIjOn2F02/PSIFxR+3VVDoIXC1
U0ceT6F4D7n3NpqbO9KPRwVGBC0xJl9z4XKep7iHx+R8liLQM0VwU6vUURGgQMzR
iS1txc5s7+DXP7Gl7VfbayLkw6hkcnpvvZKLe5B7Q/MXySUaK4ReMLrzAQrIG0Pn
BtxLArBrvxM93wiB0zfmAx8GNADmYsHIkRdbBRRlE6HulJmkGve68zg+JBWz0YPr
pob89lF5lzc8IpAgkxWuR6Da4OvlAT4HLe5kJcenfltOvHSoyNxJzxmlxkaeYJ9K
5vLY7GxzncEoqbDxXG34RWgpYGfHa18qs8BC6kZfjSml6puhNtdi1neEccpPXD6A
UN9zi+dSM6TpTckIqNCjkm8cVUHa05C7pj3ejPRKmxQ/nYh90a53nVTvO/QaN+R4
RZSU9s3QDXxYPMSz3D2xbDRxXqyiOyfplCgpZG4erZU8kPOU2+MWaGetq8ltwYTK
HnoV9yjoOTP2BWwEMBapFmWqJZOdaG6pNBpA1qNYqPpeAu7qi030IPRNCzO0jrdh
5V3y9smxVtbVXIkMq+B30JmW2JvYrbfSh+VNtFRoBzLPgcKj9v4dD8SB8z5PGteD
IwEt7YhMT/1bRYmkU3A9nAdyHo9uF/H+I9IM9ofyO/75qpeM6435j2xVEtaxXgM9
ElY=
=w3c+
-----END PGP SIGNATURE-----

vasild's public key is on openpgp.org

Comment thread src/private_broadcast.cpp
}
const size_t attempts_remaining{m_max_send_attempts - std::min(state.send_statuses.size(), m_max_send_attempts)};
entries.emplace_back(TxBroadcastInfo{.tx = tx, .time_added = state.time_added, .attempts_remaining = attempts_remaining, .peers = std::move(peers)});
const size_t attempts_remaining{IsPending(state) ? m_max_send_attempts - state.send_statuses.size() : 0};

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.

This implies that if IsPending() returns true then m_max_send_attempts >= state.send_statuses.size(). I can see how this ends with assigning a negative value to size_t after a future change in IsPending().

It is ok to rely on that if IsPending() is documented to behave in that way. But it is not documented at all. So either document it like "when this function returns then m_max_send_attempts >= state.send_statuses.size() will be true". Or, I would prefer add the condition here, then it is more obvious and the reader does not have to jump to other places in the code to check for the correctness:

const size_t attempts_remaining{IsPending(state) && m_max_send_attempts >= state.send_statuses.size()
    ? m_max_send_attempts - state.send_statuses.size()
    : 0};

Btw currently when IsPending() returns m_max_send_attempts > state.send_statuses.size() is true (off-by-one in the "good" direction).

@instagibbs

Copy link
Copy Markdown
Member

I think it is useful for users and node operators to see what happened with their transaction. Like, how long ago was it broadcast.

Not saying it's not useful to a couple people, but I don't think it's worth it, given there are costs and limited realistic users.

The network roundtrip time is also useful in general, IMO.

Sure, we don't need tons of variable sized state for that. A segregated buffer of timings with their according wtxid may be handy and less heavy weight. But this goes against the desire to keep tx around "just in case", which I disagree with as motivated enough, at a minimum.

While it's not longer ever-increasing with explicit caps, I don't think holding around transactions forever for a tiny minority use case is "no tradeoff". If you just meant the "stats", there's less downside, I agree as I mention above, and we should think of what those stats should be. Could start with the obvious accumulator-y kinda things, number of failure

Gave my two cents, I'll let others weigh in now.

@andrewtoth

andrewtoth commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor Author

As discussed with @instagibbs offline, this will also close the theoretical timing attack discussed in #29415 (comment).

Essentially it works with a sybil attacker Alice receiving all 3 txs from Bob, and on the third connection:

  1. Alice receives the inv but does not yet send getdata.
  2. Alice opens another connection to Bob (or already has a long-lived connection to him), and sends the tx to him.
  3. Alice now sends the getdata to Bob over the private broadcast connection. If Bob closes the connection instead of responding with the tx message, Bob is confirmed to Alice to be the tx initiator.

With the tx held in memory after being received back, this attack is thwarted since Bob will reply with the tx message in step 3 whether or not he was given the tx via a different connection by Alice.

@instagibbs

Copy link
Copy Markdown
Member

This PR is neither necessary nor sufficient for plugging known privacy leaks

@andrewtoth

Copy link
Copy Markdown
Contributor Author

This PR is neither necessary nor sufficient for plugging known privacy leaks

I may be missing some context on other known privacy leaks. I did not make any claims about anything other than the timing attack outlined in #34707 (comment).
Can you elaborate on why it is insufficient at closing that attack? I'm not convinced it can be exploited in the first place, but my understanding is that retaining the transaction would remove the observable difference described there.

@andrewtoth

Copy link
Copy Markdown
Contributor Author

@optout21 @danielabrozzoni would either of you care to re-review this?

@optout21 optout21 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.

ACK 9217011

I'd like to state some cons first:

  • It adds some extra complexity (pending vs. non-pending, removal if needed, extra corner cases, etc.).
  • It's not evident that the extra complexity is worth the benefits.

Pros:

  • The extra complexity is encapsulated rather well into the private broadcast implementation.
  • The received info can be useful for some clients.
  • Received info can be especially useful in case of a future delayed broadcast feature.

Overall, I'm for it.
The implementation LGTM. Left a 1-2 comments.
Re-reviewed (as previous was some time ago).
Built and executed tests locally.

Comment thread src/private_broadcast.h
Comment on lines +74 to +75
/// Number of additional send attempts allowed for this transaction (0 if
/// exhausted or already received back from the network).

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.

f6753e7 net: track received peer in private broadcast:

Nit: Line-breaking, the parenthesis part could be in the second line entirely.

Comment thread src/private_broadcast.cpp
Comment on lines +27 to +35
if (m_transactions.size() >= m_max_transactions) {
// Make room by dropping the oldest transaction that is no longer being broadcast.
auto finished{m_transactions | std::views::filter([this](const auto& entry) { return !IsPending(entry.second); })};
const auto oldest{std::ranges::min_element(finished, {}, [](const auto& entry) { return entry.second.time_added; })};

if (oldest == finished.end()) return AddResult::QueueFull;

m_transactions.erase(oldest.base());
}

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.

f6753e7 net: track received peer in private broadcast:

The if body removes at most one element, but the condition checks for larger. So there is no guarantee, that after the if m_transactions.size() < m_max_transactions.
The if could be made into a while to achieve this. After the if there could be an assert for m_transactions.size() < m_max_transactions (or after the try_emplace, with <=).

sedited added a commit that referenced this pull request Oct 6, 2026
…ections

00c1b52 net: remove PrivateBroadcast::DidNodeConfirmReception (Andrew Toth)
dc95a70 net: always complete all initial private broadcast connections (Andrew Toth)

Pull request description:

  Make sure we send out all three connections when initiating a private broadcast.

  Use `MarkResolved` instead of `Remove` to mark a tx as having been received back or mempool-conflicted. Prevent additional stale retry connections by tracking disconnects and explicitly granting new connections with `TryGrantRetry`.

  Intended as an alternative to #34707, addressing the theoretical timing attack described in #34707 (comment). Also fixes theoretical timing attacks described in #29415 (comment). This is based on the idea described in #29415 (comment):

  > always send 3 times regardless if the tx is received in our mempool or is otherwise invalid

ACKs for top commit:
  vasild:
    ACK 00c1b52
  mzumsande:
    Code Review ACK 00c1b52
  sedited:
    utACK 00c1b52

Tree-SHA512: 82c03898e9377f9702ee4e01a3a68cd2071fe6e137c531812761da145acb16780ca866b33c7ffbd2db5db100e203b27095ae9e9745c012e317434cd2f5368847
@DrahtBot

DrahtBot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

@andrewtoth

Copy link
Copy Markdown
Contributor Author

After #36277 this needs a rework, and there are also outstanding conceptual issues. Will close as up for grabs.

@vasild

vasild commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

I am working on adapting this on the latest master (with #36277 merged).

Edit: here #36477

Kino1994 pushed a commit to Kino1994/bitcoin-full-history that referenced this pull request Oct 12, 2026
… broadcast connections

00c1b52ee56b9a2f067befc56c171eef27efda41 net: remove PrivateBroadcast::DidNodeConfirmReception (Andrew Toth)
dc95a705d402d44b7092150878e33c75383a4d47 net: always complete all initial private broadcast connections (Andrew Toth)

Pull request description:

  Make sure we send out all three connections when initiating a private broadcast.

  Use `MarkResolved` instead of `Remove` to mark a tx as having been received back or mempool-conflicted. Prevent additional stale retry connections by tracking disconnects and explicitly granting new connections with `TryGrantRetry`.

  Intended as an alternative to #34707, addressing the theoretical timing attack described in bitcoin/bitcoin#34707 (comment). Also fixes theoretical timing attacks described in bitcoin/bitcoin#29415 (comment). This is based on the idea described in bitcoin/bitcoin#29415 (comment):

  > always send 3 times regardless if the tx is received in our mempool or is otherwise invalid

ACKs for top commit:
  vasild:
    ACK 00c1b52ee56b9a2f067befc56c171eef27efda41
  mzumsande:
    Code Review ACK 00c1b52ee56b9a2f067befc56c171eef27efda41
  sedited:
    utACK 00c1b52ee56b9a2f067befc56c171eef27efda41

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants