This document explains the Embedded Grape DHT architecture that makes this exchange a true P2P system. Each node now runs its own Grape DHT server, eliminating the need for external infrastructure.
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Node 1 │ │ Node 2 │ │ Node 3 │
│(Exchange)│ │(Exchange)│ │(Exchange)│
└────┬─────┘ └────┬─────┘ └────┬─────┘
│ │ │
└────────────────┼────────────────┘
│
┌────────────▼────────────┐
│ EXTERNAL GRAPE DHT │ ❌ Single Point of Failure
│ (Manual servers) │ ❌ External Infrastructure
│ grape --dp 20001 │ ❌ NOT True P2P
│ grape --dp 20002 │
└─────────────────────────┘
Problems:
- ❌ Required manual Grape server setup
- ❌ External infrastructure dependency
- ❌ Single point of failure
- ❌ NOT true P2P (centralized discovery)
┌─────────────────────┐ ┌─────────────────────┐ ┌─────────────────────┐
│ Node 1 │ │ Node 2 │ │ Node 3 │
│ ┌──────────────┐ │ │ ┌──────────────┐ │ │ ┌──────────────┐ │
│ │ Exchange │ │ │ │ Exchange │ │ │ │ Exchange │ │
│ └──────────────┘ │ │ └──────────────┘ │ │ └──────────────┘ │
│ ┌──────────────┐ │ │ ┌──────────────┐ │ │ ┌──────────────┐ │
│ │Embedded Grape│◄──┼──┼─►│Embedded Grape│◄──┼──┼─►│Embedded Grape│ │
│ │DHT (Kademlia)│ │ │ │DHT (Kademlia)│ │ │ │DHT (Kademlia)│ │
│ └──────────────┘ │ │ └──────────────┘ │ │ └──────────────┘ │
└─────────────────────┘ └─────────────────────┘ └─────────────────────┘
↑ ↑ ↑ ↑ ↑ ↑
│ └─────────────────────┘ └─────────────────────┘ │
└───────────────────────────────────────────────────┘
Distributed Kademlia DHT Network
Benefits:
- ✅ Each node runs its own Grape DHT server
- ✅ Zero external infrastructure needed
- ✅ No single point of failure
- ✅ TRUE P2P distributed system
- ✅ Kademlia DHT protocol (same as BitTorrent)
Each node runs its own Grape server, forming a distributed DHT network:
# Terminal 1 - Node 1 (Bootstrap node)
GRAPE_DHT_PORT=20001 \
GRAPE_API_PORT=30001 \
P2P_PORT=3001 \
npm start
# Terminal 2 - Node 2 (Connects to Node 1's DHT)
GRAPE_DHT_PORT=20002 \
GRAPE_API_PORT=30002 \
GRAPE_BOOTSTRAP_NODES=127.0.0.1:20001 \
P2P_PORT=3002 \
npm start
# Terminal 3 - Node 3 (Connects to existing DHT)
GRAPE_DHT_PORT=20003 \
GRAPE_API_PORT=30003 \
GRAPE_BOOTSTRAP_NODES=127.0.0.1:20001,127.0.0.1:20002 \
P2P_PORT=3003 \
npm startIf you want to use external Grape servers (old way):
# Start external Grape servers
grape --dp 20001 --aph 30001 --bn '127.0.0.1:20002'
grape --dp 20002 --aph 40001 --bn '127.0.0.1:20001'
# Start nodes with embedded Grape disabled
EMBEDDED_GRAPE=false \
GRAPE_URL=http://127.0.0.1:30001 \
npm start# Enable/disable embedded Grape (default: true)
EMBEDDED_GRAPE=true
# Grape DHT port (each node needs different port)
GRAPE_DHT_PORT=20001
# Grape HTTP API port (each node needs different port)
GRAPE_API_PORT=30001
# Bootstrap DHT nodes (comma-separated host:port)
GRAPE_BOOTSTRAP_NODES=127.0.0.1:20001,127.0.0.1:20002
# Grape bind host (default: 127.0.0.1)
GRAPE_HOST=127.0.0.1# Only used when EMBEDDED_GRAPE=false
GRAPE_URL=http://127.0.0.1:30001Each node needs unique ports when running on the same machine:
| Node | GRAPE_DHT_PORT | GRAPE_API_PORT | P2P_PORT |
|---|---|---|---|
| 1 | 20001 | 30001 | 3001 |
| 2 | 20002 | 30002 | 3002 |
| 3 | 20003 | 30003 | 3003 |
| N | 2000N | 3000N | 300N |
Node starts
↓
Start Embedded Grape DHT server
↓
Connect to bootstrap nodes (if any)
↓
Join Kademlia DHT network
↓
Wait for DHT to bootstrap (2s)
↓
Start Exchange Client
↓
Announce service in DHT
↓
Ready to trade!
Node 3 starts
↓
Connects to bootstrap nodes (Node 1, Node 2)
↓
Learns about other DHT participants via Kademlia
↓
Builds routing table of nearby nodes
↓
Can now discover ANY service in the DHT
↓
No central server needed!
The Kademlia DHT protocol ensures:
- Decentralized: No central coordinator
- Scalable: O(log n) routing complexity
- Resilient: Nodes can join/leave freely
- Self-healing: Automatically replaces failed nodes
| Feature | External Grape | Embedded Grape |
|---|---|---|
| Infrastructure | Manual servers | Automatic |
| Setup Complexity | High | Low |
| External Dependency | Yes ❌ | No ✅ |
| Single Point of Failure | Yes ❌ | No ✅ |
| True P2P | No ❌ | Yes ✅ |
| Production Ready | No ❌ | Yes ✅ |
| DHT Protocol | Kademlia | Kademlia |
| Scalability | Limited | Unlimited |
-
Decentralized Peer Discovery
- Each node runs its own DHT server
- Kademlia protocol (same as BitTorrent)
- No central registry
-
No External Infrastructure
- Zero manual setup required
- Self-contained nodes
- Autonomous operation
-
No Single Point of Failure
- DHT is distributed across all nodes
- Node failures don't affect network
- Self-healing network
-
Direct Peer Communication
- Nodes connect directly after discovery
- No proxy or intermediary
- True peer-to-peer connections
-
Independent Operation
- Each node fully autonomous
- No dependency on external services
- Can operate in isolation
GRAPE_DHT_PORT=20001 GRAPE_API_PORT=30001 P2P_PORT=3001 npm startExpected output:
🍇 Starting embedded Grape DHT server...
✅ Embedded Grape server started!
DHT Port: 20001
API Port: 30001
🍇 Grape Mode: Embedded (True P2P)
# Terminal 1
GRAPE_DHT_PORT=20001 GRAPE_API_PORT=30001 P2P_PORT=3001 npm start
# Terminal 2
GRAPE_DHT_PORT=20002 GRAPE_API_PORT=30002 GRAPE_BOOTSTRAP_NODES=127.0.0.1:20001 P2P_PORT=3002 npm start
# Terminal 3
GRAPE_DHT_PORT=20003 GRAPE_API_PORT=30003 GRAPE_BOOTSTRAP_NODES=127.0.0.1:20001 P2P_PORT=3003 npm startExpected: All nodes discover each other automatically via DHT.
- Start 3 nodes as above
- Kill Node 1 (bootstrap)
- Start Node 4 using Node 2 as bootstrap
- Node 4 should still discover Node 3
Expected: Network continues functioning without Node 1.
Error: EADDRINUSE: address already in use
Solution: Each node needs unique ports:
# Node 1
GRAPE_DHT_PORT=20001 GRAPE_API_PORT=30001 ...
# Node 2
GRAPE_DHT_PORT=20002 GRAPE_API_PORT=30002 ...Solution: Check bootstrap configuration:
# Node 2+ must specify bootstrap nodes
GRAPE_BOOTSTRAP_NODES=127.0.0.1:20001Solution: Wait longer or increase bootstrap time:
// In index.js, increase timeout
await new Promise(resolve => setTimeout(resolve, 5000)); // 5 secondsGrenache uses Kademlia, the same DHT protocol as:
- BitTorrent
- IPFS
- Ethereum node discovery
Key Properties:
- XOR distance metric: Efficient routing
- k-buckets: Distributed routing tables
- Parallel lookups: Fast peer discovery
- Redundancy: Multiple paths to data
| Aspect | Embedded | External |
|---|---|---|
| Deployment | One process | Two processes |
| Lifecycle | Automatic | Manual |
| Coupling | Tight | Loose |
| Overhead | Minimal | Higher |
| Reliability | Higher | Lower |
The Embedded Grape architecture transforms this exchange from a pseudo-P2P system (requiring external infrastructure) into a TRUE P2P system where:
- ✅ Each node is fully autonomous
- ✅ No external infrastructure required
- ✅ Distributed Kademlia DHT for peer discovery
- ✅ No single point of failure
- ✅ Production-ready and scalable
This is now a REAL P2P exchange! 🎉