Skip to content

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:

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

TypeDescription
P2PNodeMain libp2p swarm node
P2PHandleHandle for sending commands to a running node
NodeConfigNode configuration (listen addresses, bootstrap peers)
KademliaDhtKademlia DHT wrapper with content routing
ConnectionPoolConnection pooling with health tracking
CircuitBreakerPer-peer circuit breaker state machine
RouterMulti-hop message router with pluggable strategies
PeerReputationPeer scoring with auto-blacklisting
NetworkMessageTyped network message with priority
NetworkMetricsLatency and throughput metrics
P2pErrorCrate error type

Feature Flags

FeatureDefaultDescription
fullyesEnables all protocols (gossipsub + kademlia + mdns + relay)
gossipsubnoGossipSub pub/sub only
kademlianoKademlia DHT only
mdnsnomDNS local discovery only
relaynoRelay and DCUtR only

Released under the MIT License.