Repository navigation
net: keep finished private broadcast txs in memory - #34707
andrewtoth wants to merge 4 commits into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34707. ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. |
a88810a to
afca844
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
There was a problem hiding this comment.
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).
|
A more important conceptual point I'd like to raise: As I see:
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) |
| 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 |
There was a problem hiding this comment.
GetStale() needs to be changed to omit the concluded (finished) transactions.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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.
2c3c80d to
9f58d9a
Compare
|
Thank you for your reviews @optout21 and @vasild.
Also addressed several other refactor and style changes, and split up and reordered commits for easier review.
@optout21 that's an interesting point. I do think it is out of scope for this PR though. Could be raised independently. |
9f58d9a to
c9b19d7
Compare
|
Updated test to fix a bad rebase. Also, addressed feedback to make rebroadcast logic more clear #34707 (comment).
|
c9b19d7 to
6ecbf18
Compare
911d4eb to
bd31f73
Compare
|
Changed the approach to use an enum for the mempool states we care about: This should make it easier to reason about what is happening in this PR. |
bd31f73 to
420abcb
Compare
vasild
left a comment
There was a problem hiding this comment.
ACK a0508a3
Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
ACK a0508a315935eec89e92b29d8cb99ba073e22f61
-----BEGIN PGP SIGNATURE-----
iQRPBAEBCAA5FiEE5k2NRWFNsHVF2czBVN8G9ktVy78FAmqYQFUbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMiwzAAoJEFTfBvZLVcu/ghIf/ib9EdCLB/4pfiM0hPn/
eCR/HvGTEir1QNxUE0LBReE0xQwyUaTf5Lvuj3qrBgexoEwGcHTUsY0lJxoz85YH
3RSNPP/58W33GyWfP9x08BX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l
Cs7w7JRJcvgMxEZACh3XM26cHpXbrxEClLQzb/Q8iQUiliedyE84p92djZyqDxoO
i7YXtAeMMJD1JC6XPhwbUXPthxC6DiK9Cy8zJfSgBQc4L+4h1mnBhz9Q5RjYA18d
I8UfCdimN9eUNfzdVtxraeWOdEoxVhKpxbXq41VqB8nloJvjO0jXctcdeXgyHx7D
/8o=
=YbDU
-----END PGP SIGNATURE-----
vasild's public key is on openpgp.org
There was a problem hiding this comment.
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.
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. |
getrawtransaction?
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. |
|
|
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.
Non-functional refactor.
a0508a3 to
9217011
Compare
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
left a comment
There was a problem hiding this comment.
ACK 9217011
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
| } | ||
| 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}; |
There was a problem hiding this comment.
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).
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.
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. |
|
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:
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. |
|
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). |
|
@optout21 @danielabrozzoni would either of you care to re-review this? |
optout21
left a comment
There was a problem hiding this comment.
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.
| /// Number of additional send attempts allowed for this transaction (0 if | ||
| /// exhausted or already received back from the network). |
There was a problem hiding this comment.
f6753e7 net: track received peer in private broadcast:
Nit: Line-breaking, the parenthesis part could be in the second line entirely.
| 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()); | ||
| } |
There was a problem hiding this comment.
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 <=).
…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
|
🐙 This pull request conflicts with the target branch and needs rebase. |
|
After #36277 this needs a rework, and there are also outstanding conceptual issues. Will close as up for grabs. |
… 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
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
getprivatebroadcastinfoRPC.Mempool rejected txs are still removed from the queue as before.