Series COCOMSS Deck Log

~2 min read

Building an Order Book and Ledger Inside COCOMSS

COCOMSS only consumed exchange prices. This leg turned toward the hardest correctness problem in an exchange backend. It hand-built a price-time-priority order book and double-entry ledger, dropped matching latency to nanoseconds in Rust, and stood up HMAC-signed orders, settlement, and custody state machines in a sandbox. Real funds and regulation were honestly marked as out-of-code.

UnderwayExchangeRustMatching EngineBackend

Log

This leg began with a question I put to myself. "Does this service only consume exchange prices, or can it build exchange core?" I decided to answer in code.

  • The matching engine is a hand-built price-time-priority order book (CLOB). Best price comes first and the same price is FIFO (per-level queues). It supports limit·market orders, partial fills, IOC/GTC, and cancels. Money math uses no float and all Decimal. A floating-point error is an asset vanishing
  • The double-entry ledger reserves the needed asset on order and settles it to the counterparty on fill. Total assets across all accounts must be invariant before and after
  • Invariants are enforced as tests. 8-seed × thousands-of-orders fuzzing checks, on every order, ① value conservation ② no crossed book ③ no negative balance. Be correct before being fast
  • The Rust high-frequency engine took on latency. Python handled correctness and Rust handled latency. It uses integer fixed-point, a single-threaded hot path (no contention, no locks), and a lock-free ring buffer. Measured matching latency is p50 42ns · p99 167ns · 13.45M orders/s
  • Private·settlement·custody run in a sandbox. API key/secret carry HMAC signatures and nonce replay protection. It has an append-only audit ledger with reconciliation, a withdrawal state machine (requested→approved→broadcast→confirmed, illegal transitions blocked), and hot/cold separation

Judgment

Key point

There was a reason to go from consuming prices to the core. Assessing myself, COCOMSS had ample evidence of "builds services in the exchange ecosystem" but no evidence of "can build exchange core." Matching and settlement can be proven by correctness alone, without real trades or funds, so that is where I went.

"Not wrong" came first and "fast" came second. A one-nanosecond matcher is meaningless if assets leak. So more time went into planting the same invariants in both Python and Rust and hammering them with fuzzing than into the flashy Rust numbers. Understanding in code why LMAX runs single-threaded ("no contention, no need for locks") was also a gain of this work.

I also made the honest boundary clear. This is proof of the core logic an exchange runs. Real-key·HSM·multisig custody, actual chain broadcast, and travel rule·KYC need real funds and legal work. Those areas are not solved by writing more code and need organization, licensing, and audit first, so I left them as a boundary.

Charted Ahead

  • Per-market sharding and a replicated state machine for the matching engine need real infra
  • Remove the Rust engine's tail latency (allocation·OS jitter): slab pre-allocation, thread pinning
  • An external load balancer (ALB) will absorb nginx/host failures. The IaC (Terraform) is already drawn and will be applied when account and budget are ready

Related

Portfolio'COCOMSS' — Crypto AI Portfolio AdvisorOperationsCOCOMSS — A crypto AI portfolio advisor that ran learn → understand → verify → decide·practice, down to hand-built exchange-core internalsJournalCOCOMSS Confirms Its Recommendation Engine Failed and Turns to 2027 TaxationJournalFour Projects Into One COCOMSS