Docs Técnicas
atlas-p2p
atlas-p2p is the networking crate for validator-to-validator communication in the Rust workspace. It wraps libp2p, defines the Atlas network protocol payloads, manages peer sets, and exposes an adapter plus publisher interface that the node runtime uses to publish proposals, votes, heartbeats, and state-sync requests.
Summary
atlas-p2p is the networking crate for validator-to-validator communication in the Rust workspace. It wraps libp2p, defines the Atlas network protocol payloads, manages peer sets, and exposes an adapter plus publisher interface that the node runtime uses to publish proposals, votes, heartbeats, and state-sync requests.
This crate is the transport layer, not the consensus engine itself. Consensus and runtime code depend on it to move messages and track peers, while keeping the higher-level protocol decisions elsewhere.
Why This Crate Exists
- encapsulate libp2p setup and swarm behavior
- define Atlas-specific request/response and gossip message structures
- provide a transport adapter that turns swarm events into runtime-facing events
- manage peer discovery, active/reserve peer sets, and address knowledge
- expose a narrow publisher interface for the node runtime and proposal service
Current Role In The Workspace
atlas-p2p currently owns five related concerns:
- libp2p swarm construction and behavior wiring
- Atlas-specific network protocol messages and bundles
- adapter event translation between libp2p and runtime code
- peer management and peer scoring
- test and fallback network abstractions such as the in-memory network
It is the main transport dependency of the runtime. atlas-node builds a Libp2pAdapter, and atlas-consensus relies heavily on its peer manager and related abstractions.
Public Surface
Top-Level Modules
adapterbehaviourbuildercodecconfigerroreventsin_memorykey_managermessagepeer_managerportsprotocoltelemetrytraitsutils
Main Public Reexports
Libp2pAdapterP2pConfigPeerManager
Important Types
P2pConfigLibp2pAdapterAdapterCmdAdapterEventP2pBehaviourPeerManagerPeerCommandPeerEventTxRequestTxBundleCheckpointStateAdapterHandle
Important Traits
ports::P2pPublishertraits::Network
Key Modules
- `lib.rs`: public export surface
- `config.rs`: network configuration model
- `builder.rs`: swarm construction, transport setup, gossipsub config, and bootstrap dialing
- `behaviour.rs`: composed libp2p behavior and core topic subscription
- `adapter.rs`: runtime-facing libp2p adapter and main event loop
- `events.rs`: composed libp2p event enum and runtime-facing adapter events
- `protocol.rs`: request/response protocol for tx fetches, state sync, checkpoints, and pending-by-sender queries
- `ports.rs`:
P2pPublishertrait andAdapterHandleimplementation - `peer_manager.rs`: peer registration, scoring, active/reserve rotation, and peer stats updates
- `message.rs`: higher-level cluster message wrapper for proposal and vote traffic
- `in_memory.rs`: test-oriented in-memory network implementation with dedup behavior
- `telemetry.rs`: transport-level metrics
Inputs And Outputs
Inputs
- P2P configuration such as listen addresses, bootstrap peers, and keypair path
- runtime publish commands for topics and request/response messages
- swarm events from libp2p
- peer information and node stats updates
Outputs
- gossiped proposal, vote, heartbeat, and tx traffic
- request/response exchanges for state sync and pending transaction retrieval
- runtime-facing
AdapterEventvalues - updated peer manager state and address knowledge
- transport telemetry counters
Internal Dependencies
Workspace Dependencies
atlas-common
This crate sits directly on top of the shared types layer and below consensus/runtime logic.
External Dependencies That Shape The Design
libp2pfor transport, discovery, gossip, request/response, identify, ping, and Kademliatokiofor async orchestrationserdeandbincodefor protocol serializationsha2andhexfor dedup-style message IDs and hashing helpersmetricsfor network instrumentation
Used By
These packages depend directly on atlas-p2p today:
atlas-consensusatlas-node
Observed usage in the workspace includes:
- `runtime/builder.rs`: node runtime creates
Libp2pAdapter, command channels, andAdapterHandle - `rpc/server.rs`: proposal gRPC service is generic over
P2pPublisher - `block_producer.rs`: block producer publishes over
P2pPublisher - `maestro/events.rs`: node runtime reacts to
AdapterEventvalues and updates peer stats - `sync_driver/request.rs`: sync driver uses
P2pPublisherto request state and pending transactions - `cluster/core.rs`: consensus runtime owns a shared
PeerManager - `consensus/engine.rs`: consensus engine uses peer data and address conversion helpers
Protocol Model
The network behavior assembled in `behaviour.rs` combines:
identifyping- optional
mdns - optional
kademlia gossipsub- request/response
The current core gossip topics are:
atlas/heartbeat/v1atlas/proposal/v1atlas/vote/v1atlas/tx/v1
The request/response protocol in `protocol.rs` supports:
- transaction fetches by tx ID
- state sync requests by height and last hash
- checkpoint-assisted state bundles
- pending transaction retrieval by sender and nonce range
Runtime Integration Model
The main runtime-facing seam is the Libp2pAdapter in `adapter.rs`.
Its role is to:
- run the swarm event loop
- translate low-level libp2p events into
AdapterEvent - accept
AdapterCmdcommands from higher layers - perform heartbeat and maintenance ticks
- keep address knowledge and peer state in sync
AdapterHandle then exposes a smaller P2pPublisher interface to the rest of the runtime so those layers do not need to manipulate the swarm directly.
Peer Management Model
PeerManager in `peer_manager.rs` keeps:
- known peers
- active peers
- reserve peers
- max sizes for active and reserve sets
It also includes:
- score comparison based on reliability and latency
- demotion and reserve placement on disconnect
- one-step rotation between reserve and active peers
- stats updates tied to last-seen freshness
This is one of the main ways consensus and runtime logic reason about peer quality without directly touching libp2p internals.
Testing
The crate relies on module-local tests rather than a separate integration test suite.
Coverage includes:
- configuration cloning and option preservation
- address parsing and swarm builder helper behavior
- stable gossipsub content-based message IDs
- topic subscription set shape
- adapter command wrapping in
AdapterHandle - peer manager registration, rotation, and scoring behavior
- protocol serialization roundtrips
- in-memory network delivery and dedup behavior
- error rendering and telemetry labels
Risks Or Design Tension
Mixed Abstraction Layers
This crate exposes both:
- a real libp2p transport adapter used by runtime code
- a higher-level
Networktrait with an in-memory implementation used for tests and abstraction
That is useful, but it means the crate carries both production transport code and a parallel simulated-network seam.
Broad Surface Area
atlas-p2p bundles transport setup, peer management, protocol messages, adapter orchestration, and testing utilities in one place. The crate boundary is still coherent, but it is fairly wide.
PeerManager Is Widely Shared
PeerManager is used beyond the adapter itself, especially by consensus code. That makes it a de facto shared policy object, not just a transport helper.
Feature-Gated Discovery
mDNS is behind a crate feature and configuration flag, while Kademlia is runtime-toggle-based. That flexibility is useful, but it means discovery behavior depends on both build-time and runtime switches.
Open Questions
- should the
Networktrait andInMemoryNetworkremain here long term, or move to a more explicitly test-oriented crate - should peer scoring and rotation policy stay transport-local, or migrate toward consensus/runtime ownership
- should the Atlas protocol message layer split more cleanly from libp2p adapter code if the transport surface grows
Relevant Files
- `Cargo.toml`
- `lib.rs`
- `config.rs`
- `builder.rs`
- `behaviour.rs`
- `adapter.rs`
- `events.rs`
- `protocol.rs`
- `ports.rs`
- `peer_manager.rs`
- `message.rs`
- `in_memory.rs`
- `telemetry.rs`
- `runtime/builder.rs`
- `rpc/server.rs`
- `cluster/core.rs`