Overview
This page outlines potential improvements and features that could enhance the P2P file sharing system. These are not currently implemented but represent opportunities for future development.Piece Selection Strategies
Rarest-First Strategy
Current Implementation: The system currently requests pieces sequentially without prioritization. Proposed Enhancement: Implement a “rarest-first” piece selection algorithm where peers prioritize downloading pieces that are least common in the swarm. Benefits:- Better piece distribution across the network
- Reduces risk of a piece becoming unavailable if seeders leave
- Improves overall swarm health
- Faster distribution in scenarios with limited seeders
Random First for Initial Pieces
Prioritize random piece selection when a leecher first starts to increase piece diversity quickly.Bandwidth Management
Choking and Unchoking
Current Implementation: All connected peers can request and receive pieces without limits. Proposed Enhancement: Implement choking mechanisms to manage upload bandwidth and incentivize fair sharing. Features:- Limit simultaneous uploads to N peers (e.g., 4 peers)
- Rotate unchoked peers every 10-30 seconds
- Implement “optimistic unchoking” to discover faster peers
- Prefer peers with better upload/download ratios
- Prevents bandwidth saturation
- Encourages reciprocal sharing (tit-for-tat)
- Improves performance for all peers
- Reduces network congestion
BitTorrent-style Choking
Traditional BitTorrent uses choking to create a “tit-for-tat” economy where peers that upload to you are prioritized for downloads.
Connection Resilience
Timeout Mechanisms
Current Limitation: No explicit timeouts for piece requests. If a peer stops responding, the piece remains inpendingPieces until connection closes.
Proposed Enhancement:
- Set timeout for piece requests (e.g., 30 seconds)
- Re-request timed-out pieces from other peers
- Track peer response times and prioritize faster peers
Automatic Reconnection
Proposed Enhancement:- Automatically attempt to reconnect to known peers if connection drops
- Implement exponential backoff for reconnection attempts
- Maintain a list of previously successful peer addresses
- Persist peer list to disk for recovery after crashes
Security Enhancements
Encryption and TLS Support
Current Limitation: All communication is unencrypted plain TCP with JSON messages. Proposed Enhancement: Implement TLS encryption for peer connections. Implementation:- Protects file data from eavesdropping
- Prevents man-in-the-middle attacks
- Ensures data integrity during transmission
Peer Authentication
Proposed Enhancement:- Implement peer identity verification
- Use public key cryptography for authentication
- Prevent unauthorized peers from joining swarms
- Optional: require shared secret for private swarms
Data Integrity
Per-Piece Hash Verification
Current Implementation: Only the complete file hash is verified after full download. Proposed Enhancement: Calculate and verify hash for each individual piece upon receipt. Benefits:- Early detection of corrupted pieces
- Re-request corrupted pieces immediately
- No need to re-download entire file if corruption detected
- Better resilience to network errors
Merkle Tree Verification
For very large files, implement Merkle tree-based verification for efficient partial verification.Performance Optimizations
Large File Handling
Current Implementation: Works well for files up to several GB, but could be optimized for very large files (100+ GB). Proposed Enhancements:-
Dynamic Piece Size:
- Adjust piece size based on file size
- Larger files → larger pieces (e.g., 256 KiB or 1 MiB)
- Reduces overhead from excessive piece count
-
Piece Pipelining:
- Request multiple pieces from same peer simultaneously
- Reduce round-trip latency impact
- Currently only one piece per peer at a time
-
Streaming Support:
- Enable playback/use of file while still downloading
- Prioritize pieces in sequential order when requested
- Useful for video streaming scenarios
Memory Optimization
Proposed Enhancements:- Stream piece data instead of loading entire pieces into memory
- Implement piece caching with LRU eviction
- Use memory-mapped files for large file operations
Protocol Enhancements
Extended Messaging
Current Protocol Messages:handshake,bitfield,request,piece,have,peers
-
cancel- Cancel a pending piece request -
port- Advertise DHT port (future DHT support) -
keepalive- Maintain connection during idle periods -
metadata- Exchange additional file metadata
Compression
Proposed Enhancement: Compress piece data before transmission for text/compressible files.Advanced Features
Distributed Hash Table (DHT)
Current Limitation: Requires at least one known peer to bootstrap. No peer discovery mechanism. Proposed Enhancement: Implement a DHT for decentralized peer discovery. Benefits:- No need for initial peer addresses
- Peers can find each other automatically
- Improved network resilience
- Truly distributed architecture
Tracker Support
Proposed Enhancement: Optional tracker support for centralized peer discovery (alongside PEX). Use Cases:- Easier coordination in managed networks
- Faster initial peer discovery
- Statistics and monitoring capabilities
Magnet Links
Proposed Enhancement: Support for magnet URI scheme for easy file sharing.Web Interface
Provide a web-based UI for monitoring and controlling the P2P node. Features:- Real-time download progress
- Connected peers visualization
- Bandwidth usage graphs
- File management
Monitoring and Diagnostics
Enhanced Logging
Proposed Enhancements:- Structured logging with log levels (DEBUG, INFO, WARN, ERROR)
- Log rotation and archival
- Export logs in JSON format for analysis
- Performance metrics logging
Statistics Dashboard
Proposed Metrics:- Total bytes uploaded/downloaded
- Per-peer transfer rates
- Piece availability distribution
- Network topology visualization
- Average piece download time
- Connection uptime statistics
Multi-File Support
Current Limitation: One swarm per file. Multi-file torrents not supported. Proposed Enhancement: Support sharing directories or multiple files as a single “torrent”. Technical Approach:- Create manifest file listing all files and their offsets
- Map pieces across multiple files
- Allow selective file downloading