Skip to content

fix: reject FlushToInsurance on trading LP pools (pool_mode == 1) - #69

Closed
0x-SquidSol wants to merge 1 commit into
dcccrypto:masterfrom
0x-SquidSol:fix/flush-to-insurance-pool-mode-guard
Closed

fix: reject FlushToInsurance on trading LP pools (pool_mode == 1)#69
0x-SquidSol wants to merge 1 commit into
dcccrypto:masterfrom
0x-SquidSol:fix/flush-to-insurance-pool-mode-guard

Conversation

@0x-SquidSol

@0x-SquidSol 0x-SquidSol commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Summary

FlushToInsurance moves collateral from the stake vault to the wrapper insurance fund. This is designed for insurance LP pools (mode 0) only.

On trading LP pools (mode 1), AccrueFees computes pool_value = total_deposited - total_withdrawn + total_fees_earned without subtracting total_flushed. After any flush, current_vault_balance < pool_value permanently, stopping all fee accrual and breaking withdrawals.

Adds pool_mode == 0 guard before vault-balance check.

Test plan

  • All existing tests pass
  • FlushToInsurance returns InvalidPoolMode on mode-1 pool

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced pool mode validation for flush-to-insurance operations, preventing execution in unsupported pool states.

FlushToInsurance moves collateral from the stake vault to the wrapper
insurance fund via CPI TopUpInsurance. This operation is designed for
insurance LP pools (mode 0) where the vault buffer and insurance fund
are the same conceptual reserve.

For trading LP pools (mode 1), flushing creates a permanent accounting
inconsistency:

1. total_flushed increases but AccrueFees computes pool_value as:
   total_deposited - total_withdrawn + total_fees_earned
   (does NOT subtract total_flushed)
2. The actual vault balance drops below the accounting-derived pool_value,
   so AccrueFees would never report fees (current_balance < pool_value
   always) after even a small flush.
3. LP holders can no longer withdraw their full share, as the vault lacks
   sufficient collateral even though pool_value says otherwise.

Adds pool_mode == 0 check before the vault-balance guard.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Apr 1, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2cef948f-dfd5-4ee9-b364-11bc428fdfe3

📥 Commits

Reviewing files that changed from the base of the PR and between 1881738 and 900b9ad.

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

📝 Walkthrough

Walkthrough

A pool mode precondition was added to the process_flush_to_insurance function. The function now rejects FlushToInsurance operations when pool mode is not 0, returning an InvalidPoolMode error before proceeding with vault balance verification and CPI execution.

Changes

Cohort / File(s) Summary
Pool Mode Validation Guard
src/processor.rs
Added a precondition check in process_flush_to_insurance that rejects operations when pool.pool_mode != 0, returning StakeError::InvalidPoolMode before executing vault balance verification and CPI logic.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 The pool mode must be zero, you see,
Before flushing to insurance with glee,
A guard stands firm at the door,
Protecting the vault evermore,
Now only valid modes get through, wee! ✨

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the problem, the solution, and includes a test plan, but does not follow the repository's required template structure with Summary/How to test/Checklist sections. Restructure the description to match the template: use 'Summary', 'How to test', and 'Checklist' sections with the required testing commands (cargo test, clippy, fmt, and Kani if applicable).
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: rejecting FlushToInsurance on trading LP pools (pool_mode == 1), which matches the core purpose of the PR.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@dcccrypto dcccrypto left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Security review — Rejects FlushToInsurance on trading LP pools (pool_mode == 1). Prevents insurance drain from wrong pool type. Security APPROVED ✅

@dcccrypto

Copy link
Copy Markdown
Owner

Superseded by PR#95 (merged) and PR#96 (omnibus test coverage). All changes in this PR are already in master. Closing as part of PERC-8433 cleanup.

@dcccrypto

Copy link
Copy Markdown
Owner

Implemented in PR #98 and merged to master. Thank you for identifying this vulnerability — the fix was applied based on your diff after two rounds of security review.

@dcccrypto dcccrypto closed this Apr 5, 2026
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