Skip to content

fix(math): correct distribute_fees fee split at extreme values — senior no longer zeroed (closes #120) - #133

Closed
1Jarvis42069 wants to merge 1 commit into
dcccrypto:mainfrom
1Jarvis42069:fix/distribute-fees-overflow
Closed

fix(math): correct distribute_fees fee split at extreme values — senior no longer zeroed (closes #120)#133
1Jarvis42069 wants to merge 1 commit into
dcccrypto:mainfrom
1Jarvis42069:fix/distribute-fees-overflow

Conversation

@1Jarvis42069

Copy link
Copy Markdown
Contributor

Closes #120.

In distribute_fees's overflow path, part2 = floor(r * junior_weight / total_weight) was computed as r.checked_mul(junior_weight).map(|p| p / total_weight).unwrap_or(total_fee as u128). With r < total_weight (~2^81) and junior_weight up to ~2^80, the product r * junior_weight (~2^161) overflows u128, so checked_mul returns None and the fallback unwrap_or(total_fee) hands the junior tranche 100% of the fee — 0% to the protected senior tranche.

This replaces that fallback with an exact, overflow-safe mul_div_floor(r, junior_weight, total_weight) (full 256-bit product via u64 limbs, then bitwise long division). part1 is unchanged — it's already bounded by junior_weight <= total_weight, so it can't overflow.

Verification:

  • Added test_distribute_fees_overflow_proportional with expected splits computed via arbitrary-precision integers, e.g. symmetric u64::MAX balances at 5x → junior 15372286728091293012 (~5/6), senior 3074457345618258603 (~1/6) — previously junior got 100%.
  • Added test_mul_div_floor_matches_native_when_product_fits (fast-path agreement + a slow-path sanity case).
  • Full cargo test passes — including proptest_math (17) and proptest_extended (34); no property regressed.

Note: the kani.rs harness is unaffected by this change (conservation + senior-protection still hold), but worth re-running it on your side as a belt-and-suspenders check.

…dcccrypto#120)

In the overflow path, part2 computed r * junior_weight / total_weight, but
r (< total_weight, ~2^81) * junior_weight (~2^80) overflows u128, and the
unwrap_or(total_fee) fallback then handed the junior tranche 100% of the fee
(0% to the protected senior tranche).

Compute part2 with an exact, overflow-safe 256-bit mul-div (mul_div_floor:
full 256-bit product via u64 limbs, then bitwise long division). Added unit
tests with arbitrary-precision-verified expected splits for the overflow cases
(symmetric/junior-heavy/senior-heavy/mid). Full suite incl. proptest_math passes.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Nullguy42069, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 24 minutes and 41 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, 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 include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 28125cbd-64be-4d54-8a4c-dcdb9f7bfa10

📥 Commits

Reviewing files that changed from the base of the PR and between 1bee206 and d96756c.

📒 Files selected for processing (1)
  • src/math.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

Copy link
Copy Markdown
Owner

Integrated into main via #142 (overflow-safe mul_div_floor in distribute_fees). Cherry-picked cleanly with attribution. Closes #120. Thanks!

@dcccrypto dcccrypto closed this Jun 17, 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.

math: distribute_fees double-overflow fallback gives 100% to junior at extreme u64 values

2 participants