ipfrs-network
Production-ready P2P networking layer for IPFRS built on rust-libp2p.
Overview
ipfrs-network provides comprehensive networking capabilities for IPFRS with 48 modules covering all aspects of P2P networking:
- Core Networking: libp2p-based swarm with QUIC and TCP transports
- Peer Discovery: Kademlia DHT, mDNS, Bootstrap nodes, Semantic DHT
- Connection Management: Intelligent limits, priority-based pruning, rate limiting
- NAT Traversal: AutoNAT, DCUtR hole punching, Circuit Relay v2
- Pub/Sub: GossipSub with topic-based messaging and mesh optimization
- Advanced Routing: Geographic routing, quality prediction, intelligent peer selection
- Transport: Multipath QUIC, connection migration, QUIC monitoring
- Privacy: Tor integration with onion routing and hidden services
- Mobile/IoT: Battery optimization, adaptive polling, background mode, offline queue
- Monitoring: Comprehensive metrics, Prometheus export, health checks, diagnostics
- Reliability: Retry logic, circuit breakers, fallback strategies
- Performance: Auto-tuning, load testing, traffic analysis, benchmarking
- Policy: Fine-grained network control with connection/bandwidth/content policies
Key Features
Core Networking
- Multi-transport: QUIC (primary) with TCP fallback for maximum compatibility
- NAT Traversal: Three-layer approach (AutoNAT + DCUtR + Circuit Relay)
- Peer Discovery: Multiple mechanisms (DHT, mDNS, bootstrap, peer exchange)
- Connection Management: Intelligent limits, priority-based pruning, bandwidth tracking
Advanced DHT
- Content Routing: Provider record publishing and discovery with automatic refresh
- Peer Routing: Find closest peers, routing table management
- Semantic DHT: Vector-based content discovery using embeddings and LSH
- Query Optimization: Early termination, pipelining, quality scoring, caching
- Pluggable Providers: Custom DHT implementations via trait interface
Pub/Sub & Messaging
- GossipSub: Topic-based publish/subscribe with mesh optimization
- Message Deduplication: Efficient duplicate detection with TTL
- Peer Scoring: Quality-based peer selection for reliable delivery
- Content Announcements: Standard topics for network-wide broadcasts
Mobile & IoT Optimizations
- Adaptive Polling: Activity-based interval adjustment for power saving
- Bandwidth Throttling: Token bucket rate limiting with burst support
- Background Mode: Pause/resume for mobile app lifecycle
- Offline Queue: Request queuing with priority-based replay
- Memory Monitoring: Component-level tracking with budget enforcement
- Network Monitoring: Interface change detection and handling
- Connection Migration: Seamless QUIC migration on network switches
Advanced Routing & Selection
- Geographic Routing: Proximity-based peer selection using Haversine distance
- Quality Prediction: Historical performance tracking for intelligent routing
- Peer Selection: Multi-factor scoring combining geography and quality
- Multipath QUIC: Multiple path management with quality-based selection
Privacy & Security
- Tor Integration: Onion routing with circuit management
- Hidden Services: Host and connect to .onion addresses
- Stream Isolation: Maximum privacy with separate circuits
- Policy Engine: Fine-grained control over connections and content
Monitoring & Observability
- Comprehensive Metrics: Connection, DHT, bandwidth, latency tracking
- Prometheus Export: Ready-to-use metrics for monitoring systems
- Health Checks: Component-level and overall health assessment
- Diagnostics: Automated troubleshooting and issue detection
- Traffic Analysis: Pattern detection and anomaly identification
- Time-Series Aggregation: Historical metrics with statistical analysis
Performance & Reliability
- Auto-Tuning: Automatic configuration based on system resources
- Load Testing: Comprehensive stress testing utilities
- Benchmarking: Performance measurement and regression tracking
- Circuit Breakers: Prevent cascading failures
- Retry Logic: Exponential backoff with configurable limits
- Fallback Strategies: Alternative peers, relay fallback, degraded mode
Testing & Simulation
- Network Simulation: Test under adverse conditions (latency, packet loss, partitions)
- Load Testing: Stress test with connection storms, query floods, bandwidth saturation
- Chaos Testing: Verify resilience under extreme conditions
- IPFS Compatibility: Full compatibility testing with Kubo
Quick Start
Basic Usage
use ;
async
High-Level Facade
For easy integration of all features, use the NetworkFacade:
use NetworkFacadeBuilder;
async
Configuration Presets
Use pre-configured presets for common scenarios:
use NetworkPreset;
// Low-memory devices (< 128 MB RAM)
let preset = low_memory;
// IoT devices (128-512 MB RAM)
let preset = iot;
// Mobile devices
let preset = mobile;
// High-performance servers
let preset = high_performance;
// Privacy-focused with Tor
let preset = privacy;
// Development/testing
let preset = development;
Architecture
The crate consists of 48 modules organized into logical groups:
Core Components
- node: Core NetworkNode implementation with libp2p swarm
- dht: Kademlia DHT operations and caching
- peer: Peer store for tracking known peers
- connection_manager: Connection limits and pruning
- bootstrap: Bootstrap peer management
Advanced Features
- facade: High-level API for easy integration
- semantic_dht: Vector-based semantic routing
- gossipsub: Pub/sub messaging
- multipath_quic: Multipath transport support
- tor: Privacy-preserving networking
Mobile & IoT
- adaptive_polling: Power-saving polling strategies
- throttle: Bandwidth throttling
- background_mode: Mobile app lifecycle integration
- offline_queue: Offline request queuing
- memory_monitor: Memory usage tracking
Routing & Selection
- geo_routing: Geographic proximity routing
- quality_predictor: Performance-based routing
- peer_selector: Multi-factor peer selection
- dht_provider: Pluggable DHT backends
Monitoring & Analysis
- metrics: Comprehensive metrics tracking
- metrics_aggregator: Time-series aggregation
- health: Health monitoring
- diagnostics: Troubleshooting utilities
- traffic_analyzer: Traffic pattern analysis
Performance & Testing
- auto_tuner: Automatic configuration tuning
- benchmarking: Performance benchmarks
- load_tester: Load and stress testing
- network_simulator: Network condition simulation
Reliability & Policy
- fallback: Fallback strategies
- rate_limiter: Connection rate limiting
- reputation: Peer reputation system
- policy: Network policy engine
- session: Session lifecycle management
Examples
The examples/ directory contains 38 comprehensive examples:
Basic Examples
basic_node.rs- Creating and starting a network nodedht_operations.rs- Content announcement and discoveryconnection_management.rs- Connection tracking and bandwidth
Advanced Features
network_facade_demo.rs- Using the high-level facadesemantic_search.rs- Vector-based content discoverypubsub_messaging.rs- Pub/sub messaging with GossipSubmultipath_quic.rs- Multipath QUIC demonstrationtor_privacy.rs- Privacy-preserving networking
Mobile & IoT
low_memory_node.rs- Low-memory configurationbackground_mode.rs- Mobile lifecycle integrationnetwork_monitoring.rs- Network interface monitoringoffline_queue_demo.rs- Offline request handling
Performance & Monitoring
auto_tuning.rs- Automatic configurationperformance_benchmarking.rs- Benchmarking utilitiesload_testing.rs- Load and stress testingmetrics_prometheus.rs- Metrics exporthealth_monitoring.rs- Health checking
Advanced Routing
geographic_routing.rs- Geographic optimizationquality_prediction.rs- Quality-based routingintelligent_peer_selection.rs- Multi-factor selection
Testing & Analysis
network_simulation.rs- Network condition simulationtraffic_analysis.rs- Traffic pattern analysisnetwork_diagnostics.rs- Troubleshooting
Testing
The crate includes:
- 507+ unit tests covering all modules
- 35 doc tests validating documentation examples
- Integration tests with IPFS Kubo compatibility
- Chaos tests for resilience verification
- Zero warnings - strict code quality enforcement
Run tests:
Performance
Performance targets (achieved on typical hardware):
- Connection establishment: < 100ms (local), < 500ms (remote)
- DHT lookup: < 2s (20 hops)
- Concurrent connections: 1000+ peers
- Memory per peer: < 10KB
- Zero-copy operations: Where possible
- ARM optimization: Full ARMv7/AArch64 support
Dependencies
libp2p- P2P networking frameworkquinn- QUIC implementationtokio- Async runtimecid- Content addressingmultihash- Hash functionsprometheus- Metrics exportdashmap- Concurrent data structuresparking_lot- Fast synchronization
Documentation
Comprehensive documentation is available:
- Module docs: See
cargo doc --open - Architecture guide:
docs/ARCHITECTURE.md - Peer discovery guide:
docs/PEER_DISCOVERY.md - NAT traversal guide:
docs/NAT_TRAVERSAL.md - Configuration reference:
docs/CONFIGURATION.md
License
See LICENSE file in the root of the repository.
Contributing
This is part of the IPFRS project. See the main repository for contribution guidelines.