spt-p2p
P2P networking layer -- libp2p-based node with Kademlia DHT, GossipSub, mDNS, relay, and circuit breaker
Overview
spt-p2p is the peer-to-peer networking layer for the SPT platform, built on libp2p 0.54. It provides a composed swarm with GossipSub pub/sub, Kademlia DHT for content routing, mDNS for local peer discovery, relay/DCUtR for NAT traversal, and a circuit breaker state machine for resilient connections. CBOR-serialized request-response messaging and peer reputation scoring with auto-blacklisting round out the feature set.
Key Features
- libp2p swarm -- composed node with Noise encryption, Yamux multiplexing, TCP transport
- Kademlia DHT -- distributed hash table with bootstrap, content routing, and peer reputation
- GossipSub -- pub/sub messaging for topic-based broadcast
- mDNS -- zero-config local network peer discovery
- Circuit breaker -- state machine (Closed/Open/HalfOpen) for peer connection resilience
- Peer reputation -- scoring with configurable thresholds and auto-blacklisting
- Multi-hop routing -- pluggable routing strategies with path tracking
Dependencies
Key external dependencies:
libp2p0.54 -- core P2P networking (TCP, Noise, Yamux, GossipSub, Kademlia, mDNS, relay, DCUtR, AutoNAT)
Usage
rust
use spt_p2p::{P2PNode, NodeConfig, NetworkMessage, MessagePriority};
let config = NodeConfig::default();
let (node, handle) = P2PNode::new(config);
// node.start().await?;API Reference
Core Types
| Type | Description |
|---|---|
P2PNode | Main libp2p swarm node |
P2PHandle | Handle for sending commands to a running node |
NodeConfig | Node configuration (listen addresses, bootstrap peers) |
KademliaDht | Kademlia DHT wrapper with content routing |
ConnectionPool | Connection pooling with health tracking |
CircuitBreaker | Per-peer circuit breaker state machine |
Router | Multi-hop message router with pluggable strategies |
PeerReputation | Peer scoring with auto-blacklisting |
NetworkMessage | Typed network message with priority |
NetworkMetrics | Latency and throughput metrics |
P2pError | Crate error type |
Feature Flags
| Feature | Default | Description |
|---|---|---|
full | yes | Enables all protocols (gossipsub + kademlia + mdns + relay) |
gossipsub | no | GossipSub pub/sub only |
kademlia | no | Kademlia DHT only |
mdns | no | mDNS local discovery only |
relay | no | Relay and DCUtR only |