SubmitMarketable and MatchOrders derived their returned fills by re-slicing the shared trade log (ob.Trades[startOffset:]) with an offset captured BEFORE matching. ob.Trades self-truncates at 100k entries (keeps the last 50k), so a match that crosses that boundary mid-match trims the log and leaves the pre-match offset pointing past the end -> panic 'slice bounds out of range'. The log feeds settlement, so fills had to stay exact. tryMatchImmediateLocked / MatchOrders now capture each fill as it is produced, independent of ob.Trades, and return that slice. Decomplects 'this match's fills' (a value the caller needs) from 'the trade log' (a bounded-retention buffer): correct across the truncation boundary by construction, deterministic across validators. Adds orderbook_truncation_test.go: three regression cases that cross the boundary through the production match path (single submits, one sweeping submit, legacy MatchOrders) with exact-fill and value-conservation asserts. They panic on the old code, pass on the fixed code. No sleeps, no stubs. Also: 0x9999 D-matches/C-settles docs (LLM.md, README, ARCHITECTURE), venue-canonical naming, fourpath-live proof tool, drop dead snowman-api key from run-lux-testnet.sh.
15 KiB
LX Architecture
Overview
The LX is built on a highly scalable, modular architecture designed to achieve planet-scale performance with sub-microsecond latency. The system uses a multi-layer approach with specialized components for different aspects of trading.
System Architecture
┌──────────────────────────────────────────────────────────┐
│ Client Layer │
│ Web UI | Mobile | API Clients | Trading Bots | SDKs │
└────────────────────┬─────────────────────────────────────┘
│
┌────────────────────┴─────────────────────────────────────┐
│ Gateway Layer │
│ Load Balancer | Rate Limiter | Auth | WebSocket/gRPC │
└────────────────────┬─────────────────────────────────────┘
│
┌────────────────────┴─────────────────────────────────────┐
│ Application Layer │
│ Order Management | Risk Engine | Market Data | Admin │
└────────────────────┬─────────────────────────────────────┘
│
┌────────────────────┴─────────────────────────────────────┐
│ Core Engine Layer │
│ Matching Engine | Clearing | Settlement | Liquidation │
└────────────────────┬─────────────────────────────────────┘
│
┌────────────────────┴─────────────────────────────────────┐
│ Consensus Layer │
│ DAG Consensus | Validator Network | State Machine │
└────────────────────┬─────────────────────────────────────┘
│
┌────────────────────┴─────────────────────────────────────┐
│ Storage Layer │
│ Order DB | Trade History | State DB | Archive │
└──────────────────────────────────────────────────────────┘
Core Components
1. Matching Engine
The heart of the DEX, responsible for order matching with extreme performance.
Architecture
type MatchingEngine struct {
orderBooks map[string]*OrderBook // Per-symbol order books
lockFreePool *LockFreeOrderPool // Lock-free order allocation
tradeBuffer *CircularTradeBuffer // Zero-copy trade buffer
backend Backend // Go/C++/GPU backend
}
Key Features
- Lock-free data structures: Atomic operations for concurrency
- Memory pooling: Reuse allocations to minimize GC
- Multiple backends: Auto-select optimal implementation
- Price-time priority: Fair order matching algorithm
Performance Optimizations
- Integer price levels: Avoid floating-point operations
- B-tree for price levels: O(log n) operations
- Circular buffers: Zero-copy trade recording
- SIMD operations: Vectorized computations (C++ backend)
2. Order Book
Each trading pair has its own order book with optimized data structures.
Structure
BID SIDE ASK SIDE
┌─────────────┐ ┌─────────────┐
│ Price: 50100│ │ Price: 50101│
│ Orders: [...│ │ Orders: [...│
└──────┬──────┘ └──────┬──────┘
│ │
┌──────┴──────┐ ┌──────┴──────┐
│ Price: 50099│ │ Price: 50102│
│ Orders: [...│ │ Orders: [...│
└──────┬──────┘ └──────┬──────┘
│ │
(B-Tree) (B-Tree)
Order Types
- Market: Execute immediately at best price
- Limit: Execute at specified price or better
- Stop: Trigger when price reaches threshold
- Iceberg: Show only partial size
- Hidden: Completely hidden from book
- Pegged: Track reference price
3. Risk Engine
Pre-trade and post-trade risk management.
Pre-trade Checks
- Balance verification
- Position limits
- Leverage constraints
- Rate limiting
- Order validation
Post-trade Processing
- PnL calculation
- Margin requirements
- Liquidation monitoring
- Insurance fund management
4. Clearing & Settlement
Handles the finalization of trades.
Process Flow
- Trade Matching: Orders matched in engine
- Clearing: Verify and record trade
- Settlement: Update balances
- Confirmation: Notify participants
Atomic Settlement
All operations in single transaction:
func (c *Clearinghouse) SettleTrade(trade *Trade) error {
tx := c.BeginTransaction()
defer tx.Rollback()
// Update buyer balance
tx.DebitAccount(trade.Buyer, trade.QuoteAmount)
tx.CreditAccount(trade.Buyer, trade.BaseAmount)
// Update seller balance
tx.DebitAccount(trade.Seller, trade.BaseAmount)
tx.CreditAccount(trade.Seller, trade.QuoteAmount)
// Record trade
tx.RecordTrade(trade)
return tx.Commit()
}
On-chain settlement (C↔D)
The matching engine above runs on the D-Chain (dexvm). It does not move
EVM balances directly. Instead, D matches and C settles: the D-Chain matches
an order, emits a DFillReceipt, and a D-validator quorum BLS-signs the receipt
root; the C-Chain receipt-settlement precompile at 0x9999 then verifies that
certificate inline and debits/credits EVM balances.
┌──────────────── D-CHAIN (dexvm, ~10 validators) ────────────────┐
│ match order → fill → DFillReceipt → BLS-sign receipt root │
└───────────────────────────────┬─────────────────────────────────┘
│ receipt + Merkle proof + BLS cert
│ (in V4 hookData / predicate rail)
┌───────────────────────────────▼─────────────────────────────────┐
│ C-CHAIN 0x9999 (cEVM, ~1000 validators) │
│ verify cert INLINE (deterministic → fork-safe) │
│ debit tokenIn(sender) · credit tokenOut(recipient) │
│ mark consumedReceipt[receiptID] · Block-STM parallel │
└──────────────────────────────────────────────────────────────────┘
D matches · C settles · BLS certifies day-1 · Q later · X finalizes
Key properties:
- Build vs verify: only the build/propose path touches a live matcher; the C-Chain verify path settles solely from the certificate, never calling a live matcher — this is what keeps a single-tx synchronous swap fork-safe.
- Uniswap-V4 ABI:
0x9999exposes the V4PoolManagerABI unchanged, so existing routers/clients work with only an address change (0x9010is deprecated; same impl, same namespace). - Block-STM: fine-grained per-account/asset/receipt/pool keys, no global hot slots, so thousands of independent fills settle in parallel.
- Upgradeable cert:
certType(BLS day-1 → Q / PQ / ZK) is resolved via an on-chain verifier registry with no ABI change. - Halt: multi-layer, fund-preserving default (stop new swaps; allow cancel, settle of safe receipts, and withdrawals).
The normative specification is LP-9999 (DEX V4 Receipt-Settlement Precompile); this document references it and does not duplicate it.
5. DAG Consensus
Directed Acyclic Graph consensus for parallel processing.
Architecture
┌───────┐
│ Node A│───────┐
└───┬───┘ │
│ ┌───▼───┐
│ │ Node C│
│ └───┬───┘
┌───▼───┐ │
│ Node B│───────┘
└───────┘
Features
- Parallel validation: Multiple chains process simultaneously
- Fast finality: 50ms consensus time
- Quantum-resistant: Post-quantum signatures ready
6. Cross-Chain Bridge
Universal bridge for asset transfers between chains.
Supported Chains
- Ethereum & EVM-compatible
- Cosmos ecosystem
- Solana
- Bitcoin (via wrapped tokens)
- Lux native chain
Bridge Flow
- Lock: Assets locked on source chain
- Verify: Validators confirm transaction
- Mint: Equivalent assets minted on destination
- Complete: User receives bridged assets
Data Flow
Order Lifecycle
Client Request
│
▼
[Gateway Layer]
│
├─> Rate Limiting
├─> Authentication
└─> Validation
│
▼
[Risk Engine]
│
├─> Balance Check
├─> Position Limits
└─> Margin Requirements
│
▼
[Matching Engine]
│
├─> Order Book Update
├─> Match Orders
└─> Generate Trades
│
▼
[Clearinghouse]
│
├─> Clear Trades
├─> Update Balances
└─> Settlement
│
▼
[Consensus Layer]
│
├─> Validate Block
├─> Achieve Consensus
└─> Finalize State
│
▼
[Storage Layer]
│
└─> Persist Data
Performance Architecture
Multi-Engine Design
The DEX supports multiple execution backends:
-
Pure Go Engine
- Portable and maintainable
- 163K-332K msgs/sec (FIX protocol)
- 33.5μs average latency
-
C++ Engine
- SIMD optimizations
- Lock-free algorithms
- 444K-1.08M msgs/sec (FIX protocol)
- 8.2μs average latency
-
Rust Engine
- Memory safety with performance
- 232K-586K msgs/sec (FIX protocol)
- 11.9μs average latency
-
GPU Engine (MLX)
- Metal Performance Shaders on Apple Silicon
- Massive parallelization
- 3.12M-5.95M msgs/sec (FIX protocol)
- 0.68-1.75μs average latency
- 434M+ orders/sec batch processing
FIX Protocol Performance (December 2024)
| Engine | NewOrderSingle | ExecutionReport | MarketDataSnapshot |
|---|---|---|---|
| Pure Go | 163K/sec | 124K/sec | 332K/sec |
| Hybrid Go/C++ | 167K/sec | 378K/sec | 616K/sec |
| Pure C++ | 444K/sec | 804K/sec | 1.08M/sec |
| Rust | 484K/sec | 232K/sec | 586K/sec |
| MLX (Apple Silicon) | 3.12M/sec | 4.27M/sec | 5.95M/sec |
*MLX achieves 7-40x throughput improvement via GPU parallelism
Memory Management
Object Pooling
var orderPool = sync.Pool{
New: func() interface{} {
return &Order{}
},
}
func GetOrder() *Order {
return orderPool.Get().(*Order)
}
func PutOrder(order *Order) {
order.Reset()
orderPool.Put(order)
}
Zero-Copy Techniques
- Use of
unsafefor direct memory access - Circular buffers for trades
- Memory-mapped files for large datasets
Concurrency Model
Lock-Free Operations
type AtomicCounter struct {
value atomic.Int64
}
func (c *AtomicCounter) Increment() int64 {
return c.value.Add(1)
}
Goroutine Management
- Worker pools for order processing
- Bounded channels for backpressure
- Context for graceful shutdown
Scalability
Horizontal Scaling
Sharding Strategy
- By Symbol: Each shard handles specific trading pairs
- By User: Users distributed across shards
- By Region: Geographic distribution
Load Balancing
- Consistent hashing for shard selection
- Health-based routing
- Automatic failover
Vertical Scaling
Hardware Optimization
- CPU pinning for critical threads
- NUMA-aware memory allocation
- Kernel bypass networking (DPDK)
Security Architecture
Cryptography
- Ed25519: Digital signatures
- BLS: Aggregate signatures
- Post-Quantum: Lattice-based crypto ready
Access Control
- JWT: API authentication
- RBAC: Role-based permissions
- 2FA: Two-factor authentication
Network Security
- TLS 1.3: Encrypted connections
- DDoS Protection: Rate limiting and filtering
- Firewall Rules: Strict ingress/egress
Monitoring & Observability
Metrics Collection
var (
ordersProcessed = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "orders_processed_total",
Help: "Total number of orders processed",
},
)
orderLatency = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "order_latency_seconds",
Help: "Order processing latency",
},
)
)
Distributed Tracing
- OpenTelemetry integration
- Request flow visualization
- Performance bottleneck identification
Logging
- Structured logging with context
- Log aggregation (ELK stack)
- Real-time alerting
Deployment Architecture
Container Strategy
services:
matching-engine:
replicas: 3
resources:
limits:
cpu: "4"
memory: "8Gi"
risk-engine:
replicas: 2
resources:
limits:
cpu: "2"
memory: "4Gi"
Kubernetes Deployment
- StatefulSets for order books
- Horizontal Pod Autoscaling
- Persistent volume claims for state
High Availability
- Multi-region deployment
- Automatic failover
- Data replication
- Disaster recovery
Future Enhancements
Planned Features
- Layer 2 Scaling: Optimistic rollups
- Advanced Order Types: Options, futures
- AI Market Making: ML-based liquidity
- Quantum Computing: Quantum-resistant fully
- Hardware Acceleration: FPGA/ASIC support