Core Engine

DEX Core Engine

High-performance matching engine with sub-microsecond latency and hardware acceleration

DEX Core Engine

Implementation Status

ComponentStatusSource
Order Book CoreCompletepkg/lx/orderbook.go
Matching EngineCompletepkg/lx/orderbook_advanced.go
Price-Time PriorityCompletepkg/lx/orderbook.go#L108
Integer Price RepresentationCompletepkg/lx/orderbook.go#L16-L34
MLX Hardware AccelerationCompletepkg/engine/mlx_engine.go
DAG Consensus IntegrationCompletepkg/consensus/dag.go
Event SystemCompletepkg/lx/orderbook_extended.go

Architecture Overview

                              DEX CORE ENGINE ARCHITECTURE

    ┌─────────────────────────────────────────────────────────────────────────────┐
    │                              API LAYER                                       │
    │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐  ┌─────────────────────┐ │
    │  │  JSON-RPC   │  │  WebSocket  │  │    gRPC     │  │      FIX 4.4        │ │
    │  └──────┬──────┘  └──────┬──────┘  └──────┬──────┘  └──────────┬──────────┘ │
    └─────────┼────────────────┼────────────────┼────────────────────┼────────────┘
              │                │                │                    │
              v                v                v                    v
    ┌─────────────────────────────────────────────────────────────────────────────┐
    │                          ORDER ROUTER                                        │
    │  ┌───────────────────────────────────────────────────────────────────────┐  │
    │  │  Order Validation -> Risk Check -> Self-Trade Prevention -> Routing   │  │
    │  └───────────────────────────────────────────────────────────────────────┘  │
    └─────────────────────────────────────────────────────────────────────────────┘

              v
    ┌─────────────────────────────────────────────────────────────────────────────┐
    │                         MATCHING ENGINE                                      │
    │  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────────────────┐  │
    │  │   Order Book    │  │ Price Levels    │  │     Trade Generator         │  │
    │  │   (per symbol)  │  │ (B-Tree + Heap) │  │   (FIFO/Pro-rata)           │  │
    │  └────────┬────────┘  └────────┬────────┘  └────────────┬────────────────┘  │
    │           │                    │                        │                   │
    │           v                    v                        v                   │
    │  ┌─────────────────────────────────────────────────────────────────────┐    │
    │  │                    OPTIMIZED DATA STRUCTURES                        │    │
    │  │  • PriceInt (int64 * 10^8) for fast integer comparison              │    │
    │  │  • IntBTree for O(log n) price level operations                     │    │
    │  │  • OrderLinkedList for O(1) insertion/removal at price level        │    │
    │  │  • CircularTradeBuffer for efficient trade history                  │    │
    │  │  • sync.Map for lock-free order tracking                            │    │
    │  └─────────────────────────────────────────────────────────────────────┘    │
    └─────────────────────────────────────────────────────────────────────────────┘

              v
    ┌─────────────────────────────────────────────────────────────────────────────┐
    │                     HARDWARE ACCELERATION LAYER                              │
    │  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────────────────┐  │
    │  │   Pure Go       │  │   MLX (Metal)   │  │      FPGA                   │  │
    │  │   Default       │  │   Apple Silicon │  │   AMD Versal / AWS F2       │  │
    │  └─────────────────┘  └─────────────────┘  └─────────────────────────────┘  │
    └─────────────────────────────────────────────────────────────────────────────┘

              v
    ┌─────────────────────────────────────────────────────────────────────────────┐
    │                        DAG CONSENSUS                                         │
    │  ┌─────────────────────────────────────────────────────────────────────┐    │
    │  │  Lux Consensus (FPC) + Quasar Certificates + Ringtail PQ Signatures │    │
    │  └─────────────────────────────────────────────────────────────────────┘    │
    └─────────────────────────────────────────────────────────────────────────────┘

Core Components

1. Order Book (OrderBook)

The central data structure for maintaining buy and sell orders with price-time priority.

package main

import (
    "fmt"
    "github.com/luxfi/dex/pkg/lx"
)

func main() {
    // Create a new order book for BTC/USD
    ob := lx.NewOrderBook("BTC/USD")

    // Add a limit buy order
    buyOrder := &lx.Order{
        Side:  lx.Buy,
        Type:  lx.Limit,
        Price: 50000.00,
        Size:  1.5,
        User:  "trader-1",
    }
    orderID := ob.AddOrder(buyOrder)
    fmt.Printf("Buy order placed: ID=%d\n", orderID)

    // Add a limit sell order
    sellOrder := &lx.Order{
        Side:  lx.Sell,
        Type:  lx.Limit,
        Price: 50000.00,
        Size:  1.0,
        User:  "trader-2",
    }
    ob.AddOrder(sellOrder)

    // Execute matching
    trades := ob.MatchOrders()
    for _, trade := range trades {
        fmt.Printf("Trade executed: Price=%.2f Size=%.4f\n",
            trade.Price, trade.Size)
    }

    // Get order book depth
    depth := ob.GetDepth(10)
    fmt.Printf("Bid levels: %d, Ask levels: %d\n",
        len(depth.Bids), len(depth.Asks))
}

2. Integer Price Representation

All prices are internally represented as PriceInt (int64) with 8 decimal precision for fast integer comparison operations.

// PriceInt represents price as integer (multiplied by 10^8 for 8 decimal precision)
type PriceInt int64

const PriceMultiplier = 100000000

// MaxSafePrice is the maximum price that can be safely converted to PriceInt
// without overflow (math.MaxInt64 / PriceMultiplier)
const MaxSafePrice = float64(math.MaxInt64) / PriceMultiplier

// Example: $50,000.12345678 becomes 5000012345678 (int64)
priceInt := PriceInt(50000.12345678 * PriceMultiplier)
// Result: 5000012345678

3. Order Tree (OrderTree)

Each side of the order book (bids/asks) is implemented as an OrderTree with:

  • B-Tree (IntBTree) for sorted price levels with O(log n) operations
  • Price Heap for quick best price access
  • Price Level Map for O(1) price level lookup
  • Atomic Best Price for lock-free reads
type OrderTree struct {
    side        Side
    priceLevels map[PriceInt]*OptimizedPriceLevel  // O(1) lookup
    priceTree   *IntBTree                           // O(log n) range queries
    orders      map[uint64]*Order                   // Order tracking
    bestPrice   atomic.Int64                        // Lock-free best price
    priceHeap   PriceHeap                           // Heap for compatibility
    mu          sync.RWMutex
}

4. Price Levels (OptimizedPriceLevel)

Each price level maintains orders in FIFO order using a doubly-linked list for O(1) operations:

type OptimizedPriceLevel struct {
    Price      float64              // Original price for API
    PriceInt   PriceInt             // Integer price for operations
    Orders     []*Order             // Slice for compatibility
    OrderList  *OrderLinkedList     // O(1) insertion/removal
    TotalSize  float64
    OrderCount int
    atomicSize  atomic.Int64        // Lock-free size updates
    atomicCount atomic.Int32        // Lock-free count updates
}

type OrderLinkedList struct {
    head  *OrderNode
    tail  *OrderNode
    index map[uint64]*OrderNode    // O(1) order lookup
}

Performance Characteristics

OperationTime ComplexityNotes
Add OrderO(log n)B-tree insertion + heap push
Cancel OrderO(1)Direct lookup via order ID map
Modify OrderO(log n)Remove + re-insert
Get Best PriceO(1)Atomic load
Match OrdersO(k)k = number of price levels crossed
Get DepthO(d log d)d = requested depth levels

Latency Targets

  • Order Add: < 500ns median
  • Order Match: < 1us per trade
  • Best Bid/Ask: < 100ns
  • Full Snapshot: < 10us

FIX Protocol Performance (December 2024)

EngineNewOrderSingleExecutionReportMarketDataSnapshot
Pure Go163K/sec124K/sec332K/sec
Pure C++444K/sec804K/sec1.08M/sec
MLX (Apple Silicon)3.12M/sec4.27M/sec5.95M/sec

*MLX GPU achieves sub-2μs average latency (0.68-1.75μs) through Metal Performance Shaders

Hardware Acceleration Backends

The engine automatically detects and uses the best available backend:

type Backend int

const (
    BackendGo Backend = iota    // Pure Go (default)
    BackendCGO                  // CGO optimizations
    BackendMLX                  // Apple Silicon Metal
    BackendCUDA                 // NVIDIA GPU
)

func detectBestBackend() Backend {
    // CUDA if available
    if os.Getenv("CUDA_VISIBLE_DEVICES") != "" {
        return BackendCUDA
    }

    // MLX on Apple Silicon
    if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
        if _, err := os.Stat("/System/Library/Frameworks/Metal.framework"); err == nil {
            return BackendMLX
        }
    }

    // CGO if enabled
    if os.Getenv("CGO_ENABLED") == "1" {
        return BackendCGO
    }

    return BackendGo
}

MLX Engine (Apple Silicon)

import "github.com/luxfi/dex/pkg/engine"

config := engine.MLXConfig{
    MaxMarkets:    1000,
    MarketDepth:   1000,
    UnifiedMemory: true,    // No CPU<->GPU copies!
    CacheGB:       64,
    BufferGB:      128,
}

mlx, err := engine.NewMLXEngine(config)
if err != nil {
    log.Fatal(err)
}

// Process batch of orders on GPU
result, err := mlx.ProcessBatch(orders)
fmt.Printf("Throughput: %.2f M orders/sec\n", result.ThroughputMPS)
fmt.Printf("Latency: %d ns\n", result.LatencyNanos)

Trading Engine Integration

The TradingEngine orchestrates multiple order books and integrates with DeFi components:

import "github.com/luxfi/dex/pkg/lx"

config := lx.EngineConfig{
    EnablePerps:   true,
    EnableVaults:  true,
    EnableLending: true,
}

engine := lx.NewTradingEngine(config)
engine.Start()
defer engine.Stop()

// Create spot market
btcBook := engine.CreateSpotMarket("BTC/USD")

// Create perpetual market
engine.PerpManager.CreateMarket(lx.PerpMarketConfig{
    Symbol:         "BTC-PERP",
    IndexSymbol:    "BTC/USD",
    MaxLeverage:    100,
    MaintenanceReq: 0.005,
    FundingRate:    0.0001,
})

Memory Optimization

Object Pooling

The engine uses sync.Pool for zero-allocation order processing:

var orderPool = &sync.Pool{
    New: func() interface{} {
        return &Order{}
    },
}

var levelPool = &sync.Pool{
    New: func() interface{} {
        return &OptimizedPriceLevel{}
    },
}

// Get from pool
order := orderPool.Get().(*Order)
// ... use order ...
// Return to pool
orderPool.Put(order)

Circular Trade Buffer

Trade history uses a fixed-size circular buffer to avoid allocations:

type CircularTradeBuffer struct {
    buffer   []Trade     // Pre-allocated
    head     uint64
    tail     uint64
    size     uint64
    capacity uint64
}

// Add is O(1), no allocations
func (ctb *CircularTradeBuffer) Add(trade Trade) {
    ctb.buffer[ctb.tail] = trade
    ctb.tail = (ctb.tail + 1) % ctb.capacity
    // ...
}

Cache Line Padding

The OrderBook struct includes padding to prevent false sharing:

type OrderBook struct {
    // ... fields ...

    // Cache line padding to prevent false sharing
    _padding [64]byte
}

Supported Order Types

TypeDescriptionImplementation
LimitExecute at specified price or betterAddOrder()
MarketExecute immediately at best availableprocessMarketOrderOptimized()
StopTrigger market order at stop priceprocessStop()
Stop-LimitTrigger limit order at stop priceprocessStop()
IcebergLarge order with visible portionprocessIcebergOrder()
HiddenNon-displayed orderprocessHiddenOrder()
PeggedPrice tracks best bid/askprocessPeggedOrder()
BracketTrailing stop lossprocessTrailingStop()

Time-in-Force Options

const (
    TIF_DAY TimeInForce = iota  // Good for day
    TIF_IOC                      // Immediate-or-cancel
    TIF_FOK                      // Fill-or-kill
    TIF_GTC                      // Good-til-canceled
)

// IOC: Execute what you can, cancel rest
order.TimeInForce = "IOC"

// FOK: Execute entirely or reject
order.TimeInForce = "FOK"

Self-Trade Prevention

const (
    OrderFlagNone       OrderFlags = 0
    OrderFlagPostOnly   OrderFlags = 1 << 0  // Maker only
    OrderFlagReduceOnly OrderFlags = 1 << 1  // Reduce position only
    OrderFlagSTP        OrderFlags = 1 << 2  // Self-trade prevention
)

// Check for self-trade before matching
func (ob *OrderBook) checkSelfTrade(order *Order) bool {
    // ... compares user IDs of crossing orders ...
}

Error Handling

var (
    ErrOrderNotFound     = fmt.Errorf("order not found")
    ErrInsufficientFunds = fmt.Errorf("insufficient funds")
    ErrInvalidOrder      = fmt.Errorf("invalid order")
    ErrInvalidPrice      = fmt.Errorf("invalid price")
    ErrInvalidSize       = fmt.Errorf("invalid size")
    ErrSelfTrade         = fmt.Errorf("self trade not allowed")
    ErrPostOnlyWouldTake = fmt.Errorf("post-only order would take")
    ErrUnauthorized      = fmt.Errorf("unauthorized")
)