Skip to content

fix: batched address tree init assert tree and queue index are the same#2318

Open
ananas-block wants to merge 5 commits intomainfrom
jorrit/fix-batched-address-tree-init
Open

fix: batched address tree init assert tree and queue index are the same#2318
ananas-block wants to merge 5 commits intomainfrom
jorrit/fix-batched-address-tree-init

Conversation

@ananas-block
Copy link
Contributor

@ananas-block ananas-block commented Feb 28, 2026

Summary by CodeRabbit

  • Bug Fixes

    • Ensured batched address creation uses a single, consistent index so related addresses reference the same account, preventing mismatched derivations.
    • Strengthened validation to require the queue index to match the merkle-tree index and to reject operations when they differ.
    • Fixed assigned address index handling to respect the provided merkle-tree index instead of defaulting to zero.
  • Tests

    • Updated tests and assertions to reflect the new index-consistency behavior.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 28, 2026

📝 Walkthrough

Walkthrough

BatchedAddressTree path: compute the tree index into a local variable, assign both tree_index and queue_index from it, and add a validation that address_queue_index == address_merkle_tree_account_index, returning SystemProgramError::AddressMerkleTreeAccountDiscriminatorMismatch if they differ. In zero-copy packing, address_queue_account_index is initialized from address_merkle_tree_account_index instead of 0. Tests updated accordingly.

Changes

Cohort / File(s) Summary
Batched address derivation
programs/system/src/processor/create_address_cpi_data.rs
Compute local tree_index and set both tree_index and queue_index from it; add guard ensuring address_queue_index == address_merkle_tree_account_index, returning AddressMerkleTreeAccountDiscriminatorMismatch on mismatch.
Zero-copy assignment update
program-libs/compressed-account/src/instruction_data/zero_copy_set.rs
Change address_queue_account_index initialization to use provided address_merkle_tree_account_index (was constant 0) for v2 address trees; packing logic otherwise unchanged.
Tests updated to match new invariant
program-libs/compressed-account/tests/zero_copy_set.rs
Adjust expectations: address_queue_account_index now mirrors address_merkle_tree_account_index (e.g., 10, 20, 30 in successive sets) instead of remaining 0; assertions and commentary updated accordingly.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

ai-review

Suggested reviewers

  • sergeytimoshin
  • SwenSchaeferjohann

Poem

Two indices now share one small seed,
A local value guides their deed.
A sentinel checks they match in line,
So Merkle queues and trees align. 🌲✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding validation to assert that tree and queue indices match in batched address tree initialization.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 70.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch jorrit/fix-batched-address-tree-init

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@programs/system/src/processor/create_address_cpi_data.rs`:
- Around line 86-94: Move the index-equality validation for batched address
trees to run immediately on entering the BatchedAddressTree match arm (i.e.,
before calling get_index_or_insert, set_address_fee, or
insert_address_sequence_number): check that
new_address_params.address_queue_index() ==
new_address_params.address_merkle_tree_account_index() and return an appropriate
error on mismatch. Replace or clarify the current
AddressMerkleTreeAccountDiscriminatorMismatch usage—either use a new error
variant like AddressQueueIndexMismatch or add a comment/documentation in the
error definition explaining the reuse—so the intent is clear when locating the
check in the BatchedAddressTree branch.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b5f7f10 and ad0d643.

📒 Files selected for processing (1)
  • programs/system/src/processor/create_address_cpi_data.rs

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
program-libs/compressed-account/tests/zero_copy_set.rs (1)

39-39: ⚠️ Potential issue | 🟡 Minor

Update the header docstring to reflect the new invariant.

The docstring still states "Invariant: address queue account index always 0", but the actual invariant (as documented in the updated test at lines 583-584) is now address_queue_account_index == address_merkle_tree_account_index. This mismatch will confuse future readers.

📝 Proposed fix
-///    - Invariant: address queue account index always 0
+///    - Invariant: address queue account index equals address merkle tree account index (v2 batched trees)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@program-libs/compressed-account/tests/zero_copy_set.rs` at line 39, Update
the header docstring in zero_copy_set.rs to replace the outdated invariant text
"Invariant: address queue account index always 0" with the new invariant
"Invariant: address_queue_account_index == address_merkle_tree_account_index" so
the comment matches the actual test logic; locate the header comment near the
top of the file and modify the string accordingly, ensuring the exact symbol
names address_queue_account_index and address_merkle_tree_account_index are used
in the docstring.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@program-libs/compressed-account/tests/zero_copy_set.rs`:
- Line 39: Update the header docstring in zero_copy_set.rs to replace the
outdated invariant text "Invariant: address queue account index always 0" with
the new invariant "Invariant: address_queue_account_index ==
address_merkle_tree_account_index" so the comment matches the actual test logic;
locate the header comment near the top of the file and modify the string
accordingly, ensuring the exact symbol names address_queue_account_index and
address_merkle_tree_account_index are used in the docstring.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d8620fe and a0783bd.

⛔ Files ignored due to path filters (1)
  • sdk-tests/csdk-anchor-full-derived-test/tests/integration_tests.rs is excluded by none and included by none
📒 Files selected for processing (1)
  • program-libs/compressed-account/tests/zero_copy_set.rs

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