ipfrs-network 0.2.0

Peer-to-peer networking layer with libp2p and QUIC for IPFRS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# IPFRS Network Architecture

This document describes the architecture and design of the IPFRS network layer.

## Overview

IPFRS Network is a comprehensive peer-to-peer networking layer built on libp2p, providing content-addressed storage with semantic routing capabilities. It combines traditional DHT-based content routing with vector-based semantic discovery.

## High-Level Architecture

```
┌─────────────────────────────────────────────────────────────────┐
│                         NetworkNode                              │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐          │
│  │   libp2p     │  │    DHT       │  │  Semantic    │          │
│  │    Swarm     │  │   Manager    │  │     DHT      │          │
│  └──────────────┘  └──────────────┘  └──────────────┘          │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐          │
│  │  GossipSub   │  │  Connection  │  │   Bootstrap  │          │
│  │   Manager    │  │   Manager    │  │   Manager    │          │
│  └──────────────┘  └──────────────┘  └──────────────┘          │
└─────────────────────────────────────────────────────────────────┘
           │                    │                    │
           ▼                    ▼                    ▼
┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐
│   Transport     │  │   Discovery     │  │   Protocols     │
│                 │  │                 │  │                 │
│  • QUIC         │  │  • Kademlia     │  │  • Identify     │
│  • TCP          │  │  • mDNS         │  │  • Ping         │
│  • WebSocket    │  │  • Bootstrap    │  │  • Bitswap      │
│                 │  │  • AutoNAT      │  │  • Custom       │
└─────────────────┘  └─────────────────┘  └─────────────────┘
```

## Core Components

### 1. NetworkNode

The `NetworkNode` is the main entry point for network operations. It manages:

- **libp2p Swarm**: Core event loop and connection management
- **Protocol Behaviors**: Kademlia, mDNS, Ping, Identify, AutoNAT, DCUtR, Relay
- **Event Processing**: Asynchronous event handling with tokio
- **Lifecycle Management**: Start, stop, and graceful shutdown

**Key Methods:**
- `new(config)` - Create a new network node
- `start()` - Begin listening and processing events
- `connect(peer_id, addrs)` - Connect to a peer
- `provide(cid)` - Announce content to DHT
- `find_providers(cid)` - Discover content providers

### 2. DHT Manager

The `DhtManager` provides advanced Kademlia DHT operations:

**Features:**
- Provider record management with automatic refresh
- Query result caching with TTL
- Routing table health monitoring
- Peer discovery and routing

**Architecture:**
```
DhtManager
├── Provider Records (DashMap<Cid, ProviderInfo>)
├── Query Cache (DashMap<QueryKey, CachedResult>)
├── Peer Cache (DashMap<PeerId, PeerCacheEntry>)
└── Background Tasks (Tokio spawn)
    ├── Provider refresh (every 12h)
    └── Cache cleanup (every 5min)
```

### 3. Semantic DHT

The `SemanticDht` extends traditional DHT with vector-based routing:

**Design:**
```
SemanticDht
├── Namespaces (text, image, audio, custom)
├── LSH Projections (random vectors per namespace)
├── Local Index (Cid → Embedding mapping)
├── Query Cache (TTL-based)
└── Hash→Peer Mapping (for distributed routing)
```

**LSH Process:**
1. Input: High-dimensional embedding (e.g., 768-dim)
2. Project onto random hyperplanes (8 functions × 4 tables)
3. Quantize projections into hash buckets
4. Map hash to DHT key (CID)
5. Query DHT for peers in similar buckets

**Distance Metrics:**
- **Euclidean**: L2 distance, good for spatial data
- **Cosine**: Angular distance, good for text embeddings
- **Manhattan**: L1 distance, robust to outliers
- **Dot Product**: Direct similarity, for normalized vectors

### 4. GossipSub Manager

The `GossipSubManager` implements topic-based pub/sub:

**Mesh Formation:**
```
Topic Mesh
├── Target peers (D = 6)
├── Low watermark (D_low = 4)
├── High watermark (D_high = 12)
└── Gossip peers (D_lazy = 3)
```

**Message Flow:**
1. Publisher sends to mesh peers (D peers)
2. Mesh peers forward to their mesh
3. Non-mesh peers receive gossip (IHAVE messages)
4. Peers with missing messages request (IWANT)
5. Duplicate detection via seen cache

**Peer Scoring:**
```
Score = avg(topic_scores) × (1 - invalid_ratio)

Where:
- topic_scores: Per-topic behavior scores
- invalid_ratio: Invalid messages / Total messages
```

### 5. Connection Manager

Manages connection limits and quality:

**Connection Limits:**
- Max total connections (default: 100)
- Max inbound connections (default: 60)
- Max outbound connections (default: 50)
- Reserved slots for important peers

**Pruning Strategy:**
1. Calculate connection value: `1.0 / (latency_ms + 1.0)`
2. Sort connections by value (lowest first)
3. Prune lowest-value connections
4. Preserve reserved and recent connections

### 6. Bootstrap Manager

Handles network bootstrapping with resilience:

**Features:**
- Exponential backoff retry (100ms → 6.4s)
- Circuit breaker pattern (5 consecutive failures → open)
- Per-peer connection tracking
- Configurable bootstrap peer list

**Bootstrap Flow:**
```
1. Load bootstrap peers from config
2. For each peer:
   a. Check circuit breaker state
   b. If closed: attempt connection
   c. On success: reset backoff
   d. On failure: increase backoff, increment failure count
3. If connected peers < threshold: retry after backoff
```

## Transport Layer

### QUIC Transport

Primary transport for modern networking:

**Advantages:**
- Built-in encryption (TLS 1.3)
- Connection migration (mobile/WiFi switching)
- Multiplexing without head-of-line blocking
- 0-RTT connection resumption
- UDP-based (NAT-friendly)

**Configuration:**
```rust
QuicConfig::new(&keypair)
```

### TCP Transport

Fallback for environments without UDP:

**Stack:**
```
TCP → Noise (encryption) → Yamux (multiplexing)
```

**Configuration:**
```rust
let tcp = TcpTransport::new(config)
    .upgrade(noise_authenticated)
    .multiplex(yamux_config)
```

## NAT Traversal

### Three-Layer Approach

1. **AutoNAT**: Detect NAT type and external address
2. **DCUtR** (Direct Connection Upgrade through Relay): Hole punching
3. **Circuit Relay v2**: Fallback relayed connection

**Flow:**
```
1. AutoNAT probe → Determine if behind NAT
2. If behind NAT:
   a. Try DCUtR hole punching
   b. If DCUtR fails → Use relay
3. If public → Direct connection
```

### NAT Types Handled

- **Full Cone**: Direct connection possible
- **Restricted Cone**: DCUtR usually succeeds
- **Port-Restricted**: DCUtR with coordination
- **Symmetric**: Relay required

## Discovery Mechanisms

### 1. Kademlia DHT

**Routing Table:**
- K-buckets (k=20) organized by XOR distance
- Replacement cache for full buckets
- Periodic refresh to maintain health

**Queries:**
- `FIND_NODE`: Discover peers near a key
- `GET_PROVIDERS`: Find content providers
- `ADD_PROVIDER`: Announce content

**Optimization:**
- Alpha (α=3) concurrent queries
- Query timeout: 60s
- Replication factor: 20

### 2. mDNS

Local network discovery without infrastructure:

**Configuration:**
- Query interval: 5s
- Service name: `_ipfrs._udp.local`
- Auto-connect on discovery

### 3. Bootstrap Nodes

Static peer list for initial network entry:

**Default Bootstrap Peers:**
- Configurable via `NetworkConfig::bootstrap_peers`
- Persistent storage of successful peers
- Automatic retry with backoff

## Protocol Registry

Extensible protocol system for custom protocols:

**Architecture:**
```
ProtocolRegistry
├── Handlers (HashMap<ProtocolId, Box<dyn ProtocolHandler>>)
├── Capabilities (HashMap<ProtocolId, ProtocolCapabilities>)
└── Lifecycle Management (init/shutdown hooks)
```

**Version Negotiation:**
```
Client: Supports [1.0.0, 1.1.0, 1.2.0]
Server: Supports [1.1.0, 1.2.0, 1.3.0]
Result: Negotiate 1.2.0 (highest common)
```

## Fallback Strategies

Multi-layer resilience approach:

### 1. Alternative Peers

When primary peer fails, try alternatives:
- Select from provider list
- Sort by latency/reputation
- Implement max retry limit

### 2. Relay Fallback

If direct connection fails:
- Use Circuit Relay v2
- Automatic relay selection
- Bandwidth accounting

### 3. Degraded Mode

When network is constrained:
- Reduce connection limits
- Disable non-essential protocols
- Increase query timeouts

### 4. Circuit Breaker

Prevent cascading failures:
- **Closed**: Normal operation
- **Open**: Fast-fail after threshold failures
- **Half-Open**: Test recovery after timeout

## Metrics and Monitoring

### Prometheus Metrics

**Connection Metrics:**
- `connections_established_total`
- `connections_failed_total`
- `connections_active`
- `connections_closed_total`

**DHT Metrics:**
- `dht_queries_total`
- `dht_successful_queries`
- `dht_failed_queries`
- `dht_providers_announced`
- `dht_providers_found`

**Bandwidth Metrics:**
- `bandwidth_bytes_sent`
- `bandwidth_bytes_received`

**Health Metrics:**
- `health_score` (0.0-1.0)
- `routing_table_peers`
- `connected_peers`

### Health Checking

**Component Health:**
- **Connections**: Are we connected to peers?
- **DHT**: Is routing table healthy?
- **Bandwidth**: Are we transmitting data?

**Overall Health:**
```
Healthy:     score >= 0.7
Degraded:    0.3 <= score < 0.7
Unhealthy:   score < 0.3
Unknown:     No data yet
```

## Event Processing

**Event Loop:**
```rust
loop {
    tokio::select! {
        event = swarm.next() => {
            // Process libp2p events
            match event {
                SwarmEvent::ConnectionEstablished { .. } => { ... }
                SwarmEvent::Behaviour(event) => { ... }
                ...
            }
        }
        _ = shutdown_rx.changed() => {
            // Graceful shutdown
            break;
        }
    }
}
```

**Event Types:**
- Connection events (established, closed, failed)
- DHT events (bootstrap, providers, routing)
- Protocol events (identify, ping, custom)
- NAT status changes

## Data Flow

### Content Announcement

```
1. Application calls provide(cid)
2. DhtManager computes DHT key
3. Announce to k closest peers (k=20)
4. Store provider record (TTL=24h)
5. Background task refreshes every 12h
```

### Content Discovery

```
1. Application calls find_providers(cid)
2. Check provider cache (TTL=5min)
3. If miss: Query DHT
4. Query k closest peers in parallel
5. Aggregate results (remove duplicates)
6. Cache results
7. Return provider list
```

### Semantic Search

```
1. Application submits SemanticQuery
2. Compute LSH hashes for embedding
3. Convert hashes to DHT keys
4. Query DHT for each hash
5. Collect candidate peers
6. Compute exact distances (optional)
7. Rank by similarity score
8. Return top-k results
```

## Performance Characteristics

### Latency

- **Local Connection**: <100ms
- **Remote Connection**: <500ms
- **DHT Lookup**: <2s (20 hops)
- **Semantic Query**: <3s (LSH + DHT)
- **Provider Refresh**: Background (non-blocking)

### Throughput

- **QUIC**: Up to 1 Gbps (hardware limited)
- **TCP**: Up to 800 Mbps (hardware limited)
- **DHT Queries**: ~100 concurrent
- **GossipSub Topics**: Unlimited
- **Semantic Namespaces**: Unlimited

### Scalability

- **Concurrent Connections**: 1000+ peers
- **Memory per Peer**: <10KB
- **DHT Routing Table**: ~1000 peers (auto-pruned)
- **Provider Cache**: 10,000 entries
- **Query Cache**: 10,000 entries

## Security Considerations

### Transport Security

- **QUIC**: TLS 1.3 (mandatory)
- **TCP**: Noise protocol (XX handshake)
- **Peer Authentication**: Ed25519 signatures

### Content Verification

- **CID**: Self-verifying content addresses
- **Multihash**: Cryptographic content hashing
- **Provider Verification**: Signature-based

### Denial of Service Protection

- **Connection Limits**: Per-peer and global
- **Rate Limiting**: Query and bandwidth limits
- **Peer Scoring**: Identify and prune bad actors
- **Circuit Breaker**: Fast-fail on repeated failures

### Privacy

- **NAT Traversal**: Minimal information disclosure
- **DHT Privacy**: No content stored in DHT (only CIDs)
- **Relay Privacy**: End-to-end encryption maintained

## Future Enhancements

### Planned Features

- **Geographic Routing**: Optimize by physical location
- **QUIC Multipath**: Aggregate multiple network paths
- **Tor Integration**: Privacy-preserving networking
- **Mobile Optimization**: Pause/resume, background mode
- **Custom DHT**: Pluggable routing algorithms

### Research Directions

- **ML-based Routing**: Predict best peers using learning
- **Hybrid Consensus**: Combine DHT with blockchain
- **Quantum-Resistant**: Post-quantum cryptography
- **Edge Computing**: Optimize for IoT and edge devices

## References

- [libp2p Specification](https://github.com/libp2p/specs)
- [Kademlia Paper](https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf)
- [GossipSub Specification](https://github.com/libp2p/specs/tree/master/pubsub/gossipsub)
- [QUIC Protocol](https://www.rfc-editor.org/rfc/rfc9000.html)
- [LSH for Approximate Nearest Neighbor](https://web.mit.edu/andoni/www/LSH/)