Skip to content

net: deduplicate private broadcast state and snapshot types - #35016

Closed
kenji-yamam0to wants to merge 1 commit into
bitcoin:masterfrom
kenji-yamam0to:deduplicate-private-broadcast-types
Closed

kenji-yamam0to wants to merge 1 commit into
bitcoin:masterfrom
kenji-yamam0to:deduplicate-private-broadcast-types

Conversation

@kenji-yamam0to

@kenji-yamam0to kenji-yamam0to commented Apr 6, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up cleanup in the private broadcast subsystem.

This PR is inspired by #34707 (comment).

This change removes duplication between the internal private broadcast state
and the snapshot used by PeerManager, RPC, and tests.

  • Remove PeerSendInfo and TxSendStatus.
  • Reuse SendStatus directly for per-peer broadcast status.
  • Expose PrivateBroadcast::Transactions as the snapshot type.
  • Keep getprivatebroadcastinfo JSON output unchanged.

No behavior change intended.

@DrahtBot DrahtBot added the P2P label Apr 6, 2026
@DrahtBot

DrahtBot commented Apr 6, 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/35016.

Reviews

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

Type Reviewers
ACK danielabrozzoni, optout21
Concept ACK nervana21
Approach NACK andrewtoth
Stale ACK vasild

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:

  • #34628 (p2p: Replace per-peer transaction rate-limiting with global rate limits by ajtowns)
  • #33922 (mining: add getMemoryLoad() and track template non-mempool memory footprint by Sjors)

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

Copy link
Copy Markdown
Contributor

Looks like this was pulled out of the conversation here #34707 (comment).

cc @vasild

@fanquake

fanquake commented Apr 7, 2026

Copy link
Copy Markdown
Member

https://github.com/bitcoin/bitcoin/actions/runs/24045821228/job/70255455947?pr=35016#step:9:1647:

[ 21%] Building CXX object src/CMakeFiles/bitcoin_node.dir/rpc/mempool.cpp.o
cd /home/admin/actions-runner/_work/bitcoin/bitcoin/ci_build/src && /usr/bin/ccache /usr/bin/clang++ -DABORT_ON_FAILED_ASSUME -DBOOST_MULTI_INDEX_DISABLE_SERIALIZATION -DBOOST_NO_CXX98_FUNCTION_BASE -DDEBUG -DDEBUG_LOCKCONTENTION -DDEBUG_LOCKORDER -DENABLE_EMBEDDED_ASMAP=1 -DENABLE_ZMQ=1 -DRPC_DOC_CHECK -I/home/admin/actions-runner/_work/bitcoin/bitcoin/ci_build/src -I/home/admin/actions-runner/_work/bitcoin/bitcoin/src -I/home/admin/actions-runner/_work/bitcoin/bitcoin/src/leveldb/include -I/home/admin/actions-runner/_work/bitcoin/bitcoin/src/minisketch/include -I/home/admin/actions-runner/_work/bitcoin/bitcoin/src/univalue/include -Wno-error=unused-member-function -O0 -ftrapv -g3 -std=c++20 -fPIC -fmacro-prefix-map=/home/admin/actions-runner/_work/bitcoin/bitcoin/src=. -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -Wstack-protector -fstack-protector-all -fcf-protection=full -fstack-clash-protection -Wall -Wextra -Wgnu -Wcovered-switch-default -Wformat -Wformat-security -Wvla -Wshadow-field -Wthread-safety -Wloop-analysis -Wredundant-decls -Wunused-member-function -Wdate-time -Wconditional-uninitialized -Woverloaded-virtual -Wsuggest-override -Wimplicit-fallthrough -Wunreachable-code -Wdocumentation -Wself-assign -Wundef -Wno-unused-parameter -Werror -MD -MT src/CMakeFiles/bitcoin_node.dir/rpc/mempool.cpp.o -MF CMakeFiles/bitcoin_node.dir/rpc/mempool.cpp.o.d -o CMakeFiles/bitcoin_node.dir/rpc/mempool.cpp.o -c /home/admin/actions-runner/_work/bitcoin/bitcoin/src/rpc/mempool.cpp  -O3 -g2
/home/admin/actions-runner/_work/bitcoin/bitcoin/src/rpc/mempool.cpp:187:86: error: no member named 'time_added' in 'PrivateBroadcast::TxBroadcastInfo'
  187 |                 o.pushKV("time_added", TicksSinceEpoch<std::chrono::seconds>(tx_info.time_added));
      |                                                                              ~~~~~~~ ^
/home/admin/actions-runner/_work/bitcoin/bitcoin/src/rpc/mempool.cpp:189:49: error: no member named 'peers' in 'PrivateBroadcast::TxBroadcastInfo'
  189 |                 for (const auto& peer : tx_info.peers) {
      |                                         ~~~~~~~ ^
2 errors generated.

@DrahtBot

DrahtBot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task test ancestor commits: https://github.com/bitcoin/bitcoin/actions/runs/24045821228/job/70255455947
LLM reason (✨ experimental): CI failed due to a build error while compiling rpc/mempool.cpp.o for bitcoin_node (CMake/gmake returned non-zero).

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.

@kenji-yamam0to

kenji-yamam0to commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Fixed. The CI failure was due to a non-atomic commit split: the first commit changed the TxBroadcastInfo layout while rpc/mempool.cpp was only updated in the second commit. Squashed them into one so each commit builds cleanly.

I also noticed this PR overlaps with the dedup suggested by @vasild in #34707 (comment). Happy to rebase on top of #34707 or close this if it becomes redundant after that PR lands.

@DrahtBot
DrahtBot marked this pull request as draft April 9, 2026 08:15
@kenji-yamam0to
kenji-yamam0to force-pushed the deduplicate-private-broadcast-types branch 2 times, most recently from 6275b82 to 86643bb Compare April 9, 2026 11:29
@kenji-yamam0to
kenji-yamam0to marked this pull request as ready for review April 9, 2026 11:34
@vasild vasild mentioned this pull request Apr 14, 2026
22 of 27 tasks

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

Concept ACK

It is convenient if you cross link a PR in case it has a specific motivator. In this case "This PR is inspired by #34707 (comment).

Thanks!

Comment thread src/test/private_broadcast_tests.cpp Resolved
Comment thread src/private_broadcast.h Resolved
Comment thread src/test/private_broadcast_tests.cpp Resolved
@kenji-yamam0to

Copy link
Copy Markdown
Contributor Author

Addressed the remaining review comments in 4a89e31da7.

Tested:

  • ./build_pr35016_linux/bin/test_bitcoin --run_test=private_broadcast_tests
  • python3 ./build_pr35016_linux/test/functional/test_runner.py p2p_private_broadcast.py -j 1

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

Update: see #35016 (comment)

A relatively straightforward merging of duplicated structs, done in two stages to minimize diffs. An existing unit test has been also extended with some extra checks.
I have verified by reimplementing the moves and updates.
Left some remarks, none major.

Comment thread src/test/private_broadcast_tests.cpp Resolved
Comment thread src/test/private_broadcast_tests.cpp Resolved
Comment thread src/private_broadcast.h Resolved
@kenji-yamam0to
kenji-yamam0to force-pushed the deduplicate-private-broadcast-types branch from 4a89e31 to 9bb2e40 Compare April 18, 2026 10:30
@kenji-yamam0to

kenji-yamam0to commented Apr 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I've cleaned up the history to remove the transient snapshot_structural_invariant add/remove step, split the NodeClockContext changes into a final test-only commit, and clarified the duplicated CTransactionRef storage tradeoff in the main commit message while fixing its formatting.

@optout21

Copy link
Copy Markdown
Contributor

Thanks for the changes.

I've cleaned up the history to remove the [...] add/remove step

By suggesting to remove the adding of a large 'temporary' test that is removed in the next commit, I didn't mean to squash to two commits, but I had in mind changing the first commit (and the second too, but that's an automatic merge-type operation). In this particular case the squashed commit is fine, but in general in such cases it's better to preserve the original commits (unless there is an explicit motivation for squash). So it's OK, this remark is more for the future.

@DrahtBot
DrahtBot requested a review from vasild April 21, 2026 09:54

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

cr 9bb2e40

My previous concerns have been addressed, LGTM. (#35016 (review))

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

tACK 9bb2e40

The cleanup looks good to me. The PR is removing TxSendStatus and PeerSendInfo, in favor of using TxBroadcastInfo and SendStatus everywhere in the code. As a consequence, SendStatus is moved to public scope.

Also nice to see the private_broadcasts tests use NodeClockContext instead of SetMockTime, as far as I understand this is preferred in unit tests as it doesn't need to be manually cleaned up at the end of the test.

Comment thread src/private_broadcast.h Resolved
@nervana21

Copy link
Copy Markdown
Contributor

Concept ACK

@sedited

sedited commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Ping @danielabrozzoni @optout21 to take another look here.

@optout21

optout21 commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

ACK c307e64b3f3942ec24b91ea3d3cc41a0a89ac310

Re-reviewed after rebase, no significant changes (prev: #35016 (comment))
git range-diff e6e6c3fa34c61572fafc9e26b01d7d259ec399a8...c307e64b3f3942ec24b91ea3d3cc41a0a89ac310

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

EDIT: ouch! rebase conflict! oh well, I will review again after rebase :)

tACK c307e64b3f3942ec24b91ea3d3cc41a0a89ac310

Deduplicating the structures makes the code easier to follow and reason about.

Changes introduced in this PR:

  • SendStatus is unchanged, but moved from private to public inside PrivateBroadcast
  • TxSendStatus is renamed to Status, unchanged, and moved from private to public inside PrivateBroadcast
  • PeerSendInfo and TxBroadcastInfo are removed:
    • Instead of building a vector<TxBroadcastInfo>, GetBroadcastInfo() now returns a copy of m_transactions. This also exposes the NodeId of each peer, which is ok.
    • CTransactionRefHash/CTransactionRefComp are made public (needed in order to return m_transactions)
    • Instead of using PeerSendInfo, we use SendStatus

Also, in the test:

  • Drop find_tx_info: now that GetBroadcastInfo returns m_transactions, we can just call .at()
  • Check that time_added matches the expected value

@kenji-yamam0to
kenji-yamam0to force-pushed the deduplicate-private-broadcast-types branch from c307e64 to 70f6026 Compare July 11, 2026 14:46
Store private broadcast entries in a transaction-keyed map and
return that map as the snapshot from GetBroadcastInfo().

Remove the snapshot-only wrapper types and avoid storing the same
CTransactionRef in both the map key and value. Callers use the
CTransactionRef key for transaction data, while Status keeps the
per-transaction broadcast state.

The getprivatebroadcastinfo JSON output is unchanged.

No behavior change intended.
@kenji-yamam0to
kenji-yamam0to marked this pull request as draft July 11, 2026 15:10
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task NetBSD Cross: https://github.com/bitcoin/bitcoin/actions/runs/29156714592/job/86554916210
LLM reason (✨ experimental): CI failed due to a Clang build error in the fuzz target (private_broadcast.cpp), where info is a std::pair but the code incorrectly accesses nonexistent members tx and peers.

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.

@kenji-yamam0to
kenji-yamam0to force-pushed the deduplicate-private-broadcast-types branch from 70f6026 to 32e24b1 Compare July 11, 2026 15:42
@kenji-yamam0to
kenji-yamam0to marked this pull request as ready for review July 11, 2026 17:06
@kenji-yamam0to

Copy link
Copy Markdown
Contributor Author

No behavior or RPC JSON changes intended.

Rebased on current master (ef101b04a8). Kept the #35678 changes and
adapted the unit/fuzz tests to the Transactions map.

Range-diff reviewed: c307e64b3f -> 32e24b1510. Unit, functional, and
fuzz tests pass; CI is green.

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

reACK 32e24b1

@DrahtBot
DrahtBot requested a review from vasild July 13, 2026 10:30
@optout21

optout21 commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

reACK 32e24b1

Re-reviewed, no significant change. (prev: #35016 (comment))
git range-diff c307e64b3f3942ec24b91ea3d3cc41a0a89ac310...32e24b15106d783dbcd87e19a1b0abd71f72a296

(Unfortunately I was not able to find the previously-reviewed commit c307e64b3f3942ec24b91ea3d3cc41a0a89ac310, neither in kenji-yamam0to nor in takeshikurosawaa remotes.)

@maflcko

maflcko commented Jul 14, 2026

Copy link
Copy Markdown
Member

(Unfortunately I was not able to find the previously-reviewed commit c307e64, neither in kenji-yamam0to nor in takeshikurosawaa remotes.)

It should be in all remotes. You can fetch a commit via:

$ git fetch https://github.com/bitcoin/bitcoin c307e64b3f3942ec24b91ea3d3cc41a0a89ac310
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (10/10), done.
remote: Total 11 (delta 10), reused 10 (delta 10), pack-reused 1 (from 1)
Unpacking objects: 100% (11/11), 26.27 KiB | 4.38 MiB/s, done.
From https://github.com/bitcoin/bitcoin
 * branch                  c307e64b3f3942ec24b91ea3d3cc41a0a89ac310 -> FETCH_HEAD

@fanquake

Copy link
Copy Markdown
Member

cc @instagibbs

@instagibbs

Copy link
Copy Markdown
Member

could be wrong, but I think this will directly conflict with the stated goals of #35680 (comment)

@DrahtBot

Copy link
Copy Markdown
Contributor

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

@andrewtoth

Copy link
Copy Markdown
Contributor

After looking at #35680 and #35016, I'm approach NACK on this, sorry.

#35680 is fixing an unbounded state issue, which directly conflicts with this approach which is just a refactor.

This also exposes more internal state to callers, which I think is the wrong direction. We should try and keep state encapsulated so callsites can't depend on internal layout.

@fanquake

fanquake commented Aug 3, 2026

Copy link
Copy Markdown
Member

Closing for now, as it's not clear this is the correct direction to take.

@fanquake fanquake closed this Aug 3, 2026
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.