Skip to content

fix: v2 tree rollover balance check#2278

Merged
ananas-block merged 1 commit intomainfrom
jorrit/fix-v2-tree-rollover-balance-check
Feb 12, 2026
Merged

fix: v2 tree rollover balance check#2278
ananas-block merged 1 commit intomainfrom
jorrit/fix-v2-tree-rollover-balance-check

Conversation

@ananas-block
Copy link
Contributor

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

Summary by CodeRabbit

  • Bug Fixes
    • Improved stability of rollover operations by refining readiness validation checks and eliminating potential panic conditions.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 12, 2026

📝 Walkthrough

Walkthrough

Two rollover operation functions in the batched-merkle-tree library have updated their rent-exemption readiness checks. The address tree rollover now uses a threshold comparison instead of equality, and the state tree rollover uses a different inequality formulation. Both changes affect when the NotReadyForRollover error is triggered.

Changes

Cohort / File(s) Summary
Rollover Rent-Exemption Checks
program-libs/batched-merkle-tree/src/rollover_address_tree.rs, program-libs/batched-merkle-tree/src/rollover_state_tree.rs
Modified rent-exemption validation logic for rollover readiness. Address tree check changed from equality (with unwrap risk) to threshold-based comparison (old_lamports >= 2 * new_mt_rent). State tree check reformulated from equality to inequality boundary (lamports < reimbursement_for_rent + queue_rent), affecting NotReadyForRollover error conditions.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • sergeytimoshin

Poem

💰 Rent checks refined with sharper sight,
No more equality's brittle plight,
Thresholds now guide the rollover way,
Safer boundaries for Solana's day! ✨

🚥 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 clearly and specifically identifies the main change: fixing v2 tree rollover balance check logic across two files.
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
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch jorrit/fix-v2-tree-rollover-balance-check

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.

Caution

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

⚠️ Outside diff range comments (1)
program-libs/batched-merkle-tree/src/rollover_state_tree.rs (1)

98-106: 🧹 Nitpick | 🔵 Trivial

Consider moving this check before rollover_batched_state_tree to fail fast.

In the address-tree file, you moved the balance gate before loading/mutating the old tree (line 22 of rollover_address_tree.rs). Here, the rent values (merkle_tree_rent, queue_rent, additional_bytes_rent) are all known by line 79 — well before the rollover call on line 99. Moving the check earlier would short-circuit the transaction sooner, saving compute units on the unhappy path and keeping the two rollover functions structurally consistent.

Suggested reorder
+    let reimbursement_for_rent = merkle_tree_rent + queue_rent + additional_bytes_rent;
+    // 5. Check that queue account has sufficient balance for rollover.
+    #[cfg(target_os = "solana")]
+    if old_output_queue.lamports() < reimbursement_for_rent.saturating_add(queue_rent) {
+        return Err(MerkleTreeMetadataError::NotReadyForRollover.into());
+    }
+
     let new_mt_data = &mut new_state_merkle_tree.try_borrow_mut_data()?;
     let params = RolloverBatchStateTreeParams {
         ...
     };
 
-    // 5. Rollover the old Merkle tree and queue to new Merkle tree and queue.
+    // 6. Rollover the old Merkle tree and queue to new Merkle tree and queue.
     rollover_batched_state_tree(params)?;
-    let reimbursement_for_rent = merkle_tree_rent + queue_rent + additional_bytes_rent;
-    // 6. Check that queue account is rent exempt post rollover.
-    #[cfg(target_os = "solana")]
-    if old_output_queue.lamports() < reimbursement_for_rent.saturating_add(queue_rent) {
-        return Err(MerkleTreeMetadataError::NotReadyForRollover.into());
-    }
     Ok(reimbursement_for_rent)

@ananas-block ananas-block merged commit 2cde4dd into main Feb 12, 2026
34 checks passed
@ananas-block ananas-block deleted the jorrit/fix-v2-tree-rollover-balance-check branch February 12, 2026 17:07
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.

2 participants