Skip to content

refactor: decompose Peer struct into focused sub-components#34588

Open
w0xlt wants to merge 5 commits intobitcoin:masterfrom
w0xlt:refactor/decompose-peer-struct
Open

refactor: decompose Peer struct into focused sub-components#34588
w0xlt wants to merge 5 commits intobitcoin:masterfrom
w0xlt:refactor/decompose-peer-struct

Conversation

@w0xlt
Copy link
Contributor

@w0xlt w0xlt commented Feb 14, 2026

The Peer struct in net_processing.cpp has ~40 fields protected by 6 different mutexes, mixing address relay, headers sync, block announcements, ping/pong, transaction relay, and misbehavior state into one flat struct.
This makes it hard to see which fields belong together and which mutex protects what.

This PR groups related fields into four sub-structs, each owning its mutex where applicable:

Sub-struct Fields Mutex
PeerAddrRelay 12 m_addr_send_times_mutex
PeerHeadersSyncState 7 m_headers_sync_mutex
PeerBlockAnnouncement 3 m_block_inv_mutex
PeerLatency 3 (all atomic)

Access changes from flat (peer.m_addrs_to_send) to qualified (peer.m_addr_relay.m_addrs_to_send). No locking semantics, runtime behavior, or public interfaces change.

The first commit defines the four structs and adds them as members of Peer. The remaining four commits each move one group of fields and update all call sites.

Define four new sub-structs in the anonymous namespace before Peer:
- PeerAddrRelay: address gossip fields and mutex
- PeerHeadersSyncState: headers sync fields and mutex
- PeerBlockAnnouncement: block inv relay fields and mutex
- PeerLatency: ping/pong atomics (no mutex needed)

Add four member objects to Peer (m_addr_relay, m_headers_sync_state,
m_block_announcement, m_latency). No call sites change yet -- old
fields still exist alongside the new sub-struct members.

This prepares for subsequent scripted-diff commits that will move
fields from Peer into the corresponding sub-structs.
Move-only rename: all address relay fields (m_addrs_to_send,
m_addr_known, m_addr_relay_enabled, m_getaddr_sent, m_getaddr_recvd,
m_wants_addrv2, m_addr_token_bucket, m_addr_token_timestamp,
m_addr_rate_limited, m_addr_processed, m_next_addr_send,
m_next_local_addr_send, m_addr_send_times_mutex) are moved from
Peer into Peer::m_addr_relay (PeerAddrRelay sub-struct).

Also moves m_inv_triggered_getheaders_before_sync into
Peer::m_headers_sync_state since it was adjacent to the removed
addr fields and belongs to headers sync state.

Delete original fields from Peer, update all ~50 call sites.
Move-only rename: all headers sync fields (m_headers_sync_mutex,
m_headers_sync, m_sent_sendheaders, m_headers_sync_timeout,
m_prefers_headers, m_last_getheaders_timestamp) are moved from
Peer into Peer::m_headers_sync_state (PeerHeadersSyncState sub-struct).

Update EXCLUSIVE_LOCKS_REQUIRED annotations on
IsContinuationOfLowWorkHeadersSync() and TryLowWorkHeadersSync()
to reference peer.m_headers_sync_state.m_headers_sync_mutex.

Delete original fields from Peer, update all ~40 call sites.
Move-only rename: all block announcement fields (m_block_inv_mutex,
m_blocks_for_inv_relay, m_blocks_for_headers_relay,
m_continuation_block) are moved from Peer into
Peer::m_block_announcement (PeerBlockAnnouncement sub-struct).

Delete original fields from Peer, update all ~25 call sites.
Move-only rename: all latency measurement fields (m_ping_nonce_sent,
m_ping_start, m_ping_queued) are moved from Peer into
Peer::m_latency (PeerLatency sub-struct).

All fields are atomic so no mutex is needed for the sub-struct.

Delete original fields from Peer, update all ~25 call sites.
@DrahtBot
Copy link
Contributor

DrahtBot commented Feb 14, 2026

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

Reviews

See the guideline for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #34796 (rpc, net: remove startingheight field of getpeerinfo RPC and from node state by theStack)
  • #34743 (p2p: protect manual evictions by willcl-ark)
  • #34717 (p2p: remove m_getaddr_sent by naiyoma)
  • #34628 (p2p: Replace per-peer transaction rate-limiting with global rate limits by ajtowns)
  • #34565 (refactor: extract BlockDownloadManager from PeerManagerImpl by w0xlt)
  • #34389 (net/log: standardize peer+addr log formatting via LogPeer by l0rinc)
  • #34059 (refactor: Use NodeClock::time_point for m_addr_token_timestamp by maflcko)

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.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • will not potentially leaking addr information -> will not potentially leak addr information [grammatical error: incorrect verb form "leaking" should be the base form "leak" to follow "will not potentially"]

Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

  • LogDebug(BCLog::NET, "ping timeout: %fs, %s", 0.000001 * count_microseconds(now - peer.m_latency.m_ping_start.load()), node_to.DisconnectMsg(fLogIPs)) in src/net_processing.cpp

2026-02-14 02:47:54

@DrahtBot
Copy link
Contributor

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

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.

2 participants