feat(network): move Pools to TcpTransport + PeerActor-as-coordinator (12/n)#15586
Merged
shreyan-gupta merged 3 commits intomasterfrom Apr 29, 2026
Merged
feat(network): move Pools to TcpTransport + PeerActor-as-coordinator (12/n)#15586shreyan-gupta merged 3 commits intomasterfrom
shreyan-gupta merged 3 commits intomasterfrom
Conversation
0366ca8 to
dc69523
Compare
b161623 to
e9559c7
Compare
5219c27 to
7ccac14
Compare
e9559c7 to
0fa30fd
Compare
7ccac14 to
33903a6
Compare
0f6053a to
fa53dce
Compare
33903a6 to
c22319f
Compare
c22319f to
a85230d
Compare
465f304 to
3378772
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #15586 +/- ##
==========================================
- Coverage 69.49% 69.48% -0.01%
==========================================
Files 942 942
Lines 215148 215133 -15
Branches 215148 215133 -15
==========================================
- Hits 149521 149491 -30
- Misses 59710 59725 +15
Partials 5917 5917
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
3378772 to
a4ecc66
Compare
a85230d to
84258a0
Compare
a4ecc66 to
959a79d
Compare
84258a0 to
b729d5b
Compare
959a79d to
c6dc5e9
Compare
0202790 to
ad97cf7
Compare
4429ed9 to
38aed11
Compare
4eb1ff9 to
7d0d3a7
Compare
38aed11 to
2636ed3
Compare
7d0d3a7 to
397e46b
Compare
2636ed3 to
e76d2d1
Compare
397e46b to
4c6787e
Compare
pugachAG
approved these changes
Apr 28, 2026
e76d2d1 to
764d40b
Compare
20bde0b to
8a4d3f2
Compare
19cea36 to
17b1eaa
Compare
954bbdc to
9611d30
Compare
6a16f8b to
8a4262e
Compare
9611d30 to
26f4173
Compare
8a4262e to
b986020
Compare
26f4173 to
2975ba4
Compare
…inator
Final target architecture. NetworkState has zero Pool fields; PeerActor
coordinates register/unregister between NetworkState and TcpTransport.
Changes
-------
**Pool ownership move**
- `tier1`/`tier2`/`tier3: Pool` and `inbound_handshake_permits` move
from NetworkState to TcpTransport.
- `LIMIT_PENDING_PEERS` const moves with them (from
`network_state/mod.rs` to `tcp_transport.rs`).
- TcpTransport's trait impl switches from `self.state.tier_N.X` to
direct `self.tier_N.X`.
**TcpTransport internals**
- `pool_insert(tier, conn) -> Result<(), RegisterPeerError>` and
`pool_remove(tier, &conn)` — internal methods called by PeerActor
during register/unregister. NOT on the trait (design-v2/trait.md
is explicit).
- `self_weak: Weak<Self>` populated via `Arc::new_cyclic`, upgraded
inside `connect_to_peer` to pass `Arc<Self>` to
`PeerActor::spawn_and_handshake`.
- `handle: TokioRuntimeHandle<PeerManagerActor>` replaced with
`spawner: Box<dyn FutureSpawner>`. Production gets it via
`handle.future_spawner()`; tests can pass a standalone spawner.
Lets `peer/testonly.rs` build a working TcpTransport without a
PMA `TokioRuntimeHandle`.
**PeerActor as coordinator**
- `PeerActor` field `transport: Arc<dyn NetworkTransport>` →
`tcp: Arc<TcpTransport>` (concrete, per design-v2/peer-actor.md).
- `PeerActor::spawn` / `spawn_and_handshake` signatures take
`Arc<TcpTransport>` instead of `Arc<dyn NetworkTransport>`.
- `spawn_and_handshake` no longer materializes a throwaway
`TcpTransport::new_wrapping(state)` internally — caller passes
the real one.
- Register flow (3-step) inlined in PeerActor:
1. `state.validate_new_connection(&conn, &edge, &*tcp)?`
2. `tcp.pool_insert(conn.tier, conn.clone())?`
3. `state.on_peer_connected(&clock, edge, &conn, tcp.clone()).await`
- Unregister flow (2-step) inlined in PeerActor::stopping:
1. `tcp.pool_remove(conn.tier, &conn)` (synchronous)
2. Fire-and-forget spawn of `state.on_peer_disconnected(...)`
**Deletions**
- `NetworkState::register` / `NetworkState::unregister` monolithic
methods.
- `TcpTransport::new_wrapping` stub constructor.
- `OutboundTcpConnect` variant + handler on PMA. The handler is
replaced by `TcpTransport::spawn_outbound_from_stream` (pub —
callable from unit tests and integration-tests), driven via the
`Arc<TcpTransport>` exposed from `PeerManagerActor::spawn`'s
return tuple.
**PMA::spawn return change**
- Returns `(TokioRuntimeHandle<Self>, Arc<TcpTransport>)` instead of
just the handle. Production callers (`nearcore/src/lib.rs`,
`integration-tests/src/tests/network/{peer_handshake,runner}.rs`)
discard `_tcp`. Test fixtures keep the `Arc<TcpTransport>` for
`spawn_outbound_from_stream` and direct Pool-state inspection.
- `ActorHandler` in `peer_manager/testonly.rs` gains `tcp:
Arc<TcpTransport>` field. `send_outbound_connect`, `connect_to`,
`start_outbound` now call `self.tcp.spawn_outbound_from_stream`
directly.
- `integration-tests` runner: `NodeHandle` gains `tcp` field;
`setup_network_node_with_tcp` returns both. The action that
previously sent `OutboundTcpConnect` now calls
`tcp.spawn_outbound_from_stream` directly.
**Test fixture migrations**
- `peer_manager/testonly.rs`: `wait_for_direct_connection`,
`wait_for_num_connected_peers`, `check_consistency`, `disconnect`
read `self.tcp.tier2.load()` directly instead of reaching through
`s.tier2.load()` via `with_state`.
- `peer_manager/testonly.rs:send_ping` switches to
`with_state_and_transport` instead of constructing a throwaway.
- `peer_manager/tests/tier1.rs:send_message_to_account` same.
- `peer_manager/tests/routing.rs` reads `pm0.tcp.tier2.load()`.
- `peer/testonly.rs` builds a TcpTransport via
`new_with_spawner(actor_system.new_future_spawner(...))`.
Validation
----------
- `grep 'self\.tier[123]\.' chain/network/src/peer_manager/network_state/`
— empty. No Pool references on NetworkState.
- `grep 'Arc<dyn NetworkTransport>' chain/network/src/peer/peer_actor.rs`
— zero. PeerActor holds `Arc<TcpTransport>` concretely.
- `grep 'OutboundTcpConnect' chain/network/src/` — zero. The variant
and all handlers are gone.
- `grep 'new_wrapping' chain/ integration-tests/ nearcore/` — zero.
- NetworkState no longer has `tier1`/`tier2`/`tier3`/
`inbound_handshake_permits` fields.
- TcpTransport owns the Pools; its trait impl uses `self.tier_N`
directly.
After this PR, the de-entanglement is complete. Stage 2 can now
implement `TestLoopTransport` against the 7-method trait and drop in
the real PMA.
…ency guard, Weak Arc, transport_info, shutdown)
2975ba4 to
4adf4a4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Final PR. NetworkState has zero Pool fields, TcpTransport owns them, PeerActor coordinates the lifecycle.
tier1/tier2/tier3: Pool,inbound_handshake_permits,LIMIT_PENDING_PEERSmove from NetworkState to TcpTransportself.state.tier_N→self.tier_Npool_insert(tier, conn)/pool_remove(tier, &conn)on TcpTransport, called by PeerActorstate.validate → tcp.pool_insert → state.on_peer_connectedtcp.pool_remove → state.on_peer_disconnected(fire-and-forget)NetworkState::register/NetworkState::unregisterPeerManagerActor::spawnreturns(handle, Arc<TcpTransport>)tupletcp_transport.spawn_outbound_from_stream