README
ΒΆ
Lux Consensus Examples - Pedagogical Guide
This directory contains progressive examples teaching you how to use the Lux consensus and cross-chain infrastructure.
Learning Path π
The examples are designed to be completed in order, each building on concepts from the previous:
Basic Concepts β Networking β AI Integration β Advanced Consensus
β β β β
01-02 03-04 05-06 07
Examples Overview
Level 1: Basic Concepts
01-simple-bridge - Cross-Chain Bridge Basics
- Learn: Bridge creation, asset transfers, status monitoring
- Tech: Go, Lux DEX bridge
- Time: 15 minutes
- Prerequisites: None
02-ai-payment - AI Payment Validation
- Learn: AI decision making, confidence scoring, fraud detection
- Tech: Go, AI consensus agents
- Time: 20 minutes
- Prerequisites: Example 01
Level 2: Networking & Communication
03-qzmq-networking - Quantum-Secure Messaging
- Learn: QZMQ protocol, quantum-resistant encryption, message routing
- Tech: Go, ZeroMQ, post-quantum crypto
- Time: 25 minutes
- Prerequisites: Example 01
04-grpc-service - gRPC API Integration
- Learn: Protocol buffers, gRPC services, API design
- Tech: Go, gRPC, Protocol Buffers
- Time: 30 minutes
- Prerequisites: Example 01, 03
Level 3: Multi-Language Integration
05-python-client - Python Bridge Client
- Learn: Cross-language RPC, Python integration, async programming
- Tech: Python, gRPC, asyncio
- Time: 25 minutes
- Prerequisites: Example 04
06-nodejs-client - Node.js TypeScript Client
- Learn: TypeScript integration, WebSocket connections, real-time updates
- Tech: TypeScript, Node.js, WebSocket
- Time: 25 minutes
- Prerequisites: Example 04
Level 4: Advanced AI Consensus
07-ai-consensus - Dynamic AI-Managed Consensus
- Learn: Multi-agent consensus, shared hallucinations, photonβquasar flow
- Tech: Go, AI agents, consensus protocols
- Time: 45 minutes
- Prerequisites: All previous examples
Quick Start
Run a Simple Example
# Example 1: Simple Bridge
cd 01-simple-bridge
go run main.go
# Run its tests
go test -v
Run All Examples
# Test all Go examples
./run_all_tests.sh
# Or individually
for dir in 0*/; do
cd "$dir"
go test -v
cd ..
done
What You'll Learn
Core Concepts
-
Cross-Chain Transfers (Ex 01)
- How assets move between blockchains
- Transfer lifecycle and confirmations
- Exchange rates and liquidity
-
AI Decision Making (Ex 02)
- How AI validates transactions
- Confidence scoring and thresholds
- Continuous learning from outcomes
-
Quantum-Secure Networking (Ex 03)
- Post-quantum cryptography
- Secure message routing
- Performance vs security tradeoffs
-
Service Communication (Ex 04)
- gRPC vs REST patterns
- Protocol buffer schemas
- Service mesh integration
-
Language Interoperability (Ex 05-06)
- Go backend, Python/Node frontend
- Cross-language type safety
- Error handling across boundaries
-
Advanced Consensus (Ex 07)
- Multi-agent coordination
- Shared hallucinations
- PhotonβQuasar consensus flow
Integration Patterns
Each example demonstrates a key integration pattern:
- 01: Production bridge as dependency
- 02: AI agent wrapping business logic
- 03: QZMQ for secure transport layer
- 04: gRPC for service boundaries
- 05-06: Multi-language clients
- 07: Full AI consensus orchestration
Testing Philosophy
Each example includes:
- β
Runnable Demo (
main.goormain.py) - See it work - β
Unit Tests (
*_test.go) - Verify behavior - β
Documentation (
README.md) - Understand why - β Interactive - Modify and experiment
Run tests to prove the examples work:
# Go examples
go test -v
# Python examples
pytest -v
# Node.js examples
npm test
Project Structure
examples/
βββ README.md # This file
βββ 01-simple-bridge/ # Basic bridge usage
β βββ main.go # Runnable demo
β βββ bridge_test.go # Tests
β βββ go.mod # Dependencies
β βββ README.md # Guide
β
βββ 02-ai-payment/ # AI validation
β βββ main.go
β βββ payment_test.go
β βββ README.md
β
βββ 03-qzmq-networking/ # Quantum-secure messaging
β βββ main.go
β βββ qzmq_test.go
β βββ README.md
β
βββ 04-grpc-service/ # gRPC service
β βββ server/main.go
β βββ client/main.go
β βββ proto/bridge.proto
β βββ README.md
β
βββ 05-python-client/ # Python integration
β βββ client.py
β βββ test_client.py
β βββ requirements.txt
β βββ README.md
β
βββ 06-nodejs-client/ # Node.js integration
β βββ src/client.ts
β βββ test/client.test.ts
β βββ package.json
β βββ README.md
β
βββ 07-ai-consensus/ # Advanced AI consensus
βββ main.go
βββ consensus_test.go
βββ README.md
Tips for Learning
- Follow the Order: Each example builds on previous concepts
- Run the Code: Don't just read - execute and experiment
- Read the Tests: Tests show expected behavior and edge cases
- Modify and Break: Change values, see what fails, understand why
- Check Dependencies: Each example lists required packages
- Ask Questions: README files explain the "why" behind decisions
Common Patterns
Creating Clients
// Pattern 1: Direct instantiation
bridge := lx.NewCrossChainBridge(config)
// Pattern 2: Builder pattern
engine := ai.NewBuilder().
WithInference(model).
WithDecision(threshold).
Build()
// Pattern 3: Dependency injection
adapter := NewAIBridgeAdapter(bridge, agent, nodeID)
Error Handling
// Pattern 1: Immediate error check
result, err := client.Transfer(...)
if err != nil {
return fmt.Errorf("transfer failed: %w", err)
}
// Pattern 2: Retry with backoff
for i := 0; i < maxRetries; i++ {
if err := operation(); err == nil {
break
}
time.Sleep(backoff * time.Duration(i))
}
// Pattern 3: Circuit breaker
if breaker.Allow() {
err := operation()
breaker.Record(err)
}
Testing Strategies
// Pattern 1: Table-driven tests
tests := []struct {
name string
input int
want int
}{
{"case1", 1, 2},
{"case2", 2, 4},
}
// Pattern 2: Mocking dependencies
mock := &MockBridge{}
client := NewClient(mock)
// Pattern 3: Integration tests
if testing.Short() {
t.Skip("skipping integration test")
}
Troubleshooting
"Cannot find package"
# Ensure you're in the example directory
cd examples/01-simple-bridge
# Initialize module
go mod init
go mod tidy
"Connection refused"
# For examples requiring servers (04, 05, 06)
# Start the server first in one terminal
cd server && go run main.go
# Then run client in another terminal
cd ../client && go run main.go
"Import cycle"
# Check go.mod has correct replace directives
replace github.com/luxfi/consensus => ../..
replace github.com/luxfi/dex => ../../../dex
Contributing
Want to add an example? Follow these guidelines:
- Pedagogical: Teach one concept clearly
- Progressive: Build on previous examples
- Tested: Include comprehensive tests
- Documented: Explain the "why" not just "how"
- Runnable: Should work out of the box
Additional Resources
Support
Questions? Check:
- Example-specific README.md
- Test files for usage patterns
- Source code comments
- Main project documentation
Happy learning! π
Documentation
ΒΆ
There is no documentation for this package.