midnight-p2p-networking

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

P2P Networking

P2P 网络

The peer-to-peer (P2P) layer connects Midnight nodes and enables data exchange across the decentralized network. Midnight uses the default Polkadot SDK Rust libp2p implementation.
点对点(P2P)层连接Midnight节点,实现去中心化网络中的数据交换。Midnight采用默认的Polkadot SDK Rust libp2p实现。

Stack overview

技术栈概览

mermaid
flowchart TB
  subgraph Discovery["Peer discovery"]
    BS["Bootstrap nodes"]
    MDNS["mDNS (LAN)"]
    KDHT["Kademlia DHT random walk"]
  end

  subgraph Transport["Connection"]
    TCP["TCP/IP"]
    WS["WebSockets"]
    DNS["DNS resolution"]
  end

  subgraph Security["Upgrade"]
    Noise["Noise Protocol — encryption"]
    Yamux["Yamux — multiplexing"]
    MS["Multistream-Select negotiation"]
  end

  subgraph Protocols["Substream protocols"]
    Ping["Ping — liveness"]
    RR["Request-Response — block/tx queries"]
    Notif["Notifications — gossip, finality, sync"]
  end

  Discovery --> Transport
  Transport --> Security
  Security --> Protocols

mermaid
flowchart TB
  subgraph Discovery["Peer discovery"]
    BS["Bootstrap nodes"]
    MDNS["mDNS (LAN)"]
    KDHT["Kademlia DHT random walk"]
  end

  subgraph Transport["Connection"]
    TCP["TCP/IP"]
    WS["WebSockets"]
    DNS["DNS resolution"]
  end

  subgraph Security["Upgrade"]
    Noise["Noise Protocol — encryption"]
    Yamux["Yamux — multiplexing"]
    MS["Multistream-Select negotiation"]
  end

  subgraph Protocols["Substream protocols"]
    Ping["Ping — liveness"]
    RR["Request-Response — block/tx queries"]
    Notif["Notifications — gossip, finality, sync"]
  end

  Discovery --> Transport
  Transport --> Security
  Security --> Protocols

Discovery mechanisms

节点发现机制

To join the network, a node must discover and connect to peers.
要加入网络,节点必须发现并连接到其他节点

Bootstrap nodes

Bootstrap节点

  • Predefined trusted peers in network configuration (hard-coded identities + addresses).
  • On startup, the node connects to bootstrap peers to join the network and learn about additional peers.
  • 网络配置中预定义的可信节点(硬编码身份+地址)。
  • 启动时,节点连接到Bootstrap节点以加入网络,并了解更多节点信息。

mDNS (multicast DNS)

mDNS(多播DNS)

  • Discovers peers on the same LAN via UDP broadcast.
  • Useful for development, testnets, or co-located deployments.
  • Enable/disable via network settings.
  • 通过UDP广播发现**同一局域网(LAN)**内的节点。
  • 适用于开发测试网或同址部署场景。
  • 可通过网络设置启用/禁用。

Kademlia DHT (random walk)

Kademlia DHT(随机游走)

  • Once connected to at least one peer, the node performs a Kademlia-based random walk on each configured chain's DHT.
  • Sends
    FIND_NODE
    queries to discover more peers.
  • Builds a resilient, expanding view of the network over time.
mermaid
flowchart LR
  Start["Node starts"] --> Boot["Connect to bootstrap"]
  Boot --> Learn["Learn peer addresses"]
  Learn --> Walk["Kademlia random walk"]
  Walk --> More["More peers discovered"]
  More --> Walk
  MDNS["mDNS on LAN"] -.-> Learn

  • 连接到至少一个节点后,节点会在每条配置链的DHT上执行基于Kademlia的随机游走
  • 发送
    FIND_NODE
    查询以发现更多节点。
  • 随时间推移构建一个弹性扩展的网络视图。
mermaid
flowchart LR
  Start["Node starts"] --> Boot["Connect to bootstrap"]
  Boot --> Learn["Learn peer addresses"]
  Learn --> Walk["Kademlia random walk"]
  Walk --> More["More peers discovered"]
  More --> Walk
  MDNS["mDNS on LAN"] -.-> Learn

Connection establishment

连接建立

After discovery, peers:
  1. Establish direct connections.
  2. Negotiate shared protocol capabilities (transport, security, multiplexing).
This foundation supports higher-level sync and gossip protocols.

发现节点后,节点会:
  1. 建立直接连接
  2. 协商共享协议能力(传输层、安全层、多路复用)。
这一基础支持更高层级的同步gossip协议。

Transport layer

传输层

When node A (Alice) connects to node B (Bob):
TransportDescription
TCP/IPTraditional IPv4/IPv6 sockets
WebSocketsTCP + WebSocket framing (browser/proxy friendly)
DNSDomain names resolved to IPs during connection

当节点A(Alice)连接到节点B(Bob)时:
传输协议描述
TCP/IP传统IPv4/IPv6套接字
WebSocketsTCP + WebSocket帧格式(适配浏览器/代理)
DNS连接过程中将域名解析为IP地址

Encryption and multiplexing

加密与多路复用

After the base transport connects, peers upgrade via negotiated protocols:
ProtocolPurpose
NoiseEnd-to-end confidentiality and integrity
YamuxMultiple logical streams over one physical connection
Multistream-SelectChoose compatible encryption and multiplexing options
mermaid
sequenceDiagram
  participant A as Alice
  participant B as Bob

  A->>B: TCP / WebSocket connect
  A->>B: Multistream-Select (Noise)
  Note over A,B: Encrypted channel
  A->>B: Multistream-Select (Yamux)
  Note over A,B: Multiplexed substreams
  A->>B: Open Ping / Request-Response / Notifications

基础传输连接建立后,节点会通过协商协议进行升级:
协议用途
Noise端到端机密性与完整性保障
Yamux在一条物理连接上承载多个逻辑流
Multistream-Select选择兼容的加密与多路复用方案
mermaid
sequenceDiagram
  participant A as Alice
  participant B as Bob

  A->>B: TCP / WebSocket connect
  A->>B: Multistream-Select (Noise)
  Note over A,B: Encrypted channel
  A->>B: Multistream-Select (Yamux)
  Note over A,B: Multiplexed substreams
  A->>B: Open Ping / Request-Response / Notifications

Substream protocols

子流协议

Each substream runs a dedicated application-level protocol:
ProtocolPurpose
PingLiveness checks and latency measurement
Request-ResponseStructured exchange (block queries, transaction queries)
NotificationsBroadcast: new transactions, block announcements, finality updates, light-client state sync

每个子流运行一个专用的应用层协议:
协议用途
Ping存活状态检查与延迟测量
Request-Response结构化数据交换(区块查询、交易查询)
Notifications广播:新交易、区块公告、最终性更新、轻客户端状态同步

Peer identification

节点标识

  • Each node has a unique Ed25519 public key for P2P identity.
  • This keypair is separate from consensus message signing keys.
  • Exchanged during handshake; used to authenticate peers for the session.
See
midnight-cryptography/
for Ed25519 usage in libp2p vs GRANDPA.

  • 每个节点拥有唯一的Ed25519公钥作为P2P身份标识。
  • 该密钥对与共识消息签名密钥相互独立
  • 在握手阶段交换;用于会话中的节点身份验证。
查看
midnight-cryptography/
了解libp2p与GRANDPA中Ed25519的使用方式。

Related skills

相关技能

  • midnight-consensus/
    — what validators gossip (blocks, finality)
  • midnight-cryptography/
    — Noise, Ed25519 peer identity
  • midnight-rpc/
    — external client access (complements P2P sync)
  • midnight-consensus/
    —— 验证节点 gossip 的内容(区块、最终性信息)
  • midnight-cryptography/
    —— Noise加密、Ed25519节点身份
  • midnight-rpc/
    —— 外部客户端访问(补充P2P同步)