Skip to content

refactor(p2p): split message processing from libp2p networking#23754

Open
PhilWindle wants to merge 1 commit into
merge-train/spartanfrom
pw/p2p-worker-refactor
Open

refactor(p2p): split message processing from libp2p networking#23754
PhilWindle wants to merge 1 commit into
merge-train/spartanfrom
pw/p2p-worker-refactor

Conversation

@PhilWindle
Copy link
Copy Markdown
Collaborator

What

Splits the P2P stack into two pieces along the existing P2PService boundary, with no behaviour change:

  • LibP2PService — the networking layer (libp2p node, gossipsub, reqresp transport, peer manager, discovery, message dedup, report-to-pubsub).
  • P2PMessageProcessor (new) — received-message handling that depends on main-thread node state: the message validators, the six consensus callbacks (+ their register* methods), validate/store/process for txs, block proposals, checkpoint proposals and attestations, the tx-validation pipeline, validateRequestedBlockTxsConsistency, and the reqresp data handlers (status/tx/block-txs). It calls back into the network through a narrow P2PNetwork interface (propagate + penalizePeer).

The processor is built in the factory (the composition root) and injected into both the libp2p service and the P2PClient. Consensus callbacks now register directly on the processor via P2PClient, not on the networking service — so registration no longer routes through LibP2PService/P2PService. The register* methods were removed from the P2PService interface, LibP2PService, and DummyP2PService.

Why

This is the first step toward running the libp2p stack on a worker thread so its synchronous CPU (gossipsub, snappy, signature verification, peer scoring) stops blocking the main event loop. The split lands the boundary exactly where the worker edge will be: message-handling and callback registration stay on the main thread (the processor, owned/registered by P2PClient), while LibP2PService becomes the networking layer that will move into the worker and hold only an injected reference it calls into. No worker thread is introduced in this PR.

Verification

  • p2p package type-checks with no new errors; yarn format and yarn lint clean.
  • Unit tests green: libp2p_service (48), p2p_client (20), and the broader p2p services + client suite (~552), including the block-txs integration test.

Extract received-message handling (validation, persistence, consensus callbacks, and the reqresp data handlers) out of LibP2PService into a new P2PMessageProcessor. The processor is built in the factory (the composition root) and injected into both the libp2p service and the P2PClient; consensus callbacks now register directly on the processor rather than on the networking service. LibP2PService is reduced to the networking layer.

No behaviour change. This draws the boundary along the already-mostly-async P2PService interface in preparation for moving the libp2p stack onto a worker thread.
@PhilWindle PhilWindle added the ci-full Run all master checks. label May 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-full Run all master checks.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant