Skip to content

Latest commit

 

History

History
531 lines (415 loc) · 14.1 KB

File metadata and controls

531 lines (415 loc) · 14.1 KB

Architecture: True Peer-to-Peer Exchange

🎯 What Makes This a TRUE P2P System?

This is a genuine peer-to-peer distributed exchange. Here's why:

✅ Core P2P Characteristics

  1. No Central Servers Required

    • Each node runs independently
    • Direct TCP connections between peers
    • No single point of failure
  2. No Central Authority

    • No node has special privileges
    • All nodes are equal
    • Consensus through distributed orderbooks
  3. Distributed Discovery

    • Multiple discovery mechanisms
    • Nodes find each other organically
    • No DNS or centralized registry required
  4. Data Replication

    • Each node maintains full orderbook
    • Orders and trades propagate peer-to-peer
    • No central database

🏗️ System Architecture

Three-Layer Architecture

┌─────────────────────────────────────────────────────────┐
│                  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    │  │
│  └──────────────┘  └──────────────┘  └──────────────┘  │
└─────────────────────────────────────────────────────────┘

🔌 P2P Network Layer (Core)

DirectConnectionService

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

PeerManager

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

PeerDiscovery

Purpose: Find new peers without central servers

Discovery Strategies:

  1. mDNS (Multicast DNS)

    • Zero-configuration local network discovery
    • Nodes announce themselves via multicast
    • Other nodes on same LAN discover automatically
    • No servers needed
  2. 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
  3. Peer Exchange

    • Peers share their peer lists
    • Exponential network growth
    • Similar to BitTorrent's PEX
    • Completely decentralized
  4. 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

📨 Message Routing

MessageRouter

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

🔍 Why Bootstrap Nodes Aren't "Centralization"

Common Misconception

"If you need bootstrap nodes, it's not true P2P!"

Reality

Bootstrap nodes are NOT central servers. Here's why:

  1. 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
  2. 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
  3. Anyone Can Be a Bootstrap Node

    • No special software required
    • Any running node can be bootstrap
    • Community-operated
    • Multiple redundant options
  4. Precedent in P2P Systems

    • Bitcoin uses DNS seeds (similar concept)
    • BitTorrent uses tracker URLs
    • Ethereum uses bootnodes
    • All considered "true P2P"

How to Join Network Without Bootstrap

# 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 PEX

🚀 Operating Modes

1. Pure P2P Mode (DEFAULT)

Configuration:

# No environment variables needed!
# Just run:
npm start

Features:

  • 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

2. P2P + DHT Mode (OPTIONAL)

Configuration:

EMBEDDED_GRAPE=true \
GRAPE_DHT_PORT=20001 \
GRAPE_API_PORT=30001 \
GRAPE_BOOTSTRAP_NODES=peer1.com:20001 \
npm start

Additional 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

3. Legacy Grenache Mode (NOT RECOMMENDED)

Why NOT recommended:

  • Requires external Grape servers
  • Those servers are centralized
  • Defeats purpose of P2P

Only use for:

  • Legacy compatibility
  • Testing
  • Migration period

🔒 Security Model

Decentralized Security

  1. No Single Point of Failure

    • Network survives node failures
    • No "server" to attack
    • Distributed data storage
  2. Peer Reputation

    • Track peer reliability
    • Automatic bad peer detection
    • Community-based trust
  3. Message Verification

    • Vector clocks for ordering
    • Message signatures (planned)
    • Byzantine fault tolerance (planned)

📊 Comparison with Other Systems

Feature This System Centralized Exchange "Fake P2P"
Central Server ❌ None ✅ Required ⚠️ "Coordinator"
Direct Connections ✅ Yes ❌ Client→Server ⚠️ Through relay
Distributed Data ✅ Full replication ❌ Central DB ⚠️ Cached
Discovery ✅ Decentralized ❌ DNS/IP ⚠️ Central registry
Single Point of Failure ❌ No ✅ Yes ⚠️ Coordinator
Bootstrap Needed ⚠️ First time only ✅ Always ⚠️ Always
TRUE P2P ✅ YES ❌ NO ❌ NO

🌐 Network Topology

Mesh Network

Each node connects to multiple peers:

        Node A
       /  |  \
      /   |   \
   Node B-|-Node C
      \   |   /
       \  |  /
        Node D

Properties:

  • No hierarchy
  • No "master" nodes
  • Fault tolerant
  • Self-healing

Network Growth

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

🎓 Educational: P2P vs Federated vs Centralized

Centralized Architecture

       Server
      /  |  \
   Client Client Client
  • Single point of failure ❌
  • Easy to censor ❌
  • Scales vertically ⚠️
  • Simple to build ✅

Federated Architecture

Server1 ←→ Server2 ←→ Server3
   ↓           ↓          ↓
Clients    Clients    Clients
  • Multiple servers ⚠️
  • Harder to censor ⚠️
  • Servers still control data ❌
  • Example: Email (SMTP)

Peer-to-Peer Architecture (THIS SYSTEM)

Peer1 ←→ Peer2 ←→ Peer3
  ↕         ↕         ↕
Peer4 ←→ Peer5 ←→ Peer6
  • No servers needed ✅
  • Censorship resistant ✅
  • Highly fault tolerant ✅
  • Self-organizing ✅
  • Examples: BitTorrent, Bitcoin

🔬 Technical Deep Dive

Message Propagation

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

Consensus Mechanism

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

Fault Tolerance

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

🚫 What This System Does NOT Require

  • ❌ 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)

✅ What This System DOES Require

  • ✅ 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

📈 Scalability

Horizontal Scaling

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

Network Limits

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 ✅

🎯 Conclusion

Is This TRUE P2P?

YES

Evidence:

  1. Direct TCP connections between peers
  2. No central servers required
  3. Decentralized peer discovery
  4. Distributed data storage
  5. Self-organizing network
  6. No single point of failure
  7. No central authority

Bootstrap Nodes Don't Disqualify P2P

Just like:

  • Bitcoin has DNS seeds
  • BitTorrent has tracker URLs
  • Ethereum has bootnodes

Bootstrap nodes are entry points, not controllers.

Core Philosophy

"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!