Development Guide

Build, test, and contribute to LX

LX Development Guide

Specification: LP-0050 Developer Tools Overview | LP-0006 Network Runner and Testing Framework | Discussions

Implementation: github.com/luxfi/dex

Getting Started

Prerequisites

  • Go 1.21+
  • Docker & Docker Compose
  • Make
  • Git

Initial Setup

# Clone repository
git clone https://github.com/luxfi/dex.git
cd dex

# Install dependencies
go mod download

# Run tests to verify setup
make test

# Build the project
make build

Project Structure

dex/
├── pkg/                    # Core packages
│   ├── lx/                # Trading engine
│   │   ├── orderbook.go  # Order matching engine
│   │   ├── margin.go      # Margin trading
│   │   ├── vaults.go      # DeFi vaults
│   │   └── bridge.go      # Cross-chain bridge
│   ├── api/               # API servers
│   ├── consensus/         # DAG consensus
│   └── client/            # Client libraries
├── cmd/                   # Executables
├── examples/              # Example code
├── test/                  # Test suites
│   ├── unit/             # Unit tests
│   ├── integration/      # Integration tests
│   └── e2e/              # End-to-end tests
├── sdk/                   # Language SDKs
├── docs/                  # Documentation
└── .github/workflows/     # CI/CD pipelines

Development Workflow

1. Create Feature Branch

git checkout -b feature/your-feature-name

2. Make Changes

Follow the coding standards:

  • Use meaningful variable names
  • Add comments for complex logic
  • Follow Go conventions
  • Keep functions small and focused

3. Write Tests

Every new feature must include tests:

func TestYourFeature(t *testing.T) {
    // Arrange
    setup := setupTest()

    // Act
    result := YourFeature(input)

    // Assert
    assert.Equal(t, expected, result)
}

4. Run Tests Locally

# Run all tests
make test

# Run specific test
go test -run TestOrderBook ./pkg/lx/

# Run with coverage
go test -cover ./pkg/lx/

5. Commit Changes

git add .
git commit -m "feat: add new feature

- Detailed description
- What was changed
- Why it was changed"

6. Push and Create PR

git push origin feature/your-feature-name

Testing

Unit Tests

# Run unit tests
go test ./pkg/...

# With coverage
go test -cover ./pkg/...

# Generate coverage report
go test -coverprofile=coverage.out ./pkg/...
go tool cover -html=coverage.out

Integration Tests

# Run integration tests
go test ./test/integration/...

# Skip in CI (long-running)
go test -short ./test/integration/...

Benchmarks

# Run all benchmarks
make bench

# Run specific benchmark
go test -bench=BenchmarkOrderBook -benchtime=10s ./pkg/lx/

# With memory profiling
go test -bench=. -benchmem ./pkg/lx/

Load Testing

# Run load test
go run cmd/stress-test/main.go

# Monitor performance
watch -n 1 'go run cmd/monitor/main.go'

Performance Optimization

Profiling

# CPU profiling
go test -cpuprofile=cpu.prof -bench=. ./pkg/lx/
go tool pprof cpu.prof

# Memory profiling
go test -memprofile=mem.prof -bench=. ./pkg/lx/
go tool pprof mem.prof

# Trace execution
go test -trace=trace.out ./pkg/lx/
go tool trace trace.out

Optimization Tips

  1. Use sync.Pool for frequently allocated objects
  2. Minimize allocations in hot paths
  3. Use atomic operations instead of mutexes where possible
  4. Profile before optimizing - measure, don't guess
  5. Batch operations when possible

Example Optimization

// Before: Allocates on every call
func processOrder(order Order) {
    result := &Result{} // Allocation
    // process...
}

// After: Reuse with sync.Pool
var resultPool = sync.Pool{
    New: func() interface{} {
        return &Result{}
    },
}

func processOrder(order Order) {
    result := resultPool.Get().(*Result)
    defer resultPool.Put(result)
    result.Reset() // Clear previous data
    // process...
}

Contributing

Code Style

  • Run gofmt before committing
  • Use golangci-lint for linting
  • Follow Effective Go
# Format code
make fmt

# Run linters
make lint

# Run all checks
make check

Commit Convention

Follow Conventional Commits:

  • feat: New feature
  • fix: Bug fix
  • docs: Documentation
  • test: Testing
  • perf: Performance improvement
  • refactor: Code refactoring
  • chore: Maintenance

Pull Request Process

  1. Update documentation
  2. Add/update tests
  3. Ensure CI passes
  4. Request review
  5. Address feedback
  6. Squash commits if needed

Review Checklist

  • Tests pass
  • Documentation updated
  • No security issues
  • Performance impact considered
  • Breaking changes documented
  • Error handling appropriate

Debugging

Local Debugging

# Run with debug logs
LOG_LEVEL=debug go run cmd/dex-server/main.go

# Use Delve debugger
dlv debug cmd/dex-server/main.go

# Attach to running process
dlv attach <pid>

Remote Debugging

# Start with debug server
dlv debug --headless --listen=:2345 cmd/dex-server/main.go

# Connect from IDE or CLI
dlv connect :2345

Performance Metrics

Key Metrics to Monitor

  • Latency: Order matching time
  • Throughput: Orders/second
  • Memory: Heap usage and GC pressure
  • Goroutines: Active count and leaks
  • Network: Message rate and bandwidth

Monitoring Tools

# Built-in metrics
curl http://localhost:9090/metrics

# Prometheus
docker-compose up prometheus grafana

# Custom dashboard
go run cmd/monitor/main.go

Deployment

Local Development

# Single node
make run

# Multi-node cluster
make run-cluster

# With Docker
docker-compose up

Production

# Build production binary
CGO_ENABLED=0 make build

# Build Docker image
docker build -t lux-dex:latest .

# Deploy to Kubernetes
kubectl apply -f k8s/

Troubleshooting

Common Issues

1. Tests timing out

# Increase timeout
go test -timeout=30s ./pkg/lx/

# Run in short mode
go test -short ./pkg/lx/

2. Memory leaks

# Check for goroutine leaks
go test -run=. -count=1 ./pkg/lx/

# Profile memory
go tool pprof http://localhost:6060/debug/pprof/heap

3. Race conditions

# Run with race detector
go test -race ./pkg/lx/

Resources

Documentation

Tools

Community