Skip to content

fix: use checked_add for total_fee_requested in process_delegation_cleanup#180

Open
amathxbt wants to merge 1 commit intomagicblock-labs:mainfrom
amathxbt:fix/undelegate-fee-overflow
Open

fix: use checked_add for total_fee_requested in process_delegation_cleanup#180
amathxbt wants to merge 1 commit intomagicblock-labs:mainfrom
amathxbt:fix/undelegate-fee-overflow

Conversation

@amathxbt
Copy link
Copy Markdown

@amathxbt amathxbt commented May 3, 2026

Summary

Fixes an unchecked arithmetic overflow in process_delegation_cleanup (src/processor/fast/undelegate.rs).


Bug — Plain + on commit_fee + SESSION_FEE_LAMPORTS can overflow

// Before
let commit_count = delegation_last_update_nonce.saturating_sub(1);
let commit_fee = COMMIT_FEE_LAMPORTS
    .checked_mul(commit_count)
    .ok_or(DlpError::Overflow)?;  // ← already protected
let total_fee_requested = commit_fee + SESSION_FEE_LAMPORTS;  // ← NOT protected

The per-commit fee multiplication is correctly protected with checked_mul, but the subsequent addition of the constant SESSION_FEE_LAMPORTS uses a plain + operator. For a long-lived delegation with a very large commit_count, commit_fee can approach u64::MAX. Adding SESSION_FEE_LAMPORTS on top then overflows, wrapping around to a small value. This causes the protocol to collect far less than the intended fee, letting the validator escape the session fee entirely.

Example: If COMMIT_FEE_LAMPORTS * commit_count = u64::MAX - 1 and SESSION_FEE_LAMPORTS = 5000, the result wraps to 4998 instead of the expected value, and total_fee_requested.min(total_lamports) silently charges the wrong amount.


Fix

// After
let total_fee_requested = commit_fee
    .checked_add(SESSION_FEE_LAMPORTS)
    .ok_or(DlpError::Overflow)?;

Consistent with every other arithmetic operation in this codebase.


Impact

  • Severity: High — overflow in fee calculation causes under-collection of protocol/session fees
  • Affected component: delegation-program / src/processor/fast/undelegate.rs

…eanup

In process_delegation_cleanup the per-commit fee (already protected by
checked_mul) was added to SESSION_FEE_LAMPORTS with a plain + operator:

    let total_fee_requested = commit_fee + SESSION_FEE_LAMPORTS;

For a long-lived delegation with a very large commit_count the result of
COMMIT_FEE_LAMPORTS * commit_count can approach u64::MAX. Adding the
constant SESSION_FEE_LAMPORTS on top of such a value overflows, wrapping
around to a small number and causing the protocol to charge far less than
it should, letting the validator escape paying the session fee.

Fix: use checked_add(...).ok_or(DlpError::Overflow) consistent with every
other arithmetic operation in this file.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 3, 2026

Warning

Rate limit exceeded

@amathxbt has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 59 minutes and 49 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ 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: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 5a858ec2-3472-4f2f-abee-3d97d2fcca6c

📥 Commits

Reviewing files that changed from the base of the PR and between f555df8 and ac1a4fd.

📒 Files selected for processing (1)
  • src/processor/fast/undelegate.rs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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
Review rate limit: 0/1 reviews remaining, refill in 59 minutes and 49 seconds.

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

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