Last updated: 2026-03-23
Path: comm/
P2P networking with Kademlia DHT discovery, custom TLS transport, and connection management.
P2P Layer (PacketHandler)
|
RP Layer (Remote Protocol - connection lifecycle)
|
Transport Layer (TLS/gRPC - encrypted channels)
|
Discovery Layer (Kademlia DHT - peer finding)
pub struct PeerNode {
pub id: NodeIdentifier, // 32-byte hex-encoded node ID
pub endpoint: Endpoint, // host, tcp_port, udp_port
}
// URI format: rnode://hexid@host?protocol=8080&discovery=8081KademliaNodeDiscovery:
- Identify sparsest distance buckets
- Create target keys that differ at specific bit positions
- Query random peers for nodes near targets (Lookup RPC)
- Filter and add discovered peers
PeerTable -- k-bucket routing table:
- 256 buckets (XOR distance metric), k=20, alpha=3
- LRU with stale peer replacement via ping
gRPC Kademlia service (port 40404):
SendPing/SendLookupRPCs- Network ID validation prevents cross-network discovery
Custom verification: HostnameTrustManager extracts P256 public key from peer certificate, derives F1R3FLY address (Keccak256), and matches against advertised identity. Prevents MITM without a CA.
TransportLayer trait:
send(peer, msg),broadcast(peers, msg),stream(peer, blob),disconnect(peer)- Helpers:
send_with_retry(),send_to_bootstrap()
pub struct ConnectionsCell {
pub peers: Arc<Mutex<Connections>>,
}add_conns(),remove_conns(),refresh_conn(),random(max)
Protocol messages: Heartbeat, ProtocolHandshake, ProtocolHandshakeResponse, Disconnect, Packet
Connection lifecycle:
- Local sends
ProtocolHandshakewith network_id - Peer validates network_id, responds with
ProtocolHandshakeResponse - Connection added to
ConnectionsCell - Periodic
Heartbeatfor liveness - Explicit
Disconnecton teardown
Chunker splits large messages:
- Compress if content > 500KB
- Split into fragments (max_message_size - 2KB buffer)
- Header chunk with metadata + data chunks
- Circuit breaker pattern for error detection
assure_port_forwarding(ports) -- Discovers UPnP gateways, maps TCP ports with "F1r3fly" description. Falls back to AWS/WhatIsMyIP for external IP.
Sources: f1r3fly.comm.rp.connect, f1r3fly.comm.rp.handle, f1r3fly.comm.discovery.kademlia, f1r3fly.comm.rp.transport
Counters: connect, disconnect, ping, lookup, send. Histograms: connect-time, ping-time, lookup-time, send-time.
19 test files in tests/: transport specs (stream_handler, grpc_transport, transport_layer, uri_parse), discovery specs (distance, kademlia_rpc, kademlia, peer_table), RP specs (find_and_connect, clear_connections, connect, connections), who_am_i_spec.rs. Chunker includes inline unit tests.
See also: comm/ crate README