Skip to content

feat(network): move Pools to TcpTransport + PeerActor-as-coordinator (12/n)#15586

Merged
shreyan-gupta merged 3 commits intomasterfrom
shreyan-gupta/network/deentangle/12
Apr 29, 2026
Merged

feat(network): move Pools to TcpTransport + PeerActor-as-coordinator (12/n)#15586
shreyan-gupta merged 3 commits intomasterfrom
shreyan-gupta/network/deentangle/12

Conversation

@shreyan-gupta
Copy link
Copy Markdown
Contributor

@shreyan-gupta shreyan-gupta commented Apr 15, 2026

Final PR. NetworkState has zero Pool fields, TcpTransport owns them, PeerActor coordinates the lifecycle.

  • tier1/tier2/tier3: Pool, inbound_handshake_permits, LIMIT_PENDING_PEERS move from NetworkState to TcpTransport
  • TcpTransport trait impl: self.state.tier_Nself.tier_N
  • pool_insert(tier, conn) / pool_remove(tier, &conn) on TcpTransport, called by PeerActor
  • Register 3-step: state.validate → tcp.pool_insert → state.on_peer_connected
  • Unregister 2-step: tcp.pool_remove → state.on_peer_disconnected (fire-and-forget)
  • Delete NetworkState::register / NetworkState::unregister
  • PeerManagerActor::spawn returns (handle, Arc<TcpTransport>) tuple
  • Integration tests updated to use tcp_transport.spawn_outbound_from_stream

@shreyan-gupta shreyan-gupta requested a review from a team as a code owner April 15, 2026 20:15
@shreyan-gupta shreyan-gupta requested review from saketh-are and removed request for a team April 15, 2026 20:15
@shreyan-gupta shreyan-gupta marked this pull request as draft April 15, 2026 20:17
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch from 0366ca8 to dc69523 Compare April 15, 2026 22:32
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch 2 times, most recently from b161623 to e9559c7 Compare April 15, 2026 22:35
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch 2 times, most recently from 5219c27 to 7ccac14 Compare April 15, 2026 22:39
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch from e9559c7 to 0fa30fd Compare April 15, 2026 22:39
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch from 7ccac14 to 33903a6 Compare April 15, 2026 23:04
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch 2 times, most recently from 0f6053a to fa53dce Compare April 16, 2026 02:37
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch from 33903a6 to c22319f Compare April 16, 2026 02:37
@shreyan-gupta shreyan-gupta changed the title feat(network): [12/n] move Pools to TcpTransport + PeerActor-as-coordinator feat(network): 12/n move Pools to TcpTransport + PeerActor-as-coordinator Apr 16, 2026
@shreyan-gupta shreyan-gupta changed the title feat(network): 12/n move Pools to TcpTransport + PeerActor-as-coordinator feat(network): move Pools to TcpTransport + PeerActor-as-coordinator (12/n) Apr 16, 2026
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch from c22319f to a85230d Compare April 16, 2026 02:54
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch from 465f304 to 3378772 Compare April 16, 2026 02:54
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 16, 2026

Codecov Report

❌ Patch coverage is 85.39326% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.48%. Comparing base (89ab526) to head (4adf4a4).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...ain/network/src/peer_manager/peer_manager_actor.rs 0.00% 10 Missing ⚠️
chain/network/src/peer_manager/tcp_transport.rs 94.11% 2 Missing ⚠️
chain/network/src/peer/peer_actor.rs 97.22% 1 Missing ⚠️
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              
Flag Coverage Δ
pytests-nightly 1.13% <0.00%> (+<0.01%) ⬆️
unittests 68.93% <85.39%> (-0.01%) ⬇️
unittests-nightly 69.03% <85.39%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@shreyan-gupta shreyan-gupta marked this pull request as ready for review April 16, 2026 04:13
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch from 3378772 to a4ecc66 Compare April 23, 2026 15:03
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch from a85230d to 84258a0 Compare April 23, 2026 15:03
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch from a4ecc66 to 959a79d Compare April 23, 2026 21:44
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch from 84258a0 to b729d5b Compare April 23, 2026 21:44
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch from 959a79d to c6dc5e9 Compare April 23, 2026 22:05
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch 2 times, most recently from 0202790 to ad97cf7 Compare April 27, 2026 14:13
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch 2 times, most recently from 4429ed9 to 38aed11 Compare April 27, 2026 22:53
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch from 4eb1ff9 to 7d0d3a7 Compare April 27, 2026 22:53
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch from 38aed11 to 2636ed3 Compare April 27, 2026 23:29
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch from 7d0d3a7 to 397e46b Compare April 27, 2026 23:29
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch from 2636ed3 to e76d2d1 Compare April 28, 2026 14:55
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch from 397e46b to 4c6787e Compare April 28, 2026 14:55
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch from e76d2d1 to 764d40b Compare April 28, 2026 19:47
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch 2 times, most recently from 20bde0b to 8a4d3f2 Compare April 28, 2026 20:03
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch 2 times, most recently from 19cea36 to 17b1eaa Compare April 28, 2026 21:15
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch 2 times, most recently from 954bbdc to 9611d30 Compare April 28, 2026 21:52
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch 2 times, most recently from 6a16f8b to 8a4262e Compare April 28, 2026 22:33
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch from 9611d30 to 26f4173 Compare April 28, 2026 22:33
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/11 branch from 8a4262e to b986020 Compare April 28, 2026 23:04
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch from 26f4173 to 2975ba4 Compare April 28, 2026 23:04
Base automatically changed from shreyan-gupta/network/deentangle/11 to master April 28, 2026 23:33
…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)
@shreyan-gupta shreyan-gupta force-pushed the shreyan-gupta/network/deentangle/12 branch from 2975ba4 to 4adf4a4 Compare April 28, 2026 23:39
@shreyan-gupta shreyan-gupta enabled auto-merge April 28, 2026 23:46
@shreyan-gupta shreyan-gupta added this pull request to the merge queue Apr 28, 2026
Merged via the queue into master with commit 378d9e4 Apr 29, 2026
32 checks passed
@shreyan-gupta shreyan-gupta deleted the shreyan-gupta/network/deentangle/12 branch April 29, 2026 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants