Architecture Deep-Dive
This document provides a comprehensive technical overview of GX Core’s internal architecture, including consensus, block production, state management, and the data flow from order submission to settlement.
Dual-Engine Architecture
GX Chain runs two execution environments under a shared BFT consensus layer:
| Engine | Runtime | Purpose |
|---|---|---|
| GX Core | Native Rust (no VM) | Order matching, perpetuals, spot, risk engine, liquidations, oracle |
| GX EVM | EVM-compatible runtime | Solidity smart contracts, DeFi (including gxUSD CDP), NFTs, bridge contracts |
Both engines process transactions proposed in the same block. GX Core transactions are applied first (deterministic, native), followed by GX EVM transactions. A system contract interface allows the EVM to read GX Core state (prices, positions, balances).
GX BFT Consensus
GX Chain uses GX BFT consensus, a leader-based Byzantine Fault Tolerant protocol with linear message complexity and consensus pipelining for overlapping proposal/vote phases.
How It Works
- Leader election — A round-robin or stake-weighted leader is selected for each block height
- Proposal — The leader bundles pending transactions into a block proposal
- Voting — Validators vote on the proposal; a block is committed when more than two-thirds of weighted stake votes in favor
- Finality — Committed blocks are final immediately — no reorgs, no rollbacks
- View change — If the leader fails to propose within the timeout, validators elect a new leader
Key Properties
| Property | Value |
|---|---|
| Fault tolerance | f < n/3 (tolerates up to one-third Byzantine validators) |
| Block time | Sub-second target |
| Finality | Immediate (single-slot finality) |
| Message complexity | O(n) per block (linear in validator count) |
| Implementation | Custom Rust BFT crate |
Validator Set
The validator set is managed through epochs. At each epoch boundary (configurable number of blocks), the active validator set can be updated based on staking changes, jailing events, or governance decisions.
| Parameter | Value |
|---|---|
| Launch validators | 3-5 (team-operated) |
| Target validator set | 21-51 active validators |
| Minimum stake (Phase 2) | 500,000 GX |
| Minimum stake (Phase 3) | 100,000 GX |
Block Production Pipeline
1. MEMPOOL
- Orders arrive via REST API (port 4001)
- Each order is EIP-712 signature-verified
- Orders are placed in the semantic mempool
- Semantic ordering: cancels before new orders, liquidations before regular orders
2. BLOCK PROPOSAL (Leader)
- Leader drains the mempool up to the block gas/size limit
- Constructs a block with ordered transactions
- Signs the block proposal
- Broadcasts to all validators via P2P gossip
3. BFT VOTING
- Validators verify the block proposal
- Each validator votes (sign with validator key)
- Leader collects votes; commits when >2/3 quorum reached
- Block is finalized (near-instant from proposal)
4. BLOCK APPLICATION (All Validators)
- GX Core transactions applied first:
a. Process cancellations
b. Process liquidations
c. Match new orders against the order book
d. Update positions, margin, PnL
e. Apply funding rate payments
f. Settle trading fees
- GX EVM transactions applied second:
a. Execute Solidity contract calls
b. Update EVM state trie
- State hash computed and stored
5. PERSISTENCE
- RocksDB: block state snapshot
- PostgreSQL: fills, candles, block metadata
- WebSocket: broadcast orderbook updates, trades, positionsSemantic Mempool
Unlike generic blockchain mempools that order transactions by gas price, GX Core uses a semantic mempool that understands the meaning of each transaction type:
| Priority | Transaction Type | Rationale |
|---|---|---|
| 1 (highest) | Liquidations | Must execute immediately to protect protocol solvency |
| 2 | Order cancellations | Prevent stale orders from matching |
| 3 | Withdrawals | Time-sensitive user operations |
| 4 | New orders | Standard trading flow |
| 5 | Governance | Non-time-critical protocol operations |
Within each priority level, transactions are ordered by arrival time (FIFO). This semantic ordering prevents front-running and ensures that risk-critical operations always execute before regular trades.
State Storage
RocksDB (Hot State Persistence)
RocksDB stores the canonical chain state as key-value pairs. On every committed block, the full state is checkpointed. On validator restart, state is loaded from the latest RocksDB snapshot.
Key state domains:
| Domain | Contents |
|---|---|
accounts/ | Balances, collateral, margin per address |
positions/ | Open perpetual positions per address per market |
orderbook/ | Resting orders per market |
markets/ | Market configuration, funding rates, oracle prices |
staking/ | Staked amounts, cooldowns, rewards |
blocks/ | Block headers, state hashes |
PostgreSQL (Historical Indexer)
PostgreSQL stores historical data for API queries. It is populated asynchronously by the indexer and is not part of the consensus-critical path.
| Table | Contents |
|---|---|
fills | Every matched trade with price, size, maker, taker |
candles | OHLCV aggregates at 1m, 5m, 15m, 1h, 4h, 1d intervals |
blocks | Block height, timestamp, transaction count, state hash |
funding_rates | Historical funding rate snapshots |
liquidations | Liquidation events with bankrupt price, backstop amount |
P2P Networking
GX Chain uses a libp2p-based P2P layer for inter-validator communication:
| Feature | Implementation |
|---|---|
| Transport | TCP with Noise protocol encryption |
| Discovery | Bootstrap nodes + gossip-based peer exchange |
| Block propagation | Gossip protocol with deduplication |
| Transaction propagation | Direct relay to the current leader |
| Peer scoring | Reputation system; misbehaving peers are deprioritized |
| Connection management | Configurable connection limits, NAT traversal |
The P2P port is 9000 by default. All validator-to-validator communication is authenticated using secp256k1 signatures and encrypted using Noise framework.
Authentication
All user-facing operations require EIP-712 typed data signatures:
EIP-712 Domain:
name: "GXExchange"
version: "1"
chainId: 42069
Order Signature Payload:
market: "BTC-USD"
side: "buy"
size: "1.5"
price: "67400"
type: "limit"
nonce: 42
expiry: 1714000000The same Ethereum private key that controls a user’s MetaMask wallet signs orders on GX Chain. No separate key generation or chain-specific wallet is required.
For programmatic access, GX Core also supports API key authentication using HMAC-SHA256 signatures, enabling low-latency trading bots without per-order wallet signing.
Monitoring
GX Core exposes Prometheus-compatible metrics at the /metrics endpoint:
| Metric | Description |
|---|---|
gx_block_height | Current block height |
gx_orders_per_second | Rolling order throughput |
gx_match_latency_ns | Order match latency histogram |
gx_block_apply_ms | Block application time |
gx_active_connections | WebSocket subscriber count |
gx_orderbook_depth | Number of resting orders per market |
gx_validator_votes | Consensus vote counts per round |