Skip to content

Add worst-case critbit tree depth tests to order book - #164

Merged
mikemaccana merged 2 commits into
mainfrom
claude/peaceful-tesla-k0gp10
Sep 23, 2026
Merged

mikemaccana merged 2 commits into
mainfrom
claude/peaceful-tesla-k0gp10

Conversation

@mikemaccana

@mikemaccana mikemaccana commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A critbit tree does not rebalance. Asks at doubling prices each set a new highest price bit, so each one adds a level to the path to the best ask. The price fills the top 64 bits of the 128-bit key, so prices can add at most 64 levels, and no path can exceed 128. This adds tests that build that worst case and measure what it costs, in both Anchor copies.

Tests (Anchor v2 and Anchor v1)

  • doubling_prices_build_the_deepest_path_prices_allow: four funded sellers place 63 asks at 2^1 through 2^63 (at most 20 open orders each). Two probe asks then go in at price 1. The test reads the asks tree from the account's bytes and asserts that the best ask is 64 inner nodes deep, and 1 deep on a book holding only the probes.
  • deepest_path_adds_little_compute_to_insert_fill_and_cancel: runs the same insert, fill, and cancel at the bottom of that path and on a shallow book. It asserts that each costs under 15,000 extra compute units and fits the default 200,000-unit instruction budget.

Measured, in compute units:

v2 shallow v2 depth 64 v1 shallow v1 depth 64
insert 13,715 24,253 26,612 38,754
fill 24,380 35,444 31,283 41,941
cancel 2,816 10,844 8,925 16,871

Other changes

  • The v1 copy gains a solana-transaction dev-dependency, at the version solana-kite already builds against, so the test can read each transaction's compute units. solana-kite's send helper doesn't return them.
  • Both READMEs no longer say the tree "stays shallow no matter what order keys arrive in", or that Phoenix uses the same structure. Phoenix uses a red-black tree. The expected test output there was stale and is brought up to date.
  • CHANGELOG entries at the root and in both copies.

Compatibility with #158 (eviction)

I merged this branch onto #158's head in a scratch worktree and ran the v2 suite: 34 of 34 pass. With eviction, the deep fill costs 13,736 extra units, still under the 15,000 limit. The textual conflicts in the tests, READMEs, and CHANGELOGs come from both branches appending to the same files. Whichever of the two merges second needs to keep both sides.

cargo test passes in finance/order-book/anchor and finance/order-book/anchor-v1: 29 of 29 each.

🤖 Generated with Claude Code

https://claude.ai/code/session_01RQ5RZgoQjVK36qSjqUSq6X

Mike MacCana and others added 2 commits September 23, 2026 02:34
A critbit tree does not rebalance, so asks at doubling prices stretch the
path to the best ask by one level each. The price fills the top 64 bits of
the 128-bit key, so prices can add at most 64 levels. Both Anchor copies gain
two tests: one builds that 64-level path and reads its depth from the
account, the other checks that insert, fill, and cancel at the bottom of it
each cost under 15,000 compute units more than on a shallow book and fit
the default 200,000-unit budget.

Measured, Anchor v2: insert 13,715 -> 24,253 CU, fill 24,380 -> 35,444,
cancel 2,816 -> 10,844. Anchor v1: insert 26,612 -> 38,754, fill
31,283 -> 41,941, cancel 8,925 -> 16,871.

The README no longer claims the tree stays shallow whatever order keys
arrive in, or that Phoenix uses the same structure (it uses a red-black
tree), and its expected test output is brought up to date.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RQ5RZgoQjVK36qSjqUSq6X
…t notes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RQ5RZgoQjVK36qSjqUSq6X
@mikemaccana
mikemaccana merged commit 99c410d into main Sep 23, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant