MPT State Layer
A Merkle Patricia Trie that provides cryptographic state commitments without impacting match latency.
What Is an MPT?
A Merkle Patricia Trie (MPT) is a data structure that combines a prefix trie (for efficient key lookups) with a Merkle tree (for cryptographic hashing). Every node in the trie is identified by the hash of its contents, and the root hash is a compact cryptographic commitment to the entire state — all account balances, all open orders, all ledger entries. Changing any value anywhere in the trie produces a completely different root hash.
Vela uses the MPT to produce state roots that are published to the data availability layer after each committed batch. These roots allow any verifier to confirm that the operator computed the correct state, without needing to store the full state themselves.
In-Memory Cache
Trie traversal is expensive — a naive MPT read or write involves hashing multiple trie nodes and potentially disk I/O. Performing these operations on the critical path of order matching would add unacceptable latency.
To avoid this, all reads and writes during order matching go through an in-memory HashMap cache that mirrors the relevant portions of the trie. The matching engine reads and writes the HashMap directly, with zero trie traversal. The trie itself is updated only at batch commit time, after the engine has finished processing the batch.
Batch Commit Process
- The matching engine sends a
CommitBatchto the committer channel. - The committer receives the batch delta — a list of key-value changes (account balances, order states).
- The committer applies the delta to the MPT.
- The new MPT root is computed.
- The root and batch data are published to the DA backend.
Steps 3–5 happen asynchronously, entirely decoupled from the engine's main loop. The engine continues processing new requests immediately after sending the CommitBatch message.
State Keys
The MPT stores the following key types:
| Key prefix | Value |
|---|---|
| balance:{address}:{asset} | Available and locked balance (u128, u128) |
| order:{order_id} | Full order state (price, quantity, status, etc.) |
| nonce:{address} | Last used nonce for replay protection |
| credit:{address} | Credit ratio and current utilization |