agentleFS
Kali-DecoderKali-Decoder/Midnight-Skills/.agents/skills/midnight-p2p-networking/SKILL.md

midnight-p2p-networking

Skill18 starsChanged 18 days agoMITView on GitHub

Midnight P2P networking — Polkadot SDK libp2p, peer discovery (bootstrap, mDNS, Kademlia DHT), transport (TCP/WebSocket/DNS), Noise encryption, Yamux multiplexing, and substream protocols. Use when configuring nodes, debugging connectivity, or explaining how validators exchange blocks and gossip.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    When node A (Alice) connects to node B (Bob):

    line 92
  2. Distinctive:

    After the base transport connects, peers upgrade via negotiated protocols:

    line 104

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

P2P Networking

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.

Stack overview

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

  • 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.

mDNS (multicast DNS)

  • Discovers peers on the same LAN via UDP broadcast.
  • Useful for development, testnets, or co-located deployments.
  • Enable/disable via network settings.

Kademlia DHT (random walk)

  • 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.
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.


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

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
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

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.


Related skills

  • midnight-consensus/ — what validators gossip (blocks, finality)
  • midnight-cryptography/ — Noise, Ed25519 peer identity
  • midnight-rpc/ — external client access (complements P2P sync)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.