Skip to content

fix: include total_returned in FlushToInsurance available-balance guard - #92

Closed
0x-SquidSol wants to merge 1 commit into
dcccrypto:masterfrom
0x-SquidSol:fix/flush-available-excludes-total-returned
Closed

fix: include total_returned in FlushToInsurance available-balance guard#92
0x-SquidSol wants to merge 1 commit into
dcccrypto:masterfrom
0x-SquidSol:fix/flush-available-excludes-total-returned

Conversation

@0x-SquidSol

Copy link
Copy Markdown
Contributor

Summary

process_flush_to_insurance computed the available-to-flush balance as:

available = total_deposited - total_withdrawn - total_flushed

but omitted + total_returned.

Impact

After AdminWithdrawInsurance runs (incrementing total_returned), the vault physically holds those returned tokens again, but the old formula still subtracted the full total_flushed without adding back total_returned. This causes:

  1. Incorrect capacity reporting — valid flushes are rejected even when tokens are present in the vault.
  2. Spurious Overflow — if total_flushed > total_deposited - total_withdrawn after a partial return cycle, checked_sub returns NoneErr(Overflow), permanently blocking FlushToInsurance even though the vault is solvent.

A pool that has completed one flush+return cycle can never re-flush insurance capital without triggering this error, locking the admin out of a core operational action.

Fix

Add .checked_add(pool.total_returned) to match the formula already used by total_pool_value():

let available = pool.total_deposited
    .checked_sub(pool.total_withdrawn)
    .and_then(|v| v.checked_sub(pool.total_flushed))
    .and_then(|v| v.checked_add(pool.total_returned))  // ← added
    .ok_or(StakeError::Overflow)?;

Test plan

  • cargo test passes (57 tests)
  • Manual trace: deposited=1000, withdrawn=0, flushed=1000, returned=500 → old formula: underflow → new formula: available=500 ✓

🤖 Generated with Claude Code

The available-to-flush calculation was:
  total_deposited - total_withdrawn - total_flushed

It omitted + total_returned.  After AdminWithdrawInsurance is called,
total_returned grows (tokens re-enter the vault), but total_flushed is not
reduced.  The old formula therefore:
  - Reports too-low available capacity (preventing valid flushes after a
    partial insurance return).
  - Underflows with Err(Overflow) if total_returned pushes the net below
    what the subtraction can represent, permanently blocking FlushToInsurance
    even when the vault holds real tokens.

Fix: add .checked_add(pool.total_returned) to mirror the formula used by
total_pool_value(), which correctly accounts for all four counters.

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

coderabbitai Bot commented Apr 1, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@0x-SquidSol has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 16 minutes and 2 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 16 minutes and 2 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5881edce-0a2f-46f6-a9de-436052aa4797

📥 Commits

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

📒 Files selected for processing (1)
  • src/processor.rs
✨ 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 — Include total_returned in FlushToInsurance available-balance guard. Prevents flushing more than available. Security APPROVED ✅

@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 APPROVED. Includes total_returned in flush available balance calculation. Original formula omitted returned tokens, causing false InsufficientFunds after AdminWithdrawInsurance. Now matches total_pool_value() accounting.

@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