This is a genuine peer-to-peer distributed exchange. Here's why:
-
No Central Servers Required
- Each node runs independently
- Direct TCP connections between peers
- No single point of failure
-
No Central Authority
- No node has special privileges
- All nodes are equal
- Consensus through distributed orderbooks
-
Distributed Discovery
- Multiple discovery mechanisms
- Nodes find each other organically
- No DNS or centralized registry required
-
Data Replication
- Each node maintains full orderbook
- Orders and trades propagate peer-to-peer
- No central database
┌─────────────────────────────────────────────────────────┐
│ APPLICATION LAYER │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ OrderBook │ │ Trade Engine │ │ Matching │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────┘
▲
│
┌─────────────────────────────────────────────────────────┐
│ ROUTING LAYER │
│ ┌──────────────────────────────────────────────────┐ │
│ │ Message Router │ │
│ │ - Deduplication │ │
│ │ - Broadcast coordination │ │
│ │ - Retry/Queue management │ │
│ └──────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────┘
▲
│
┌─────────────────────────────────────────────────────────┐
│ NETWORK LAYER (P2P) │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │Direct TCP │ │Peer Manager │ │ Peer │ │
│ │Connections │ │ │ │ Discovery │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────┘
Purpose: Manage direct TCP connections between peers
Key Features:
- Each node runs a TCP server (default port 3000)
- Accepts inbound connections from other nodes
- Initiates outbound connections to discovered peers
- Implements peer protocol (handshake, heartbeat, messaging)
Why it's P2P:
- ✅ No intermediary servers
- ✅ Direct socket-to-socket communication
- ✅ Each node is both client and server
Purpose: Manage peer lifecycle and health
Key Features:
- Tracks all known peers
- Monitors peer health (heartbeats)
- Automatic reconnection to lost peers
- Peer reputation system
- Persistent peer storage (remembers peers across restarts)
Why it's P2P:
- ✅ Decentralized peer tracking
- ✅ No central peer registry
- ✅ Peers remember each other autonomously
Purpose: Find new peers without central servers
Discovery Strategies:
-
mDNS (Multicast DNS)
- Zero-configuration local network discovery
- Nodes announce themselves via multicast
- Other nodes on same LAN discover automatically
- No servers needed
-
Bootstrap Peers
- Well-known node addresses (like Bitcoin)
- Only needed for initial network join
- NOT central servers (just entry points)
- Can be ANY node in the network
-
Peer Exchange
- Peers share their peer lists
- Exponential network growth
- Similar to BitTorrent's PEX
- Completely decentralized
-
Persistent Peers
- Nodes remember previous connections
- Automatic reconnection on restart
- No discovery needed after first run
Why it's P2P:
- ✅ Multiple redundant discovery methods
- ✅ No single discovery server required
- ✅ Self-organizing network
Purpose: Intelligent message delivery with fallback
Message Flow:
Order Placed
│
▼
┌─────────────────┐
│ Try Direct TCP │ ← PRIMARY (Default)
└─────────────────┘
│ Success ✓
▼
Message Delivered
│ Failed ✗
▼
┌─────────────────┐
│ Queue for Retry │
└─────────────────┘
Features:
- Message deduplication (prevent loops)
- Broadcast coordination
- Automatic retry with backoff
- Queue management
Why it's P2P:
- ✅ Direct connections prioritized
- ✅ No message broker required
- ✅ Peer-to-peer message propagation
"If you need bootstrap nodes, it's not true P2P!"
Bootstrap nodes are NOT central servers. Here's why:
-
Entry Points, Not Controllers
- Help new nodes find the network
- Provide initial peer list
- Don't process transactions
- Don't store data
- Can't control the network
-
Temporary Need
- Only used once (first connection)
- Nodes remember peers in
.peers.json - After first run, bootstrap not needed
- Can work without bootstrap via mDNS
-
Anyone Can Be a Bootstrap Node
- No special software required
- Any running node can be bootstrap
- Community-operated
- Multiple redundant options
-
Precedent in P2P Systems
- Bitcoin uses DNS seeds (similar concept)
- BitTorrent uses tracker URLs
- Ethereum uses bootnodes
- All considered "true P2P"
# Option 1: mDNS (local network)
# Just start the node, peers discover automatically
npm start
# Option 2: Manual peer connection
# Connect to any single peer
BOOTSTRAP_PEERS=friend.example.com:3000 npm start
# Option 3: Peer exchange
# Connect to one peer, discover rest via PEXConfiguration:
# No environment variables needed!
# Just run:
npm startFeatures:
- Direct TCP peer connections
- mDNS discovery (LAN)
- Well-known bootstrap nodes
- Peer exchange
- Persistent peers
Architecture:
Node A ←──TCP──→ Node B
↕ ↕
TCP TCP
↕ ↕
Node C ←──TCP──→ Node D
Is this TRUE P2P? ✅ YES
- No central servers
- Direct peer connections
- Decentralized discovery
- Self-organizing network
Configuration:
EMBEDDED_GRAPE=true \
GRAPE_DHT_PORT=20001 \
GRAPE_API_PORT=30001 \
GRAPE_BOOTSTRAP_NODES=peer1.com:20001 \
npm startAdditional Features:
- Kademlia DHT (like BitTorrent)
- Distributed hash table
- Faster peer discovery
- Better for large networks
Architecture:
DHT Network (Kademlia)
┌───────┬───────┬───────┐
│ │ │ │
Node A Node B Node C Node D
└───────┴───────┴───────┘
P2P Connections
Is this TRUE P2P? ✅ YES
- Each node runs own DHT server
- No central DHT server
- Distributed hash table
- Still peer-to-peer
Why NOT recommended:
- Requires external Grape servers
- Those servers are centralized
- Defeats purpose of P2P
Only use for:
- Legacy compatibility
- Testing
- Migration period
-
No Single Point of Failure
- Network survives node failures
- No "server" to attack
- Distributed data storage
-
Peer Reputation
- Track peer reliability
- Automatic bad peer detection
- Community-based trust
-
Message Verification
- Vector clocks for ordering
- Message signatures (planned)
- Byzantine fault tolerance (planned)
| Feature | This System | Centralized Exchange | "Fake P2P" |
|---|---|---|---|
| Central Server | ❌ None | ✅ Required | |
| Direct Connections | ✅ Yes | ❌ Client→Server | |
| Distributed Data | ✅ Full replication | ❌ Central DB | |
| Discovery | ✅ Decentralized | ❌ DNS/IP | |
| Single Point of Failure | ❌ No | ✅ Yes | |
| Bootstrap Needed | ✅ Always | ||
| TRUE P2P | ✅ YES | ❌ NO | ❌ NO |
Each node connects to multiple peers:
Node A
/ | \
/ | \
Node B-|-Node C
\ | /
\ | /
Node D
Properties:
- No hierarchy
- No "master" nodes
- Fault tolerant
- Self-healing
Phase 1: Bootstrap
New Node → Bootstrap Node → Peer List
Phase 2: Peer Exchange
New Node → Known Peer → More Peers → Even More Peers
Phase 3: Stable Network
New Node → mDNS → Persistent Peers → Full Mesh
Server
/ | \
Client Client Client
- Single point of failure ❌
- Easy to censor ❌
- Scales vertically
⚠️ - Simple to build ✅
Server1 ←→ Server2 ←→ Server3
↓ ↓ ↓
Clients Clients Clients
- Multiple servers
⚠️ - Harder to censor
⚠️ - Servers still control data ❌
- Example: Email (SMTP)
Peer1 ←→ Peer2 ←→ Peer3
↕ ↕ ↕
Peer4 ←→ Peer5 ←→ Peer6
- No servers needed ✅
- Censorship resistant ✅
- Highly fault tolerant ✅
- Self-organizing ✅
- Examples: BitTorrent, Bitcoin
Gossip Protocol:
1. Node A places order
2. Node A → Broadcast to connected peers
3. Peers receive → Deduplicate → Rebroadcast
4. Exponential propagation
5. All nodes receive order
Deduplication:
- Message hash cache
- Time-based expiry
- Prevents infinite loops
- Efficient bandwidth usage
Vector Clocks:
- Each event has logical timestamp
- Partial ordering of events
- Detect concurrent operations
- Resolve conflicts deterministically
Example:
Node A: [A:1, B:0, C:0] - Order 1
Node B: [A:1, B:1, C:0] - Order 2
Node C: [A:1, B:1, C:1] - Order 3
Ordering: Order1 → Order2 → Order3
Circuit Breaker Pattern:
- Detect failing peers
- Prevent cascade failures
- Automatic recovery
- Graceful degradation
Heartbeat Monitoring:
- Periodic peer health checks
- Automatic reconnection
- Dead peer removal
- Network self-healing
- ❌ Central database server
- ❌ Message queue server (RabbitMQ, Kafka, etc.)
- ❌ Load balancer
- ❌ Reverse proxy
- ❌ API gateway
- ❌ Service discovery server
- ❌ Configuration server
- ❌ Centralized logging server
- ❌ Central authentication server
- ❌ DNS server (except for bootstrap lookup)
- ✅ Network connectivity (internet/LAN)
- ✅ Open P2P port (default 3000)
- ✅ Node software running
⚠️ At least ONE bootstrap peer (first run only)- OR local network with mDNS
- OR manual peer connection
Add more peers = More capacity
Small Network (3 nodes):
Each handles 1000 orders/sec
Total: 3000 orders/sec
Large Network (100 nodes):
Each handles 1000 orders/sec
Total: 100,000 orders/sec
Practical Limits:
- Max peers per node: ~50-100 (configurable)
- Max network size: Thousands of nodes
- Message latency: O(log n) with smart routing
Not Limited By:
- Central server capacity ✅
- Database size ✅
- Single server bandwidth ✅
YES ✅
Evidence:
- Direct TCP connections between peers
- No central servers required
- Decentralized peer discovery
- Distributed data storage
- Self-organizing network
- No single point of failure
- No central authority
Just like:
- Bitcoin has DNS seeds
- BitTorrent has tracker URLs
- Ethereum has bootnodes
Bootstrap nodes are entry points, not controllers.
"A peer-to-peer system is one in which peers communicate directly with each other, without requiring a central coordinator."
This system fulfills that definition completely.
Still have questions? See FAQ.md or file an issue!