Add worst-case critbit tree depth tests to order book - #164
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
Other changes
solana-transactiondev-dependency, at the versionsolana-kitealready builds against, so the test can read each transaction's compute units.solana-kite's send helper doesn't return them.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 testpasses infinance/order-book/anchorandfinance/order-book/anchor-v1: 29 of 29 each.🤖 Generated with Claude Code
https://claude.ai/code/session_01RQ5RZgoQjVK36qSjqUSq6X