It focuses on the
p2p.Server architecture, peer lifecycle management, connection types, and protocol multiplexing.
For peer discovery mechanisms, see Discovery Mechanisms.For blockchain synchronization protocols, see Chain Synchronization.
For protocol handler implementations, see Protocol Handlers.
Server Architecture
The P2P layer is centered around thep2p.Server structure, which manages all peer connections and implements the core networking functionality of a StableNet node.It unifies inbound and outbound connections and acts as an intermediary between discovery protocols and protocol handlers.
Server Structure
TheServer structure maintains the following core components:
Server Configuration and Initialization
Server initialization proceeds through the following steps:setupLocalNode()
Generates the local node ID and ENR from the private key and initializes the node database.setupListening()
Starts the TCP listener on the configured network address.setupDiscovery()
Initializes Discovery v4/v5 and configures the discovery mixer.setupDialScheduler()
Creates the dial scheduler for static and discovered nodes.run()
Starts the server’s main event loop for peer event handling.listenLoop()
Continuously accepts inbound connections.
Server Main Loop
The server’s main event loop performs the following tasks:- Handles peer addition and removal events
- Processes connection requests from the dial scheduler
- Gracefully shuts down all peers on termination signals
- Broadcasts peer state changes via the event feed
Peer Lifecycle
Each connected peer is represented by aPeer structure that encapsulates connection state, protocol handlers, and lifecycle metadata.
Connection Establishment Flow
Peer connections follow this sequence:- TCP connection establishment
- Encrypted handshake
- Protocol handshake and capability negotiation
- Protocol multiplexing setup
- Peer registration and event emission
Handshake Protocols
Connection setup consists of two handshake phases:- Encryption Handshake (
doEncHandshake)
Establishes an RLPx-encrypted communication channel. - Protocol Handshake (
doProtoHandshake)
Negotiates supported protocols and versions.
protoHandshake structure includes:
Protocol Multiplexing
After the handshake, the server matches commonly supported protocols and assigns message code ranges to each protocol.- Base P2P messages: codes 0–15
- First matched subprotocol: starting at code 16
- Subsequent protocols: offset increased by the previous protocol’s length
Connection Types and Management
The P2P server usesconnFlag values to distinguish connection types:
Peer Limits and Capacity
Default peer capacity control policies include:MaxPeers: total peer count limitMaxPendingPeers: limit on pending connection attempts (default 50 per direction)DialRatio: outbound peer ratio (default 1/3)
Dial Scheduler
ThedialScheduler manages outbound connection attempts:
- Tracks recent dial history to avoid excessive retries
- Prioritizes static nodes over discovered nodes
- Enforces maximum concurrent dial limits
- Uses exponential backoff to throttle retries for failed endpoints
Peer Operations and Lifecycle
Adding and Removing Peers
The server manages its peer set using the following methods:Disconnect Reasons
Disconnect reasons are recorded usingDiscReason codes:
Peer Event Feed
The server emits peer state changes viaPeerEvent notifications:
Integration with the Ethereum Backend
The Ethereum backend integrates with the P2P server through protocol registration. Integration flow:eth.Ethereum.Protocols()– returns protocol definitionseth.MakeProtocols()– createsethsubprotocol handlerssnap.MakeProtocols()– createssnapprotocol handlers (if enabled)node.Stack.RegisterProtocols()– registers protocols with the P2P serverhandler.Start()– begins protocol message processing
Backend Access to the P2P Server
The backend maintains a reference to the P2P server for RPC services, used for:NetAPI– network information queriesAdminAPI– peer management RPCs- Dynamic ENR updates
Configuration Flags
Key flags used to configure the P2P network include:
These configuration values are applied during node initialization via
SetP2PConfig() and setNodeKey().
