A perp order book inside CometBFT consensus
NeutronX matches perp orders inside the chain's state machine, not in a smart contract. How the block flow, determinism and on-chain risk checks work.
·
11 min read
Contents
On NeutronX, a perpetual futures order is an ordinary signed transaction. Matching runs in every block as part of the chain's state machine: the block proposer computes it, and every validator re-runs it and compares the result before accepting the block. There is no off-chain matcher and no exchange smart contract, and the exchange's parameters change only through on-chain governance. This post walks through how the block flow, determinism and the risk checks fit inside CometBFT consensus.
Why not a smart contract
A central limit order book is busy code. Traders place, cancel and replace orders all the time, and the book has to match, settle, check margin, run liquidations and charge funding.
On a general-purpose chain, every order and every cancel pays gas, and each transaction lives under a compute limit. A contract also has no step of its own at the start and the end of a block, which is where liquidations and funding naturally belong: something has to call it.
There are three common ways to build an on-chain venue:
Design | Where matching runs | Cost per order | Own block steps |
|---|---|---|---|
CLOB in a smart contract | inside the contract | gas per order | none |
Off-chain matcher | an operator's server | none on-chain | off-chain |
App-chain matching | the state machine | one transaction | start and end |
With an off-chain matcher, the chain settles trades, but the order of matches comes from the operator's server. An app-chain makes matching part of the chain itself, so it can also own a step at the start and the end of every block.
Other venues took the app-chain path in their own ways. On dYdX v4, each full node keeps an in-memory order book, and block proposers build blocks from the matches in their local book (dYdX docs). Injective's exchange module runs order book management, matching and settlement on chain (Injective docs). Hyperliquid's HyperCore holds margin and matching engine state without off-chain order books (Hyperliquid docs).
On NeutronX, every order is a transaction in a block, the book lives in chain state, and every validator recomputes the matching.
Architecture overview

Order path: a wallet signs an order transaction, every node checks it in the mempool, validators attach oracle prices to their votes, the proposer matches the orders on a snapshot of the book and puts the result first in the block, every validator re-runs and compares it, the committed block writes prices and trades, then other transactions, then the end-of-block risk engine, and events flow to the indexer, API and UI
An order goes through these steps:
The wallet signs an order transaction. It is an ordinary transaction, like a transfer.
Every node checks the order before it puts the transaction in its mempool.
Validators attach oracle prices to their votes. CometBFT vote extensions carry each validator's prices with its vote for the block.
The proposer builds the block. In PrepareProposal it aggregates the prices, matches the orders on a snapshot of the book and puts the result first in the block.
Every validator checks it. In ProcessProposal each validator re-runs the matching and compares. Any difference rejects the block before any state is written.
The block is committed. In FinalizeBlock the prices and trades are written to chain state first, then the other transactions run, then the end-of-block risk engine.
Events reach the indexer, which serves the API and the UI.
Blocks average about 0.85 s on mainnet, measured in November 2026. A block is final as soon as it is committed: there are no reorgs, so a trade is final in the block that included its order.
The proposer can't invent a trade, a price, an order or a cancel. The block names only signed transactions of the same block, and a result that differs from what the validators compute rejects the block. The proposer does decide one thing, the order of transactions in its block; more on that under trade-offs.
What happens inside one block

Inside one block, about 0.85 s on mainnet: at the start the oracle prices are recorded and the batch runs cancels, new orders, the queue by position in the block, resting orders, the cross of the books, then market and FOK orders; then the other transactions; at the end marks are set from fresh oracle prices, markets whose price came back reopen, liquidations run through the book with ADL behind them, then TP/SL, trailing stops, TWAP and POV, funding, the books are checked against chain state and invariants run last
The matching of a block runs in a fixed order:
Cancels.
New orders are admitted, each with a margin check.
Admitted orders queue by their position in the block.
Limit orders rest in the book.
The books cross.
Market and fill-or-kill orders take liquidity.
Unfilled IOC and FOK remainders are cleared.
Every loop in this pass has a bounded number of steps.
Priority is price, then time. At one price level, an order from an earlier block goes first, and inside a block the earlier position in the block goes first. No field the user picks moves an order in the queue. A trade happens at the price of the order it fills against, usually the maker's, never at the mark price.
After the other transactions, the end of the block runs its own steps:
Fresh oracle prices become the marks.
Funding samples are taken.
A market whose price came back reopens.
Liquidations run through the book, with ADL behind them.
Delisting settlements finish.
TP/SL, trailing stops, conditional entries, TWAP and POV orders run.
Funding is charged.
The books are checked against chain state and rebuilt where needed.
Invariants run last.
One reason for this order is worth spelling out: a market reopens before the liquidations, so a liquidation on it goes through the book and not straight to ADL. A broken invariant halts trading, not the chain, and cancels and reduce-only orders still work while trading is halted.
Determinism
Every validator has to reach the same state from the same block, bit for bit. A few rules make that possible.
The book lives in chain state. Every order and a price-sorted index sit in the chain's store, which is the source of truth. Each node also keeps a copy of the book in memory for speed. That copy is checked against state every block and rebuilt when it differs and after a restart.
Numbers are fixed-point only. Prices, quantities, margin and funding are decimals with 18 places. Amounts are integers in the smallest units, so USDC has 6 decimals. The consensus code has no floating-point numbers, no map iteration without sorting, no system clock and no randomness.
Here is the price-time rule at one level of the book:
// simplified: one price level, price-time priority
// makers come oldest first: earlier block, then earlier place in it
for maker := range level.oldestFirst() {
fill := min(taker.remaining, maker.remaining)
trade(maker.price, fill) // the maker's price, never the mark
if taker.remaining == 0 {
break
}
}
We check it three ways. Every block, each validator fully recomputes the matching and compares it. Tests run the same inputs again and expect the same result. Before a release, we replay real mainnet state on two nodes, one on the old binary and one on the new, and compare the app hash of every block.
An internal audit once found a class of bug worth sharing. A local query to one node could change that node's in-memory state, so the block result on that node could drift from the rest of the network. A node in that state stops, and the network goes on without it. We fixed the class with a journal of in-memory changes kept within each transaction. There was no incident on mainnet.
The second lesson came from restarts. If every node makes the same mistake after a restart, the app hash still matches, and nothing looks wrong. So a restart needs tests of its own.
Risk checks in consensus
Oracle. Prices arrive through CometBFT vote extensions. On each validator, a price service collects quotes from several exchanges. The proposer computes a stake-weighted median, and every validator recomputes it. A vote far from the median is recorded as evidence against its validator. Without a quorum of stake a pair's price is not updated, and a sharp jump puts the pair on a circuit breaker.
Mark price and funding. The mark price used for margin, liquidations and TP/SL is that oracle price. For funding the index is the oracle price, and the premium comes from the depth of the book (impact price), as on dYdX v4 (dYdX docs). Funding accrues only while the chain and the market are running. The funding rate guide covers the trader's side.
Margin and subaccounts. An address can hold up to 128 subaccounts, a governance parameter. A subaccount is cross-margined, and isolation means a separate subaccount; see cross vs isolated margin. An opening order is checked against equity at fresh marks when it is admitted, not later. Trades between subaccounts of the same owner are refused.
Liquidations. The chain itself runs them, at the end of the same block that brought the price. They go through the book first, the rest through ADL, and the insurance fund covers a deficit. Liquidations keep running even while trading is halted. How liquidation works explains the steps from a trader's view.
Trade-offs
Ordering. The proposer decides the order of transactions in its block, as on dYdX v4 (dYdX docs) and other CometBFT chains. So a proposer can technically put its own order ahead of someone else's within its block. What limits it: the proposer changes by stake, it can't invent a trade, the mempool is public, trades with yourself are refused, and every trade and its order are visible in chain events.
The validator set today. The chain runs 4 validators, all operated by the team. The chain's parameters allow up to 60.
Governance and upgrades. Exchange parameters change only through on-chain governance votes, and upgrades go through governance proposals. No key changes exchange parameters or touches user funds. Governance does assign some roles, for example for promo programs.
Work and state. Every validator recomputes every block's matching, so the work grows with the load on the network. The book lives in chain state, so state grows with it.
Build on it
The chain and its data are public, and you can read the order book directly through REST and gRPC:
RPC:
https://rpc.neutronchain.ioREST:
https://api.neutronchain.iogRPC:
grpc.neutronchain.io:443Indexer API:
https://indexer.neutronchain.io/api/v1Explorer:
https://explorer.neutronchain.ioDocs:
https://docs.neutronx.com
Live on mainnet today: 14 perpetual markets quoted in USDC; limit and market orders with GTC, IOC, FOK, post-only and reduce-only; TP/SL, trailing stops, TWAP and POV that the chain itself runs at the end of the block; and leader vaults.
FAQ
Is the order book fully on-chain?
Yes. Orders are signed transactions in blocks, the book lives in chain state, and matching runs in the chain's state machine: the block proposer computes it and every validator re-runs it before accepting the block. The indexer is a read-only mirror of the chain.
Can validators front-run orders?
The proposer of a block decides the order of transactions in that block, as CometBFT lets every proposer do, so it can place its own order ahead of others within its block. It cannot invent a trade, a price or a cancellation: every validator re-runs the matching and rejects a block that differs. The proposer changes every block by stake, trades between subaccounts of the same owner are refused, and every trade with its order is public on-chain. Today the chain runs 4 validators, all operated by the team.
How fast is order matching?
An order is matched in the block that includes it. Blocks average about 0.85 s on mainnet, measured in November 2026, and a committed block is final: there are no reorgs.
Can I query the order book myself?
Yes. Anyone can read the order book, markets and positions through the public RPC, REST and gRPC endpoints and the indexer API, without an account.