You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
P2P Decentralized Exchange (DEX) - Complete System Documentation
π― System Overview
The P2P Decentralized Exchange is a sophisticated distributed trading system built on Grenache DHT network that enables peer-to-peer order matching without a central authority. The system implements a hybrid consensus mechanism to ensure all peers maintain synchronized order books while handling high-frequency trading operations.
Key Features
Fully Decentralized: No central authority or single point of failure
Consensus-Driven: Advanced hybrid consensus algorithm for state synchronization
Scalable: Supports 1-5 configurable peers with dynamic discovery
High Performance: Optimized order matching with sub-second consensus completion
Fault Tolerant: Robust error handling and network partition recovery
Real-time Synchronization: Live order book updates across all peers
ποΈ Architecture Components
graph TB
subgraph "P2P DEX System Architecture"
subgraph "Infrastructure Layer"
DHT1[Grenache DHT Server 1 Port: 30001]
DHT2[Grenache DHT Server 2 Port: 40001]
end
subgraph "Peer Network"
PA[Peer A RPC + Pub/Sub]
PB[Peer B RPC + Pub/Sub]
PC[Peer C RPC + Pub/Sub]
PD[Peer D RPC + Pub/Sub]
PE[Peer E RPC + Pub/Sub]
end
subgraph "Order Generation"
REQ[Order Requester Dynamic Peer Discovery]
end
subgraph "System Orchestration"
STARTER[Starter Service Management]
end
end
DHT1 <--> DHT2
PA <--> DHT1
PB <--> DHT1
PC <--> DHT1
PD <--> DHT1
PE <--> DHT1
REQ --> PA
REQ --> PB
REQ --> PC
REQ --> PD
REQ --> PE
STARTER --> DHT1
STARTER --> DHT2
STARTER --> PA
STARTER --> PB
STARTER --> PC
STARTER --> PD
STARTER --> PE
STARTER --> REQ
graph TB
subgraph "Peer Internal Architecture"
subgraph "Communication Layer"
RPC[RPC Server Order Reception]
PUB[Publisher Order Broadcasting]
SUB[Subscriber Order Reception]
end
subgraph "Processing Layer"
OQ[Order Queue Sequential Processing]
BQ[Broadcast Queue Timed Broadcasting]
end
subgraph "Business Logic"
OB[Order Book Matching Engine]
OS[Orderbook State Fingerprinting]
end
subgraph "Consensus Layer"
CM[Consensus Manager State Synchronization]
end
subgraph "Infrastructure"
LOG[Logger File + Console]
end
end
RPC --> OQ
SUB --> OQ
OQ --> OB
OB --> BQ
BQ --> PUB
OB --> OS
OS --> CM
CM --> BQ
LOG -.-> OQ
LOG -.-> OB
LOG -.-> CM
LOG -.-> BQ
Loading
Key Methods and Responsibilities:
PeerFactory Class:
constructor(peerId): Initialize peer with unique ID and channels
start(): Launch all peer services and establish network connections
setupRPCServer(): Configure incoming order handling
setupPubSub(): Initialize order broadcasting and subscription
handleIncomingOrder(order): Process orders from external sources
processOrderInQueue(order): Sequential order processing with duplicate detection
broadcastOrder(order): Distribute orders to peer network
checkConsensusNeeded(): Trigger consensus at order checkpoints (every 3 orders)
sequenceDiagram
participant P1 as Peer A
participant P2 as Peer B
participant P3 as Peer C
participant P4 as Peer D
participant P5 as Peer E
Note over P1,P5: Consensus Trigger (Every 3 Orders)
P1->>P1: Create Snapshot
P1->>P2: CONSENSUS_REQUEST_STATE
P1->>P3: CONSENSUS_REQUEST_STATE
P1->>P4: CONSENSUS_REQUEST_STATE
P1->>P5: CONSENSUS_REQUEST_STATE
P2->>P1: CONSENSUS_STATE_RESPONSE
P3->>P1: CONSENSUS_STATE_RESPONSE
P4->>P1: CONSENSUS_STATE_RESPONSE
P5->>P1: CONSENSUS_STATE_RESPONSE
P1->>P1: Analyze States
P1->>P1: Determine Target State
Note over P1,P5: Consensus Resolution
P1->>P2: Sync to Target State
P1->>P3: Sync to Target State
P1->>P4: Sync to Target State
P1->>P5: Sync to Target State
Note over P1,P5: All Peers Synchronized
Loading
Consensus Algorithm:
Majority Rule: If >50% peers have identical states β use majority state
Plurality Rule: If no majority β use most common state among available peers
Deterministic Fallback: If all states unique β use lexicographically smallest hash
Timeout Handling: 10-second timeout with partial peer completion
Key Methods:
startConsensus(): Initiate consensus round with state snapshot
handleConsensusMessage(message): Process incoming consensus messages
resolveConsensus(): Apply consensus algorithm to determine target state
isActive(): Check if consensus is currently running
4. Order Queue (src/Components/orderQueue.js)
graph TB
subgraph "Order Queue Processing"
ENQUEUE[Enqueue Order]
ENQUEUE --> SORT[Sort by Timestamp then Order Number]
SORT --> PAUSED{Queue Paused for Consensus?}
PAUSED -->|Yes| WAIT[Wait for Resume]
PAUSED -->|No| PROCESSING{Currently Processing?}
PROCESSING -->|Yes| QUEUE[Queue Order]
PROCESSING -->|No| IMMEDIATE[Process Immediately]
WAIT --> RESUME[Resume Signal]
RESUME --> QUEUE
QUEUE --> PROCESS[Process Next Order]
IMMEDIATE --> PROCESS
PROCESS --> EXECUTE[Execute Process Function]
EXECUTE --> NEXT{More Orders in Queue?}
NEXT -->|Yes| PROCESS
NEXT -->|No| IDLE[Queue Idle]
end