bitbit
BitTorrent Protocol Client & Trackerless Kademlia DHT
Highlights
- Downloaded a 1.24 GB Arch Linux ISO with 0 centralized tracker nodes
- SHA-1 piece integrity verification across 2,373 pieces
- Tit-for-tat unchoking with 10s recalibration and 30s optimistic unchoking
- 160-bit Kademlia DHT with XOR routing and iterative k-closest lookups
System Overview
bitbit is a BitTorrent client and peer wire implementation written in Go from scratch. It features bencode parsing, SHA-1 piece integrity verification, rarest-first scheduling, tit-for-tat choking, and a trackerless Kademlia DHT overlay.
The Problem & Architectural Rationale
Centralized file distribution suffers from single-origin bandwidth exhaustion and single points of failure. Peer swarming solves this but requires distributed piece scheduling, cryptographic piece verification, and game-theoretic incentives to enforce fair peer uploading.
Architecture & Components
1. Bencode Parser & Metainfo
Custom decoder and encoder for bencoded .torrent files, extracting infohashes, piece lengths, and SHA-1 chunk arrays.
2. Piece Selection Policies
Implements Strict Policy for fast piece completion, Rarest First to distribute scarce pieces before seeds leave, Random First bootstrap for new leechers, and Endgame broadcast mode for the final missing blocks.
3. Tit-for-Tat Choker
Calculates recent peer download rates every 10 seconds to unchoke the top 4 fastest peers, and executes an Optimistic Unchoke every 30 seconds to discover new peers.
4. Kademlia DHT Routing
Maintains routing k-buckets sorted by XOR distance over a 160-bit keyspace, executing recursive FIND_NODE / FIND_VALUE RPC lookups for decentralized swarm discovery.
Core Implementation
Rarest-first piece scheduling algorithm
Outcomes & Results
- Validated end-to-end swarm download of a 1.24 GB Linux ISO with zero active tracker servers.
- Verified SHA-1 hash integrity across 2,373 individual pieces.