The matching engine (pkg/lx, 45.5K LOC incl. tests) and the deterministic core layered on it (pkg/dex) are one matcher spread across two packages (dex imported lx in 12 files; lx imported neither). Fold lx into dex and drop the "lx" name entirely — pkg/dex is now the single matcher package. Pure rename/fold, ZERO logic change. Behavior proven identical: the merged pkg/dex runs 457 top-level tests (431 former-lx + 26 former-dex) and 489 test funcs (468 + 21), all green — matching, batch (BatchMatchEqualsSequential, run-to-run determinism, tile boundary), GPU/CPU parity, truncation, and CL swap math all pass unchanged. What moved: - git mv all 101 pkg/lx/*.go + 1 .c into pkg/dex/ (package lx -> package dex). - lx's swap_test.go -> swap_math_test.go (only filename collision with dex's existing swap_test.go). - Rewrote imports repo-wide: 54 pkg/lx import lines removed; 42 external importers (api, client, consensus, dchain, dpdk, lxgpu, marketdata, test/, tests/) repointed to pkg/dex with lx.X -> dex.X; the 12 in-package dex files had lx.X -> bare X. 717 selector lines rewritten. Stale pkg/lx comment refs updated (pkg/lxgpu left intact). Collisions resolved (2, both package-level vars): - ErrNoLiquidity: lx's pool-level error (dexError, used only in swap.go, never referenced via qualifier) renamed ErrPoolNoLiquidity to match the existing ErrPool* convention; dex's router-level ErrNoLiquidity (public, has errors.Is tests) kept. - priceMultiplierBig: identical after fold (both big.NewInt(PriceMultiplier)); deduped by deleting dex's copy. Not folded — pkg/orderbook: it is NOT the GPU seam (that is orderbook_gpu.go, which lives in the engine and folded in here). pkg/orderbook is a zero-importer, placeholder-cgo island whose 13 core domain identifiers (Order, OrderBook, Trade, OrderType, OrderStatus, PriceLevel, Buy, Sell, Market, Limit, Filled, PartiallyFilled, NewOrderBook) collide incompatibly with the engine's. Folding it verbatim breaks the build; folding via rename reintroduces the exact parallel duplication this fold removes. Left for owner decision (recommend deletion as dead code) — see refactor notes. Follow-on (separate repos, not touched): 19 files under luxfi/dex import pkg/lx externally — lux/precompile/dex, lux/precompile-dex/dex, lux/consensus/examples/01-simple-bridge, lux/_consensus-v12537/examples, lux/cex/pkg/engine — repoint to pkg/dex after this lands. Verified: CGO_ENABLED=0 GOWORK=off go build ./... clean; go vet ./... clean; go test ./... -count=1 all green (22 ok / 0 fail). Co-authored-by: Hanzo Dev <[email protected]>
16 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. Order-matching throughput is 11.88M orders/sec (C++, 10 threads, 169 ns avg match) / 2.2M orders/sec (pure Go) on the default CPU build, and up to 12.76B orders/sec (AMD 8060S) / 9.13B (GB10) on the GPU-native per-book matcher (backend 4). The per-engine msgs/sec and latency figures below are stale December-2024 FIX wire encode/decode rates (a separate axis from matching) and have not been re-verified.
-
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-native per-book matcher (
pkg/lx/orderbook_gpu.go, CGO_ENABLED=1)- Unified
lux-gpubackend, runtime-select CUDA > HIP > Metal > CPU - Byte-identical to the CPU oracle (
MatchOrderCPU), parity-verified (pkg/lxgpu/orderbook_parity_test.go,three_mode_parity_test.go) - Up to 12.76B orders/sec (AMD 8060S) / 9.13B (GB10) / 5.60B (M4 Max) / 2.80B (M1 Max); 21.9B on a two-node fabric — deterministic per-book, one thread per book across millions of books
- Kernels ship prebuilt from luxcpp/dex
- Unified
FIX Protocol Performance (December 2024 — stale, not re-verified this session)
FIX wire encode/decode rates only (a separate axis from order matching); treat as historical, not current.
| 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 |
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